Skip to content

Commit

Permalink
fix(router): namespace driver default (#1141)
Browse files Browse the repository at this point in the history
  • Loading branch information
hekike authored Jul 3, 2024
1 parent 6e6ec2d commit 92478a7
Showing 1 changed file with 56 additions and 4 deletions.
60 changes: 56 additions & 4 deletions internal/server/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package router

import (
"encoding/json"
"errors"
"fmt"
"io"
"net/http"

Expand Down Expand Up @@ -62,6 +64,50 @@ type Config struct {
EntitlementsEnabled bool
}

func (c Config) Validate() error {
if c.NamespaceManager == nil {
return errors.New("namespace manager is required")
}

if c.ErrorHandler == nil {
return errors.New("error handler is required")
}

if c.IngestHandler == nil {
return errors.New("ingest handler is required")
}

// Validate repositories
if c.Meters == nil {
return errors.New("meters repository is required")
}

// Validate connectors
if c.StreamingConnector == nil {
return errors.New("streaming connector is required")
}

if c.EntitlementsEnabled {
if c.FeatureConnector == nil {
return errors.New("feature connector is required")
}

if c.EntitlementConnector == nil {
return errors.New("entitlement connector is required")
}

if c.EntitlementBalanceConnector == nil {
return errors.New("entitlement balance connector is required")
}

if c.GrantConnector == nil {
return errors.New("grant connector is required")
}
}

return nil
}

type Router struct {
config Config

Expand All @@ -75,32 +121,38 @@ type Router struct {
var _ api.ServerInterface = (*Router)(nil)

func NewRouter(config Config) (*Router, error) {
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("invalid router config: %w", err)
}

router := &Router{
config: config,
}

staticNamespaceDecoder := namespacedriver.StaticNamespaceDecoder(config.NamespaceManager.GetDefaultNamespace())

if config.EntitlementsEnabled {
router.featureHandler = productcatalog_httpdriver.NewFeatureHandler(
config.FeatureConnector,
namespacedriver.StaticNamespaceDecoder("default"),
staticNamespaceDecoder,
httptransport.WithErrorHandler(config.ErrorHandler),
)

router.entitlementHandler = entitlement_httpdriver.NewEntitlementHandler(
config.EntitlementConnector,
namespacedriver.StaticNamespaceDecoder("default"),
staticNamespaceDecoder,
httptransport.WithErrorHandler(config.ErrorHandler),
)

router.meteredEntitlementHandler = entitlement_httpdriver.NewMeteredEntitlementHandler(
config.EntitlementConnector,
config.EntitlementBalanceConnector,
namespacedriver.StaticNamespaceDecoder("default"),
staticNamespaceDecoder,
httptransport.WithErrorHandler(config.ErrorHandler),
)

router.creditHandler = credit_httpdriver.NewGrantHandler(
namespacedriver.StaticNamespaceDecoder("default"),
staticNamespaceDecoder,
config.GrantConnector,
httptransport.WithErrorHandler(config.ErrorHandler),
)
Expand Down

0 comments on commit 92478a7

Please sign in to comment.