Implemented event dispatcher

This commit is contained in:
ErickSkrauch 2020-02-08 14:31:47 +03:00
parent 2abe2db469
commit db728451f8
No known key found for this signature in database
GPG Key ID: 669339FCBB30EE0E
6 changed files with 55 additions and 6 deletions

9
Gopkg.lock generated
View File

@ -14,6 +14,14 @@
revision = "f6df55f235c24f236d11dbcf665249a59ac2021f"
version = "1.1"
[[projects]]
branch = "master"
digest = "1:33c6f6a0ee7d594d07ad9b67e93266f8a859ab2ddd882f8be19207162f40cce8"
name = "github.com/asaskevich/EventBus"
packages = ["."]
pruneopts = ""
revision = "d46933a94f05c6657d7b923fcf5ac563ee37ec79"
[[projects]]
digest = "1:c7b11da9bf0707e6920e1b361fbbbbe9b277ef3a198377baa4527f6e31049be0"
name = "github.com/certifi/gocertifi"
@ -301,6 +309,7 @@
input-imports = [
"github.com/SermoDigital/jose/crypto",
"github.com/SermoDigital/jose/jws",
"github.com/asaskevich/EventBus",
"github.com/getsentry/raven-go",
"github.com/gorilla/mux",
"github.com/h2non/gock",

View File

@ -36,6 +36,10 @@ ignored = ["github.com/elyby/chrly"]
name = "github.com/tevino/abool"
branch = "master"
[[constraint]]
branch = "master"
name = "github.com/asaskevich/EventBus"
# Testing dependencies
[[constraint]]

View File

@ -1,7 +1,6 @@
package bootstrap
import (
"github.com/elyby/chrly/http"
"net/url"
"os"
"time"
@ -14,6 +13,8 @@ import (
"github.com/mono83/slf/wd"
"github.com/spf13/viper"
"github.com/elyby/chrly/dispatcher"
"github.com/elyby/chrly/http"
"github.com/elyby/chrly/mojangtextures"
"github.com/elyby/chrly/version"
)
@ -93,3 +94,7 @@ func CreateMojangUUIDsProvider(emitter http.Emitter) (mojangtextures.UUIDsProvid
return uuidsProvider, nil
}
func CreateEventDispatcher() dispatcher.EventDispatcher {
return dispatcher.New()
}

View File

@ -26,6 +26,8 @@ var serveCmd = &cobra.Command{
}
logger.Info("Logger successfully initialized")
dispatcher := bootstrap.CreateEventDispatcher()
storageFactory := db.StorageFactory{Config: viper.GetViper()}
logger.Info("Initializing skins repository")
@ -53,7 +55,7 @@ var serveCmd = &cobra.Command{
return
}
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(nil)
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(dispatcher)
if err != nil {
logger.Emergency("Unable to parse remote url :err", wd.ErrParam(err))
return
@ -62,10 +64,10 @@ var serveCmd = &cobra.Command{
texturesStorage := mojangtextures.NewInMemoryTexturesStorage()
texturesStorage.Start()
mojangTexturesProvider := &mojangtextures.Provider{
// TODO: configure emitter
Emitter: dispatcher,
UUIDsProvider: uuidsProvider,
TexturesProvider: &mojangtextures.MojangApiTexturesProvider{
// TODO: configure emitter
Emitter: dispatcher,
},
Storage: &mojangtextures.SeparatedStorage{
UuidsStorage: mojangUuidsRepository,
@ -76,6 +78,7 @@ var serveCmd = &cobra.Command{
address := fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port"))
handler := (&http.Skinsystem{
Emitter: dispatcher,
SkinsRepo: skinsRepo,
CapesRepo: capesRepo,
MojangTexturesProvider: mojangTexturesProvider,

View File

@ -22,7 +22,9 @@ var workerCmd = &cobra.Command{
}
logger.Info("Logger successfully initialized")
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(logger)
dispatcher := bootstrap.CreateEventDispatcher()
uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(dispatcher)
if err != nil {
logger.Emergency("Unable to parse remote url :err", wd.ErrParam(err))
return
@ -30,8 +32,8 @@ var workerCmd = &cobra.Command{
address := fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port"))
handler := (&http.UUIDsWorker{
Emitter: dispatcher,
UUIDsProvider: uuidsProvider,
// TODO: configure emitter
}).CreateHandler()
finishChan := make(chan bool)

26
dispatcher/dispatcher.go Normal file
View File

@ -0,0 +1,26 @@
package dispatcher
import "github.com/asaskevich/EventBus"
type EventDispatcher interface {
Subscribe(name string, fn interface{})
Emit(name string, args ...interface{})
}
type LocalEventDispatcher struct {
bus EventBus.Bus
}
func (d *LocalEventDispatcher) Subscribe(name string, fn interface{}) {
_ = d.bus.Subscribe(name, fn)
}
func (d *LocalEventDispatcher) Emit(name string, args ...interface{}) {
d.bus.Publish(name, args...)
}
func New() EventDispatcher {
return &LocalEventDispatcher{
bus: EventBus.New(),
}
}