mirror of
https://github.com/elyby/chrly.git
synced 2024-12-22 13:09:53 +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(
|
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,
|
||||||
) {
|
) {
|
||||||
|
@ -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(),
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
}
|
}
|
||||||
|
23
http/http.go
23
http/http.go
@ -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) {
|
||||||
|
@ -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 {
|
||||||
|
Loading…
Reference in New Issue
Block a user