diff --git a/di/dispatcher.go b/di/dispatcher.go index 6a59e11..13416bd 100644 --- a/di/dispatcher.go +++ b/di/dispatcher.go @@ -12,6 +12,8 @@ import ( var dispatcher = di.Options( di.Provide(newDispatcher, + di.As(new(d.Emitter)), + di.As(new(d.Subscriber)), di.As(new(http.Emitter)), di.As(new(mojangtextures.Emitter)), di.As(new(eventsubscribers.Subscriber)), @@ -19,12 +21,12 @@ var dispatcher = di.Options( di.Invoke(enableEventsHandlers), ) -func newDispatcher() d.EventDispatcher { +func newDispatcher() d.Dispatcher { return d.New() } func enableEventsHandlers( - dispatcher d.EventDispatcher, + dispatcher d.Subscriber, logger slf.Logger, statsReporter slf.StatsReporter, ) { diff --git a/dispatcher/dispatcher.go b/dispatcher/dispatcher.go index b20872b..4c692cd 100644 --- a/dispatcher/dispatcher.go +++ b/dispatcher/dispatcher.go @@ -2,12 +2,19 @@ package dispatcher import "github.com/asaskevich/EventBus" -// TODO: split on 2 interfaces and use them across the application -type EventDispatcher interface { +type Subscriber interface { Subscribe(topic string, fn interface{}) +} + +type Emitter interface { Emit(topic string, args ...interface{}) } +type Dispatcher interface { + Subscriber + Emitter +} + type localEventDispatcher struct { bus EventBus.Bus } @@ -20,7 +27,7 @@ func (d *localEventDispatcher) Emit(topic string, args ...interface{}) { d.bus.Publish(topic, args...) } -func New() EventDispatcher { +func New() Dispatcher { return &localEventDispatcher{ bus: EventBus.New(), } diff --git a/eventsubscribers/subscriber.go b/eventsubscribers/subscriber.go index 8b64092..00aea01 100644 --- a/eventsubscribers/subscriber.go +++ b/eventsubscribers/subscriber.go @@ -1,5 +1,7 @@ package eventsubscribers +import "github.com/elyby/chrly/dispatcher" + type Subscriber interface { - Subscribe(topic string, fn interface{}) + dispatcher.Subscriber } diff --git a/http/http.go b/http/http.go index ef02d34..b6e373a 100644 --- a/http/http.go +++ b/http/http.go @@ -3,37 +3,20 @@ package http import ( "context" "encoding/json" - "net" "net/http" "os" "os/signal" "strings" - "time" "github.com/gorilla/mux" "github.com/mono83/slf" "github.com/mono83/slf/wd" + + "github.com/elyby/chrly/dispatcher" ) type Emitter interface { - Emit(name string, args ...interface{}) -} - -func Serve(address string, handler http.Handler) error { - listener, err := net.Listen("tcp", address) - if err != nil { - return err - } - - server := &http.Server{ - ReadTimeout: 5 * time.Second, - WriteTimeout: 5 * time.Second, - IdleTimeout: 60 * time.Second, - MaxHeaderBytes: 1 << 16, - Handler: handler, - } - - return server.Serve(listener) + dispatcher.Emitter } func StartServer(server *http.Server, logger slf.Logger) { diff --git a/mojangtextures/mojang_textures.go b/mojangtextures/mojang_textures.go index 08885c3..205431c 100644 --- a/mojangtextures/mojang_textures.go +++ b/mojangtextures/mojang_textures.go @@ -7,6 +7,7 @@ import ( "sync" "github.com/elyby/chrly/api/mojang" + "github.com/elyby/chrly/dispatcher" ) type broadcastResult struct { @@ -72,7 +73,7 @@ type TexturesProvider interface { } type Emitter interface { - Emit(name string, args ...interface{}) + dispatcher.Emitter } type Provider struct {