mirror of
https://github.com/openimsdk/open-im-server.git
synced 2026-05-10 03:56:00 +08:00
api to rpc use generic paradigm
This commit is contained in:
+1227
-1242
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,107 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
"Open_IM/pkg/api_struct"
|
||||
"Open_IM/pkg/proto/group"
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"google.golang.org/grpc"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type Ignore struct{}
|
||||
|
||||
func temp(client group.GroupClient, ctx context.Context, in *group.KickGroupMemberReq, opts ...grpc.CallOption) (*group.KickGroupMemberResp, error) {
|
||||
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
type ApiBind[A, B any] interface {
|
||||
OperationID() string
|
||||
OpUserID() (string, error)
|
||||
Bind(*A) error
|
||||
Error(error)
|
||||
Write(*B)
|
||||
}
|
||||
|
||||
func NewApiBind[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]) Error(err error) {
|
||||
//TODO implement me
|
||||
}
|
||||
|
||||
func (g *ginApiBind[A, B]) Write(b *B) {
|
||||
//TODO implement me
|
||||
}
|
||||
|
||||
func TestName(t *testing.T) {
|
||||
//var bind ApiBind[int, int]
|
||||
//NewRpc(bind, "", group.NewGroupClient, temp)
|
||||
|
||||
var c *gin.Context
|
||||
NewRpc(NewApiBind[api_struct.KickGroupMemberReq, api_struct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember)
|
||||
|
||||
}
|
||||
|
||||
func KickGroupMember(c *gin.Context) {
|
||||
// 默认 全部自动
|
||||
NewRpc(NewApiBind[api_struct.KickGroupMemberReq, api_struct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember).Execute()
|
||||
// 可以自定义编辑请求和响应
|
||||
NewRpc(NewApiBind[api_struct.KickGroupMemberReq, api_struct.KickGroupMemberResp](c), "", group.NewGroupClient, group.GroupClient.KickGroupMember).Before(func(apiReq *interface{}, rpcReq *interface{}, bind func() error) error {
|
||||
return bind()
|
||||
}).After(func(rpcResp api_struct.KickGroupMemberResp, apiResp *interface{}, bind func() error) error {
|
||||
return bind()
|
||||
}).Execute()
|
||||
}
|
||||
|
||||
func NewRpc[A, B any, C, D any, Z any](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)) *RpcRun[A, B, C, D, Z] {
|
||||
return &RpcRun[A, B, C, D, Z]{
|
||||
bind: bind,
|
||||
name: name,
|
||||
client: client,
|
||||
rpc: rpc,
|
||||
}
|
||||
}
|
||||
|
||||
type RpcRun[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 C, rpcReq C, bind func() error) error
|
||||
after func(rpcResp B, apiResp D, bind func() error) error
|
||||
}
|
||||
|
||||
func (a *RpcRun[A, B, C, D, Z]) Before(fn func(apiReq C, rpcReq C, bind func() error) error) *RpcRun[A, B, C, D, Z] {
|
||||
a.before = fn
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *RpcRun[A, B, C, D, Z]) After(fn func(rpcResp B, apiResp D, bind func() error) error) *RpcRun[A, B, C, D, Z] {
|
||||
a.after = fn
|
||||
return a
|
||||
}
|
||||
|
||||
func (a *RpcRun[A, B, C, D, Z]) Execute() {
|
||||
|
||||
}
|
||||
@@ -1,89 +1,89 @@
|
||||
package group
|
||||
|
||||
import (
|
||||
jsonData "Open_IM/internal/utils"
|
||||
api "Open_IM/pkg/api_struct"
|
||||
"Open_IM/pkg/common/config"
|
||||
"Open_IM/pkg/common/log"
|
||||
"Open_IM/pkg/common/token_verify"
|
||||
"Open_IM/pkg/getcdv3"
|
||||
rpc "Open_IM/pkg/proto/group"
|
||||
"Open_IM/pkg/utils"
|
||||
"context"
|
||||
"github.com/gin-gonic/gin"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func GetJoinedSuperGroupList(c *gin.Context) {
|
||||
req := api.GetJoinedSuperGroupListReq{}
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.NewError("0", "BindJSON failed ", err.Error())
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
|
||||
ok, opUserID, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
|
||||
if !ok {
|
||||
errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
return
|
||||
}
|
||||
reqPb := rpc.GetJoinedSuperGroupListReq{OperationID: req.OperationID, OpUserID: opUserID, UserID: req.FromUserID}
|
||||
etcdConn := rpc.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID)
|
||||
if etcdConn == nil {
|
||||
errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
return
|
||||
}
|
||||
client := rpc.NewGroupClient(etcdConn)
|
||||
rpcResp, err := client.GetJoinedSuperGroupList(context.Background(), &reqPb)
|
||||
if err != nil {
|
||||
log.NewError(req.OperationID, "InviteUserToGroup failed ", err.Error(), reqPb.String())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
GroupListResp := api.GetJoinedSuperGroupListResp{GetJoinedGroupListResp: api.GetJoinedGroupListResp{CommResp: api.CommResp{ErrCode: rpcResp.CommonResp.ErrCode, ErrMsg: rpcResp.CommonResp.ErrMsg}, GroupInfoList: rpcResp.GroupList}}
|
||||
GroupListResp.Data = jsonData.JsonDataList(GroupListResp.GroupInfoList)
|
||||
log.NewInfo(req.OperationID, "GetJoinedSuperGroupList api return ", GroupListResp)
|
||||
c.JSON(http.StatusOK, GroupListResp)
|
||||
}
|
||||
|
||||
func GetSuperGroupsInfo(c *gin.Context) {
|
||||
req := api.GetSuperGroupsInfoReq{}
|
||||
if err := c.BindJSON(&req); err != nil {
|
||||
log.NewError("0", "BindJSON failed ", err.Error())
|
||||
c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
|
||||
ok, opUserID, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
|
||||
if !ok {
|
||||
errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
return
|
||||
}
|
||||
reqPb := rpc.GetSuperGroupsInfoReq{OperationID: req.OperationID, OpUserID: opUserID, GroupIDList: req.GroupIDList}
|
||||
etcdConn := rpc.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID)
|
||||
if etcdConn == nil {
|
||||
errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
|
||||
log.NewError(req.OperationID, errMsg)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
return
|
||||
}
|
||||
client := rpc.NewGroupClient(etcdConn)
|
||||
rpcResp, err := client.GetSuperGroupsInfo(context.Background(), &reqPb)
|
||||
if err != nil {
|
||||
log.NewError(req.OperationID, "InviteUserToGroup failed ", err.Error(), reqPb.String())
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
resp := api.GetSuperGroupsInfoResp{GetGroupInfoResp: api.GetGroupInfoResp{CommResp: api.CommResp{ErrCode: rpcResp.CommonResp.ErrCode, ErrMsg: rpcResp.CommonResp.ErrMsg}, GroupInfoList: rpcResp.GroupInfoList}}
|
||||
resp.Data = jsonData.JsonDataList(resp.GroupInfoList)
|
||||
log.NewInfo(req.OperationID, "GetGroupsInfo api return ", resp)
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
//import (
|
||||
// jsonData "Open_IM/internal/utils"
|
||||
// api "Open_IM/pkg/api_struct"
|
||||
// "Open_IM/pkg/common/config"
|
||||
// "Open_IM/pkg/common/log"
|
||||
// "Open_IM/pkg/common/token_verify"
|
||||
// "Open_IM/pkg/getcdv3"
|
||||
// rpc "Open_IM/pkg/proto/group"
|
||||
// "Open_IM/pkg/utils"
|
||||
// "context"
|
||||
// "github.com/gin-gonic/gin"
|
||||
// "net/http"
|
||||
// "strings"
|
||||
//)
|
||||
//
|
||||
//func GetJoinedSuperGroupList(c *gin.Context) {
|
||||
// req := api.GetJoinedSuperGroupListReq{}
|
||||
// if err := c.BindJSON(&req); err != nil {
|
||||
// log.NewError("0", "BindJSON failed ", err.Error())
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
// return
|
||||
// }
|
||||
// log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
|
||||
// ok, opUserID, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
|
||||
// if !ok {
|
||||
// errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
|
||||
// log.NewError(req.OperationID, errMsg)
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
// return
|
||||
// }
|
||||
// reqPb := rpc.GetJoinedSuperGroupListReq{OperationID: req.OperationID, OpUserID: opUserID, UserID: req.FromUserID}
|
||||
// etcdConn := rpc.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID)
|
||||
// if etcdConn == nil {
|
||||
// errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
|
||||
// log.NewError(req.OperationID, errMsg)
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
// return
|
||||
// }
|
||||
// client := rpc.NewGroupClient(etcdConn)
|
||||
// rpcResp, err := client.GetJoinedSuperGroupList(context.Background(), &reqPb)
|
||||
// if err != nil {
|
||||
// log.NewError(req.OperationID, "InviteUserToGroup failed ", err.Error(), reqPb.String())
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
|
||||
// return
|
||||
// }
|
||||
// GroupListResp := api.GetJoinedSuperGroupListResp{GetJoinedGroupListResp: api.GetJoinedGroupListResp{CommResp: api.CommResp{ErrCode: rpcResp.CommonResp.ErrCode, ErrMsg: rpcResp.CommonResp.ErrMsg}, GroupInfoList: rpcResp.GroupList}}
|
||||
// GroupListResp.Data = jsonData.JsonDataList(GroupListResp.GroupInfoList)
|
||||
// log.NewInfo(req.OperationID, "GetJoinedSuperGroupList api return ", GroupListResp)
|
||||
// c.JSON(http.StatusOK, GroupListResp)
|
||||
//}
|
||||
//
|
||||
//func GetSuperGroupsInfo(c *gin.Context) {
|
||||
// req := api.GetSuperGroupsInfoReq{}
|
||||
// if err := c.BindJSON(&req); err != nil {
|
||||
// log.NewError("0", "BindJSON failed ", err.Error())
|
||||
// c.JSON(http.StatusBadRequest, gin.H{"errCode": 400, "errMsg": err.Error()})
|
||||
// return
|
||||
// }
|
||||
// log.NewInfo(req.OperationID, utils.GetSelfFuncName(), "req: ", req)
|
||||
// ok, opUserID, errInfo := token_verify.GetUserIDFromToken(c.Request.Header.Get("token"), req.OperationID)
|
||||
// if !ok {
|
||||
// errMsg := req.OperationID + " " + "GetUserIDFromToken failed " + errInfo + " token:" + c.Request.Header.Get("token")
|
||||
// log.NewError(req.OperationID, errMsg)
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
// return
|
||||
// }
|
||||
// reqPb := rpc.GetSuperGroupsInfoReq{OperationID: req.OperationID, OpUserID: opUserID, GroupIDList: req.GroupIDList}
|
||||
// etcdConn := rpc.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImGroupName, req.OperationID)
|
||||
// if etcdConn == nil {
|
||||
// errMsg := req.OperationID + "getcdv3.GetDefaultConn == nil"
|
||||
// log.NewError(req.OperationID, errMsg)
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": errMsg})
|
||||
// return
|
||||
// }
|
||||
// client := rpc.NewGroupClient(etcdConn)
|
||||
// rpcResp, err := client.GetSuperGroupsInfo(context.Background(), &reqPb)
|
||||
// if err != nil {
|
||||
// log.NewError(req.OperationID, "InviteUserToGroup failed ", err.Error(), reqPb.String())
|
||||
// c.JSON(http.StatusInternalServerError, gin.H{"errCode": 500, "errMsg": err.Error()})
|
||||
// return
|
||||
// }
|
||||
//
|
||||
// resp := api.GetSuperGroupsInfoResp{GetGroupInfoResp: api.GetGroupInfoResp{CommResp: api.CommResp{ErrCode: rpcResp.CommonResp.ErrCode, ErrMsg: rpcResp.CommonResp.ErrMsg}, GroupInfoList: rpcResp.GroupInfoList}}
|
||||
// resp.Data = jsonData.JsonDataList(resp.GroupInfoList)
|
||||
// log.NewInfo(req.OperationID, "GetGroupsInfo api return ", resp)
|
||||
// c.JSON(http.StatusOK, resp)
|
||||
//}
|
||||
|
||||
Reference in New Issue
Block a user