我有一个OpenAPI规范,并且我已经使用openapi-generator
来生成Golang gin
服务器。
从OpenAPI规范生成Swagger文档服务器的常规方法是什么?
我已经尝试过swag:它生成关于http://localhost:8080/swagger/index.html端点的文档。但这需要在代码批注中描述API。我正在寻找一个来自OpenAPI规范的Swagger UI生成器,我已经有了。
谢谢。
发布于 2021-02-26 01:04:06
您可以在docker容器中运行swagger编辑器。从https://hub.docker.com/r/swaggerapi/swagger-editor中拉出它,运行它,将浏览器指向http://localhost:8080,然后加载api.yaml文件。您还可以运行swagger ui https://hub.docker.com/r/swaggerapi/swagger-ui。
发布于 2021-04-07 23:35:33
我对Gin不太确定,但我很高兴分享我的解决方案(基于go-server):
在您的项目根目录中创建一个名为./swagger-ui
url: "/api/openapi.yaml"
dist/*
-ui (https://github.com/swagger-api/swagger-ui/tree/master/dist)下的文件此处为url: "/api/openapi.yaml"
swagger-ui/index.html
中的main.go
规范的位置创建一个用//go:embed swagger-ui/* api/openapi.yaml
注释的新变量(后一个目录是由openapi生成器CLI创建的)<代码>H214<代码>H115在<代码>d16中的(生成的)路由器下添加:router.PathPrefix("/").Handler(http.FileServer(http.FS(staticFiles)))
就是这样-您将在/ Swagger -ui下获得swagger UI,并从/ api /openapi.yaml自动加载api定义。
package main
import (
"context"
...
)
//go:embed swagger-ui/* api/openapi.yaml
var staticFiles embed.FS
func main() {
router := ....
// Embed the Swagger UI within Go binary
router.PathPrefix("/").Handler(http.FileServer(http.FS(staticFiles)))
...
发布于 2021-05-21 04:49:12
有一个库将Swagger UI打包为Go http.Handler
:https://github.com/swaggest/swgui。
package main
import (
"net/http"
"github.com/swaggest/swgui/v3emb" // For go1.16 or later.
// "github.com/swaggest/swgui/v3" // For go1.15 and below.
)
func main() {
http.Handle("/", v3.NewHandler("My API", "/swagger.json", "/"))
http.ListenAndServe(":8080", nil)
}
本例中的"/swagger.json"
是指向OpenAPI规范文件的URL。
https://stackoverflow.com/questions/66340086
复制相似问题