refactor: localize protobuf definitions
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/cms/internal/logic/category"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type CategoryServer struct {
|
||||
@@ -21,7 +21,7 @@ func (s *CategoryServer) Fetch(ctx context.Context, in *pb.IdentRequest) (*pb.Ca
|
||||
}
|
||||
|
||||
// 添加分类
|
||||
func (s *CategoryServer) Create(ctx context.Context, in *pb.CategoryItem) (*pb.StatusReply, error) {
|
||||
func (s *CategoryServer) Create(ctx context.Context, in *pb.CmsCategoryItem) (*pb.StatusReply, error) {
|
||||
return category.Create(ctx, in)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,18 +2,12 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
"context"
|
||||
|
||||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/reflection"
|
||||
"google.golang.org/protobuf/proto"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
@@ -27,7 +21,6 @@ func New(addr string) *Server {
|
||||
srv := &Server{
|
||||
Ctx: context.Background(),
|
||||
Grpc: grpc.NewServer(),
|
||||
Mux: gwRuntime.NewServeMux(gwRuntime.WithForwardResponseRewriter(responseEnvelope)),
|
||||
grpcConns: make(map[string]*grpc.ClientConn),
|
||||
}
|
||||
|
||||
@@ -40,72 +33,5 @@ func New(addr string) *Server {
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 连接池: 只创建一次连接并复用
|
||||
conn, ok := srv.grpcConns[addr]
|
||||
if !ok {
|
||||
var err error
|
||||
conn, err = grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
panic("failed to dial grpc server: " + err.Error())
|
||||
}
|
||||
srv.grpcConns[addr] = conn
|
||||
}
|
||||
|
||||
// 将服务注册到Gateway
|
||||
|
||||
if err := pb.RegisterCategoryHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Category handler: " + err.Error())
|
||||
}
|
||||
|
||||
if err := pb.RegisterPagesHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Pages handler: " + err.Error())
|
||||
}
|
||||
|
||||
if err := pb.RegisterPostHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Post handler: " + err.Error())
|
||||
}
|
||||
|
||||
if err := pb.RegisterSiteHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Site handler: " + err.Error())
|
||||
}
|
||||
|
||||
if err := pb.RegisterTagsHandler(srv.Ctx, srv.Mux, conn); err != nil {
|
||||
panic("Failed to register Tags handler: " + err.Error())
|
||||
}
|
||||
|
||||
// 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": vars.OK,
|
||||
"details": response,
|
||||
"timeseq": time.Now().Unix(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/cms/internal/logic/pages"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type PagesServer struct {
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/cms/internal/logic/post"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type PostServer struct {
|
||||
@@ -31,7 +31,7 @@ func (s *PostServer) GetByKey(ctx context.Context, in *pb.GetPostByKeyRequest) (
|
||||
}
|
||||
|
||||
// 搜索文章
|
||||
func (s *PostServer) Search(ctx context.Context, in *pb.SearchRequest) (*pb.PostListReply, error) {
|
||||
func (s *PostServer) Search(ctx context.Context, in *pb.CmsSearchRequest) (*pb.PostListReply, error) {
|
||||
return post.Search(ctx, in)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/cms/internal/logic/site"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type SiteServer struct {
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/base/cms/internal/logic/tags"
|
||||
pb "bsm/full/module/base/cms/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type TagsServer struct {
|
||||
@@ -21,12 +21,12 @@ func (s *TagsServer) Fetch(ctx context.Context, in *pb.IdentRequest) (*pb.TagsLi
|
||||
}
|
||||
|
||||
// 创建标签
|
||||
func (s *TagsServer) Create(ctx context.Context, in *pb.TagsItem) (*pb.StatusReply, error) {
|
||||
func (s *TagsServer) Create(ctx context.Context, in *pb.CmsTagsItem) (*pb.StatusReply, error) {
|
||||
return tags.Create(ctx, in)
|
||||
}
|
||||
|
||||
// 修改标签
|
||||
func (s *TagsServer) Modify(ctx context.Context, in *pb.TagsItem) (*pb.StatusReply, error) {
|
||||
func (s *TagsServer) Modify(ctx context.Context, in *pb.CmsTagsItem) (*pb.StatusReply, error) {
|
||||
return tags.Modify(ctx, in)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user