Skip to content

Commit

Permalink
server: enable server to initialize orchestrator API router
Browse files Browse the repository at this point in the history
  • Loading branch information
joelrebel committed Jul 3, 2024
1 parent 8e6b2eb commit 64b3344
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
30 changes: 26 additions & 4 deletions internal/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ var (

// Server type holds attributes of the condition orc server
type Server struct {
// Logger is the app logger
orchestrator bool // run in orchestrator API mode
authMWConfigs []ginjwt.AuthConfig
logger *logrus.Logger
streamBroker events.Stream
streamSubjectPrefix string
listenAddress string
facilityCode string
conditionDefinitions rctypes.Definitions
repository store.Repository
fleetDBClient fleetdb.FleetDB
Expand Down Expand Up @@ -73,9 +75,10 @@ func WithListenAddress(addr string) Option {
}

// WithStreamBroker sets the event stream broker.
func WithStreamBroker(broker events.Stream) Option {
func WithStreamBroker(broker events.Stream, streamSubjectPrefix string) Option {
return func(s *Server) {
s.streamBroker = broker
s.streamSubjectPrefix = streamSubjectPrefix
}
}

Expand All @@ -93,6 +96,14 @@ func WithAuthMiddlewareConfig(authMWConfigs []ginjwt.AuthConfig) Option {
}
}

// WithAsOrchestrator registers just the Orchestrator routes, excluding any Condition API routes.
func WithAsOrchestrator(facilityCode string) Option {
return func(s *Server) {
s.orchestrator = true
s.facilityCode = facilityCode
}
}

func New(opts ...Option) *http.Server {
s := &Server{}

Expand All @@ -109,10 +120,15 @@ func New(opts ...Option) *http.Server {
routes.WithLogger(s.logger),
routes.WithStore(s.repository),
routes.WithFleetDBClient(s.fleetDBClient),
routes.WithStreamBroker(s.streamBroker),
routes.WithStreamBroker(s.streamBroker, s.streamSubjectPrefix),
routes.WithConditionDefinitions(s.conditionDefinitions),
}

// orchestrator mode requires a facility code
if s.orchestrator {
options = append(options, routes.WithFacilityCode(s.facilityCode))
}

// add auth middleware
if s.authMWConfigs != nil {
authMW, err := ginjwt.NewMultiTokenMiddlewareFromConfigs(s.authMWConfigs...)
Expand All @@ -128,7 +144,13 @@ func New(opts ...Option) *http.Server {
s.logger.Fatal(errors.Wrap(err, ErrRoutes.Error()))
}

v1Router.Routes(g.Group(routes.PathPrefix))
if s.orchestrator {
v1Router.RoutesOrchestrator(g.Group(routes.PathPrefix))
s.logger.Info("Orchestrator API server routes registered.")
} else {
v1Router.Routes(g.Group(routes.PathPrefix))
s.logger.Info("Condition API server routes registered.")
}

g.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{"message": "invalid request - route not found"})
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func newTester(t *testing.T) *integrationTester {
server.WithListenAddress("localhost:9999"),
server.WithStore(repository),
server.WithFleetDBClient(fleetDBClient),
server.WithStreamBroker(stream),
server.WithStreamBroker(stream, "foo"),
server.WithConditionDefinitions(
[]*rctypes.Definition{
{Kind: rctypes.FirmwareInstall},
Expand Down
2 changes: 1 addition & 1 deletion pkg/api/v1/routes/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func mockserver(t *testing.T, logger *logrus.Logger, fleetDBClient fleetdb.Fleet
}

if stream != nil {
options = append(options, WithStreamBroker(stream))
options = append(options, WithStreamBroker(stream, "foo"))
}

v1Router, err := NewRoutes(options...)
Expand Down

0 comments on commit 64b3344

Please sign in to comment.