diff --git a/src/lib/server/config.ts b/src/lib/server/config.ts new file mode 100644 index 0000000..5b5c0e5 --- /dev/null +++ b/src/lib/server/config.ts @@ -0,0 +1,56 @@ +import path from "node:path" + +const to_absolute_path = (p: string): string => { + if (path.isAbsolute(p)) { + return p + } + return path.resolve(process.cwd(), p) +} + +const resolve_env_to_path = (env: string|undefined, fallback: string): string => { + if (!env) { + return to_absolute_path(fallback) + } + return to_absolute_path(env) +} + +const resolve_env_to_boolean = (env: string|undefined, fallback: boolean): boolean => { + if (!env) { + return fallback + } + + const str = env.toLowerCase() + + switch (str) { + case "false": + case "no": + case "off": return false + } + + return true +} + +class Config { + private _log_dir: string + private _log_to_file_when_debug: boolean + + readonly is_debug: boolean = process.env.NODE_ENV != "production" + readonly is_production: boolean = process.env.NODE_ENV == "production" + + get log_dir(): string { + return this._log_dir + } + get log_to_file_when_debug(): boolean { + return this._log_to_file_when_debug + } + + constructor() { + this._log_dir = resolve_env_to_path(process.env.APP_LOG_DIR, "./data/logs") + this._log_to_file_when_debug = resolve_env_to_boolean(process.env.LOG_TO_FILE_WHEN_DEBUG, false) + } + +} + +const _config = new Config() + +export default _config;