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

story(gRPC): event store constructor #59

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion cmd/serve_grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func withServeGrpcCmd() func(*viper.Viper) *cobra.Command {
}
zap.L().Info("listening for grpc requests", zap.String("addr", addr))

evrys := grpc.NewEvrysService(zap.L())
evrys := grpc.NewEvrysService(nil, zap.L())
err = evrys.Serve(cmd.Context(), ls)
if err != nil && !errors.Is(err, grpc.ErrServerStopped) {
zap.L().Fatal(
Expand Down
8 changes: 8 additions & 0 deletions grpc/grpc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package grpc

import "github.com/z5labs/evrys/eventstore"

// EventStore combines single method interfaces from eventstore into one interface
type EventStore interface {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think making a new file just for this is overkill

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file can be used for other interfaces. I like organizing interfaces in one place

eventstore.AppendOnly
}
16 changes: 12 additions & 4 deletions grpc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,27 @@ import (
"google.golang.org/grpc/status"
)

// ErrServerStopped wraps grpc.ErrServerStopped
var ErrServerStopped = grpc.ErrServerStopped

var _ evryspb.EvrysServer = &EvrysService{}

// EvrysService is defines the grpc for evrys and implements the interface from evrys proto
type EvrysService struct {
evryspb.UnimplementedEvrysServer

log *zap.Logger
eventStore EventStore
log *zap.Logger
}

func NewEvrysService(logger *zap.Logger) *EvrysService {
// NewEvrysService creates an instance of EvrysService
func NewEvrysService(eventStore EventStore, logger *zap.Logger) *EvrysService {
return &EvrysService{
log: logger,
eventStore: eventStore,
log: logger,
}
}

// Serve creates and runs the grpc server
func (s *EvrysService) Serve(ctx context.Context, ls net.Listener) error {
grpcServer := grpc.NewServer(grpc.Creds(insecure.NewCredentials()))
evryspb.RegisterEvrysServer(grpcServer, s)
Expand All @@ -70,10 +75,12 @@ func (s *EvrysService) Serve(ctx context.Context, ls net.Listener) error {
}
}

// GetEvent retrieves an event from the event store
func (s *EvrysService) GetEvent(ctx context.Context, req *evryspb.GetEventRequest) (*cloudeventpb.CloudEvent, error) {
return nil, status.Error(codes.NotFound, "")
}

// RecordEvent records an event in the event store
func (s *EvrysService) RecordEvent(ctx context.Context, req *cloudeventpb.CloudEvent) (*evryspb.RecordEventResponse, error) {
event, err := format.FromProto(req)
if err != nil {
Expand All @@ -88,6 +95,7 @@ func (s *EvrysService) RecordEvent(ctx context.Context, req *cloudeventpb.CloudE
return new(evryspb.RecordEventResponse), nil
}

// SliceEvents retrieves multiple events from the event store
func (s *EvrysService) SliceEvents(req *evryspb.SliceEventsRequest, stream evryspb.Evrys_SliceEventsServer) error {
s.log.Info("received slice request")
return nil
Expand Down
6 changes: 3 additions & 3 deletions grpc/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestEvrysService_GetEvent(t *testing.T) {
return
}

s := NewEvrysService(zap.L())
s := NewEvrysService(&mockEventStore{}, zap.L())

errChan := make(chan error, 1)
go func(ls net.Listener) {
Expand Down Expand Up @@ -96,7 +96,7 @@ func TestEvrysService_RecordEvent(t *testing.T) {
return
}

s := NewEvrysService(zap.L())
s := NewEvrysService(&mockEventStore{}, zap.L())

errChan := make(chan error, 1)
go func(ls net.Listener) {
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestEvrysService_SliceEvents(t *testing.T) {
return
}

s := NewEvrysService(zap.L())
s := NewEvrysService(&mockEventStore{}, zap.L())

errChan := make(chan error, 1)
go func(ls net.Listener) {
Expand Down
8 changes: 8 additions & 0 deletions grpc/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ import (
evryspb "github.com/z5labs/evrys/proto"

cloudeventpb "github.com/cloudevents/sdk-go/binding/format/protobuf/v2/pb"
"github.com/cloudevents/sdk-go/v2/event"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
)

type mockEventStore struct {
}

func (m *mockEventStore) Append(ctx context.Context, event *event.Event) error {
return nil
}

func MockEvrys(opts ...func(MockEvrysService) MockEvrysService) MockEvrysService {
s := MockEvrysService{}
for _, opt := range opts {
Expand Down