Skip to content

Commit

Permalink
split method into new file
Browse files Browse the repository at this point in the history
  • Loading branch information
evlekht committed Oct 1, 2024
1 parent fd18a9e commit 4195ba5
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 17 deletions.
22 changes: 22 additions & 0 deletions internal/rpc/server/ping_server_example.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package server

import (
"buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/ping/v1/pingv1grpc"

"google.golang.org/grpc"
)

var _ pingv1grpc.PingServiceServer = (*ping_srv1)(nil)

type ping_srv1 struct {

Check failure on line 11 in internal/rpc/server/ping_server_example.go

View workflow job for this annotation

GitHub Actions / Static Analysis

var-naming: don't use underscores in Go names; type ping_srv1 should be pingSrv1 (revive)
reqProcessor externalRequestProcessor
}

func NewPingServer(
grpcServer *grpc.Server,
reqProcess externalRequestProcessor,
) pingv1grpc.PingServiceServer {
ping_srv := &ping_srv1{reqProcessor: reqProcess}

Check failure on line 19 in internal/rpc/server/ping_server_example.go

View workflow job for this annotation

GitHub Actions / Static Analysis

var-naming: don't use underscores in Go names; var ping_srv should be pingSrv (revive)
pingv1grpc.RegisterPingServiceServer(grpcServer, ping_srv)
return ping_srv
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,11 @@ import (
"context"
"fmt"

"buf.build/gen/go/chain4travel/camino-messenger-protocol/grpc/go/cmp/services/ping/v1/pingv1grpc"
pingv1 "buf.build/gen/go/chain4travel/camino-messenger-protocol/protocolbuffers/go/cmp/services/ping/v1"

"github.com/chain4travel/camino-messenger-bot/internal/messaging/clients"
"google.golang.org/grpc"
)

var _ pingv1grpc.PingServiceServer = (*ping_srv1)(nil)

type ping_srv1 struct {
reqProcessor externalRequestProcessor
}

func NewPingServer(
grpcServer *grpc.Server,
reqProcess externalRequestProcessor,
) pingv1grpc.PingServiceServer {
ping_srv := &ping_srv1{reqProcessor: reqProcess}
pingv1grpc.RegisterPingServiceServer(grpcServer, ping_srv)
return ping_srv
}

func (s *ping_srv1) Ping(ctx context.Context, request *pingv1.PingRequest) (*pingv1.PingResponse, error) {
response, err := s.reqProcessor.processExternalRequest(ctx, clients.PingServiceV1Request, request)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/rpc/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ func (s *server) Stop() {
s.logger.Info("Stopping gRPC server...")
s.grpcServer.Stop()
}

func (s *server) processInternalRequest(ctx context.Context, requestType types.MessageType, request protoreflect.ProtoMessage) (protoreflect.ProtoMessage, error) {

Check failure on line 82 in internal/rpc/server/server.go

View workflow job for this annotation

GitHub Actions / Static Analysis

func `(*server).processInternalRequest` is unused (unused)
ctx, span := s.tracer.Start(ctx, "server.processInternalRequest", trace.WithSpanKind(trace.SpanKindServer))
defer span.End()
Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions internal/rpc/server/server_method.go.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Code generated by '{{GENERATOR}}'. DO NOT EDIT.
// template: {{TEMPLATE}}

package server

import (
"context"
"fmt"

{{TYPE_PACKAGE}} "{{PROTO_INC}}"

"github.com/chain4travel/camino-messenger-bot/internal/messaging/clients"
)

func (s *{{TYPE_PACKAGE}}_srv1) {{METHOD}}(ctx context.Context, request *{{TYPE_PACKAGE}}.{{REQUEST}}) (*{{TYPE_PACKAGE}}.{{RESPONSE}}, error) {
response, err := s.reqProcessor.processExternalRequest(ctx, clients.{{SERVICE}}V{{VERSION}}Request, request)
if err != nil {
return nil, fmt.Errorf("failed to process %s request: %w", clients.{{SERVICE}}V{{VERSION}}Request, err)
}
resp, ok := response.(*{{TYPE_PACKAGE}}.{{RESPONSE}})
if !ok {
return nil, fmt.Errorf("invalid response type: expected %s, got %T", clients.{{SERVICE}}V{{VERSION}}Response, response)
}
return resp, nil
}

0 comments on commit 4195ba5

Please sign in to comment.