2023-01-07 13:00:00 +01:00
|
|
|
module main
|
|
|
|
|
|
|
|
import toml
|
2023-01-12 19:34:28 +01:00
|
|
|
import os
|
2023-01-07 13:00:00 +01:00
|
|
|
|
|
|
|
const (
|
2023-01-12 19:34:28 +01:00
|
|
|
path = config_path()
|
2023-01-07 13:00:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
struct Config {
|
2023-01-11 19:08:24 +01:00
|
|
|
host string
|
|
|
|
port int
|
|
|
|
token string
|
|
|
|
redirect bool
|
2023-01-07 13:00:00 +01:00
|
|
|
redirect_url string
|
2023-01-11 19:08:24 +01:00
|
|
|
db_path string
|
2023-01-12 19:34:28 +01:00
|
|
|
origins []string
|
2023-01-07 13:00:00 +01:00
|
|
|
}
|
|
|
|
|
2023-01-10 11:00:00 +01:00
|
|
|
fn (config Config) copy() Config {
|
|
|
|
return config
|
|
|
|
}
|
|
|
|
|
2023-01-12 19:34:28 +01:00
|
|
|
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('Error while parsing config file:\n' + err.msg())
|
|
|
|
}
|
2023-01-07 13:00:00 +01:00
|
|
|
|
2023-01-11 19:08:24 +01:00
|
|
|
return Config{
|
2023-01-11 19:00:00 +01:00
|
|
|
host: config.value('host').string()
|
|
|
|
port: config.value('port').int()
|
|
|
|
token: config.value('token').string()
|
|
|
|
redirect: config.value('redirect').bool()
|
|
|
|
redirect_url: config.value('redirect_url').string()
|
|
|
|
db_path: config.value('db_path').string()
|
2023-01-12 19:34:28 +01:00
|
|
|
origins: config.value('origins').array().as_strings()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn config_path() string {
|
|
|
|
path_env := $env('CONFIG_PATH')
|
|
|
|
if path_env.trim_space().len != 0 {
|
|
|
|
return path_env
|
2023-01-07 13:00:00 +01:00
|
|
|
}
|
2023-01-12 19:34:28 +01:00
|
|
|
return './config.toml'
|
2023-01-07 13:00:00 +01:00
|
|
|
}
|