56 lines
1.2 KiB
D
56 lines
1.2 KiB
D
module util.config;
|
|
|
|
import std.stdio;
|
|
import slf4d;
|
|
|
|
private const CONFIG_FILE = "finnow-api-config.json";
|
|
|
|
struct AppConfig {
|
|
ushort port;
|
|
string webOrigin;
|
|
string logLevel;
|
|
}
|
|
|
|
Level getConfiguredLoggingLevel(in AppConfig cfg) {
|
|
try {
|
|
return parseLoggingLevel(cfg.logLevel);
|
|
} catch (LoggingException e) {
|
|
return Levels.INFO;
|
|
}
|
|
}
|
|
|
|
AppConfig readConfig() {
|
|
import std.file : exists, readText;
|
|
import std.json;
|
|
import std.conv : to;
|
|
|
|
AppConfig defaultConfig = AppConfig(
|
|
8080,
|
|
"http://localhost:5173",
|
|
"INFO"
|
|
);
|
|
// Local dev environment if no config is given.
|
|
if (!exists(CONFIG_FILE)) {
|
|
return defaultConfig;
|
|
}
|
|
JSONValue obj;
|
|
try {
|
|
obj = parseJSON(readText(CONFIG_FILE));
|
|
} catch (Exception e) {
|
|
stderr.writefln!"Failed to read config from file %s, using default config as a fallback: %s"(
|
|
CONFIG_FILE,
|
|
e.msg
|
|
);
|
|
return defaultConfig;
|
|
}
|
|
if ("port" in obj.object) {
|
|
defaultConfig.port = obj.object["port"].integer.to!ushort;
|
|
}
|
|
if ("webOrigin" in obj.object) {
|
|
defaultConfig.webOrigin = obj.object["webOrigin"].str;
|
|
}
|
|
if ("logLevel" in obj.object) {
|
|
defaultConfig.logLevel = obj.object["logLevel"].str;
|
|
}
|
|
return defaultConfig;
|
|
} |