feat: validate environment variables at startup

This commit is contained in:
Isaac 2023-02-23 21:53:18 +00:00
parent 55985e95f0
commit ea3413d8cb
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
2 changed files with 70 additions and 12 deletions

61
src/env.js Normal file
View File

@ -0,0 +1,61 @@
/* eslint-disable no-console */
const dotenv = require('dotenv');
const { colours } = require('leeks.js');
const providers = ['mysql', 'postgresql', 'sqlite'];
// ideally the defaults would be set here too, but the pre-install script may run when `src/` is not available
const env = {
DB_CONNECTION_URL: v =>
!!v ||
(process.env.DB_PROVIDER === 'sqlite') ||
new Error('must be set when "DB_PROVIDER" is not "sqlite"'),
DB_PROVIDER: v =>
(!!v && providers.includes(v)) ||
new Error(`must be one of: ${providers.map(v => `"${v}"`).join(', ')}`),
DISCORD_SECRET: v =>
!!v ||
new Error('is required'),
DISCORD_TOKEN: v =>
!!v ||
new Error('is required'),
ENCRYPTION_KEY: v =>
(!!v && v.length >= 48) ||
new Error('is required and must be at least 48 characters long; run "npm run keygen" to generate a key'),
HTTP_EXTERNAL: v =>
(!!v && v.startsWith('http') && !v.endsWith('/')) ||
new Error('must be a valid URL without a trailing slash'),
HTTP_HOST: v =>
(!!v && !v.startsWith('http')) ||
new Error('is required and must be an address, not a URL'),
HTTP_PORT: v =>
!!v ||
new Error('is required'),
HTTP_TRUST_PROXY: () => true, // optional
OVERRIDE_ARCHIVE: () => true, // optional
PUBLIC_BOT: () => true, // optional
SETTINGS_HOST: v =>
(!!v && !v.startsWith('http')) ||
new Error('is required and must be an address, not a URL'),
SETTINGS_PORT: v =>
!!v ||
new Error('is required'),
SUPER: () => true, // optional
};
const load = options => {
dotenv.config(options);
Object.entries(env).forEach(([name, validate]) => {
const result = validate(process.env[name]); // `true` for pass, or `Error` for fail
if (result instanceof Error) {
console.log('\x07' + colours.redBright(`Error: The "${name}" environment variable ${result.message}.`));
process.exit(1);
}
});
};
module.exports = {
env,
load,
};

View File

@ -23,20 +23,12 @@
/* eslint-disable no-console */
process.env.NODE_ENV ??= 'development'; // make sure NODE_ENV is set
require('dotenv').config(); // load env file
const pkg = require('../package.json');
const banner = require('./lib/banner');
console.log(banner(pkg.version)); // print big title
const fs = require('fs');
const semver = require('semver');
const { colours } = require('leeks.js');
const logger = require('./lib/logger');
const YAML = require('yaml');
const Client = require('./client');
const http = require('./http');
// check node version
if (!semver.satisfies(process.versions.node, pkg.engines.node)) {
@ -44,10 +36,15 @@ if (!semver.satisfies(process.versions.node, pkg.engines.node)) {
process.exit(1);
}
if (process.env.ENCRYPTION_KEY === undefined) {
console.log('\x07' + colours.redBright('Error: The "ENCRYPTION_KEY" environment variable is not set.\nRun "npm run keygen" to generate a key.'));
process.exit(1);
}
// this could be done first, but then there would be no banner :(
process.env.NODE_ENV ??= 'development'; // make sure NODE_ENV is set
require('./env').load(); // load and check environment variables
const fs = require('fs');
const YAML = require('yaml');
const logger = require('./lib/logger');
const Client = require('./client');
const http = require('./http');
if (!fs.existsSync('./user/config.yml')) {
const examplePath = './user/example.config.yml';