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() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "http.server",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
|
||||
@@ -27,7 +27,7 @@ type MIDIInput struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "midi.input",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
portString, err := params.GetString("port")
|
||||
if err != nil {
|
||||
|
||||
@@ -27,7 +27,7 @@ type MIDIOutput struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "midi.output",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
|
||||
portString, err := params.GetString("port")
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
package module
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sync"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
)
|
||||
|
||||
@@ -15,18 +15,9 @@ type ModuleError struct {
|
||||
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 string `json:"type"`
|
||||
New func(config.ModuleConfig) (Module, error)
|
||||
New func(config.ModuleConfig) (common.Module, error)
|
||||
}
|
||||
|
||||
func RegisterModule(mod ModuleRegistration) {
|
||||
|
||||
@@ -26,7 +26,7 @@ type MQTTClient struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "mqtt.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
brokerString, err := params.GetString("broker")
|
||||
|
||||
|
||||
@@ -25,7 +25,7 @@ type NATSClient struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "nats.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
urlString, err := params.GetString("url")
|
||||
if err != nil {
|
||||
|
||||
@@ -27,7 +27,7 @@ type NATSServer struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "nats.server",
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||
params := moduleConfig.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
|
||||
@@ -26,7 +26,7 @@ type PSNClient struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
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
|
||||
},
|
||||
|
||||
@@ -25,7 +25,7 @@ type RedisClient struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "redis.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
hostString, err := params.GetString("host")
|
||||
if err != nil {
|
||||
|
||||
@@ -30,7 +30,7 @@ type SerialClient struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "serial.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
portString, err := params.GetString("port")
|
||||
if err != nil {
|
||||
|
||||
@@ -46,7 +46,7 @@ type sipCallContextKey string
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.call.server",
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||
params := moduleConfig.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
|
||||
@@ -46,7 +46,7 @@ type SIPDTMFCall struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "sip.dtmf.server",
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||
params := moduleConfig.Params
|
||||
|
||||
portNum, err := params.GetInt("port")
|
||||
|
||||
@@ -27,7 +27,7 @@ type TCPClient struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
hostString, err := params.GetString("host")
|
||||
if err != nil {
|
||||
|
||||
@@ -33,7 +33,7 @@ type TCPServer struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.tcp.server",
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||
params := moduleConfig.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/jwetzell/showbridge-go/internal/common"
|
||||
"github.com/jwetzell/showbridge-go/internal/config"
|
||||
"github.com/jwetzell/showbridge-go/internal/module"
|
||||
)
|
||||
@@ -43,7 +44,7 @@ func TestModuleBadRegistrationNoType(t *testing.T) {
|
||||
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "",
|
||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
return &TestModule{}, nil
|
||||
},
|
||||
})
|
||||
@@ -71,14 +72,14 @@ func TestModuleBadRegistrationExistingType(t *testing.T) {
|
||||
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "module.test",
|
||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
return &TestModule{}, nil
|
||||
},
|
||||
})
|
||||
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
Type: "module.test",
|
||||
New: func(config config.ModuleConfig) (module.Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
return &TestModule{}, nil
|
||||
},
|
||||
})
|
||||
|
||||
@@ -24,7 +24,7 @@ type TimeInterval struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "time.interval",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
|
||||
durationInt, err := params.GetInt("duration")
|
||||
|
||||
@@ -24,7 +24,7 @@ type TimeTimer struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "time.timer",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
|
||||
durationNum, err := params.GetInt("duration")
|
||||
|
||||
@@ -25,7 +25,7 @@ type UDPClient struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.client",
|
||||
New: func(config config.ModuleConfig) (Module, error) {
|
||||
New: func(config config.ModuleConfig) (common.Module, error) {
|
||||
params := config.Params
|
||||
hostString, err := params.GetString("host")
|
||||
if err != nil {
|
||||
|
||||
@@ -25,7 +25,7 @@ type UDPMulticast struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.multicast",
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||
params := moduleConfig.Params
|
||||
ipString, err := params.GetString("ip")
|
||||
if err != nil {
|
||||
|
||||
@@ -25,7 +25,7 @@ type UDPServer struct {
|
||||
func init() {
|
||||
RegisterModule(ModuleRegistration{
|
||||
Type: "net.udp.server",
|
||||
New: func(moduleConfig config.ModuleConfig) (Module, error) {
|
||||
New: func(moduleConfig config.ModuleConfig) (common.Module, error) {
|
||||
params := moduleConfig.Params
|
||||
portNum, err := params.GetInt("port")
|
||||
if err != nil {
|
||||
|
||||
@@ -25,7 +25,7 @@ type Router struct {
|
||||
contextCancel context.CancelFunc
|
||||
Context context.Context
|
||||
// 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
|
||||
RouteInstances []*route.Route
|
||||
ConfigChange chan config.Config
|
||||
@@ -106,7 +106,7 @@ func (r *Router) addRoute(routeDecl config.RouteConfig) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Router) getModule(moduleId string) module.Module {
|
||||
func (r *Router) getModule(moduleId string) common.Module {
|
||||
moduleInstance, ok := r.ModuleInstances[moduleId]
|
||||
if !ok {
|
||||
return nil
|
||||
@@ -117,7 +117,7 @@ func (r *Router) getModule(moduleId string) module.Module {
|
||||
func NewRouter(routerConfig config.Config) (*Router, []module.ModuleError, []route.RouteError) {
|
||||
|
||||
router := Router{
|
||||
ModuleInstances: make(map[string]module.Module),
|
||||
ModuleInstances: make(map[string]common.Module),
|
||||
RouteInstances: []*route.Route{},
|
||||
ConfigChange: make(chan config.Config, 1),
|
||||
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.moduleWait.Wait()
|
||||
|
||||
r.ModuleInstances = make(map[string]module.Module)
|
||||
r.ModuleInstances = make(map[string]common.Module)
|
||||
r.RouteInstances = []*route.Route{}
|
||||
|
||||
var moduleErrors []module.ModuleError
|
||||
|
||||
@@ -65,7 +65,7 @@ func (mcm *MockCounterModule) Stop() {
|
||||
func init() {
|
||||
module.RegisterModule(module.ModuleRegistration{
|
||||
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
|
||||
},
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user