修复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

@ -9,6 +9,7 @@ package router
import (
"reflect"
"strings"
)
// controller 解析controller有哪些方法要注册为接口
@ -91,7 +92,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
formType = methodType.In(2).Elem()
}
cfg.FormDataType = formType
_, metaFieldExist := formType.FieldByName(FieldNameMeta)
metaField, metaFieldExist := formType.FieldByName(FieldNameMeta)
if !metaFieldExist {
needRegister = false
return
@ -116,6 +117,23 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n
return
}
}
// 解析meta信息
cfg.Path = metaField.Tag.Get(TagNamePath)
cfg.RequestMethod = metaField.Tag.Get(TagNameMethod)
cfg.Desc = metaField.Tag.Get(TagNameDesc)
cfg.TagList = strings.Split(metaField.Tag.Get(TagNameUriTag), ",")
// 解析第一个返回值, 要求必须是结构体或者是map
outputStrictModel := metaField.Tag.Get(TagNameOutputStrict)
cfg.OutputStrict = outputStrictModel == "1" || outputStrictModel == "true"
if cfg.OutputStrict {
// 开启输出严格模式校验
if methodType.Out(0).Kind() != reflect.Struct && methodType.Out(0).Kind() != reflect.Map {
panic(cfg.Path + " : 接口配置输出严格校验, 输出数据类型必须为 struct 或 *struct 或 map, 实际返回数据类型 : " + methodType.Out(0).Kind().String())
return
}
}
// 解析参数配置
//cfg.ParamList = c.parseParamConfig(formType)
cfg.ApiLogicFunc = reflectMethod
needRegister = true
return

View File

@ -10,18 +10,18 @@ package router
import (
"reflect"
api_doc "git.zhangdeman.cn/gateway/api-doc"
apiDoc "git.zhangdeman.cn/gateway/api-doc"
"git.zhangdeman.cn/gateway/api-doc/define"
)
func NewDoc(info *define.Info, servers []*define.ServerItem) *Doc {
return &Doc{
instance: api_doc.NewOpenapiDoc(info, servers),
instance: apiDoc.NewOpenapiDoc(info, servers),
}
}
type Doc struct {
instance *api_doc.Generate
instance *apiDoc.Generate
}
// Add 增加接口文档测试

View File

@ -8,8 +8,6 @@
package router
import (
"encoding/json"
"fmt"
"testing"
"github.com/gin-gonic/gin"
@ -46,7 +44,5 @@ func Test_parseController(t *testing.T) {
SetValidateErrTag("err_msg")
s := NewServer(8080, nil)
s.Group("test", nil, TestController{})
byteData, _ := json.Marshal(s.docInstance.Data())
fmt.Println(string(byteData))
s.Start()
}

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())
}