context.go 882 B

12345678910111213141516171819202122232425262728293031323334353637383940
  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(ctx, "account_id", accountID)
  15. return newCtx
  16. }
  17. type AccountContext struct {
  18. ctx context.Context
  19. }
  20. func (this *AccountContext) Deadline() (deadline time.Time, ok bool) {
  21. return this.ctx.Deadline()
  22. }
  23. func (*AccountContext) Done() <-chan struct{} {
  24. return nil
  25. }
  26. func (this *AccountContext) Err() error {
  27. return this.ctx.Err()
  28. }
  29. func (this *AccountContext) Value(key interface{}) interface{} {
  30. return this.ctx.Value(key)
  31. }