context.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. package common
  2. import (
  3. "context"
  4. "time"
  5. context2 "git.ikuban.com/server/kratos-utils/http/context"
  6. )
  7. func NewContext(parentCtx context.Context) context.Context {
  8. ctx := &AccountContext{ctx: parentCtx}
  9. return ctx
  10. }
  11. func NewContextWithAccountID(parentCtx context.Context, accountID int64) context.Context {
  12. ctx := &AccountContext{ctx: parentCtx}
  13. newCtx := context2.AppendToContext(ctx, "user_id", accountID)
  14. newCtx = context2.AppendToContext(newCtx, "account_id", accountID)
  15. return newCtx
  16. }
  17. func NewContextWithValues(parentCtx context.Context, values map[string]interface{}) context.Context {
  18. ctx := &AccountContext{ctx: parentCtx}
  19. newCtx := context.Context(ctx)
  20. for k, v := range values {
  21. _k := k
  22. _v := v
  23. newCtx = context2.AppendToContext(ctx, _k, _v)
  24. }
  25. return newCtx
  26. }
  27. type AccountContext struct {
  28. ctx context.Context
  29. }
  30. func (this *AccountContext) Deadline() (deadline time.Time, ok bool) {
  31. return
  32. }
  33. func (*AccountContext) Done() <-chan struct{} {
  34. return nil
  35. }
  36. func (this *AccountContext) Err() error {
  37. return nil
  38. }
  39. func (this *AccountContext) Value(key interface{}) interface{} {
  40. return this.ctx.Value(key)
  41. }