mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-21 16:11:56 +00:00
Daniel Mason
9cbb94fc56
- Add registration_enabled to /api/v1/serverinfo - Add config table caching on save - Fix redis TTL not being parsed correctly - Move registration enabled to backend toggle - Fixed navbar when loading /u/profile URL - Token now shows on user page + can reset - Added basic popup validation to disconnect/reset buttons
86 lines
1.7 KiB
Go
86 lines
1.7 KiB
Go
package goscrobble
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
)
|
|
|
|
var redisDb *redis.Client
|
|
var redisPrefix string
|
|
|
|
var ctx = context.Background()
|
|
|
|
// InitRedis - Boot redis connection!
|
|
func InitRedis() {
|
|
redisHost := os.Getenv("REDIS_HOST")
|
|
redisPort := os.Getenv("REDIS_PORT")
|
|
redisDatabase := os.Getenv("REDIS_DB")
|
|
redisAuth := os.Getenv("REDIS_AUTH")
|
|
redisPrefix = os.Getenv("REDIS_PREFIX")
|
|
|
|
redisDbNum := 0
|
|
if redisDatabase != "" {
|
|
redisDbNum, _ = strconv.Atoi(redisDatabase)
|
|
}
|
|
|
|
// Create new connection
|
|
redisDb = redis.NewClient(&redis.Options{
|
|
Addr: redisHost + ":" + redisPort,
|
|
Password: redisAuth,
|
|
DB: redisDbNum,
|
|
})
|
|
|
|
// Lets just check it's active..
|
|
err := redisDb.Set(ctx, "testSetKey", "value", 0).Err()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
redisDb.Del(ctx, "testSetKey")
|
|
fmt.Println("Redis connected")
|
|
}
|
|
|
|
func CloseRedisConn() {
|
|
redisDb.Close()
|
|
}
|
|
|
|
// setRedis - Uses default 24 hour TTL
|
|
func setRedisVal(key string, val string) error {
|
|
ttl := time.Hour * time.Duration(24)
|
|
return setRedisValTtl(key, val, ttl)
|
|
}
|
|
|
|
// setRedisTtl - Allows custom TTL
|
|
func setRedisValTtl(key string, val string, ttl time.Duration) error {
|
|
return redisDb.Set(ctx, redisPrefix+key, val, ttl).Err()
|
|
}
|
|
|
|
// getRedisVal - Returns value if exists
|
|
func getRedisVal(key string) string {
|
|
val, err := redisDb.Get(ctx, redisPrefix+key).Result()
|
|
if err != nil {
|
|
if err == redis.Nil {
|
|
return ""
|
|
}
|
|
log.Printf("Failed to fetch redis key (%+v) Error: %+v", key, err)
|
|
}
|
|
|
|
return val
|
|
}
|
|
|
|
func getRedisKeyExists(key string) bool {
|
|
val, err := redisDb.Exists(ctx, redisPrefix+key).Result()
|
|
if err != nil {
|
|
log.Printf("Failed to fetch redis key (%+v) Error: %+v", key, err)
|
|
return false
|
|
}
|
|
|
|
return val == 1
|
|
}
|