2020-04-16 22:12:38 +05:30
|
|
|
package di
|
|
|
|
|
|
|
|
import (
|
2020-05-01 05:16:12 +05:30
|
|
|
"context"
|
2020-04-21 00:48:27 +05:30
|
|
|
"fmt"
|
|
|
|
|
2023-12-13 21:59:12 +05:30
|
|
|
"github.com/defval/di"
|
2024-02-07 22:04:57 +05:30
|
|
|
"github.com/etherlabsio/healthcheck/v2"
|
2020-04-16 22:12:38 +05:30
|
|
|
"github.com/spf13/viper"
|
|
|
|
|
2024-02-07 22:04:57 +05:30
|
|
|
"ely.by/chrly/internal/db"
|
2024-02-01 12:42:34 +05:30
|
|
|
"ely.by/chrly/internal/db/redis"
|
|
|
|
"ely.by/chrly/internal/mojang"
|
|
|
|
"ely.by/chrly/internal/profiles"
|
2020-04-16 22:12:38 +05:30
|
|
|
)
|
|
|
|
|
2020-04-21 00:48:27 +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(
|
2020-04-21 00:48:27 +05:30
|
|
|
di.Provide(newRedis,
|
2024-01-30 13:35:04 +05:30
|
|
|
di.As(new(profiles.ProfilesRepository)),
|
|
|
|
di.As(new(profiles.ProfilesFinder)),
|
2024-01-10 06:12:10 +05:30
|
|
|
di.As(new(mojang.MojangUuidsStorage)),
|
2020-04-21 00:48:27 +05:30
|
|
|
),
|
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) {
|
2020-04-19 05:01:09 +05:30
|
|
|
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)
|
2020-04-19 05:01:09 +05:30
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
conn, err := redis.New(
|
2024-02-14 05:26:48 +05:30
|
|
|
ctx,
|
2024-02-07 22:04:57 +05:30
|
|
|
db.NewZlibEncoder(&db.JsonSerializer{}),
|
2020-04-21 00:48:27 +05:30
|
|
|
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",
|
2024-02-07 22:04:57 +05:30
|
|
|
Checker: healthcheck.CheckerFunc(conn.Ping),
|
2020-04-21 00:48:27 +05:30
|
|
|
}
|
|
|
|
}); 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
|
|
|
}
|