chrly/internal/di/db.go

53 lines
1.3 KiB
Go
Raw Normal View History

2020-04-16 19:42:38 +03:00
package di
import (
"context"
"fmt"
2023-12-13 17:29:12 +01:00
"github.com/defval/di"
"github.com/etherlabsio/healthcheck/v2"
2020-04-16 19:42:38 +03:00
"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 19:42:38 +03:00
)
// Since there are no options for selecting target backends,
// all constants in this case point to static specific implementations.
2024-02-14 00:56:48 +01:00
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 19:42:38 +03:00
)
2024-02-14 00:56:48 +01:00
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 03:56:41 +03:00
config.SetDefault("storage.redis.poolSize", 10)
conn, err := redis.New(
2024-02-14 00:56:48 +01:00
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 19:42:38 +03:00
}
if err := container.Provide(func() *namedHealthChecker {
return &namedHealthChecker{
Name: "redis",
Checker: healthcheck.CheckerFunc(conn.Ping),
}
}); err != nil {
return nil, err
2020-04-16 19:42:38 +03:00
}
return conn, nil
2020-04-16 19:42:38 +03:00
}