mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-30 06:45:30 +00:00
Compare commits
150 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
100edab869 | ||
|
|
fd4c2cb59b | ||
|
|
b6ee3c68f1 | ||
|
|
7e0c2b2560 | ||
|
|
c9a3eb80bc | ||
|
|
2a5feec07a | ||
|
|
ced4bc839e | ||
|
|
ce6a9ff4a8 | ||
|
|
bd3b8a19d5 | ||
|
|
cb738a9b8e | ||
|
|
1b59dbc1e5 | ||
|
|
c75a2327d5 | ||
|
|
a17be985e6 | ||
|
|
78737f57af | ||
|
|
720c0cd52a | ||
|
|
3e1d4f07f9 | ||
|
|
452e308f8f | ||
|
|
d16d833520 | ||
|
|
f203b32261 | ||
|
|
361c4f4499 | ||
|
|
a76d12d799 | ||
|
|
7d91e64ec4 | ||
|
|
589c59c693 | ||
|
|
56c67c5445 | ||
|
|
13229215a6 | ||
|
|
909b4d2337 | ||
|
|
3bcb9a6608 | ||
|
|
17dcffc09b | ||
|
|
8318645eef | ||
|
|
0c777a8874 | ||
|
|
a8f9894ff0 | ||
|
|
f0f74f50e5 | ||
|
|
bb3a8b982b | ||
|
|
be9ce5a8aa | ||
|
|
ac2cf078a4 | ||
|
|
0fc264d0e9 | ||
|
|
3c99eef999 | ||
|
|
f7c3a85649 | ||
|
|
76caccd9f8 | ||
|
|
e8c71562a6 | ||
|
|
42551127ba | ||
|
|
a99fef9389 | ||
|
|
e6099b6acd | ||
|
|
b93d754c19 | ||
|
|
710dcf3a02 | ||
|
|
ac83b339a6 | ||
|
|
cb06e2dbb5 | ||
|
|
3ef41d0026 | ||
|
|
8dcb70bfee | ||
|
|
59f00c1a32 | ||
|
|
6e88d259b8 | ||
|
|
bb33974e1c | ||
|
|
bd2a68ff6e | ||
|
|
7e2d76ef3a | ||
|
|
d1cec1e094 | ||
|
|
477d70fad0 | ||
|
|
70f4636522 | ||
|
|
0248ca6973 | ||
|
|
4aa586427b | ||
|
|
6d3cf6692f | ||
|
|
a263b10690 | ||
|
|
3ce2909b0f | ||
|
|
b15e282d59 | ||
|
|
f97f9b9fc9 | ||
|
|
12de947f3d | ||
|
|
7335ba973a | ||
|
|
a994286402 | ||
|
|
b639a5c786 | ||
|
|
d170958d9b | ||
|
|
ed4c14e82b | ||
|
|
fcb1378784 | ||
|
|
97f4cbeace | ||
|
|
8ffc7d02a5 | ||
|
|
3458b52206 | ||
|
|
e12b6e098e | ||
|
|
58cb7766fe | ||
|
|
407f1f3618 | ||
|
|
3c0f555a6f | ||
|
|
6c6e50e0eb | ||
|
|
0ed4d6669f | ||
|
|
cc37559bde | ||
|
|
18b307bca1 | ||
|
|
17106b5096 | ||
|
|
df9ffdda9e | ||
|
|
479cf41ea0 | ||
|
|
af833cb212 | ||
|
|
a6a8e275ee | ||
|
|
6968539c59 | ||
|
|
e96913230b | ||
|
|
f1dff33704 | ||
|
|
ff426994e4 | ||
|
|
a1275b3b69 | ||
|
|
227d042feb | ||
|
|
e53515267e | ||
|
|
502586dec6 | ||
|
|
cad3714664 | ||
|
|
cf6519b594 | ||
|
|
7dcac9470a | ||
|
|
e5268f42f8 | ||
|
|
ccff105e37 | ||
|
|
6611821155 | ||
|
|
7cde1c244a | ||
|
|
70878e6df6 | ||
|
|
bd005da358 | ||
|
|
25f2ec30c2 | ||
|
|
e679bf2b46 | ||
|
|
460d2f051d | ||
|
|
efdcbae5c4 | ||
|
|
076a13f48a | ||
|
|
971eea6e41 | ||
|
|
1172159455 | ||
|
|
ecb415f321 | ||
|
|
b552672011 | ||
|
|
168265f0a9 | ||
|
|
caee7b269c | ||
|
|
b0cc47236f | ||
|
|
f6c2b1d9ac | ||
|
|
f977b845be | ||
|
|
0166383978 | ||
|
|
66cfaa3091 | ||
|
|
0904d2fcb8 | ||
|
|
6a13c38e77 | ||
|
|
1a8ccfc64a | ||
|
|
2e3bb408c3 | ||
|
|
3d75165a61 | ||
|
|
50f755f914 | ||
|
|
b10e296d0a | ||
|
|
91c44420cb | ||
|
|
c1b2fa714e | ||
|
|
ff1949ce69 | ||
|
|
76583b1fb8 | ||
|
|
e6433878b5 | ||
|
|
3ad067cbb8 | ||
|
|
30b406d601 | ||
|
|
7315d40015 | ||
|
|
e7105b8b39 | ||
|
|
ec40194ecb | ||
|
|
85d8dc5787 | ||
|
|
b27bfb1a6d | ||
|
|
fb9c0bc657 | ||
|
|
d32d23041b | ||
|
|
b4149df00a | ||
|
|
61bd4b64f5 | ||
|
|
38d73881c9 | ||
|
|
3138bdfcdb | ||
|
|
ba862300b2 | ||
|
|
e414bf336e | ||
|
|
da95fb5c75 | ||
|
|
1fdc30aa9e | ||
|
|
128920dff9 |
5
.dockerignore
Normal file
5
.dockerignore
Normal file
@@ -0,0 +1,5 @@
|
||||
Dockerfile
|
||||
.github
|
||||
.vscode
|
||||
README.md
|
||||
LICENSE
|
||||
6
.github/labeler.yml
vendored
6
.github/labeler.yml
vendored
@@ -14,10 +14,14 @@ processor:
|
||||
- changed-files:
|
||||
- any-glob-to-any-file: 'internal/processor/**'
|
||||
|
||||
router:
|
||||
- changed-files:
|
||||
- any-glob-to-any-file: 'router*'
|
||||
|
||||
route:
|
||||
- changed-files:
|
||||
- any-glob-to-any-file: 'internal/route/**'
|
||||
|
||||
|
||||
cli:
|
||||
- changed-files:
|
||||
- any-glob-to-any-file: 'cmd/showbridge/**'
|
||||
|
||||
5
.github/release.yml
vendored
5
.github/release.yml
vendored
@@ -17,12 +17,13 @@ changelog:
|
||||
- title: Processor 🏭
|
||||
labels:
|
||||
- processor
|
||||
- title: Route 🛣️
|
||||
- title: Routing 🛣️
|
||||
labels:
|
||||
- router
|
||||
- route
|
||||
- title: CLI ⌨️
|
||||
labels:
|
||||
- cmd
|
||||
- cli
|
||||
- title: Other Changes
|
||||
labels:
|
||||
- '*'
|
||||
39
.github/workflows/release-showbridge.yaml
vendored
39
.github/workflows/release-showbridge.yaml
vendored
@@ -3,7 +3,7 @@ name: showbridge release
|
||||
on:
|
||||
push:
|
||||
tags:
|
||||
- "*"
|
||||
- "v*"
|
||||
|
||||
permissions:
|
||||
contents: write
|
||||
@@ -29,3 +29,40 @@ jobs:
|
||||
args: release --clean
|
||||
env:
|
||||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
|
||||
docker:
|
||||
runs-on: ubuntu-latest
|
||||
needs: goreleaser
|
||||
steps:
|
||||
- name: Check out repository
|
||||
uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v6.0.0
|
||||
|
||||
- name: Set up QEMU
|
||||
uses: docker/setup-qemu-action@c7c53464625b32c7a7e944ae62b3e17d2b600130 # v3.7.0
|
||||
|
||||
- name: Set up Docker Buildx
|
||||
uses: docker/setup-buildx-action@8d2750c68a42422c14e847fe6c8ac0403b4cbd6f # v3.12.0
|
||||
|
||||
- name: Login to Docker Hub
|
||||
uses: docker/login-action@c94ce9fb468520275223c153574b00df6fe4bcc9 # v3.7.0
|
||||
with:
|
||||
username: ${{ secrets.DOCKERHUB_USERNAME }}
|
||||
password: ${{ secrets.DOCKERHUB_TOKEN }}
|
||||
|
||||
- name: Setup Docker metadata
|
||||
id: meta
|
||||
uses: docker/metadata-action@c299e40c65443455700f0fdfc63efafe5b349051 # v5.10.0
|
||||
with:
|
||||
images: |
|
||||
jwetzell/showbridge
|
||||
|
||||
- name: Build and push
|
||||
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83 # v6.18.0
|
||||
with:
|
||||
push: true
|
||||
context: ./
|
||||
file: ./Dockerfile
|
||||
tags: ${{ steps.meta.outputs.tags }}
|
||||
labels: ${{ steps.meta.outputs.labels }}
|
||||
cache-from: type=gha
|
||||
cache-to: type=gha,mode=max
|
||||
platforms: linux/amd64,linux/arm64,linux/arm/v7
|
||||
|
||||
4
.vscode/launch.json
vendored
4
.vscode/launch.json
vendored
@@ -10,7 +10,9 @@
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
"program": "cmd/showbridge"
|
||||
"program": "cmd/showbridge",
|
||||
"args": ["--log-level", "debug"],
|
||||
"cwd": "./"
|
||||
}
|
||||
]
|
||||
}
|
||||
15
Dockerfile
Normal file
15
Dockerfile
Normal file
@@ -0,0 +1,15 @@
|
||||
ARG GO_VERSION=1.25.5
|
||||
FROM golang:${GO_VERSION}-alpine AS build
|
||||
RUN apk --no-cache add ca-certificates tzdata
|
||||
WORKDIR /build
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build ./cmd/showbridge
|
||||
|
||||
FROM scratch
|
||||
WORKDIR /app
|
||||
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
|
||||
COPY --from=build /build/showbridge /app/showbridge
|
||||
ENTRYPOINT [ "/app/showbridge" ]
|
||||
41
README.md
41
README.md
@@ -6,28 +6,37 @@ Simple protocol router _/s_
|
||||
|
||||
</div>
|
||||
|
||||
<p align="center">
|
||||
<a href="https://github.com/jwetzell/showbridge-go/releases">Releases</a> ·
|
||||
<a href="https://docs.showbridge.io">Documentation</a>
|
||||
</p>
|
||||
|
||||
### Supported Protocols
|
||||
- HTTP
|
||||
- client
|
||||
- server
|
||||
- UDP
|
||||
- client
|
||||
- server
|
||||
- TCP
|
||||
- client
|
||||
- server
|
||||
- [MQTT](https://mqtt.org/)
|
||||
- client
|
||||
- [NATS](https://nats.io/)
|
||||
- client
|
||||
- [PosiStageNet](https://posistage.net/)
|
||||
- client
|
||||
- MIDI
|
||||
- client (not included in pre-built binaries yet)
|
||||
- MIDI (not included in pre-built binaries yet)
|
||||
- Serial (not included in pre-built binaries yet)
|
||||
- [OSC](https://opensoundcontrol.stanford.edu/spec-1_0.html)
|
||||
- [FreeD](https://ptzoptics.com/freed/)
|
||||
- [SIP](https://en.wikipedia.org/wiki/Session_Initiation_Protocol)
|
||||
- call server
|
||||
- [DTMF](https://en.wikipedia.org/wiki/Dual-tone_multi-frequency_signaling) server
|
||||
|
||||
|
||||
|
||||
|
||||
### CLI Usage
|
||||
|
||||
```
|
||||
NAME:
|
||||
showbridge - Simple protocol router /s
|
||||
|
||||
USAGE:
|
||||
showbridge [global options]
|
||||
|
||||
GLOBAL OPTIONS:
|
||||
--config string path to config file (default: "./config.yaml")
|
||||
--log-level string set log level (default: "info")
|
||||
--log-format string log format to use (default: "text")
|
||||
--help, -h show help
|
||||
--version, -v print the version
|
||||
```
|
||||
|
||||
@@ -2,10 +2,13 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"slices"
|
||||
"sync"
|
||||
|
||||
"github.com/jwetzell/showbridge-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -20,37 +23,43 @@ var (
|
||||
func main() {
|
||||
cmd := &cli.Command{
|
||||
Name: "showbridge",
|
||||
Usage: "Simple protocol router /s",
|
||||
Version: version,
|
||||
Flags: []cli.Flag{
|
||||
&cli.StringFlag{
|
||||
Name: "config",
|
||||
Value: "./config.yaml",
|
||||
Usage: "path to config file",
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "log-level",
|
||||
Value: "info",
|
||||
Usage: "set log level",
|
||||
Validator: func(level string) error {
|
||||
levels := []string{"debug", "info", "warn", "error"}
|
||||
if !slices.Contains(levels, level) {
|
||||
return fmt.Errorf("unknown log level: %s", level)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
&cli.StringFlag{
|
||||
Name: "log-format",
|
||||
Value: "text",
|
||||
Usage: "log format to use",
|
||||
Validator: func(format string) error {
|
||||
formats := []string{"text", "json"}
|
||||
if !slices.Contains(formats, format) {
|
||||
return fmt.Errorf("unknown log format: %s", format)
|
||||
}
|
||||
return nil
|
||||
},
|
||||
},
|
||||
},
|
||||
Action: func(ctx context.Context, c *cli.Command) error {
|
||||
configPath := c.String("config")
|
||||
if configPath == "" {
|
||||
return fmt.Errorf("config value cannot be empty")
|
||||
}
|
||||
|
||||
config, err := readConfig(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(ctx, config)
|
||||
for _, moduleError := range moduleErrors {
|
||||
slog.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||
}
|
||||
|
||||
for _, routeError := range routeErrors {
|
||||
slog.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
||||
}
|
||||
router.Run()
|
||||
return nil
|
||||
},
|
||||
Action: run,
|
||||
}
|
||||
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, os.Interrupt)
|
||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||
defer cancel()
|
||||
err := cmd.Run(ctx, os.Args)
|
||||
|
||||
@@ -76,3 +85,78 @@ func readConfig(configPath string) (config.Config, error) {
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func run(ctx context.Context, c *cli.Command) error {
|
||||
configPath := c.String("config")
|
||||
if configPath == "" {
|
||||
return errors.New("config value cannot be empty")
|
||||
}
|
||||
|
||||
config, err := readConfig(configPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
logLevel := slog.LevelInfo
|
||||
|
||||
logLevelFromFlag := c.String("log-level")
|
||||
|
||||
switch logLevelFromFlag {
|
||||
case "debug":
|
||||
logLevel = slog.LevelDebug
|
||||
case "info":
|
||||
logLevel = slog.LevelInfo
|
||||
case "warn":
|
||||
logLevel = slog.LevelWarn
|
||||
case "error":
|
||||
logLevel = slog.LevelError
|
||||
default:
|
||||
logLevel = slog.LevelInfo
|
||||
}
|
||||
|
||||
logHandlerOptions := &slog.HandlerOptions{
|
||||
Level: logLevel,
|
||||
}
|
||||
|
||||
logOutput := os.Stderr
|
||||
|
||||
var logHandler slog.Handler
|
||||
|
||||
logFormat := c.String("log-format")
|
||||
|
||||
switch logFormat {
|
||||
case "json":
|
||||
logHandler = slog.NewJSONHandler(logOutput, logHandlerOptions)
|
||||
case "text":
|
||||
logHandler = slog.NewTextHandler(logOutput, logHandlerOptions)
|
||||
default:
|
||||
logHandler = slog.NewTextHandler(logOutput, logHandlerOptions)
|
||||
}
|
||||
|
||||
slog.SetDefault(slog.New(logHandler))
|
||||
|
||||
commandLogger := slog.Default().With("component", "cmd")
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(config)
|
||||
|
||||
for _, moduleError := range moduleErrors {
|
||||
commandLogger.Error("problem initializing module", "index", moduleError.Index, "error", moduleError.Error)
|
||||
}
|
||||
|
||||
for _, routeError := range routeErrors {
|
||||
commandLogger.Error("problem initializing route", "index", routeError.Index, "error", routeError.Error)
|
||||
}
|
||||
|
||||
routerRunner := sync.WaitGroup{}
|
||||
|
||||
routerRunner.Go(func() {
|
||||
router.Run(context.Background())
|
||||
})
|
||||
|
||||
<-ctx.Done()
|
||||
commandLogger.Debug("shutting down router")
|
||||
router.Stop()
|
||||
commandLogger.Debug("waiting for router to exit")
|
||||
routerRunner.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
modules:
|
||||
- id: http
|
||||
type: net.http.server
|
||||
type: http.server
|
||||
params:
|
||||
port: 3000
|
||||
- id: udp
|
||||
|
||||
33
go.mod
33
go.mod
@@ -1,55 +1,66 @@
|
||||
module github.com/jwetzell/showbridge-go
|
||||
|
||||
go 1.25.3
|
||||
go 1.25.5
|
||||
|
||||
require (
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1
|
||||
github.com/emiago/diago v0.23.1-0.20251211215055-e1d875617111
|
||||
github.com/emiago/sipgo v1.0.1-alpha.0.20251212165843-9c9bcdf9126f
|
||||
github.com/expr-lang/expr v1.17.6
|
||||
github.com/emiago/diago v0.26.2
|
||||
github.com/emiago/sipgo v1.1.2
|
||||
github.com/expr-lang/expr v1.17.7
|
||||
github.com/extism/go-sdk v1.7.1
|
||||
github.com/jwetzell/artnet-go v0.2.1
|
||||
github.com/jwetzell/free-d-go v0.1.0
|
||||
github.com/jwetzell/osc-go v0.1.0
|
||||
github.com/jwetzell/psn-go v0.3.0
|
||||
github.com/nats-io/nats.go v1.47.0
|
||||
github.com/urfave/cli/v3 v3.6.1
|
||||
github.com/nats-io/nats.go v1.48.0
|
||||
github.com/urfave/cli/v3 v3.6.2
|
||||
gitlab.com/gomidi/midi/v2 v2.3.18
|
||||
go.bug.st/serial v1.6.4
|
||||
modernc.org/quickjs v0.17.0
|
||||
modernc.org/quickjs v0.17.1
|
||||
sigs.k8s.io/yaml v1.6.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/creack/goselect v0.1.2 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/dylibso/observe-sdk/go v0.0.0-20240819160327-2d926c5d788a // indirect
|
||||
github.com/emiago/dtls/v3 v3.0.0-20260122183559-8b8d23e359c0 // indirect
|
||||
github.com/go-audio/riff v1.0.0 // indirect
|
||||
github.com/gobwas/glob v0.2.3 // indirect
|
||||
github.com/gobwas/httphead v0.1.0 // indirect
|
||||
github.com/gobwas/pool v0.2.1 // indirect
|
||||
github.com/gobwas/ws v1.4.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/gorilla/websocket v1.5.3 // indirect
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20240805132620-81f5be970eca // indirect
|
||||
github.com/icholy/digest v1.1.0 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/nats-io/nkeys v0.4.11 // indirect
|
||||
github.com/nats-io/nuid v1.0.1 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||
github.com/pion/logging v0.2.4 // indirect
|
||||
github.com/pion/randutil v0.1.0 // indirect
|
||||
github.com/pion/rtcp v1.2.16 // indirect
|
||||
github.com/pion/rtp v1.8.26 // indirect
|
||||
github.com/pion/srtp/v3 v3.0.9 // indirect
|
||||
github.com/pion/transport/v3 v3.1.1 // indirect
|
||||
github.com/pion/transport/v4 v4.0.1 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 // indirect
|
||||
github.com/tetratelabs/wazero v1.9.0 // indirect
|
||||
github.com/zaf/g711 v1.4.0 // indirect
|
||||
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
|
||||
go.yaml.in/yaml/v2 v2.4.2 // indirect
|
||||
golang.org/x/crypto v0.42.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 // indirect
|
||||
golang.org/x/net v0.44.0 // indirect
|
||||
golang.org/x/sync v0.19.0 // indirect
|
||||
golang.org/x/sys v0.39.0 // indirect
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
gopkg.in/hraban/opus.v2 v2.0.0-20230925203106-0188a62cb302 // indirect
|
||||
modernc.org/libc v1.66.10 // indirect
|
||||
modernc.org/libquickjs v0.12.2 // indirect
|
||||
modernc.org/libc v1.67.1 // indirect
|
||||
modernc.org/libquickjs v0.12.3 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
)
|
||||
|
||||
70
go.sum
70
go.sum
@@ -4,16 +4,24 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/dylibso/observe-sdk/go v0.0.0-20240819160327-2d926c5d788a h1:UwSIFv5g5lIvbGgtf3tVwC7Ky9rmMFBp0RMs+6f6YqE=
|
||||
github.com/dylibso/observe-sdk/go v0.0.0-20240819160327-2d926c5d788a/go.mod h1:C8DzXehI4zAbrdlbtOByKX6pfivJTBiV9Jjqv56Yd9Q=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE=
|
||||
github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU=
|
||||
github.com/emiago/diago v0.23.1-0.20251211215055-e1d875617111 h1:jqhOZbH40pf2jiUhGaBNk334wOtNYvAaXg/mHOXhy/Y=
|
||||
github.com/emiago/diago v0.23.1-0.20251211215055-e1d875617111/go.mod h1:3vLCCq8/G/Ei5I64IHtrmBTag+nPLcgXcKeN1KkLtuc=
|
||||
github.com/emiago/sipgo v1.0.1-alpha.0.20251212165843-9c9bcdf9126f h1:n3wpx7ZyJBr0popXyATnTUZp/AdLIHjKbnKzr4ruF7g=
|
||||
github.com/emiago/sipgo v1.0.1-alpha.0.20251212165843-9c9bcdf9126f/go.mod h1:DuwAxBZhKMqIzQFPGZb1MVAGU6Wuxj64oTOhd5dx/FY=
|
||||
github.com/expr-lang/expr v1.17.6 h1:1h6i8ONk9cexhDmowO/A64VPxHScu7qfSl2k8OlINec=
|
||||
github.com/expr-lang/expr v1.17.6/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
|
||||
github.com/emiago/diago v0.26.2 h1:3QL03V0drX96eIBFBpfueNcywydRgYqffKihluGL0gA=
|
||||
github.com/emiago/diago v0.26.2/go.mod h1:jZ+7EnKcmgqKnLjCHPqfbP4Y/9Q/JLSLxMflDrp2J1M=
|
||||
github.com/emiago/dtls/v3 v3.0.0-20260122183559-8b8d23e359c0 h1:o4LxpUnZ1zxiQ+Qjc9kLwXcjz31NGAHmnZ7xoJto3VM=
|
||||
github.com/emiago/dtls/v3 v3.0.0-20260122183559-8b8d23e359c0/go.mod h1:ydcZ977eS1I6uOWodzMuw30BwvNAzT9su/xcNYSJqjA=
|
||||
github.com/emiago/sipgo v1.1.2 h1:JvLqEvqNSQm2mBX40qZ7O0WC3Ee67Z0UrfmBI7y6Beo=
|
||||
github.com/emiago/sipgo v1.1.2/go.mod h1:DuwAxBZhKMqIzQFPGZb1MVAGU6Wuxj64oTOhd5dx/FY=
|
||||
github.com/expr-lang/expr v1.17.7 h1:Q0xY/e/2aCIp8g9s/LGvMDCC5PxYlvHgDZRQ4y16JX8=
|
||||
github.com/expr-lang/expr v1.17.7/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4=
|
||||
github.com/extism/go-sdk v1.7.1 h1:lWJos6uY+tRFdlIHR+SJjwFDApY7OypS/2nMhiVQ9Sw=
|
||||
github.com/extism/go-sdk v1.7.1/go.mod h1:IT+Xdg5AZM9hVtpFUA+uZCJMge/hbvshl8bwzLtFyKA=
|
||||
github.com/go-audio/riff v1.0.0 h1:d8iCGbDvox9BfLagY94fBynxSPHO80LmZCaOsmKxokA=
|
||||
github.com/go-audio/riff v1.0.0/go.mod h1:l3cQwc85y79NQFCRB7TiPoNiaijp6q8Z0Uv38rVG498=
|
||||
github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y=
|
||||
github.com/gobwas/glob v0.2.3/go.mod h1:d3Ez4x06l9bZtSvzIay5+Yzi0fmZzPgnTbPcKjJAkT8=
|
||||
github.com/gobwas/httphead v0.1.0 h1:exrUm0f4YX0L7EBwZHuCF4GDp8aJfVeBrlLQrs6NqWU=
|
||||
github.com/gobwas/httphead v0.1.0/go.mod h1:O/RXo79gxV8G+RqlR/otEwx4Q36zl9rqC5u12GKvMCM=
|
||||
github.com/gobwas/pool v0.2.1 h1:xfeeEhW7pwmX8nuLVlqbzVc7udMDrwetjEv+TZIz1og=
|
||||
@@ -28,8 +36,12 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20240805132620-81f5be970eca h1:T54Ema1DU8ngI+aef9ZhAhNGQhcRTrWxVeG07F+c/Rw=
|
||||
github.com/ianlancetaylor/demangle v0.0.0-20240805132620-81f5be970eca/go.mod h1:gx7rwoVhcfuVKG5uya9Hs3Sxj7EIvldVofAWIUtGouw=
|
||||
github.com/icholy/digest v1.1.0 h1:HfGg9Irj7i+IX1o1QAmPfIBNu/Q5A5Tu3n/MED9k9H4=
|
||||
github.com/icholy/digest v1.1.0/go.mod h1:QNrsSGQ5v7v9cReDI0+eyjsXGUoRSUZQHeQ5C4XLa0Y=
|
||||
github.com/jwetzell/artnet-go v0.2.1 h1:iYTKWcwYrF5kBkYfkw2UbWvoueeA23iKEn7fR27mWZE=
|
||||
github.com/jwetzell/artnet-go v0.2.1/go.mod h1:gli97Z32a0kMkZ6taoTiK7/lqHcF/dhiGjGJdx/PxqA=
|
||||
github.com/jwetzell/free-d-go v0.1.0 h1:xHt6dvyit98X+OC3jVzV0aLidxbyzi3vI9QiYkteEtA=
|
||||
github.com/jwetzell/free-d-go v0.1.0/go.mod h1:KmrkooRARRaxJTBSPvwt/6IMAIaHH1R8bSA8cwbbELw=
|
||||
github.com/jwetzell/osc-go v0.1.0 h1:EXxup5VWBErHot2Ri4MFToPf6KCzLDTbCt2x6GLfw8I=
|
||||
@@ -44,14 +56,14 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/nats-io/nats.go v1.47.0 h1:YQdADw6J/UfGUd2Oy6tn4Hq6YHxCaJrVKayxxFqYrgM=
|
||||
github.com/nats-io/nats.go v1.47.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g=
|
||||
github.com/nats-io/nats.go v1.48.0 h1:pSFyXApG+yWU/TgbKCjmm5K4wrHu86231/w84qRVR+U=
|
||||
github.com/nats-io/nats.go v1.48.0/go.mod h1:iRWIPokVIFbVijxuMQq4y9ttaBTMe0SFdlZfMDd+33g=
|
||||
github.com/nats-io/nkeys v0.4.11 h1:q44qGV008kYd9W1b1nEBkNzvnWxtRSQ7A8BoqRrcfa0=
|
||||
github.com/nats-io/nkeys v0.4.11/go.mod h1:szDimtgmfOi9n25JpfIdGw12tZFYXqhGxjhVxsatHVE=
|
||||
github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw=
|
||||
github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/pion/logging v0.2.4 h1:tTew+7cmQ+Mc1pTBLKH2puKsOvhm32dROumOZ655zB8=
|
||||
github.com/pion/logging v0.2.4/go.mod h1:DffhXTKYdNZU+KtJ5pyQDjvOAh/GsNSyv1lbkFbe3so=
|
||||
github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA=
|
||||
@@ -64,6 +76,8 @@ github.com/pion/srtp/v3 v3.0.9 h1:lRGF4G61xxj+m/YluB3ZnBpiALSri2lTzba0kGZMrQY=
|
||||
github.com/pion/srtp/v3 v3.0.9/go.mod h1:E+AuWd7Ug2Fp5u38MKnhduvpVkveXJX6J4Lq4rxUYt8=
|
||||
github.com/pion/transport/v3 v3.1.1 h1:Tr684+fnnKlhPceU+ICdrw6KKkTms+5qHMgw6bIkYOM=
|
||||
github.com/pion/transport/v3 v3.1.1/go.mod h1:+c2eewC5WJQHiAA46fkMMzoYZSuGzA/7E2FPrOYHctQ=
|
||||
github.com/pion/transport/v4 v4.0.1 h1:sdROELU6BZ63Ab7FrOLn13M6YdJLY20wldXW2Cu2k8o=
|
||||
github.com/pion/transport/v4 v4.0.1/go.mod h1:nEuEA4AD5lPdcIegQDpVLgNoDGreqM/YqmEx3ovP4jM=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
@@ -72,24 +86,30 @@ github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR
|
||||
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/urfave/cli/v3 v3.6.1 h1:j8Qq8NyUawj/7rTYdBGrxcH7A/j7/G8Q5LhWEW4G3Mo=
|
||||
github.com/urfave/cli/v3 v3.6.1/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||
github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834 h1:ZF+QBjOI+tILZjBaFj3HgFonKXUcwgJ4djLb6i42S3Q=
|
||||
github.com/tetratelabs/wabin v0.0.0-20230304001439-f6f874872834/go.mod h1:m9ymHTgNSEjuxvw8E7WWe4Pl4hZQHXONY8wE6dMLaRk=
|
||||
github.com/tetratelabs/wazero v1.9.0 h1:IcZ56OuxrtaEz8UYNRHBrUa9bYeX9oVY93KspZZBf/I=
|
||||
github.com/tetratelabs/wazero v1.9.0/go.mod h1:TSbcXCfFP0L2FGkRPxHphadXPjo1T6W+CseNNY7EkjM=
|
||||
github.com/urfave/cli/v3 v3.6.2 h1:lQuqiPrZ1cIz8hz+HcrG0TNZFxU70dPZ3Yl+pSrH9A8=
|
||||
github.com/urfave/cli/v3 v3.6.2/go.mod h1:ysVLtOEmg2tOy6PknnYVhDoouyC/6N42TMeoMzskhso=
|
||||
github.com/zaf/g711 v1.4.0 h1:XZYkjjiAg9QTBnHqEg37m2I9q3IIDv5JRYXs2N8ma7c=
|
||||
github.com/zaf/g711 v1.4.0/go.mod h1:eCDXt3dSp/kYYAoooba7ukD/Q75jvAaS4WOMr0l1Roo=
|
||||
gitlab.com/gomidi/midi/v2 v2.3.18 h1:sj2fOhtvOe+zI8YJe8qTxLw5zv0ntULLUDwcFOaZQbI=
|
||||
gitlab.com/gomidi/midi/v2 v2.3.18/go.mod h1:jDpP4O4skYi+7iVwt6Zyp18bd2M4hkjtMuw2cmgKgfw=
|
||||
go.bug.st/serial v1.6.4 h1:7FmqNPgVp3pu2Jz5PoPtbZ9jJO5gnEnZIvnI1lzve8A=
|
||||
go.bug.st/serial v1.6.4/go.mod h1:nofMJxTeNVny/m6+KaafC6vJGj3miwQZ6vW4BZUGJPI=
|
||||
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
|
||||
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
|
||||
go.yaml.in/yaml/v2 v2.4.2 h1:DzmwEr2rDGHl7lsFgAHxmNz/1NlQ7xLIrlN2h5d1eGI=
|
||||
go.yaml.in/yaml/v2 v2.4.2/go.mod h1:081UH+NErpNdqlCXm3TtEran0rJZGxAYx9hb/ELlsPU=
|
||||
go.yaml.in/yaml/v3 v3.0.3 h1:bXOww4E/J3f66rav3pX3m8w6jDE4knZjGOw8b5Y6iNE=
|
||||
go.yaml.in/yaml/v3 v3.0.3/go.mod h1:tBHosrYAkRZjRAOREWbDnBXUf08JOwYq++0QNwQiWzI=
|
||||
golang.org/x/crypto v0.42.0 h1:chiH31gIWm57EkTXpwnqf8qeuMUi0yekh6mT2AvFlqI=
|
||||
golang.org/x/crypto v0.42.0/go.mod h1:4+rDnOTJhQCx2q7/j6rAN5XDw8kPjeaXEUR2eL94ix8=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
|
||||
golang.org/x/mod v0.29.0 h1:HV8lRxZC4l2cr3Zq1LvtOsi/ThTgWnUk/y64QSs8GwA=
|
||||
golang.org/x/mod v0.29.0/go.mod h1:NyhrlYXJ2H4eJiRy/WDBO6HMqZQ6q9nk4JzS3NuCK+w=
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546 h1:mgKeJMpvi0yx/sU5GsxQ7p6s2wtOnGAHZWCHUM4KGzY=
|
||||
golang.org/x/exp v0.0.0-20251023183803-a4bb9ffd2546/go.mod h1:j/pmGrbnkbPtQfxEe5D0VQhZC6qKbfKifgD0oM7sR70=
|
||||
golang.org/x/mod v0.30.0 h1:fDEXFVZ/fmCKProc/yAXXUijritrDzahmwwefnjoPFk=
|
||||
golang.org/x/mod v0.30.0/go.mod h1:lAsf5O2EvJeSFMiBxXDki7sCgAxEUcZHXoXMKT4GJKc=
|
||||
golang.org/x/net v0.44.0 h1:evd8IRDyfNBMBTTY5XRF1vaZlD+EmWx6x8PkhR04H/I=
|
||||
golang.org/x/net v0.44.0/go.mod h1:ECOoLqd5U3Lhyeyo/QDCEVQ4sNgYsqvCZ722XogGieY=
|
||||
golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4=
|
||||
@@ -97,8 +117,10 @@ golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
|
||||
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
|
||||
golang.org/x/tools v0.37.0 h1:DVSRzp7FwePZW356yEAChSdNcQo6Nsp+fex1SUW09lE=
|
||||
golang.org/x/tools v0.37.0/go.mod h1:MBN5QPQtLMHVdvsbtarmTNukZDdgwdwlO5qGacAzF0w=
|
||||
golang.org/x/tools v0.38.0 h1:Hx2Xv8hISq8Lm16jvBZ2VQf+RLmbd7wVUsALibYI/IQ=
|
||||
golang.org/x/tools v0.38.0/go.mod h1:yEsQ/d/YK8cjh0L6rZlY8tgtlKiBNTL14pGDJPJpYQs=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
|
||||
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
|
||||
@@ -120,18 +142,18 @@ modernc.org/gc/v3 v3.1.1 h1:k8T3gkXWY9sEiytKhcgyiZ2L0DTyCQ/nvX+LoCljoRE=
|
||||
modernc.org/gc/v3 v3.1.1/go.mod h1:HFK/6AGESC7Ex+EZJhJ2Gni6cTaYpSMmU/cT9RmlfYY=
|
||||
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||
modernc.org/libc v1.66.10 h1:yZkb3YeLx4oynyR+iUsXsybsX4Ubx7MQlSYEw4yj59A=
|
||||
modernc.org/libc v1.66.10/go.mod h1:8vGSEwvoUoltr4dlywvHqjtAqHBaw0j1jI7iFBTAr2I=
|
||||
modernc.org/libquickjs v0.12.2 h1:XtF6iD+aoN/pEz1MAQjg1wMZT412pzmOlO11UGLW/wQ=
|
||||
modernc.org/libquickjs v0.12.2/go.mod h1:n+vyuJ4mXh1pQYt1bJttvoCE+2t+MqCM8BFlcSDg/70=
|
||||
modernc.org/libc v1.67.1 h1:bFaqOaa5/zbWYJo8aW0tXPX21hXsngG2M7mckCnFSVk=
|
||||
modernc.org/libc v1.67.1/go.mod h1:QvvnnJ5P7aitu0ReNpVIEyesuhmDLQ8kaEoyMjIFZJA=
|
||||
modernc.org/libquickjs v0.12.3 h1:2IU9B6njBmce2PuYttJDkXeoLRV9WnvgP+eU5HAC8YI=
|
||||
modernc.org/libquickjs v0.12.3/go.mod h1:iCsgVxnHTX3i0YPxxHBmJk0GLA5sVUHXWI/090UXgeE=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/quickjs v0.17.0 h1:gYZRSCKVokyOiHhZEKwran/NoBaGjAaWk+xJq52SWfQ=
|
||||
modernc.org/quickjs v0.17.0/go.mod h1:TE+wpAYX4V7Nvi/sHRUrd9l5L176ZIiS0as3MMObvC0=
|
||||
modernc.org/quickjs v0.17.1 h1:CbYnbTf7ksZk9YZ1rRM2Ab1Zfi+X6s50kXiOhpd2NIg=
|
||||
modernc.org/quickjs v0.17.1/go.mod h1:hATT7DIJc33I5Q/Fjffhm0tpUHNSqdKHma/ossibTA0=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
package framer
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Framer interface {
|
||||
Decode([]byte) [][]byte
|
||||
Encode([]byte) []byte
|
||||
@@ -11,19 +7,19 @@ type Framer interface {
|
||||
Buffer() []byte
|
||||
}
|
||||
|
||||
func GetFramer(framingType string) (Framer, error) {
|
||||
func GetFramer(framingType string) Framer {
|
||||
switch framingType {
|
||||
case "CR":
|
||||
return NewByteSeparatorFramer([]byte{'\r'}), nil
|
||||
return NewByteSeparatorFramer([]byte{'\r'})
|
||||
case "LF":
|
||||
return NewByteSeparatorFramer([]byte{'\n'}), nil
|
||||
return NewByteSeparatorFramer([]byte{'\n'})
|
||||
case "CRLF":
|
||||
return NewByteSeparatorFramer([]byte{'\r', '\n'}), nil
|
||||
return NewByteSeparatorFramer([]byte{'\r', '\n'})
|
||||
case "SLIP":
|
||||
return NewSlipFramer(), nil
|
||||
return NewSlipFramer()
|
||||
case "RAW":
|
||||
return NewRawFramer(), nil
|
||||
return NewRawFramer()
|
||||
default:
|
||||
return nil, fmt.Errorf("unknown framing method: %s", framingType)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
15
internal/framer/framer_test.go
Normal file
15
internal/framer/framer_test.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package framer_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
func TestNilGetFramer(t *testing.T) {
|
||||
nilFramer := framer.GetFramer("asldfiudchuehrkbjbkjrbb")
|
||||
|
||||
if nilFramer != nil {
|
||||
t.Fatalf("Expected nil framer, got %v", nilFramer)
|
||||
}
|
||||
}
|
||||
78
internal/framer/raw_test.go
Normal file
78
internal/framer/raw_test.go
Normal file
@@ -0,0 +1,78 @@
|
||||
package framer_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
func TestGoodRawFramerDecode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
input []byte
|
||||
expected [][]byte
|
||||
}{
|
||||
{
|
||||
name: "basic raw framer",
|
||||
framer: framer.GetFramer("RAW"),
|
||||
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello\nWorld\nThis is a test\n"),
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frames := test.framer.Decode(test.input)
|
||||
if len(frames) != len(test.expected) {
|
||||
t.Fatalf("raw framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
}
|
||||
for i, frame := range frames {
|
||||
if !slices.Equal(frame, test.expected[i]) {
|
||||
t.Errorf("raw framer frame %d got %s, expected %s", i, frame, test.expected[i])
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodRawFramerEncode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
expected []byte
|
||||
input []byte
|
||||
}{
|
||||
{
|
||||
name: "basic raw framer",
|
||||
framer: framer.GetFramer("RAW"),
|
||||
expected: []byte("Hello\nWorld\nThis is a test\n"),
|
||||
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frame := test.framer.Encode(test.input)
|
||||
if !slices.Equal(frame, test.expected) {
|
||||
t.Fatalf("raw frame encode got %s, expected %s", frame, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRawFramerBuffer(t *testing.T) {
|
||||
framer := framer.GetFramer("RAW")
|
||||
framer.Decode([]byte("Hello, World!"))
|
||||
|
||||
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||
t.Fatalf("raw framer buffer got %s, expected empty", framer.Buffer())
|
||||
}
|
||||
framer.Clear()
|
||||
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||
t.Fatalf("raw framer buffer got %s, expected empty after clear", framer.Buffer())
|
||||
}
|
||||
}
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
func TestGoodSeparatorFramer(t *testing.T) {
|
||||
func TestGoodSeparatorFramerDecode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
@@ -17,7 +17,7 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "new line separator",
|
||||
framer: framer.NewByteSeparatorFramer([]byte{0x0a}),
|
||||
framer: framer.GetFramer("LF"),
|
||||
input: []byte("Hello\nWorld\nThis is a test\n"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello"),
|
||||
@@ -26,9 +26,20 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
},
|
||||
buffer: []byte{},
|
||||
},
|
||||
{
|
||||
name: "CR separator",
|
||||
framer: framer.GetFramer("CR"),
|
||||
input: []byte("Hello\rWorld\rThis is a test\r"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello"),
|
||||
[]byte("World"),
|
||||
[]byte("This is a test"),
|
||||
},
|
||||
buffer: []byte{},
|
||||
},
|
||||
{
|
||||
name: "CRLF separator",
|
||||
framer: framer.NewByteSeparatorFramer([]byte{0x0d, 0x0a}),
|
||||
framer: framer.GetFramer("CRLF"),
|
||||
input: []byte("Hello\r\nWorld\r\nThis is a test\r\n"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello"),
|
||||
@@ -39,7 +50,7 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
},
|
||||
{
|
||||
name: "extra data after separator",
|
||||
framer: framer.NewByteSeparatorFramer([]byte{0x0d, 0x0a}),
|
||||
framer: framer.GetFramer("CRLF"),
|
||||
input: []byte("Hello\r\nWorld\r\nThis is a test\r\nextra"),
|
||||
expected: [][]byte{
|
||||
[]byte("Hello"),
|
||||
@@ -54,7 +65,7 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frames := test.framer.Decode(test.input)
|
||||
if len(frames) != len(test.expected) {
|
||||
t.Errorf("separator framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
t.Fatalf("separator framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
}
|
||||
for i, frame := range frames {
|
||||
if !slices.Equal(frame, test.expected[i]) {
|
||||
@@ -62,8 +73,58 @@ func TestGoodSeparatorFramer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if !slices.Equal(test.framer.Buffer(), test.buffer) {
|
||||
t.Errorf("separator framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||
t.Fatalf("separator framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodSeparatorFramerEncode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
input []byte
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
name: "new line separator",
|
||||
framer: framer.GetFramer("LF"),
|
||||
input: []byte("Hello"),
|
||||
expected: []byte("Hello\n"),
|
||||
},
|
||||
{
|
||||
name: "CR separator",
|
||||
framer: framer.GetFramer("CR"),
|
||||
input: []byte("Hello"),
|
||||
expected: []byte("Hello\r"),
|
||||
},
|
||||
{
|
||||
name: "CRLF separator",
|
||||
framer: framer.GetFramer("CRLF"),
|
||||
input: []byte("Hello"),
|
||||
expected: []byte("Hello\r\n"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frame := test.framer.Encode(test.input)
|
||||
if !slices.Equal(frame, test.expected) {
|
||||
t.Fatalf("separator framer got %s, expected %s", frame, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeparatorFrameBuffer(t *testing.T) {
|
||||
framer := framer.GetFramer("LF")
|
||||
framer.Decode([]byte("Hello\nWorld\nThis is a test\nextra"))
|
||||
if !slices.Equal(framer.Buffer(), []byte("extra")) {
|
||||
t.Fatalf("separator framer buffer got %s, expected %s", framer.Buffer(), []byte("extra"))
|
||||
}
|
||||
|
||||
framer.Clear()
|
||||
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||
t.Fatalf("separator framer buffer got %s, expected empty slice", framer.Buffer())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,9 +59,9 @@ func (sf *SlipFramer) Encode(data []byte) []byte {
|
||||
for _, byteToEncode := range data {
|
||||
switch byteToEncode {
|
||||
case END:
|
||||
encodedBytes = append(encodedBytes, ESC_END)
|
||||
encodedBytes = append(encodedBytes, ESC, ESC_END)
|
||||
case ESC:
|
||||
encodedBytes = append(encodedBytes, ESC_ESC)
|
||||
encodedBytes = append(encodedBytes, ESC, ESC_ESC)
|
||||
default:
|
||||
encodedBytes = append(encodedBytes, byteToEncode)
|
||||
}
|
||||
|
||||
@@ -7,7 +7,7 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/framer"
|
||||
)
|
||||
|
||||
func TestGoodSLIPFramer(t *testing.T) {
|
||||
func TestGoodSLIPFramerDecode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
@@ -17,20 +17,34 @@ func TestGoodSLIPFramer(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "OSC SLIP messages",
|
||||
framer: framer.NewSlipFramer(),
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
input: []byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0},
|
||||
expected: [][]byte{
|
||||
{0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00},
|
||||
},
|
||||
buffer: []byte{},
|
||||
},
|
||||
{
|
||||
name: "SLIP decode escaped end",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
expected: [][]byte{{0xc0}},
|
||||
input: []byte{0xc0, 0xdb, 0xdc, 0xc0},
|
||||
buffer: []byte{},
|
||||
},
|
||||
{
|
||||
name: "SLIP decode escaped escape",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
expected: [][]byte{{0xdb}},
|
||||
input: []byte{0xc0, 0xdb, 0xdd, 0xc0},
|
||||
buffer: []byte{},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frames := test.framer.Decode(test.input)
|
||||
if len(frames) != len(test.expected) {
|
||||
t.Errorf("SLIP framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
t.Fatalf("SLIP framer got %d frames, expected %d", len(frames), len(test.expected))
|
||||
}
|
||||
for i, frame := range frames {
|
||||
if !slices.Equal(frame, test.expected[i]) {
|
||||
@@ -38,8 +52,59 @@ func TestGoodSLIPFramer(t *testing.T) {
|
||||
}
|
||||
}
|
||||
if !slices.Equal(test.framer.Buffer(), test.buffer) {
|
||||
t.Errorf("SLIP framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||
t.Fatalf("SLIP framer buffer got %s, expected %s", test.framer.Buffer(), test.buffer)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodSLIPFramerEncode(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
framer framer.Framer
|
||||
input []byte
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
name: "OSC SLIP messages",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
input: []byte{
|
||||
0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00,
|
||||
},
|
||||
expected: []byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0},
|
||||
},
|
||||
{
|
||||
name: "SLIP encode end",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
input: []byte{0xc0},
|
||||
expected: []byte{0xc0, 0xdb, 0xdc, 0xc0},
|
||||
},
|
||||
{
|
||||
name: "SLIP encode esc",
|
||||
framer: framer.GetFramer("SLIP"),
|
||||
input: []byte{0xdb},
|
||||
expected: []byte{0xc0, 0xdb, 0xdd, 0xc0},
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
frame := test.framer.Encode(test.input)
|
||||
if !slices.Equal(frame, test.expected) {
|
||||
t.Fatalf("SLIP framer frame got %s, expected %s", frame, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSlipFramerBuffer(t *testing.T) {
|
||||
framer := framer.GetFramer("SLIP")
|
||||
framer.Decode([]byte{0xc0, 0x2f, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x00, 0x00, 0x2c, 0x00, 0x00, 0x00, 0xc0, 0xc0, 0x45})
|
||||
if !slices.Equal(framer.Buffer(), []byte{0x45}) {
|
||||
t.Fatalf("SLIP framer buffer got %s, expected %s", framer.Buffer(), []byte{0x45})
|
||||
}
|
||||
framer.Clear()
|
||||
if !slices.Equal(framer.Buffer(), []byte{}) {
|
||||
t.Fatalf("SLIP framer buffer got %s, expected empty slice", framer.Buffer())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
@@ -16,14 +16,15 @@ type HTTPClient struct {
|
||||
ctx context.Context
|
||||
client *http.Client
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "http.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
|
||||
return &HTTPClient{config: config, ctx: ctx, router: router}, nil
|
||||
return &HTTPClient{config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -36,27 +37,34 @@ func (hc *HTTPClient) Type() string {
|
||||
return hc.config.Type
|
||||
}
|
||||
|
||||
func (hc *HTTPClient) Run() error {
|
||||
func (hc *HTTPClient) Run(ctx context.Context) error {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("http.client unable to get router from context")
|
||||
}
|
||||
hc.router = router
|
||||
hc.ctx = ctx
|
||||
|
||||
hc.client = &http.Client{
|
||||
Timeout: 10 * time.Second,
|
||||
}
|
||||
|
||||
<-hc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", hc.Id())
|
||||
hc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hc *HTTPClient) Output(payload any) error {
|
||||
func (hc *HTTPClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadRequest, ok := payload.(*http.Request)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("http.client is only able to output an http.Request")
|
||||
return errors.New("http.client is only able to output an http.Request")
|
||||
}
|
||||
|
||||
if hc.client == nil {
|
||||
return fmt.Errorf("http.client client is nil")
|
||||
return errors.New("http.client client is nil")
|
||||
}
|
||||
|
||||
response, err := hc.client.Do(payloadRequest)
|
||||
@@ -66,7 +74,7 @@ func (hc *HTTPClient) Output(payload any) error {
|
||||
}
|
||||
|
||||
if hc.router != nil {
|
||||
hc.router.HandleInput(hc.Id(), response)
|
||||
hc.router.HandleInput(hc.ctx, hc.Id(), response)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -3,11 +3,13 @@ package module
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
@@ -16,30 +18,56 @@ type HTTPServer struct {
|
||||
Port uint16
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type ResponseData struct {
|
||||
Message string `json:"message"`
|
||||
Status string `json:"status"`
|
||||
type ResponseIOError struct {
|
||||
Index int `json:"index"`
|
||||
OutputErrors []string `json:"outputErrors"`
|
||||
ProcessError *string `json:"processError"`
|
||||
InputError *string `json:"inputError"`
|
||||
}
|
||||
|
||||
type IOResponseData struct {
|
||||
IOErrors []ResponseIOError `json:"ioErrors"`
|
||||
Message string `json:"message"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
type httpServerContextKey string
|
||||
|
||||
type HTTPServerResponseWriter struct {
|
||||
http.ResponseWriter
|
||||
done bool
|
||||
}
|
||||
|
||||
func (hsrw *HTTPServerResponseWriter) WriteHeader(status int) {
|
||||
hsrw.done = true
|
||||
hsrw.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func (hsrw *HTTPServerResponseWriter) Write(data []byte) (int, error) {
|
||||
hsrw.done = true
|
||||
return hsrw.ResponseWriter.Write(data)
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "http.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.server requires a port parameter")
|
||||
return nil, errors.New("http.server requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.server port must be uint16")
|
||||
return nil, errors.New("http.server port must be uint16")
|
||||
}
|
||||
|
||||
return &HTTPServer{Port: uint16(portNum), config: config, ctx: ctx, router: router}, nil
|
||||
return &HTTPServer{Port: uint16(portNum), config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -52,58 +80,127 @@ func (hs *HTTPServer) Type() string {
|
||||
return hs.config.Type
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) HandleDefault(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
func (hs *HTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
responseWriter := HTTPServerResponseWriter{ResponseWriter: w}
|
||||
|
||||
response := ResponseData{
|
||||
response := IOResponseData{
|
||||
Message: "routing successful",
|
||||
Status: "ok",
|
||||
}
|
||||
|
||||
if hs.router != nil {
|
||||
routingErrors := hs.router.HandleInput(hs.Id(), r)
|
||||
if routingErrors != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
response.Status = "error"
|
||||
response.Message = "routing failed"
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response.Message = "routing successful"
|
||||
inputContext := context.WithValue(hs.ctx, httpServerContextKey("responseWriter"), &responseWriter)
|
||||
aRouteFound, routingErrors := hs.router.HandleInput(inputContext, hs.Id(), r)
|
||||
if !responseWriter.done {
|
||||
if aRouteFound {
|
||||
if routingErrors != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
response.Status = "error"
|
||||
response.Message = "routing failed"
|
||||
|
||||
response.IOErrors = []ResponseIOError{}
|
||||
for _, responseIOError := range routingErrors {
|
||||
errorToAdd := ResponseIOError{
|
||||
Index: responseIOError.Index,
|
||||
}
|
||||
|
||||
if responseIOError.InputError != nil {
|
||||
errorMsg := responseIOError.InputError.Error()
|
||||
errorToAdd.InputError = &errorMsg
|
||||
}
|
||||
|
||||
if responseIOError.ProcessError != nil {
|
||||
errorMsg := responseIOError.ProcessError.Error()
|
||||
errorToAdd.ProcessError = &errorMsg
|
||||
}
|
||||
|
||||
if responseIOError.OutputErrors != nil {
|
||||
outputErrorMsgs := []string{}
|
||||
|
||||
for _, outputError := range responseIOError.OutputErrors {
|
||||
outputErrorMsgs = append(outputErrorMsgs, outputError.Error())
|
||||
}
|
||||
|
||||
errorToAdd.OutputErrors = outputErrorMsgs
|
||||
}
|
||||
|
||||
response.IOErrors = append(response.IOErrors, errorToAdd)
|
||||
|
||||
}
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response.Message = "routing successful"
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusNotFound)
|
||||
response.Status = "error"
|
||||
response.Message = "no matching routes found"
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
}
|
||||
}
|
||||
} else {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
response.Message = "no router registered"
|
||||
response.Status = "error"
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
}
|
||||
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) Run() error {
|
||||
http.HandleFunc("/", hs.HandleDefault)
|
||||
func (hs *HTTPServer) Run(ctx context.Context) error {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("http.server unable to get router from context")
|
||||
}
|
||||
hs.router = router
|
||||
hs.ctx = ctx
|
||||
|
||||
httpServer := &http.Server{
|
||||
Addr: fmt.Sprintf(":%d", hs.Port),
|
||||
Handler: http.DefaultServeMux,
|
||||
Handler: hs,
|
||||
}
|
||||
|
||||
go func() {
|
||||
<-hs.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", hs.Id())
|
||||
httpServer.Close()
|
||||
}()
|
||||
|
||||
err := httpServer.ListenAndServe()
|
||||
slog.Debug("http.server closed", "id", hs.Id())
|
||||
// TODO(jwetzell): handle server closed error differently
|
||||
if err != nil {
|
||||
return err
|
||||
if err.Error() != "http: Server closed" {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
<-hs.ctx.Done()
|
||||
hs.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (hs *HTTPServer) Output(payload any) error {
|
||||
return fmt.Errorf("http.server output is not implemented")
|
||||
func (hs *HTTPServer) Output(ctx context.Context, payload any) error {
|
||||
responseWriter, ok := ctx.Value(httpServerContextKey("responseWriter")).(*HTTPServerResponseWriter)
|
||||
|
||||
if !ok {
|
||||
return errors.New("http.server output must originate from an http.server input")
|
||||
}
|
||||
|
||||
payloadResponse, ok := payload.(processor.HTTPResponse)
|
||||
|
||||
if !ok {
|
||||
return errors.New("http.server is only able to output HTTPResponse")
|
||||
}
|
||||
|
||||
if responseWriter.done {
|
||||
return errors.New("http.server response writer has already been written to")
|
||||
}
|
||||
|
||||
responseWriter.WriteHeader(payloadResponse.Status)
|
||||
responseWriter.Write(payloadResponse.Body)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,73 +0,0 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type Interval struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
ticker *time.Ticker
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "gen.interval",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("gen.interval requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("gen.interval duration must be number")
|
||||
}
|
||||
|
||||
return &Interval{Duration: uint32(durationNum), config: config, ctx: ctx, router: router}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (i *Interval) Id() string {
|
||||
return i.config.Id
|
||||
}
|
||||
|
||||
func (i *Interval) Type() string {
|
||||
return i.config.Type
|
||||
}
|
||||
|
||||
func (i *Interval) Run() error {
|
||||
ticker := time.NewTicker(time.Millisecond * time.Duration(i.Duration))
|
||||
i.ticker = ticker
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-i.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", i.Id())
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if i.router != nil {
|
||||
i.router.HandleInput(i.Id(), time.Now())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (i *Interval) Output(payload any) error {
|
||||
i.ticker.Reset(time.Millisecond * time.Duration(i.Duration))
|
||||
return nil
|
||||
}
|
||||
@@ -4,6 +4,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
@@ -19,49 +20,57 @@ type MIDIInput struct {
|
||||
router route.RouteIO
|
||||
Port string
|
||||
SendFunc func(midi.Message) error
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "midi.input",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.input requires a port parameter")
|
||||
return nil, errors.New("midi.input requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.input port must be a string")
|
||||
return nil, errors.New("midi.input port must be a string")
|
||||
}
|
||||
|
||||
return &MIDIInput{config: config, Port: portString, ctx: ctx, router: router}, nil
|
||||
return &MIDIInput{config: config, Port: portString, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (mc *MIDIInput) Id() string {
|
||||
return mc.config.Id
|
||||
func (mi *MIDIInput) Id() string {
|
||||
return mi.config.Id
|
||||
}
|
||||
|
||||
func (mc *MIDIInput) Type() string {
|
||||
return mc.config.Type
|
||||
func (mi *MIDIInput) Type() string {
|
||||
return mi.config.Type
|
||||
}
|
||||
|
||||
func (mc *MIDIInput) Run() error {
|
||||
func (mi *MIDIInput) Run(ctx context.Context) error {
|
||||
defer midi.CloseDriver()
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
in, err := midi.FindInPort(mc.Port)
|
||||
if !ok {
|
||||
return errors.New("midi.input unable to get router from context")
|
||||
}
|
||||
mi.router = router
|
||||
mi.ctx = ctx
|
||||
|
||||
in, err := midi.FindInPort(mi.Port)
|
||||
if err != nil {
|
||||
return fmt.Errorf("midi.input can't find input port: %s", mc.Port)
|
||||
return fmt.Errorf("midi.input can't find input port: %s", mi.Port)
|
||||
}
|
||||
|
||||
stop, err := midi.ListenTo(in, func(msg midi.Message, timestampms int32) {
|
||||
if mc.router != nil {
|
||||
mc.router.HandleInput(mc.Id(), msg)
|
||||
if mi.router != nil {
|
||||
mi.router.HandleInput(mi.ctx, mi.Id(), msg)
|
||||
}
|
||||
}, midi.UseSysEx())
|
||||
|
||||
@@ -71,11 +80,11 @@ func (mc *MIDIInput) Run() error {
|
||||
|
||||
defer stop()
|
||||
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
<-mi.ctx.Done()
|
||||
mi.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *MIDIInput) Output(payload any) error {
|
||||
return fmt.Errorf("midi.input output is not implemented")
|
||||
func (mi *MIDIInput) Output(ctx context.Context, payload any) error {
|
||||
return errors.New("midi.input output is not implemented")
|
||||
}
|
||||
|
||||
@@ -1,89 +0,0 @@
|
||||
//go:build cgo
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
|
||||
type MIDIOutput struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Port string
|
||||
SendFunc func(midi.Message) error
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
//TODO(jwetzell): find a better namespace than "misc"
|
||||
Type: "midi.output",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.output requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.output port must be a string")
|
||||
}
|
||||
|
||||
return &MIDIOutput{config: config, Port: portString, ctx: ctx, router: router}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (mc *MIDIOutput) Id() string {
|
||||
return mc.config.Id
|
||||
}
|
||||
|
||||
func (mc *MIDIOutput) Type() string {
|
||||
return mc.config.Type
|
||||
}
|
||||
|
||||
func (mc *MIDIOutput) Run() error {
|
||||
defer midi.CloseDriver()
|
||||
|
||||
out, err := midi.FindOutPort(mc.Port)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("midi.output can't find output port: %s", mc.Port)
|
||||
}
|
||||
|
||||
send, err := midi.SendTo(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mc.SendFunc = send
|
||||
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *MIDIOutput) Output(payload any) error {
|
||||
if mc.SendFunc == nil {
|
||||
return fmt.Errorf("midi.output output is not setup")
|
||||
}
|
||||
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("midi.output can only ouptut midi.Message")
|
||||
}
|
||||
|
||||
return mc.SendFunc(payloadMessage)
|
||||
}
|
||||
97
internal/module/midi-output.go
Normal file
97
internal/module/midi-output.go
Normal file
@@ -0,0 +1,97 @@
|
||||
//go:build cgo
|
||||
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||
)
|
||||
|
||||
type MIDIOutput struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Port string
|
||||
SendFunc func(midi.Message) error
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "midi.output",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.output requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("midi.output port must be a string")
|
||||
}
|
||||
|
||||
return &MIDIOutput{config: config, Port: portString, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Id() string {
|
||||
return mo.config.Id
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Type() string {
|
||||
return mo.config.Type
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Run(ctx context.Context) error {
|
||||
defer midi.CloseDriver()
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("midi.output unable to get router from context")
|
||||
}
|
||||
mo.router = router
|
||||
mo.ctx = ctx
|
||||
|
||||
out, err := midi.FindOutPort(mo.Port)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("midi.output can't find output port: %s", mo.Port)
|
||||
}
|
||||
|
||||
send, err := midi.SendTo(out)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
mo.SendFunc = send
|
||||
|
||||
<-mo.ctx.Done()
|
||||
mo.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mo *MIDIOutput) Output(ctx context.Context, payload any) error {
|
||||
if mo.SendFunc == nil {
|
||||
return errors.New("midi.output output is not setup")
|
||||
}
|
||||
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return errors.New("midi.output can only ouptut midi.Message")
|
||||
}
|
||||
|
||||
return mo.SendFunc(payloadMessage)
|
||||
}
|
||||
@@ -3,10 +3,10 @@ package module
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type ModuleError struct {
|
||||
@@ -18,13 +18,13 @@ type ModuleError struct {
|
||||
type Module interface {
|
||||
Id() string
|
||||
Type() string
|
||||
Run() error
|
||||
Output(any) error
|
||||
Run(context.Context) error
|
||||
Output(context.Context, any) error
|
||||
}
|
||||
|
||||
type ModuleRegistration struct {
|
||||
Type string `json:"type"`
|
||||
New func(context.Context, config.ModuleConfig, route.RouteIO) (Module, error)
|
||||
New func(config.ModuleConfig) (Module, error)
|
||||
}
|
||||
|
||||
func RegisterModule(mod ModuleRegistration) {
|
||||
@@ -49,3 +49,7 @@ var (
|
||||
moduleRegistryMu sync.RWMutex
|
||||
ModuleRegistry = make(map[string]ModuleRegistration)
|
||||
)
|
||||
|
||||
func CreateLogger(config config.ModuleConfig) *slog.Logger {
|
||||
return slog.Default().With("component", "module", "id", config.Id, "type", config.Type)
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
@@ -18,50 +18,51 @@ type MQTTClient struct {
|
||||
ClientID string
|
||||
Topic string
|
||||
client mqtt.Client
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "mqtt.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
broker, ok := params["broker"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client requires a broker parameter")
|
||||
return nil, errors.New("mqtt.client requires a broker parameter")
|
||||
}
|
||||
|
||||
brokerString, ok := broker.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client broker must be string")
|
||||
return nil, errors.New("mqtt.client broker must be string")
|
||||
}
|
||||
|
||||
topic, ok := params["topic"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client requires a topic parameter")
|
||||
return nil, errors.New("mqtt.client requires a topic parameter")
|
||||
}
|
||||
|
||||
topicString, ok := topic.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client topic must be string")
|
||||
return nil, errors.New("mqtt.client topic must be string")
|
||||
}
|
||||
|
||||
clientId, ok := params["clientId"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client requires a clientId parameter")
|
||||
return nil, errors.New("mqtt.client requires a clientId parameter")
|
||||
}
|
||||
|
||||
clientIdString, ok := clientId.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.client clientId must be string")
|
||||
return nil, errors.New("mqtt.client clientId must be string")
|
||||
}
|
||||
|
||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, ctx: ctx, router: router}, nil
|
||||
return &MQTTClient{config: config, Broker: brokerString, Topic: topicString, ClientID: clientIdString, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -74,7 +75,15 @@ func (mc *MQTTClient) Type() string {
|
||||
return mc.config.Type
|
||||
}
|
||||
|
||||
func (mc *MQTTClient) Run() error {
|
||||
func (mc *MQTTClient) Run(ctx context.Context) error {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("mqtt.client unable to get router from context")
|
||||
}
|
||||
mc.router = router
|
||||
mc.ctx = ctx
|
||||
|
||||
opts := mqtt.NewClientOptions()
|
||||
opts.AddBroker(mc.Broker)
|
||||
opts.SetClientID(mc.ClientID)
|
||||
@@ -83,7 +92,7 @@ func (mc *MQTTClient) Run() error {
|
||||
|
||||
opts.OnConnect = func(c mqtt.Client) {
|
||||
token := mc.client.Subscribe(mc.Topic, 1, func(c mqtt.Client, m mqtt.Message) {
|
||||
mc.router.HandleInput(mc.Id(), m)
|
||||
mc.router.HandleInput(mc.ctx, mc.Id(), m)
|
||||
})
|
||||
token.Wait()
|
||||
}
|
||||
@@ -99,25 +108,23 @@ func (mc *MQTTClient) Run() error {
|
||||
}
|
||||
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
mc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *MQTTClient) Output(payload any) error {
|
||||
func (mc *MQTTClient) Output(ctx context.Context, payload any) error {
|
||||
payloadMessage, ok := payload.(mqtt.Message)
|
||||
|
||||
fmt.Printf("payload type: %T\n", payload)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("mqtt.client is only able to output a MQTTMessage")
|
||||
return errors.New("mqtt.client is only able to output a MQTTMessage")
|
||||
}
|
||||
|
||||
if mc.client == nil {
|
||||
return fmt.Errorf("mqtt.client client is not setup")
|
||||
return errors.New("mqtt.client client is not setup")
|
||||
}
|
||||
|
||||
if !mc.client.IsConnected() {
|
||||
return fmt.Errorf("mqtt.client is not connected")
|
||||
return errors.New("mqtt.client is not connected")
|
||||
}
|
||||
|
||||
token := mc.client.Publish(payloadMessage.Topic(), payloadMessage.Qos(), payloadMessage.Retained(), payloadMessage.Payload())
|
||||
|
||||
@@ -2,7 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -18,38 +18,39 @@ type NATSClient struct {
|
||||
URL string
|
||||
Subject string
|
||||
client *nats.Conn
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "nats.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
url, ok := params["url"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.client requires a url parameter")
|
||||
return nil, errors.New("nats.client requires a url parameter")
|
||||
}
|
||||
|
||||
urlString, ok := url.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.client url must be string")
|
||||
return nil, errors.New("nats.client url must be string")
|
||||
}
|
||||
|
||||
subject, ok := params["subject"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.client requires a subject parameter")
|
||||
return nil, errors.New("nats.client requires a subject parameter")
|
||||
}
|
||||
|
||||
subjectString, ok := subject.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.client subject must be string")
|
||||
return nil, errors.New("nats.client subject must be string")
|
||||
}
|
||||
|
||||
return &NATSClient{config: config, URL: urlString, Subject: subjectString, ctx: ctx, router: router}, nil
|
||||
return &NATSClient{config: config, URL: urlString, Subject: subjectString, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -62,7 +63,16 @@ func (nc *NATSClient) Type() string {
|
||||
return nc.config.Type
|
||||
}
|
||||
|
||||
func (nc *NATSClient) Run() error {
|
||||
func (nc *NATSClient) Run(ctx context.Context) error {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("nats.client unable to get router from context")
|
||||
}
|
||||
|
||||
nc.router = router
|
||||
nc.ctx = ctx
|
||||
|
||||
client, err := nats.Connect(nc.URL, nats.RetryOnFailedConnect(true))
|
||||
|
||||
if err != nil {
|
||||
@@ -76,7 +86,7 @@ func (nc *NATSClient) Run() error {
|
||||
|
||||
sub, err := nc.client.Subscribe(nc.Subject, func(msg *nats.Msg) {
|
||||
if nc.router != nil {
|
||||
nc.router.HandleInput(nc.Id(), msg)
|
||||
nc.router.HandleInput(nc.ctx, nc.Id(), msg)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -87,24 +97,24 @@ func (nc *NATSClient) Run() error {
|
||||
defer sub.Unsubscribe()
|
||||
|
||||
<-nc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", nc.Id())
|
||||
nc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (nc *NATSClient) Output(payload any) error {
|
||||
func (nc *NATSClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadMessage, ok := payload.(processor.NATSMessage)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("nats.client is only able to output NATSMessage")
|
||||
return errors.New("nats.client is only able to output NATSMessage")
|
||||
}
|
||||
|
||||
if nc.client == nil {
|
||||
return fmt.Errorf("nats.client client is not setup")
|
||||
return errors.New("nats.client client is not setup")
|
||||
}
|
||||
|
||||
if !nc.client.IsConnected() {
|
||||
return fmt.Errorf("nats.client is not connected")
|
||||
return errors.New("nats.client is not connected")
|
||||
}
|
||||
|
||||
err := nc.client.Publish(payloadMessage.Subject, payloadMessage.Payload)
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -18,14 +19,15 @@ type PSNClient struct {
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
decoder *psn.Decoder
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "psn.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
|
||||
return &PSNClient{config: config, decoder: psn.NewDecoder(), ctx: ctx, router: router}, nil
|
||||
return &PSNClient{config: config, decoder: psn.NewDecoder(), logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -38,7 +40,14 @@ func (pc *PSNClient) Type() string {
|
||||
return pc.config.Type
|
||||
}
|
||||
|
||||
func (pc *PSNClient) Run() error {
|
||||
func (pc *PSNClient) Run(ctx context.Context) error {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("psn.client unable to get router from context")
|
||||
}
|
||||
pc.router = router
|
||||
pc.ctx = ctx
|
||||
|
||||
addr, err := net.ResolveUDPAddr("udp", "236.10.10.10:56565")
|
||||
if err != nil {
|
||||
@@ -58,7 +67,7 @@ func (pc *PSNClient) Run() error {
|
||||
select {
|
||||
case <-pc.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", pc.Id())
|
||||
pc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
pc.conn.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -76,21 +85,22 @@ func (pc *PSNClient) Run() error {
|
||||
message := buffer[:numBytes]
|
||||
err := pc.decoder.Decode(message)
|
||||
if err != nil {
|
||||
slog.Error("psn.client problem decoding psn traffic", "id", pc.Id(), "error", err)
|
||||
pc.logger.Error("problem decoding psn traffic", "error", err)
|
||||
}
|
||||
|
||||
if pc.router != nil {
|
||||
// TODO(jwetzell): better input handling
|
||||
for _, tracker := range pc.decoder.Trackers {
|
||||
pc.router.HandleInput(pc.Id(), tracker)
|
||||
pc.router.HandleInput(pc.ctx, pc.Id(), tracker)
|
||||
}
|
||||
} else {
|
||||
slog.Error("psn.client has no router", "id", pc.Id())
|
||||
pc.logger.Error("has no router")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (pc *PSNClient) Output(payload any) error {
|
||||
func (pc *PSNClient) Output(ctx context.Context, payload any) error {
|
||||
return fmt.Errorf("psn.client output is not implemented")
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
@@ -22,135 +23,142 @@ type SerialClient struct {
|
||||
Framer framer.Framer
|
||||
Mode *serial.Mode
|
||||
port serial.Port
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
//TODO(jwetzell): find a better namespace than "misc"
|
||||
Type: "serial.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client requires a port parameter")
|
||||
return nil, errors.New("serial.client requires a port parameter")
|
||||
}
|
||||
|
||||
portString, ok := port.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client port must be a string")
|
||||
return nil, errors.New("serial.client port must be a string")
|
||||
}
|
||||
|
||||
framingMethod := "RAW"
|
||||
framingMethod, ok := params["framing"]
|
||||
|
||||
framingMethodRaw, ok := params["framing"]
|
||||
|
||||
if ok {
|
||||
framingMethodString, ok := framingMethodRaw.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client framing method must be a string")
|
||||
}
|
||||
framingMethod = framingMethodString
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client requires a framing parameter")
|
||||
}
|
||||
|
||||
framer, err := framer.GetFramer(framingMethod)
|
||||
framingMethodString, ok := framingMethod.(string)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if !ok {
|
||||
return nil, errors.New("serial.client framing method must be a string")
|
||||
}
|
||||
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("serial.client unknown framing method: %s", framingMethod)
|
||||
}
|
||||
|
||||
buadRate, ok := params["baudRate"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client requires a baudRate parameter")
|
||||
return nil, errors.New("serial.client requires a baudRate parameter")
|
||||
}
|
||||
|
||||
baudRateNum, ok := buadRate.(float64)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("serial.client baudRate must be a number")
|
||||
return nil, errors.New("serial.client baudRate must be a number")
|
||||
}
|
||||
|
||||
mode := serial.Mode{
|
||||
BaudRate: int(baudRateNum),
|
||||
}
|
||||
|
||||
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, ctx: ctx, router: router}, nil
|
||||
return &SerialClient{config: config, Port: portString, Framer: framer, Mode: &mode, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (mc *SerialClient) Id() string {
|
||||
return mc.config.Id
|
||||
func (sc *SerialClient) Id() string {
|
||||
return sc.config.Id
|
||||
}
|
||||
|
||||
func (mc *SerialClient) Type() string {
|
||||
return mc.config.Type
|
||||
func (sc *SerialClient) Type() string {
|
||||
return sc.config.Type
|
||||
}
|
||||
|
||||
func (mc *SerialClient) SetupPort() error {
|
||||
func (sc *SerialClient) SetupPort() error {
|
||||
|
||||
port, err := serial.Open(mc.Port, mc.Mode)
|
||||
port, err := serial.Open(sc.Port, sc.Mode)
|
||||
if err != nil {
|
||||
return fmt.Errorf("serial.client can't open input port: %s", mc.Port)
|
||||
return fmt.Errorf("serial.client can't open input port: %s", sc.Port)
|
||||
}
|
||||
|
||||
mc.port = port
|
||||
sc.port = port
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (mc *SerialClient) Run() error {
|
||||
func (sc *SerialClient) Run(ctx context.Context) error {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("serial.client unable to get router from context")
|
||||
}
|
||||
|
||||
sc.router = router
|
||||
sc.ctx = ctx
|
||||
|
||||
// TODO(jwetzell): shutdown with router.Context properly
|
||||
go func() {
|
||||
<-mc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
if mc.port != nil {
|
||||
mc.port.Close()
|
||||
<-sc.ctx.Done()
|
||||
sc.logger.Debug("done")
|
||||
if sc.port != nil {
|
||||
sc.port.Close()
|
||||
}
|
||||
}()
|
||||
|
||||
for {
|
||||
err := mc.SetupPort()
|
||||
err := sc.SetupPort()
|
||||
if err != nil {
|
||||
if mc.ctx.Err() != nil {
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
if sc.ctx.Err() != nil {
|
||||
sc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
slog.Error("serial.client", "id", mc.Id(), "error", err.Error())
|
||||
sc.logger.Error("port setup error", "error", err.Error())
|
||||
time.Sleep(time.Second * 2)
|
||||
continue
|
||||
}
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
select {
|
||||
case <-mc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
case <-sc.ctx.Done():
|
||||
sc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
READ:
|
||||
for {
|
||||
select {
|
||||
case <-mc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", mc.Id())
|
||||
case <-sc.ctx.Done():
|
||||
sc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
byteCount, err := mc.port.Read(buffer)
|
||||
byteCount, err := sc.port.Read(buffer)
|
||||
|
||||
if err != nil {
|
||||
mc.Framer.Clear()
|
||||
sc.Framer.Clear()
|
||||
break READ
|
||||
}
|
||||
|
||||
if mc.Framer != nil {
|
||||
if sc.Framer != nil {
|
||||
if byteCount > 0 {
|
||||
messages := mc.Framer.Decode(buffer[0:byteCount])
|
||||
messages := sc.Framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if mc.router != nil {
|
||||
mc.router.HandleInput(mc.Id(), message)
|
||||
if sc.router != nil {
|
||||
sc.router.HandleInput(sc.ctx, sc.Id(), message)
|
||||
} else {
|
||||
slog.Error("serial.client has no router", "id", mc.Id())
|
||||
sc.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -161,14 +169,14 @@ func (mc *SerialClient) Run() error {
|
||||
}
|
||||
}
|
||||
|
||||
func (mc *SerialClient) Output(payload any) error {
|
||||
func (sc *SerialClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("serial.client can only ouptut bytes")
|
||||
return errors.New("serial.client can only ouptut bytes")
|
||||
}
|
||||
|
||||
_, err := mc.port.Write(mc.Framer.Encode(payloadBytes))
|
||||
_, err := sc.port.Write(sc.Framer.Encode(payloadBytes))
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/emiago/diago"
|
||||
@@ -12,6 +15,7 @@ import (
|
||||
"github.com/emiago/sipgo"
|
||||
"github.com/emiago/sipgo/sip"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
@@ -24,16 +28,24 @@ type SIPCallServer struct {
|
||||
Transport string
|
||||
UserAgent string
|
||||
dg *diago.Diago
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type SIPCallMessage struct {
|
||||
To string
|
||||
}
|
||||
|
||||
type SIPCall struct {
|
||||
inDialog *diago.DialogServerSession
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
type sipCallContextKey string
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.call.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
portNum := 5060
|
||||
|
||||
@@ -42,7 +54,7 @@ func init() {
|
||||
specificPortNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.call.server port must be a number")
|
||||
return nil, errors.New("sip.call.server port must be a number")
|
||||
}
|
||||
portNum = int(specificPortNum)
|
||||
}
|
||||
@@ -55,7 +67,7 @@ func init() {
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.call.server ip must be a string")
|
||||
return nil, errors.New("sip.call.server ip must be a string")
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
@@ -68,7 +80,7 @@ func init() {
|
||||
specificTransportString, ok := transport.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.call.server transport must be a string")
|
||||
return nil, errors.New("sip.call.server transport must be a string")
|
||||
}
|
||||
transportString = specificTransportString
|
||||
}
|
||||
@@ -81,28 +93,37 @@ func init() {
|
||||
specificTransportString, ok := userAgent.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.call.server userAgent must be a string")
|
||||
return nil, errors.New("sip.call.server userAgent must be a string")
|
||||
}
|
||||
userAgentString = specificTransportString
|
||||
}
|
||||
return &SIPCallServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString}, nil
|
||||
|
||||
return &SIPCallServer{config: config, IP: ipString, Port: int(portNum), Transport: transportString, UserAgent: userAgentString, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) Id() string {
|
||||
return sds.config.Id
|
||||
func (scs *SIPCallServer) Id() string {
|
||||
return scs.config.Id
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) Type() string {
|
||||
return sds.config.Type
|
||||
func (scs *SIPCallServer) Type() string {
|
||||
return scs.config.Type
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) Run() error {
|
||||
func (scs *SIPCallServer) Run(ctx context.Context) error {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("sip.call.server unable to get router from context")
|
||||
}
|
||||
scs.router = router
|
||||
scs.ctx = ctx
|
||||
|
||||
diagoLogger := slog.New(slog.NewJSONHandler(io.Discard, nil))
|
||||
|
||||
ua, _ := sipgo.NewUA(
|
||||
sipgo.WithUserAgent(sds.UserAgent),
|
||||
sipgo.WithUserAgent(scs.UserAgent),
|
||||
sipgo.WithUserAgentTransportLayerOptions(sip.WithTransportLayerLogger(diagoLogger)),
|
||||
sipgo.WithUserAgentTransactionLayerOptions(sip.WithTransactionLayerLogger(diagoLogger)),
|
||||
)
|
||||
@@ -112,74 +133,98 @@ func (sds *SIPCallServer) Run() error {
|
||||
media.SetDefaultLogger(diagoLogger)
|
||||
dg := diago.NewDiago(ua, diago.WithLogger(diagoLogger), diago.WithTransport(
|
||||
diago.Transport{
|
||||
Transport: sds.Transport,
|
||||
BindHost: sds.IP,
|
||||
BindPort: sds.Port,
|
||||
Transport: scs.Transport,
|
||||
BindHost: scs.IP,
|
||||
BindPort: scs.Port,
|
||||
},
|
||||
))
|
||||
|
||||
go func() {
|
||||
dg.Serve(sds.ctx, func(inDialog *diago.DialogServerSession) {
|
||||
sds.HandleCall(inDialog)
|
||||
dg.Serve(scs.ctx, func(inDialog *diago.DialogServerSession) {
|
||||
scs.HandleCall(inDialog)
|
||||
})
|
||||
}()
|
||||
|
||||
sds.dg = dg
|
||||
scs.dg = dg
|
||||
|
||||
<-sds.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", sds.Id())
|
||||
<-scs.ctx.Done()
|
||||
scs.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) HandleCall(inDialog *diago.DialogServerSession) {
|
||||
func (scs *SIPCallServer) HandleCall(inDialog *diago.DialogServerSession) {
|
||||
inDialog.Trying()
|
||||
inDialog.Ringing()
|
||||
inDialog.Answer()
|
||||
sds.router.HandleInput(sds.Id(), SIPCallMessage{
|
||||
|
||||
dialogContext := context.WithValue(scs.ctx, sipCallContextKey("call"), &SIPCall{
|
||||
inDialog: inDialog,
|
||||
})
|
||||
scs.router.HandleInput(dialogContext, scs.Id(), SIPCallMessage{
|
||||
To: inDialog.ToUser(),
|
||||
})
|
||||
<-inDialog.Context().Done()
|
||||
}
|
||||
|
||||
func (sds *SIPCallServer) Output(payload any) error {
|
||||
func (scs *SIPCallServer) Output(ctx context.Context, payload any) error {
|
||||
|
||||
call, ok := ctx.Value(sipCallContextKey("call")).(*SIPCall)
|
||||
|
||||
payloadMsg, ok := payload.(string)
|
||||
if !ok {
|
||||
return fmt.Errorf("sip.call.server output payload must be of type string")
|
||||
return errors.New("sip.call.server output must originate from sip.call.server input")
|
||||
}
|
||||
|
||||
if sds.dg == nil {
|
||||
return fmt.Errorf("sip.call.server diago is not initialized")
|
||||
gotLock := call.lock.TryLock()
|
||||
|
||||
if !gotLock {
|
||||
return errors.New("sip.call.server call is already locked")
|
||||
}
|
||||
|
||||
var uri sip.Uri
|
||||
err := sip.ParseUri(payloadMsg, &uri)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server output payload is not a valid SIP URI: %v", err)
|
||||
}
|
||||
outDialog, err := sds.dg.NewDialog(uri, diago.NewDialogOptions{
|
||||
Transport: sds.Transport,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server failed to create new dialog: %v", err)
|
||||
if call.inDialog.LoadState() == sip.DialogStateEnded {
|
||||
return errors.New("sip.call.server inDialog already ended")
|
||||
}
|
||||
|
||||
err = outDialog.Invite(sds.ctx, diago.InviteClientOptions{})
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server failed to send invite: %v", err)
|
||||
payloadDTMFResponse, ok := payload.(processor.SipDTMFResponse)
|
||||
|
||||
if ok {
|
||||
dtmfWriter := call.inDialog.AudioWriterDTMF()
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
for i, dtmfRune := range payloadDTMFResponse.Digits {
|
||||
err := dtmfWriter.WriteDTMF(dtmfRune)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.dtmf.server error output dtmf digit at index %d", i)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
return nil
|
||||
}
|
||||
|
||||
err = outDialog.Ack(sds.ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server failed to send ack: %v", err)
|
||||
payloadAudioFileResponse, ok := payload.(processor.SipAudioFileResponse)
|
||||
|
||||
if ok {
|
||||
audioFile, err := os.Open(payloadAudioFileResponse.AudioFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer audioFile.Close()
|
||||
|
||||
playback, err := call.inDialog.PlaybackCreate()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadAudioFileResponse.PreWait))
|
||||
|
||||
_, err = playback.Play(audioFile, "audio/wav")
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadAudioFileResponse.PostWait))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// TODO(jwetzell): make this configurable
|
||||
// NOTE(jwetzell): wait 5 seconds before hanging up the call
|
||||
time.Sleep(5 * time.Second)
|
||||
err = outDialog.Hangup(sds.ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.call.server failed to hangup call: %v", err)
|
||||
}
|
||||
return nil
|
||||
return errors.New("sip.dtmf.server can only output SipDTMFResponse or SipAudioFileResponse")
|
||||
}
|
||||
|
||||
@@ -2,10 +2,13 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/emiago/diago"
|
||||
@@ -13,6 +16,7 @@ import (
|
||||
"github.com/emiago/sipgo"
|
||||
"github.com/emiago/sipgo/sip"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
@@ -24,6 +28,7 @@ type SIPDTMFServer struct {
|
||||
Port int
|
||||
Transport string
|
||||
Separator string
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type SIPDTMFMessage struct {
|
||||
@@ -31,10 +36,15 @@ type SIPDTMFMessage struct {
|
||||
Digits string
|
||||
}
|
||||
|
||||
type SIPDTMFCall struct {
|
||||
inDialog *diago.DialogServerSession
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.dtmf.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
portNum := 5060
|
||||
|
||||
@@ -43,7 +53,7 @@ func init() {
|
||||
specificPortNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server port must be a number")
|
||||
return nil, errors.New("sip.dtmf.server port must be a number")
|
||||
}
|
||||
portNum = int(specificPortNum)
|
||||
}
|
||||
@@ -56,7 +66,7 @@ func init() {
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server ip must be a string")
|
||||
return nil, errors.New("sip.dtmf.server ip must be a string")
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
@@ -69,28 +79,28 @@ func init() {
|
||||
specificTransportString, ok := transport.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server transport must be a string")
|
||||
return nil, errors.New("sip.dtmf.server transport must be a string")
|
||||
}
|
||||
transportString = specificTransportString
|
||||
}
|
||||
|
||||
separator, ok := params["separator"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server requires a separator parameter")
|
||||
return nil, errors.New("sip.dtmf.server requires a separator parameter")
|
||||
}
|
||||
separatorString, ok := separator.(string)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("sip.dtmf.server separator must be a string")
|
||||
return nil, errors.New("sip.dtmf.server separator must be a string")
|
||||
}
|
||||
|
||||
if len(separatorString) != 1 {
|
||||
return nil, fmt.Errorf("sip.dtmf.server separator must be a single character")
|
||||
return nil, errors.New("sip.dtmf.server separator must be a single character")
|
||||
}
|
||||
|
||||
if !strings.ContainsRune("0123456789*#ABCD", rune(separatorString[0])) {
|
||||
return nil, fmt.Errorf("sip.dtmf.server separator must be a valid DTMF character")
|
||||
return nil, errors.New("sip.dtmf.server separator must be a valid DTMF character")
|
||||
}
|
||||
return &SIPDTMFServer{config: config, ctx: ctx, router: router, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString}, nil
|
||||
return &SIPDTMFServer{config: config, IP: ipString, Port: int(portNum), Transport: transportString, Separator: separatorString, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -103,7 +113,15 @@ func (sds *SIPDTMFServer) Type() string {
|
||||
return sds.config.Type
|
||||
}
|
||||
|
||||
func (sds *SIPDTMFServer) Run() error {
|
||||
func (sds *SIPDTMFServer) Run(ctx context.Context) error {
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("sip.dtmf.server unable to get router from context")
|
||||
}
|
||||
sds.router = router
|
||||
sds.ctx = ctx
|
||||
|
||||
diagoLogger := slog.New(slog.NewJSONHandler(io.Discard, nil))
|
||||
|
||||
ua, _ := sipgo.NewUA(
|
||||
@@ -131,7 +149,7 @@ func (sds *SIPDTMFServer) Run() error {
|
||||
}
|
||||
|
||||
<-sds.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", sds.Id())
|
||||
sds.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -142,10 +160,14 @@ func (sds *SIPDTMFServer) HandleCall(inDialog *diago.DialogServerSession) error
|
||||
|
||||
reader := inDialog.AudioReaderDTMF()
|
||||
userString := ""
|
||||
|
||||
return reader.Listen(func(dtmf rune) error {
|
||||
if dtmf == rune(sds.Separator[0]) {
|
||||
if sds.router != nil {
|
||||
sds.router.HandleInput(sds.Id(), SIPDTMFMessage{
|
||||
dialogContext := context.WithValue(sds.ctx, sipCallContextKey("call"), &SIPDTMFCall{
|
||||
inDialog: inDialog,
|
||||
})
|
||||
sds.router.HandleInput(dialogContext, sds.Id(), SIPDTMFMessage{
|
||||
To: inDialog.ToUser(),
|
||||
Digits: userString,
|
||||
})
|
||||
@@ -158,6 +180,66 @@ func (sds *SIPDTMFServer) HandleCall(inDialog *diago.DialogServerSession) error
|
||||
}, 5*time.Second)
|
||||
}
|
||||
|
||||
func (sds *SIPDTMFServer) Output(payload any) error {
|
||||
return fmt.Errorf("sip.dtmf.server output is not implemented")
|
||||
func (sds *SIPDTMFServer) Output(ctx context.Context, payload any) error {
|
||||
call, ok := ctx.Value(sipCallContextKey("call")).(*SIPDTMFCall)
|
||||
|
||||
if !ok {
|
||||
return errors.New("sip.dtmf.server output must originate from sip.dtmf.server input")
|
||||
}
|
||||
|
||||
gotLock := call.lock.TryLock()
|
||||
|
||||
if !gotLock {
|
||||
return errors.New("sip.dtmf.server call is already locked")
|
||||
}
|
||||
|
||||
if call.inDialog.LoadState() == sip.DialogStateEnded {
|
||||
return errors.New("sip.dtmf.server inDialog already ended")
|
||||
}
|
||||
|
||||
payloadDTMFResponse, ok := payload.(processor.SipDTMFResponse)
|
||||
|
||||
if ok {
|
||||
dtmfWriter := call.inDialog.AudioWriterDTMF()
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
for i, dtmfRune := range payloadDTMFResponse.Digits {
|
||||
err := dtmfWriter.WriteDTMF(dtmfRune)
|
||||
|
||||
if err != nil {
|
||||
return fmt.Errorf("sip.dtmf.server error output dtmf digit at index %d", i)
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadDTMFResponse.PreWait))
|
||||
return nil
|
||||
}
|
||||
|
||||
payloadAudioFileResponse, ok := payload.(processor.SipAudioFileResponse)
|
||||
|
||||
if ok {
|
||||
audioFile, err := os.Open(payloadAudioFileResponse.AudioFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer audioFile.Close()
|
||||
|
||||
playback, err := call.inDialog.PlaybackCreate()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadAudioFileResponse.PreWait))
|
||||
|
||||
_, err = playback.Play(audioFile, "audio/wav")
|
||||
|
||||
time.Sleep(time.Millisecond * time.Duration(payloadAudioFileResponse.PostWait))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
return errors.New("sip.dtmf.server can only output SipDTMFResponse or SipAudioFileResponse")
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -19,34 +20,35 @@ type TCPClient struct {
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Addr *net.TCPAddr
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
host, ok := params["host"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.client requires a host parameter")
|
||||
return nil, errors.New("net.tcp.client requires a host parameter")
|
||||
}
|
||||
|
||||
hostString, ok := host.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.client host must be string")
|
||||
return nil, errors.New("net.tcp.client host must be string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.client requires a port parameter")
|
||||
return nil, errors.New("net.tcp.client requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.client port must be a number")
|
||||
return nil, errors.New("net.tcp.client port must be a number")
|
||||
}
|
||||
|
||||
addr, err := net.ResolveTCPAddr("tcp", fmt.Sprintf("%s:%d", hostString, uint16(portNum)))
|
||||
@@ -54,26 +56,24 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
framingMethod := "RAW"
|
||||
framingMethod, ok := params["framing"]
|
||||
|
||||
framingMethodRaw, ok := params["framing"]
|
||||
|
||||
if ok {
|
||||
framingMethodString, ok := framingMethodRaw.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
||||
}
|
||||
framingMethod = framingMethodString
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client requires a framing parameter")
|
||||
}
|
||||
|
||||
framer, err := framer.GetFramer(framingMethod)
|
||||
framingMethodString, ok := framingMethod.(string)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.client framing method must be a string")
|
||||
}
|
||||
|
||||
return &TCPClient{framer: framer, Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("net.tcp.client unknown framing method: %s", framingMethod)
|
||||
}
|
||||
return &TCPClient{framer: framer, Addr: addr, config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -86,12 +86,20 @@ func (tc *TCPClient) Type() string {
|
||||
return tc.config.Type
|
||||
}
|
||||
|
||||
func (tc *TCPClient) Run() error {
|
||||
func (tc *TCPClient) Run(ctx context.Context) error {
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("net.tcp.client unable to get router from context")
|
||||
}
|
||||
tc.router = router
|
||||
tc.ctx = ctx
|
||||
|
||||
// TODO(jwetzell): shutdown with router.Context properly
|
||||
go func() {
|
||||
<-tc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
tc.logger.Debug("done")
|
||||
if tc.conn != nil {
|
||||
tc.conn.Close()
|
||||
}
|
||||
@@ -101,10 +109,10 @@ func (tc *TCPClient) Run() error {
|
||||
err := tc.SetupConn()
|
||||
if err != nil {
|
||||
if tc.ctx.Err() != nil {
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
tc.logger.Debug("done")
|
||||
return nil
|
||||
}
|
||||
slog.Error("net.tcp.client", "id", tc.Id(), "error", err.Error())
|
||||
tc.logger.Error("connection error", "error", err.Error())
|
||||
time.Sleep(time.Second * 2)
|
||||
continue
|
||||
}
|
||||
@@ -112,14 +120,14 @@ func (tc *TCPClient) Run() error {
|
||||
buffer := make([]byte, 1024)
|
||||
select {
|
||||
case <-tc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
tc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
READ:
|
||||
for {
|
||||
select {
|
||||
case <-tc.ctx.Done():
|
||||
slog.Debug("router context done in module", "id", tc.Id())
|
||||
tc.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
byteCount, err := tc.conn.Read(buffer)
|
||||
@@ -134,9 +142,9 @@ func (tc *TCPClient) Run() error {
|
||||
messages := tc.framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if tc.router != nil {
|
||||
tc.router.HandleInput(tc.Id(), message)
|
||||
tc.router.HandleInput(tc.ctx, tc.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.tcp.client has no router", "id", tc.Id())
|
||||
tc.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -153,7 +161,7 @@ func (tc *TCPClient) SetupConn() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (tc *TCPClient) Output(payload any) error {
|
||||
func (tc *TCPClient) Output(ctx context.Context, payload any) error {
|
||||
// NOTE(jwetzell): not sure how this would occur but
|
||||
if tc.conn == nil {
|
||||
err := tc.SetupConn()
|
||||
@@ -163,7 +171,7 @@ func (tc *TCPClient) Output(payload any) error {
|
||||
}
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("net.tcp.client is only able to output bytes")
|
||||
return errors.New("net.tcp.client is only able to output bytes")
|
||||
}
|
||||
_, err := tc.conn.Write(tc.framer.Encode(payloadBytes))
|
||||
return err
|
||||
|
||||
@@ -26,41 +26,41 @@ type TCPServer struct {
|
||||
wg sync.WaitGroup
|
||||
connections []*net.TCPConn
|
||||
connectionsMu sync.RWMutex
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.server requires a port parameter")
|
||||
return nil, errors.New("net.tcp.server requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.server port must be a number")
|
||||
return nil, errors.New("net.tcp.server port must be a number")
|
||||
}
|
||||
|
||||
framingMethod := "RAW"
|
||||
framingMethod, ok := params["framing"]
|
||||
|
||||
framingMethodRaw, ok := params["framing"]
|
||||
|
||||
if ok {
|
||||
framingMethodString, ok := framingMethodRaw.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("misc.serial.client framing method must be a string")
|
||||
}
|
||||
framingMethod = framingMethodString
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.server requires a framing parameter")
|
||||
}
|
||||
|
||||
framer, err := framer.GetFramer(framingMethod)
|
||||
framingMethodString, ok := framingMethod.(string)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if !ok {
|
||||
return nil, errors.New("net.tcp.server framing method must be a string")
|
||||
}
|
||||
|
||||
framer := framer.GetFramer(framingMethodString)
|
||||
|
||||
if framer == nil {
|
||||
return nil, fmt.Errorf("net.tcp.server unknown framing method: %s", framingMethod)
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
@@ -71,7 +71,7 @@ func init() {
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.tcp.server ip must be a string")
|
||||
return nil, errors.New("net.tcp.server ip must be a string")
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
@@ -80,8 +80,7 @@ func init() {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), ctx: ctx, router: router}, nil
|
||||
return &TCPServer{Framer: framer, Addr: addr, config: config, quit: make(chan interface{}), logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -98,7 +97,7 @@ func (ts *TCPServer) handleClient(client *net.TCPConn) {
|
||||
ts.connectionsMu.Lock()
|
||||
ts.connections = append(ts.connections, client)
|
||||
ts.connectionsMu.Unlock()
|
||||
slog.Debug("net.tcp.server connection accepted", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
ts.logger.Debug("net.tcp.server connection accepted", "remoteAddr", client.RemoteAddr().String())
|
||||
defer client.Close()
|
||||
|
||||
buffer := make([]byte, 1024)
|
||||
@@ -125,7 +124,7 @@ ClientRead:
|
||||
break
|
||||
}
|
||||
}
|
||||
slog.Debug("net.tcp.server connection reset", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
ts.logger.Debug("connection reset", "remoteAddr", client.RemoteAddr().String())
|
||||
ts.connectionsMu.Unlock()
|
||||
}
|
||||
}
|
||||
@@ -138,7 +137,7 @@ ClientRead:
|
||||
break
|
||||
}
|
||||
}
|
||||
slog.Debug("net.tcp.server stream ended", "id", ts.Id(), "remoteAddr", client.RemoteAddr().String())
|
||||
ts.logger.Debug("stream ended", "remoteAddr", client.RemoteAddr().String())
|
||||
ts.connectionsMu.Unlock()
|
||||
}
|
||||
return
|
||||
@@ -148,9 +147,9 @@ ClientRead:
|
||||
messages := ts.Framer.Decode(buffer[0:byteCount])
|
||||
for _, message := range messages {
|
||||
if ts.router != nil {
|
||||
ts.router.HandleInput(ts.Id(), message)
|
||||
ts.router.HandleInput(ts.ctx, ts.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.tcp.server has no router", "id", ts.Id())
|
||||
ts.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -159,7 +158,16 @@ ClientRead:
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *TCPServer) Run() error {
|
||||
func (ts *TCPServer) Run(ctx context.Context) error {
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("net.tcp.server unable to get router from context")
|
||||
}
|
||||
ts.router = router
|
||||
ts.ctx = ctx
|
||||
|
||||
listener, err := net.ListenTCP("tcp", ts.Addr)
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -170,7 +178,7 @@ func (ts *TCPServer) Run() error {
|
||||
<-ts.ctx.Done()
|
||||
close(ts.quit)
|
||||
listener.Close()
|
||||
slog.Debug("router context done in module", "id", ts.Id())
|
||||
ts.logger.Debug("done")
|
||||
}()
|
||||
|
||||
AcceptLoop:
|
||||
@@ -181,7 +189,7 @@ AcceptLoop:
|
||||
case <-ts.quit:
|
||||
break AcceptLoop
|
||||
default:
|
||||
slog.Debug("net.tcp.server problem with listener", "error", err)
|
||||
ts.logger.Debug("problem with listener", "error", err)
|
||||
}
|
||||
} else {
|
||||
ts.wg.Add(1)
|
||||
@@ -196,11 +204,11 @@ AcceptLoop:
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ts *TCPServer) Output(payload any) error {
|
||||
func (ts *TCPServer) Output(ctx context.Context, payload any) error {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return fmt.Errorf("net.tcp.server is only able to output bytes")
|
||||
return errors.New("net.tcp.server is only able to output bytes")
|
||||
}
|
||||
ts.connectionsMu.Lock()
|
||||
errorString := ""
|
||||
@@ -216,5 +224,5 @@ func (ts *TCPServer) Output(payload any) error {
|
||||
if errorString == "" {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("%s", errorString)
|
||||
return fmt.Errorf("net.tcp.server error during output: %s", errorString)
|
||||
}
|
||||
|
||||
82
internal/module/time-interval.go
Normal file
82
internal/module/time-interval.go
Normal file
@@ -0,0 +1,82 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type TimeInterval struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
ticker *time.Ticker
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "time.interval",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, errors.New("time.interval requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("time.interval duration must be number")
|
||||
}
|
||||
return &TimeInterval{Duration: uint32(durationNum), config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Id() string {
|
||||
return i.config.Id
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Type() string {
|
||||
return i.config.Type
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Run(ctx context.Context) error {
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("time.interval unable to get router from context")
|
||||
}
|
||||
i.router = router
|
||||
i.ctx = ctx
|
||||
|
||||
ticker := time.NewTicker(time.Millisecond * time.Duration(i.Duration))
|
||||
i.ticker = ticker
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-i.ctx.Done():
|
||||
i.logger.Debug("done")
|
||||
return nil
|
||||
case <-ticker.C:
|
||||
if i.router != nil {
|
||||
i.router.HandleInput(i.ctx, i.Id(), time.Now())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (i *TimeInterval) Output(ctx context.Context, payload any) error {
|
||||
i.ticker.Reset(time.Millisecond * time.Duration(i.Duration))
|
||||
return nil
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
@@ -10,62 +10,72 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type Timer struct {
|
||||
type TimeTimer struct {
|
||||
config config.ModuleConfig
|
||||
Duration uint32
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
timer *time.Timer
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "gen.timer",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
Type: "time.timer",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("gen.timer requires a duration parameter")
|
||||
return nil, errors.New("time.timer requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("gen.timer duration must be a number")
|
||||
return nil, errors.New("time.timer duration must be a number")
|
||||
}
|
||||
|
||||
return &Timer{Duration: uint32(durationNum), config: config, ctx: ctx, router: router}, nil
|
||||
return &TimeTimer{Duration: uint32(durationNum), config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (t *Timer) Id() string {
|
||||
func (t *TimeTimer) Id() string {
|
||||
return t.config.Id
|
||||
}
|
||||
|
||||
func (t *Timer) Type() string {
|
||||
func (t *TimeTimer) Type() string {
|
||||
return t.config.Type
|
||||
}
|
||||
|
||||
func (t *Timer) Run() error {
|
||||
func (t *TimeTimer) Run(ctx context.Context) error {
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("net.tcp.client unable to get router from context")
|
||||
}
|
||||
t.router = router
|
||||
t.ctx = ctx
|
||||
|
||||
t.timer = time.NewTimer(time.Millisecond * time.Duration(t.Duration))
|
||||
defer t.timer.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-t.ctx.Done():
|
||||
t.timer.Stop()
|
||||
slog.Debug("router context done in module", "id", t.Id())
|
||||
t.logger.Debug("done")
|
||||
return nil
|
||||
case time := <-t.timer.C:
|
||||
if t.router != nil {
|
||||
t.router.HandleInput(t.Id(), time)
|
||||
t.router.HandleInput(t.ctx, t.Id(), time)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (t *Timer) Output(payload any) error {
|
||||
func (t *TimeTimer) Output(ctx context.Context, payload any) error {
|
||||
t.timer.Reset(time.Millisecond * time.Duration(t.Duration))
|
||||
return nil
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -17,42 +18,42 @@ type UDPClient struct {
|
||||
conn *net.UDPConn
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.client",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
host, ok := params["host"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client requires a host parameter")
|
||||
return nil, errors.New("net.udp.client requires a host parameter")
|
||||
}
|
||||
|
||||
hostString, ok := host.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client host must be a string")
|
||||
return nil, errors.New("net.udp.client host must be a string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client requires a port parameter")
|
||||
return nil, errors.New("net.udp.client requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client port must be a number")
|
||||
return nil, errors.New("net.udp.client port must be a number")
|
||||
}
|
||||
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", hostString, uint16(portNum)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &UDPClient{Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||
return &UDPClient{Addr: addr, config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -71,7 +72,15 @@ func (uc *UDPClient) SetupConn() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (uc *UDPClient) Run() error {
|
||||
func (uc *UDPClient) Run(ctx context.Context) error {
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("net.udp.client unable to get router from context")
|
||||
}
|
||||
uc.router = router
|
||||
uc.ctx = ctx
|
||||
|
||||
err := uc.SetupConn()
|
||||
if err != nil {
|
||||
@@ -79,18 +88,18 @@ func (uc *UDPClient) Run() error {
|
||||
}
|
||||
|
||||
<-uc.ctx.Done()
|
||||
slog.Debug("router context done in module", "id", uc.Id())
|
||||
uc.logger.Debug("done")
|
||||
if uc.conn != nil {
|
||||
uc.conn.Close()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (uc *UDPClient) Output(payload any) error {
|
||||
func (uc *UDPClient) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("net.udp.client is only able to output bytes")
|
||||
return errors.New("net.udp.client is only able to output bytes")
|
||||
}
|
||||
if uc.conn != nil {
|
||||
_, err := uc.conn.Write(payloadBytes)
|
||||
@@ -99,7 +108,7 @@ func (uc *UDPClient) Output(payload any) error {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return fmt.Errorf("net.udp.client client is not setup")
|
||||
return errors.New("net.udp.client client is not setup")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
@@ -17,41 +18,42 @@ type UDPMulticast struct {
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Addr *net.UDPAddr
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.multicast",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
ip, ok := params["ip"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client requires am ip parameter")
|
||||
return nil, errors.New("net.udp.multicast requires an ip parameter")
|
||||
}
|
||||
|
||||
ipString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client ip must be a string")
|
||||
return nil, errors.New("net.udp.multicast ip must be a string")
|
||||
}
|
||||
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client requires a port parameter")
|
||||
return nil, errors.New("net.udp.multicast requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.client port must be a number")
|
||||
return nil, errors.New("net.udp.multicast port must be a number")
|
||||
}
|
||||
|
||||
addr, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", ipString, uint16(portNum)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &UDPMulticast{config: config, Addr: addr, ctx: ctx, router: router}, nil
|
||||
return &UDPMulticast{config: config, Addr: addr, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -64,7 +66,15 @@ func (um *UDPMulticast) Type() string {
|
||||
return um.config.Type
|
||||
}
|
||||
|
||||
func (um *UDPMulticast) Run() error {
|
||||
func (um *UDPMulticast) Run(ctx context.Context) error {
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("net.udp.multicast unable to get router from context")
|
||||
}
|
||||
um.router = router
|
||||
um.ctx = ctx
|
||||
|
||||
client, err := net.ListenMulticastUDP("udp", nil, um.Addr)
|
||||
if err != nil {
|
||||
@@ -79,7 +89,7 @@ func (um *UDPMulticast) Run() error {
|
||||
select {
|
||||
case <-um.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", um.Id())
|
||||
um.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
um.conn.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -97,24 +107,24 @@ func (um *UDPMulticast) Run() error {
|
||||
message := buffer[:numBytes]
|
||||
|
||||
if um.router != nil {
|
||||
um.router.HandleInput(um.Id(), message)
|
||||
um.router.HandleInput(um.ctx, um.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.udp.multicast has no router", "id", um.Id())
|
||||
um.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (um *UDPMulticast) Output(payload any) error {
|
||||
func (um *UDPMulticast) Output(ctx context.Context, payload any) error {
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
if !ok {
|
||||
return fmt.Errorf("net.udp.multicast can only output bytes")
|
||||
return errors.New("net.udp.multicast can only output bytes")
|
||||
}
|
||||
|
||||
if um.conn == nil {
|
||||
return fmt.Errorf("net.udp.multicast connection is not setup")
|
||||
return errors.New("net.udp.multicast connection is not setup")
|
||||
}
|
||||
|
||||
_, err := um.conn.Write(payloadBytes)
|
||||
|
||||
@@ -2,6 +2,7 @@ package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
@@ -13,26 +14,28 @@ import (
|
||||
)
|
||||
|
||||
type UDPServer struct {
|
||||
Addr *net.UDPAddr
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
Addr *net.UDPAddr
|
||||
BufferSize int
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.server",
|
||||
New: func(ctx context.Context, config config.ModuleConfig, router route.RouteIO) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
params := config.Params
|
||||
port, ok := params["port"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.server requires a port parameter")
|
||||
return nil, errors.New("net.udp.server requires a port parameter")
|
||||
}
|
||||
|
||||
portNum, ok := port.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.server port must be a number")
|
||||
return nil, errors.New("net.udp.server port must be a number")
|
||||
}
|
||||
|
||||
ipString := "0.0.0.0"
|
||||
@@ -43,7 +46,7 @@ func init() {
|
||||
specificIpString, ok := ip.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("net.udp.server ip must be a string")
|
||||
return nil, errors.New("net.udp.server ip must be a string")
|
||||
}
|
||||
ipString = specificIpString
|
||||
}
|
||||
@@ -53,7 +56,18 @@ func init() {
|
||||
log.Fatalf("error resolving UDP address: %v", err)
|
||||
}
|
||||
|
||||
return &UDPServer{Addr: addr, config: config, ctx: ctx, router: router}, nil
|
||||
bufferSizeNum := 2048
|
||||
bufferSize, ok := params["bufferSize"]
|
||||
|
||||
if ok {
|
||||
bufferSizeFloat, ok := bufferSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("net.udp.server bufferSize must be a number")
|
||||
}
|
||||
bufferSizeNum = int(bufferSizeFloat)
|
||||
}
|
||||
return &UDPServer{Addr: addr, BufferSize: bufferSizeNum, config: config, logger: CreateLogger(config)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -66,7 +80,15 @@ func (us *UDPServer) Type() string {
|
||||
return us.config.Id
|
||||
}
|
||||
|
||||
func (us *UDPServer) Run() error {
|
||||
func (us *UDPServer) Run(ctx context.Context) error {
|
||||
|
||||
router, ok := ctx.Value(route.RouterContextKey).(route.RouteIO)
|
||||
|
||||
if !ok {
|
||||
return errors.New("net.udp.server unable to get router from context")
|
||||
}
|
||||
us.router = router
|
||||
us.ctx = ctx
|
||||
|
||||
listener, err := net.ListenUDP("udp", us.Addr)
|
||||
if err != nil {
|
||||
@@ -75,13 +97,12 @@ func (us *UDPServer) Run() error {
|
||||
|
||||
defer listener.Close()
|
||||
|
||||
// TODO(jwetzell): make buffer size configurable
|
||||
buffer := make([]byte, 65535)
|
||||
buffer := make([]byte, us.BufferSize)
|
||||
for {
|
||||
select {
|
||||
case <-us.ctx.Done():
|
||||
// TODO(jwetzell): cleanup?
|
||||
slog.Debug("router context done in module", "id", us.Id())
|
||||
us.logger.Debug("done")
|
||||
return nil
|
||||
default:
|
||||
listener.SetDeadline(time.Now().Add(time.Millisecond * 200))
|
||||
@@ -96,15 +117,15 @@ func (us *UDPServer) Run() error {
|
||||
}
|
||||
message := buffer[:numBytes]
|
||||
if us.router != nil {
|
||||
us.router.HandleInput(us.Id(), message)
|
||||
us.router.HandleInput(us.ctx, us.Id(), message)
|
||||
} else {
|
||||
slog.Error("net.udp.server has no router", "id", us.Id())
|
||||
us.logger.Error("input received but no router is configured")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (us *UDPServer) Output(payload any) error {
|
||||
return fmt.Errorf("net.udp.server output is not implemented")
|
||||
func (us *UDPServer) Output(ctx context.Context, payload any) error {
|
||||
return errors.New("net.udp.server output is not implemented")
|
||||
}
|
||||
|
||||
42
internal/processor/artnet-packet-decode.go
Normal file
42
internal/processor/artnet-packet-decode.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/artnet-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ArtNetPacketDecode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (apd *ArtNetPacketDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage, err := artnet.Decode(payloadBytes)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (apd *ArtNetPacketDecode) Type() string {
|
||||
return apd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.decode",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &ArtNetPacketDecode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
41
internal/processor/artnet-packet-encode.go
Normal file
41
internal/processor/artnet-packet-encode.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/artnet-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ArtNetPacketEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ape *ArtNetPacketEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadPacket, ok := payload.(artnet.ArtNetPacket)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.encode processor only accepts an ArtNetPacket")
|
||||
}
|
||||
|
||||
payloadBytes, err := payloadPacket.MarshalBinary()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return payloadBytes, nil
|
||||
}
|
||||
|
||||
func (ape *ArtNetPacketEncode) Type() string {
|
||||
return ape.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.encode",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &ArtNetPacketEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
52
internal/processor/artnet-packet-filter.go
Normal file
52
internal/processor/artnet-packet-filter.go
Normal file
@@ -0,0 +1,52 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/artnet-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ArtNetPacketFilter struct {
|
||||
config config.ProcessorConfig
|
||||
OpCode uint16
|
||||
}
|
||||
|
||||
func (apf *ArtNetPacketFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadPacket, ok := payload.(artnet.ArtNetPacket)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.filter processor only accepts an ArtNetPacket")
|
||||
}
|
||||
|
||||
if payloadPacket.GetOpCode() != apf.OpCode {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return payloadPacket, nil
|
||||
}
|
||||
|
||||
func (apf *ArtNetPacketFilter) Type() string {
|
||||
return apf.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "artnet.packet.filter",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
opCode, ok := params["opCode"]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.filter requires an opCode parameter")
|
||||
}
|
||||
opCodeNum, ok := opCode.(float64)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("artnet.packet.filter opCode must be a number")
|
||||
}
|
||||
|
||||
return &ArtNetPacketFilter{config: config, OpCode: uint16(opCodeNum)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -10,10 +10,11 @@ import (
|
||||
|
||||
type DebugLog struct {
|
||||
config config.ProcessorConfig
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (dl *DebugLog) Process(ctx context.Context, payload any) (any, error) {
|
||||
slog.Debug("debug.log", "payload", payload, "payloadType", fmt.Sprintf("%T", payload))
|
||||
dl.logger.Debug("", "payload", payload, "payloadType", fmt.Sprintf("%T", payload))
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
@@ -25,7 +26,7 @@ func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "debug.log",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &DebugLog{config: config}, nil
|
||||
return &DebugLog{config: config, logger: slog.Default().With("component", "processor", "type", config.Type)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
39
internal/processor/debug-log_test.go
Normal file
39
internal/processor/debug-log_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestDebugLogFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["debug.log"]
|
||||
if !ok {
|
||||
t.Fatalf("debug.log processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "debug.log",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create debug.log processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "debug.log" {
|
||||
t.Fatalf("debug.log processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
|
||||
payload := "test"
|
||||
expected := "test"
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("debug.log processing failed: %s", err)
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Fatalf("debug.log got %+v, expected %+v", got, expected)
|
||||
}
|
||||
}
|
||||
@@ -2,25 +2,25 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type FloatParse struct {
|
||||
config config.ProcessorConfig
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (fp *FloatParse) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("float.parse processor only accepts a string")
|
||||
return nil, errors.New("float.parse processor only accepts a string")
|
||||
}
|
||||
|
||||
// TODO(jwetzell): make bitSize configurable
|
||||
payloadFloat, err := strconv.ParseFloat(payloadString, 64)
|
||||
payloadFloat, err := strconv.ParseFloat(payloadString, fp.BitSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +35,19 @@ func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "float.parse",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &FloatParse{config: config}, nil
|
||||
params := config.Params
|
||||
bitSizeNum := 64
|
||||
bitSize, ok := params["bitSize"]
|
||||
if ok {
|
||||
bitSizeFloat, ok := bitSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("float.parse bitSize must be a number")
|
||||
}
|
||||
|
||||
bitSizeNum = int(bitSizeFloat)
|
||||
}
|
||||
return &FloatParse{config: config, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,30 +3,107 @@ package processor_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestFloatParseFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["float.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("float.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "float.parse",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create float.parse processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "float.parse" {
|
||||
t.Fatalf("float.parse processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatParseBadConfigBitSizeString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["float.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("float.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "float.parse",
|
||||
Params: map[string]any{
|
||||
"bitSize": "64",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("float.parse should have returned an error for bad bitSize config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestFloatParseGoodConfig(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["float.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("float.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "float.parse",
|
||||
Params: map[string]any{
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("float.parse should have created processor but got error: %s", err)
|
||||
}
|
||||
|
||||
payload := "12345.0"
|
||||
expected := float64(12345.0)
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("float.parse processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotFloat, ok := got.(float64)
|
||||
if !ok {
|
||||
t.Fatalf("float.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
|
||||
if gotFloat != expected {
|
||||
t.Fatalf("float.parse got %f, expected %f", gotFloat, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodFloatParse(t *testing.T) {
|
||||
floatParser := processor.FloatParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
bitSize int
|
||||
expected float64
|
||||
}{
|
||||
{
|
||||
name: "positive number",
|
||||
payload: "12345.67",
|
||||
bitSize: 64,
|
||||
expected: 12345.67,
|
||||
},
|
||||
{
|
||||
name: "negative number",
|
||||
payload: "-12345.67",
|
||||
bitSize: 64,
|
||||
expected: -12345.67,
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
payload: "0",
|
||||
bitSize: 64,
|
||||
expected: 0,
|
||||
},
|
||||
}
|
||||
@@ -37,48 +114,59 @@ func TestGoodFloatParse(t *testing.T) {
|
||||
|
||||
gotFloat, ok := got.(float64)
|
||||
if !ok {
|
||||
t.Errorf("float.parse returned a %T payload: %s", got, got)
|
||||
t.Fatalf("float.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("float.parse failed: %s", err)
|
||||
t.Fatalf("float.parse failed: %s", err)
|
||||
}
|
||||
if gotFloat != test.expected {
|
||||
t.Errorf("float.parse got %f, expected %f", gotFloat, test.expected)
|
||||
t.Fatalf("float.parse got %f, expected %f", gotFloat, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadFloatParse(t *testing.T) {
|
||||
floatParser := processor.FloatParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
bitSize int
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
payload: []byte{0x01},
|
||||
bitSize: 64,
|
||||
errorString: "float.parse processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "not float string",
|
||||
payload: "abcd",
|
||||
bitSize: 64,
|
||||
errorString: "strconv.ParseFloat: parsing \"abcd\": invalid syntax",
|
||||
},
|
||||
{
|
||||
name: "bit size overflow",
|
||||
payload: "1.79e+64",
|
||||
bitSize: 32,
|
||||
errorString: "strconv.ParseFloat: parsing \"1.79e+64\": value out of range",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
floatParser := processor.FloatParse{
|
||||
BitSize: test.bitSize,
|
||||
}
|
||||
got, err := floatParser.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("float.parse expected to fail but succeeded, got: %v", got)
|
||||
t.Fatalf("float.parse expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("float.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("float.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
"text/template"
|
||||
|
||||
@@ -190,13 +190,13 @@ func init() {
|
||||
id, ok := params["id"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an id parameter")
|
||||
return nil, errors.New("freed.create requires an id parameter")
|
||||
}
|
||||
|
||||
idString, ok := id.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create id must be a string")
|
||||
return nil, errors.New("freed.create id must be a string")
|
||||
}
|
||||
|
||||
idTemplate, err := template.New("id").Parse(idString)
|
||||
@@ -208,13 +208,13 @@ func init() {
|
||||
pan, ok := params["pan"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an pan parameter")
|
||||
return nil, errors.New("freed.create requires a pan parameter")
|
||||
}
|
||||
|
||||
panString, ok := pan.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create pan must be a string")
|
||||
return nil, errors.New("freed.create pan must be a string")
|
||||
}
|
||||
|
||||
panTemplate, err := template.New("pan").Parse(panString)
|
||||
@@ -222,13 +222,13 @@ func init() {
|
||||
tilt, ok := params["tilt"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an tilt parameter")
|
||||
return nil, errors.New("freed.create requires a tilt parameter")
|
||||
}
|
||||
|
||||
tiltString, ok := tilt.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create tilt must be a string")
|
||||
return nil, errors.New("freed.create tilt must be a string")
|
||||
}
|
||||
|
||||
tiltTemplate, err := template.New("tilt").Parse(tiltString)
|
||||
@@ -236,13 +236,13 @@ func init() {
|
||||
roll, ok := params["roll"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an roll parameter")
|
||||
return nil, errors.New("freed.create requires a roll parameter")
|
||||
}
|
||||
|
||||
rollString, ok := roll.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create roll must be a string")
|
||||
return nil, errors.New("freed.create roll must be a string")
|
||||
}
|
||||
|
||||
rollTemplate, err := template.New("roll").Parse(rollString)
|
||||
@@ -254,13 +254,13 @@ func init() {
|
||||
posX, ok := params["posX"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires a posX parameter")
|
||||
return nil, errors.New("freed.create requires a posX parameter")
|
||||
}
|
||||
|
||||
posXString, ok := posX.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create posX must be a string")
|
||||
return nil, errors.New("freed.create posX must be a string")
|
||||
}
|
||||
|
||||
posXTemplate, err := template.New("posX").Parse(posXString)
|
||||
@@ -272,13 +272,13 @@ func init() {
|
||||
posY, ok := params["posY"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires a posY parameter")
|
||||
return nil, errors.New("freed.create requires a posY parameter")
|
||||
}
|
||||
|
||||
posYString, ok := posY.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create posY must be a string")
|
||||
return nil, errors.New("freed.create posY must be a string")
|
||||
}
|
||||
|
||||
posYTemplate, err := template.New("posY").Parse(posYString)
|
||||
@@ -290,13 +290,13 @@ func init() {
|
||||
posZ, ok := params["posZ"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires a posZ parameter")
|
||||
return nil, errors.New("freed.create requires a posZ parameter")
|
||||
}
|
||||
|
||||
posZString, ok := posZ.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create posZ must be a string")
|
||||
return nil, errors.New("freed.create posZ must be a string")
|
||||
}
|
||||
|
||||
posZTemplate, err := template.New("posZ").Parse(posZString)
|
||||
@@ -308,13 +308,13 @@ func init() {
|
||||
zoom, ok := params["zoom"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an zoom parameter")
|
||||
return nil, errors.New("freed.create requires a zoom parameter")
|
||||
}
|
||||
|
||||
zoomString, ok := zoom.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create zoom must be a string")
|
||||
return nil, errors.New("freed.create zoom must be a string")
|
||||
}
|
||||
|
||||
zoomTemplate, err := template.New("zoom").Parse(zoomString)
|
||||
@@ -322,13 +322,13 @@ func init() {
|
||||
focus, ok := params["focus"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create requires an focus parameter")
|
||||
return nil, errors.New("freed.create requires a focus parameter")
|
||||
}
|
||||
|
||||
focusString, ok := focus.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.create focus must be a string")
|
||||
return nil, errors.New("freed.create focus must be a string")
|
||||
}
|
||||
|
||||
focusTemplate, err := template.New("focus").Parse(focusString)
|
||||
|
||||
@@ -2,8 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"errors"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -17,12 +16,12 @@ func (fdd *FreeDDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.decode processor only accepts a []byte")
|
||||
return nil, errors.New("freed.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage, err := freeD.Decode(payloadBytes)
|
||||
if err != nil {
|
||||
slog.Error("error decoding", "err", err)
|
||||
return nil, err
|
||||
}
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
freeD "github.com/jwetzell/free-d-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -16,7 +16,7 @@ func (fde *FreeDEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadPosition, ok := payload.(freeD.FreeDPosition)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("freed.decode processor only accepts a FreeDEncode")
|
||||
return nil, errors.New("freed.decode processor only accepts a FreeDEncode")
|
||||
}
|
||||
|
||||
payloadBytes := freeD.Encode(payloadPosition)
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"net/http"
|
||||
"text/template"
|
||||
|
||||
@@ -16,10 +16,10 @@ type HTTPRequestCreate struct {
|
||||
URL *template.Template
|
||||
}
|
||||
|
||||
func (hre *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (hrc *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var urlBuffer bytes.Buffer
|
||||
err := hre.URL.Execute(&urlBuffer, payload)
|
||||
err := hrc.URL.Execute(&urlBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -28,7 +28,7 @@ func (hre *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, er
|
||||
urlString := urlBuffer.String()
|
||||
|
||||
//TODO(jwetzell): support body
|
||||
request, err := http.NewRequest(hre.Method, urlString, bytes.NewBuffer([]byte{}))
|
||||
request, err := http.NewRequest(hrc.Method, urlString, bytes.NewBuffer([]byte{}))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -37,8 +37,8 @@ func (hre *HTTPRequestCreate) Process(ctx context.Context, payload any) (any, er
|
||||
return request, nil
|
||||
}
|
||||
|
||||
func (hre *HTTPRequestCreate) Type() string {
|
||||
return hre.config.Type
|
||||
func (hrc *HTTPRequestCreate) Type() string {
|
||||
return hrc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -50,25 +50,25 @@ func init() {
|
||||
method, ok := params["method"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.create requires an method parameter")
|
||||
return nil, errors.New("http.request.create requires a method parameter")
|
||||
}
|
||||
|
||||
methodString, ok := method.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.create url must be a string")
|
||||
return nil, errors.New("http.request.create url must be a string")
|
||||
}
|
||||
|
||||
url, ok := params["url"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.create requires an url parameter")
|
||||
return nil, errors.New("http.request.create requires a url parameter")
|
||||
}
|
||||
|
||||
urlString, ok := url.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.create url must be a string")
|
||||
return nil, errors.New("http.request.create url must be a string")
|
||||
}
|
||||
|
||||
urlTemplate, err := template.New("url").Parse(urlString)
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
@@ -17,7 +17,7 @@ func (hre *HTTPRequestEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadRequest, ok := payload.(*http.Request)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.encode processor only accepts an http.Request")
|
||||
return nil, errors.New("http.request.encode processor only accepts an http.Request")
|
||||
}
|
||||
|
||||
bytes, err := io.ReadAll(payloadRequest.Body)
|
||||
|
||||
@@ -2,6 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
@@ -20,7 +21,7 @@ func (hrf *HTTPRequestFilter) Process(ctx context.Context, payload any) (any, er
|
||||
payloadRequest, ok := payload.(*http.Request)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter can only operate on http.Request payloads")
|
||||
return nil, errors.New("http.request.filter can only operate on http.Request payloads")
|
||||
}
|
||||
|
||||
if hrf.Method != "" {
|
||||
@@ -48,13 +49,13 @@ func init() {
|
||||
path, ok := params["path"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter requires an path parameter")
|
||||
return nil, errors.New("http.request.filter requires a path parameter")
|
||||
}
|
||||
|
||||
pathString, ok := path.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter path must be a string")
|
||||
return nil, errors.New("http.request.filter path must be a string")
|
||||
}
|
||||
|
||||
pathRegexp, err := regexp.Compile(fmt.Sprintf("^%s$", pathString))
|
||||
@@ -69,7 +70,7 @@ func init() {
|
||||
methodString, ok := method.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter method must be a string")
|
||||
return nil, errors.New("http.request.filter method must be a string")
|
||||
}
|
||||
return &HTTPRequestFilter{config: config, Path: pathRegexp, Method: methodString}, nil
|
||||
}
|
||||
|
||||
81
internal/processor/http-response-create.go
Normal file
81
internal/processor/http-response-create.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type HTTPResponseCreate struct {
|
||||
Status int
|
||||
BodyTmpl *template.Template
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
type HTTPResponse struct {
|
||||
Status int
|
||||
Body []byte
|
||||
}
|
||||
|
||||
func (hre *HTTPResponseCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
var bodyBuffer bytes.Buffer
|
||||
err := hre.BodyTmpl.Execute(&bodyBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return HTTPResponse{
|
||||
Status: hre.Status,
|
||||
Body: bodyBuffer.Bytes(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (hre *HTTPResponseCreate) Type() string {
|
||||
return hre.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "http.response.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
status, ok := params["status"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create requires a status parameter")
|
||||
}
|
||||
|
||||
statusNum, ok := status.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create status must be a number")
|
||||
}
|
||||
|
||||
bodyTmpl, ok := params["bodyTemplate"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create requires a bodyTemplate parameter")
|
||||
}
|
||||
|
||||
bodyTemplateString, ok := bodyTmpl.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("http.response.create bodyTemplate must be a string")
|
||||
}
|
||||
|
||||
bodyTemplate, err := template.New("body").Parse(bodyTemplateString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// TODO(jwetzell): support other body kind (direct bytes from input, from file?)
|
||||
return &HTTPResponseCreate{config: config, Status: int(statusNum), BodyTmpl: bodyTemplate}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
@@ -17,7 +17,7 @@ func (hre *HTTPResponseEncode) Process(ctx context.Context, payload any) (any, e
|
||||
payloadResponse, ok := payload.(*http.Response)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.response.encode processor only accepts an http.Response")
|
||||
return nil, errors.New("http.response.encode processor only accepts an http.Response")
|
||||
}
|
||||
defer payloadResponse.Body.Close()
|
||||
|
||||
|
||||
@@ -2,25 +2,26 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type IntParse struct {
|
||||
config config.ProcessorConfig
|
||||
Base int
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ip *IntParse) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("int.parse processor only accepts a string")
|
||||
return nil, errors.New("int.parse processor only accepts a string")
|
||||
}
|
||||
|
||||
// TODO(jwetzell): make base and bitSize configurable
|
||||
payloadInt, err := strconv.ParseInt(payloadString, 10, 64)
|
||||
payloadInt, err := strconv.ParseInt(payloadString, ip.Base, ip.BitSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +36,32 @@ func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "int.parse",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &IntParse{config: config}, nil
|
||||
params := config.Params
|
||||
|
||||
baseNum := 10
|
||||
base, ok := params["base"]
|
||||
if ok {
|
||||
baseFloat, ok := base.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.parse base must be a number")
|
||||
}
|
||||
|
||||
baseNum = int(baseFloat)
|
||||
}
|
||||
|
||||
bitSizeNum := 64
|
||||
bitSize, ok := params["bitSize"]
|
||||
if ok {
|
||||
bitSizeFloat, ok := bitSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.parse bitSize must be a number")
|
||||
}
|
||||
|
||||
bitSizeNum = int(bitSizeFloat)
|
||||
}
|
||||
return &IntParse{config: config, Base: baseNum, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,82 +3,216 @@ package processor_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestIntParseFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create int.parse processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "int.parse" {
|
||||
t.Fatalf("int.parse processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntParseBadConfigBaseString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
Params: map[string]any{
|
||||
"base": "10",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("int.parse should have returned an error for bad base config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntParseBadConfigBitSizeString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
Params: map[string]any{
|
||||
"bitSize": "64",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("int.parse should have returned an error for bad bitSize config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIntParseGoodConfig(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["int.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("int.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "int.parse",
|
||||
Params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("int.parse should have created processor but got error: %s", err)
|
||||
}
|
||||
|
||||
payload := "12345"
|
||||
expected := int64(12345)
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("int.parse processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotInt, ok := got.(int64)
|
||||
if !ok {
|
||||
t.Fatalf("int.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
|
||||
if gotInt != expected {
|
||||
t.Fatalf("int.parse got %d, expected %d", gotInt, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodIntParse(t *testing.T) {
|
||||
intParser := processor.IntParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected int64
|
||||
base int
|
||||
bitSize int
|
||||
}{
|
||||
{
|
||||
name: "positive number",
|
||||
payload: "12345",
|
||||
expected: 12345,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "negative number",
|
||||
payload: "-12345",
|
||||
expected: -12345,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
payload: "0",
|
||||
expected: 0,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "binary",
|
||||
payload: "1010101",
|
||||
expected: 85,
|
||||
base: 2,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "hex",
|
||||
payload: "15F",
|
||||
expected: 351,
|
||||
base: 16,
|
||||
bitSize: 64,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
intParser := processor.IntParse{
|
||||
Base: test.base,
|
||||
BitSize: test.bitSize,
|
||||
}
|
||||
|
||||
got, err := intParser.Process(t.Context(), test.payload)
|
||||
|
||||
gotInt, ok := got.(int64)
|
||||
if !ok {
|
||||
t.Errorf("int.parse returned a %T payload: %s", got, got)
|
||||
t.Fatalf("int.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("int.parse failed: %s", err)
|
||||
t.Fatalf("int.parse failed: %s", err)
|
||||
}
|
||||
if gotInt != test.expected {
|
||||
t.Errorf("int.parse got %d, expected %d", gotInt, test.expected)
|
||||
t.Fatalf("int.parse got %d, expected %d", gotInt, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadIntParse(t *testing.T) {
|
||||
intParser := processor.IntParse{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
base int
|
||||
bitSize int
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
payload: []byte{0x01},
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "int.parse processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "not int string",
|
||||
payload: "123.46",
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "strconv.ParseInt: parsing \"123.46\": invalid syntax",
|
||||
},
|
||||
{
|
||||
name: "bit overflow",
|
||||
payload: "12345678901234567890",
|
||||
base: 10,
|
||||
bitSize: 32,
|
||||
errorString: "strconv.ParseInt: parsing \"12345678901234567890\": value out of range",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
intParser := processor.IntParse{
|
||||
Base: test.base,
|
||||
BitSize: test.bitSize,
|
||||
}
|
||||
got, err := intParser.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("int.parse expected to fail but succeeded, got: %v", got)
|
||||
t.Fatalf("int.parse expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("int.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("int.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
61
internal/processor/int-random.go
Normal file
61
internal/processor/int-random.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/rand/v2"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type IntRandom struct {
|
||||
Min int
|
||||
Max int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (up *IntRandom) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadInt := rand.IntN(up.Max-up.Min+1) + up.Min
|
||||
return payloadInt, nil
|
||||
}
|
||||
|
||||
func (up *IntRandom) Type() string {
|
||||
return up.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "int.random",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
min, ok := params["min"]
|
||||
if !ok {
|
||||
return nil, errors.New("int.random requires a min parameter")
|
||||
}
|
||||
|
||||
minFloat, ok := min.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.random min must be a number")
|
||||
}
|
||||
|
||||
max, ok := params["max"]
|
||||
if !ok {
|
||||
return nil, errors.New("int.random requires a max parameter")
|
||||
}
|
||||
|
||||
maxFloat, ok := max.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("int.random max must be a number")
|
||||
}
|
||||
|
||||
if maxFloat < minFloat {
|
||||
return nil, errors.New("int.random max must be greater than min")
|
||||
}
|
||||
|
||||
return &IntRandom{config: config, Min: int(minFloat), Max: int(maxFloat)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
42
internal/processor/json-encode.go
Normal file
42
internal/processor/json-encode.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type JsonEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (je *JsonEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
var payloadBuffer bytes.Buffer
|
||||
|
||||
err := json.NewEncoder(&payloadBuffer).Encode(payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payloadBytes := payloadBuffer.Bytes()
|
||||
|
||||
payloadBytes = payloadBytes[0 : len(payloadBytes)-1]
|
||||
|
||||
return payloadBytes, nil
|
||||
}
|
||||
|
||||
func (je *JsonEncode) Type() string {
|
||||
return je.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "json.encode",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &JsonEncode{config: config}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
45
internal/processor/json-encode_test.go
Normal file
45
internal/processor/json-encode_test.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/osc-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestGoodJsonEncode(t *testing.T) {
|
||||
stringEncoder := processor.JsonEncode{}
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected []byte
|
||||
}{
|
||||
{
|
||||
processor: &stringEncoder,
|
||||
name: "hello",
|
||||
payload: osc.OSCMessage{
|
||||
Address: "/hello",
|
||||
},
|
||||
expected: []byte("{\"address\":\"/hello\",\"args\":null}"),
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
gotBytes, ok := got.([]byte)
|
||||
if !ok {
|
||||
t.Fatalf("json.encode returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("json.encode failed: %s", err)
|
||||
}
|
||||
if !slices.Equal(gotBytes, test.expected) {
|
||||
t.Fatalf("json.encode got %x, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"text/template"
|
||||
@@ -13,17 +14,18 @@ import (
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
)
|
||||
|
||||
// TODO(jwetzell): support using numbers in config file treated as hardcoded values
|
||||
type MIDIMessageCreate struct {
|
||||
config config.ProcessorConfig
|
||||
ProcessFunc func(ctx context.Context, payload any) (any, error)
|
||||
}
|
||||
|
||||
func (mmd *MIDIMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
return mmd.ProcessFunc(ctx, payload)
|
||||
func (mmc *MIDIMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
return mmc.ProcessFunc(ctx, payload)
|
||||
}
|
||||
|
||||
func (mmd *MIDIMessageCreate) Type() string {
|
||||
return mmd.config.Type
|
||||
func (mmc *MIDIMessageCreate) Type() string {
|
||||
return mmc.config.Type
|
||||
}
|
||||
|
||||
func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
@@ -33,13 +35,13 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a channel parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn channel must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn channel must be a string")
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -51,13 +53,13 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
note, ok := params["note"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a note parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a note parameter")
|
||||
}
|
||||
|
||||
noteString, ok := note.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn note must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn note must be a string")
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
@@ -69,13 +71,13 @@ func newMidiNoteOnCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
velocity, ok := params["velocity"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a velocity parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a velocity parameter")
|
||||
}
|
||||
|
||||
velocityString, ok := velocity.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn velocity must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn velocity must be a string")
|
||||
}
|
||||
|
||||
velocityTemplate, err := template.New("velocity").Parse(velocityString)
|
||||
@@ -124,13 +126,13 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a channel parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn channel must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn channel must be a string")
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -142,13 +144,13 @@ func newMidiNoteOffCreate(config config.ProcessorConfig) (Processor, error) {
|
||||
note, ok := params["note"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn requires a note parameter")
|
||||
return nil, errors.New("midi.message.create NoteOn requires a note parameter")
|
||||
}
|
||||
|
||||
noteString, ok := note.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create NoteOn note must be a string")
|
||||
return nil, errors.New("midi.message.create NoteOn note must be a string")
|
||||
}
|
||||
|
||||
noteTemplate, err := template.New("note").Parse(noteString)
|
||||
@@ -189,13 +191,13 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange requires a channel parameter")
|
||||
return nil, errors.New("midi.message.create ControlChange requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange channel must be a string")
|
||||
return nil, errors.New("midi.message.create ControlChange channel must be a string")
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -207,13 +209,13 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
controller, ok := params["controller"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange requires a controller parameter")
|
||||
return nil, errors.New("midi.message.create ControlChange requires a controller parameter")
|
||||
}
|
||||
|
||||
controllerString, ok := controller.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange controller must be a string")
|
||||
return nil, errors.New("midi.message.create ControlChange controller must be a string")
|
||||
}
|
||||
|
||||
controllerTemplate, err := template.New("controller").Parse(controllerString)
|
||||
@@ -225,13 +227,13 @@ func newMidiControlChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
value, ok := params["value"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange requires a value parameter")
|
||||
return nil, errors.New("midi.message.create ControlChange requires a value parameter")
|
||||
}
|
||||
|
||||
valueString, ok := value.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ControlChange value must be a string")
|
||||
return nil, errors.New("midi.message.create ControlChange value must be a string")
|
||||
}
|
||||
|
||||
valueTemplate, err := template.New("value").Parse(valueString)
|
||||
@@ -281,13 +283,13 @@ func newMidiProgramChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
channel, ok := params["channel"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ProgramChange requires a channel parameter")
|
||||
return nil, errors.New("midi.message.create ProgramChange requires a channel parameter")
|
||||
}
|
||||
|
||||
channelString, ok := channel.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ProgramChange channel must be a string")
|
||||
return nil, errors.New("midi.message.create ProgramChange channel must be a string")
|
||||
}
|
||||
|
||||
channelTemplate, err := template.New("channel").Parse(channelString)
|
||||
@@ -299,13 +301,13 @@ func newMidiProgramChangeCreate(config config.ProcessorConfig) (Processor, error
|
||||
program, ok := params["program"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ProgramChange requires a program parameter")
|
||||
return nil, errors.New("midi.message.create ProgramChange requires a program parameter")
|
||||
}
|
||||
|
||||
programString, ok := program.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create ProgramChange program must be a string")
|
||||
return nil, errors.New("midi.message.create ProgramChange program must be a string")
|
||||
}
|
||||
|
||||
programTemplate, err := template.New("program").Parse(programString)
|
||||
@@ -348,13 +350,13 @@ func init() {
|
||||
msgType, ok := params["type"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create requires a type parameter")
|
||||
return nil, errors.New("midi.message.create requires a type parameter")
|
||||
}
|
||||
|
||||
msgTypeString, ok := msgType.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.create type parameter must be a string")
|
||||
return nil, errors.New("midi.message.create type parameter must be a string")
|
||||
}
|
||||
|
||||
switch msgTypeString {
|
||||
|
||||
@@ -4,7 +4,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
@@ -18,7 +18,7 @@ func (mmd *MIDIMessageDecode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.decode processor only accepts a []byte")
|
||||
return nil, errors.New("midi.message.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage := midi.Message(payloadBytes)
|
||||
|
||||
@@ -4,7 +4,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
@@ -18,7 +18,7 @@ func (mme *MIDIMessageEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.encode processor only accepts an midi.Message")
|
||||
return nil, errors.New("midi.message.encode processor only accepts an midi.Message")
|
||||
}
|
||||
|
||||
return payloadMessage.Bytes(), nil
|
||||
|
||||
@@ -4,7 +4,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"gitlab.com/gomidi/midi/v2"
|
||||
@@ -19,7 +19,7 @@ func (mmf *MIDIMessageFilter) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.filter processor only accepts an midi.Message")
|
||||
return nil, errors.New("midi.message.filter processor only accepts an midi.Message")
|
||||
}
|
||||
|
||||
if payloadMessage.Type().String() != mmf.MIDIType {
|
||||
@@ -41,12 +41,12 @@ func init() {
|
||||
midiType, ok := params["type"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.filter requires a type parameter")
|
||||
return nil, errors.New("midi.message.filter requires a type parameter")
|
||||
}
|
||||
midiTypeString, ok := midiType.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.filter type must be a string")
|
||||
return nil, errors.New("midi.message.filter type must be a string")
|
||||
}
|
||||
|
||||
return &MIDIMessageFilter{config: config, MIDIType: midiTypeString}, nil
|
||||
|
||||
@@ -4,6 +4,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -47,7 +48,7 @@ func (mmu *MIDIMessageUnpack) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMidi, ok := payload.(midi.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("midi.message.unpack processor only accepts a midi.Message")
|
||||
return nil, errors.New("midi.message.unpack processor only accepts a midi.Message")
|
||||
}
|
||||
|
||||
switch payloadMidi.Type() {
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
@@ -51,6 +51,7 @@ func (mm MQTTMessage) Payload() []byte {
|
||||
func (mm MQTTMessage) Ack() {}
|
||||
|
||||
func (mmc *MQTTMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
// TODO(jwetzell): support templating
|
||||
|
||||
message := MQTTMessage{
|
||||
topic: mmc.Topic,
|
||||
@@ -74,44 +75,44 @@ func init() {
|
||||
topic, ok := params["topic"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create requires an topic parameter")
|
||||
return nil, errors.New("mqtt.message.create requires a topic parameter")
|
||||
}
|
||||
|
||||
topicString, ok := topic.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create topic must be a string")
|
||||
return nil, errors.New("mqtt.message.create topic must be a string")
|
||||
}
|
||||
|
||||
qos, ok := params["qos"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create requires an qos parameter")
|
||||
return nil, errors.New("mqtt.message.create requires a qos parameter")
|
||||
}
|
||||
|
||||
qosByte, ok := qos.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create qos must be a number")
|
||||
return nil, errors.New("mqtt.message.create qos must be a number")
|
||||
}
|
||||
|
||||
retained, ok := params["retained"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create requires an retained parameter")
|
||||
return nil, errors.New("mqtt.message.create requires a retained parameter")
|
||||
}
|
||||
|
||||
retainedBool, ok := retained.(bool)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create retained must be a boolean")
|
||||
return nil, errors.New("mqtt.message.create retained must be a boolean")
|
||||
}
|
||||
|
||||
//TODO(jwetzell): convert payload into []byte or string for sending
|
||||
payload, ok := params["payload"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create requires an payload parameter")
|
||||
return nil, errors.New("mqtt.message.create requires a payload parameter")
|
||||
}
|
||||
|
||||
if payloadBytes, ok := payload.([]byte); ok {
|
||||
@@ -121,7 +122,7 @@ func init() {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.create payload must be a string or byte array")
|
||||
return nil, errors.New("mqtt.message.create payload must be a string or byte array")
|
||||
}
|
||||
|
||||
payloadBytes := []byte(payloadString)
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -16,7 +16,7 @@ func (mme *MQTTMessageEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(mqtt.Message)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("mqtt.message.encode processor only accepts an mqtt.Message")
|
||||
return nil, errors.New("mqtt.message.encode processor only accepts an mqtt.Message")
|
||||
}
|
||||
|
||||
return payloadMessage.Payload(), nil
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -16,7 +16,7 @@ type NATSMessage struct {
|
||||
|
||||
type NATSMessageCreate struct {
|
||||
config config.ProcessorConfig
|
||||
Subject string
|
||||
Subject *template.Template
|
||||
Payload *template.Template
|
||||
}
|
||||
|
||||
@@ -31,8 +31,17 @@ func (nmc *NATSMessageCreate) Process(ctx context.Context, payload any) (any, er
|
||||
|
||||
payloadString := payloadBuffer.String()
|
||||
|
||||
var subjectBuffer bytes.Buffer
|
||||
err = nmc.Subject.Execute(&subjectBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
subjectString := subjectBuffer.String()
|
||||
|
||||
message := NATSMessage{
|
||||
Subject: nmc.Subject,
|
||||
Subject: subjectString,
|
||||
Payload: []byte(payloadString),
|
||||
}
|
||||
|
||||
@@ -48,29 +57,34 @@ func init() {
|
||||
Type: "nats.message.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
// TODO(jwetzell): support template for subject
|
||||
subject, ok := params["subject"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.message.create requires a subject parameter")
|
||||
return nil, errors.New("nats.message.create requires a subject parameter")
|
||||
}
|
||||
|
||||
subjectString, ok := subject.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.message.create subject must be a string")
|
||||
return nil, errors.New("nats.message.create subject must be a string")
|
||||
}
|
||||
|
||||
subjectTemplate, err := template.New("subject").Parse(subjectString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload, ok := params["payload"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create requires a payload parameter")
|
||||
return nil, errors.New("nats.message.create requires a payload parameter")
|
||||
}
|
||||
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create payload must be a string")
|
||||
return nil, errors.New("nats.message.create payload must be a string")
|
||||
}
|
||||
|
||||
payloadTemplate, err := template.New("payload").Parse(payloadString)
|
||||
@@ -79,7 +93,7 @@ func init() {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &NATSMessageCreate{config: config, Subject: subjectString, Payload: payloadTemplate}, nil
|
||||
return &NATSMessageCreate{config: config, Subject: subjectTemplate, Payload: payloadTemplate}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/nats-io/nats.go"
|
||||
@@ -16,7 +16,7 @@ func (nme *NATSMessageEncode) Process(ctx context.Context, payload any) (any, er
|
||||
payloadMessage, ok := payload.(*nats.Msg)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("nats.message.encode processor only accepts an nats.Msg")
|
||||
return nil, errors.New("nats.message.encode processor only accepts an nats.Msg")
|
||||
}
|
||||
|
||||
return payloadMessage.Data, nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"text/template"
|
||||
@@ -19,10 +20,10 @@ type OSCMessageCreate struct {
|
||||
Types string
|
||||
}
|
||||
|
||||
func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (omc *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var addressBuffer bytes.Buffer
|
||||
err := o.Address.Execute(&addressBuffer, payload)
|
||||
err := omc.Address.Execute(&addressBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -31,11 +32,11 @@ func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error
|
||||
addressString := addressBuffer.String()
|
||||
|
||||
if len(addressString) == 0 {
|
||||
return nil, fmt.Errorf("osc.message.create address must not be empty")
|
||||
return nil, errors.New("osc.message.create address must not be empty")
|
||||
}
|
||||
|
||||
if addressString[0] != '/' {
|
||||
return nil, fmt.Errorf("osc.message.create address must start with '/'")
|
||||
return nil, errors.New("osc.message.create address must start with '/'")
|
||||
}
|
||||
|
||||
payloadMessage := osc.OSCMessage{
|
||||
@@ -44,7 +45,7 @@ func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error
|
||||
|
||||
args := []osc.OSCArg{}
|
||||
|
||||
for argIndex, argTemplate := range o.Args {
|
||||
for argIndex, argTemplate := range omc.Args {
|
||||
var argBuffer bytes.Buffer
|
||||
err := argTemplate.Execute(&argBuffer, payload)
|
||||
|
||||
@@ -54,7 +55,7 @@ func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error
|
||||
|
||||
argString := argBuffer.String()
|
||||
|
||||
typedArg, err := argToTypedArg(argString, o.Types[argIndex])
|
||||
typedArg, err := argToTypedArg(argString, omc.Types[argIndex])
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -70,8 +71,8 @@ func (o *OSCMessageCreate) Process(ctx context.Context, payload any) (any, error
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageCreate) Type() string {
|
||||
return o.config.Type
|
||||
func (omc *OSCMessageCreate) Type() string {
|
||||
return omc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -82,13 +83,13 @@ func init() {
|
||||
address, ok := params["address"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create requires an address parameter")
|
||||
return nil, errors.New("osc.message.create requires an address parameter")
|
||||
}
|
||||
|
||||
addressString, ok := address.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create address must be a string")
|
||||
return nil, errors.New("osc.message.create address must be a string")
|
||||
}
|
||||
|
||||
addressTemplate, err := template.New("address").Parse(addressString)
|
||||
@@ -109,17 +110,17 @@ func init() {
|
||||
types, ok := params["types"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create requires a types parameter with args")
|
||||
return nil, errors.New("osc.message.create requires a types parameter with args")
|
||||
}
|
||||
|
||||
typesString, ok := types.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create types must be a string")
|
||||
return nil, errors.New("osc.message.create types must be a string")
|
||||
}
|
||||
|
||||
if len(rawArgs) != len(typesString) {
|
||||
return nil, fmt.Errorf("osc.message.create args and types must be the same length")
|
||||
return nil, errors.New("osc.message.create args and types must be the same length")
|
||||
}
|
||||
|
||||
argTemplates := []*template.Template{}
|
||||
@@ -128,7 +129,7 @@ func init() {
|
||||
argString, ok := rawArg.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.create arg must be a string")
|
||||
return nil, errors.New("osc.message.create arg must be a string")
|
||||
}
|
||||
|
||||
argTemplate, err := template.New("arg").Parse(argString)
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
osc "github.com/jwetzell/osc-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -12,19 +12,19 @@ type OSCMessageDecode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (o *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (omd *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.decode processor only accepts a []byte payload")
|
||||
return nil, errors.New("osc.message.decode processor only accepts a []byte payload")
|
||||
}
|
||||
|
||||
if len(payloadBytes) == 0 {
|
||||
return nil, fmt.Errorf("osc.message.decode processor can't work on empty []byte")
|
||||
return nil, errors.New("osc.message.decode processor can't work on empty []byte")
|
||||
}
|
||||
|
||||
if payloadBytes[0] != '/' {
|
||||
return nil, fmt.Errorf("osc.message.decode processor needs an OSC looking []byte")
|
||||
return nil, errors.New("osc.message.decode processor needs an OSC looking []byte")
|
||||
}
|
||||
|
||||
message, err := osc.MessageFromBytes(payloadBytes)
|
||||
@@ -34,8 +34,8 @@ func (o *OSCMessageDecode) Process(ctx context.Context, payload any) (any, error
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageDecode) Type() string {
|
||||
return o.config.Type
|
||||
func (omd *OSCMessageDecode) Type() string {
|
||||
return omd.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
osc "github.com/jwetzell/osc-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -12,19 +12,19 @@ type OSCMessageEncode struct {
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (o *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (ome *OSCMessageEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := payload.(osc.OSCMessage)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.encode processor only accepts an OSCMessage")
|
||||
return nil, errors.New("osc.message.encode processor only accepts an OSCMessage")
|
||||
}
|
||||
|
||||
bytes := payloadMessage.ToBytes()
|
||||
return bytes, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageEncode) Type() string {
|
||||
return o.config.Type
|
||||
func (ome *OSCMessageEncode) Type() string {
|
||||
return ome.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -2,6 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
@@ -15,23 +16,23 @@ type OSCMessageFilter struct {
|
||||
Address *regexp.Regexp
|
||||
}
|
||||
|
||||
func (o *OSCMessageFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (omf *OSCMessageFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
payloadMessage, ok := payload.(osc.OSCMessage)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.filter can only operate on OSCMessage payloads")
|
||||
return nil, errors.New("osc.message.filter can only operate on OSCMessage payloads")
|
||||
}
|
||||
|
||||
if !o.Address.MatchString(payloadMessage.Address) {
|
||||
if !omf.Address.MatchString(payloadMessage.Address) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageFilter) Type() string {
|
||||
return o.config.Type
|
||||
func (omf *OSCMessageFilter) Type() string {
|
||||
return omf.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -42,13 +43,13 @@ func init() {
|
||||
address, ok := params["address"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.filter requires an address parameter")
|
||||
return nil, errors.New("osc.message.filter requires an address parameter")
|
||||
}
|
||||
|
||||
addressString, ok := address.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.filter address must be a string")
|
||||
return nil, errors.New("osc.message.filter address must be a string")
|
||||
}
|
||||
|
||||
addressPattern := strings.ReplaceAll(addressString, "?", ".")
|
||||
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
osc "github.com/jwetzell/osc-go"
|
||||
@@ -15,16 +15,15 @@ type OSCMessageTransform struct {
|
||||
Address *template.Template
|
||||
}
|
||||
|
||||
func (o *OSCMessageTransform) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (omt *OSCMessageTransform) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadMessage, ok := payload.(osc.OSCMessage)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.transform processor only accepts an OSCMessage")
|
||||
return nil, errors.New("osc.message.transform processor only accepts an OSCMessage")
|
||||
}
|
||||
|
||||
var addressBuffer bytes.Buffer
|
||||
//TODO(jwetzell): actually inject data into template
|
||||
err := o.Address.Execute(&addressBuffer, payloadMessage)
|
||||
err := omt.Address.Execute(&addressBuffer, payloadMessage)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -33,11 +32,11 @@ func (o *OSCMessageTransform) Process(ctx context.Context, payload any) (any, er
|
||||
addressString := addressBuffer.String()
|
||||
|
||||
if len(addressString) == 0 {
|
||||
return nil, fmt.Errorf("osc.message.transform address must not be empty")
|
||||
return nil, errors.New("osc.message.transform address must not be empty")
|
||||
}
|
||||
|
||||
if addressString[0] != '/' {
|
||||
return nil, fmt.Errorf("osc.message.transform address must start with '/'")
|
||||
return nil, errors.New("osc.message.transform address must start with '/'")
|
||||
}
|
||||
|
||||
payloadMessage.Address = addressString
|
||||
@@ -45,8 +44,8 @@ func (o *OSCMessageTransform) Process(ctx context.Context, payload any) (any, er
|
||||
return payloadMessage, nil
|
||||
}
|
||||
|
||||
func (o *OSCMessageTransform) Type() string {
|
||||
return o.config.Type
|
||||
func (omt *OSCMessageTransform) Type() string {
|
||||
return omt.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -57,13 +56,13 @@ func init() {
|
||||
address, ok := params["address"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.transform requires an address parameter")
|
||||
return nil, errors.New("osc.message.transform requires an address parameter")
|
||||
}
|
||||
|
||||
addressString, ok := address.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("osc.message.transform address must be a string")
|
||||
return nil, errors.New("osc.message.transform address must be a string")
|
||||
}
|
||||
|
||||
addressTemplate, err := template.New("address").Parse(addressString)
|
||||
|
||||
62
internal/processor/processor_test.go
Normal file
62
internal/processor/processor_test.go
Normal file
@@ -0,0 +1,62 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type TestProcessor struct {
|
||||
}
|
||||
|
||||
func (p *TestProcessor) Type() string {
|
||||
return "test"
|
||||
}
|
||||
func (p *TestProcessor) Process(ctx context.Context, input any) (any, error) {
|
||||
return input, nil
|
||||
}
|
||||
|
||||
func TestProcessorBadRegistrationNoType(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("processor registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
processor.RegisterProcessor(processor.ProcessorRegistration{
|
||||
Type: "",
|
||||
New: func(config config.ProcessorConfig) (processor.Processor, error) {
|
||||
return &TestProcessor{}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessorBadRegistrationNoNew(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("processor registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
processor.RegisterProcessor(processor.ProcessorRegistration{
|
||||
Type: "test",
|
||||
New: nil,
|
||||
})
|
||||
}
|
||||
|
||||
func TestProcessorBadRegistrationExistingType(t *testing.T) {
|
||||
defer func() {
|
||||
if r := recover(); r == nil {
|
||||
t.Fatalf("processor registration should have panicked but did not")
|
||||
}
|
||||
}()
|
||||
|
||||
processor.RegisterProcessor(processor.ProcessorRegistration{
|
||||
Type: "string.create",
|
||||
New: func(config config.ProcessorConfig) (processor.Processor, error) {
|
||||
return &TestProcessor{}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/expr-lang/expr/vm"
|
||||
@@ -38,13 +38,13 @@ func init() {
|
||||
expression, ok := params["expression"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.expr requires an expression parameter")
|
||||
return nil, errors.New("script.expr requires an expression parameter")
|
||||
}
|
||||
|
||||
expressionString, ok := expression.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.expr expression must be a string")
|
||||
return nil, errors.New("script.expr expression must be a string")
|
||||
}
|
||||
|
||||
program, err := expr.Compile(expressionString)
|
||||
|
||||
171
internal/processor/script-expr_test.go
Normal file
171
internal/processor/script-expr_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/expr-lang/expr"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestScriptExprFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: map[string]any{
|
||||
"expression": "foo + bar",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create script.expr processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "script.expr" {
|
||||
t.Fatalf("script.expr processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptExprNoProgram(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: map[string]any{},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr processor should have thrown an error when creating")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptExprBadConfigWrongExpressionType(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: map[string]any{
|
||||
"expression": 12345,
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr processor should have thrown an error when creating with non-string expression")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptExprBadConfigNonCompilingExpression(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.expr"]
|
||||
if !ok {
|
||||
t.Fatalf("script.expr processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.expr",
|
||||
Params: map[string]any{
|
||||
"expression": "foo + ",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr processor should have thrown an error when creating with non-compiling expression")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodScriptExpr(t *testing.T) {
|
||||
tests := []struct {
|
||||
program string
|
||||
name string
|
||||
payload map[string]any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
program: "foo + bar",
|
||||
name: "number",
|
||||
payload: map[string]any{
|
||||
"foo": 1,
|
||||
"bar": 1,
|
||||
},
|
||||
expected: 2,
|
||||
},
|
||||
{
|
||||
program: "foo + bar",
|
||||
name: "string",
|
||||
payload: map[string]any{
|
||||
"foo": "1",
|
||||
"bar": "1",
|
||||
},
|
||||
expected: "11",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
program, err := expr.Compile(test.program)
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed to compile program: %s", err)
|
||||
}
|
||||
|
||||
exprProcessor := &processor.ScriptExpr{Program: program}
|
||||
|
||||
got, err := exprProcessor.Process(t.Context(), test.payload)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed: %s", err)
|
||||
}
|
||||
|
||||
//TODO(jwetzell): work out better way to compare the any/any
|
||||
if got != test.expected {
|
||||
t.Fatalf("script.expr got %+v (%T), expected %+v (%T)", got, got, test.expected, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadScriptExpr(t *testing.T) {
|
||||
tests := []struct {
|
||||
program string
|
||||
name string
|
||||
payload map[string]any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "accessing missing field",
|
||||
program: "foo + bar",
|
||||
payload: map[string]any{
|
||||
"foo": 1,
|
||||
},
|
||||
errorString: "invalid operation: int + <nil> (1:5)\n | foo + bar\n | ....^",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
program, err := expr.Compile(test.program)
|
||||
if err != nil {
|
||||
t.Fatalf("script.expr failed to compile program: %s", err)
|
||||
}
|
||||
|
||||
exprProcessor := &processor.ScriptExpr{Program: program}
|
||||
|
||||
got, err := exprProcessor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.expr expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("script.expr got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"modernc.org/quickjs"
|
||||
@@ -74,13 +74,13 @@ func init() {
|
||||
program, ok := params["program"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.js requires a program parameter")
|
||||
return nil, errors.New("script.js requires a program parameter")
|
||||
}
|
||||
|
||||
programString, ok := program.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.js program must be a string")
|
||||
return nil, errors.New("script.js program must be a string")
|
||||
}
|
||||
|
||||
return &ScriptJS{config: config, Program: programString}, nil
|
||||
|
||||
179
internal/processor/script-js_test.go
Normal file
179
internal/processor/script-js_test.go
Normal file
@@ -0,0 +1,179 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"maps"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestScriptJSFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.js"]
|
||||
if !ok {
|
||||
t.Fatalf("script.js processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.js",
|
||||
Params: map[string]any{
|
||||
"program": `
|
||||
payload = payload + 1
|
||||
`,
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create script.js processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "script.js" {
|
||||
t.Fatalf("script.js processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
|
||||
payload := 1
|
||||
expected := 2
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("script.js processing failed: %s", err)
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Fatalf("script.js got %+v, expected %+v", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptJSNoProgram(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.js"]
|
||||
if !ok {
|
||||
t.Fatalf("script.js processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.js",
|
||||
Params: map[string]any{},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.js processor should have thrown an error when creating")
|
||||
}
|
||||
}
|
||||
|
||||
func TestScriptJSBadConfigWrongProgramType(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["script.js"]
|
||||
if !ok {
|
||||
t.Fatalf("script.js processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "script.js",
|
||||
Params: map[string]any{
|
||||
"program": 12345,
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.js processor should have thrown an error when creating with non-string program")
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodScriptJS(t *testing.T) {
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected any
|
||||
}{
|
||||
{
|
||||
name: "number",
|
||||
processor: &processor.ScriptJS{Program: `
|
||||
payload = payload + 1
|
||||
`},
|
||||
payload: 1,
|
||||
expected: 2,
|
||||
},
|
||||
{
|
||||
name: "string",
|
||||
processor: &processor.ScriptJS{Program: `
|
||||
payload = payload + "1"
|
||||
`},
|
||||
payload: "1",
|
||||
expected: "11",
|
||||
},
|
||||
{
|
||||
name: "object",
|
||||
processor: &processor.ScriptJS{Program: `
|
||||
payload = { key: payload }
|
||||
`},
|
||||
payload: "1",
|
||||
expected: map[string]any{"key": "1"},
|
||||
},
|
||||
{
|
||||
name: "nil",
|
||||
processor: &processor.ScriptJS{Program: `
|
||||
payload = undefined
|
||||
`},
|
||||
payload: "1",
|
||||
expected: nil,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("script.js process failed: %s", err)
|
||||
}
|
||||
|
||||
//TODO(jwetzell): work out better way to compare the any/any
|
||||
|
||||
gotMap, ok := got.(map[string]interface{})
|
||||
if ok {
|
||||
// got a map
|
||||
expectedMap, ok := test.expected.(map[string]interface{})
|
||||
if ok {
|
||||
if !maps.Equal(gotMap, expectedMap) {
|
||||
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
|
||||
}
|
||||
} else {
|
||||
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
|
||||
}
|
||||
} else {
|
||||
if got != test.expected {
|
||||
t.Fatalf("script.js got %+v, expected %+v", got, test.expected)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadScriptJS(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
processor processor.Processor
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "accessing not defined variable",
|
||||
processor: &processor.ScriptJS{Program: `paylod = foo`},
|
||||
payload: 0,
|
||||
errorString: "ReferenceError: 'foo' is not defined",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("script.js expected to fail but succeeded, got: %v", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("script.js got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
92
internal/processor/script-wasm.go
Normal file
92
internal/processor/script-wasm.go
Normal file
@@ -0,0 +1,92 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
extism "github.com/extism/go-sdk"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type ScriptWASM struct {
|
||||
config config.ProcessorConfig
|
||||
Program *extism.CompiledPlugin
|
||||
Function string
|
||||
}
|
||||
|
||||
func (se *ScriptWASM) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm can only operator on byte array")
|
||||
}
|
||||
|
||||
program, err := se.Program.Instance(ctx, extism.PluginInstanceConfig{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
_, output, err := program.Call(se.Function, payloadBytes)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return output, nil
|
||||
}
|
||||
|
||||
func (se *ScriptWASM) Type() string {
|
||||
return se.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "script.wasm",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
path, ok := params["path"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm requires a path parameter")
|
||||
}
|
||||
|
||||
pathString, ok := path.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm path must be a string")
|
||||
}
|
||||
|
||||
functionString := "process"
|
||||
|
||||
function, ok := params["function"]
|
||||
|
||||
if ok {
|
||||
specificFunctionString, ok := function.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("script.wasm function must be a string")
|
||||
}
|
||||
functionString = specificFunctionString
|
||||
}
|
||||
|
||||
manifest := extism.Manifest{
|
||||
Wasm: []extism.Wasm{
|
||||
extism.WasmFile{
|
||||
Path: pathString,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
program, err := extism.NewCompiledPlugin(context.Background(), manifest, extism.PluginConfig{}, []extism.HostFunction{})
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &ScriptWASM{config: config, Program: program, Function: functionString}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
97
internal/processor/sip-response-audio-create.go
Normal file
97
internal/processor/sip-response-audio-create.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type SipResponseAudioCreate struct {
|
||||
config config.ProcessorConfig
|
||||
PreWait int
|
||||
PostWait int
|
||||
AudioFile *template.Template
|
||||
}
|
||||
|
||||
type SipAudioFileResponse struct {
|
||||
PreWait int
|
||||
PostWait int
|
||||
AudioFile string
|
||||
}
|
||||
|
||||
func (scc *SipResponseAudioCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var audioFileBuffer bytes.Buffer
|
||||
err := scc.AudioFile.Execute(&audioFileBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
audioFileString := audioFileBuffer.String()
|
||||
|
||||
return SipAudioFileResponse{
|
||||
PreWait: scc.PreWait,
|
||||
PostWait: scc.PostWait,
|
||||
AudioFile: audioFileString,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (scc *SipResponseAudioCreate) Type() string {
|
||||
return scc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "sip.response.audio.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
preWait, ok := params["preWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a preWait parameter")
|
||||
}
|
||||
|
||||
preWaitNum, ok := preWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create preWait must be a number")
|
||||
}
|
||||
|
||||
postWait, ok := params["postWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a postWait parameter")
|
||||
}
|
||||
|
||||
postWaitNum, ok := postWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create postWait must be a number")
|
||||
}
|
||||
|
||||
audioFile, ok := params["audioFile"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create requires a audioFile parameter")
|
||||
}
|
||||
|
||||
audioFileString, ok := audioFile.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.audio.create audioFile must be a string")
|
||||
}
|
||||
|
||||
audioFileTemplate, err := template.New("audioFile").Parse(audioFileString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SipResponseAudioCreate{config: config, AudioFile: audioFileTemplate, PreWait: int(preWaitNum), PostWait: int(postWaitNum)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
103
internal/processor/sip-response-dtmf-create.go
Normal file
103
internal/processor/sip-response-dtmf-create.go
Normal file
@@ -0,0 +1,103 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type SipResponseDTMFCreate struct {
|
||||
config config.ProcessorConfig
|
||||
PreWait int
|
||||
PostWait int
|
||||
Digits *template.Template
|
||||
validDTMF *regexp.Regexp
|
||||
}
|
||||
|
||||
type SipDTMFResponse struct {
|
||||
PreWait int
|
||||
PostWait int
|
||||
Digits string
|
||||
}
|
||||
|
||||
func (scc *SipResponseDTMFCreate) Process(ctx context.Context, payload any) (any, error) {
|
||||
|
||||
var digitsBuffer bytes.Buffer
|
||||
err := scc.Digits.Execute(&digitsBuffer, payload)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
digitsString := digitsBuffer.String()
|
||||
|
||||
if !scc.validDTMF.MatchString(digitsString) {
|
||||
return nil, errors.New("sip.response.dtmf.create result of digits template contains invalid characters")
|
||||
}
|
||||
|
||||
return SipDTMFResponse{
|
||||
PreWait: scc.PreWait,
|
||||
PostWait: scc.PostWait,
|
||||
Digits: digitsString,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (scc *SipResponseDTMFCreate) Type() string {
|
||||
return scc.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "sip.response.dtmf.create",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
preWait, ok := params["preWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a preWait parameter")
|
||||
}
|
||||
|
||||
preWaitNum, ok := preWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create preWait must be a number")
|
||||
}
|
||||
|
||||
postWait, ok := params["postWait"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a postWait parameter")
|
||||
}
|
||||
|
||||
postWaitNum, ok := postWait.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create postWait must be a number")
|
||||
}
|
||||
|
||||
digits, ok := params["digits"]
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create requires a digits parameter")
|
||||
}
|
||||
|
||||
digitsString, ok := digits.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("sip.response.dtmf.create digits must be a string")
|
||||
}
|
||||
|
||||
digitsTemplate, err := template.New("digits").Parse(digitsString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &SipResponseDTMFCreate{config: config, Digits: digitsTemplate, PreWait: int(preWaitNum), PostWait: int(postWaitNum), validDTMF: regexp.MustCompile(`^[0-9*#A-Da-d]+$`)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -3,7 +3,7 @@ package processor
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -39,13 +39,13 @@ func init() {
|
||||
tmpl, ok := params["template"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.create requires a template parameter")
|
||||
return nil, errors.New("string.create requires a template parameter")
|
||||
}
|
||||
|
||||
templateString, ok := tmpl.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.create template must be a string")
|
||||
return nil, errors.New("string.create template must be a string")
|
||||
}
|
||||
|
||||
templateTemplate, err := template.New("template").Parse(templateString)
|
||||
|
||||
187
internal/processor/string-create_test.go
Normal file
187
internal/processor/string-create_test.go
Normal file
@@ -0,0 +1,187 @@
|
||||
package processor_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"text/template"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type TestStruct struct {
|
||||
Data string
|
||||
}
|
||||
|
||||
func (t TestStruct) GetData() string {
|
||||
return t.Data
|
||||
}
|
||||
|
||||
func TestStringCreateFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["string.create"]
|
||||
if !ok {
|
||||
t.Fatalf("string.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "string.create",
|
||||
Params: map[string]any{
|
||||
"template": "{{.}}",
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create string.create processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "string.create" {
|
||||
t.Fatalf("string.create processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
|
||||
payload := "hello"
|
||||
expected := "hello"
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("string.create processing failed: %s", err)
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Fatalf("string.create got %+v, expected %+v", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodStringCreate(t *testing.T) {
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
template string
|
||||
payload any
|
||||
expected string
|
||||
}{
|
||||
{
|
||||
name: "string payload",
|
||||
template: "{{.}}",
|
||||
payload: "hello",
|
||||
expected: "hello",
|
||||
},
|
||||
{
|
||||
name: "number payload",
|
||||
template: "{{.}}",
|
||||
payload: 4,
|
||||
expected: "4",
|
||||
},
|
||||
{
|
||||
name: "boolean payload",
|
||||
template: "{{.}}",
|
||||
payload: true,
|
||||
expected: "true",
|
||||
},
|
||||
{
|
||||
name: "struct payload - field",
|
||||
template: "{{.Data}}",
|
||||
payload: TestStruct{Data: "test"},
|
||||
expected: "test",
|
||||
},
|
||||
{
|
||||
name: "struct payload - method",
|
||||
template: "{{.GetData}}",
|
||||
payload: TestStruct{Data: "test"},
|
||||
expected: "test",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
template, err := template.New("template").Parse(test.template)
|
||||
if err != nil {
|
||||
t.Fatalf("string.create template parsing failed: %s", err)
|
||||
}
|
||||
|
||||
processor := &processor.StringCreate{Template: template}
|
||||
|
||||
got, err := processor.Process(t.Context(), test.payload)
|
||||
|
||||
gotStrings, ok := got.(string)
|
||||
if !ok {
|
||||
t.Fatalf("string.create returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("string.create failed: %s", err)
|
||||
}
|
||||
if gotStrings != test.expected {
|
||||
t.Fatalf("string.create got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBadStringCreate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
params map[string]any
|
||||
payload any
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "no template param",
|
||||
payload: "hello",
|
||||
params: map[string]any{},
|
||||
errorString: "string.create requires a template parameter",
|
||||
},
|
||||
{
|
||||
name: "non string template",
|
||||
payload: "hello",
|
||||
params: map[string]any{
|
||||
"template": 1,
|
||||
},
|
||||
errorString: "string.create template must be a string",
|
||||
},
|
||||
{
|
||||
name: "invalid template",
|
||||
payload: "hello",
|
||||
params: map[string]any{
|
||||
"template": "{{.",
|
||||
},
|
||||
errorString: "template: template:1: illegal number syntax: \".\"",
|
||||
},
|
||||
{
|
||||
name: "bad property in template",
|
||||
payload: "hello",
|
||||
params: map[string]any{
|
||||
"template": "{{.Invalid}}",
|
||||
},
|
||||
errorString: "template: template:1:2: executing \"template\" at <.Invalid>: can't evaluate field Invalid in type string",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
|
||||
registration, ok := processor.ProcessorRegistry["string.create"]
|
||||
if !ok {
|
||||
t.Fatalf("string.create processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "string.create",
|
||||
Params: test.params,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if test.errorString != err.Error() {
|
||||
t.Fatalf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("string.encode expected to fail but got payload: %s", got)
|
||||
}
|
||||
|
||||
if err.Error() != test.errorString {
|
||||
t.Fatalf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
@@ -15,7 +15,7 @@ func (sd *StringDecode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.decode processor only accepts a []byte")
|
||||
return nil, errors.New("string.decode processor only accepts a []byte")
|
||||
}
|
||||
|
||||
payloadMessage := string(payloadBytes)
|
||||
|
||||
@@ -3,9 +3,40 @@ package processor_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestStringDecodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["string.decode"]
|
||||
if !ok {
|
||||
t.Fatalf("string.decode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "string.decode",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create string.decode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "string.decode" {
|
||||
t.Fatalf("string.decode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
|
||||
payload := []byte{'h', 'e', 'l', 'l', 'o'}
|
||||
expected := "hello"
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("string.decode processing failed: %s", err)
|
||||
}
|
||||
|
||||
if got != expected {
|
||||
t.Fatalf("string.decode got %+v, expected %+v", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodStringDecode(t *testing.T) {
|
||||
stringDecoder := processor.StringDecode{}
|
||||
tests := []struct {
|
||||
@@ -28,13 +59,13 @@ func TestGoodStringDecode(t *testing.T) {
|
||||
|
||||
gotString, ok := got.(string)
|
||||
if !ok {
|
||||
t.Errorf("string.decode returned a %T payload: %s", got, got)
|
||||
t.Fatalf("string.decode returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("string.decode failed: %s", err)
|
||||
t.Fatalf("string.decode failed: %s", err)
|
||||
}
|
||||
if gotString != test.expected {
|
||||
t.Errorf("string.decode got %s, expected %s", got, test.expected)
|
||||
t.Fatalf("string.decode got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -61,10 +92,10 @@ func TestBadStringDecode(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("string.decode expected to fail but got payload: %s", got)
|
||||
t.Fatalf("string.decode expected to fail but got payload: %s", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("string.decode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
@@ -15,7 +15,7 @@ func (se *StringEncode) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.encode processor only accepts a string")
|
||||
return nil, errors.New("string.encode processor only accepts a string")
|
||||
}
|
||||
|
||||
payloadBytes := []byte(payloadString)
|
||||
|
||||
@@ -4,9 +4,46 @@ import (
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestStringEncodeFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["string.encode"]
|
||||
if !ok {
|
||||
t.Fatalf("string.encode processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "string.encode",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create string.encode processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "string.encode" {
|
||||
t.Fatalf("string.encode processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
|
||||
payload := "hello"
|
||||
expected := []byte{'h', 'e', 'l', 'l', 'o'}
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("string.encode processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotBytes, ok := got.([]byte)
|
||||
|
||||
if !ok {
|
||||
t.Fatalf("string.encode should return byte slice")
|
||||
}
|
||||
|
||||
if !slices.Equal(gotBytes, expected) {
|
||||
t.Fatalf("string.encode got %+v, expected %+v", got, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodStringEncode(t *testing.T) {
|
||||
stringEncoder := processor.StringEncode{}
|
||||
tests := []struct {
|
||||
@@ -29,13 +66,13 @@ func TestGoodStringEncode(t *testing.T) {
|
||||
|
||||
gotBytes, ok := got.([]byte)
|
||||
if !ok {
|
||||
t.Errorf("string.encode returned a %T payload: %s", got, got)
|
||||
t.Fatalf("string.encode returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("string.encode failed: %s", err)
|
||||
t.Fatalf("string.encode failed: %s", err)
|
||||
}
|
||||
if !slices.Equal(gotBytes, test.expected) {
|
||||
t.Errorf("string.encode got %s, expected %s", got, test.expected)
|
||||
t.Fatalf("string.encode got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -62,10 +99,10 @@ func TestBadStringEncode(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("string.encode expected to fail but got payload: %s", got)
|
||||
t.Fatalf("string.encode expected to fail but got payload: %s", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("string.encode got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"regexp"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -13,22 +13,22 @@ type StringFilter struct {
|
||||
Pattern *regexp.Regexp
|
||||
}
|
||||
|
||||
func (se *StringFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (sf *StringFilter) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.filter processor only accepts a string")
|
||||
return nil, errors.New("string.filter processor only accepts a string")
|
||||
}
|
||||
|
||||
if !se.Pattern.MatchString(payloadString) {
|
||||
if !sf.Pattern.MatchString(payloadString) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
return payloadString, nil
|
||||
}
|
||||
|
||||
func (se *StringFilter) Type() string {
|
||||
return se.config.Type
|
||||
func (sf *StringFilter) Type() string {
|
||||
return sf.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -40,16 +40,16 @@ func init() {
|
||||
pattern, ok := params["pattern"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter requires an pattern parameter")
|
||||
return nil, errors.New("string.filter requires a pattern parameter")
|
||||
}
|
||||
|
||||
patternString, ok := pattern.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("http.request.filter pattern must be a string")
|
||||
return nil, errors.New("string.filter pattern must be a string")
|
||||
}
|
||||
|
||||
patternRegexp, err := regexp.Compile(fmt.Sprintf("^%s$", patternString))
|
||||
patternRegexp, err := regexp.Compile(patternString)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
|
||||
@@ -2,7 +2,7 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -13,20 +13,20 @@ type StringSplit struct {
|
||||
Separator string
|
||||
}
|
||||
|
||||
func (se *StringSplit) Process(ctx context.Context, payload any) (any, error) {
|
||||
func (ss *StringSplit) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.split only accepts a string")
|
||||
return nil, errors.New("string.split only accepts a string")
|
||||
}
|
||||
|
||||
payloadParts := strings.Split(payloadString, se.Separator)
|
||||
payloadParts := strings.Split(payloadString, ss.Separator)
|
||||
|
||||
return payloadParts, nil
|
||||
}
|
||||
|
||||
func (se *StringSplit) Type() string {
|
||||
return se.config.Type
|
||||
func (ss *StringSplit) Type() string {
|
||||
return ss.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
@@ -38,13 +38,13 @@ func init() {
|
||||
separator, ok := params["separator"]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.split requires a separator")
|
||||
return nil, errors.New("string.split requires a separator")
|
||||
}
|
||||
|
||||
separatorString, ok := separator.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("string.split separator must be a string")
|
||||
return nil, errors.New("string.split separator must be a string")
|
||||
}
|
||||
|
||||
return &StringSplit{config: config, Separator: separatorString}, nil
|
||||
|
||||
@@ -28,19 +28,19 @@ func TestGoodStringSplit(t *testing.T) {
|
||||
|
||||
gotStrings, ok := got.([]string)
|
||||
if !ok {
|
||||
t.Errorf("string.split returned a %T payload: %s", got, got)
|
||||
t.Fatalf("string.split returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("string.split failed: %s", err)
|
||||
t.Fatalf("string.split failed: %s", err)
|
||||
}
|
||||
if !slices.Equal(gotStrings, test.expected) {
|
||||
t.Errorf("string.split got %s, expected %s", got, test.expected)
|
||||
t.Fatalf("string.split got %s, expected %s", got, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBasStringSplit(t *testing.T) {
|
||||
func TestBadStringSplit(t *testing.T) {
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
@@ -60,10 +60,10 @@ func TestBasStringSplit(t *testing.T) {
|
||||
got, err := test.processor.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("string.split expected error but got none, payload: %s", got)
|
||||
t.Fatalf("string.split expected error but got none, payload: %s", got)
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("string.split got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("string.split got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
45
internal/processor/time-sleep.go
Normal file
45
internal/processor/time-sleep.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type MetaDelay struct {
|
||||
config config.ProcessorConfig
|
||||
Duration time.Duration
|
||||
}
|
||||
|
||||
func (md *MetaDelay) Process(ctx context.Context, payload any) (any, error) {
|
||||
time.Sleep(md.Duration)
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func (md *MetaDelay) Type() string {
|
||||
return md.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "time.sleep",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
duration, ok := params["duration"]
|
||||
if !ok {
|
||||
return nil, errors.New("time.sleep requires a duration parameter")
|
||||
}
|
||||
|
||||
durationNum, ok := duration.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("time.sleep duration must be number")
|
||||
}
|
||||
|
||||
return &MetaDelay{config: config, Duration: time.Millisecond * time.Duration(durationNum)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -2,25 +2,26 @@ package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type UintParse struct {
|
||||
config config.ProcessorConfig
|
||||
Base int
|
||||
BitSize int
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (up *UintParse) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadString, ok := payload.(string)
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("uint.parse processor only accepts a string")
|
||||
return nil, errors.New("uint.parse processor only accepts a string")
|
||||
}
|
||||
|
||||
// TODO(jwetzell): make base and bitSize configurable
|
||||
payloadUint, err := strconv.ParseUint(payloadString, 10, 64)
|
||||
payloadUint, err := strconv.ParseUint(payloadString, up.Base, up.BitSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -35,7 +36,31 @@ func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "uint.parse",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
return &UintParse{config: config}, nil
|
||||
params := config.Params
|
||||
baseNum := 10
|
||||
base, ok := params["base"]
|
||||
if ok {
|
||||
baseFloat, ok := base.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("uint.parse base must be a number")
|
||||
}
|
||||
|
||||
baseNum = int(baseFloat)
|
||||
}
|
||||
|
||||
bitSizeNum := 64
|
||||
bitSize, ok := params["bitSize"]
|
||||
if ok {
|
||||
bitSizeFloat, ok := bitSize.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("uint.parse bitSize must be a number")
|
||||
}
|
||||
|
||||
bitSizeNum = int(bitSizeFloat)
|
||||
}
|
||||
return &UintParse{config: config, Base: baseNum, BitSize: bitSizeNum}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -3,42 +3,158 @@ package processor_test
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
func TestUintParseFromRegistry(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "uint.parse",
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create uint.parse processor: %s", err)
|
||||
}
|
||||
|
||||
if processorInstance.Type() != "uint.parse" {
|
||||
t.Fatalf("uint.parse processor has wrong type: %s", processorInstance.Type())
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintParseBadConfigBaseString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "uint.parse",
|
||||
Params: map[string]any{
|
||||
"base": "10",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("uint.parse should have returned an error for bad base config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintParseBadConfigBitSizeString(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse processor not registered")
|
||||
}
|
||||
|
||||
_, err := registration.New(config.ProcessorConfig{
|
||||
Type: "uint.parse",
|
||||
Params: map[string]any{
|
||||
"bitSize": "64",
|
||||
},
|
||||
})
|
||||
|
||||
if err == nil {
|
||||
t.Fatalf("uint.parse should have returned an error for bad bitSize config")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUintParseGoodConfig(t *testing.T) {
|
||||
registration, ok := processor.ProcessorRegistry["uint.parse"]
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse processor not registered")
|
||||
}
|
||||
|
||||
processorInstance, err := registration.New(config.ProcessorConfig{
|
||||
Type: "uint.parse",
|
||||
Params: map[string]any{
|
||||
"base": 10.0,
|
||||
"bitSize": 64.0,
|
||||
},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("uint.parse should have created processor but got error: %s", err)
|
||||
}
|
||||
|
||||
payload := "12345"
|
||||
expected := uint64(12345)
|
||||
|
||||
got, err := processorInstance.Process(t.Context(), payload)
|
||||
if err != nil {
|
||||
t.Fatalf("uint.parse processing failed: %s", err)
|
||||
}
|
||||
|
||||
gotUint, ok := got.(uint64)
|
||||
if !ok {
|
||||
t.Fatalf("uint.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
|
||||
if gotUint != expected {
|
||||
t.Fatalf("uint.parse got %d, expected %d", gotUint, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGoodUintParse(t *testing.T) {
|
||||
uintParser := processor.UintParse{}
|
||||
|
||||
tests := []struct {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
expected uint64
|
||||
base int
|
||||
bitSize int
|
||||
}{
|
||||
{
|
||||
name: "positive number",
|
||||
payload: "12345",
|
||||
expected: 12345,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "zero",
|
||||
payload: "0",
|
||||
expected: 0,
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "binary",
|
||||
payload: "1010101",
|
||||
expected: 85,
|
||||
base: 2,
|
||||
bitSize: 64,
|
||||
},
|
||||
{
|
||||
name: "hex",
|
||||
payload: "15F",
|
||||
expected: 351,
|
||||
base: 16,
|
||||
bitSize: 64,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
uintParser := processor.UintParse{
|
||||
Base: test.base,
|
||||
BitSize: test.bitSize,
|
||||
}
|
||||
got, err := uintParser.Process(t.Context(), test.payload)
|
||||
|
||||
gotUint, ok := got.(uint64)
|
||||
if !ok {
|
||||
t.Errorf("uint.parse returned a %T payload: %s", got, got)
|
||||
t.Fatalf("uint.parse returned a %T payload: %s", got, got)
|
||||
}
|
||||
if err != nil {
|
||||
t.Errorf("uint.parse failed: %s", err)
|
||||
t.Fatalf("uint.parse failed: %s", err)
|
||||
}
|
||||
if gotUint != test.expected {
|
||||
t.Errorf("uint.parse got %d, expected %d", gotUint, test.expected)
|
||||
t.Fatalf("uint.parse got %d, expected %d", gotUint, test.expected)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -50,18 +166,31 @@ func TestBadUintParse(t *testing.T) {
|
||||
processor processor.Processor
|
||||
name string
|
||||
payload any
|
||||
base int
|
||||
bitSize int
|
||||
errorString string
|
||||
}{
|
||||
{
|
||||
name: "non-string input",
|
||||
payload: []byte{0x01},
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "uint.parse processor only accepts a string",
|
||||
},
|
||||
{
|
||||
name: "not uint string",
|
||||
payload: "-1234",
|
||||
base: 10,
|
||||
bitSize: 64,
|
||||
errorString: "strconv.ParseUint: parsing \"-1234\": invalid syntax",
|
||||
},
|
||||
{
|
||||
name: "bit overflow",
|
||||
payload: "123456789012345678901234567",
|
||||
base: 10,
|
||||
bitSize: 32,
|
||||
errorString: "strconv.ParseUint: parsing \"123456789012345678901234567\": value out of range",
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
@@ -69,11 +198,11 @@ func TestBadUintParse(t *testing.T) {
|
||||
got, err := uintParser.Process(t.Context(), test.payload)
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("uint.parse expected to fail but succeeded, got: %v", got)
|
||||
t.Fatalf("uint.parse expected to fail but succeeded, got: %v", got)
|
||||
|
||||
}
|
||||
if err.Error() != test.errorString {
|
||||
t.Errorf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
t.Fatalf("uint.parse got error '%s', expected '%s'", err.Error(), test.errorString)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
61
internal/processor/uint-random.go
Normal file
61
internal/processor/uint-random.go
Normal file
@@ -0,0 +1,61 @@
|
||||
package processor
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"math/rand/v2"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
type UintRandom struct {
|
||||
Min uint
|
||||
Max uint
|
||||
config config.ProcessorConfig
|
||||
}
|
||||
|
||||
func (ur *UintRandom) Process(ctx context.Context, payload any) (any, error) {
|
||||
payloadInt := rand.UintN(ur.Max-ur.Min+1) + ur.Min
|
||||
return payloadInt, nil
|
||||
}
|
||||
|
||||
func (ur *UintRandom) Type() string {
|
||||
return ur.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
RegisterProcessor(ProcessorRegistration{
|
||||
Type: "uint.random",
|
||||
New: func(config config.ProcessorConfig) (Processor, error) {
|
||||
params := config.Params
|
||||
|
||||
min, ok := params["min"]
|
||||
if !ok {
|
||||
return nil, errors.New("uint.random requires a min parameter")
|
||||
}
|
||||
|
||||
minFloat, ok := min.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("uint.random min must be a number")
|
||||
}
|
||||
|
||||
max, ok := params["max"]
|
||||
if !ok {
|
||||
return nil, errors.New("uint.random requires a max parameter")
|
||||
}
|
||||
|
||||
maxFloat, ok := max.(float64)
|
||||
|
||||
if !ok {
|
||||
return nil, errors.New("uint.random max must be a number")
|
||||
}
|
||||
|
||||
if maxFloat < minFloat {
|
||||
return nil, errors.New("uint.random max must be greater than min")
|
||||
}
|
||||
|
||||
return &UintRandom{config: config, Min: uint(minFloat), Max: uint(maxFloat)}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -8,6 +8,11 @@ import (
|
||||
"github.com/jwetzell/showbridge-go/internal/processor"
|
||||
)
|
||||
|
||||
type routeContextKey string
|
||||
|
||||
var RouterContextKey routeContextKey = routeContextKey("router")
|
||||
var SourceContextKey routeContextKey = routeContextKey("source")
|
||||
|
||||
type RouteError struct {
|
||||
Index int
|
||||
Config config.RouteConfig
|
||||
@@ -15,20 +20,21 @@ type RouteError struct {
|
||||
}
|
||||
|
||||
type RouteIOError struct {
|
||||
Index int
|
||||
Error error
|
||||
Index int
|
||||
OutputErrors []error
|
||||
ProcessError error
|
||||
InputError error
|
||||
}
|
||||
|
||||
type RouteIO interface {
|
||||
HandleInput(sourceId string, payload any) []RouteIOError
|
||||
HandleOutput(sourceId string, destinationId string, payload any) error
|
||||
HandleInput(ctx context.Context, sourceId string, payload any) (bool, []RouteIOError)
|
||||
HandleOutput(ctx context.Context, destinationId string, payload any) []error
|
||||
}
|
||||
|
||||
type Route interface {
|
||||
Input() string
|
||||
Output() string
|
||||
HandleInput(ctx context.Context, sourceId string, payload any, router RouteIO) error
|
||||
HandleOutput(ctx context.Context, sourceId string, payload any, router RouteIO) error
|
||||
ProcessPayload(ctx context.Context, payload any) (any, error)
|
||||
}
|
||||
|
||||
type ProcessorRoute struct {
|
||||
@@ -66,21 +72,18 @@ func (r *ProcessorRoute) Output() string {
|
||||
return r.output
|
||||
}
|
||||
|
||||
func (r *ProcessorRoute) HandleInput(ctx context.Context, sourceId string, payload any, router RouteIO) error {
|
||||
var err error
|
||||
func (r *ProcessorRoute) ProcessPayload(ctx context.Context, payload any) (any, error) {
|
||||
for _, processor := range r.processors {
|
||||
payload, err = processor.Process(ctx, payload)
|
||||
processedPayload, err := processor.Process(ctx, payload)
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, err
|
||||
}
|
||||
//NOTE(jwetzell) nil payload will result in the route being "terminated"
|
||||
if payload == nil {
|
||||
return nil
|
||||
if processedPayload == nil {
|
||||
return nil, nil
|
||||
}
|
||||
payload = processedPayload
|
||||
}
|
||||
return r.HandleOutput(ctx, sourceId, payload, router)
|
||||
}
|
||||
|
||||
func (r *ProcessorRoute) HandleOutput(ctx context.Context, sourceId string, payload any, router RouteIO) error {
|
||||
return router.HandleOutput(sourceId, r.output, payload)
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
166
internal/route/route_test.go
Normal file
166
internal/route/route_test.go
Normal file
@@ -0,0 +1,166 @@
|
||||
package route_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"slices"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
func TestRouteCreate(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
if testRoute.Input() != routeConfig.Input {
|
||||
t.Fatalf("route input does not match expected input")
|
||||
}
|
||||
if testRoute.Output() != routeConfig.Output {
|
||||
t.Fatalf("route output does not match expected output")
|
||||
}
|
||||
}
|
||||
|
||||
type MockRouter struct{}
|
||||
|
||||
func (mr *MockRouter) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []route.RouteIOError) {
|
||||
return false, []route.RouteIOError{}
|
||||
}
|
||||
|
||||
func (mr *MockRouter) HandleOutput(ctx context.Context, destinationId string, payload any) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestGoodRouteHandleInput(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "string.encode"},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
inputData := "test input data"
|
||||
payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), inputData)
|
||||
if err != nil {
|
||||
t.Fatalf("route ProcessPayload returned error: %v", err)
|
||||
}
|
||||
|
||||
payloadBytes, ok := payload.([]byte)
|
||||
if !ok {
|
||||
t.Fatalf("payload should be []byte got %T", payload)
|
||||
}
|
||||
|
||||
if !slices.Equal([]byte(inputData), payloadBytes) {
|
||||
t.Fatalf("route returned the wrong payload. expected: %+v got %+v", inputData, payloadBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteHandleInputWithProcessorError(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "string.create", Params: map[string]any{"template": "{{.invalid}}}"}},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
inputData := "test input data"
|
||||
_, err = testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), inputData)
|
||||
if err == nil {
|
||||
t.Fatalf("route HandleOutput did not return error for bad processor")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteHandleNilPayload(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), nil)
|
||||
if err != nil {
|
||||
t.Fatalf("route ProcessPayload returned error: %v", err)
|
||||
}
|
||||
if payload != nil {
|
||||
t.Fatalf("route returned the wrong payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteHandleNilPayloadFromProcessor(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "script.js", Params: map[string]any{"program": "payload = undefined"}},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
testRoute, err := route.NewRoute(routeConfig)
|
||||
if err != nil {
|
||||
t.Fatalf("route failed to create: %v", err)
|
||||
}
|
||||
|
||||
payload, err := testRoute.ProcessPayload(context.WithValue(t.Context(), route.RouterContextKey, &MockRouter{}), "test")
|
||||
if err != nil {
|
||||
t.Fatalf("route HandleOutput returned error for nil payload: %v", err)
|
||||
}
|
||||
|
||||
if payload != nil {
|
||||
t.Fatalf("route returned the wrong payload")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteUnknownProcessor(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "asdfasdflkjalkj"},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
_, err := route.NewRoute(routeConfig)
|
||||
if err == nil {
|
||||
t.Fatalf("route error expected when creating route with an unknown processor, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouteBadProcessorConfig(t *testing.T) {
|
||||
routeConfig := config.RouteConfig{
|
||||
Input: "input",
|
||||
Processors: []config.ProcessorConfig{
|
||||
{Type: "string.create", Params: map[string]any{}},
|
||||
},
|
||||
Output: "output",
|
||||
}
|
||||
|
||||
_, err := route.NewRoute(routeConfig)
|
||||
if err == nil {
|
||||
t.Fatalf("route error expected creating route with bad processor, got nil")
|
||||
}
|
||||
}
|
||||
140
router.go
140
router.go
@@ -2,9 +2,8 @@ package showbridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
@@ -18,29 +17,35 @@ type Router struct {
|
||||
ModuleInstances []module.Module
|
||||
RouteInstances []route.Route
|
||||
moduleWait sync.WaitGroup
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func NewRouter(ctx context.Context, config config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: slog.LevelInfo,
|
||||
}))
|
||||
func NewRouter(config config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
||||
|
||||
slog.SetDefault(logger)
|
||||
|
||||
slog.Debug("creating router")
|
||||
|
||||
routerContext, cancel := context.WithCancel(ctx)
|
||||
router := Router{
|
||||
Context: routerContext,
|
||||
contextCancel: cancel,
|
||||
ModuleInstances: []module.Module{},
|
||||
RouteInstances: []route.Route{},
|
||||
logger: slog.Default().With("component", "router"),
|
||||
}
|
||||
|
||||
router.logger.Debug("creating")
|
||||
|
||||
var moduleErrors []module.ModuleError
|
||||
|
||||
for moduleIndex, moduleDecl := range config.Modules {
|
||||
|
||||
if moduleDecl.Id == "" {
|
||||
if moduleErrors == nil {
|
||||
moduleErrors = []module.ModuleError{}
|
||||
}
|
||||
moduleErrors = append(moduleErrors, module.ModuleError{
|
||||
Index: moduleIndex,
|
||||
Config: moduleDecl,
|
||||
Error: errors.New("module id cannot be empty"),
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
moduleInfo, ok := module.ModuleRegistry[moduleDecl.Type]
|
||||
if !ok {
|
||||
if moduleErrors == nil {
|
||||
@@ -49,7 +54,7 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []module.Mod
|
||||
moduleErrors = append(moduleErrors, module.ModuleError{
|
||||
Index: moduleIndex,
|
||||
Config: moduleDecl,
|
||||
Error: fmt.Errorf("module type not defined"),
|
||||
Error: errors.New("module type not defined"),
|
||||
})
|
||||
continue
|
||||
}
|
||||
@@ -64,14 +69,14 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []module.Mod
|
||||
moduleErrors = append(moduleErrors, module.ModuleError{
|
||||
Index: moduleIndex,
|
||||
Config: moduleDecl,
|
||||
Error: fmt.Errorf("duplicate module id"),
|
||||
Error: errors.New("duplicate module id"),
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !moduleInstanceExists {
|
||||
moduleInstance, err := moduleInfo.New(router.Context, moduleDecl, &router)
|
||||
moduleInstance, err := moduleInfo.New(moduleDecl)
|
||||
if err != nil {
|
||||
if moduleErrors == nil {
|
||||
moduleErrors = []module.ModuleError{}
|
||||
@@ -109,52 +114,95 @@ func NewRouter(ctx context.Context, config config.Config) (*Router, []module.Mod
|
||||
return &router, moduleErrors, routeErrors
|
||||
}
|
||||
|
||||
func (r *Router) Run() {
|
||||
slog.Info("running router")
|
||||
func (r *Router) Run(ctx context.Context) {
|
||||
r.logger.Info("running")
|
||||
routerContext, cancel := context.WithCancel(ctx)
|
||||
r.Context = routerContext
|
||||
r.contextCancel = cancel
|
||||
contextWithRouter := context.WithValue(routerContext, route.RouterContextKey, r)
|
||||
|
||||
for _, moduleInstance := range r.ModuleInstances {
|
||||
r.moduleWait.Add(1)
|
||||
go func() {
|
||||
err := moduleInstance.Run()
|
||||
r.moduleWait.Go(func() {
|
||||
err := moduleInstance.Run(contextWithRouter)
|
||||
if err != nil {
|
||||
slog.Error("error encountered running module", "id", moduleInstance.Id(), "error", err)
|
||||
r.logger.Error("error encountered running module", "error", err)
|
||||
}
|
||||
r.moduleWait.Done()
|
||||
}()
|
||||
})
|
||||
}
|
||||
<-r.Context.Done()
|
||||
r.logger.Debug("waiting for modules to exit")
|
||||
r.moduleWait.Wait()
|
||||
slog.Info("router done")
|
||||
r.logger.Info("done")
|
||||
}
|
||||
|
||||
func (r *Router) Stop() {
|
||||
r.logger.Info("stopping")
|
||||
r.contextCancel()
|
||||
}
|
||||
|
||||
func (r *Router) HandleInput(sourceId string, payload any) []route.RouteIOError {
|
||||
var routingErrors []route.RouteIOError
|
||||
func (r *Router) HandleInput(ctx context.Context, sourceId string, payload any) (bool, []route.RouteIOError) {
|
||||
var routeIOErrors []route.RouteIOError
|
||||
routeFound := false
|
||||
|
||||
var routeWaitGroup sync.WaitGroup
|
||||
|
||||
for routeIndex, routeInstance := range r.RouteInstances {
|
||||
if routeInstance.Input() == sourceId {
|
||||
err := routeInstance.HandleInput(r.Context, sourceId, payload, r)
|
||||
if err != nil {
|
||||
if routingErrors == nil {
|
||||
routingErrors = []route.RouteIOError{}
|
||||
routeWaitGroup.Go(func() {
|
||||
|
||||
routeFound = true
|
||||
routeContext := context.WithValue(ctx, route.SourceContextKey, sourceId)
|
||||
|
||||
payload, err := routeInstance.ProcessPayload(routeContext, payload)
|
||||
if err != nil {
|
||||
if routeIOErrors == nil {
|
||||
routeIOErrors = []route.RouteIOError{}
|
||||
}
|
||||
r.logger.Error("unable to process input", "route", routeIndex, "source", sourceId, "error", err)
|
||||
routeIOErrors = append(routeIOErrors, route.RouteIOError{
|
||||
Index: routeIndex,
|
||||
ProcessError: err,
|
||||
})
|
||||
return
|
||||
}
|
||||
routingErrors = append(routingErrors, route.RouteIOError{
|
||||
Index: routeIndex,
|
||||
Error: err,
|
||||
})
|
||||
slog.Error("router unable to route input", "route", routeIndex, "source", sourceId, "error", err)
|
||||
|
||||
if payload == nil {
|
||||
r.logger.Error("no input after processing", "route", routeIndex, "source", sourceId)
|
||||
return
|
||||
}
|
||||
|
||||
outputErrors := r.HandleOutput(routeContext, routeInstance.Output(), payload)
|
||||
if outputErrors != nil {
|
||||
if routeIOErrors == nil {
|
||||
routeIOErrors = []route.RouteIOError{}
|
||||
}
|
||||
routeIOErrors = append(routeIOErrors, route.RouteIOError{
|
||||
Index: routeIndex,
|
||||
OutputErrors: outputErrors,
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
}
|
||||
routeWaitGroup.Wait()
|
||||
return routeFound, routeIOErrors
|
||||
}
|
||||
|
||||
func (r *Router) HandleOutput(ctx context.Context, destinationId string, payload any) []error {
|
||||
|
||||
var outputErrors []error
|
||||
for _, moduleInstance := range r.ModuleInstances {
|
||||
if moduleInstance.Id() == destinationId {
|
||||
err := moduleInstance.Output(ctx, payload)
|
||||
if err != nil {
|
||||
if outputErrors == nil {
|
||||
outputErrors = []error{}
|
||||
}
|
||||
outputErrors = append(outputErrors, err)
|
||||
r.logger.Error("unable to route output", "module", moduleInstance.Id(), "error", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return routingErrors
|
||||
}
|
||||
|
||||
func (r *Router) HandleOutput(sourceId string, destinationId string, payload any) error {
|
||||
for _, moduleInstance := range r.ModuleInstances {
|
||||
if moduleInstance.Id() == destinationId {
|
||||
return moduleInstance.Output(payload)
|
||||
}
|
||||
}
|
||||
return fmt.Errorf("router could not find module instance for destination %s", destinationId)
|
||||
return outputErrors
|
||||
}
|
||||
|
||||
341
router_test.go
Normal file
341
router_test.go
Normal file
@@ -0,0 +1,341 @@
|
||||
package showbridge_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jwetzell/showbridge-go"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
"github.com/jwetzell/showbridge-go/internal/route"
|
||||
)
|
||||
|
||||
type MockModule struct {
|
||||
config config.ModuleConfig
|
||||
ctx context.Context
|
||||
outputCount int
|
||||
router route.RouteIO
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
func (m *MockModule) Id() string {
|
||||
return m.config.Id
|
||||
}
|
||||
|
||||
func (m *MockModule) Output(context.Context, any) error {
|
||||
m.outputCount += 1
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockModule) Run(ctx context.Context) error {
|
||||
m.ctx = ctx
|
||||
<-m.ctx.Done()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MockModule) Type() string {
|
||||
return m.config.Type
|
||||
}
|
||||
|
||||
func init() {
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "mock.counter",
|
||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
||||
|
||||
return &MockModule{config: config, logger: slog.Default()}, nil
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func TestNewRouter(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, moduleErrors, routeErrors := showbridge.NewRouter(routerConfig)
|
||||
|
||||
if moduleErrors != nil {
|
||||
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
|
||||
}
|
||||
|
||||
if routeErrors != nil {
|
||||
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRouterUnknownModuleType(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "asd.fjlkj23oiu4ksldj",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, moduleErrors, _ := showbridge.NewRouter(routerConfig)
|
||||
|
||||
if moduleErrors == nil {
|
||||
t.Fatalf("router should have returned 'unknown module' module errors")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewRouterDuplicateModuleId(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
_, moduleErrors, _ := showbridge.NewRouter(routerConfig)
|
||||
|
||||
if moduleErrors == nil {
|
||||
t.Fatalf("router should have returned 'duplicate id' module error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterInputSingleRoute(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
Routes: []config.RouteConfig{
|
||||
{
|
||||
Input: "mock",
|
||||
Output: "mock",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(routerConfig)
|
||||
|
||||
if moduleErrors != nil {
|
||||
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
|
||||
}
|
||||
|
||||
if routeErrors != nil {
|
||||
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
|
||||
}
|
||||
|
||||
routerRunner := sync.WaitGroup{}
|
||||
|
||||
routerRunner.Go(func() {
|
||||
router.Run(t.Context())
|
||||
})
|
||||
|
||||
time.Sleep(time.Second * 1)
|
||||
|
||||
defer router.Stop()
|
||||
|
||||
mockModuleInputCount := 3
|
||||
for i := range mockModuleInputCount {
|
||||
aRouteFound, routingErrors := router.HandleInput(t.Context(), "mock", fmt.Sprintf("test %d", i))
|
||||
|
||||
if routingErrors != nil {
|
||||
t.Fatalf("router should not have encountered routing errors")
|
||||
}
|
||||
|
||||
if !aRouteFound {
|
||||
t.Fatalf("router should have found a valid route for the input")
|
||||
}
|
||||
}
|
||||
|
||||
for _, moduleInstance := range router.ModuleInstances {
|
||||
if moduleInstance.Id() == "mock" {
|
||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
||||
if !ok {
|
||||
t.Fatalf("couldn't get mock module")
|
||||
}
|
||||
|
||||
if mockModuleInstance.outputCount != mockModuleInputCount {
|
||||
t.Fatalf("mock module output count did not matched expected: %d got: %d", mockModuleInputCount, mockModuleInstance.outputCount)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterInputMultipleRoutes(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
Routes: []config.RouteConfig{
|
||||
{
|
||||
Input: "mock",
|
||||
Output: "mock",
|
||||
},
|
||||
{
|
||||
Input: "mock",
|
||||
Output: "mock",
|
||||
},
|
||||
{
|
||||
Input: "mock",
|
||||
Output: "mock",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(routerConfig)
|
||||
|
||||
if moduleErrors != nil {
|
||||
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
|
||||
}
|
||||
|
||||
if routeErrors != nil {
|
||||
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
|
||||
}
|
||||
|
||||
routerRunner := sync.WaitGroup{}
|
||||
|
||||
routerRunner.Go(func() {
|
||||
router.Run(t.Context())
|
||||
})
|
||||
time.Sleep(time.Second * 1)
|
||||
|
||||
defer router.Stop()
|
||||
|
||||
mockModuleInputCount := 3
|
||||
for i := range mockModuleInputCount {
|
||||
aRouteFound, routingErrors := router.HandleInput(t.Context(), "mock", fmt.Sprintf("test %d", i))
|
||||
|
||||
if routingErrors != nil {
|
||||
t.Fatalf("router should not have encountered routing errors")
|
||||
}
|
||||
|
||||
if !aRouteFound {
|
||||
t.Fatalf("router should have found a valid route for the input")
|
||||
}
|
||||
}
|
||||
|
||||
for _, moduleInstance := range router.ModuleInstances {
|
||||
if moduleInstance.Id() == "mock" {
|
||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
||||
if !ok {
|
||||
t.Fatalf("couldn't get mock module")
|
||||
}
|
||||
|
||||
if mockModuleInstance.outputCount != len(router.RouteInstances)*mockModuleInputCount {
|
||||
t.Fatalf("mock module output count did not matched expected: %d got: %d", len(router.RouteInstances)*mockModuleInputCount, mockModuleInstance.outputCount)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRouterInputMultipleModules(t *testing.T) {
|
||||
routerConfig := config.Config{
|
||||
Modules: []config.ModuleConfig{
|
||||
{
|
||||
Id: "mock1",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
{
|
||||
Id: "mock2",
|
||||
Type: "mock.counter",
|
||||
},
|
||||
},
|
||||
Routes: []config.RouteConfig{
|
||||
{
|
||||
Input: "mock1",
|
||||
Output: "mock1",
|
||||
},
|
||||
{
|
||||
Input: "mock2",
|
||||
Output: "mock2",
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
router, moduleErrors, routeErrors := showbridge.NewRouter(routerConfig)
|
||||
|
||||
if moduleErrors != nil {
|
||||
t.Fatalf("router should not have returned any module errors: %v", moduleErrors)
|
||||
}
|
||||
|
||||
if routeErrors != nil {
|
||||
t.Fatalf("router should not have returned any route errors: %v", routeErrors)
|
||||
}
|
||||
|
||||
routerRunner := sync.WaitGroup{}
|
||||
|
||||
routerRunner.Go(func() {
|
||||
router.Run(t.Context())
|
||||
})
|
||||
|
||||
time.Sleep(time.Second * 1)
|
||||
|
||||
defer router.Stop()
|
||||
|
||||
mock1ModuleInputCount := 3
|
||||
for i := range mock1ModuleInputCount {
|
||||
aRouteFound, routingErrors := router.HandleInput(t.Context(), "mock1", fmt.Sprintf("test %d", i))
|
||||
|
||||
if routingErrors != nil {
|
||||
t.Fatalf("router should not have encountered routing errors")
|
||||
}
|
||||
|
||||
if !aRouteFound {
|
||||
t.Fatalf("router should have found a valid route for the input")
|
||||
}
|
||||
}
|
||||
|
||||
mock2ModuleInputCount := 2
|
||||
for i := range mock2ModuleInputCount {
|
||||
aRouteFound, routingErrors := router.HandleInput(t.Context(), "mock2", fmt.Sprintf("test %d", i))
|
||||
|
||||
if routingErrors != nil {
|
||||
t.Fatalf("router should not have encountered routing errors")
|
||||
}
|
||||
|
||||
if !aRouteFound {
|
||||
t.Fatalf("router should have found a valid route for the input")
|
||||
}
|
||||
}
|
||||
|
||||
for _, moduleInstance := range router.ModuleInstances {
|
||||
if moduleInstance.Id() == "mock1" {
|
||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
||||
if !ok {
|
||||
t.Fatalf("couldn't get mock module")
|
||||
}
|
||||
|
||||
if mockModuleInstance.outputCount != mock1ModuleInputCount {
|
||||
t.Fatalf("mock module output count did not matched expected: %d got: %d", mock1ModuleInputCount, mockModuleInstance.outputCount)
|
||||
}
|
||||
break
|
||||
}
|
||||
if moduleInstance.Id() == "mock2" {
|
||||
mockModuleInstance, ok := moduleInstance.(*MockModule)
|
||||
if !ok {
|
||||
t.Fatalf("couldn't get mock module")
|
||||
}
|
||||
|
||||
if mockModuleInstance.outputCount != mock2ModuleInputCount {
|
||||
t.Fatalf("mock module output count did not matched expected: %d got: %d", mock2ModuleInputCount, mockModuleInstance.outputCount)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user