refactor: localize protobuf definitions
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 创建群组
|
||||
func Create(ctx context.Context, in *pb.GroupItem) (reply *pb.StatusReply, err error) {
|
||||
func Create(ctx context.Context, in *pb.GroupItem) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -54,7 +54,7 @@ func Create(ctx context.Context, in *pb.GroupItem) (reply *pb.StatusReply, err e
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: record.Identity,
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// 解散群组
|
||||
func Disband(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func Disband(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -25,7 +25,7 @@ func Disband(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, e
|
||||
|
||||
// TODO: add your logic code & delete this line.
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 修改群组信息
|
||||
func Modify(ctx context.Context, in *pb.GroupItem) (reply *pb.StatusReply, err error) {
|
||||
func Modify(ctx context.Context, in *pb.GroupItem) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -38,7 +38,7 @@ func Modify(ctx context.Context, in *pb.GroupItem) (reply *pb.StatusReply, err e
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 申请加群
|
||||
func DoJoin(ctx context.Context, in *pb.DoJoinRequest) (reply *pb.StatusReply, err error) {
|
||||
func DoJoin(ctx context.Context, in *pb.DoJoinRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -54,7 +54,7 @@ func DoJoin(ctx context.Context, in *pb.DoJoinRequest) (reply *pb.StatusReply, e
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 踢人
|
||||
func DoKick(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
|
||||
func DoKick(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -37,7 +37,7 @@ func DoKick(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply,
|
||||
//更新群组表数据统计
|
||||
models.UpsetGroupMemberTotal(in.GroupIdentity, "-")
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 退群
|
||||
func DoQuit(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
|
||||
func DoQuit(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -39,7 +39,7 @@ func DoQuit(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply,
|
||||
models.UpsetGroupMemberTotal(in.GroupIdentity, "-")
|
||||
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 设置或取消管理员
|
||||
func DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
|
||||
func DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -39,7 +39,7 @@ func DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusR
|
||||
//取消管理员
|
||||
impl.DBService.Model(&models.GroupMember{}).Where("identity=?", in.Identity).UpdateColumn("role", ROLE_MEMBER)
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 申请加群处理
|
||||
func JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusReply, err error) {
|
||||
func JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.DataStatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -60,7 +60,7 @@ func JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (reply *pb.StatusR
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/group/internal/logic/basic"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type BasicServer struct {
|
||||
@@ -31,16 +31,16 @@ func (s *BasicServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.GroupIt
|
||||
}
|
||||
|
||||
// 创建群组
|
||||
func (s *BasicServer) Create(ctx context.Context, in *pb.GroupItem) (*pb.StatusReply, error) {
|
||||
func (s *BasicServer) Create(ctx context.Context, in *pb.GroupItem) (*pb.DataStatusReply, error) {
|
||||
return basic.Create(ctx, in)
|
||||
}
|
||||
|
||||
// 修改群组信息
|
||||
func (s *BasicServer) Modify(ctx context.Context, in *pb.GroupItem) (*pb.StatusReply, error) {
|
||||
func (s *BasicServer) Modify(ctx context.Context, in *pb.GroupItem) (*pb.DataStatusReply, error) {
|
||||
return basic.Modify(ctx, in)
|
||||
}
|
||||
|
||||
// 解散群组
|
||||
func (s *BasicServer) Disband(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *BasicServer) Disband(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return basic.Disband(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/group/internal/logic/member"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type MemberServer struct {
|
||||
@@ -21,7 +21,7 @@ func (s *MemberServer) Fetch(ctx context.Context, in *pb.IdentRequest) (*pb.Grou
|
||||
}
|
||||
|
||||
// 申请加群
|
||||
func (s *MemberServer) DoJoin(ctx context.Context, in *pb.DoJoinRequest) (*pb.StatusReply, error) {
|
||||
func (s *MemberServer) DoJoin(ctx context.Context, in *pb.DoJoinRequest) (*pb.DataStatusReply, error) {
|
||||
return member.DoJoin(ctx, in)
|
||||
}
|
||||
|
||||
@@ -31,21 +31,21 @@ func (s *MemberServer) JoinFetch(ctx context.Context, in *pb.Empty) (*pb.JoinFet
|
||||
}
|
||||
|
||||
// 申请加群处理
|
||||
func (s *MemberServer) JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (*pb.StatusReply, error) {
|
||||
func (s *MemberServer) JoinDoHandle(ctx context.Context, in *pb.GroupOPRequest) (*pb.DataStatusReply, error) {
|
||||
return member.JoinDoHandle(ctx, in)
|
||||
}
|
||||
|
||||
// 设置或取消管理员
|
||||
func (s *MemberServer) DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (*pb.StatusReply, error) {
|
||||
func (s *MemberServer) DoSetManager(ctx context.Context, in *pb.GroupOPRequest) (*pb.DataStatusReply, error) {
|
||||
return member.DoSetManager(ctx, in)
|
||||
}
|
||||
|
||||
// 踢人
|
||||
func (s *MemberServer) DoKick(ctx context.Context, in *pb.GroupOPRequest) (*pb.StatusReply, error) {
|
||||
func (s *MemberServer) DoKick(ctx context.Context, in *pb.GroupOPRequest) (*pb.DataStatusReply, error) {
|
||||
return member.DoKick(ctx, in)
|
||||
}
|
||||
|
||||
// 退群
|
||||
func (s *MemberServer) DoQuit(ctx context.Context, in *pb.GroupOPRequest) (*pb.StatusReply, error) {
|
||||
func (s *MemberServer) DoQuit(ctx context.Context, in *pb.GroupOPRequest) (*pb.DataStatusReply, error) {
|
||||
return member.DoQuit(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,28 +2,27 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
pb "bsm/full/module/social/group/pb"
|
||||
"context"
|
||||
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
Mux *gwRuntime.ServeMux
|
||||
Grpc *grpc.Server
|
||||
Ctx context.Context
|
||||
Mux *gwRuntime.ServeMux
|
||||
grpcConns map[string]*grpc.ClientConn // 连接池
|
||||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux(
|
||||
gwRuntime.WithForwardResponseRewriter(responseEnvelope),
|
||||
)}
|
||||
srv := &Server{
|
||||
Ctx: context.Background(),
|
||||
Grpc: grpc.NewServer(),
|
||||
grpcConns: make(map[string]*grpc.ClientConn),
|
||||
}
|
||||
|
||||
// register service to grpc.Server
|
||||
pb.RegisterBasicServer(srv.Grpc, NewBasicServer())
|
||||
@@ -31,43 +30,5 @@ func New(addr string) *Server {
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 将服务注册到Gateway
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
pb.RegisterBasicHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterMemberHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
|
||||
// Register services swagger
|
||||
srv.RegisterSwagger()
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
// RegisterSwagger 注册swagger
|
||||
func (s *Server) RegisterSwagger() {
|
||||
srvKey := strings.ToLower(vars.ServiceKey)
|
||||
s.Mux.HandlePath("GET", "/"+srvKey+".swagger.json", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
bytes, err := os.ReadFile("./swagger/" + srvKey + ".swagger.json")
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
w.Write([]byte(err.Error()))
|
||||
return
|
||||
}
|
||||
w.Write(bytes)
|
||||
return
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
// response envelope
|
||||
func responseEnvelope(_ context.Context, response proto.Message) (interface{}, error) {
|
||||
name := string(response.ProtoReflect().Descriptor().Name())
|
||||
if name == "Status" || name == "Error" || name == "StatusReply" {
|
||||
return response, nil
|
||||
}
|
||||
return map[string]any{
|
||||
"code": 0,
|
||||
"message": "OK",
|
||||
"result": response,
|
||||
}, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user