refactor: localize protobuf definitions
This commit is contained in:
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/market/internal/logic/agency"
|
||||
pb "bsm/full/module/ec/market/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type AgencyServer struct {
|
||||
@@ -16,12 +16,12 @@ func NewAgencyServer() *AgencyServer {
|
||||
}
|
||||
|
||||
// 登录
|
||||
func (s *AgencyServer) Login(ctx context.Context, in *pb.LoginRequest) (*pb.LoginReply, error) {
|
||||
func (s *AgencyServer) Login(ctx context.Context, in *pb.LoginRequest) (*pb.MarketLoginReply, error) {
|
||||
return agency.Login(ctx, in)
|
||||
}
|
||||
|
||||
// 新增代理商
|
||||
func (s *AgencyServer) Create(ctx context.Context, in *pb.MarketAgenctyItem) (*pb.StatusReply, error) {
|
||||
func (s *AgencyServer) Create(ctx context.Context, in *pb.MarketAgenctyItem) (*pb.IdentityStatusReply, error) {
|
||||
return agency.Create(ctx, in)
|
||||
}
|
||||
|
||||
@@ -31,31 +31,31 @@ func (s *AgencyServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.Market
|
||||
}
|
||||
|
||||
// 代理商列表
|
||||
func (s *AgencyServer) Fetch(ctx context.Context, in *pb.FetchRequest) (*pb.AgencyReply, error) {
|
||||
func (s *AgencyServer) Fetch(ctx context.Context, in *pb.MarketFetchRequest) (*pb.AgencyReply, error) {
|
||||
return agency.Fetch(ctx, in)
|
||||
}
|
||||
|
||||
// 更新代理商数据
|
||||
func (s *AgencyServer) Modify(ctx context.Context, in *pb.MarketAgenctyItem) (*pb.StatusReply, error) {
|
||||
func (s *AgencyServer) Modify(ctx context.Context, in *pb.MarketAgenctyItem) (*pb.IdentityStatusReply, error) {
|
||||
return agency.Modify(ctx, in)
|
||||
}
|
||||
|
||||
// 删除一个代理商
|
||||
func (s *AgencyServer) Delete(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *AgencyServer) Delete(ctx context.Context, in *pb.IdentRequest) (*pb.IdentityStatusReply, error) {
|
||||
return agency.Delete(ctx, in)
|
||||
}
|
||||
|
||||
// 更新密码
|
||||
func (s *AgencyServer) SetPassword(ctx context.Context, in *pb.SetPasswordRequest) (*pb.StatusReply, error) {
|
||||
func (s *AgencyServer) SetPassword(ctx context.Context, in *pb.SetPasswordRequest) (*pb.IdentityStatusReply, error) {
|
||||
return agency.SetPassword(ctx, in)
|
||||
}
|
||||
|
||||
// 待审核
|
||||
func (s *AgencyServer) Pending(ctx context.Context, in *pb.FetchRequest) (*pb.AgencyReply, error) {
|
||||
func (s *AgencyServer) Pending(ctx context.Context, in *pb.MarketFetchRequest) (*pb.AgencyReply, error) {
|
||||
return agency.Pending(ctx, in)
|
||||
}
|
||||
|
||||
// 审核
|
||||
func (s *AgencyServer) Approve(ctx context.Context, in *pb.ApproveRequest) (*pb.StatusReply, error) {
|
||||
func (s *AgencyServer) Approve(ctx context.Context, in *pb.ApproveRequest) (*pb.IdentityStatusReply, error) {
|
||||
return agency.Approve(ctx, in)
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"bsm/full/module/ec/market/internal/logic/data"
|
||||
pb "bsm/full/module/ec/market/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type DataServer struct {
|
||||
@@ -21,7 +21,7 @@ func (s *DataServer) Overview(ctx context.Context, in *pb.Empty) (*pb.OverviewRe
|
||||
}
|
||||
|
||||
// 会员列表
|
||||
func (s *DataServer) MemberFetch(ctx context.Context, in *pb.FetchRequest) (*pb.DataReply, error) {
|
||||
func (s *DataServer) MemberFetch(ctx context.Context, in *pb.MarketFetchRequest) (*pb.DataReply, error) {
|
||||
return data.MemberFetch(ctx, in)
|
||||
}
|
||||
|
||||
@@ -31,7 +31,7 @@ func (s *DataServer) MemberDetails(ctx context.Context, in *pb.IdentRequest) (*p
|
||||
}
|
||||
|
||||
// 订单列表
|
||||
func (s *DataServer) OrderFetch(ctx context.Context, in *pb.FetchRequest) (*pb.DataReply, error) {
|
||||
func (s *DataServer) OrderFetch(ctx context.Context, in *pb.MarketFetchRequest) (*pb.DataReply, error) {
|
||||
return data.OrderFetch(ctx, in)
|
||||
}
|
||||
|
||||
|
||||
@@ -2,28 +2,27 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
pb "bsm/full/module/ec/market/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/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.RegisterAgencyServer(srv.Grpc, NewAgencyServer())
|
||||
@@ -32,44 +31,5 @@ func New(addr string) *Server {
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 将服务注册到Gateway
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
pb.RegisterAgencyHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterDataHandlerFromEndpoint(srv.Ctx, srv.Mux, addr, opts)
|
||||
pb.RegisterSupplyHandlerFromEndpoint(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/ec/market/internal/logic/supply"
|
||||
pb "bsm/full/module/ec/market/pb"
|
||||
"context"
|
||||
)
|
||||
|
||||
type SupplyServer struct {
|
||||
@@ -16,7 +16,7 @@ func NewSupplyServer() *SupplyServer {
|
||||
}
|
||||
|
||||
// 新增供应商
|
||||
func (s *SupplyServer) Create(ctx context.Context, in *pb.MarketSupplyItem) (*pb.StatusReply, error) {
|
||||
func (s *SupplyServer) Create(ctx context.Context, in *pb.MarketSupplyItem) (*pb.IdentityStatusReply, error) {
|
||||
return supply.Create(ctx, in)
|
||||
}
|
||||
|
||||
@@ -26,16 +26,16 @@ func (s *SupplyServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.Market
|
||||
}
|
||||
|
||||
// 供应商列表
|
||||
func (s *SupplyServer) Fetch(ctx context.Context, in *pb.FetchRequest) (*pb.SupplyReply, error) {
|
||||
func (s *SupplyServer) Fetch(ctx context.Context, in *pb.MarketFetchRequest) (*pb.SupplyReply, error) {
|
||||
return supply.Fetch(ctx, in)
|
||||
}
|
||||
|
||||
// 更新供应商数据
|
||||
func (s *SupplyServer) Modify(ctx context.Context, in *pb.MarketSupplyItem) (*pb.StatusReply, error) {
|
||||
func (s *SupplyServer) Modify(ctx context.Context, in *pb.MarketSupplyItem) (*pb.IdentityStatusReply, error) {
|
||||
return supply.Modify(ctx, in)
|
||||
}
|
||||
|
||||
// 删除一个供应商
|
||||
func (s *SupplyServer) Delete(ctx context.Context, in *pb.IdentRequest) (*pb.StatusReply, error) {
|
||||
func (s *SupplyServer) Delete(ctx context.Context, in *pb.IdentRequest) (*pb.IdentityStatusReply, error) {
|
||||
return supply.Delete(ctx, in)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user