package main 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" "context" "encoding/json" "flag" "fmt" "log" "strconv" "time" "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/svc" "app.yhyue.com/moapp/jy_docs/rpc/userlib/userlib" "github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/zrpc" "google.golang.org/grpc" ) var configFile = flag.String("f", "etc/userlib.yaml", "the config file") func main() { flag.Parse() conf.MustLoad(*configFile, &config.Configs) if config.Configs.JyDocsMysqlDB.DataSourceName != "" { jyDocsRpcUtil.InitDB(config.Configs.JyDocsMysqlDB.DataSourceName, config.Configs.JyDocsMysqlDB.DriverName, config.Configs.JyDocsMysqlDB.MaxOpenConn, config.Configs.JyDocsMysqlDB.MaxIdleConn) ctx := svc.NewServiceContext(config.Configs) srv := server.NewUserLibServer(ctx) s := zrpc.MustNewServer(config.Configs.RpcServerConf, func(grpcServer *grpc.Server) { userlib.RegisterUserLibServer(grpcServer, srv) }) s.AddUnaryInterceptors(rateLimitInterceptor) defer s.Stop() fmt.Printf("Starting rpc server at %s...\n", config.Configs.ListenOn) s.Start() } else { log.Fatal("【jy_docs】 read config error!") } } func rateLimitInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { 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 }