From db728451f86e680ab51e95490f051d15f333ccb8 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Sat, 8 Feb 2020 14:31:47 +0300 Subject: [PATCH] Implemented event dispatcher --- Gopkg.lock | 9 +++++++++ Gopkg.toml | 4 ++++ bootstrap/bootstrap.go | 7 ++++++- cmd/serve.go | 9 ++++++--- cmd/worker.go | 6 ++++-- dispatcher/dispatcher.go | 26 ++++++++++++++++++++++++++ 6 files changed, 55 insertions(+), 6 deletions(-) create mode 100644 dispatcher/dispatcher.go diff --git a/Gopkg.lock b/Gopkg.lock index 8416f5d..1c46e89 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -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", diff --git a/Gopkg.toml b/Gopkg.toml index a260730..e851485 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -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]] diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 39a4b51..b93f540 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -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() +} diff --git a/cmd/serve.go b/cmd/serve.go index 010b8a2..5d96bae 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -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, diff --git a/cmd/worker.go b/cmd/worker.go index 8f3f22a..53b9f61 100644 --- a/cmd/worker.go +++ b/cmd/worker.go @@ -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) diff --git a/dispatcher/dispatcher.go b/dispatcher/dispatcher.go new file mode 100644 index 0000000..e8d1515 --- /dev/null +++ b/dispatcher/dispatcher.go @@ -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(), + } +}