57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
// Code generated by protoc-gen-slc. DO NOT EDIT.
|
||
package server
|
||
|
||
import (
|
||
pb "bsm/full/module/ec/market/pb"
|
||
"context"
|
||
|
||
"git.apinb.com/bsm-sdk/core/printer"
|
||
gwRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
|
||
"google.golang.org/grpc"
|
||
"google.golang.org/grpc/codes"
|
||
"google.golang.org/grpc/reflection"
|
||
"google.golang.org/grpc/status"
|
||
)
|
||
|
||
// recoverUnaryInterceptor 捕获处理过程中的 panic,转为 Internal 错误返回,避免进程崩溃。
|
||
func recoverUnaryInterceptor(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
|
||
defer func() {
|
||
if r := recover(); r != nil {
|
||
printer.Error("grpc panic recovered: %v", r)
|
||
err = status.Error(codes.Internal, "internal server error")
|
||
}
|
||
}()
|
||
return handler(ctx, req)
|
||
}
|
||
|
||
type Server struct {
|
||
Grpc *grpc.Server
|
||
Ctx context.Context
|
||
Mux *gwRuntime.ServeMux
|
||
grpcConns map[string]*grpc.ClientConn // 连接池
|
||
}
|
||
|
||
func New(grpcServ *grpc.Server) *Server {
|
||
standalone := grpcServ == nil
|
||
if standalone {
|
||
grpcServ = grpc.NewServer(grpc.UnaryInterceptor(recoverUnaryInterceptor))
|
||
}
|
||
|
||
srv := &Server{
|
||
Ctx: context.Background(),
|
||
Grpc: grpcServ,
|
||
grpcConns: make(map[string]*grpc.ClientConn),
|
||
}
|
||
|
||
// register service to grpc.Server
|
||
pb.RegisterAgencyServer(srv.Grpc, NewAgencyServer())
|
||
pb.RegisterDataServer(srv.Grpc, NewDataServer())
|
||
pb.RegisterSupplyServer(srv.Grpc, NewSupplyServer())
|
||
|
||
if standalone {
|
||
reflection.Register(srv.Grpc)
|
||
}
|
||
|
||
return srv
|
||
}
|