diff --git a/adapter/block/grpc/impl.go b/adapter/block/grpc/impl.go index f918e7d..43a88e1 100644 --- a/adapter/block/grpc/impl.go +++ b/adapter/block/grpc/impl.go @@ -1,24 +1,59 @@ package grpc import ( + "fmt" + "github.com/blackhorseya/ryze/adapter/block/wirex" + "github.com/blackhorseya/ryze/app/infra/transports/grpcx" + "github.com/blackhorseya/ryze/entity/domain/block/model" "github.com/blackhorseya/ryze/pkg/adapterx" + "github.com/blackhorseya/ryze/pkg/contextx" + "go.uber.org/zap" + "google.golang.org/grpc" ) -type grpc struct { +type impl struct { + injector *wirex.Injector + server *grpcx.Server +} + +// NewGRPC creates a new impl service. +func NewGRPC(injector *wirex.Injector, server *grpcx.Server) adapterx.Service { + return &impl{ + injector: injector, + server: server, + } } -// NewGRPC creates a new grpc service. -func NewGRPC(injector *wirex.Injector) adapterx.Service { - return &grpc{} +func (i *impl) Start() error { + ctx := contextx.Background() + + err := i.server.Start(ctx) + if err != nil { + ctx.Error("Failed to start grpc server", zap.Error(err)) + return err + } + + ctx.Info("start grpc server") + + return nil } -func (i *grpc) Start() error { - // TODO: 2024/7/29|sean|add grpc logic here - panic("implement me") +func (i *impl) AwaitSignal() error { + ctx := contextx.Background() + ctx.Info("receive signal to stop server") + + if err := i.server.Stop(ctx); err != nil { + ctx.Error("Failed to stop server", zap.Error(err)) + return fmt.Errorf("failed to stop server: %w", err) + } + + return nil } -func (i *grpc) AwaitSignal() error { - // TODO: 2024/7/29|sean|add grpc logic here - panic("implement me") +// NewInitServersFn creates a new impl server init function. +func NewInitServersFn(injector *wirex.Injector) grpcx.InitServers { + return func(s *grpc.Server) { + model.RegisterBlockServiceServer(s, injector.BlockService) + } } diff --git a/adapter/block/grpc/main_test.go b/adapter/block/grpc/main_test.go new file mode 100644 index 0000000..2f78ede --- /dev/null +++ b/adapter/block/grpc/main_test.go @@ -0,0 +1,34 @@ +//go:build external + +package grpc + +import ( + "os" + "os/signal" + "syscall" + "testing" + + "github.com/spf13/viper" +) + +func TestRun(t *testing.T) { + service, err := New(viper.New()) + if err != nil { + t.Fatalf("New() error = %v", err) + } + + err = service.Start() + if err != nil { + t.Fatalf("Start() error = %v", err) + } + + signalChan := make(chan os.Signal, 1) + signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT) + + <-signalChan + + err = service.AwaitSignal() + if err != nil { + t.Fatalf("AwaitSignal() error = %v", err) + } +} diff --git a/adapter/block/grpc/wire.go b/adapter/block/grpc/wire.go index 90d8ae9..3c3a23c 100644 --- a/adapter/block/grpc/wire.go +++ b/adapter/block/grpc/wire.go @@ -13,6 +13,7 @@ import ( "github.com/blackhorseya/ryze/app/infra/configx" "github.com/blackhorseya/ryze/app/infra/otelx" "github.com/blackhorseya/ryze/app/infra/tonx" + "github.com/blackhorseya/ryze/app/infra/transports/grpcx" "github.com/blackhorseya/ryze/pkg/adapterx" "github.com/blackhorseya/ryze/pkg/contextx" "github.com/google/wire" @@ -47,5 +48,8 @@ func New(v *viper.Viper) (adapterx.Service, error) { biz.NewBlockService, initTonx, + + grpcx.NewServer, + NewInitServersFn, )) } diff --git a/adapter/block/grpc/wire_gen.go b/adapter/block/grpc/wire_gen.go index 4d6da3d..b796600 100644 --- a/adapter/block/grpc/wire_gen.go +++ b/adapter/block/grpc/wire_gen.go @@ -14,6 +14,7 @@ import ( "github.com/blackhorseya/ryze/app/infra/configx" "github.com/blackhorseya/ryze/app/infra/otelx" "github.com/blackhorseya/ryze/app/infra/tonx" + "github.com/blackhorseya/ryze/app/infra/transports/grpcx" "github.com/blackhorseya/ryze/pkg/adapterx" "github.com/blackhorseya/ryze/pkg/contextx" "github.com/spf13/viper" @@ -40,7 +41,12 @@ func New(v *viper.Viper) (adapterx.Service, error) { A: application, BlockService: blockServiceServer, } - service := NewGRPC(injector) + initServers := NewInitServersFn(injector) + server, err := grpcx.NewServer(application, initServers) + if err != nil { + return nil, err + } + service := NewGRPC(injector, server) return service, nil }