ctx.go 617 B

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