golang-im聊天
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

140 行
3.1 KiB

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