Skip to content

Commit

Permalink
feat: refactor gRPC implementation in block package (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackhorseya authored Jul 29, 2024
2 parents de61044 + 807fe82 commit 72d78c6
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 11 deletions.
55 changes: 45 additions & 10 deletions adapter/block/grpc/impl.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
34 changes: 34 additions & 0 deletions adapter/block/grpc/main_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 4 additions & 0 deletions adapter/block/grpc/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -47,5 +48,8 @@ func New(v *viper.Viper) (adapterx.Service, error) {

biz.NewBlockService,
initTonx,

grpcx.NewServer,
NewInitServersFn,
))
}
8 changes: 7 additions & 1 deletion adapter/block/grpc/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 72d78c6

Please sign in to comment.