ctx.go 619 B

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