import { readFileSync } from "fs";
import { join } from "path";
/**
* Removes undefined values from an object
* @param {any} obj
* @return {any}
*/
function removeUndefined(obj: any): any {
Object.keys(obj).forEach((key) => obj[key] === undefined ? delete obj[key] : {});
return obj;
}
/**
* Configuration class
*/
class Config {
static machine: number;
static instance: number;
static mysql: {
host: string,
database: string,
password: string,
user: string,
port: string,
supportBigNumbers: true,
url: string
};
static isDev: boolean;
static domains: {
backend: string;
frontend: string;
files: string;
};
static paypal: {
base: string,
secret: string,
public: string,
planId: string
};
static s3: {
encryption?: string,
accessKeyId?: string,
secretAccessKey?: string,
bucket?: string,
endpoint?: string
};
static captcha: {
secret: string
};
/**
* Loads the configuration
* @return {void}
*/
static load() {
var config: Record<string, any> = {};
try {
config = JSON.parse(readFileSync(join(__dirname, "../../config.json"), "utf-8"));
} catch(e) {
console.warn("Couldn't read config. Make sure it's in place and is valid json.");
}
Config.machine = process.env.MACHINE || config.machine || 0;
Config.instance = process.env.INSTANCE || config.instance || 0;
Config.mysql = Object.assign(config.mysql || {}, removeUndefined({
host: process.env.MYSQL_HOST,
database: process.env.MYSQL_DATABASE,
password: process.env.MYSQL_PASSWORD,
user: process.env.MYSQL_USER,
port: process.env.MYSQL_PORT,
supportBigNumbers: true,
url: process.env.MYSQL_URL
}));
Config.isDev = process.env.DEV || config.dev || false;
// @ts-ignore
if(Config.isDev.toString().toLowerCase() === "false") Config.isDev = false;
Config.paypal = Object.assign(config.paypal || {}, removeUndefined({
public: process.env.PAYPAL_PUBLIC,
secret: process.env.PAYPAL_SECRET,
base: process.env.PAYPAL_BASE,
planId: process.env.PAYPAL_PLAN_ID
}));
Config.s3 = Object.assign(config.s3 || {}, removeUndefined({
encryption: process.env.S3_ENCRYPTION,
accessKeyId: process.env.S3_ACCESS_KEY_ID,
secretAccessKey: process.env.S3_SECRET_ACCESS_KEY,
endpoint: process.env.S3_ENDPOINT,
bucket: process.env.S3_BUCKET
}));
Config.domains = Object.assign(config.domains || {}, removeUndefined({
backend: process.env.DOMAIN_BACKEND,
frontend: process.env.DOMAIN_FRONTEND,
files: process.env.DOMAIN_FILES
}));
Config.captcha = Object.assign(config.captcha || {}, removeUndefined({
secret: process.env.CAPTCHA_SECRET
}));
if(!Config.mysql) {
console.warn("MySQL connection details required");
process.exit(1);
} else {
console.log("Loaded config");
console.log(this);
}
}
}
Config.load();
export default Config;
Source