Skip to content

Commit

Permalink
env check
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanchenxi95 committed Nov 17, 2017
1 parent 04bbb2f commit b8ac526
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
1 change: 0 additions & 1 deletion src/config/env/.env_example
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
MONGO_DB_HOST="YOUR_MONGO_DB_URI"
COOKIE_SECRET="YOUR_COOKIE_SECRET"
SSL_PASS_PHRASE="YOUR_SSL_PASS_PHRASE"
21 changes: 21 additions & 0 deletions src/dotenv/index.js
Original file line number Diff line number Diff line change
@@ -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,
};
5 changes: 2 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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();
};
6 changes: 6 additions & 0 deletions src/util/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const _ = require('lodash');

module.exports.checkEnvVariable = function(variable, type) {
const envVar = process.env[variable];
return !_.isNil(envVar) && typeof envVar === 'string';
};

0 comments on commit b8ac526

Please sign in to comment.