feat(server): configuration through environment

This commit is contained in:
AlphaNecron 2021-10-03 20:27:00 +07:00
parent 893fa1789e
commit b780dfe02c
3 changed files with 89 additions and 4 deletions

View File

@ -12,7 +12,7 @@ const validator = yup.object({
enabled: yup.bool().default(false),
prefix: yup.string().min(1).required(),
token: yup.string(),
admin: yup.array().default([]),
admins: yup.array().default([]),
log_channel: yup.string(),
hostname: yup.string().required()
}),

View File

@ -2,14 +2,98 @@ const { join } = require('path');
const { info, error } = require('./logger');
const { existsSync, readFileSync } = require('fs');
const e = (val, type, fn) => ({ val, type, fn });
const envValues = [
e('SECURE', 'boolean', (c, v) => c.core.secure = v),
e('SECRET', 'string', (c, v) => c.core.secret = v),
e('HOST', 'string', (c, v) => c.core.host = v),
e('PORT', 'number', (c, v) => c.core.port = v),
e('DATABASE_URL', 'string', (c, v) => c.core.database_url = v),
e('UPLOADER_RAW_ROUTE', 'string', (c, v) => c.uploader.raw_route = v),
e('UPLOADER_LENGTH', 'number', (c, v) => c.uploader.length = v),
e('UPLOADER_DIRECTORY', 'string', (c, v) => c.uploader.directory = v),
e('UPLOADER_BLACKLISTED', 'array', (c, v) => v ? c.uploader.blacklisted = v : c.uploader.blacklisted = []),
e('BOT_ENABLED', 'boolean', (c, v) => c.bot.enabled = v),
e('BOT_PREFIX', 'string', (c, v) => c.bot.prefix = v),
e('BOT_TOKEN', 'string', (c, v) => c.bot.token = v),
e('BOT_ADMINS', 'array', (c, v) => v ? c.bot.admins = v : c.bot.admins = []),
e('BOT_LOG_CHANNEL', 'string', (c, v) => c.bot.log_channel = v),
e('BOT_HOSTNAME', 'string', (c, v) => c.bot.hostname = v),
e('SHORTENER_ROUTE', 'string', (c, v) => c.shortener.route = v),
e('SHORTENER_LENGTH', 'number', (c, v) => c.shortener.length = v)
];
module.exports = () => {
if (!existsSync(join(process.cwd(), 'config.toml'))) {
error('CONFIG', 'Config file not found, please create one.');
process.exit(1);
return tryReadEnv();
} else {
info('CONFIG', 'Reading config file');
const str = readFileSync(join(process.cwd(), 'config.toml'), 'utf8');
const parsed = require('@iarna/toml/parse-string')(str);
return parsed;
}
};
};
function tryReadEnv() {
const config = {
core: {
secure: undefined,
secret: undefined,
host: undefined,
port: undefined,
database_url: undefined,
},
uploader: {
raw_route: undefined,
length: undefined,
directory: undefined,
blacklisted: undefined,
},
bot: {
enabled: undefined,
prefix: undefined,
admins: undefined,
log_channel: undefined,
hostname: undefined
},
shortener: {
route: undefined,
length: undefined,
},
};
for (let i = 0, L = envValues.length; i !== L; ++i) {
const envValue = envValues[i];
let value = process.env[envValue.val];
if (!value) {
envValues[i].fn(config, undefined);
} else {
envValues[i].fn(config, value);
if (envValue.type === 'number') value = parseToNumber(value);
else if (envValue.type === 'boolean') value = parseToBoolean(value);
else if (envValue.type === 'array') value = parseToArray(value);
envValues[i].fn(config, value);
}
}
return config;
}
function parseToNumber(value) {
const number = Number(value);
if (isNaN(number)) return undefined;
return number;
}
function parseToBoolean(value) {
if (!value || value === 'false') return false;
else return true;
}
function parseToArray(value) {
return value.split(',');
}

View File

@ -10,7 +10,7 @@ export interface Bot {
enabled: boolean;
prefix: string;
token: string;
admin: string[];
admins: string[];
log_channel: string;
hostname: string;
}
@ -22,6 +22,7 @@ export interface Shortener {
}
export interface Uploader {
raw_route: string;
length: number;
directory: string;
blacklisted: string[];