ctx.go 665 B

12345678910111213141516171819202122232425262728293031323334
  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) (gCtx *GContext) {
  19. gCtx = &GContext{
  20. Sess: &JySession{},
  21. RouterRule: &Router{},
  22. }
  23. value := ctx.Value(GContextKey)
  24. if value == nil {
  25. return
  26. }
  27. gCtx, _ = value.(*GContext)
  28. return
  29. }