From 0c12a266f6f19aa1af7b59f7885485eb3f8c1895 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, 9 Aug 2025 14:49:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81gin.Context=E8=BD=AC?= =?UTF-8?q?=E6=8D=A2=E4=B8=BAcontext.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 | 42 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 util/context.go diff --git a/go.mod b/go.mod index 7287f5c..09e1a9b 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-20250622103742-f363092a1a50 + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250809053723-230c636a6f29 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 25f4500..fe9268b 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995 h1:LmPRAf git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc= git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250622103742-f363092a1a50 h1:Bx1MtVKAWvCbtM03+9Ob6Eeaaivj6A+RmkYTo2N46XY= 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/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 new file mode 100644 index 0000000..4101ac6 --- /dev/null +++ b/util/context.go @@ -0,0 +1,42 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2025-08-09 13:10 +package util + +import ( + "context" + + "git.zhangdeman.cn/zhangdeman/consts" + "github.com/gin-gonic/gin" +) + +// GinCtxToContext gin.Cotext 转换为 context.Context +func GinCtxToContext(ctx *gin.Context) context.Context { + newContext := context.Background() + if nil == ctx { + return newContext + } + newGinContext := ctx.Copy() + // 复制自定义的业务上下文值 + for k, v := range newGinContext.Keys { + newContext = context.WithValue(newContext, k, v) + } + // 复制gin request 基本数据 + requestData := map[string]any{ + consts.GinContextField: ctx, + } + 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 +}