mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add the concept of processors
This commit is contained in:
68
route.go
68
route.go
@@ -1,32 +1,62 @@
|
||||
package showbridge
|
||||
|
||||
import "log/slog"
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internals/processing"
|
||||
)
|
||||
|
||||
type Route struct {
|
||||
index int
|
||||
Input string
|
||||
Output string
|
||||
router *Router
|
||||
index int
|
||||
Input string
|
||||
Processors []processing.Processor
|
||||
Output string
|
||||
router *Router
|
||||
}
|
||||
|
||||
type RouteConfig struct {
|
||||
Input string `json:"input"`
|
||||
Output string `json:"output"`
|
||||
Input string `json:"input"`
|
||||
Processors []processing.ProcessorConfig `json:"processors"`
|
||||
Output string `json:"output"`
|
||||
}
|
||||
|
||||
func NewRoute(index int, config RouteConfig, router *Router) *Route {
|
||||
return &Route{Input: config.Input, Output: config.Output, router: router, index: index}
|
||||
}
|
||||
func NewRoute(index int, config RouteConfig, router *Router) (*Route, error) {
|
||||
processors := []processing.Processor{}
|
||||
|
||||
func (r *Route) HandleInput(sourceId string, payload any) {
|
||||
slog.Debug("route input", "index", r.index, "source", sourceId, "payload", payload)
|
||||
r.HandleOutput(payload)
|
||||
}
|
||||
if len(config.Processors) > 0 {
|
||||
for _, processorDecl := range config.Processors {
|
||||
processorInfo, ok := processing.ProcessorRegistry[processorDecl.Type]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("problem loading processor registration for processor type: %s", processorDecl.Type)
|
||||
}
|
||||
|
||||
func (r *Route) HandleOutput(payload any) {
|
||||
slog.Debug("route output", "index", r.index, "destination", r.Output, "payload", payload)
|
||||
err := r.router.HandleOutput(r.Output, payload)
|
||||
if err != nil {
|
||||
slog.Error("problem with route output", "error", err.Error())
|
||||
processor, err := processorInfo.New(processorDecl)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
processors = append(processors, processor)
|
||||
}
|
||||
}
|
||||
|
||||
return &Route{Input: config.Input, Processors: processors, Output: config.Output, router: router, index: index}, nil
|
||||
}
|
||||
|
||||
func (r *Route) HandleInput(sourceId string, payload any) error {
|
||||
slog.Debug("route input", "index", r.index, "source", sourceId, "payload", payload)
|
||||
slog.Debug("route processing", "processorCount", len(r.Processors))
|
||||
|
||||
var err error
|
||||
for _, processor := range r.Processors {
|
||||
payload, err = processor.Process(r.router.Context, payload)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return r.HandleOutput(payload)
|
||||
}
|
||||
|
||||
func (r *Route) HandleOutput(payload any) error {
|
||||
slog.Debug("route output", "index", r.index, "destination", r.Output, "payload", payload)
|
||||
return r.router.HandleOutput(r.Output, payload)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user