upgrade: 优化GinCtx转context.Context
This commit is contained in:
2
go.mod
2
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
|
||||
|
2
go.sum
2
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=
|
||||
|
@ -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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user