Skip to content

Commit

Permalink
feat: refactor server initialization process
Browse files Browse the repository at this point in the history
- 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 <[email protected]>
  • Loading branch information
blackhorseya committed Jul 29, 2024
1 parent d7eacdc commit 3c4a1e2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 6 additions & 1 deletion app/infra/transports/grpcx/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,17 @@ 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
addr string
}

// 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(
Expand All @@ -38,6 +41,8 @@ func NewServer(app *configx.Application) (*Server, error) {
)),
)

init(server)

return &Server{
grpcserver: server,
addr: app.GRPC.GetAddr(),
Expand Down
5 changes: 4 additions & 1 deletion app/infra/transports/grpcx/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c4a1e2

Please sign in to comment.