mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
add sender to template data for relevant modules
This commit is contained in:
@@ -5,3 +5,4 @@ type contextKey string
|
|||||||
const RouterContextKey contextKey = contextKey("router")
|
const RouterContextKey contextKey = contextKey("router")
|
||||||
const SourceContextKey contextKey = contextKey("source")
|
const SourceContextKey contextKey = contextKey("source")
|
||||||
const ModulesContextKey contextKey = contextKey("modules")
|
const ModulesContextKey contextKey = contextKey("modules")
|
||||||
|
const SenderContextKey contextKey = contextKey("sender")
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/jwetzell/showbridge-go/internal/common"
|
"github.com/jwetzell/showbridge-go/internal/common"
|
||||||
@@ -84,6 +85,10 @@ func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
if hs.router != nil {
|
if hs.router != nil {
|
||||||
inputContext := context.WithValue(hs.ctx, httpServerContextKey("responseWriter"), &responseWriter)
|
inputContext := context.WithValue(hs.ctx, httpServerContextKey("responseWriter"), &responseWriter)
|
||||||
|
senderAddr, err := net.ResolveTCPAddr("tcp", r.RemoteAddr)
|
||||||
|
if err == nil {
|
||||||
|
inputContext = context.WithValue(inputContext, common.SenderContextKey, senderAddr)
|
||||||
|
}
|
||||||
aRouteFound, routingErrors := hs.router.HandleInput(inputContext, hs.Id(), r)
|
aRouteFound, routingErrors := hs.router.HandleInput(inputContext, hs.Id(), r)
|
||||||
if !responseWriter.done {
|
if !responseWriter.done {
|
||||||
if aRouteFound {
|
if aRouteFound {
|
||||||
|
|||||||
@@ -142,7 +142,13 @@ ClientRead:
|
|||||||
messages := ts.Framer.Decode(buffer[0:byteCount])
|
messages := ts.Framer.Decode(buffer[0:byteCount])
|
||||||
for _, message := range messages {
|
for _, message := range messages {
|
||||||
if ts.router != nil {
|
if ts.router != nil {
|
||||||
|
senderAddr, ok := client.RemoteAddr().(*net.TCPAddr)
|
||||||
|
if ok {
|
||||||
|
senderCtx := context.WithValue(ts.ctx, common.SenderContextKey, senderAddr)
|
||||||
|
ts.router.HandleInput(senderCtx, ts.Id(), message)
|
||||||
|
} else {
|
||||||
ts.router.HandleInput(ts.ctx, ts.Id(), message)
|
ts.router.HandleInput(ts.ctx, ts.Id(), message)
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
ts.logger.Error("input received but no router is configured")
|
ts.logger.Error("input received but no router is configured")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,7 +97,7 @@ func (us *UDPServer) Start(ctx context.Context) error {
|
|||||||
default:
|
default:
|
||||||
listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||||
|
|
||||||
numBytes, _, err := listener.ReadFromUDP(buffer)
|
numBytes, senderAddr, err := listener.ReadFromUDP(buffer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
//NOTE(jwetzell) we hit deadline
|
//NOTE(jwetzell) we hit deadline
|
||||||
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
|
if opErr, ok := err.(*net.OpError); ok && opErr.Timeout() {
|
||||||
@@ -107,7 +107,8 @@ func (us *UDPServer) Start(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
message := buffer[:numBytes]
|
message := buffer[:numBytes]
|
||||||
if us.router != nil {
|
if us.router != nil {
|
||||||
us.router.HandleInput(us.ctx, us.Id(), message)
|
senderCtx := context.WithValue(us.ctx, common.SenderContextKey, senderAddr)
|
||||||
|
us.router.HandleInput(senderCtx, us.Id(), message)
|
||||||
} else {
|
} else {
|
||||||
us.logger.Error("input received but no router is configured")
|
us.logger.Error("input received but no router is configured")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,6 +50,7 @@ func GetAnyAs[T any](p any) (T, bool) {
|
|||||||
type TemplateData struct {
|
type TemplateData struct {
|
||||||
Payload any
|
Payload any
|
||||||
Modules any
|
Modules any
|
||||||
|
Sender any
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTemplateData(ctx context.Context, payload any) TemplateData {
|
func GetTemplateData(ctx context.Context, payload any) TemplateData {
|
||||||
@@ -58,5 +59,10 @@ func GetTemplateData(ctx context.Context, payload any) TemplateData {
|
|||||||
if modules != nil {
|
if modules != nil {
|
||||||
templateData.Modules = modules
|
templateData.Modules = modules
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sender := ctx.Value(common.SenderContextKey)
|
||||||
|
if sender != nil {
|
||||||
|
templateData.Sender = sender
|
||||||
|
}
|
||||||
return templateData
|
return templateData
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user