feat: 支持gin.Context转换为context.Context #15
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-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
|
||||
|
2
go.sum
2
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=
|
||||
|
42
util/context.go
Normal file
42
util/context.go
Normal file
@ -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
|
||||
}
|
Reference in New Issue
Block a user