修复URL注册BUG

This commit is contained in:
2025-02-15 22:34:21 +08:00
parent 99df73e50e
commit c3df76e94d
4 changed files with 50 additions and 15 deletions

View File

@ -12,6 +12,9 @@ import (
"net/http"
"strings"
"git.zhangdeman.cn/zhangdeman/consts"
apiDocDefine "git.zhangdeman.cn/gateway/api-doc/define"
"github.com/gin-gonic/gin"
)
@ -25,9 +28,14 @@ func NewServer(port int, option any) *server {
panic("port should be greater than 80")
}
return &server{
router: gin.Default(),
docInstance: NewDoc(nil, nil), // TODO : 配置相关信息
port: port,
router: gin.Default(),
docInstance: NewDoc(nil, []*apiDocDefine.ServerItem{
{
Url: fmt.Sprintf("http://127.0.0.1:%d", port),
Description: "测试服务器",
},
}), // TODO : 配置相关信息
port: port,
}
}
@ -79,6 +87,8 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, cL
g.OPTIONS(itemUriCfg.Path, RequestHandler(itemUriCfg))
case http.MethodTrace:
panic(`method Trace is not supported`)
default:
panic("method " + itemUriCfg.RequestMethod + " is not support")
}
}
}
@ -93,11 +103,22 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, cL
// Date : 21:45 2025/2/15
func (s *server) SwaggerUI() {
swaggerInstance := NewSwaggerUI(s.docInstance, SwaggerUIThemeDefault)
// 默认swagger
s.router.GET("/swagger/*any", func(ctx *gin.Context) {
if ctx.Request.RequestURI == "/swagger/doc.json" {
s.router.GET("/doc/swagger/*any", func(ctx *gin.Context) {
if ctx.Request.RequestURI == "/doc/swagger/doc.json" {
// 默认swagger, 通过此接口读取文档数据
ctx.JSON(http.StatusOK, swaggerInstance.docInstance.Data())
ctx.Abort()
}
}, swaggerInstance.HandleSwaggerUI())
if ctx.Request.RequestURI == "/doc/swagger/openapi.json" {
// knife4go 文档通过此接口读取文档列表
ctx.JSON(http.StatusOK, []map[string]any{
{
"name": "服务文档",
"url": "doc.json",
"swaggerVersion": consts.SwaggerDocVersion3,
},
})
ctx.Abort()
}
}, swaggerInstance.Handler())
}