From e5fad125969f3d482ffe630ce56ebe888f94f453 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 17 Aug 2025 13:19:39 +0800 Subject: [PATCH] =?UTF-8?q?upgrade:=20=E4=BC=98=E5=8C=96GinCtx=E8=BD=ACcon?= =?UTF-8?q?text.Context?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 2 +- go.sum | 2 ++ util/context.go | 16 +++++----------- 3 files changed, 8 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 09e1a9b..92b1270 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ toolchain go1.24.2 require ( git.zhangdeman.cn/gateway/api-doc v0.0.0-20250528130517-e02098e64392 - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250809053723-230c636a6f29 + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250816114446-59d5034e469b git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20250429065800-fc340b9417cf git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250510123912-a0d52fc093ab git.zhangdeman.cn/zhangdeman/graceful v0.0.0-20250529070945-92833db6f3a4 diff --git a/go.sum b/go.sum index fe9268b..63a91db 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250622103742-f363092a1a50 h1:Bx1MtV git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250622103742-f363092a1a50/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250809053723-230c636a6f29 h1:SKiCgxI3bQOcoQqLIIK1I1x1yaX7VIiL411MjlFKqxQ= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250809053723-230c636a6f29/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250816114446-59d5034e469b h1:OwAtjuvSgUxA94HYd55L6Nh2xm8atUOo3sjzEeZ0z/I= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250816114446-59d5034e469b/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc= git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20250429065800-fc340b9417cf h1:xCPM3U6i62UvLo9VNvDP45Ue3dPl7ratHu1rSEJRE2k= git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20250429065800-fc340b9417cf/go.mod h1:onY+qrB+Uwfuv75JlgHlGdkirAfYcINrvCashtVoBX0= git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI= diff --git a/util/context.go b/util/context.go index 4101ac6..9ffb61c 100644 --- a/util/context.go +++ b/util/context.go @@ -16,27 +16,21 @@ import ( // GinCtxToContext gin.Cotext 转换为 context.Context func GinCtxToContext(ctx *gin.Context) context.Context { - newContext := context.Background() if nil == ctx { - return newContext + return context.Background() } newGinContext := ctx.Copy() - // 复制自定义的业务上下文值 - for k, v := range newGinContext.Keys { - newContext = context.WithValue(newContext, k, v) - } // 复制gin request 基本数据 requestData := map[string]any{ consts.GinContextField: ctx, } + for k, v := range newGinContext.Keys { + requestData[k] = v + } if nil != ctx.Request { requestData[consts.GinRequestURIField] = ctx.Request.RequestURI requestData[consts.GinRequestMethodField] = ctx.Request.Method } - for k, v := range requestData { - newContext = context.WithValue(newContext, k, v) - } - - return newContext + return context.WithValue(context.Background(), consts.GinContextDataField, requestData) }