Skip to content

Commit

Permalink
feat(fxgrpcserver): Provided module
Browse files Browse the repository at this point in the history
  • Loading branch information
ekkinox committed Feb 22, 2024
1 parent 7d24072 commit 2794b4b
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
17 changes: 11 additions & 6 deletions fxgrpcserver/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,20 @@ type FxGrpcServerParam struct {
}

func NewFxGrpcServer(p FxGrpcServerParam) (*grpc.Server, error) {
// interceptors
// server interceptors
unaryInterceptors, streamInterceptors := createInterceptors(p)

// server options
grpcServerOptions := []grpc.ServerOption{
grpc.ChainUnaryInterceptor(unaryInterceptors...),
grpc.ChainStreamInterceptor(streamInterceptors...),
}

grpcServerOptions = append(grpcServerOptions, p.Registry.ResolveGrpcServerOptions()...)

// server
grpcServer, err := p.Factory.Create(
grpcserver.WithServerOptions(
grpc.ChainUnaryInterceptor(unaryInterceptors...),
grpc.ChainStreamInterceptor(streamInterceptors...),
),
grpcserver.WithServerOptions(grpcServerOptions...),
grpcserver.WithReflection(p.Config.GetBool("modules.grpc.server.reflection.enabled")),
)
if err != nil {
Expand All @@ -96,7 +101,7 @@ func NewFxGrpcServer(p FxGrpcServerParam) (*grpc.Server, error) {
}

// registrations
resolvedServices, err := p.Registry.ResolveGrpcServices()
resolvedServices, err := p.Registry.ResolveGrpcServerServices()
if err != nil {
return nil, err
}

Check warning on line 107 in fxgrpcserver/module.go

View check run for this annotation

Codecov / codecov/patch

fxgrpcserver/module.go#L106-L107

Added lines #L106 - L107 were not covered by tests
Expand Down
2 changes: 1 addition & 1 deletion fxgrpcserver/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func TestModule(t *testing.T) {
fxgrpcserver.FxGrpcServerModule,
fx.Provide(service.NewTestServiceDependency),
fx.Options(
fxgrpcserver.AsGrpcService(service.NewTestServiceServer, &proto.Service_ServiceDesc),
fxgrpcserver.AsGrpcServerService(service.NewTestServiceServer, &proto.Service_ServiceDesc),
),
fx.Populate(&grpcServer, &lis, &logBuffer, &traceExporter, &metricsRegistry),
).RequireStart().RequireStop()
Expand Down
21 changes: 20 additions & 1 deletion fxgrpcserver/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"google.golang.org/grpc"
)

func AsGrpcService(constructor any, description *grpc.ServiceDesc) fx.Option {
func AsGrpcServerService(constructor any, description *grpc.ServiceDesc) fx.Option {
return fx.Options(
fx.Provide(
fx.Annotate(
Expand All @@ -23,3 +23,22 @@ func AsGrpcService(constructor any, description *grpc.ServiceDesc) fx.Option {
),
)
}

func AsGrpcServerOptions(options ...grpc.ServerOption) fx.Option {
var serverOptions []fx.Option

for _, option := range options {
serverOptions = append(
serverOptions,
fx.Supply(
fx.Annotate(
option,
fx.As(new(grpc.ServerOption)),
fx.ResultTags(`group:"grpc-server-options"`),
),
),
)
}

Check warning on line 41 in fxgrpcserver/register.go

View check run for this annotation

Codecov / codecov/patch

fxgrpcserver/register.go#L27-L41

Added lines #L27 - L41 were not covered by tests

return fx.Options(serverOptions...)

Check warning on line 43 in fxgrpcserver/register.go

View check run for this annotation

Codecov / codecov/patch

fxgrpcserver/register.go#L43

Added line #L43 was not covered by tests
}
2 changes: 1 addition & 1 deletion fxgrpcserver/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func TestAsGrpcService(t *testing.T) {
t.Parallel()

result := fxgrpcserver.AsGrpcService(service.NewTestServiceServer, &proto.Service_ServiceDesc)
result := fxgrpcserver.AsGrpcServerService(service.NewTestServiceServer, &proto.Service_ServiceDesc)

assert.Equal(t, "fx.optionGroup", fmt.Sprintf("%T", result))
}
10 changes: 9 additions & 1 deletion fxgrpcserver/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,37 @@ package fxgrpcserver

import (
"fmt"
"google.golang.org/grpc"

"go.uber.org/fx"
)

type GrpcServerRegistry struct {
options []grpc.ServerOption
services []any
definitions []GrpcServiceDefinition
}

type FxGrpcServiceRegistryParam struct {
fx.In
Options []grpc.ServerOption `group:"grpc-server-options"`
Services []any `group:"grpc-server-services"`
Definitions []GrpcServiceDefinition `group:"grpc-server-service-definitions"`
}

func NewFxGrpcServerRegistry(p FxGrpcServiceRegistryParam) *GrpcServerRegistry {
return &GrpcServerRegistry{
options: p.Options,
services: p.Services,
definitions: p.Definitions,
}
}

func (r *GrpcServerRegistry) ResolveGrpcServices() ([]*ResolvedGrpcService, error) {
func (r *GrpcServerRegistry) ResolveGrpcServerOptions() []grpc.ServerOption {
return r.options
}

func (r *GrpcServerRegistry) ResolveGrpcServerServices() ([]*ResolvedGrpcService, error) {
var grpcServices []*ResolvedGrpcService

for _, definition := range r.definitions {
Expand Down
4 changes: 2 additions & 2 deletions fxgrpcserver/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestResolveGrpcServicesSuccess(t *testing.T) {

registry := fxgrpcserver.NewFxGrpcServerRegistry(param)

resolvedServices, err := registry.ResolveGrpcServices()
resolvedServices, err := registry.ResolveGrpcServerServices()
assert.NoError(t, err)

assert.Len(t, resolvedServices, 1)
Expand All @@ -60,7 +60,7 @@ func TestResolveCheckerProbesRegistrationsFailure(t *testing.T) {

registry := fxgrpcserver.NewFxGrpcServerRegistry(param)

_, err := registry.ResolveGrpcServices()
_, err := registry.ResolveGrpcServerServices()
assert.Error(t, err)
assert.Equal(t, "cannot find grpc service implementation for type invalid", err.Error())
}

0 comments on commit 2794b4b

Please sign in to comment.