ctx.go 723 B

12345678910111213141516171819202122232425262728293031323334
  1. package router
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/frame/g"
  5. "github.com/gogf/gf/v2/net/ghttp"
  6. )
  7. // GContext 网关上下文
  8. type GContext struct {
  9. UserId string // 上下文用户信息
  10. SessData g.Map // 当前Session管理对象
  11. RouterRule *Router // 路由校验规则
  12. ServerAddr string // 服务地址
  13. }
  14. const GContextKey = "jyCtx"
  15. // UpdateGContext 更新上下文GContext
  16. func UpdateGContext(r *ghttp.Request, GCtx *GContext) {
  17. r.SetCtxVar(GContextKey, GCtx)
  18. }
  19. // GetGContext 获取上下文GContext
  20. func GetGContext(ctx context.Context) *GContext {
  21. value := ctx.Value(GContextKey)
  22. if value == nil {
  23. return nil
  24. }
  25. if GCtx, ok := value.(*GContext); ok {
  26. return GCtx
  27. }
  28. return nil
  29. }