|
@@ -1,11 +1,16 @@
|
|
package main
|
|
package main
|
|
|
|
|
|
import (
|
|
import (
|
|
|
|
+ "app.yhyue.com/moapp/jy_docs/services/model"
|
|
|
|
+ userLibService "app.yhyue.com/moapp/jy_docs/services/userlib"
|
|
jyDocsRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
|
|
jyDocsRpcUtil "app.yhyue.com/moapp/jy_docs/services/util"
|
|
"context"
|
|
"context"
|
|
|
|
+ "encoding/json"
|
|
"flag"
|
|
"flag"
|
|
"fmt"
|
|
"fmt"
|
|
"log"
|
|
"log"
|
|
|
|
+ "strconv"
|
|
|
|
+ "time"
|
|
|
|
|
|
"app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/config"
|
|
"app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/config"
|
|
"app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/server"
|
|
"app.yhyue.com/moapp/jy_docs/rpc/userlib/internal/server"
|
|
@@ -16,7 +21,6 @@ import (
|
|
"github.com/tal-tech/go-zero/zrpc"
|
|
"github.com/tal-tech/go-zero/zrpc"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc"
|
|
)
|
|
)
|
|
-
|
|
|
|
var configFile = flag.String("f", "etc/userlib.yaml", "the config file")
|
|
var configFile = flag.String("f", "etc/userlib.yaml", "the config file")
|
|
func main() {
|
|
func main() {
|
|
flag.Parse()
|
|
flag.Parse()
|
|
@@ -36,8 +40,97 @@ func main() {
|
|
log.Fatal("【jy_docs】 read config error!")
|
|
log.Fatal("【jy_docs】 read config error!")
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
func rateLimitInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
func rateLimitInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
|
|
- fmt.Println("拦截器")
|
|
|
|
- log.Println("方法名:",info.FullMethod," 参数:",req)
|
|
|
|
- return handler(ctx, req)
|
|
|
|
|
|
+ jsonStr := Strval(req)
|
|
|
|
+ var dat map[string]interface{}
|
|
|
|
+ if err := json.Unmarshal([]byte(jsonStr), &dat); err == nil {
|
|
|
|
+ fmt.Println("==============json str 转map=======================")
|
|
|
|
+ }
|
|
|
|
+ resp, err = handler(ctx, req)
|
|
|
|
+ /*fmt.Println("接口名称:",info.FullMethod)
|
|
|
|
+ fmt.Println("被调用者身份:",config.Configs.Callee)
|
|
|
|
+ fmt.Println("调用者身份:",dat["appId"])
|
|
|
|
+ fmt.Println("入参:",req)
|
|
|
|
+ fmt.Println("返回信息:",resp)
|
|
|
|
+ fmt.Println("节点:",config.Configs.Node)
|
|
|
|
+ fmt.Println("摘要:",resp)
|
|
|
|
+ fmt.Println("时间戳:",time.Now().Unix())*/
|
|
|
|
+ res := fmt.Sprint(resp)
|
|
|
|
+ nameRune := []rune(res)
|
|
|
|
+ if len(res) > 255 {
|
|
|
|
+ res = string(nameRune[:255])
|
|
|
|
+ }
|
|
|
|
+ data := &model.InterfaceLog{}
|
|
|
|
+ data.InterName = info.FullMethod
|
|
|
|
+ data.CalleeId = config.Configs.Callee
|
|
|
|
+ data.AppId = fmt.Sprint(dat["appId"])
|
|
|
|
+ data.InParameter = fmt.Sprint(req)
|
|
|
|
+ data.ReturnInfo = res
|
|
|
|
+ data.Node = config.Configs.Node
|
|
|
|
+ data.Summary = res
|
|
|
|
+ data.Timestamp = time.Now()
|
|
|
|
+ flag := userLibService.InterfaceLog(data)
|
|
|
|
+ if flag {
|
|
|
|
+ log.Println("接口调用日志记录成功")
|
|
|
|
+ }
|
|
|
|
+ return resp,err
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// Strval 获取变量的字符串值
|
|
|
|
+// 浮点型 3.0将会转换成字符串3, "3"
|
|
|
|
+// 非数值或字符类型的变量将会被转换成JSON格式字符串
|
|
|
|
+func Strval(value interface{}) string {
|
|
|
|
+ var key string
|
|
|
|
+ if value == nil {
|
|
|
|
+ return key
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ switch value.(type) {
|
|
|
|
+ case float64:
|
|
|
|
+ ft := value.(float64)
|
|
|
|
+ key = strconv.FormatFloat(ft, 'f', -1, 64)
|
|
|
|
+ case float32:
|
|
|
|
+ ft := value.(float32)
|
|
|
|
+ key = strconv.FormatFloat(float64(ft), 'f', -1, 64)
|
|
|
|
+ case int:
|
|
|
|
+ it := value.(int)
|
|
|
|
+ key = strconv.Itoa(it)
|
|
|
|
+ case uint:
|
|
|
|
+ it := value.(uint)
|
|
|
|
+ key = strconv.Itoa(int(it))
|
|
|
|
+ case int8:
|
|
|
|
+ it := value.(int8)
|
|
|
|
+ key = strconv.Itoa(int(it))
|
|
|
|
+ case uint8:
|
|
|
|
+ it := value.(uint8)
|
|
|
|
+ key = strconv.Itoa(int(it))
|
|
|
|
+ case int16:
|
|
|
|
+ it := value.(int16)
|
|
|
|
+ key = strconv.Itoa(int(it))
|
|
|
|
+ case uint16:
|
|
|
|
+ it := value.(uint16)
|
|
|
|
+ key = strconv.Itoa(int(it))
|
|
|
|
+ case int32:
|
|
|
|
+ it := value.(int32)
|
|
|
|
+ key = strconv.Itoa(int(it))
|
|
|
|
+ case uint32:
|
|
|
|
+ it := value.(uint32)
|
|
|
|
+ key = strconv.Itoa(int(it))
|
|
|
|
+ case int64:
|
|
|
|
+ it := value.(int64)
|
|
|
|
+ key = strconv.FormatInt(it, 10)
|
|
|
|
+ case uint64:
|
|
|
|
+ it := value.(uint64)
|
|
|
|
+ key = strconv.FormatUint(it, 10)
|
|
|
|
+ case string:
|
|
|
|
+ key = value.(string)
|
|
|
|
+ case []byte:
|
|
|
|
+ key = string(value.([]byte))
|
|
|
|
+ default:
|
|
|
|
+ newValue, _ := json.Marshal(value)
|
|
|
|
+ key = string(newValue)
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return key
|
|
}
|
|
}
|