40 lines
902 B
Go
40 lines
902 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.1in9.net/raider/wroofauth/internal/logger"
|
|
"github.com/redis/go-redis/v9"
|
|
"github.com/spf13/viper"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
var Redis *redis.Client
|
|
|
|
func RedisConnect(ctx context.Context) {
|
|
logger.Logger.Info("establishing connection to redis...")
|
|
|
|
Redis = redis.NewClient(&redis.Options{
|
|
Addr: viper.GetString("redis.addr"),
|
|
Username: viper.GetString("redis.username"),
|
|
Password: viper.GetString("redis.password"),
|
|
DB: viper.GetInt("redis.db"),
|
|
})
|
|
|
|
err := Redis.Ping(ctx).Err()
|
|
if err != nil {
|
|
logger.Logger.Fatal("unable to ping to redis", zap.Error(err))
|
|
}
|
|
|
|
setupLocker()
|
|
|
|
logger.Logger.Info("redis connection established")
|
|
}
|
|
|
|
func RedisDisconnect() {
|
|
err := Redis.Close()
|
|
if err != nil {
|
|
logger.Logger.Warn("failed to gracefully disconnect redis", zap.Error(err))
|
|
}
|
|
logger.Logger.Info("redis gracefully disconnected")
|
|
}
|