From 3c4a1e224fc5db9d919e101e12f72ef934dff652 Mon Sep 17 00:00:00 2001 From: Sean Zheng Date: Mon, 29 Jul 2024 10:14:38 +0800 Subject: [PATCH] feat: refactor server initialization process - Add an `InitServers` function to `server.go` - Modify `NewServer` function to accept `InitServers` parameter - Call the `init` function in `NewServer` function in `server.go` - Update `server_test.go` to test the `NewServer` function with the `init` function Signed-off-by: Sean Zheng --- app/infra/transports/grpcx/server.go | 7 ++++++- app/infra/transports/grpcx/server_test.go | 5 ++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/infra/transports/grpcx/server.go b/app/infra/transports/grpcx/server.go index 8afb0df..3969efc 100644 --- a/app/infra/transports/grpcx/server.go +++ b/app/infra/transports/grpcx/server.go @@ -14,6 +14,9 @@ import ( "google.golang.org/grpc" ) +// InitServers define register handler +type InitServers func(s *grpc.Server) + // Server represents the grpc server. type Server struct { grpcserver *grpc.Server @@ -21,7 +24,7 @@ type Server struct { } // NewServer creates a new grpc server. -func NewServer(app *configx.Application) (*Server, error) { +func NewServer(app *configx.Application, init InitServers) (*Server, error) { logger := contextx.Background().Logger server := grpc.NewServer( grpc.StreamInterceptor(grpcmiddleware.ChainStreamServer( @@ -38,6 +41,8 @@ func NewServer(app *configx.Application) (*Server, error) { )), ) + init(server) + return &Server{ grpcserver: server, addr: app.GRPC.GetAddr(), diff --git a/app/infra/transports/grpcx/server_test.go b/app/infra/transports/grpcx/server_test.go index 305af6e..3ca7a7e 100644 --- a/app/infra/transports/grpcx/server_test.go +++ b/app/infra/transports/grpcx/server_test.go @@ -6,10 +6,13 @@ import ( "github.com/blackhorseya/ryze/app/infra/configx" "github.com/blackhorseya/ryze/pkg/contextx" + "google.golang.org/grpc" ) func TestNewServer(t *testing.T) { - server, err := NewServer(&configx.Application{}) + server, err := NewServer(&configx.Application{}, func(s *grpc.Server) { + t.Log("init grpc server") + }) if err != nil { t.Errorf("Error: %v", err) return