diff --git a/.gitignore b/.gitignore index ea6bf1f..72e7cd1 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,6 @@ custom.json swagger go.sum Makefile -index.html \ No newline at end of file +index.html + +swagger-* \ No newline at end of file diff --git a/README.MD b/README.MD index c2fb4d5..2b6682d 100644 --- a/README.MD +++ b/README.MD @@ -1,8 +1,20 @@ # 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" \ @@ -10,7 +22,18 @@ curl -F "file=@docs/example.test.json" \ 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 & diff --git a/main.go b/main.go index 0c97df6..603556a 100644 --- a/main.go +++ b/main.go @@ -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() @@ -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) {