Created scaffolding for config module with log default
This commit is contained in:
parent
8e8c2bbf68
commit
ecff8798fe
|
|
@ -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;
|
||||
Loading…
Reference in New Issue