mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
inject router on HandleInput
This commit is contained in:
15
route.go
15
route.go
@@ -18,10 +18,9 @@ type Route struct {
|
|||||||
Input string
|
Input string
|
||||||
Processors []processing.Processor
|
Processors []processing.Processor
|
||||||
Output string
|
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{}
|
processors := []processing.Processor{}
|
||||||
|
|
||||||
if len(config.Processors) > 0 {
|
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
|
var err error
|
||||||
for _, processor := range r.Processors {
|
for _, processor := range r.Processors {
|
||||||
payload, err = processor.Process(r.router.Context, payload)
|
payload, err = processor.Process(router.Context, payload)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -54,9 +53,9 @@ func (r *Route) HandleInput(sourceId string, payload any) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return r.HandleOutput(sourceId, payload)
|
return r.HandleOutput(sourceId, payload, router)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Route) HandleOutput(sourceId string, payload any) error {
|
func (r *Route) HandleOutput(sourceId string, payload any, router *Router) error {
|
||||||
return r.router.HandleOutput(sourceId, r.Output, payload)
|
return router.HandleOutput(sourceId, r.Output, payload)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []ModuleErro
|
|||||||
|
|
||||||
var routeErrors []RouteError
|
var routeErrors []RouteError
|
||||||
for routeIndex, routeDecl := range config.Routes {
|
for routeIndex, routeDecl := range config.Routes {
|
||||||
route, err := NewRoute(routeIndex, routeDecl, &router)
|
route, err := NewRoute(routeIndex, routeDecl)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if routeErrors == nil {
|
if routeErrors == nil {
|
||||||
routeErrors = []RouteError{}
|
routeErrors = []RouteError{}
|
||||||
@@ -138,7 +138,7 @@ func (r *Router) HandleInput(sourceId string, payload any) []RoutingError {
|
|||||||
var routingErrors []RoutingError
|
var routingErrors []RoutingError
|
||||||
for routeIndex, route := range r.RouteInstances {
|
for routeIndex, route := range r.RouteInstances {
|
||||||
if route.Input == sourceId {
|
if route.Input == sourceId {
|
||||||
err := route.HandleInput(sourceId, payload)
|
err := route.HandleInput(sourceId, payload, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if routingErrors == nil {
|
if routingErrors == nil {
|
||||||
routingErrors = []RoutingError{}
|
routingErrors = []RoutingError{}
|
||||||
|
|||||||
Reference in New Issue
Block a user