diff --git a/src/config/env/.env_example b/src/config/env/.env_example index 3cc074f..7243d28 100644 --- a/src/config/env/.env_example +++ b/src/config/env/.env_example @@ -1,3 +1,2 @@ -MONGO_DB_HOST="YOUR_MONGO_DB_URI" COOKIE_SECRET="YOUR_COOKIE_SECRET" SSL_PASS_PHRASE="YOUR_SSL_PASS_PHRASE" \ No newline at end of file diff --git a/src/dotenv/index.js b/src/dotenv/index.js new file mode 100644 index 0000000..989f84c --- /dev/null +++ b/src/dotenv/index.js @@ -0,0 +1,21 @@ +const dotenv = require('dotenv'); +const util = require('../util'); + +function configureEnv() { + dotenv.config({ path: __dirname + '/../config/env/.env' }); + checkEnvVariables(); +} + +function checkEnvVariables() { + const envList = ['SSL_PASS_PHRASE', 'COOKIE_SECRET']; + envList.forEach((envVar) => { + if (util.checkEnvVariable(envVar, 'string') === false) { + throw new Error(`Missing ENV variable ${envVar}. Check ./src/config/env`); + } + }); +} + + +module.exports = { + configureEnv, +}; diff --git a/src/index.js b/src/index.js index b3d2de4..320192f 100644 --- a/src/index.js +++ b/src/index.js @@ -1,10 +1,9 @@ module.exports.init = function() { - const dotenv = require('dotenv'); - dotenv.config({ path: __dirname + '/config/env/.env' }); + const { configureEnv } = require('./dotenv'); + configureEnv(); const logger = require('./logger'); logger.configureLog(); - const server = require('./server'); server.configureServer(); }; diff --git a/src/util/index.js b/src/util/index.js new file mode 100644 index 0000000..1d3627b --- /dev/null +++ b/src/util/index.js @@ -0,0 +1,6 @@ +const _ = require('lodash'); + +module.exports.checkEnvVariable = function(variable, type) { + const envVar = process.env[variable]; + return !_.isNil(envVar) && typeof envVar === 'string'; +};