chrly/internal/di/mojang.go

103 lines
2.6 KiB
Go
Raw Normal View History

2020-04-16 22:12:38 +05:30
package di
import (
"net/http"
2020-04-16 22:12:38 +05:30
"net/url"
"time"
2020-04-16 22:12:38 +05:30
2023-12-13 21:59:12 +05:30
"github.com/defval/di"
2020-04-16 22:12:38 +05:30
"github.com/spf13/viper"
"ely.by/chrly/internal/mojang"
"ely.by/chrly/internal/profiles"
2020-04-16 22:12:38 +05:30
)
var mojangDiOptions = di.Options(
di.Provide(newMojangApi),
2020-04-16 22:12:38 +05:30
di.Provide(newMojangTexturesProviderFactory),
di.Provide(newMojangTexturesProvider),
di.Provide(newMojangTexturesUuidsProviderFactory),
di.Provide(newMojangTexturesBatchUUIDsProvider),
2020-04-16 22:12:38 +05:30
di.Provide(newMojangSignedTexturesProvider),
)
2024-02-19 18:24:12 +05:30
func newMojangApi(config *viper.Viper, httpClient *http.Client) (*mojang.MojangApi, error) {
batchUuidsUrl := config.GetString("mojang.batch_uuids_url")
if batchUuidsUrl != "" {
if _, err := url.ParseRequestURI(batchUuidsUrl); err != nil {
return nil, err
}
}
profileUrl := config.GetString("mojang.profile_url")
if profileUrl != "" {
if _, err := url.ParseRequestURI(batchUuidsUrl); err != nil {
return nil, err
}
}
return mojang.NewMojangApi(httpClient, batchUuidsUrl, profileUrl), nil
}
2020-04-16 22:12:38 +05:30
func newMojangTexturesProviderFactory(
container *di.Container,
config *viper.Viper,
) (profiles.MojangProfilesProvider, error) {
config.SetDefault("mojang_textures.enabled", true)
2020-04-16 22:12:38 +05:30
if !config.GetBool("mojang_textures.enabled") {
return &mojang.NilProvider{}, nil
2020-04-16 22:12:38 +05:30
}
var provider *mojang.MojangTexturesProvider
2020-04-16 22:12:38 +05:30
err := container.Resolve(&provider)
if err != nil {
return nil, err
}
return provider, nil
}
func newMojangTexturesProvider(
uuidsProvider mojang.UuidsProvider,
texturesProvider mojang.TexturesProvider,
2024-02-19 18:24:12 +05:30
) (*mojang.MojangTexturesProvider, error) {
return mojang.NewMojangTexturesProvider(
uuidsProvider,
texturesProvider,
)
2020-04-16 22:12:38 +05:30
}
func newMojangTexturesUuidsProviderFactory(
batchProvider *mojang.BatchUuidsProvider,
uuidsStorage mojang.MojangUuidsStorage,
2024-02-19 18:24:12 +05:30
) (mojang.UuidsProvider, error) {
return mojang.NewUuidsProviderWithCache(batchProvider, uuidsStorage)
}
func newMojangTexturesBatchUUIDsProvider(
mojangApi *mojang.MojangApi,
config *viper.Viper,
) (*mojang.BatchUuidsProvider, error) {
config.SetDefault("queue.loop_delay", 2*time.Second+500*time.Millisecond)
config.SetDefault("queue.batch_size", 10)
config.SetDefault("queue.strategy", "periodic")
// TODO: healthcheck is broken
2024-02-19 18:24:12 +05:30
return mojang.NewBatchUuidsProvider(
mojangApi.UsernamesToUuids,
config.GetInt("queue.batch_size"),
config.GetDuration("queue.loop_delay"),
config.GetString("queue.strategy") == "full-bus",
)
2020-04-16 22:12:38 +05:30
}
2024-02-19 18:24:12 +05:30
func newMojangSignedTexturesProvider(mojangApi *mojang.MojangApi) (mojang.TexturesProvider, error) {
provider, err := mojang.NewMojangApiTexturesProvider(mojangApi.UuidToTextures)
if err != nil {
return nil, err
}
return mojang.NewTexturesProviderWithInMemoryCache(provider)
2020-04-16 22:12:38 +05:30
}