diff --git a/internal/common/common.go b/internal/common/common.go new file mode 100644 index 0000000..a7adc74 --- /dev/null +++ b/internal/common/common.go @@ -0,0 +1,7 @@ +package common + +type contextKey string + +const RouterContextKey contextKey = contextKey("router") +const SourceContextKey contextKey = contextKey("source") +const ModulesContextKey contextKey = contextKey("modules") diff --git a/internal/module/http-client.go b/internal/module/http-client.go index fc38932..39f696c 100644 --- a/internal/module/http-client.go +++ b/internal/module/http-client.go @@ -7,6 +7,7 @@ import ( "net/http" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -41,7 +42,7 @@ func (hc *HTTPClient) Type() string { func (hc *HTTPClient) Start(ctx context.Context) error { hc.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("http.client unable to get router from context") diff --git a/internal/module/http-server.go b/internal/module/http-server.go index 85b5a2b..e436d52 100644 --- a/internal/module/http-server.go +++ b/internal/module/http-server.go @@ -8,6 +8,7 @@ import ( "log/slog" "net/http" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -142,7 +143,7 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) { func (hs *HTTPServer) Start(ctx context.Context) error { hs.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("http.server unable to get router from context") diff --git a/internal/module/midi-input.go b/internal/module/midi-input.go index 46e067d..a34ee68 100644 --- a/internal/module/midi-input.go +++ b/internal/module/midi-input.go @@ -8,6 +8,7 @@ import ( "fmt" "log/slog" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/route" "gitlab.com/gomidi/midi/v2" @@ -50,7 +51,7 @@ func (mi *MIDIInput) Type() string { func (mi *MIDIInput) Start(ctx context.Context) error { mi.logger.Debug("running") defer midi.CloseDriver() - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("midi.input unable to get router from context") diff --git a/internal/module/midi-output.go b/internal/module/midi-output.go index fbecb50..958ed47 100644 --- a/internal/module/midi-output.go +++ b/internal/module/midi-output.go @@ -8,6 +8,7 @@ import ( "fmt" "log/slog" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -52,7 +53,7 @@ func (mo *MIDIOutput) Type() string { func (mo *MIDIOutput) Start(ctx context.Context) error { mo.logger.Debug("running") defer midi.CloseDriver() - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("midi.output unable to get router from context") diff --git a/internal/module/mqtt-client.go b/internal/module/mqtt-client.go index 560237f..223b3ce 100644 --- a/internal/module/mqtt-client.go +++ b/internal/module/mqtt-client.go @@ -7,6 +7,7 @@ import ( "log/slog" mqtt "github.com/eclipse/paho.mqtt.golang" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -62,7 +63,7 @@ func (mc *MQTTClient) Type() string { func (mc *MQTTClient) Start(ctx context.Context) error { mc.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("mqtt.client unable to get router from context") diff --git a/internal/module/nats-client.go b/internal/module/nats-client.go index d488a15..fde35b1 100644 --- a/internal/module/nats-client.go +++ b/internal/module/nats-client.go @@ -5,6 +5,7 @@ import ( "errors" "log/slog" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -53,7 +54,7 @@ func (nc *NATSClient) Type() string { func (nc *NATSClient) Start(ctx context.Context) error { nc.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("nats.client unable to get router from context") diff --git a/internal/module/nats-server.go b/internal/module/nats-server.go index 908cc29..4f45bbf 100644 --- a/internal/module/nats-server.go +++ b/internal/module/nats-server.go @@ -8,6 +8,7 @@ import ( "net" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/route" "github.com/nats-io/nats-server/v2/server" @@ -66,7 +67,7 @@ func (ns *NATSServer) Type() string { func (ns *NATSServer) Start(ctx context.Context) error { ns.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("nats.server unable to get router from context") diff --git a/internal/module/psn-client.go b/internal/module/psn-client.go index d17ccf1..4cca068 100644 --- a/internal/module/psn-client.go +++ b/internal/module/psn-client.go @@ -9,6 +9,7 @@ import ( "time" "github.com/jwetzell/psn-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -43,7 +44,7 @@ func (pc *PSNClient) Type() string { func (pc *PSNClient) Start(ctx context.Context) error { pc.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("psn.client unable to get router from context") diff --git a/internal/module/serial-client.go b/internal/module/serial-client.go index 5cb38dc..8c757d0 100644 --- a/internal/module/serial-client.go +++ b/internal/module/serial-client.go @@ -9,6 +9,7 @@ import ( "log/slog" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" "github.com/jwetzell/showbridge-go/internal/processor" @@ -85,7 +86,7 @@ func (sc *SerialClient) SetupPort() error { func (sc *SerialClient) Start(ctx context.Context) error { sc.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("serial.client unable to get router from context") diff --git a/internal/module/sip-call-server.go b/internal/module/sip-call-server.go index 9dd616f..ac555aa 100644 --- a/internal/module/sip-call-server.go +++ b/internal/module/sip-call-server.go @@ -14,6 +14,7 @@ import ( "github.com/emiago/diago/media" "github.com/emiago/sipgo" "github.com/emiago/sipgo/sip" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -100,7 +101,7 @@ func (scs *SIPCallServer) Type() string { func (scs *SIPCallServer) Start(ctx context.Context) error { scs.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("sip.call.server unable to get router from context") diff --git a/internal/module/sip-dtmf-server.go b/internal/module/sip-dtmf-server.go index eab9ec7..69ed547 100644 --- a/internal/module/sip-dtmf-server.go +++ b/internal/module/sip-dtmf-server.go @@ -15,6 +15,7 @@ import ( "github.com/emiago/diago/media" "github.com/emiago/sipgo" "github.com/emiago/sipgo/sip" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -113,7 +114,7 @@ func (sds *SIPDTMFServer) Type() string { func (sds *SIPDTMFServer) Start(ctx context.Context) error { sds.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("sip.dtmf.server unable to get router from context") diff --git a/internal/module/tcp-client.go b/internal/module/tcp-client.go index 6c7fccd..e6cba7b 100644 --- a/internal/module/tcp-client.go +++ b/internal/module/tcp-client.go @@ -8,6 +8,7 @@ import ( "net" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" "github.com/jwetzell/showbridge-go/internal/processor" @@ -70,7 +71,7 @@ func (tc *TCPClient) Type() string { func (tc *TCPClient) Start(ctx context.Context) error { tc.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("net.tcp.client unable to get router from context") diff --git a/internal/module/tcp-server.go b/internal/module/tcp-server.go index 36b4603..4358438 100644 --- a/internal/module/tcp-server.go +++ b/internal/module/tcp-server.go @@ -11,6 +11,7 @@ import ( "syscall" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/framer" "github.com/jwetzell/showbridge-go/internal/processor" @@ -154,7 +155,7 @@ ClientRead: func (ts *TCPServer) Start(ctx context.Context) error { ts.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("net.tcp.server unable to get router from context") diff --git a/internal/module/time-interval.go b/internal/module/time-interval.go index 6de59a8..78065d0 100644 --- a/internal/module/time-interval.go +++ b/internal/module/time-interval.go @@ -7,6 +7,7 @@ import ( "log/slog" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -46,7 +47,7 @@ func (i *TimeInterval) Type() string { func (i *TimeInterval) Start(ctx context.Context) error { i.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("time.interval unable to get router from context") diff --git a/internal/module/time-timer.go b/internal/module/time-timer.go index f35fda4..1c00a76 100644 --- a/internal/module/time-timer.go +++ b/internal/module/time-timer.go @@ -7,6 +7,7 @@ import ( "log/slog" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -47,7 +48,7 @@ func (t *TimeTimer) Type() string { func (t *TimeTimer) Start(ctx context.Context) error { t.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("net.tcp.client unable to get router from context") diff --git a/internal/module/udp-client.go b/internal/module/udp-client.go index 9866a55..74edaeb 100644 --- a/internal/module/udp-client.go +++ b/internal/module/udp-client.go @@ -7,6 +7,7 @@ import ( "log/slog" "net" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -63,7 +64,7 @@ func (uc *UDPClient) SetupConn() error { func (uc *UDPClient) Start(ctx context.Context) error { uc.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("net.udp.client unable to get router from context") diff --git a/internal/module/udp-multicast.go b/internal/module/udp-multicast.go index a4262d0..e311f8e 100644 --- a/internal/module/udp-multicast.go +++ b/internal/module/udp-multicast.go @@ -8,6 +8,7 @@ import ( "net" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -57,7 +58,7 @@ func (um *UDPMulticast) Type() string { func (um *UDPMulticast) Start(ctx context.Context) error { um.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("net.udp.multicast unable to get router from context") diff --git a/internal/module/udp-server.go b/internal/module/udp-server.go index 23f7941..d8ad7d0 100644 --- a/internal/module/udp-server.go +++ b/internal/module/udp-server.go @@ -8,6 +8,7 @@ import ( "net" "time" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/route" ) @@ -69,7 +70,7 @@ func (us *UDPServer) Type() string { func (us *UDPServer) Start(ctx context.Context) error { us.logger.Debug("running") - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return errors.New("net.udp.server unable to get router from context") diff --git a/internal/route/route.go b/internal/route/route.go index 7b5e8a5..cc41a03 100644 --- a/internal/route/route.go +++ b/internal/route/route.go @@ -12,11 +12,6 @@ import ( "go.opentelemetry.io/otel/trace" ) -type routeContextKey string - -var RouterContextKey routeContextKey = routeContextKey("router") -var SourceContextKey routeContextKey = routeContextKey("source") - type RouteError struct { Index int Config config.RouteConfig diff --git a/internal/route/route_test.go b/internal/route/route_test.go index 8c3f751..dd8a9ca 100644 --- a/internal/route/route_test.go +++ b/internal/route/route_test.go @@ -5,6 +5,7 @@ import ( "slices" "testing" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/processor" "github.com/jwetzell/showbridge-go/internal/route" @@ -54,7 +55,7 @@ func TestGoodRouteHandleInput(t *testing.T) { } inputData := "test input data" - payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), inputData) + payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), inputData) if err != nil { t.Fatalf("route ProcessPayload returned error: %v", err) } @@ -84,7 +85,7 @@ func TestRouteHandleInputWithProcessorError(t *testing.T) { } inputData := "test input data" - _, err = testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), inputData) + _, err = testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), inputData) if err == nil { t.Fatalf("route HandleOutput did not return error for bad processor") } @@ -103,7 +104,7 @@ func TestRouteHandleNilPayload(t *testing.T) { return } - payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), nil) + payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), nil) if err != nil { t.Fatalf("route ProcessPayload returned error: %v", err) } @@ -126,7 +127,7 @@ func TestRouteHandleNilPayloadFromProcessor(t *testing.T) { t.Fatalf("route failed to create: %v", err) } - payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), "test") + payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), common.RouterContextKey, &MockRouter{}), "test") if err != nil { t.Fatalf("route HandleOutput returned error for nil payload: %v", err) } diff --git a/router.go b/router.go index 6c36ca1..6ccd30c 100644 --- a/router.go +++ b/router.go @@ -6,6 +6,7 @@ import ( "log/slog" "sync" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/module" "github.com/jwetzell/showbridge-go/internal/route" @@ -154,7 +155,7 @@ func (r *Router) Start(ctx context.Context) { routerContext, cancel := context.WithCancel(ctx) r.Context = routerContext r.contextCancel = cancel - contextWithRouter := context.WithValue(routerContext, route.RouterContextKey, r) + contextWithRouter := context.WithValue(routerContext, common.RouterContextKey, r) for moduleId := range r.ModuleInstances { // TODO(jwetzell): handle module run errors @@ -188,7 +189,8 @@ func (r *Router) HandleInput(ctx context.Context, sourceId string, payload any) routeWaitGroup.Go(func() { routeFound = true - routeContext := context.WithValue(spanCtx, route.SourceContextKey, sourceId) + routeContext := context.WithValue(spanCtx, common.SourceContextKey, sourceId) + routeContext = context.WithValue(routeContext, common.ModulesContextKey, r.ModuleInstances) routeCtx, routeSpan := otel.Tracer("router").Start(routeContext, "route", trace.WithAttributes(attribute.Int("route.index", routeIndex), attribute.String("route.input", routeInstance.Input()), attribute.String("route.output", routeInstance.Output()))) payload, err := routeInstance.ProcessPayload(routeCtx, payload) diff --git a/router_test.go b/router_test.go index d5a3ee7..981d0ad 100644 --- a/router_test.go +++ b/router_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/jwetzell/showbridge-go" + "github.com/jwetzell/showbridge-go/internal/common" "github.com/jwetzell/showbridge-go/internal/config" "github.com/jwetzell/showbridge-go/internal/module" "github.com/jwetzell/showbridge-go/internal/route" @@ -33,7 +34,7 @@ func (mcm *MockCounterModule) Output(context.Context, any) error { } func (mcm *MockCounterModule) Start(ctx context.Context) error { - router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO) + router, ok := ctx.Value(common.RouterContextKey).(route.RouteIO) if !ok { return fmt.Errorf("mock.counter could not get router from context")