我对Gin不太确定,但我很高兴与大家分享我的解决方案(基于go server):
dist/*swagger-用户界面/index.htmlurl:"/api/openapi.yaml"main.go//go: embed swagger-用户界面/*api/openapi.yamlmain.gorouter. PathPrefix("/"). Handler(http. FileServer(http. FS(staticFiles))
就这样——你将在 /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)))
...