chrly/internal/di/mojang.go

109 lines
2.7 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),
)
func newMojangApi(config *viper.Viper) (*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
}
}
httpClient := &http.Client{} // TODO: extract to the singleton dependency
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,
) *mojang.MojangTexturesProvider {
return &mojang.MojangTexturesProvider{
UuidsProvider: uuidsProvider,
2020-04-16 22:12:38 +05:30
TexturesProvider: texturesProvider,
}
}
func newMojangTexturesUuidsProviderFactory(
batchProvider *mojang.BatchUuidsProvider,
uuidsStorage mojang.MojangUuidsStorage,
) mojang.UuidsProvider {
return &mojang.UuidsProviderWithCache{
Provider: batchProvider,
Storage: uuidsStorage,
2020-04-16 22:12:38 +05:30
}
}
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
uuidsProvider := 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
return uuidsProvider, nil
2020-04-16 22:12:38 +05:30
}
func newMojangSignedTexturesProvider(mojangApi *mojang.MojangApi) mojang.TexturesProvider {
return mojang.NewTexturesProviderWithInMemoryCache(
&mojang.MojangApiTexturesProvider{
MojangApiTexturesEndpoint: mojangApi.UuidToTextures,
},
)
2020-04-16 22:12:38 +05:30
}