Files
open-im-server/internal/api_to_rpc/api.go
T

60 lines
1.9 KiB
Go
Raw Normal View History

2023-01-03 15:17:33 +08:00
package common
import (
2023-01-12 16:08:44 +08:00
"Open_IM/pkg/common/log"
2023-01-30 11:14:18 +08:00
"Open_IM/pkg/common/tracelog"
2023-01-11 16:55:25 +08:00
"Open_IM/pkg/getcdv3"
2023-01-12 10:42:27 +08:00
utils2 "Open_IM/pkg/utils"
2023-01-12 14:23:00 +08:00
"context"
2023-01-12 10:42:27 +08:00
"fmt"
2023-01-03 15:17:33 +08:00
"github.com/gin-gonic/gin"
"reflect"
)
2023-01-12 16:08:44 +08:00
func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcClientFunc interface{}, rpcFuncName string) {
2023-01-16 18:48:40 +08:00
if rpcName == "" {
rpcName = utils2.GetFuncName(1)
}
2023-01-12 10:42:27 +08:00
logFuncName := fmt.Sprintf("[ApiToRpc: %s]%s", utils2.GetFuncName(1), rpcFuncName)
2023-01-30 11:10:26 +08:00
ctx := tracelog.NewCtx1(c, rpcFuncName)
2023-01-16 16:37:55 +08:00
defer log.ShowLog(ctx)
2023-01-03 15:17:33 +08:00
if err := c.BindJSON(apiReq); err != nil {
2023-01-30 11:10:26 +08:00
tracelog.WriteErrorResponse(ctx, "BindJSON", err)
2023-01-03 15:17:33 +08:00
return
}
2023-01-30 11:10:26 +08:00
tracelog.SetCtxInfo(ctx, logFuncName, nil, "apiReq", apiReq)
2023-01-16 16:37:55 +08:00
etcdConn, err := getcdv3.GetConn(ctx, rpcName)
2023-01-03 15:17:33 +08:00
if err != nil {
2023-01-30 11:10:26 +08:00
tracelog.WriteErrorResponse(ctx, "GetConn", err)
2023-01-03 15:17:33 +08:00
return
}
2023-01-16 16:37:55 +08:00
rpcClient := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{
2023-01-03 15:17:33 +08:00
reflect.ValueOf(etcdConn),
2023-01-16 16:37:55 +08:00
})[0].MethodByName(rpcFuncName) // rpcClient func
rpcReqPtr := reflect.New(rpcClient.Type().In(1).Elem()) // *req
2023-01-13 17:16:56 +08:00
CopyAny(apiReq, rpcReqPtr.Interface())
2023-01-30 11:10:26 +08:00
tracelog.SetCtxInfo(ctx, logFuncName, nil, "opUserID", c.GetString("opUserID"), "callRpcReq", rpcString(rpcReqPtr.Elem().Interface()))
2023-01-16 16:37:55 +08:00
respArr := rpcClient.Call([]reflect.Value{
reflect.ValueOf(context.Context(c)), // context.Context (ctx operationID. opUserID)
rpcReqPtr, // rpcClient apiReq
2023-01-03 15:17:33 +08:00
}) // respArr => (apiResp, error)
2023-01-16 16:37:55 +08:00
if !respArr[1].IsNil() { // rpcClient err != nil
2023-01-03 15:17:33 +08:00
err := respArr[1].Interface().(error)
2023-01-30 11:10:26 +08:00
tracelog.WriteErrorResponse(ctx, rpcFuncName, err, "callRpcResp", "error")
2023-01-03 15:17:33 +08:00
return
}
rpcResp := respArr[0].Elem()
2023-01-30 11:10:26 +08:00
tracelog.SetCtxInfo(ctx, rpcFuncName, nil, "callRpcResp", rpcString(rpcResp.Interface()))
2023-01-03 15:17:33 +08:00
if apiResp != nil {
2023-01-13 17:16:56 +08:00
CopyAny(rpcResp.Interface(), apiResp)
2023-01-03 15:17:33 +08:00
}
2023-01-30 11:10:26 +08:00
tracelog.SetSuccess(ctx, rpcFuncName, apiResp)
2023-01-03 15:17:33 +08:00
}
2023-01-11 16:16:44 +08:00
2023-01-12 10:42:27 +08:00
func rpcString(v interface{}) string {
if s, ok := v.(interface{ String() string }); ok {
return s.String()
}
return fmt.Sprintf("%+v", v)
}