You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ctx.go 2.8 KiB

5 days ago
4 days ago
5 days ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package grpclib
  2. import (
  3. "context"
  4. "egg-im/pkg/gerrors"
  5. "egg-im/pkg/logger"
  6. "strconv"
  7. "google.golang.org/grpc/metadata"
  8. )
  9. const (
  10. CtxUserId = "user_id"
  11. CtxDeviceId = "device_id"
  12. CtxToken = "token"
  13. CtxRequestId = "request_id"
  14. )
  15. func ContextWithRequestId(ctx context.Context, requestId int64) context.Context {
  16. return metadata.NewOutgoingContext(ctx, metadata.Pairs(CtxRequestId, strconv.FormatInt(requestId, 10)))
  17. }
  18. // GetCtxRequestId 获取ctx的app_id
  19. func GetCtxRequestId(ctx context.Context) int64 {
  20. md, ok := metadata.FromIncomingContext(ctx)
  21. if !ok {
  22. return 0
  23. }
  24. requestIds, ok := md[CtxRequestId]
  25. if !ok && len(requestIds) == 0 {
  26. return 0
  27. }
  28. requestId, err := strconv.ParseInt(requestIds[0], 10, 64)
  29. if err != nil {
  30. return 0
  31. }
  32. return requestId
  33. }
  34. // GetCtxData 获取ctx的用户数据,依次返回user_id,device_id
  35. func GetCtxData(ctx context.Context) (int64, int64, error) {
  36. md, ok := metadata.FromIncomingContext(ctx)
  37. if !ok {
  38. return 0, 0, gerrors.ErrUnauthorized
  39. }
  40. var (
  41. userId int64
  42. deviceId int64
  43. err error
  44. )
  45. userIdStrs, ok := md[CtxUserId]
  46. if !ok && len(userIdStrs) == 0 {
  47. return 0, 0, gerrors.ErrUnauthorized
  48. }
  49. userId, err = strconv.ParseInt(userIdStrs[0], 10, 64)
  50. if err != nil {
  51. logger.Sugar.Error(err)
  52. return 0, 0, gerrors.ErrUnauthorized
  53. }
  54. deviceIdStrs, ok := md[CtxDeviceId]
  55. if !ok && len(deviceIdStrs) == 0 {
  56. return 0, 0, gerrors.ErrUnauthorized
  57. }
  58. deviceId, err = strconv.ParseInt(deviceIdStrs[0], 10, 64)
  59. if err != nil {
  60. logger.Sugar.Error(err)
  61. return 0, 0, gerrors.ErrUnauthorized
  62. }
  63. return userId, deviceId, nil
  64. }
  65. // GetCtxDeviceId 获取ctx的设备id
  66. func GetCtxDeviceId(ctx context.Context) (int64, error) {
  67. md, ok := metadata.FromIncomingContext(ctx)
  68. if !ok {
  69. return 0, gerrors.ErrUnauthorized
  70. }
  71. deviceIdStrs, ok := md[CtxDeviceId]
  72. if !ok && len(deviceIdStrs) == 0 {
  73. return 0, gerrors.ErrUnauthorized
  74. }
  75. deviceId, err := strconv.ParseInt(deviceIdStrs[0], 10, 64)
  76. if err != nil {
  77. logger.Sugar.Error(err)
  78. return 0, gerrors.ErrUnauthorized
  79. }
  80. return deviceId, nil
  81. }
  82. // GetCtxToken 获取ctx的token
  83. func GetCtxToken(ctx context.Context) (string, error) {
  84. md, ok := metadata.FromIncomingContext(ctx)
  85. if !ok {
  86. return "", gerrors.ErrUnauthorized
  87. }
  88. tokens, ok := md[CtxToken]
  89. if !ok && len(tokens) == 0 {
  90. return "", gerrors.ErrUnauthorized
  91. }
  92. return tokens[0], nil
  93. }
  94. // NewAndCopyRequestId 创建一个context,并且复制RequestId
  95. func NewAndCopyRequestId(ctx context.Context) context.Context {
  96. newCtx := context.TODO()
  97. md, ok := metadata.FromIncomingContext(ctx)
  98. if !ok {
  99. return newCtx
  100. }
  101. requestIds, ok := md[CtxRequestId]
  102. if !ok && len(requestIds) == 0 {
  103. return newCtx
  104. }
  105. return metadata.NewOutgoingContext(newCtx, metadata.Pairs(CtxRequestId, requestIds[0]))
  106. }