From 655a4f502154b693805eb330e7e5ef7b79a57f29 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 18:50:16 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E4=B8=80=E4=BA=9B?= =?UTF-8?q?=E9=94=99=E8=AF=AF=E5=BC=95=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- request/wrapper.go | 30 +++++++++++++----------------- router/handler.go | 4 ++-- router/server.go | 10 ---------- 3 files changed, 15 insertions(+), 29 deletions(-) diff --git a/request/wrapper.go b/request/wrapper.go index cd14a1e..065139c 100644 --- a/request/wrapper.go +++ b/request/wrapper.go @@ -11,11 +11,11 @@ import ( "strings" "sync" - "git.zhangdeman.cn/zhangdeman/trace" - "git.zhangdeman.cn/zhangdeman/wrapper/op_ternary" - + "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/gin/define" "git.zhangdeman.cn/zhangdeman/gin/request/parse_body" + "git.zhangdeman.cn/zhangdeman/trace" + "git.zhangdeman.cn/zhangdeman/wrapper/op_ternary" "github.com/gin-gonic/gin" ) @@ -242,33 +242,29 @@ func (wh *wrapperHandle) ParseCookie(ctx *gin.Context) map[string]string { } // GetLogicAfterResponse ... -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 16:54 2025/2/28 func (wh *wrapperHandle) GetLogicAfterResponse(ctx *gin.Context) *define.LogicAfterResponse { if nil == ctx || nil == ctx.Request { return nil } - l, exist := ctx.Get(define.LogicAfterResponseKey) + l, exist := ctx.Get(consts.GinLogicAfterResponseKey) if !exist || nil == l { l = &define.LogicAfterResponse{ - SuccessHookFuncList: make([]func(), 0), - FailureHookFuncList: make([]func(), 0), + SuccessHookFuncList: make([]func(ctx *gin.Context), 0), + FailureHookFuncList: make([]func(ctx *gin.Context), 0), Lock: &sync.RWMutex{}, } - ctx.Set(define.LogicAfterResponseKey, l) + ctx.Set(consts.GinLogicAfterResponseKey, l) } // 就这么写, key值如果被其他人覆盖成非法值, 此处会直接panic return l.(*define.LogicAfterResponse) } -// GetCustomContext 获取自定义context -func (wh *wrapperHandle) GetCustomContext(ctx *gin.Context) *define.Context { - return define.NewContext(ctx) -} - // GetTraceInstance 获取trace实例 func (wh *wrapperHandle) GetTraceInstance(ctx *gin.Context) *trace.Runtime { - return define.NewContext(ctx).Trace + i, exist := ctx.Get(consts.GinTraceInstanceField) + if !exist || nil == i { + i = trace.NewRuntime(wh.GetCtxStringData(ctx, consts.GinTraceIDField, ""), 2) + ctx.Set(consts.GinTraceInstanceField, i) + } + return i.(*trace.Runtime) } diff --git a/router/handler.go b/router/handler.go index 437e825..32e2284 100644 --- a/router/handler.go +++ b/router/handler.go @@ -82,8 +82,8 @@ func (s *server) RequestHandler(uriCfg UriConfig) gin.HandlerFunc { ctx.Set(consts.GinRequestSuccess, false) // 初始化响应之后logic logicAfterResponse := &define.LogicAfterResponse{ - SuccessHookFuncList: make([]func(), 0), - FailureHookFuncList: make([]func(), 0), + SuccessHookFuncList: make([]func(ctx *gin.Context), 0), + FailureHookFuncList: make([]func(ctx *gin.Context), 0), Lock: &sync.RWMutex{}, } // 此处暴露出去,是为了使用方可以获取到对应数据 diff --git a/router/server.go b/router/server.go index 0e58838..ca39410 100644 --- a/router/server.go +++ b/router/server.go @@ -15,8 +15,6 @@ import ( "git.zhangdeman.cn/zhangdeman/graceful" - "git.zhangdeman.cn/zhangdeman/gin/define" - apiDoc "git.zhangdeman.cn/zhangdeman/api-doc" "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/gin/middleware" @@ -67,10 +65,6 @@ func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption } // NewServer server实例 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 18:20 2025/2/7 func NewServer(port int, optionList ...SetServerOptionFunc) *server { if port < 80 { panic("port should be greater than 80") @@ -86,10 +80,6 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server { // CustomContext 必须在第一个, 并且进行初始化 globalMiddlewareList = append( globalMiddlewareList, - func(ctx *gin.Context) { - // 初始化上下文以及基础信息 - _ = define.NewContext(ctx) - }, ) if nil != option.loggerCfg { // 请求日志记录中间件