mirror of
https://github.com/elyby/chrly.git
synced 2024-11-26 08:42:14 +05:30
Split Dispatcher interface and use it across application
This commit is contained in:
parent
f58b980948
commit
2ea4c55d37
@ -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,
|
||||
) {
|
||||
|
@ -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(),
|
||||
}
|
||||
|
@ -1,5 +1,7 @@
|
||||
package eventsubscribers
|
||||
|
||||
import "github.com/elyby/chrly/dispatcher"
|
||||
|
||||
type Subscriber interface {
|
||||
Subscribe(topic string, fn interface{})
|
||||
dispatcher.Subscriber
|
||||
}
|
||||
|
23
http/http.go
23
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) {
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user