67 lines
1.2 KiB
Go
67 lines
1.2 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"net/url"
|
|
"os"
|
|
|
|
"git.zomo.dev/zomo/discord-retokenizer/util"
|
|
"github.com/go-redis/redis/v9"
|
|
)
|
|
|
|
var ctx = context.Background()
|
|
var client *redis.Client = nil
|
|
|
|
func initializeRedis() {
|
|
username := "default"
|
|
UpdateUsername( username)
|
|
password := util.GeneratePassword(16)
|
|
UpdatePassword( password)
|
|
fmt.Printf("FIRST TIME SETUP\nusername: %s\npassword: %s\n\n\n", username, password)
|
|
}
|
|
|
|
func validateRedis() {
|
|
if client == nil {
|
|
panic("Client == nil")
|
|
}
|
|
_, err := client.Get(ctx, "username").Result()
|
|
if err != nil {
|
|
if err != redis.Nil {
|
|
panic(err)
|
|
}
|
|
initializeRedis()
|
|
return
|
|
}
|
|
_, err = client.Get(ctx, "password").Result()
|
|
if err != nil {
|
|
if err != redis.Nil {
|
|
panic(err)
|
|
}
|
|
initializeRedis()
|
|
return
|
|
}
|
|
}
|
|
|
|
func Init() {
|
|
redisUri, err := url.Parse(os.Getenv("REDIS_URI"))
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
if redisUri.Scheme != "redis" {
|
|
panic("redisUri.Scheme != redis")
|
|
}
|
|
|
|
username := redisUri.User.Username()
|
|
pass, _ := redisUri.User.Password()
|
|
|
|
client = redis.NewClient(&redis.Options{
|
|
Addr: redisUri.Host,
|
|
Username: username,
|
|
Password: pass,
|
|
DB: 0,
|
|
})
|
|
|
|
validateRedis()
|
|
} |