hello world
This commit is contained in:
71
storage/storage.go
Normal file
71
storage/storage.go
Normal file
@@ -0,0 +1,71 @@
|
||||
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, passSet := redisUri.User.Password()
|
||||
|
||||
if !passSet {
|
||||
panic("pass not set")
|
||||
}
|
||||
|
||||
client = redis.NewClient(&redis.Options{
|
||||
Addr: redisUri.Host,
|
||||
Username: username,
|
||||
Password: pass,
|
||||
DB: 0,
|
||||
})
|
||||
|
||||
validateRedis()
|
||||
}
|
||||
Reference in New Issue
Block a user