ls-deno/config.ts

52 lines
1.1 KiB
TypeScript
Raw Normal View History

2022-10-11 23:49:36 -05:00
import {
type LevelName,
LogLevels,
} from "https://deno.land/std@0.159.0/log/mod.ts";
2022-10-11 12:20:25 -05:00
2022-10-07 23:22:35 -05:00
export interface Config {
2022-10-11 23:49:36 -05:00
log: {
consoleLevelName: LevelName;
};
2022-10-07 23:22:35 -05:00
postgres: {
url: string;
};
mailgun: {
apiKey?: string;
domain?: string;
};
}
2022-10-07 21:55:20 -05:00
2022-10-11 23:49:36 -05:00
function envOrWarn(key: string, fallback: string): string {
2022-10-07 23:22:35 -05:00
const val = Deno.env.get(key);
2022-10-11 23:49:36 -05:00
if (!val) console.warn(`${key} is not set! Using fallback: ${fallback}`);
2022-10-07 23:22:35 -05:00
return val || fallback;
}
2022-10-07 21:55:20 -05:00
2022-10-11 23:49:36 -05:00
function isLogLevelName(s: string): s is LevelName {
return s in LogLevels;
}
const desiredLogLevel = envOrWarn("LOG_LEVEL", "INFO").toUpperCase();
if (!isLogLevelName(desiredLogLevel)) {
console.warn(
`Desired LOG_LEVEL of '${desiredLogLevel}' is invalid. Falling back to 'INFO'`,
);
}
const logLevel: LevelName = isLogLevelName(desiredLogLevel)
? desiredLogLevel
: "INFO";
export const config: Config = {
log: {
consoleLevelName: logLevel,
},
2022-10-07 23:22:35 -05:00
postgres: {
url: envOrWarn(
"POSTGRES_URL",
"postgresql://postgres:@127.0.0.1:5432/lyricscreen",
),
},
2022-10-11 23:49:36 -05:00
mailgun: {},
2022-10-07 23:22:35 -05:00
};