mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 12:55:29 +00:00
add the concept of routes input/output
This commit is contained in:
31
router.go
31
router.go
@@ -10,21 +10,23 @@ import (
|
||||
type Router struct {
|
||||
Context context.Context
|
||||
ModuleInstances []Module
|
||||
RouteInstances []*Route
|
||||
}
|
||||
|
||||
func NewRouter(ctx context.Context, config Config) (*Router, error) {
|
||||
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelDebug,
|
||||
Level: slog.LevelInfo,
|
||||
}))
|
||||
|
||||
slog.SetDefault(logger)
|
||||
|
||||
slog.Debug("creating router", "config", config)
|
||||
slog.Debug("creating router")
|
||||
|
||||
router := Router{
|
||||
Context: ctx,
|
||||
ModuleInstances: []Module{},
|
||||
RouteInstances: []*Route{},
|
||||
}
|
||||
|
||||
for _, moduleDecl := range config.Modules {
|
||||
@@ -54,6 +56,14 @@ func NewRouter(ctx context.Context, config Config) (*Router, error) {
|
||||
|
||||
}
|
||||
|
||||
for routeIndex, routeDecl := range config.Routes {
|
||||
router.RouteInstances = append(router.RouteInstances, NewRoute(routeIndex, routeDecl, &router))
|
||||
}
|
||||
|
||||
for _, moduleInstance := range router.ModuleInstances {
|
||||
moduleInstance.RegisterRouter(&router)
|
||||
}
|
||||
|
||||
return &router, nil
|
||||
}
|
||||
|
||||
@@ -63,3 +73,20 @@ func (r *Router) Run() {
|
||||
}
|
||||
<-r.Context.Done()
|
||||
}
|
||||
|
||||
func (r *Router) HandleInput(sourceId string, payload any) {
|
||||
for _, route := range r.RouteInstances {
|
||||
if route.Input == sourceId {
|
||||
route.HandleInput(sourceId, payload)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Router) HandleOutput(destinationId string, payload any) error {
|
||||
for _, moduleInstance := range r.ModuleInstances {
|
||||
if moduleInstance.Id() == destinationId {
|
||||
return moduleInstance.Output(payload)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("no module instance found for destination %s", destinationId)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user