chrly/internal/di/db.go

56 lines
1.5 KiB
Go
Raw Normal View History

2020-04-16 22:12:38 +05:30
package di
import (
"context"
"fmt"
2023-12-13 21:59:12 +05:30
"github.com/defval/di"
2020-04-16 22:12:38 +05:30
"github.com/spf13/viper"
db2 "ely.by/chrly/internal/db"
"ely.by/chrly/internal/db/redis"
es "ely.by/chrly/internal/eventsubscribers"
"ely.by/chrly/internal/mojang"
"ely.by/chrly/internal/profiles"
2020-04-16 22:12:38 +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(
di.Provide(newRedis,
di.As(new(profiles.ProfilesRepository)),
di.As(new(profiles.ProfilesFinder)),
di.As(new(mojang.MojangUuidsStorage)),
),
2020-04-16 22:12:38 +05:30
)
func newRedis(container *di.Container, config *viper.Viper) (*redis.Redis, error) {
config.SetDefault("storage.redis.host", "localhost")
config.SetDefault("storage.redis.port", 6379)
2020-05-01 06:26:41 +05:30
config.SetDefault("storage.redis.poolSize", 10)
conn, err := redis.New(
2023-12-14 06:45:59 +05:30
context.Background(),
db2.NewZlibEncoder(&db2.JsonSerializer{}),
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
}
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
}
return conn, nil
2020-04-16 22:12:38 +05:30
}