mirror of
https://github.com/jwetzell/showbridge-go.git
synced 2026-04-26 12:55:29 +00:00
move module interface to common package
This commit is contained in:
13
internal/common/module.go
Normal file
13
internal/common/module.go
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Module interface {
|
||||||
|
Id() string
|
||||||
|
Type() string
|
||||||
|
Start(context.Context) error
|
||||||
|
Stop()
|
||||||
|
Output(context.Context, any) error
|
||||||
|
}
|
||||||
@@ -56,7 +56,7 @@ func (hsrw *HTTPServerResponseWriter) Write(data []byte) (int, error) {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "http.server",
|
Type: "http.server",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
portNum, err := params.GetInt("port")
|
portNum, err := params.GetInt("port")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ type MIDIInput struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "midi.input",
|
Type: "midi.input",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
portString, err := params.GetString("port")
|
portString, err := params.GetString("port")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ type MIDIOutput struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "midi.output",
|
Type: "midi.output",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
portString, err := params.GetString("port")
|
portString, err := params.GetString("port")
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
package module
|
package module
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/common"
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -15,18 +15,9 @@ type ModuleError struct {
|
|||||||
Error string `json:"error"`
|
Error string `json:"error"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Module interface {
|
|
||||||
Id() string
|
|
||||||
Type() string
|
|
||||||
Start(context.Context) error
|
|
||||||
Stop()
|
|
||||||
Output(context.Context, any) error
|
|
||||||
Get(key string) (any, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
type ModuleRegistration struct {
|
type ModuleRegistration struct {
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
New func(config.ModuleConfig) (Module, error)
|
New func(config.ModuleConfig) (common.Module, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
func RegisterModule(mod ModuleRegistration) {
|
func RegisterModule(mod ModuleRegistration) {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ type MQTTClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "mqtt.client",
|
Type: "mqtt.client",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
brokerString, err := params.GetString("broker")
|
brokerString, err := params.GetString("broker")
|
||||||
|
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type NATSClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "nats.client",
|
Type: "nats.client",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
urlString, err := params.GetString("url")
|
urlString, err := params.GetString("url")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ type NATSServer struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "nats.server",
|
Type: "nats.server",
|
||||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||||
params := moduleConfig.Params
|
params := moduleConfig.Params
|
||||||
portNum, err := params.GetInt("port")
|
portNum, err := params.GetInt("port")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ type PSNClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "psn.client",
|
Type: "psn.client",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
|
|
||||||
return &PSNClient{config: config, decoder: psn.NewDecoder(), logger: CreateLogger(config)}, nil
|
return &PSNClient{config: config, decoder: psn.NewDecoder(), logger: CreateLogger(config)}, nil
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type RedisClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "redis.client",
|
Type: "redis.client",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
hostString, err := params.GetString("host")
|
hostString, err := params.GetString("host")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ type SerialClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "serial.client",
|
Type: "serial.client",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
portString, err := params.GetString("port")
|
portString, err := params.GetString("port")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ type sipCallContextKey string
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "sip.call.server",
|
Type: "sip.call.server",
|
||||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||||
params := moduleConfig.Params
|
params := moduleConfig.Params
|
||||||
portNum, err := params.GetInt("port")
|
portNum, err := params.GetInt("port")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ type SIPDTMFCall struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "sip.dtmf.server",
|
Type: "sip.dtmf.server",
|
||||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||||
params := moduleConfig.Params
|
params := moduleConfig.Params
|
||||||
|
|
||||||
portNum, err := params.GetInt("port")
|
portNum, err := params.GetInt("port")
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ type TCPClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.tcp.client",
|
Type: "net.tcp.client",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
hostString, err := params.GetString("host")
|
hostString, err := params.GetString("host")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -33,7 +33,7 @@ type TCPServer struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.tcp.server",
|
Type: "net.tcp.server",
|
||||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||||
params := moduleConfig.Params
|
params := moduleConfig.Params
|
||||||
portNum, err := params.GetInt("port")
|
portNum, err := params.GetInt("port")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/jwetzell/showbridge-go/internal/common"
|
||||||
"github.com/jwetzell/showbridge-go/internal/config"
|
"github.com/jwetzell/showbridge-go/internal/config"
|
||||||
"github.com/jwetzell/showbridge-go/internal/module"
|
"github.com/jwetzell/showbridge-go/internal/module"
|
||||||
)
|
)
|
||||||
@@ -43,7 +44,7 @@ func TestModuleBadRegistrationNoType(t *testing.T) {
|
|||||||
|
|
||||||
module.RegisterModule(module.ModuleRegistration{
|
module.RegisterModule(module.ModuleRegistration{
|
||||||
Type: "",
|
Type: "",
|
||||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
return &TestModule{}, nil
|
return &TestModule{}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@@ -71,14 +72,14 @@ func TestModuleBadRegistrationExistingType(t *testing.T) {
|
|||||||
|
|
||||||
module.RegisterModule(module.ModuleRegistration{
|
module.RegisterModule(module.ModuleRegistration{
|
||||||
Type: "module.test",
|
Type: "module.test",
|
||||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
return &TestModule{}, nil
|
return &TestModule{}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
module.RegisterModule(module.ModuleRegistration{
|
module.RegisterModule(module.ModuleRegistration{
|
||||||
Type: "module.test",
|
Type: "module.test",
|
||||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
return &TestModule{}, nil
|
return &TestModule{}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type TimeInterval struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "time.interval",
|
Type: "time.interval",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
durationInt, err := params.GetInt("duration")
|
durationInt, err := params.GetInt("duration")
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ type TimeTimer struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "time.timer",
|
Type: "time.timer",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
|
|
||||||
durationNum, err := params.GetInt("duration")
|
durationNum, err := params.GetInt("duration")
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type UDPClient struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.client",
|
Type: "net.udp.client",
|
||||||
New: func(config config.ModuleConfig) (Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
params := config.Params
|
params := config.Params
|
||||||
hostString, err := params.GetString("host")
|
hostString, err := params.GetString("host")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type UDPMulticast struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.multicast",
|
Type: "net.udp.multicast",
|
||||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||||
params := moduleConfig.Params
|
params := moduleConfig.Params
|
||||||
ipString, err := params.GetString("ip")
|
ipString, err := params.GetString("ip")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type UDPServer struct {
|
|||||||
func init() {
|
func init() {
|
||||||
RegisterModule(ModuleRegistration{
|
RegisterModule(ModuleRegistration{
|
||||||
Type: "net.udp.server",
|
Type: "net.udp.server",
|
||||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||||
params := moduleConfig.Params
|
params := moduleConfig.Params
|
||||||
portNum, err := params.GetInt("port")
|
portNum, err := params.GetInt("port")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ type Router struct {
|
|||||||
contextCancel context.CancelFunc
|
contextCancel context.CancelFunc
|
||||||
Context context.Context
|
Context context.Context
|
||||||
// TODO(jwetzell): do these need to be guarded against concurrency?
|
// TODO(jwetzell): do these need to be guarded against concurrency?
|
||||||
ModuleInstances map[string]module.Module
|
ModuleInstances map[string]common.Module
|
||||||
// TODO(jwetzell): change to something easier to lookup
|
// TODO(jwetzell): change to something easier to lookup
|
||||||
RouteInstances []*route.Route
|
RouteInstances []*route.Route
|
||||||
ConfigChange chan config.Config
|
ConfigChange chan config.Config
|
||||||
@@ -106,7 +106,7 @@ func (r *Router) addRoute(routeDecl config.RouteConfig) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Router) getModule(moduleId string) module.Module {
|
func (r *Router) getModule(moduleId string) common.Module {
|
||||||
moduleInstance, ok := r.ModuleInstances[moduleId]
|
moduleInstance, ok := r.ModuleInstances[moduleId]
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil
|
return nil
|
||||||
@@ -117,7 +117,7 @@ func (r *Router) getModule(moduleId string) module.Module {
|
|||||||
func NewRouter(routerConfig config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
func NewRouter(routerConfig config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
||||||
|
|
||||||
router := Router{
|
router := Router{
|
||||||
ModuleInstances: make(map[string]module.Module),
|
ModuleInstances: make(map[string]common.Module),
|
||||||
RouteInstances: []*route.Route{},
|
RouteInstances: []*route.Route{},
|
||||||
ConfigChange: make(chan config.Config, 1),
|
ConfigChange: make(chan config.Config, 1),
|
||||||
logger: slog.Default().With("component", "router"),
|
logger: slog.Default().With("component", "router"),
|
||||||
@@ -324,7 +324,7 @@ func (r *Router) UpdateConfig(newConfig config.Config) ([]module.ModuleError, []
|
|||||||
r.logger.Debug("waiting for modules to exit")
|
r.logger.Debug("waiting for modules to exit")
|
||||||
r.moduleWait.Wait()
|
r.moduleWait.Wait()
|
||||||
|
|
||||||
r.ModuleInstances = make(map[string]module.Module)
|
r.ModuleInstances = make(map[string]common.Module)
|
||||||
r.RouteInstances = []*route.Route{}
|
r.RouteInstances = []*route.Route{}
|
||||||
|
|
||||||
var moduleErrors []module.ModuleError
|
var moduleErrors []module.ModuleError
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ func (mcm *MockCounterModule) Stop() {
|
|||||||
func init() {
|
func init() {
|
||||||
module.RegisterModule(module.ModuleRegistration{
|
module.RegisterModule(module.ModuleRegistration{
|
||||||
Type: "mock.counter",
|
Type: "mock.counter",
|
||||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||||
return &MockCounterModule{config: config, logger: slog.Default()}, nil
|
return &MockCounterModule{config: config, logger: slog.Default()}, nil
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user