2020-04-16 22:12:38 +05:30
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
2024-01-10 06:12:10 +05:30
|
|
|
"net/http"
|
2020-04-16 22:12:38 +05:30
|
|
|
"net/url"
|
2020-04-19 05:01:09 +05:30
|
|
|
"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"
|
|
|
|
|
2024-02-01 12:42:34 +05:30
|
|
|
"ely.by/chrly/internal/mojang"
|
|
|
|
"ely.by/chrly/internal/profiles"
|
2020-04-16 22:12:38 +05:30
|
|
|
)
|
|
|
|
|
2024-02-07 22:04:57 +05:30
|
|
|
var mojangDiOptions = di.Options(
|
2024-01-10 06:12:10 +05:30
|
|
|
di.Provide(newMojangApi),
|
2020-04-16 22:12:38 +05:30
|
|
|
di.Provide(newMojangTexturesProviderFactory),
|
|
|
|
di.Provide(newMojangTexturesProvider),
|
2020-04-19 05:01:09 +05:30
|
|
|
di.Provide(newMojangTexturesUuidsProviderFactory),
|
|
|
|
di.Provide(newMojangTexturesBatchUUIDsProvider),
|
2020-04-16 22:12:38 +05:30
|
|
|
di.Provide(newMojangSignedTexturesProvider),
|
|
|
|
)
|
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
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
|
2020-04-27 00:26:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
profileUrl := config.GetString("mojang.profile_url")
|
|
|
|
if profileUrl != "" {
|
|
|
|
if _, err := url.ParseRequestURI(batchUuidsUrl); err != nil {
|
|
|
|
return nil, err
|
2020-04-27 00:26:03 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
httpClient := &http.Client{} // TODO: extract to the singleton dependency
|
|
|
|
|
|
|
|
return mojang.NewMojangApi(httpClient, batchUuidsUrl, profileUrl), nil
|
2020-04-27 00:26:03 +05:30
|
|
|
}
|
|
|
|
|
2020-04-16 22:12:38 +05:30
|
|
|
func newMojangTexturesProviderFactory(
|
|
|
|
container *di.Container,
|
|
|
|
config *viper.Viper,
|
2024-01-30 13:35:04 +05:30
|
|
|
) (profiles.MojangProfilesProvider, error) {
|
2020-04-19 05:01:09 +05:30
|
|
|
config.SetDefault("mojang_textures.enabled", true)
|
2020-04-16 22:12:38 +05:30
|
|
|
if !config.GetBool("mojang_textures.enabled") {
|
2024-01-10 06:12:10 +05:30
|
|
|
return &mojang.NilProvider{}, nil
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
2024-01-10 06:12:10 +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(
|
2024-01-10 06:12:10 +05:30
|
|
|
uuidsProvider mojang.UuidsProvider,
|
|
|
|
texturesProvider mojang.TexturesProvider,
|
|
|
|
) *mojang.MojangTexturesProvider {
|
|
|
|
return &mojang.MojangTexturesProvider{
|
|
|
|
UuidsProvider: uuidsProvider,
|
2020-04-16 22:12:38 +05:30
|
|
|
TexturesProvider: texturesProvider,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func newMojangTexturesUuidsProviderFactory(
|
2024-01-10 06:12:10 +05:30
|
|
|
batchProvider *mojang.BatchUuidsProvider,
|
|
|
|
uuidsStorage mojang.MojangUuidsStorage,
|
|
|
|
) mojang.UuidsProvider {
|
|
|
|
return &mojang.UuidsProviderWithCache{
|
|
|
|
Provider: batchProvider,
|
|
|
|
Storage: uuidsStorage,
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
2020-04-24 15:50:03 +05:30
|
|
|
}
|
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
func newMojangTexturesBatchUUIDsProvider(
|
|
|
|
mojangApi *mojang.MojangApi,
|
2020-04-24 15:50:03 +05:30
|
|
|
config *viper.Viper,
|
2024-01-10 06:12:10 +05:30
|
|
|
) (*mojang.BatchUuidsProvider, error) {
|
2020-04-19 05:01:09 +05:30
|
|
|
config.SetDefault("queue.loop_delay", 2*time.Second+500*time.Millisecond)
|
|
|
|
config.SetDefault("queue.batch_size", 10)
|
2024-01-10 06:12:10 +05:30
|
|
|
config.SetDefault("queue.strategy", "periodic")
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
// TODO: healthcheck is broken
|
2020-04-24 15:50:03 +05:30
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
uuidsProvider := mojang.NewBatchUuidsProvider(
|
|
|
|
mojangApi.UsernamesToUuids,
|
2020-04-24 15:50:03 +05:30
|
|
|
config.GetInt("queue.batch_size"),
|
2024-01-10 06:12:10 +05:30
|
|
|
config.GetDuration("queue.loop_delay"),
|
|
|
|
config.GetString("queue.strategy") == "full-bus",
|
2020-04-24 15:50:03 +05:30
|
|
|
)
|
2020-04-16 22:12:38 +05:30
|
|
|
|
2024-01-10 06:12:10 +05:30
|
|
|
return uuidsProvider, nil
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
2024-01-10 06:12:10 +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
|
|
|
}
|