Split Dispatcher interface and use it across application

This commit is contained in:
ErickSkrauch 2020-04-20 14:29:33 +03:00
parent f58b980948
commit 2ea4c55d37
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
5 changed files with 22 additions and 27 deletions

View File

@ -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,
) {

View File

@ -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(),
}

View File

@ -1,5 +1,7 @@
package eventsubscribers
import "github.com/elyby/chrly/dispatcher"
type Subscriber interface {
Subscribe(topic string, fn interface{})
dispatcher.Subscriber
}

View File

@ -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) {

View File

@ -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 {