inject router on HandleInput

This commit is contained in:
Joel Wetzell
2025-12-06 23:08:05 -06:00
parent 9a06949b5b
commit 388851f8bd
2 changed files with 9 additions and 10 deletions

View File

@@ -18,10 +18,9 @@ type Route struct {
Input string
Processors []processing.Processor
Output string
router *Router
}
func NewRoute(index int, config config.RouteConfig, router *Router) (*Route, error) {
func NewRoute(index int, config config.RouteConfig) (*Route, error) {
processors := []processing.Processor{}
if len(config.Processors) > 0 {
@@ -39,13 +38,13 @@ func NewRoute(index int, config config.RouteConfig, router *Router) (*Route, err
}
}
return &Route{Input: config.Input, Processors: processors, Output: config.Output, router: router, index: index}, nil
return &Route{Input: config.Input, Processors: processors, Output: config.Output, index: index}, nil
}
func (r *Route) HandleInput(sourceId string, payload any) error {
func (r *Route) HandleInput(sourceId string, payload any, router *Router) error {
var err error
for _, processor := range r.Processors {
payload, err = processor.Process(r.router.Context, payload)
payload, err = processor.Process(router.Context, payload)
if err != nil {
return err
}
@@ -54,9 +53,9 @@ func (r *Route) HandleInput(sourceId string, payload any) error {
return nil
}
}
return r.HandleOutput(sourceId, payload)
return r.HandleOutput(sourceId, payload, router)
}
func (r *Route) HandleOutput(sourceId string, payload any) error {
return r.router.HandleOutput(sourceId, r.Output, payload)
func (r *Route) HandleOutput(sourceId string, payload any, router *Router) error {
return router.HandleOutput(sourceId, r.Output, payload)
}

View File

@@ -95,7 +95,7 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []ModuleErro
var routeErrors []RouteError
for routeIndex, routeDecl := range config.Routes {
route, err := NewRoute(routeIndex, routeDecl, &router)
route, err := NewRoute(routeIndex, routeDecl)
if err != nil {
if routeErrors == nil {
routeErrors = []RouteError{}
@@ -138,7 +138,7 @@ func (r *Router) HandleInput(sourceId string, payload any) []RoutingError {
var routingErrors []RoutingError
for routeIndex, route := range r.RouteInstances {
if route.Input == sourceId {
err := route.HandleInput(sourceId, payload)
err := route.HandleInput(sourceId, payload, r)
if err != nil {
if routingErrors == nil {
routingErrors = []RoutingError{}