userlib.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package main
  2. import (
  3. "app.yhyue.com/moapp/jy_docs/services/model"
  4. userLibService "app.yhyue.com/moapp/jy_docs/services/userlib"
  5. jyDocsRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
  6. "context"
  7. "encoding/json"
  8. "flag"
  9. "fmt"
  10. "log"
  11. "strconv"
  12. "time"
  13. "app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/config"
  14. "app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/server"
  15. "app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/svc"
  16. "app.yhyue.com/moapp/jy_docs/rpc/userlib/userlib"
  17. "github.com/zeromicro/go-zero/core/conf"
  18. "github.com/zeromicro/go-zero/zrpc"
  19. "google.golang.org/grpc"
  20. )
  21. var configFile = flag.String("f", "etc/userlib.yaml", "the config file")
  22. func main() {
  23. flag.Parse()
  24. conf.MustLoad(*configFile, &config.Configs)
  25. if config.Configs.JyDocsMysqlDB.DataSourceName != "" {
  26. jyDocsRpcUtil.InitDB(config.Configs.JyDocsMysqlDB.DataSourceName, config.Configs.JyDocsMysqlDB.DriverName, config.Configs.JyDocsMysqlDB.MaxOpenConn, config.Configs.JyDocsMysqlDB.MaxIdleConn)
  27. ctx := svc.NewServiceContext(config.Configs)
  28. srv := server.NewUserLibServer(ctx)
  29. s := zrpc.MustNewServer(config.Configs.RpcServerConf, func(grpcServer *grpc.Server) {
  30. userlib.RegisterUserLibServer(grpcServer, srv)
  31. })
  32. s.AddUnaryInterceptors(rateLimitInterceptor)
  33. defer s.Stop()
  34. fmt.Printf("Starting rpc server at %s...\n", config.Configs.ListenOn)
  35. s.Start()
  36. } else {
  37. log.Fatal("【jy_docs】 read config error!")
  38. }
  39. }
  40. func rateLimitInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
  41. jsonStr := Strval(req)
  42. var dat map[string]interface{}
  43. if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
  44. fmt.Println("==============json str 转map=======================")
  45. }
  46. resp, err = handler(ctx, req)
  47. /*fmt.Println("接口名称:",info.FullMethod)
  48. fmt.Println("被调用者身份:",config.Configs.Callee)
  49. fmt.Println("调用者身份:",dat["appId"])
  50. fmt.Println("入参:",req)
  51. fmt.Println("返回信息:",resp)
  52. fmt.Println("节点:",config.Configs.Node)
  53. fmt.Println("摘要:",resp)
  54. fmt.Println("时间戳:",time.Now().Unix())*/
  55. res := fmt.Sprint(resp)
  56. nameRune := []rune(res)
  57. if len(res) > 255 {
  58. res = string(nameRune[:255])
  59. }
  60. data := &model.InterfaceLog{}
  61. data.InterName = info.FullMethod
  62. data.CalleeId = config.Configs.Callee
  63. data.AppId = fmt.Sprint(dat["appId"])
  64. data.InParameter = fmt.Sprint(req)
  65. data.ReturnInfo = res
  66. data.Node = config.Configs.Node
  67. data.Summary = res
  68. data.Timestamp = time.Now()
  69. flag := userLibService.InterfaceLog(data)
  70. if flag {
  71. log.Println("接口调用日志记录成功")
  72. }
  73. return resp,err
  74. }
  75. // Strval 获取变量的字符串值
  76. // 浮点型 3.0将会转换成字符串3, "3"
  77. // 非数值或字符类型的变量将会被转换成JSON格式字符串
  78. func Strval(value interface{}) string {
  79. var key string
  80. if value == nil {
  81. return key
  82. }
  83. switch value.(type) {
  84. case float64:
  85. ft := value.(float64)
  86. key = strconv.FormatFloat(ft, 'f', -1, 64)
  87. case float32:
  88. ft := value.(float32)
  89. key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
  90. case int:
  91. it := value.(int)
  92. key = strconv.Itoa(it)
  93. case uint:
  94. it := value.(uint)
  95. key = strconv.Itoa(int(it))
  96. case int8:
  97. it := value.(int8)
  98. key = strconv.Itoa(int(it))
  99. case uint8:
  100. it := value.(uint8)
  101. key = strconv.Itoa(int(it))
  102. case int16:
  103. it := value.(int16)
  104. key = strconv.Itoa(int(it))
  105. case uint16:
  106. it := value.(uint16)
  107. key = strconv.Itoa(int(it))
  108. case int32:
  109. it := value.(int32)
  110. key = strconv.Itoa(int(it))
  111. case uint32:
  112. it := value.(uint32)
  113. key = strconv.Itoa(int(it))
  114. case int64:
  115. it := value.(int64)
  116. key = strconv.FormatInt(it, 10)
  117. case uint64:
  118. it := value.(uint64)
  119. key = strconv.FormatUint(it, 10)
  120. case string:
  121. key = value.(string)
  122. case []byte:
  123. key = string(value.([]byte))
  124. default:
  125. newValue, _ := json.Marshal(value)
  126. key = string(newValue)
  127. }
  128. return key
  129. }