Skip to content

Commit

Permalink
refactor: use go:generate and update project structure
Browse files Browse the repository at this point in the history
  • Loading branch information
flohansen committed Aug 4, 2024
1 parent f95ed44 commit 0b77e46
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 79 deletions.
6 changes: 2 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ run:
test:
go test ./... -race

generate:
sqlc generate
mockgen -package mocks -source internal/routes/routes.go -destination internal/routes/mocks/routes.go
mockgen -package mocks -source internal/notification/feature.go -destination internal/notification/mocks/feature.go
gen:
go generate ./...
protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative pkg/proto/feature.proto
19 changes: 11 additions & 8 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,38 @@ import (
"path"

"github.com/flohansen/dasher/internal/api"
"github.com/flohansen/dasher/internal/datastore"
"github.com/flohansen/dasher/internal/notification"
"github.com/flohansen/dasher/internal/repository"
"github.com/flohansen/dasher/internal/routes"
"github.com/flohansen/dasher/internal/server/feature"
"github.com/pkg/errors"
"google.golang.org/grpc"

_ "github.com/mattn/go-sqlite3"
)

var (
dataPath = flag.String("data", "/data", "The full path to the sqlite3 database file")
)

func run() error {
dataPath := flag.String("data", "/data", "The full path to the sqlite3 database file")
flag.Parse()

db, err := sql.Open("sqlite3", path.Join(*dataPath, "dasher.db"))
if err != nil {
return errors.Wrap(err, "sql open")
}

rpc := grpc.NewServer()
store := datastore.NewSQLite(db)
notifier := notification.NewFeatureNotifier(rpc, store)
migrator := datastore.NewSQLMigrator(db)
grpcServer := grpc.NewServer()
store := repository.NewSQLite(db)
notifier := feature.NewService(grpcServer, store)
migrator := repository.NewSQLMigrator(db)
routes := routes.New(store, notifier)

return api.New(
api.WithLogging(),
api.WithMigrator(migrator),
api.WithHttpHandler(":3000", routes),
api.WithNetListenerServer(":50051", rpc),
api.WithNetListenerServer(":50051", grpcServer),
).Start()
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datastore
package repository

import (
"database/sql"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package datastore
//go:generate sqlc -f ../../sqlc.yaml generate

package repository

import (
"context"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package datastore
package repository

import (
"context"
Expand Down
2 changes: 2 additions & 0 deletions internal/routes/routes.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:generate mockgen -source=routes.go -destination=mocks/routes_mock.go -package=mocks

package routes

import (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
package notification
//go:generate mockgen -source=service.go -destination=mocks/service_mock.go -package=mocks

package feature

import (
"context"
Expand All @@ -15,24 +17,23 @@ type FeatureStore interface {
Upsert(ctx context.Context, feature sqlc.Feature) error
}

type FeatureNotifier struct {
type Service struct {
proto.UnimplementedFeatureStateServiceServer
store FeatureStore
subscriptions map[string]map[proto.FeatureStateService_SubscribeFeatureChangesServer]struct{}
mu *sync.Mutex
mu sync.Mutex
}

func NewFeatureNotifier(grpcServer grpc.ServiceRegistrar, store FeatureStore) *FeatureNotifier {
notifier := FeatureNotifier{
func NewService(grpcServer grpc.ServiceRegistrar, store FeatureStore) *Service {
notifier := Service{
subscriptions: make(map[string]map[proto.FeatureStateService_SubscribeFeatureChangesServer]struct{}),
store: store,
mu: &sync.Mutex{},
}
proto.RegisterFeatureStateServiceServer(grpcServer, &notifier)
return &notifier
}

func (n *FeatureNotifier) registerSubscriptions(subscription *proto.FeatureSubscription, stream proto.FeatureStateService_SubscribeFeatureChangesServer) error {
func (n *Service) registerSubscriptions(subscription *proto.FeatureSubscription, stream proto.FeatureStateService_SubscribeFeatureChangesServer) error {
n.mu.Lock()
defer n.mu.Unlock()

Expand Down Expand Up @@ -68,7 +69,7 @@ func (n *FeatureNotifier) registerSubscriptions(subscription *proto.FeatureSubsc
return nil
}

func (n *FeatureNotifier) unregisterSubscriptions(subscription *proto.FeatureSubscription, stream proto.FeatureStateService_SubscribeFeatureChangesServer) {
func (n *Service) unregisterSubscriptions(subscription *proto.FeatureSubscription, stream proto.FeatureStateService_SubscribeFeatureChangesServer) {
n.mu.Lock()
defer n.mu.Unlock()

Expand All @@ -77,7 +78,7 @@ func (n *FeatureNotifier) unregisterSubscriptions(subscription *proto.FeatureSub
}
}

func (n *FeatureNotifier) createNonExistingFeatures(ctx context.Context, subscription *proto.FeatureSubscription, features []sqlc.Feature) ([]sqlc.Feature, error) {
func (n *Service) createNonExistingFeatures(ctx context.Context, subscription *proto.FeatureSubscription, features []sqlc.Feature) ([]sqlc.Feature, error) {
featureLookup := make(map[string]sqlc.Feature)
for _, f := range features {
featureLookup[f.FeatureID] = f
Expand All @@ -103,7 +104,7 @@ func (n *FeatureNotifier) createNonExistingFeatures(ctx context.Context, subscri
return featuresToAdd, nil
}

func (n *FeatureNotifier) SubscribeFeatureChanges(subscription *proto.FeatureSubscription, stream proto.FeatureStateService_SubscribeFeatureChangesServer) error {
func (n *Service) SubscribeFeatureChanges(subscription *proto.FeatureSubscription, stream proto.FeatureStateService_SubscribeFeatureChangesServer) error {
n.registerSubscriptions(subscription, stream)

// Wait for client closing the connection
Expand All @@ -113,7 +114,7 @@ func (n *FeatureNotifier) SubscribeFeatureChanges(subscription *proto.FeatureSub
return nil
}

func (n *FeatureNotifier) Notify(feature sqlc.Feature) error {
func (n *Service) Notify(feature sqlc.Feature) error {
n.mu.Lock()
defer n.mu.Unlock()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package notification
package feature

import (
"context"
"net"
"testing"
"time"

"github.com/flohansen/dasher/internal/notification/mocks"
"github.com/flohansen/dasher/internal/server/feature/mocks"
"github.com/flohansen/dasher/internal/sqlc"
"github.com/flohansen/dasher/pkg/proto"
"github.com/stretchr/testify/assert"
Expand All @@ -22,12 +22,12 @@ func bufDialer(lis *bufconn.Listener) func(context.Context, string) (net.Conn, e
}
}

func TestFeatureNotifier(t *testing.T) {
func TestService(t *testing.T) {
ctrl := gomock.NewController(t)
store := mocks.NewMockFeatureStore(ctrl)
lis := bufconn.Listen(2048)
s := grpc.NewServer()
notifier := NewFeatureNotifier(s, store)
notifier := NewService(s, store)

go func() {
s.Serve(lis)
Expand Down
12 changes: 6 additions & 6 deletions pkg/proto/feature.pb.go

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

82 changes: 39 additions & 43 deletions pkg/proto/feature_grpc.pb.go

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

0 comments on commit 0b77e46

Please sign in to comment.