mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-27 05:15:47 +00:00
add processor to push input into router (#104)
This commit is contained in:
55
internal/processor/router-input.go
Normal file
55
internal/processor/router-input.go
Normal file
@@ -0,0 +1,55 @@
|
|||||||
|
package processor
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/common"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
type RouterInput struct {
|
||||||
|
config config.ProcessorConfig
|
||||||
|
SourceId string
|
||||||
|
logger *slog.Logger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ro *RouterInput) Process(ctx context.Context, payload any) (any, error) {
|
||||||
|
|
||||||
|
router, ok := ctx.Value(common.RouterContextKey).(common.RouteIO)
|
||||||
|
if !ok {
|
||||||
|
return nil, errors.New("router.input no router found")
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err := router.HandleInput(ctx, ro.SourceId, payload)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, errors.New("router.input failed to send input")
|
||||||
|
}
|
||||||
|
|
||||||
|
return payload, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ro *RouterInput) Type() string {
|
||||||
|
return ro.config.Type
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
RegisterProcessor(ProcessorRegistration{
|
||||||
|
Type: "router.input",
|
||||||
|
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||||
|
|
||||||
|
params := config.Params
|
||||||
|
|
||||||
|
sourceId, err := params.GetString("source")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("router.input source error: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return &RouterInput{config: config, SourceId: sourceId, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -31,7 +31,7 @@ type Router struct {
|
|||||||
moduleWait sync.WaitGroup
|
moduleWait sync.WaitGroup
|
||||||
logger *slog.Logger
|
logger *slog.Logger
|
||||||
runningConfig config.Config
|
runningConfig config.Config
|
||||||
runningConfigMu sync.Mutex
|
runningConfigMu sync.RWMutex
|
||||||
wsConns []*websocket.Conn
|
wsConns []*websocket.Conn
|
||||||
wsConnsMu sync.Mutex
|
wsConnsMu sync.Mutex
|
||||||
apiServer *http.Server
|
apiServer *http.Server
|
||||||
@@ -183,10 +183,10 @@ func (r *Router) Stop() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []common.RouteIOError) {
|
func (r *Router) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []common.RouteIOError) {
|
||||||
r.runningConfigMu.Lock()
|
r.runningConfigMu.RLock()
|
||||||
defer r.runningConfigMu.Unlock()
|
defer r.runningConfigMu.RUnlock()
|
||||||
|
|
||||||
spanCtx, span := otel.Tracer("router").Start(ctx, "input", trace.WithAttributes(attribute.String("source.id", sourceId)), trace.WithNewRoot())
|
spanCtx, span := otel.Tracer("router").Start(ctx, "input", trace.WithAttributes(attribute.String("source.id", sourceId)))
|
||||||
defer span.End()
|
defer span.End()
|
||||||
var routeIOErrors []common.RouteIOError
|
var routeIOErrors []common.RouteIOError
|
||||||
routeFound := false
|
routeFound := false
|
||||||
|
|||||||
@@ -649,6 +649,29 @@
|
|||||||
"required": ["type"],
|
"required": ["type"],
|
||||||
"additionalProperties": false
|
"additionalProperties": false
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"type": "object",
|
||||||
|
"title": "Router Input",
|
||||||
|
"properties": {
|
||||||
|
"type": {
|
||||||
|
"type": "string",
|
||||||
|
"const": "router.input"
|
||||||
|
},
|
||||||
|
"params": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"module": {
|
||||||
|
"title": "Source",
|
||||||
|
"type": "string",
|
||||||
|
"description": "source to report as to the router"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["module"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["type", "params"],
|
||||||
|
"additionalProperties": false
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"title": "Router Output",
|
"title": "Router Output",
|
||||||
|
|||||||
Reference in New Issue
Block a user