Skip to content

Commit

Permalink
静态资源嵌入,一体化处理
Browse files Browse the repository at this point in the history
  • Loading branch information
soonio committed Aug 16, 2024
1 parent 464ce2b commit 6335f20
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ custom.json
swagger
go.sum
Makefile
index.html
index.html

swagger-*
29 changes: 26 additions & 3 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,16 +1,39 @@
# swagger-ui

## V5 使用golang,一键构建
## Golang一键构建
```bash
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-s -w" -trimpath -o swagger-linux-amd64 main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -ldflags "-s -w" -trimpath -o swagger-windows-amd64.exe main.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -ldflags "-s -w" -trimpath -o swagger-darwin-amd64 main.go
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -ldflags "-s -w" -trimpath -o swagger-darwin-arm64 main.go
```

## 使用

### 上传文件
```bash
./swagger-darwin-arm64 -secret 123456 -port 8900
```

### 快速上传本地文件到目标服务
```bash
curl -F "file=@docs/example.test.json" \
-F "filename=ttt.json" \
-F "secret=password" \
http://localhost:8900/upload
```

### 简单启动
### 获取文档文件列表

```bash
curl http://localhost:8900/list
```

### 刷新文档
```bash
curl http://localhost:8900/refresh
```

### 简单守护启动

```bash
nohup ./swagger -secret password >> server.log 2>&1 &
Expand Down
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,24 @@ var port = flag.String("port", "8900", "端口")

var locker sync.Mutex

func main() {
func init() {
_ = os.Mkdir("docs", 0777)
_, err := os.Stat("index.html")
if err != nil {
if !os.IsNotExist(err) {
panic(err)
}
var buffer bytes.Buffer
buffer.Write(part1)
buffer.Write(part2)
err = os.WriteFile("index.html", buffer.Bytes(), 0644)
if err != nil {
panic(err)
}
}
}

func main() {
flag.Parse()

e := echo.New()
Expand Down Expand Up @@ -130,7 +145,7 @@ func list(c echo.Context) error {
}
}
}
return c.JSON(http.StatusOK, options)
return c.JSONPretty(http.StatusOK, options, " ")
}

func refresh(logger echo.Logger) {
Expand Down

0 comments on commit 6335f20

Please sign in to comment.