mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-04 00:55:59 +08:00
api2rpc
This commit is contained in:
+40
-43
@@ -1,58 +1,55 @@
|
||||
package common
|
||||
package api2rpc
|
||||
|
||||
import (
|
||||
"OpenIM/pkg/common/constant"
|
||||
"OpenIM/pkg/common/log"
|
||||
"OpenIM/pkg/common/tracelog"
|
||||
utils2 "OpenIM/pkg/utils"
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/gin-gonic/gin"
|
||||
"google.golang.org/grpc/status"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcClientFunc interface{}, rpcFuncName string) {
|
||||
if rpcName == "" {
|
||||
rpcName = utils2.GetFuncName(1)
|
||||
}
|
||||
logFuncName := fmt.Sprintf("[ApiToRpc: %s]%s", utils2.GetFuncName(1), rpcFuncName)
|
||||
ctx := tracelog.NewCtx(c, rpcFuncName)
|
||||
defer log.ShowLog(ctx)
|
||||
if err := c.BindJSON(apiReq); err != nil {
|
||||
WriteErrorResponse(ctx, "BindJSON", err)
|
||||
return
|
||||
}
|
||||
tracelog.SetCtxInfo(ctx, logFuncName, nil, "apiReq", apiReq)
|
||||
etcdConn, err := rpc.GetConn(ctx, rpcName)
|
||||
if err != nil {
|
||||
WriteErrorResponse(ctx, "GetConn", err)
|
||||
return
|
||||
}
|
||||
rpcClient := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{
|
||||
reflect.ValueOf(etcdConn),
|
||||
})[0].MethodByName(rpcFuncName) // rpcClient func
|
||||
rpcReqPtr := reflect.New(rpcClient.Type().In(1).Elem()) // *req
|
||||
CopyAny(apiReq, rpcReqPtr.Interface())
|
||||
tracelog.SetCtxInfo(ctx, logFuncName, nil, "opUserID", c.GetString("opUserID"), "callRpcReq", rpcString(rpcReqPtr.Elem().Interface()))
|
||||
respArr := rpcClient.Call([]reflect.Value{
|
||||
reflect.ValueOf(context.Context(c)), // context.Context (ctx operationID. opUserID)
|
||||
rpcReqPtr, // rpcClient apiReq
|
||||
}) // respArr => (apiResp, error)
|
||||
if !respArr[1].IsNil() { // rpcClient err != nil
|
||||
err := respArr[1].Interface().(error)
|
||||
WriteErrorResponse(ctx, rpcFuncName, err, "callRpcResp", "error")
|
||||
return
|
||||
}
|
||||
rpcResp := respArr[0].Elem()
|
||||
tracelog.SetCtxInfo(ctx, rpcFuncName, nil, "callRpcResp", rpcString(rpcResp.Interface()))
|
||||
if apiResp != nil {
|
||||
CopyAny(rpcResp.Interface(), apiResp)
|
||||
}
|
||||
SetSuccess(ctx, rpcFuncName, apiResp)
|
||||
}
|
||||
//func ApiToRpc(c *gin.Context, apiReq, apiResp interface{}, rpcName string, rpcClientFunc interface{}, rpcFuncName string) {
|
||||
// if rpcName == "" {
|
||||
// rpcName = utils2.GetFuncName(1)
|
||||
// }
|
||||
// logFuncName := fmt.Sprintf("[ApiToRpc: %s]%s", utils2.GetFuncName(1), rpcFuncName)
|
||||
// ctx := tracelog.NewCtx(c, rpcFuncName)
|
||||
// defer log.ShowLog(ctx)
|
||||
// if err := c.BindJSON(apiReq); err != nil {
|
||||
// WriteErrorResponse(ctx, "BindJSON", err)
|
||||
// return
|
||||
// }
|
||||
// tracelog.SetCtxInfo(ctx, logFuncName, nil, "apiReq", apiReq)
|
||||
// etcdConn, err := rpc.GetConn(ctx, rpcName)
|
||||
// if err != nil {
|
||||
// WriteErrorResponse(ctx, "GetConn", err)
|
||||
// return
|
||||
// }
|
||||
// rpcClient := reflect.ValueOf(rpcClientFunc).Call([]reflect.Value{
|
||||
// reflect.ValueOf(etcdConn),
|
||||
// })[0].MethodByName(rpcFuncName) // rpcClient func
|
||||
// rpcReqPtr := reflect.New(rpcClient.Type().In(1).Elem()) // *req
|
||||
// CopyAny(apiReq, rpcReqPtr.Interface())
|
||||
// tracelog.SetCtxInfo(ctx, logFuncName, nil, "opUserID", c.GetString("opUserID"), "callRpcReq", rpcString(rpcReqPtr.Elem().Interface()))
|
||||
// respArr := rpcClient.Call([]reflect.Value{
|
||||
// reflect.ValueOf(context.Context(c)), // context.Context (ctx operationID. opUserID)
|
||||
// rpcReqPtr, // rpcClient apiReq
|
||||
// }) // respArr => (apiResp, error)
|
||||
// if !respArr[1].IsNil() { // rpcClient err != nil
|
||||
// err := respArr[1].Interface().(error)
|
||||
// WriteErrorResponse(ctx, rpcFuncName, err, "callRpcResp", "error")
|
||||
// return
|
||||
// }
|
||||
// rpcResp := respArr[0].Elem()
|
||||
// tracelog.SetCtxInfo(ctx, rpcFuncName, nil, "callRpcResp", rpcString(rpcResp.Interface()))
|
||||
// if apiResp != nil {
|
||||
// CopyAny(rpcResp.Interface(), apiResp)
|
||||
// }
|
||||
// SetSuccess(ctx, rpcFuncName, apiResp)
|
||||
//}
|
||||
|
||||
func rpcString(v interface{}) string {
|
||||
if s, ok := v.(interface{ String() string }); ok {
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package api2rpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Ignore struct{}
|
||||
|
||||
type ApiBind[A, B any] interface {
|
||||
OperationID() string
|
||||
OpUserID() (string, error)
|
||||
Bind(*A) error
|
||||
Context() context.Context
|
||||
Resp(resp *B, err error)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package api2rpc
|
||||
@@ -1,4 +1,4 @@
|
||||
package common
|
||||
package api2rpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
@@ -0,0 +1,67 @@
|
||||
package api2rpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
//func KickGroupMember(c *gin.Context) {
|
||||
// // 默认 全部自动
|
||||
// //var api ApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp] = NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c)
|
||||
// var api ApiBind[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp] = nil
|
||||
// var client func(conn *grpc.ClientConn) group.GroupClient = nil
|
||||
// var rpc func(ctx context.Context, in *group.KickGroupMemberReq, opts ...grpc.CallOption) (*group.KickGroupMemberResp, error) = nil
|
||||
// //NewRpc(api, client, rpc).Name("group").Call()
|
||||
// NewRpc(api, client, rpc).Name("group").Call()
|
||||
//
|
||||
// // 可以自定义编辑请求和响应
|
||||
// //a := NewRpc(NewGin[apistruct.KickGroupMemberReq, apistruct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
// //a.Before(func(apiReq *apistruct.KickGroupMemberReq, rpcReq *group.KickGroupMemberReq, bind func() error) error {
|
||||
// // return bind()
|
||||
// //}).After(func(rpcResp *group.KickGroupMemberResp, apiResp *apistruct.KickGroupMemberResp, bind func() error) error {
|
||||
// // return bind()
|
||||
// //}).Execute()
|
||||
//}
|
||||
//
|
||||
|
||||
func NewGin[A, B any](c *gin.Context) ApiBind[A, B] {
|
||||
return &ginApiBind[A, B]{
|
||||
c: c,
|
||||
}
|
||||
}
|
||||
|
||||
type ginApiBind[A, B any] struct {
|
||||
c *gin.Context
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) OperationID() string {
|
||||
return g.c.GetHeader("operationID")
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) OpUserID() (string, error) {
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) Bind(a *A) error {
|
||||
return g.c.BindJSON(a)
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) Resp(resp *B, err error) {
|
||||
if err == nil {
|
||||
g.Write(resp)
|
||||
} else {
|
||||
g.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) Error(err error) {
|
||||
//TODO implement me
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) Write(b *B) {
|
||||
//TODO implement me
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) Context() context.Context {
|
||||
return g.c
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package api2rpc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"google.golang.org/grpc"
|
||||
)
|
||||
|
||||
// NewRpc A: apiReq B: apiResp C: rpcReq D: rpcResp Z: rpcClient (group.GroupClient)
|
||||
func NewRpc[A, B any, C, D any, Z any](bind ApiBind[A, B], client func(conn *grpc.ClientConn) Z, rpc func(client Z, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)) *Rpc[A, B, C, D, Z] {
|
||||
return &Rpc[A, B, C, D, Z]{
|
||||
bind: bind,
|
||||
client: client,
|
||||
rpc: rpc,
|
||||
}
|
||||
}
|
||||
|
||||
type Rpc[A, B any, C, D any, Z any] struct {
|
||||
bind ApiBind[A, B]
|
||||
name string
|
||||
client func(conn *grpc.ClientConn) Z
|
||||
rpc func(client Z, ctx context.Context, req *C, options ...grpc.CallOption) (*D, error)
|
||||
before func(apiReq *A, rpcReq *C, bind func() error) error
|
||||
after func(rpcResp *D, apiResp *B, bind func() error) error
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) Name(name string) *Rpc[A, B, C, D, Z] {
|
||||
a.name = name
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) Before(fn func(apiReq *A, rpcReq *C, bind func() error) error) *Rpc[A, B, C, D, Z] {
|
||||
a.before = fn
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) After(fn func(rpcResp *D, apiResp *B, bind func() error) error) *Rpc[A, B, C, D, Z] {
|
||||
a.after = fn
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) defaultCopyReq(apiReq *A, rpcReq *C) error {
|
||||
CopyAny(apiReq, rpcReq)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) defaultCopyResp(rpcResp *D, apiResp *B) error {
|
||||
CopyAny(rpcResp, apiResp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) GetGrpcConn() (*grpc.ClientConn, error) {
|
||||
return nil, nil // todo
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) execute() (*B, error) {
|
||||
var apiReq A
|
||||
if err := a.bind.Bind(&apiReq); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
opID := a.bind.OperationID()
|
||||
userID, err := a.bind.OpUserID()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
_, _ = opID, userID
|
||||
var rpcReq C
|
||||
if a.before == nil {
|
||||
err = a.defaultCopyReq(&apiReq, &rpcReq)
|
||||
} else {
|
||||
err = a.before(&apiReq, &rpcReq, func() error { return a.defaultCopyReq(&apiReq, &rpcReq) })
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
conn, err := a.GetGrpcConn()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rpcResp, err := a.rpc(a.client(conn), a.bind.Context(), &rpcReq)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var apiResp B
|
||||
if a.after == nil {
|
||||
err = a.defaultCopyResp(rpcResp, &apiResp)
|
||||
} else {
|
||||
err = a.after(rpcResp, &apiResp, func() error { return a.defaultCopyResp(rpcResp, &apiResp) })
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &apiResp, nil
|
||||
}
|
||||
|
||||
func (a *Rpc[A, B, C, D, Z]) Call() {
|
||||
a.bind.Resp(a.execute())
|
||||
}
|
||||
Reference in New Issue
Block a user