refactor: localize protobuf definitions
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
)
|
||||
|
||||
// 执行关注
|
||||
func Do(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func Do(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 Do(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err er
|
||||
|
||||
// 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 Doing(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func Doing(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -37,7 +37,7 @@ func Doing(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 关注状态
|
||||
func State(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func State(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -29,7 +29,7 @@ func State(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err
|
||||
err = impl.DBService.Where("from_identity=? and to_identity=?", auth.Identity, in.Identity).First(&data).Error
|
||||
if err != nil {
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
@@ -39,7 +39,7 @@ func State(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err
|
||||
}
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: data.Identity,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 撤销关注
|
||||
func Undo(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func Undo(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -28,7 +28,7 @@ func Undo(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 好友申请
|
||||
func ApplyDo(ctx context.Context, in *pb.ApplyDoRequest) (reply *pb.StatusReply, err error) {
|
||||
func ApplyDo(ctx context.Context, in *pb.ApplyDoRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -61,7 +61,7 @@ func ApplyDo(ctx context.Context, in *pb.ApplyDoRequest) (reply *pb.StatusReply,
|
||||
}
|
||||
|
||||
//EventMQ:向mesh MQ中心发送报文,请求推送消息以及更新Cache.
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 好友申请Do:留言
|
||||
func ApplyDoMessage(ctx context.Context, in *pb.ApplyMessageRequest) (reply *pb.StatusReply, err error) {
|
||||
func ApplyDoMessage(ctx context.Context, in *pb.ApplyMessageRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -44,7 +44,7 @@ func ApplyDoMessage(ctx context.Context, in *pb.ApplyMessageRequest) (reply *pb.
|
||||
}
|
||||
|
||||
//EventMQ:向mesh MQ中心发送报文,请求推送消息以及更新Cache.
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -16,7 +16,7 @@ import (
|
||||
)
|
||||
|
||||
// 好友申请Do:确认
|
||||
func ApplyDoPass(ctx context.Context, in *pb.ApplyDoPassRequest) (reply *pb.StatusReply, err error) {
|
||||
func ApplyDoPass(ctx context.Context, in *pb.ApplyDoPassRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -45,7 +45,7 @@ func ApplyDoPass(ctx context.Context, in *pb.ApplyDoPassRequest) (reply *pb.Stat
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 好友申请Do:拒绝
|
||||
func ApplyDoReject(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func ApplyDoReject(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -30,7 +30,7 @@ func ApplyDoReject(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusRe
|
||||
}
|
||||
|
||||
//EventMQ:向mesh MQ中心发送报文,请求推送消息以及更新Cache.
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 删除好友
|
||||
func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -30,7 +30,7 @@ func Delete(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, er
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 受你欢迎的,置顶
|
||||
func DoPopular(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func DoPopular(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -30,7 +30,7 @@ func DoPopular(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply,
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 修改好友备注名称,如果要清除备注,直接nickname传空值
|
||||
func ModifyNickname(ctx context.Context, in *pb.ModifyNicknameRequest) (reply *pb.StatusReply, err error) {
|
||||
func ModifyNickname(ctx context.Context, in *pb.ModifyNicknameRequest) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -29,7 +29,7 @@ func ModifyNickname(ctx context.Context, in *pb.ModifyNicknameRequest) (reply *p
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 创建标签
|
||||
func TagDoCreate(ctx context.Context, in *pb.TagDoCreateRequest) (reply *pb.StatusReply, err error) {
|
||||
func TagDoCreate(ctx context.Context, in *pb.TagDoCreateRequest) (reply *pb.DataStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -54,7 +54,7 @@ func TagDoCreate(ctx context.Context, in *pb.TagDoCreateRequest) (reply *pb.Stat
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 更新标签成员
|
||||
func TagDoUpdate(ctx context.Context, in *pb.TagDoUpdateRequest) (reply *pb.StatusReply, err error) {
|
||||
func TagDoUpdate(ctx context.Context, in *pb.TagDoUpdateRequest) (reply *pb.DataStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -44,7 +44,7 @@ func TagDoUpdate(ctx context.Context, in *pb.TagDoUpdateRequest) (reply *pb.Stat
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 取消受欢迎的,取消置顶
|
||||
func UndoPopular(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func UndoPopular(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -30,7 +30,7 @@ func UndoPopular(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusRepl
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 执行忽略
|
||||
func DoIgnore(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func DoIgnore(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -29,7 +29,7 @@ func DoIgnore(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply,
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 执行通过,加为好友
|
||||
func DoJoin(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func DoJoin(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
// parse authorization meta.
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
@@ -37,7 +37,7 @@ func DoJoin(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, er
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixMilli(),
|
||||
}, nil
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/relation/internal/logic/follow"
|
||||
pb "bsm/full/module/social/relation/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type FollowServer struct {
|
||||
@@ -16,17 +16,17 @@ func NewFollowServer() *FollowServer {
|
||||
}
|
||||
|
||||
// 执行关注
|
||||
func (s *FollowServer) Doing(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *FollowServer) Doing(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return follow.Doing(ctx, in)
|
||||
}
|
||||
|
||||
// 撤销关注
|
||||
func (s *FollowServer) Undo(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *FollowServer) Undo(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return follow.Undo(ctx, in)
|
||||
}
|
||||
|
||||
// 关注状态
|
||||
func (s *FollowServer) State(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *FollowServer) State(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return follow.State(ctx, in)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/relation/internal/logic/friend"
|
||||
pb "bsm/full/module/social/relation/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type FriendServer struct {
|
||||
@@ -31,22 +31,22 @@ func (s *FriendServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.Relati
|
||||
}
|
||||
|
||||
// 修改好友备注名称,如果要清除备注,直接nickname传空值
|
||||
func (s *FriendServer) ModifyNickname(ctx context.Context, in *pb.ModifyNicknameRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) ModifyNickname(ctx context.Context, in *pb.ModifyNicknameRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.ModifyNickname(ctx, in)
|
||||
}
|
||||
|
||||
// 受你欢迎的,置顶
|
||||
func (s *FriendServer) DoPopular(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) DoPopular(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.DoPopular(ctx, in)
|
||||
}
|
||||
|
||||
// 取消受欢迎的,取消置顶
|
||||
func (s *FriendServer) UndoPopular(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) UndoPopular(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.UndoPopular(ctx, in)
|
||||
}
|
||||
|
||||
// 删除好友
|
||||
func (s *FriendServer) Delete(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) Delete(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.Delete(ctx, in)
|
||||
}
|
||||
|
||||
@@ -61,22 +61,22 @@ func (s *FriendServer) ApplyGet(ctx context.Context, in *pb.IdentRequest) (*pb.F
|
||||
}
|
||||
|
||||
// 好友申请
|
||||
func (s *FriendServer) ApplyDo(ctx context.Context, in *pb.ApplyDoRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) ApplyDo(ctx context.Context, in *pb.ApplyDoRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.ApplyDo(ctx, in)
|
||||
}
|
||||
|
||||
// 好友申请Do:留言
|
||||
func (s *FriendServer) ApplyDoMessage(ctx context.Context, in *pb.ApplyMessageRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) ApplyDoMessage(ctx context.Context, in *pb.ApplyMessageRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.ApplyDoMessage(ctx, in)
|
||||
}
|
||||
|
||||
// 好友申请Do:确认
|
||||
func (s *FriendServer) ApplyDoPass(ctx context.Context, in *pb.ApplyDoPassRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) ApplyDoPass(ctx context.Context, in *pb.ApplyDoPassRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.ApplyDoPass(ctx, in)
|
||||
}
|
||||
|
||||
// 好友申请Do:拒绝
|
||||
func (s *FriendServer) ApplyDoReject(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) ApplyDoReject(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.ApplyDoReject(ctx, in)
|
||||
}
|
||||
|
||||
@@ -91,11 +91,11 @@ func (s *FriendServer) TagMemberFetch(ctx context.Context, in *pb.IdentRequest)
|
||||
}
|
||||
|
||||
// 创建标签
|
||||
func (s *FriendServer) TagDoCreate(ctx context.Context, in *pb.TagDoCreateRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) TagDoCreate(ctx context.Context, in *pb.TagDoCreateRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.TagDoCreate(ctx, in)
|
||||
}
|
||||
|
||||
// 更新标签成员
|
||||
func (s *FriendServer) TagDoUpdate(ctx context.Context, in *pb.TagDoUpdateRequest) (*pb.StatusReply, error) {
|
||||
func (s *FriendServer) TagDoUpdate(ctx context.Context, in *pb.TagDoUpdateRequest) (*pb.DataStatusReply, error) {
|
||||
return friend.TagDoUpdate(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/relation/internal/logic/match"
|
||||
pb "bsm/full/module/social/relation/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type MatchServer struct {
|
||||
@@ -26,11 +26,11 @@ func (s *MatchServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.Relatio
|
||||
}
|
||||
|
||||
// 执行通过,加为好友
|
||||
func (s *MatchServer) DoJoin(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *MatchServer) DoJoin(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return match.DoJoin(ctx, in)
|
||||
}
|
||||
|
||||
// 执行忽略
|
||||
func (s *MatchServer) DoIgnore(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *MatchServer) DoIgnore(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return match.DoIgnore(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,28 +2,27 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
pb "bsm/full/module/social/relation/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.RegisterFollowServer(srv.Grpc, NewFollowServer())
|
||||
@@ -32,44 +31,5 @@ func New(addr string) *Server {
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 将服务注册到Gateway
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
pb.RegisterFollowHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterFriendHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterMatchHandlerFromEndpoint(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