highscore-server/src/config.v

65 lines
1.5 KiB
V

module main
import toml
import os
const (
path = config_path()
)
struct Config {
host string
port int
token string
valkey string
redirect bool
redirect_url string
db_path string
loglevel string
origins []string
games []string
valid_keys map[string][]string
}
fn load_config() !&Config {
if !os.is_file(path) {
return error('Config file does not exist or is not a file')
}
config := toml.parse_file(path) or { return error('Parsing error:\n' + err.str()) }
token := config.value('token').string()
if token.len == 0 {
return error('Token cannot be empty')
}
valkey := config.value('valkey').string()
if valkey.len == 0 {
return error('Verification key cannot be empty')
}
valid_keys_toml := config.value('valid_keys').as_map()
mut valid_keys := map[string][]string{}
for key, arr in valid_keys_toml {
valid_keys[key] = arr.array().as_strings()
}
return &Config{
host: config.value('host').string()
port: config.value('port').int()
token: token
valkey: valkey
redirect: config.value('redirect').bool()
redirect_url: config.value('redirect_url').string()
db_path: config.value('db_path').string()
loglevel: config.value('loglevel').string()
origins: config.value('origins').array().as_strings()
games: config.value('games').array().as_strings()
valid_keys: valid_keys
}
}
fn config_path() string {
path_env := $env('CONFIG_PATH').trim_space()
return if path_env.len != 0 { path_env } else { './config.toml' }
}