Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(rpc): avoid lint error when there are no server options #423

Merged
merged 2 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
(*codegen.File)(
// Copyright 2024 Outreach Corporation. All Rights Reserved.

// Description: This file contains the gRPC server passthrough implementation for the
// testing API defined in api/testing.proto. The concrete implementation
// exists in the server.go file in this same directory.
// Managed: true

package testing //nolint:revive // Why: We allow [-_].

import (
"context"
"fmt"
"net"

"github.com/getoutreach/gobox/pkg/events"
"github.com/getoutreach/gobox/pkg/log"
"github.com/getoutreach/gobox/pkg/trace"
"github.com/getoutreach/testing/api"
"github.com/getoutreach/services/pkg/grpcx"
"google.golang.org/grpc"
"google.golang.org/grpc/reflection"

// <<Stencil::Block(imports)>>

// <</Stencil::Block>>
)

// GRPCDependencies is used to inject dependencies into the GRPCService service
// activity. Great examples of integrations to be placed into here would be a database
// connection or perhaps a redis client that the service activity needs to use.
type GRPCDependencies struct{
// <<Stencil::Block(GRPCDependencies)>>

// <</Stencil::Block>>
}

// GRPCService is the concrete implementation of the serviceActivity interface
// which defines methods to start and stop a service. In this case the service
// being implemented is a gRPC server.
type GRPCService struct {
cfg *Config
deps *GRPCDependencies
}

// NewGRPCService creates a new GRPCService instance.
func NewGRPCService(cfg *Config, deps *GRPCDependencies) *GRPCService {
return &GRPCService{
cfg: cfg,
deps: deps,
}
}

// Servers holds all the server implementation instances.
type Servers struct {
DefaultServer api.Service
// Add your additional RPC servers here
// <<Stencil::Block(servers)>>

// <</Stencil::Block>>
}

// Run starts a gRPC server.
//
//nolint:funlen // Why: This function is long for extensibility reasons since it is generated by stencil.
func (gs *GRPCService) Run(ctx context.Context) error {
lc := &net.ListenConfig{}
listAddr := fmt.Sprintf("%s:%d", gs.cfg.ListenHost, gs.cfg.GRPCPort)
lis, err := lc.Listen(ctx, "tcp", listAddr)
if err != nil {
log.Error(ctx, "failed to listen", events.NewErrorInfo(err))
return err
}
defer lis.Close()

var servers = &Servers{}
var opts []grpcx.ServerOption
// Initialize your server instance here.
//
// <<Stencil::Block(server)>>
server, err := NewServer(ctx, gs.cfg)
if err != nil {
log.Error(ctx, "failed to create new server", events.NewErrorInfo(err))
return err
}
// <</Stencil::Block>>
servers.DefaultServer = server

srv, err := gs.StartServers(ctx, servers, opts...)
if err != nil {
log.Error(ctx, "failed to start server", events.NewErrorInfo(err))
return err
}
defer srv.Stop()

// Shutdown the server when the context is canceled
go func() {
<-ctx.Done()
srv.GracefulStop()
}()

// Note: .Serve() blocks
log.Info(ctx, "Serving GRPC Service on "+listAddr)
if err := srv.Serve(lis); err != nil {
log.Error(ctx, "unexpected grpc Serve error", events.NewErrorInfo(err))
return err
}

return nil
}

// Close closes the gRPC server.
func (gs *GRPCService) Close(ctx context.Context) error {
return nil
}

// StartServers starts a RPC server with the provided implementation.
func (gs *GRPCService) StartServers(ctx context.Context, servers *Servers, opts... grpcx.ServerOption) (*grpc.Server, error) {
// <<Stencil::Block(grpcServerOptions)>>

// <</Stencil::Block>>

s, err := grpcx.NewServer(ctx, opts...)
if err != nil {
return nil, err
}

// Register default server, title function won't work well when use underscore, so we make it dash first
api.RegisterTestingServer(s, rpcserver{servers.DefaultServer})

// Register your additional RPC servers here
// <<Stencil::Block(registrations)>>

// <</Stencil::Block>>

// Register reflection
reflection.Register(s)

return s, nil
}

// rpcserver is a shim that converts the generic Service interface
// into the grpc generated interface from the protobuf
type rpcserver struct {
api.Service
}

// Place any GRPC handler functions for your service here
//
// <<Stencil::Block(handlers)>>

// Ping is a simple ping/pong handler.
func (s rpcserver) Ping(ctx context.Context, req *api.PingRequest) (*api.PingResponse, error) {
message, err := s.Service.Ping(ctx, req.Message)
if err != nil {
return nil, err
}
return &api.PingResponse{Message: message}, nil
}

// Pong is a simple RPC that returns a message.
func (s rpcserver) Pong(ctx context.Context, req *api.PongRequest) (*api.PongResponse, error) {
message, err := s.Service.Pong(ctx, req.Message)
if err != nil {
return nil, err
}
return &api.PongResponse{Message: message}, nil
}
// <</Stencil::Block>>
)
6 changes: 4 additions & 2 deletions templates/internal/appName/rpc/rpc.go.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func (gs *GRPCService) StartServers(ctx context.Context, servers *Servers, opts.
// end gRPC server options injected by modules
}, opts...)
{{- end }}
{{- if or $grpcServerOptionInit $grpcServerOptions }}

{{- end }}
// <<Stencil::Block(grpcServerOptions)>>
{{ file.Block "grpcServerOptions" }}
// <</Stencil::Block>>
Expand All @@ -192,8 +194,8 @@ func (gs *GRPCService) StartServers(ctx context.Context, servers *Servers, opts.
// Register default server, title function won't work well when use underscore, so we make it dash first
api.Register{{ stencil.ApplyTemplate "goTitleCaseName" }}Server(s, rpcserver{servers.DefaultServer})

// Register your additional RPC servers here
// <<Stencil::Block(registrations)>>
// Register your additional RPC servers here
// <<Stencil::Block(registrations)>>
{{ file.Block "registrations" }}
// <</Stencil::Block>>

Expand Down
11 changes: 11 additions & 0 deletions templates/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,17 @@ func TestVSCodeLaunchConfig(t *testing.T) {
st.Run(true)
}

func TestGRPCServerRPC(t *testing.T) {
st := stenciltest.New(t, "internal/appName/rpc/rpc.go.tpl", libraryTmpls...)
st.Args(map[string]interface{}{
"service": true,
"serviceActivities": []interface{}{
"grpc",
},
})
st.Run(true)
}

func TestIncludeRubyToolVersionsIfRubyGRPCCLient(t *testing.T) {
st := stenciltest.New(t, "testdata/tool-versions-ruby/.tool-versions.tpl", libraryTmpls...)
st.Args(map[string]interface{}{
Expand Down