mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 21:05:30 +00:00
move config to internal
This commit is contained in:
@@ -1,6 +0,0 @@
|
|||||||
package showbridge
|
|
||||||
|
|
||||||
type Config struct {
|
|
||||||
Modules []ModuleConfig `json:"modules"`
|
|
||||||
Routes []RouteConfig `json:"routes"`
|
|
||||||
}
|
|
||||||
@@ -5,10 +5,12 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPClient struct {
|
type HTTPClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
router *Router
|
||||||
client *http.Client
|
client *http.Client
|
||||||
}
|
}
|
||||||
@@ -16,7 +18,7 @@ type HTTPClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.http.client",
|
Type: "net.http.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
|
|
||||||
return &HTTPClient{config: config}, nil
|
return &HTTPClient{config: config}, nil
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type HTTPServer struct {
|
type HTTPServer struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Port uint16
|
Port uint16
|
||||||
router *Router
|
router *Router
|
||||||
}
|
}
|
||||||
@@ -21,7 +23,7 @@ type ResponseData struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.http.server",
|
Type: "net.http.server",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
20
internal/config/config.go
Normal file
20
internal/config/config.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import "github.com/jwetzell/showbridge-go/internal/processing"
|
||||||
|
|
||||||
|
type Config struct {
|
||||||
|
Modules []ModuleConfig `json:"modules"`
|
||||||
|
Routes []RouteConfig `json:"routes"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type ModuleConfig struct {
|
||||||
|
Id string `json:"id"`
|
||||||
|
Type string `json:"type"`
|
||||||
|
Params map[string]any `json:"params"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type RouteConfig struct {
|
||||||
|
Input string `json:"input"`
|
||||||
|
Processors []processing.ProcessorConfig `json:"processors"`
|
||||||
|
Output string `json:"output"`
|
||||||
|
}
|
||||||
@@ -4,10 +4,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Interval struct {
|
type Interval struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Duration uint32
|
Duration uint32
|
||||||
router *Router
|
router *Router
|
||||||
ticker *time.Ticker
|
ticker *time.Ticker
|
||||||
@@ -16,7 +18,7 @@ type Interval struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "gen.interval",
|
Type: "gen.interval",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
duration, ok := params["duration"]
|
duration, ok := params["duration"]
|
||||||
|
|||||||
@@ -6,12 +6,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"gitlab.com/gomidi/midi/v2"
|
"gitlab.com/gomidi/midi/v2"
|
||||||
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
_ "gitlab.com/gomidi/midi/v2/drivers/rtmididrv"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MIDIClient struct {
|
type MIDIClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
router *Router
|
||||||
InputPort string
|
InputPort string
|
||||||
OutputPort string
|
OutputPort string
|
||||||
@@ -22,7 +23,7 @@ func init() {
|
|||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
//TODO(jwetzell): find a better namespace than "misc"
|
//TODO(jwetzell): find a better namespace than "misc"
|
||||||
Type: "misc.midi.client",
|
Type: "misc.midi.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
input, ok := params["input"]
|
input, ok := params["input"]
|
||||||
|
|
||||||
|
|||||||
12
module.go
12
module.go
@@ -3,11 +3,13 @@ package showbridge
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ModuleError struct {
|
type ModuleError struct {
|
||||||
Index int
|
Index int
|
||||||
Config ModuleConfig
|
Config config.ModuleConfig
|
||||||
Error error
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -19,15 +21,9 @@ type Module interface {
|
|||||||
Output(any) error
|
Output(any) error
|
||||||
}
|
}
|
||||||
|
|
||||||
type ModuleConfig struct {
|
|
||||||
Id string `json:"id"`
|
|
||||||
Type string `json:"type"`
|
|
||||||
Params map[string]any `json:"params"`
|
|
||||||
}
|
|
||||||
|
|
||||||
type ModuleRegistration struct {
|
type ModuleRegistration struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
New func(ModuleConfig) (Module, error)
|
New func(config.ModuleConfig) (Module, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterModule(mod ModuleRegistration) {
|
func RegisterModule(mod ModuleRegistration) {
|
||||||
|
|||||||
@@ -5,11 +5,12 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
mqtt "github.com/eclipse/paho.mqtt.golang"
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processing"
|
"github.com/jwetzell/showbridge-go/internal/processing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MQTTClient struct {
|
type MQTTClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
router *Router
|
||||||
Broker string
|
Broker string
|
||||||
ClientID string
|
ClientID string
|
||||||
@@ -20,7 +21,7 @@ type MQTTClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.mqtt.client",
|
Type: "net.mqtt.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
broker, ok := params["broker"]
|
broker, ok := params["broker"]
|
||||||
|
|
||||||
|
|||||||
@@ -4,12 +4,13 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processing"
|
"github.com/jwetzell/showbridge-go/internal/processing"
|
||||||
"github.com/nats-io/nats.go"
|
"github.com/nats-io/nats.go"
|
||||||
)
|
)
|
||||||
|
|
||||||
type NATSClient struct {
|
type NATSClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
router *Router
|
||||||
URL string
|
URL string
|
||||||
Subject string
|
Subject string
|
||||||
@@ -19,7 +20,7 @@ type NATSClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.nats.client",
|
Type: "net.nats.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
url, ok := params["url"]
|
url, ok := params["url"]
|
||||||
|
|
||||||
|
|||||||
@@ -7,10 +7,11 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/jwetzell/psn-go"
|
"github.com/jwetzell/psn-go"
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type PSNClient struct {
|
type PSNClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
router *Router
|
router *Router
|
||||||
decoder *psn.Decoder
|
decoder *psn.Decoder
|
||||||
@@ -19,7 +20,7 @@ type PSNClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.psn.client",
|
Type: "net.psn.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
|
|
||||||
return &PSNClient{config: config, decoder: psn.NewDecoder()}, nil
|
return &PSNClient{config: config, decoder: psn.NewDecoder()}, nil
|
||||||
},
|
},
|
||||||
|
|||||||
11
route.go
11
route.go
@@ -3,12 +3,13 @@ package showbridge
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/processing"
|
"github.com/jwetzell/showbridge-go/internal/processing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RouteError struct {
|
type RouteError struct {
|
||||||
Index int
|
Index int
|
||||||
Config RouteConfig
|
Config config.RouteConfig
|
||||||
Error error
|
Error error
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -20,13 +21,7 @@ type Route struct {
|
|||||||
router *Router
|
router *Router
|
||||||
}
|
}
|
||||||
|
|
||||||
type RouteConfig struct {
|
func NewRoute(index int, config config.RouteConfig, router *Router) (*Route, error) {
|
||||||
Input string `json:"input"`
|
|
||||||
Processors []processing.ProcessorConfig `json:"processors"`
|
|
||||||
Output string `json:"output"`
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewRoute(index int, config RouteConfig, router *Router) (*Route, error) {
|
|
||||||
processors := []processing.Processor{}
|
processors := []processing.Processor{}
|
||||||
|
|
||||||
if len(config.Processors) > 0 {
|
if len(config.Processors) > 0 {
|
||||||
|
|||||||
@@ -6,6 +6,8 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RoutingError struct {
|
type RoutingError struct {
|
||||||
@@ -21,7 +23,7 @@ type Router struct {
|
|||||||
moduleWait sync.WaitGroup
|
moduleWait sync.WaitGroup
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRouter(ctx context.Context, config Config) (*Router, []ModuleError, []RouteError) {
|
func NewRouter(ctx context.Context, config config.Config) (*Router, []ModuleError, []RouteError) {
|
||||||
|
|
||||||
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
|
||||||
Level: slog.LevelInfo,
|
Level: slog.LevelInfo,
|
||||||
|
|||||||
@@ -7,12 +7,13 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/framing"
|
"github.com/jwetzell/showbridge-go/internal/framing"
|
||||||
"go.bug.st/serial"
|
"go.bug.st/serial"
|
||||||
)
|
)
|
||||||
|
|
||||||
type SerialClient struct {
|
type SerialClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
router *Router
|
||||||
Port string
|
Port string
|
||||||
Framer framing.Framer
|
Framer framing.Framer
|
||||||
@@ -24,7 +25,7 @@ func init() {
|
|||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
//TODO(jwetzell): find a better namespace than "misc"
|
//TODO(jwetzell): find a better namespace than "misc"
|
||||||
Type: "misc.serial.client",
|
Type: "misc.serial.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
|
|
||||||
|
|||||||
@@ -6,11 +6,12 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/framing"
|
"github.com/jwetzell/showbridge-go/internal/framing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TCPClient struct {
|
type TCPClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
framer framing.Framer
|
framer framing.Framer
|
||||||
conn *net.TCPConn
|
conn *net.TCPConn
|
||||||
router *Router
|
router *Router
|
||||||
@@ -20,7 +21,7 @@ type TCPClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.tcp.client",
|
Type: "net.tcp.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
host, ok := params["host"]
|
host, ok := params["host"]
|
||||||
|
|
||||||
|
|||||||
@@ -10,11 +10,12 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/framing"
|
"github.com/jwetzell/showbridge-go/internal/framing"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TCPServer struct {
|
type TCPServer struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Addr *net.TCPAddr
|
Addr *net.TCPAddr
|
||||||
Framer framing.Framer
|
Framer framing.Framer
|
||||||
router *Router
|
router *Router
|
||||||
@@ -27,7 +28,7 @@ type TCPServer struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.tcp.server",
|
Type: "net.tcp.server",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
6
timer.go
6
timer.go
@@ -4,10 +4,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Timer struct {
|
type Timer struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Duration uint32
|
Duration uint32
|
||||||
router *Router
|
router *Router
|
||||||
timer *time.Timer
|
timer *time.Timer
|
||||||
@@ -16,7 +18,7 @@ type Timer struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "gen.timer",
|
Type: "gen.timer",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
duration, ok := params["duration"]
|
duration, ok := params["duration"]
|
||||||
|
|||||||
@@ -4,10 +4,12 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UDPClient struct {
|
type UDPClient struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
Addr *net.UDPAddr
|
Addr *net.UDPAddr
|
||||||
Port uint16
|
Port uint16
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
@@ -17,7 +19,7 @@ type UDPClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.client",
|
Type: "net.udp.client",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
host, ok := params["host"]
|
host, ok := params["host"]
|
||||||
|
|
||||||
|
|||||||
@@ -5,10 +5,12 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UDPMulticast struct {
|
type UDPMulticast struct {
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
conn *net.UDPConn
|
conn *net.UDPConn
|
||||||
router *Router
|
router *Router
|
||||||
Addr *net.UDPAddr
|
Addr *net.UDPAddr
|
||||||
@@ -17,7 +19,7 @@ type UDPMulticast struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.multicast",
|
Type: "net.udp.multicast",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
ip, ok := params["ip"]
|
ip, ok := params["ip"]
|
||||||
|
|
||||||
|
|||||||
@@ -6,18 +6,20 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net"
|
"net"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
type UDPServer struct {
|
type UDPServer struct {
|
||||||
Addr *net.UDPAddr
|
Addr *net.UDPAddr
|
||||||
config ModuleConfig
|
config config.ModuleConfig
|
||||||
router *Router
|
router *Router
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.server",
|
Type: "net.udp.server",
|
||||||
New: func(config ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
port, ok := params["port"]
|
port, ok := params["port"]
|
||||||
if !ok {
|
if !ok {
|
||||||
|
|||||||
Reference in New Issue
Block a user