2021-03-31 08:40:20 +00:00
|
|
|
package goscrobble
|
|
|
|
|
|
|
|
import (
|
2021-04-02 09:24:00 +00:00
|
|
|
"database/sql"
|
2021-03-31 08:40:20 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Setting map[string]string `json:"configs"`
|
|
|
|
}
|
|
|
|
|
2021-04-02 12:11:05 +00:00
|
|
|
type ServerInfo struct {
|
2021-04-03 00:54:07 +00:00
|
|
|
Version string `json:"version"`
|
|
|
|
RegistrationEnabled string `json:"registration_enabled"`
|
2021-04-02 12:11:05 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 08:40:20 +00:00
|
|
|
func getAllConfigs() (Config, error) {
|
|
|
|
config := Config{}
|
|
|
|
configs := make(map[string]string)
|
|
|
|
|
2022-01-05 07:58:05 +00:00
|
|
|
rows, err := db.Query(`SELECT key, value FROM config`)
|
2021-03-31 08:40:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to fetch config: %+v", err)
|
|
|
|
return config, errors.New("Failed to fetch configs")
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var key string
|
|
|
|
var value string
|
|
|
|
err := rows.Scan(&key, &value)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to fetch config: %+v", err)
|
|
|
|
return config, errors.New("Failed to fetch configs")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Append
|
|
|
|
configs[key] = value
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assign the data to the parent
|
|
|
|
config.Setting = configs
|
|
|
|
|
|
|
|
err = rows.Err()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Failed to fetch config: %+v", err)
|
|
|
|
return config, errors.New("Failed to fetch configs")
|
|
|
|
}
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func updateConfigValue(key string, value string) error {
|
2022-01-05 07:58:05 +00:00
|
|
|
_, err := db.Exec(`UPDATE config SET value = $1 WHERE key = $2`, value, key)
|
2021-03-31 08:40:20 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Failed to update config: %+v", err)
|
|
|
|
return errors.New("Failed to update config value.")
|
|
|
|
}
|
|
|
|
|
2022-01-05 09:32:20 +00:00
|
|
|
// Set cached config
|
|
|
|
redisKey := "config:" + key
|
|
|
|
setRedisVal(redisKey, value)
|
|
|
|
|
2021-03-31 08:40:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-04-02 09:24:00 +00:00
|
|
|
|
|
|
|
func getConfigValue(key string) (string, error) {
|
|
|
|
var value string
|
|
|
|
|
2022-01-05 09:32:20 +00:00
|
|
|
// Check if cached first
|
|
|
|
redisKey := "config:" + key
|
|
|
|
|
|
|
|
// TODO: Handle unset vals in DB to prevent excess calls if not using spotify/etc.
|
|
|
|
configKey := getRedisVal(redisKey)
|
|
|
|
if configKey == "" {
|
2022-06-27 04:10:01 +00:00
|
|
|
err := db.QueryRow("SELECT value FROM config "+
|
|
|
|
"WHERE key = ?",
|
2022-01-05 09:32:20 +00:00
|
|
|
key).Scan(&value)
|
|
|
|
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
return value, errors.New("Config key doesn't exist")
|
|
|
|
}
|
|
|
|
|
|
|
|
if value != "" {
|
|
|
|
setRedisVal(redisKey, value)
|
|
|
|
}
|
2021-04-02 09:24:00 +00:00
|
|
|
|
2022-01-05 09:32:20 +00:00
|
|
|
return value, nil
|
2021-04-02 09:24:00 +00:00
|
|
|
}
|
|
|
|
|
2022-01-05 09:32:20 +00:00
|
|
|
return configKey, nil
|
2021-04-02 09:24:00 +00:00
|
|
|
}
|