Added required fields check to config manager

This commit is contained in:
Jonathan Barrow 2022-10-08 10:26:48 -04:00
parent 625bab0e25
commit 3b6a4eeb07
No known key found for this signature in database
GPG Key ID: E86E9FE9049C741F
3 changed files with 64 additions and 0 deletions

22
package-lock.json generated
View File

@ -27,6 +27,8 @@
"image-pixels": "^1.1.1",
"joi": "^17.6.1",
"kaitai-struct": "^0.9.0",
"lodash.has": "^4.5.2",
"lodash.set": "^4.3.2",
"mii-js": "github:PretendoNetwork/mii-js",
"moment": "^2.24.0",
"moment-timezone": "^0.5.27",
@ -2055,6 +2057,16 @@
"resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
"integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk="
},
"node_modules/lodash.has": {
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/lodash.has/-/lodash.has-4.5.2.tgz",
"integrity": "sha512-rnYUdIo6xRCJnQmbVFEwcxF144erlD+M3YcJUVesflU9paQaE8p+fJDcIQrlMYbxoANFL+AB9hZrzSBBk5PL+g=="
},
"node_modules/lodash.set": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz",
"integrity": "sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg=="
},
"node_modules/lowercase-keys": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz",
@ -5350,6 +5362,16 @@
"resolved": "https://registry.npmjs.org/lodash.get/-/lodash.get-4.4.2.tgz",
"integrity": "sha1-LRd/ZS+jHpObRDjVNBSZ36OCXpk="
},
"lodash.has": {
"version": "4.5.2",
"resolved": "https://registry.npmjs.org/lodash.has/-/lodash.has-4.5.2.tgz",
"integrity": "sha512-rnYUdIo6xRCJnQmbVFEwcxF144erlD+M3YcJUVesflU9paQaE8p+fJDcIQrlMYbxoANFL+AB9hZrzSBBk5PL+g=="
},
"lodash.set": {
"version": "4.3.2",
"resolved": "https://registry.npmjs.org/lodash.set/-/lodash.set-4.3.2.tgz",
"integrity": "sha512-4hNPN5jlm/N/HLMCO43v8BXKq9Z7QdAGc/VGrRD61w8gN9g/6jF9A4L1pbUgBLCffi0w9VsXfTOij5x8iTyFvg=="
},
"lowercase-keys": {
"version": "2.0.0",
"resolved": "https://registry.npmjs.org/lowercase-keys/-/lowercase-keys-2.0.0.tgz",

View File

@ -38,6 +38,8 @@
"image-pixels": "^1.1.1",
"joi": "^17.6.1",
"kaitai-struct": "^0.9.0",
"lodash.has": "^4.5.2",
"lodash.set": "^4.3.2",
"mii-js": "github:PretendoNetwork/mii-js",
"moment": "^2.24.0",
"moment-timezone": "^0.5.27",

View File

@ -1,4 +1,6 @@
const fs = require('fs-extra');
const has = require('lodash.has');
const set = require('lodash.set');
const logger = require('../logger');
require('dotenv').config();
@ -37,6 +39,24 @@ require('dotenv').config();
*/
let config = {};
const requiredFields = [
['http.port', 'PN_ACT_CONFIG_HTTP_PORT', Number],
['mongoose.uri', 'PN_ACT_CONFIG_MONGO_URI'],
['mongoose.database', 'PN_ACT_CONFIG_MONGO_DB_NAME'],
['redis.client.url', 'PN_ACT_CONFIG_REDIS_URL'],
['email.host', 'PN_ACT_CONFIG_EMAIL_HOST'],
['email.port', 'PN_ACT_CONFIG_EMAIL_PORT', Number],
['email.secure', 'PN_ACT_CONFIG_EMAIL_SECURE', Boolean],
['email.auth.user', 'PN_ACT_CONFIG_EMAIL_USERNAME'],
['email.auth.pass', 'PN_ACT_CONFIG_EMAIL_PASSWORD'],
['email.from', 'PN_ACT_CONFIG_EMAIL_FROM'],
['aws.spaces.key', 'PN_ACT_CONFIG_S3_ACCESS_KEY'],
['aws.spaces.secret', 'PN_ACT_CONFIG_S3_ACCESS_SECRET'],
['hcaptcha.secret', 'PN_ACT_CONFIG_HCAPTCHA_SECRET'],
['cdn_base', 'PN_ACT_CONFIG_CDN_BASE'],
['website_base', 'PN_ACT_CONFIG_WEBSITE_BASE'],
];
function configure() {
if (process.env.PN_ACT_PREFER_ENV_CONFIG === 'true') {
logger.info('Loading config from env');
@ -93,6 +113,26 @@ function configure() {
config = require(`${__dirname}/../config.json`);
}
logger.info('Config loaded, checking integrity');
for (const requiredField of requiredFields) {
const [keyPath, env, convertType] = requiredField;
if (!has(config, keyPath)) {
if (!process.env[env] || process.env[env].trim() === '') {
logger.error(`Failed to locate required field ${keyPath}. Set ${keyPath} in config.json or the ${env} environment variable`);
process.exit(0);
} else {
logger.info(`${keyPath} not found in config, using environment variable ${env}`);
const newValue = process.env[env];
set(config, keyPath, convertType ? convertType(newValue) : newValue);
}
}
}
module.exports.config = config;
}