2020-04-16 19:42:38 +03:00
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
2020-05-01 02:46:12 +03:00
|
|
|
"context"
|
2020-04-20 22:18:27 +03:00
|
|
|
"fmt"
|
|
|
|
|
2023-12-13 17:29:12 +01:00
|
|
|
"github.com/defval/di"
|
2024-02-07 17:34:57 +01:00
|
|
|
"github.com/etherlabsio/healthcheck/v2"
|
2020-04-16 19:42:38 +03:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2024-02-07 17:34:57 +01:00
|
|
|
"ely.by/chrly/internal/db"
|
2024-02-01 08:12:34 +01:00
|
|
|
"ely.by/chrly/internal/db/redis"
|
|
|
|
"ely.by/chrly/internal/mojang"
|
|
|
|
"ely.by/chrly/internal/profiles"
|
2020-04-16 19:42:38 +03:00
|
|
|
)
|
|
|
|
|
2020-04-20 22:18:27 +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(
|
2020-04-20 22:18:27 +03:00
|
|
|
di.Provide(newRedis,
|
2024-01-30 09:05:04 +01:00
|
|
|
di.As(new(profiles.ProfilesRepository)),
|
|
|
|
di.As(new(profiles.ProfilesFinder)),
|
2024-01-10 01:42:10 +01:00
|
|
|
di.As(new(mojang.MojangUuidsStorage)),
|
2020-04-20 22:18:27 +03:00
|
|
|
),
|
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) {
|
2020-04-19 02:31:09 +03:00
|
|
|
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)
|
2020-04-19 02:31:09 +03:00
|
|
|
|
2020-04-20 22:18:27 +03:00
|
|
|
conn, err := redis.New(
|
2024-02-14 00:56:48 +01:00
|
|
|
ctx,
|
2024-02-07 17:34:57 +01:00
|
|
|
db.NewZlibEncoder(&db.JsonSerializer{}),
|
2020-04-20 22:18:27 +03:00
|
|
|
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
|
|
|
}
|
2020-04-19 02:31:09 +03:00
|
|
|
|
2020-04-20 22:18:27 +03:00
|
|
|
if err := container.Provide(func() *namedHealthChecker {
|
|
|
|
return &namedHealthChecker{
|
|
|
|
Name: "redis",
|
2024-02-07 17:34:57 +01:00
|
|
|
Checker: healthcheck.CheckerFunc(conn.Ping),
|
2020-04-20 22:18:27 +03:00
|
|
|
}
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
2020-04-16 19:42:38 +03:00
|
|
|
}
|
|
|
|
|
2020-04-20 22:18:27 +03:00
|
|
|
return conn, nil
|
2020-04-16 19:42:38 +03:00
|
|
|
}
|