chrly/internal/di/db.go

53 lines
1.3 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"
"github.com/etherlabsio/healthcheck/v2"
2020-04-16 22:12:38 +05:30
"github.com/spf13/viper"
"ely.by/chrly/internal/db"
"ely.by/chrly/internal/db/redis"
"ely.by/chrly/internal/mojang"
"ely.by/chrly/internal/profiles"
2020-04-16 22:12:38 +05:30
)
// Since there are no options for selecting target backends,
// all constants in this case point to static specific implementations.
2024-02-14 05:26:48 +05:30
var dbDiOptions = 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
)
2024-02-14 05:26:48 +05:30
func newRedis(container *di.Container, ctx context.Context, 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(
2024-02-14 05:26:48 +05:30
ctx,
db.NewZlibEncoder(&db.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: healthcheck.CheckerFunc(conn.Ping),
}
}); err != nil {
return nil, err
2020-04-16 22:12:38 +05:30
}
return conn, nil
2020-04-16 22:12:38 +05:30
}