2020-04-16 22:12:38 +05:30
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
2020-04-24 15:50:03 +05:30
|
|
|
"context"
|
2020-04-16 22:12:38 +05:30
|
|
|
"fmt"
|
|
|
|
"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"
|
|
|
|
|
2020-04-27 00:26:03 +05:30
|
|
|
"github.com/elyby/chrly/api/mojang"
|
2020-04-19 05:01:09 +05:30
|
|
|
es "github.com/elyby/chrly/eventsubscribers"
|
2020-04-16 22:12:38 +05:30
|
|
|
"github.com/elyby/chrly/http"
|
|
|
|
"github.com/elyby/chrly/mojangtextures"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mojangTextures = di.Options(
|
2020-04-27 00:26:03 +05:30
|
|
|
di.Invoke(interceptMojangApiUrls),
|
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-24 15:50:03 +05:30
|
|
|
di.Provide(newMojangTexturesBatchUUIDsProviderStrategyFactory),
|
|
|
|
di.Provide(newMojangTexturesBatchUUIDsProviderDelayedStrategy),
|
|
|
|
di.Provide(newMojangTexturesBatchUUIDsProviderFullBusStrategy),
|
2020-04-16 22:12:38 +05:30
|
|
|
di.Provide(newMojangSignedTexturesProvider),
|
|
|
|
di.Provide(newMojangTexturesStorageFactory),
|
|
|
|
)
|
|
|
|
|
2020-04-27 00:26:03 +05:30
|
|
|
func interceptMojangApiUrls(config *viper.Viper) error {
|
|
|
|
apiUrl := config.GetString("mojang.api_base_url")
|
|
|
|
if apiUrl != "" {
|
|
|
|
u, err := url.ParseRequestURI(apiUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mojang.ApiMojangDotComAddr = u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionServerUrl := config.GetString("mojang.session_server_base_url")
|
|
|
|
if sessionServerUrl != "" {
|
|
|
|
u, err := url.ParseRequestURI(apiUrl)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
mojang.SessionServerMojangComAddr = u.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-16 22:12:38 +05:30
|
|
|
func newMojangTexturesProviderFactory(
|
|
|
|
container *di.Container,
|
|
|
|
config *viper.Viper,
|
|
|
|
) (http.MojangTexturesProvider, 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") {
|
|
|
|
return &mojangtextures.NilProvider{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var provider *mojangtextures.Provider
|
|
|
|
err := container.Resolve(&provider)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return provider, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMojangTexturesProvider(
|
|
|
|
emitter mojangtextures.Emitter,
|
|
|
|
uuidsProvider mojangtextures.UUIDsProvider,
|
|
|
|
texturesProvider mojangtextures.TexturesProvider,
|
|
|
|
storage mojangtextures.Storage,
|
|
|
|
) *mojangtextures.Provider {
|
|
|
|
return &mojangtextures.Provider{
|
|
|
|
Emitter: emitter,
|
|
|
|
UUIDsProvider: uuidsProvider,
|
|
|
|
TexturesProvider: texturesProvider,
|
|
|
|
Storage: storage,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
func newMojangTexturesUuidsProviderFactory(
|
|
|
|
container *di.Container,
|
2020-04-16 22:12:38 +05:30
|
|
|
) (mojangtextures.UUIDsProvider, error) {
|
2020-04-19 05:01:09 +05:30
|
|
|
var provider *mojangtextures.BatchUuidsProvider
|
|
|
|
err := container.Resolve(&provider)
|
|
|
|
|
|
|
|
return provider, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMojangTexturesBatchUUIDsProvider(
|
|
|
|
container *di.Container,
|
2020-04-24 15:50:03 +05:30
|
|
|
strategy mojangtextures.BatchUuidsProviderStrategy,
|
2020-04-19 05:01:09 +05:30
|
|
|
emitter mojangtextures.Emitter,
|
|
|
|
) (*mojangtextures.BatchUuidsProvider, error) {
|
|
|
|
if err := container.Provide(func(emitter es.Subscriber, config *viper.Viper) *namedHealthChecker {
|
|
|
|
config.SetDefault("healthcheck.mojang_batch_uuids_provider_cool_down_duration", time.Minute)
|
|
|
|
|
|
|
|
return &namedHealthChecker{
|
|
|
|
Name: "mojang-batch-uuids-provider-response",
|
|
|
|
Checker: es.MojangBatchUuidsProviderResponseChecker(
|
|
|
|
emitter,
|
|
|
|
config.GetDuration("healthcheck.mojang_batch_uuids_provider_cool_down_duration"),
|
|
|
|
),
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
2020-04-20 15:53:02 +05:30
|
|
|
}); err != nil {
|
2020-04-19 05:01:09 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := container.Provide(func(emitter es.Subscriber, config *viper.Viper) *namedHealthChecker {
|
|
|
|
config.SetDefault("healthcheck.mojang_batch_uuids_provider_queue_length_limit", 50)
|
2020-04-16 22:12:38 +05:30
|
|
|
|
2020-04-19 05:01:09 +05:30
|
|
|
return &namedHealthChecker{
|
|
|
|
Name: "mojang-batch-uuids-provider-queue-length",
|
|
|
|
Checker: es.MojangBatchUuidsProviderQueueLengthChecker(
|
|
|
|
emitter,
|
|
|
|
config.GetInt("healthcheck.mojang_batch_uuids_provider_queue_length_limit"),
|
|
|
|
),
|
|
|
|
}
|
2020-04-20 15:53:02 +05:30
|
|
|
}); err != nil {
|
2020-04-19 05:01:09 +05:30
|
|
|
return nil, err
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
return mojangtextures.NewBatchUuidsProvider(context.Background(), strategy, emitter), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMojangTexturesBatchUUIDsProviderStrategyFactory(
|
|
|
|
container *di.Container,
|
|
|
|
config *viper.Viper,
|
|
|
|
) (mojangtextures.BatchUuidsProviderStrategy, error) {
|
|
|
|
config.SetDefault("queue.strategy", "periodic")
|
|
|
|
|
|
|
|
strategyName := config.GetString("queue.strategy")
|
|
|
|
switch strategyName {
|
|
|
|
case "periodic":
|
|
|
|
var strategy *mojangtextures.PeriodicStrategy
|
|
|
|
err := container.Resolve(&strategy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strategy, nil
|
|
|
|
case "full-bus":
|
|
|
|
var strategy *mojangtextures.FullBusStrategy
|
|
|
|
err := container.Resolve(&strategy)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return strategy, nil
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unknown queue strategy \"%s\"", strategyName)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMojangTexturesBatchUUIDsProviderDelayedStrategy(config *viper.Viper) *mojangtextures.PeriodicStrategy {
|
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)
|
|
|
|
|
2020-04-24 15:50:03 +05:30
|
|
|
return mojangtextures.NewPeriodicStrategy(
|
|
|
|
config.GetDuration("queue.loop_delay"),
|
|
|
|
config.GetInt("queue.batch_size"),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMojangTexturesBatchUUIDsProviderFullBusStrategy(config *viper.Viper) *mojangtextures.FullBusStrategy {
|
|
|
|
config.SetDefault("queue.loop_delay", 2*time.Second+500*time.Millisecond)
|
|
|
|
config.SetDefault("queue.batch_size", 10)
|
|
|
|
|
|
|
|
return mojangtextures.NewFullBusStrategy(
|
|
|
|
config.GetDuration("queue.loop_delay"),
|
|
|
|
config.GetInt("queue.batch_size"),
|
|
|
|
)
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func newMojangSignedTexturesProvider(emitter mojangtextures.Emitter) mojangtextures.TexturesProvider {
|
|
|
|
return &mojangtextures.MojangApiTexturesProvider{
|
|
|
|
Emitter: emitter,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMojangTexturesStorageFactory(
|
2020-04-28 20:27:51 +05:30
|
|
|
uuidsStorage mojangtextures.UUIDsStorage,
|
2020-04-16 22:12:38 +05:30
|
|
|
texturesStorage mojangtextures.TexturesStorage,
|
|
|
|
) mojangtextures.Storage {
|
|
|
|
return &mojangtextures.SeparatedStorage{
|
2020-04-28 20:27:51 +05:30
|
|
|
UUIDsStorage: uuidsStorage,
|
2020-04-16 22:12:38 +05:30
|
|
|
TexturesStorage: texturesStorage,
|
|
|
|
}
|
|
|
|
}
|