From 028d16ac5a7b1cf6c647e1797d6ec81566978063 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Thu, 30 Oct 2025 15:48:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=B3=A8=E5=86=8C?= =?UTF-8?q?=E6=B3=A8=E5=85=A5=E5=8F=82=E6=95=B0=E7=9A=84=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- router/common_param.go | 25 +++++++++++++++++++++++++ router/handler.go | 6 +++--- router/server.go | 27 ++++++++++++++++----------- 3 files changed, 44 insertions(+), 14 deletions(-) create mode 100644 router/common_param.go diff --git a/router/common_param.go b/router/common_param.go new file mode 100644 index 0000000..506925a --- /dev/null +++ b/router/common_param.go @@ -0,0 +1,25 @@ +// Package router ... +// +// Description : router ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-10-30 15:39 +package router + +import "github.com/gin-gonic/gin" + +// GetCommonParam 获取公共参数 +type GetCommonParam func(ctx *gin.Context) (any, error) + +// AddCommonParamRule 添加公共参数注入规则 +func (s *server) AddCommonParamRule(fieldName string, getParamFunc GetCommonParam) { + s.commonParam[fieldName] = getParamFunc +} + +// AddCommonParamRules 批量添加公共参数注入规则 +func (s *server) AddCommonParamRules(rules map[string]GetCommonParam) { + for fieldName, rule := range rules { + s.AddCommonParamRule(fieldName, rule) + } +} diff --git a/router/handler.go b/router/handler.go index 0d2476b..7ab1ff0 100644 --- a/router/handler.go +++ b/router/handler.go @@ -20,7 +20,7 @@ import ( "github.com/gin-gonic/gin" ) -func getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, error) { +func (s *server) getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, error) { var ( formParam reflect.Value formValue any @@ -43,7 +43,7 @@ func getFormInitValue(ctx *gin.Context, uriCfg UriConfig) (any, error) { } // RequestHandler 获取请求处理方法 -func RequestHandler(uriCfg UriConfig) gin.HandlerFunc { +func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc { return func(ctx *gin.Context) { var ( err error @@ -52,7 +52,7 @@ func RequestHandler(uriCfg UriConfig) gin.HandlerFunc { formValue any ) - if formValue, err = getFormInitValue(ctx, uriCfg); nil != err { + if formValue, err = s.getFormInitValue(ctx, uriCfg); nil != err { e = exception.NewFromError(400, err) response.SendWithException(ctx, e, &define.ResponseOption{ ContentType: consts.MimeTypeJson, diff --git a/router/server.go b/router/server.go index bf8cb32..2fdf84c 100644 --- a/router/server.go +++ b/router/server.go @@ -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: