1 Star 0 Fork 0

ltotal / ppw_gin

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
context.go 647 Bytes
一键复制 编辑 原始数据 按行查看 历史
ltotal 提交于 2024-03-20 16:32 . 底层优化
package pkg
import (
"context"
"github.com/gin-gonic/gin"
)
type GoroutineContextManager struct {
GlobalGinContextKey string
}
var GCM = &GoroutineContextManager{
GlobalGinContextKey: "ppw_gin_context",
}
func (gcm *GoroutineContextManager) SetContext(ctx *gin.Context) {
oldCtx := ctx.Request.Context()
newCtx := context.WithValue(oldCtx, gcm.GlobalGinContextKey, ctx)
ctx.Request = ctx.Request.WithContext(newCtx)
}
func (gcm *GoroutineContextManager) GetContext(ctx context.Context) (*gin.Context, bool) {
if ctx == nil {
return nil, false
}
ginCtx, ok := ctx.Value(gcm.GlobalGinContextKey).(*gin.Context)
return ginCtx, ok
}
1
https://gitee.com/ltotal/ppw_gin.git
git@gitee.com:ltotal/ppw_gin.git
ltotal
ppw_gin
ppw_gin
v0.8.3

搜索帮助