diff --git a/go.mod b/go.mod index 18e2d57..b04fab5 100644 --- a/go.mod +++ b/go.mod @@ -79,3 +79,7 @@ require ( gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) + +replace ( + git.zhangdeman.cn/gateway/api-doc => ../api-doc +) \ No newline at end of file diff --git a/router/define.go b/router/define.go index 9f8cfff..4f00000 100644 --- a/router/define.go +++ b/router/define.go @@ -65,6 +65,7 @@ const ( ) const ( - SwaggerUIThemeDefault = "swaggerUI" // 文档默认主题 - SwaggerUIThemeKnife4go = "knife4go" // knife4go 主题 + SwaggerUIThemeDefault = "swaggerUI" // 文档默认主题 + SwaggerUIThemeKnife4go = "knife4go" // knife4go 主题 + SwaggerUIThemeYDocLucky = "YDocLuckyUI" // YDoc Lucky UI 主题: https://github.com/NoBugBoy/LuckyUI ) diff --git a/router/register_test.go b/router/register_test.go index b29bd34..ca43da2 100644 --- a/router/register_test.go +++ b/router/register_test.go @@ -30,7 +30,7 @@ type TestOut struct { } type TestForm struct { - Meta `tag:"测试表单" path:"/a/b/c/d" desc:"测试接口" method:"get"` + Meta `tag:"测试表单" path:"/a/b/c/d" desc:"测试接口" method:"GET"` Age int `json:"age" form:"age" binding:"min=20" err_msg:"年龄不能小于20"` Name string `json:"name" form:"name"` Test *Test `json:"test" form:"test"` @@ -42,7 +42,7 @@ type Test struct { func Test_parseController(t *testing.T) { SetValidateErrTag("err_msg") - s := NewServer(8080, nil) + s := NewServer(8888, nil) s.Group("test", nil, TestController{}) s.Start() } diff --git a/router/server.go b/router/server.go index 99963ad..a5e967a 100644 --- a/router/server.go +++ b/router/server.go @@ -102,7 +102,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) + swaggerInstance := NewSwaggerUI(s.docInstance, SwaggerUIThemeKnife4go) + s.router.GET("/swagger-resources", func(ctx *gin.Context) { // lucky UI获取分组信息 + ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 允许访问所有域 + ctx.JSON(http.StatusOK, []map[string]any{ + { + "name": "服务文档", + "url": "/swagger-ui/doc/openapi.json", + "swaggerVersion": consts.SwaggerDocVersion3, + }, + }) + // ctx.JSON(http.StatusOK, swaggerInstance.docInstance.Data()) + }) + s.router.GET("/swagger-ui/doc/openapi.json", func(ctx *gin.Context) { + ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 允许访问所有域 + ctx.JSON(http.StatusOK, swaggerInstance.docInstance.Data()) + }) s.router.GET("/doc/swagger/*any", func(ctx *gin.Context) { if ctx.Request.RequestURI == "/doc/swagger/doc.json" { // 默认swagger, 通过此接口读取文档数据