From 092ea3d4e2c34e24420990f5aa98bae5f33eb6f6 Mon Sep 17 00:00:00 2001 From: ErickSkrauch Date: Mon, 6 Apr 2020 17:15:40 +0300 Subject: [PATCH] Resolves #13. Add a config param to completely disable Mojang textures provider --- CHANGELOG.md | 1 + README.md | 7 +++ bootstrap/bootstrap.go | 3 +- cmd/serve.go | 55 ++++++++++++---------- mojangtextures/nil_mojang_textures.go | 12 +++++ mojangtextures/nil_mojang_textures_test.go | 14 ++++++ 6 files changed, 67 insertions(+), 25 deletions(-) create mode 100644 mojangtextures/nil_mojang_textures.go create mode 100644 mojangtextures/nil_mojang_textures_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 23b709a..62d7d2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] - xxxx-xx-xx ### Added +- Mojang textures queue now can be completely disabled via `MOJANG_TEXTURES_ENABLED` param. - Remote mode for Mojang's textures queue with a new configuration params: `MOJANG_TEXTURES_UUIDS_PROVIDER_DRIVER` and `MOJANG_TEXTURES_UUIDS_PROVIDER_URL`. diff --git a/README.md b/README.md index 061a471..b91631c 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,13 @@ docker-compose up -d app 10 + + MOJANG_TEXTURES_ENABLED + + Allows to completely disable Mojang textures provider for unknown usernames. Enabled by default. + + true + MOJANG_TEXTURES_UUIDS_PROVIDER_DRIVER diff --git a/bootstrap/bootstrap.go b/bootstrap/bootstrap.go index 9295b28..143b6bf 100644 --- a/bootstrap/bootstrap.go +++ b/bootstrap/bootstrap.go @@ -1,6 +1,7 @@ package bootstrap import ( + "fmt" "net/url" "os" "time" @@ -79,7 +80,7 @@ func CreateMojangUUIDsProvider(emitter http.Emitter) (mojangtextures.UUIDsProvid if preferredUuidsProvider == "remote" { remoteUrl, err := url.Parse(viper.GetString("mojang_textures.uuids_provider.url")) if err != nil { - return nil, err + return nil, fmt.Errorf("Unable to parse remote url: %w", err) } uuidsProvider = &mojangtextures.RemoteApiUuidsProvider{ diff --git a/cmd/serve.go b/cmd/serve.go index 425f758..d1fb400 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -62,33 +62,39 @@ var serveCmd = &cobra.Command{ } logger.Info("Capes repository successfully initialized") - logger.Info("Preparing Mojang's textures queue") - mojangUuidsRepository, err := redisFactory.CreateMojangUuidsRepository() - if err != nil { - logger.Emergency("Error on creating mojang uuids repo: :err", wd.ErrParam(err)) - os.Exit(1) - } + var mojangTexturesProvider http.MojangTexturesProvider + if viper.GetBool("mojang_textures.enabled") { + logger.Info("Preparing Mojang's textures queue") + mojangUuidsRepository, err := redisFactory.CreateMojangUuidsRepository() + if err != nil { + logger.Emergency("Error on creating mojang uuids repo: :err", wd.ErrParam(err)) + os.Exit(1) + } - uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(dispatcher) - if err != nil { - logger.Emergency("Unable to parse remote url :err", wd.ErrParam(err)) - os.Exit(1) - } + uuidsProvider, err := bootstrap.CreateMojangUUIDsProvider(dispatcher) + if err != nil { + logger.Emergency("Unable to create mojang uuids provider: :err", wd.ErrParam(err)) + os.Exit(1) + } - texturesStorage := mojangtextures.NewInMemoryTexturesStorage() - texturesStorage.Start() - mojangTexturesProvider := &mojangtextures.Provider{ - Emitter: dispatcher, - UUIDsProvider: uuidsProvider, - TexturesProvider: &mojangtextures.MojangApiTexturesProvider{ - Emitter: dispatcher, - }, - Storage: &mojangtextures.SeparatedStorage{ - UuidsStorage: mojangUuidsRepository, - TexturesStorage: texturesStorage, - }, + texturesStorage := mojangtextures.NewInMemoryTexturesStorage() + texturesStorage.Start() + mojangTexturesProvider = &mojangtextures.Provider{ + Emitter: dispatcher, + UUIDsProvider: uuidsProvider, + TexturesProvider: &mojangtextures.MojangApiTexturesProvider{ + Emitter: dispatcher, + }, + Storage: &mojangtextures.SeparatedStorage{ + UuidsStorage: mojangUuidsRepository, + TexturesStorage: texturesStorage, + }, + } + logger.Info("Mojang's textures queue is successfully initialized") + } else { + logger.Info("Mojang's textures queue is disabled") + mojangTexturesProvider = &mojangtextures.NilProvider{} } - logger.Info("Mojang's textures queue is successfully initialized") address := fmt.Sprintf("%s:%d", viper.GetString("server.host"), viper.GetInt("server.port")) handler := (&http.Skinsystem{ @@ -129,4 +135,5 @@ func init() { viper.SetDefault("storage.redis.poll", 10) viper.SetDefault("storage.filesystem.basePath", "data") viper.SetDefault("storage.filesystem.capesDirName", "capes") + viper.SetDefault("mojang_textures.enabled", true) } diff --git a/mojangtextures/nil_mojang_textures.go b/mojangtextures/nil_mojang_textures.go new file mode 100644 index 0000000..30c9f98 --- /dev/null +++ b/mojangtextures/nil_mojang_textures.go @@ -0,0 +1,12 @@ +package mojangtextures + +import ( + "github.com/elyby/chrly/api/mojang" +) + +type NilProvider struct { +} + +func (p *NilProvider) GetForUsername(username string) (*mojang.SignedTexturesResponse, error) { + return nil, nil +} diff --git a/mojangtextures/nil_mojang_textures_test.go b/mojangtextures/nil_mojang_textures_test.go new file mode 100644 index 0000000..c3cd769 --- /dev/null +++ b/mojangtextures/nil_mojang_textures_test.go @@ -0,0 +1,14 @@ +package mojangtextures + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestNilProvider_GetForUsername(t *testing.T) { + provider := &NilProvider{} + result, err := provider.GetForUsername("username") + assert.Nil(t, result) + assert.Nil(t, err) +}