upgrade: 优化GinCtx转context.Context
This commit is contained in:
@ -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