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( var dispatcher = di.Options(
di.Provide(newDispatcher, di.Provide(newDispatcher,
di.As(new(d.Emitter)),
di.As(new(d.Subscriber)),
di.As(new(http.Emitter)), di.As(new(http.Emitter)),
di.As(new(mojangtextures.Emitter)), di.As(new(mojangtextures.Emitter)),
di.As(new(eventsubscribers.Subscriber)), di.As(new(eventsubscribers.Subscriber)),
@ -19,12 +21,12 @@ var dispatcher = di.Options(
di.Invoke(enableEventsHandlers), di.Invoke(enableEventsHandlers),
) )
func newDispatcher() d.EventDispatcher { func newDispatcher() d.Dispatcher {
return d.New() return d.New()
} }
func enableEventsHandlers( func enableEventsHandlers(
dispatcher d.EventDispatcher, dispatcher d.Subscriber,
logger slf.Logger, logger slf.Logger,
statsReporter slf.StatsReporter, statsReporter slf.StatsReporter,
) { ) {

View File

@ -2,12 +2,19 @@ package dispatcher
import "github.com/asaskevich/EventBus" import "github.com/asaskevich/EventBus"
// TODO: split on 2 interfaces and use them across the application type Subscriber interface {
type EventDispatcher interface {
Subscribe(topic string, fn interface{}) Subscribe(topic string, fn interface{})
}
type Emitter interface {
Emit(topic string, args ...interface{}) Emit(topic string, args ...interface{})
} }
type Dispatcher interface {
Subscriber
Emitter
}
type localEventDispatcher struct { type localEventDispatcher struct {
bus EventBus.Bus bus EventBus.Bus
} }
@ -20,7 +27,7 @@ func (d *localEventDispatcher) Emit(topic string, args ...interface{}) {
d.bus.Publish(topic, args...) d.bus.Publish(topic, args...)
} }
func New() EventDispatcher { func New() Dispatcher {
return &localEventDispatcher{ return &localEventDispatcher{
bus: EventBus.New(), bus: EventBus.New(),
} }

View File

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

View File

@ -3,37 +3,20 @@ package http
import ( import (
"context" "context"
"encoding/json" "encoding/json"
"net"
"net/http" "net/http"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
"time"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/mono83/slf" "github.com/mono83/slf"
"github.com/mono83/slf/wd" "github.com/mono83/slf/wd"
"github.com/elyby/chrly/dispatcher"
) )
type Emitter interface { type Emitter interface {
Emit(name string, args ...interface{}) dispatcher.Emitter
}
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)
} }
func StartServer(server *http.Server, logger slf.Logger) { func StartServer(server *http.Server, logger slf.Logger) {

View File

@ -7,6 +7,7 @@ import (
"sync" "sync"
"github.com/elyby/chrly/api/mojang" "github.com/elyby/chrly/api/mojang"
"github.com/elyby/chrly/dispatcher"
) )
type broadcastResult struct { type broadcastResult struct {
@ -72,7 +73,7 @@ type TexturesProvider interface {
} }
type Emitter interface { type Emitter interface {
Emit(name string, args ...interface{}) dispatcher.Emitter
} }
type Provider struct { type Provider struct {