feat: import services and standardize Go 1.26.5
This commit is contained in:
41
apps/ec/address/internal/server/library_server.go
Normal file
41
apps/ec/address/internal/server/library_server.go
Normal file
@@ -0,0 +1,41 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.apinb.com/bsm-ec/address/internal/logic/library"
|
||||
pb "git.apinb.com/bsm-ec/address/pb"
|
||||
)
|
||||
|
||||
type LibraryServer struct {
|
||||
pb.UnimplementedLibraryServer
|
||||
}
|
||||
|
||||
func NewLibraryServer() *LibraryServer {
|
||||
return &LibraryServer{}
|
||||
}
|
||||
|
||||
// 新增地址
|
||||
func (s *LibraryServer) Create(ctx context.Context, in *pb.AddressCreateRequest) (*pb.StatusReply, error) {
|
||||
return library.Create(ctx, in)
|
||||
}
|
||||
|
||||
// 修改地址
|
||||
func (s *LibraryServer) Modify(ctx context.Context, in *pb.AddressItem) (*pb.StatusReply, error) {
|
||||
return library.Modify(ctx, in)
|
||||
}
|
||||
|
||||
// 获取一条地址
|
||||
func (s *LibraryServer) Get(ctx context.Context, in *pb.IdentRequest) (*pb.AddressItem, error) {
|
||||
return library.Get(ctx, in)
|
||||
}
|
||||
|
||||
// 获取地址列表
|
||||
func (s *LibraryServer) Fetch(ctx context.Context, in *pb.IdentRequest) (*pb.AddressListReply, error) {
|
||||
return library.Fetch(ctx, in)
|
||||
}
|
||||
|
||||
// 删除一个地址
|
||||
func (s *LibraryServer) Delete(ctx context.Context, in *pb.AddressDeleteRequest) (*pb.StatusReply, error) {
|
||||
return library.Delete(ctx, in)
|
||||
}
|
||||
71
apps/ec/address/internal/server/new.go
Normal file
71
apps/ec/address/internal/server/new.go
Normal file
@@ -0,0 +1,71 @@
|
||||
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
pb "git.apinb.com/bsm-ec/address/pb"
|
||||
"git.apinb.com/bsm-sdk/core/vars"
|
||||
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
|
||||
}
|
||||
|
||||
func New(addr string) *Server {
|
||||
srv := &Server{Ctx: context.Background(), Grpc: grpc.NewServer(), Mux: gwRuntime.NewServeMux(
|
||||
gwRuntime.WithForwardResponseRewriter(responseEnvelope),
|
||||
)}
|
||||
|
||||
// register service to grpc.Server
|
||||
pb.RegisterLibraryServer(srv.Grpc, NewLibraryServer())
|
||||
|
||||
reflection.Register(srv.Grpc)
|
||||
|
||||
// 将服务注册到Gateway
|
||||
opts := []grpc.DialOption{grpc.WithInsecure()}
|
||||
pb.RegisterLibraryHandlerFromEndpoint(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