From 954f6fef8a12cf886f09dceab1688ab202a11f4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 1 Nov 2025 12:27:43 +0800 Subject: [PATCH] feat: remove custom context --- define/consts.go | 12 -------- define/context.go | 68 -------------------------------------------- router/config.go | 4 --- router/controller.go | 7 +---- router/define.go | 2 -- router/handler.go | 8 ++---- router/meta.go | 7 +++-- 7 files changed, 7 insertions(+), 101 deletions(-) delete mode 100644 define/consts.go delete mode 100644 define/context.go diff --git a/define/consts.go b/define/consts.go deleted file mode 100644 index 6c69649..0000000 --- a/define/consts.go +++ /dev/null @@ -1,12 +0,0 @@ -// Package define ... -// -// Description : define ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2025-04-12 20:18 -package define - -const ( - CustomContextKey = "_CUSTOM_CONTEXT" // 自定义context -) diff --git a/define/context.go b/define/context.go deleted file mode 100644 index c67284e..0000000 --- a/define/context.go +++ /dev/null @@ -1,68 +0,0 @@ -// Package define ... -// -// Description : define ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 2025-04-12 20:57 -package define - -import ( - "fmt" - "strings" - "time" - - networkUtil "git.zhangdeman.cn/zhangdeman/network/util" - "git.zhangdeman.cn/zhangdeman/wrapper/op_string" - - "git.zhangdeman.cn/zhangdeman/trace" - "github.com/gin-gonic/gin" -) - -type Context struct { - Context *gin.Context // 继承 gin context - Trace *trace.Runtime // trace 实例 - TraceID string - RequestID string - RequestTime time.Time -} - -// NewContext 创建context -func NewContext(ginCtx *gin.Context) *Context { - existCtx, exist := ginCtx.Get(CustomContextKey) - if exist && existCtx != nil { - if c, ok := existCtx.(*Context); ok { - return c - } - } - traceID := fmt.Sprintf( - "%v-%v-%v-%v", - time.Now().UnixNano()/1e6, - strings.ReplaceAll(networkUtil.IP.GetHostIP(), ".", ""), - strings.ReplaceAll(networkUtil.IP.GetRemoteIP(ginCtx.Request), ".", ""), - op_string.Md5(op_string.Random(32, "")), - ) - getRequestID := func(ctx *gin.Context, traceID string) string { - requestID := ctx.GetHeader("X-Forward-Request-Id") - if len(requestID) > 0 { - return requestID - } - if len(traceID) > 0 { - return traceID - } - return traceID - } - ctx := &Context{ - Context: ginCtx, - Trace: trace.NewRuntime(traceID, 1), - TraceID: traceID, - RequestID: getRequestID(ginCtx, traceID), - RequestTime: time.Now(), - } - httpHandleConfig := GetHttpHandleConfig() - ginCtx.Set(CustomContextKey, ctx) - ginCtx.Set(httpHandleConfig.TraceIDField, traceID) - ginCtx.Set(httpHandleConfig.RequestIDField, ctx.RequestID) - ginCtx.Set(httpHandleConfig.StartRequestTimeField, ctx.RequestTime.UnixMilli()) - return ctx -} diff --git a/router/config.go b/router/config.go index 57e6d89..859dc7b 100644 --- a/router/config.go +++ b/router/config.go @@ -12,10 +12,6 @@ import "strings" var defaultValidateErrTag = "err" // SetValidateErrTag 设置验证失败时, 获取错误信息的tag字段 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 17:42 2025/2/7 func SetValidateErrTag(tagName string) { tagName = strings.TrimSpace(tagName) if tagName == "" { diff --git a/router/controller.go b/router/controller.go index 7750d8f..1b2e11c 100644 --- a/router/controller.go +++ b/router/controller.go @@ -13,10 +13,6 @@ import ( ) // controller 解析controller有哪些方法要注册为接口 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 15:30 2025/1/27 type controller struct { } @@ -71,7 +67,7 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n } // 第一个参数必须是 *gin.Context 或者 *define.Context paramOne := methodType.In(1).String() - if paramOne != GinContextType && paramOne != CustomContextType { + if paramOne != GinContextType { needRegister = false return } @@ -107,7 +103,6 @@ func (c controller) methodConfig(reflectMethod reflect.Method) (cfg UriConfig, n } } // 解析meta信息 - cfg.CtxType = paramOne cfg.Path = metaField.Tag.Get(TagNamePath) cfg.RequestMethod = metaField.Tag.Get(TagNameMethod) cfg.Desc = metaField.Tag.Get(TagNameDesc) diff --git a/router/define.go b/router/define.go index 4a1aad1..bc49eb7 100644 --- a/router/define.go +++ b/router/define.go @@ -15,7 +15,6 @@ const ( PrefixFuncName = "RouterPrefix" // 路由前缀函数名称 MiddlewareFuncName = "RouterMiddleware" // 路由中间件函数名称 GinContextType = "*gin.Context" // gin context 类型名称 - CustomContextType = "*define.Context" // custom context 类型名称 ErrorType = "error" // error类型 ErrorInterfaceFuncName = "Error" // error接口需要实现的方法名称 ) @@ -42,7 +41,6 @@ type UriConfig struct { TagList []string `json:"tag_list"` // 接口分组 Desc string `json:"desc"` // 接口描述 OutputStrict bool `json:"output_strict"` // 接口是否为严格模式 : 不配置,可返回任意类型, 配置, 必须返回结构体或者map - CtxType string `json:"ctx_type"` // ctx参数类型 FormDataType reflect.Type `json:"-"` // 表单数据类型 ResultDataType reflect.Type `json:"-"` // 返回值数据类型 ApiStructValue reflect.Value `json:"-"` // 逻辑函数所属结构体取值 diff --git a/router/handler.go b/router/handler.go index 39e285e..29a2d60 100644 --- a/router/handler.go +++ b/router/handler.go @@ -114,12 +114,8 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc { if uriCfg.FormDataType.Kind() != reflect.Ptr { inputValue = inputValue.Elem() } - if uriCfg.CtxType == CustomContextType { - customCtx := ctx.MustGet(define.CustomContextKey) - firstParam = reflect.ValueOf(customCtx) - } else { - firstParam = reflect.ValueOf(ctx) - } + + firstParam = reflect.ValueOf(ctx) resList := uriCfg.ApiLogicFunc.Func.Call([]reflect.Value{uriCfg.ApiStructValue, firstParam, inputValue}) if resList[1].IsNil() { // 请求成功 diff --git a/router/meta.go b/router/meta.go index b821f86..0600bb4 100644 --- a/router/meta.go +++ b/router/meta.go @@ -13,7 +13,8 @@ package router // // method: 请求方法: get/post 等 // -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:40 2024/7/20 +// 使用示例如下: +// type RequestForm struct { +// Meta `json:"-" path:"接口路由" method:"get,post" sse:"1" tag:"接口分组标签" summary:"接口描述"` +// } type Meta struct{}