Skip to content

Commit

Permalink
refactor: remove unnecessary api package
Browse files Browse the repository at this point in the history
  • Loading branch information
flohansen committed Aug 4, 2024
1 parent aece567 commit ffaaf6d
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 165 deletions.
46 changes: 33 additions & 13 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"database/sql"
"flag"
"log"
"net"
"net/http"
"path"

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

Expand All @@ -28,20 +30,38 @@ func run() error {
return errors.Wrap(err, "sql open")
}

grpcServer := grpc.NewServer()
store := repository.NewFeatureSQLite(db)
notifier := feature.NewService(grpcServer, store)
migrator := repository.NewSQLiteMigrator(db)
routes := routes.New(store, notifier)

return api.New(
api.WithLogging(),
api.WithMigrator(migrator),
api.WithHttpHandler(":3000", routes),
api.WithNetListenerServer(":50051", grpcServer),
).Start()
if err := migrator.Migrate(); err != nil {
return errors.Wrap(err, "migrate")
}

store := repository.NewFeatureSQLite(db)
featureService := feature.NewService(store)
errs := make(chan error, 1)

go startGrpcServer(featureService, errs)
go startHttpServer(store, featureService, errs)
return <-errs
}

func startGrpcServer(featureService *feature.Service, errs chan error) {
grpcServer := grpc.NewServer()
proto.RegisterFeatureStateServiceServer(grpcServer, featureService)

lis, err := net.Listen("tcp", ":50051")
if err != nil {
errs <- err
return
}

errs <- grpcServer.Serve(lis)
}

func startHttpServer(store *repository.FeatureSQLite, featureService *feature.Service, errs chan error) {
routes := routes.New(store, featureService)
errs <- http.ListenAndServe(":3000", routes)
}

func main() {
log.Fatal(run())
log.Fatalf("fatal error: %s", run())
}
51 changes: 0 additions & 51 deletions internal/api/api.go

This file was deleted.

30 changes: 0 additions & 30 deletions internal/api/http.go

This file was deleted.

7 changes: 0 additions & 7 deletions internal/api/log.go

This file was deleted.

7 changes: 0 additions & 7 deletions internal/api/migration.go

This file was deleted.

35 changes: 0 additions & 35 deletions internal/api/net.go

This file was deleted.

17 changes: 0 additions & 17 deletions internal/api/option.go

This file was deleted.

5 changes: 2 additions & 3 deletions internal/server/feature/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/flohansen/dasher/internal/sqlc"
"github.com/flohansen/dasher/pkg/proto"
"github.com/pkg/errors"
"google.golang.org/grpc"
)

type FeatureStore interface {
Expand All @@ -25,12 +24,12 @@ type Service struct {
mu sync.Mutex
}

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

return &notifier
}

Expand Down
6 changes: 4 additions & 2 deletions internal/server/feature/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ func TestService(t *testing.T) {
ctrl := gomock.NewController(t)
store := mocks.NewMockFeatureStore(ctrl)
lis := bufconn.Listen(2048)

s := grpc.NewServer()
notifier := NewService(s, store)
svc := NewService(store)
proto.RegisterFeatureStateServiceServer(s, svc)

go func() {
s.Serve(lis)
Expand Down Expand Up @@ -75,7 +77,7 @@ func TestService(t *testing.T) {

// when
time.Sleep(10 * time.Millisecond) // workaround to wait until both clients subscribed
notifier.Notify(sqlc.Feature{FeatureID: "SOME_FEATURE_ID"})
svc.Notify(sqlc.Feature{FeatureID: "SOME_FEATURE_ID"})

// then
feature1, err := stream1.Recv()
Expand Down

0 comments on commit ffaaf6d

Please sign in to comment.