refactor: localize protobuf definitions
This commit is contained in:
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 计数操作
|
||||
func Action(ctx context.Context, in *pb.PostActionRequest) (reply *pb.StatusReply, err error) {
|
||||
func Action(ctx context.Context, in *pb.PostActionRequest) (reply *pb.DataStatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -26,7 +26,7 @@ func Action(ctx context.Context, in *pb.PostActionRequest) (reply *pb.StatusRepl
|
||||
printer.Error(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
)
|
||||
|
||||
// 新建评论
|
||||
func AddComment(ctx context.Context, in *pb.CommentItem) (reply *pb.StatusReply, err error) {
|
||||
func AddComment(ctx context.Context, in *pb.CommentItem) (reply *pb.DataStatusReply, err error) {
|
||||
auth, err := service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -32,7 +32,7 @@ func AddComment(ctx context.Context, in *pb.CommentItem) (reply *pb.StatusReply,
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: data.Identity,
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 修改推文
|
||||
func Change(ctx context.Context, in *pb.PostItem) (reply *pb.StatusReply, err error) {
|
||||
func Change(ctx context.Context, in *pb.PostItem) (reply *pb.DataStatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -42,7 +42,7 @@ func Change(ctx context.Context, in *pb.PostItem) (reply *pb.StatusReply, err er
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: postData.Identity,
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
@@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// 新建推文
|
||||
func Create(ctx context.Context, in *pb.PostItem) (reply *pb.StatusReply, err error) {
|
||||
func Create(ctx context.Context, in *pb.PostItem) (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.PostItem) (reply *pb.StatusReply, err er
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: postData.Identity,
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
// 删除评论
|
||||
func DeleteComment(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func DeleteComment(ctx context.Context, in *pb.IdentRequest) (reply *pb.DataStatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -26,7 +26,7 @@ func DeleteComment(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusRe
|
||||
printer.Error(err.Error())
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
return &pb.StatusReply{
|
||||
return &pb.DataStatusReply{
|
||||
Data: vars.OK,
|
||||
Timeseq: time.Now().UnixNano(),
|
||||
}, nil
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
)
|
||||
|
||||
// 删除推文
|
||||
func Remove(ctx context.Context, in *pb.IdentRequest) (reply *pb.StatusReply, err error) {
|
||||
func Remove(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 Remove(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().UnixNano(),
|
||||
}, nil
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
)
|
||||
|
||||
// 新建标签
|
||||
func Create(ctx context.Context, in *pb.AddTagRequest) (reply *pb.StatusReply, err error) {
|
||||
func Create(ctx context.Context, in *pb.AddTagRequest) (reply *pb.DataStatusReply, err error) {
|
||||
_, err = service.ParseMetaCtx(ctx, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -32,6 +32,6 @@ func Create(ctx context.Context, in *pb.AddTagRequest) (reply *pb.StatusReply, e
|
||||
return nil, errcode.ErrDB
|
||||
}
|
||||
|
||||
return &pb.StatusReply{}, nil
|
||||
return &pb.DataStatusReply{}, nil
|
||||
|
||||
}
|
||||
|
||||
@@ -2,28 +2,27 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
pb "bsm/full/module/social/feed/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.RegisterPostServer(srv.Grpc, NewPostServer())
|
||||
@@ -33,45 +32,5 @@ func New(addr string) *Server {
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 将服务注册到Gateway
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
pb.RegisterPostHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterSettingHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterTagHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterTimelineHandlerFromEndpoint(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
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/feed/internal/logic/post"
|
||||
pb "bsm/full/module/social/feed/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type PostServer struct {
|
||||
@@ -16,22 +16,22 @@ func NewPostServer() *PostServer {
|
||||
}
|
||||
|
||||
// 推文列表
|
||||
func (s *PostServer) Fetch(ctx context.Context, in *pb.FetchRequest) (*pb.PostListReply, error) {
|
||||
func (s *PostServer) Fetch(ctx context.Context, in *pb.FetchRequest) (*pb.FeedPostListReply, error) {
|
||||
return post.Fetch(ctx, in)
|
||||
}
|
||||
|
||||
// 新建推文
|
||||
func (s *PostServer) Create(ctx context.Context, in *pb.PostItem) (*pb.StatusReply, error) {
|
||||
func (s *PostServer) Create(ctx context.Context, in *pb.FeedPostItem) (*pb.DataStatusReply, error) {
|
||||
return post.Create(ctx, in)
|
||||
}
|
||||
|
||||
// 修改推文
|
||||
func (s *PostServer) Change(ctx context.Context, in *pb.PostItem) (*pb.StatusReply, error) {
|
||||
func (s *PostServer) Change(ctx context.Context, in *pb.FeedPostItem) (*pb.DataStatusReply, error) {
|
||||
return post.Change(ctx, in)
|
||||
}
|
||||
|
||||
// 删除推文
|
||||
func (s *PostServer) Remove(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *PostServer) Remove(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return post.Remove(ctx, in)
|
||||
}
|
||||
|
||||
@@ -41,16 +41,16 @@ func (s *PostServer) CommentList(ctx context.Context, in *pb.FetchRequest) (*pb.
|
||||
}
|
||||
|
||||
// 新建评论
|
||||
func (s *PostServer) AddComment(ctx context.Context, in *pb.CommentItem) (*pb.StatusReply, error) {
|
||||
func (s *PostServer) AddComment(ctx context.Context, in *pb.CommentItem) (*pb.DataStatusReply, error) {
|
||||
return post.AddComment(ctx, in)
|
||||
}
|
||||
|
||||
// 删除评论
|
||||
func (s *PostServer) DeleteComment(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *PostServer) DeleteComment(ctx context.Context, in *pb.IdentRequest) (*pb.DataStatusReply, error) {
|
||||
return post.DeleteComment(ctx, in)
|
||||
}
|
||||
|
||||
// 计数操作
|
||||
func (s *PostServer) Action(ctx context.Context, in *pb.PostActionRequest) (*pb.StatusReply, error) {
|
||||
func (s *PostServer) Action(ctx context.Context, in *pb.PostActionRequest) (*pb.DataStatusReply, error) {
|
||||
return post.Action(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/feed/internal/logic/setting"
|
||||
pb "bsm/full/module/social/feed/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type SettingServer struct {
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/feed/internal/logic/tag"
|
||||
pb "bsm/full/module/social/feed/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type TagServer struct {
|
||||
@@ -21,11 +21,11 @@ func (s *TagServer) List(ctx context.Context, in *pb.FetchRequest) (*pb.TagListR
|
||||
}
|
||||
|
||||
// 新建标签
|
||||
func (s *TagServer) Create(ctx context.Context, in *pb.AddTagRequest) (*pb.StatusReply, error) {
|
||||
func (s *TagServer) Create(ctx context.Context, in *pb.AddTagRequest) (*pb.DataStatusReply, error) {
|
||||
return tag.Create(ctx, in)
|
||||
}
|
||||
|
||||
// 话题相关的文章
|
||||
func (s *TagServer) PostList(ctx context.Context, in *pb.FetchRequest) (*pb.PostListReply, error) {
|
||||
func (s *TagServer) PostList(ctx context.Context, in *pb.FetchRequest) (*pb.FeedPostListReply, error) {
|
||||
return tag.PostList(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/social/feed/internal/logic/timeline"
|
||||
pb "bsm/full/module/social/feed/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type TimelineServer struct {
|
||||
@@ -16,21 +16,21 @@ func NewTimelineServer() *TimelineServer {
|
||||
}
|
||||
|
||||
// 推荐
|
||||
func (s *TimelineServer) Recommend(ctx context.Context, in *pb.FetchRequest) (*pb.PostListReply, error) {
|
||||
func (s *TimelineServer) Recommend(ctx context.Context, in *pb.FetchRequest) (*pb.FeedPostListReply, error) {
|
||||
return timeline.Recommend(ctx, in)
|
||||
}
|
||||
|
||||
// 好友动态
|
||||
func (s *TimelineServer) Friend(ctx context.Context, in *pb.FetchRequest) (*pb.PostListReply, error) {
|
||||
func (s *TimelineServer) Friend(ctx context.Context, in *pb.FetchRequest) (*pb.FeedPostListReply, error) {
|
||||
return timeline.Friend(ctx, in)
|
||||
}
|
||||
|
||||
// 关注的动态
|
||||
func (s *TimelineServer) Follow(ctx context.Context, in *pb.FetchRequest) (*pb.PostListReply, error) {
|
||||
func (s *TimelineServer) Follow(ctx context.Context, in *pb.FetchRequest) (*pb.FeedPostListReply, error) {
|
||||
return timeline.Follow(ctx, in)
|
||||
}
|
||||
|
||||
// 热门
|
||||
func (s *TimelineServer) Hot(ctx context.Context, in *pb.FetchRequest) (*pb.PostListReply, error) {
|
||||
func (s *TimelineServer) Hot(ctx context.Context, in *pb.FetchRequest) (*pb.FeedPostListReply, error) {
|
||||
return timeline.Hot(ctx, in)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user