feat: 增加注册注入参数的方法

This commit is contained in:
2025-10-30 15:48:43 +08:00
parent 1a9bc7b6dd
commit 028d16ac5a
3 changed files with 44 additions and 14 deletions

View File

@ -11,6 +11,7 @@ import (
"fmt"
"net/http"
"strings"
"sync"
"git.zhangdeman.cn/zhangdeman/graceful"
@ -129,14 +130,17 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server {
uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)),
port: port,
option: option,
lock: &sync.RWMutex{},
}
}
type server struct {
router *gin.Engine
port int
uiInstance *apiDoc.SwaggerUI
option *serverOption
router *gin.Engine
port int
uiInstance *apiDoc.SwaggerUI
option *serverOption
commonParam map[string]GetCommonParam // 结构体字段名, 注意, 不是TAG
lock *sync.RWMutex
}
// Start 启动服务
@ -201,21 +205,22 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, cL
for _, itemUriCfg := range urlTable {
_ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType)
method := strings.ToUpper(itemUriCfg.RequestMethod)
handleFunc := s.RequestHandler(itemUriCfg)
switch method {
case http.MethodGet:
g.GET(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.GET(itemUriCfg.Path, handleFunc)
case http.MethodHead:
g.HEAD(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.HEAD(itemUriCfg.Path, handleFunc)
case http.MethodPost:
g.POST(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.POST(itemUriCfg.Path, handleFunc)
case http.MethodPut:
g.PUT(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.PUT(itemUriCfg.Path, handleFunc)
case http.MethodPatch:
g.PATCH(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.PATCH(itemUriCfg.Path, handleFunc)
case http.MethodDelete:
g.DELETE(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.DELETE(itemUriCfg.Path, handleFunc)
case http.MethodOptions:
g.OPTIONS(itemUriCfg.Path, RequestHandler(itemUriCfg))
g.OPTIONS(itemUriCfg.Path, handleFunc)
case http.MethodTrace:
panic(`method Trace is not supported`)
default: