refactor: localize protobuf definitions

This commit is contained in:
2026-08-09 20:14:57 +08:00
parent 9f6f318bbb
commit 5ea45a76fe
429 changed files with 54058 additions and 61623 deletions

View File

@@ -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
}

View File

@@ -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)
}

View File

@@ -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 {

View File

@@ -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)
}

View File

@@ -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)
}