2020-04-16 22:12:38 +05:30
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
2020-04-21 00:48:27 +05:30
|
|
|
"fmt"
|
|
|
|
"path"
|
|
|
|
|
2020-04-16 22:12:38 +05:30
|
|
|
"github.com/goava/di"
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
"github.com/elyby/chrly/db/fs"
|
|
|
|
"github.com/elyby/chrly/db/redis"
|
|
|
|
es "github.com/elyby/chrly/eventsubscribers"
|
2020-04-16 22:12:38 +05:30
|
|
|
"github.com/elyby/chrly/http"
|
|
|
|
"github.com/elyby/chrly/mojangtextures"
|
|
|
|
)
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
// v4 had the idea that it would be possible to separate backends for storing skins and capes.
|
|
|
|
// But in v5 the storage will be unified, so this is just temporary constructors before large reworking.
|
|
|
|
//
|
|
|
|
// Since there are no options for selecting target backends,
|
|
|
|
// all constants in this case point to static specific implementations.
|
2020-04-16 22:12:38 +05:30
|
|
|
var db = di.Options(
|
2020-04-21 00:48:27 +05:30
|
|
|
di.Provide(newRedis,
|
|
|
|
di.As(new(http.SkinsRepository)),
|
|
|
|
di.As(new(mojangtextures.UuidsStorage)),
|
|
|
|
),
|
|
|
|
di.Provide(newFSFactory,
|
|
|
|
di.As(new(http.CapesRepository)),
|
|
|
|
),
|
2020-04-16 22:12:38 +05:30
|
|
|
di.Provide(newMojangSignedTexturesStorage),
|
|
|
|
)
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func newRedis(container *di.Container, config *viper.Viper) (*redis.Redis, error) {
|
2020-04-19 05:01:09 +05:30
|
|
|
config.SetDefault("storage.redis.host", "localhost")
|
|
|
|
config.SetDefault("storage.redis.port", 6379)
|
|
|
|
config.SetDefault("storage.redis.poll", 10)
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
conn, err := redis.New(
|
|
|
|
fmt.Sprintf("%s:%d", config.GetString("storage.redis.host"), config.GetInt("storage.redis.port")),
|
|
|
|
config.GetInt("storage.redis.poolSize"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
if err := container.Provide(func() *namedHealthChecker {
|
|
|
|
return &namedHealthChecker{
|
|
|
|
Name: "redis",
|
|
|
|
Checker: es.DatabaseChecker(conn),
|
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return conn, nil
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
func newFSFactory(config *viper.Viper) (*fs.Filesystem, error) {
|
|
|
|
config.SetDefault("storage.filesystem.basePath", "data")
|
|
|
|
config.SetDefault("storage.filesystem.capesDirName", "capes")
|
2020-04-16 22:12:38 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
return fs.New(path.Join(
|
|
|
|
config.GetString("storage.filesystem.basePath"),
|
|
|
|
config.GetString("storage.filesystem.capesDirName"),
|
|
|
|
))
|
2020-04-16 22:12:38 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
func newMojangSignedTexturesStorage() mojangtextures.TexturesStorage {
|
|
|
|
texturesStorage := mojangtextures.NewInMemoryTexturesStorage()
|
|
|
|
texturesStorage.Start()
|
|
|
|
|
|
|
|
return texturesStorage
|
|
|
|
}
|