ToDo/src/lib/server/config.ts

61 lines
1.3 KiB
TypeScript

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"
private _session_timeout: number = 15 * 60 * 1000
get log_dir(): string {
return this._log_dir
}
get log_to_file_when_debug(): boolean {
return this._log_to_file_when_debug
}
get session_timeout(): number { return this._session_timeout }
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;