package server import ( grpc_middleware "github.com/grpc-ecosystem/go-grpc-middleware" "google.golang.org/grpc" "google.golang.org/grpc/reflection" "grpc-base/common/logger" "grpc-base/service/testuser/api" "grpc-base/service/testuser/internal/conf" "grpc-base/service/testuser/internal/service" "net" ) var server *grpc.Server func NewGrpcServer(conf *conf.Config, svc *service.TestUser) error { if conf.Server.GrpcAddr == "" { conf.Server.GrpcAddr = ":50000" } listen, err := net.Listen("tcp", conf.Server.GrpcAddr) if err != nil { return err } server = grpc.NewServer( grpc_middleware.WithUnaryServerChain( recoverHandler, logBeforeReq, ), ) api.RegisterTestUserServer(server, svc) logger.Infof("listen grpc srv on %s\n", conf.Server.GrpcAddr) reflection.Register(server) err = server.Serve(listen) if err != nil { return err } return nil } func GracefulStopGrpc() { server.GracefulStop() logger.Warn("grpc server stop") }