78 lines
3.5 KiB
TypeScript
78 lines
3.5 KiB
TypeScript
import "dotenv/config";
|
|
import ms from "ms";
|
|
import ip from "ip";
|
|
|
|
export const DB_TYPE = process.env.DB_TYPE ?? "mysql";
|
|
export const DB_HOST = process.env.DB_HOST ?? "";
|
|
export const DB_PORT = Number(process.env.DB_PORT ?? 3306);
|
|
export const DB_NAME = process.env.DB_NAME ?? "";
|
|
export const DB_USERNAME = process.env.DB_USERNAME ?? "";
|
|
export const DB_PASSWORD = process.env.DB_PASSWORD ?? "";
|
|
|
|
export const SERVER_PORT = Number(process.env.SERVER_PORT ?? 5000);
|
|
|
|
export const APPLICATION_SECRET = process.env.APPLICATION_SECRET;
|
|
|
|
export const USE_SECURITY_STRICT_LIMIT = process.env.USE_SECURITY_STRICT_LIMIT ?? "true";
|
|
export const SECURITY_STRICT_LIMIT_WINDOW = (process.env.SECURITY_STRICT_LIMIT_WINDOW ?? "15m") as ms.StringValue;
|
|
export const SECURITY_STRICT_LIMIT_REQUEST_COUNT = Number(process.env.SECURITY_STRICT_LIMIT_REQUEST_COUNT ?? "15");
|
|
export const USE_SECURITY_LIMIT = process.env.USE_SECURITY_LIMIT ?? "true";
|
|
export const SECURITY_LIMIT_WINDOW = (process.env.SECURITY_LIMIT_WINDOW ?? "1m") as ms.StringValue;
|
|
export const SECURITY_LIMIT_REQUEST_COUNT = Number(process.env.SECURITY_LIMIT_REQUEST_COUNT ?? "500");
|
|
|
|
export const TRUST_PROXY = ((): Array<string> | string | boolean | number | null => {
|
|
const proxyVal = process.env.TRUST_PROXY;
|
|
if (!proxyVal) return null;
|
|
if (proxyVal == "true" || proxyVal == "false") {
|
|
return proxyVal == "true";
|
|
}
|
|
if (!isNaN(Number(proxyVal))) {
|
|
return Number(proxyVal);
|
|
}
|
|
if (proxyVal.includes(",") && proxyVal.split(",").every((pv) => ip.isV4Format(pv) || ip.isV6Format(pv))) {
|
|
return proxyVal.split(",");
|
|
}
|
|
if (ip.isV4Format(proxyVal) || ip.isV6Format(proxyVal)) {
|
|
return proxyVal;
|
|
}
|
|
return null;
|
|
})();
|
|
|
|
export function configCheck() {
|
|
if (DB_TYPE != "mysql" && DB_TYPE != "sqlite" && DB_TYPE != "postgres")
|
|
throw new Error("set valid value to DB_TYPE (mysql|sqlite|postgres)");
|
|
if ((DB_HOST == "" || typeof DB_HOST != "string") && DB_TYPE != "sqlite")
|
|
throw new Error("set valid value to DB_HOST");
|
|
if (DB_NAME == "" || typeof DB_NAME != "string") throw new Error("set valid value to DB_NAME");
|
|
if ((DB_USERNAME == "" || typeof DB_USERNAME != "string") && DB_TYPE != "sqlite")
|
|
throw new Error("set valid value to DB_USERNAME");
|
|
if ((DB_PASSWORD == "" || typeof DB_PASSWORD != "string") && DB_TYPE != "sqlite")
|
|
throw new Error("set valid value to DB_PASSWORD");
|
|
|
|
if (isNaN(SERVER_PORT)) throw new Error("set valid numeric value to SERVER_PORT");
|
|
|
|
if (USE_SECURITY_STRICT_LIMIT != "true" && USE_SECURITY_STRICT_LIMIT != "false")
|
|
throw new Error("set 'true' or 'false' to USE_SECURITY_STRICT_LIMIT");
|
|
checkMS(SECURITY_STRICT_LIMIT_WINDOW, "SECURITY_STRICT_LIMIT_WINDOW");
|
|
if (isNaN(SECURITY_STRICT_LIMIT_REQUEST_COUNT))
|
|
throw new Error("set valid numeric value to SECURITY_STRICT_LIMIT_REQUEST_COUNT");
|
|
if (USE_SECURITY_LIMIT != "true" && USE_SECURITY_LIMIT != "false")
|
|
throw new Error("set 'true' or 'false' to USE_SECURITY_LIMIT");
|
|
checkMS(SECURITY_LIMIT_WINDOW, "SECURITY_LIMIT_WINDOW");
|
|
if (isNaN(SECURITY_LIMIT_REQUEST_COUNT)) throw new Error("set valid numeric value to SECURITY_LIMIT_REQUEST_COUNT");
|
|
|
|
if (!TRUST_PROXY && process.env.TRUST_PROXY) {
|
|
throw new Error("set valid boolean, number, ip or ips value to TRUST_PROXY");
|
|
}
|
|
}
|
|
|
|
function checkMS(input: ms.StringValue, origin: string) {
|
|
try {
|
|
const result = ms(input);
|
|
if (result === undefined) {
|
|
throw new Error(`set valid ms value to ${origin} -> [0-9]*(y|d|h|m|s)`);
|
|
}
|
|
} catch (e) {
|
|
throw new Error(`set valid ms value to ${origin} -> [0-9]*(y|d|h|m|s)`);
|
|
}
|
|
}
|