From 0d7f984fcefe93383c1c62e9d717bdc3c43952ab Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 8 Nov 2015 08:47:46 -0600 Subject: [PATCH 1/5] Add setupNconf function --- test/api/v3/unit/libs/setupNconf.test.js | 41 ++++++++++++++++++++++++ website/src/libs/api-v3/setupNconf.js | 14 ++++++++ 2 files changed, 55 insertions(+) create mode 100644 test/api/v3/unit/libs/setupNconf.test.js create mode 100644 website/src/libs/api-v3/setupNconf.js diff --git a/test/api/v3/unit/libs/setupNconf.test.js b/test/api/v3/unit/libs/setupNconf.test.js new file mode 100644 index 0000000000..0dc0c3dbb5 --- /dev/null +++ b/test/api/v3/unit/libs/setupNconf.test.js @@ -0,0 +1,41 @@ +import setupNconf from '../../../../../website/src/libs/api-v3/setupNconf'; + +import nconf from 'nconf'; + +describe('setupNconf', () => { + afterEach(() => { + sandbox.restore(); + }); + + it('sets up nconf to load command line arguments', () => { + sandbox.spy(nconf, 'argv'); + + setupNconf(); + + expect(nconf.argv).to.be.calledOnce; + }); + + it('sets up nconf to load environmental variables', () => { + sandbox.spy(nconf, 'env'); + + setupNconf(); + + expect(nconf.env).to.be.calledOnce; + }); + + it('sets up nconf to load variables from config file', () => { + sandbox.spy(nconf, 'file'); + + setupNconf(); + + expect(nconf.file).to.be.calledOnce; + }); + + it('sets IS_PROD variable', () => { + expect(nconf.get('IS_PROD')).to.exist; + }); + + it('sets IS_DEV variable', () => { + expect(nconf.get('IS_DEV')).to.exist; + }); +}); diff --git a/website/src/libs/api-v3/setupNconf.js b/website/src/libs/api-v3/setupNconf.js new file mode 100644 index 0000000000..ff0da651df --- /dev/null +++ b/website/src/libs/api-v3/setupNconf.js @@ -0,0 +1,14 @@ +import nconf from 'nconf'; +import { join, resolve } from 'path'; + +const PATH_TO_CONFIG = join(resolve(__dirname, '../../../../config.json')); + +export default function setupNconf () { + nconf + .argv() + .env() + .file('user', PATH_TO_CONFIG); + + nconf.set('IS_PROD', nconf.get('NODE_ENV') === 'production'); + nconf.set('IS_DEV', nconf.get('NODE_ENV') === 'development'); +} From 1ef6839eea0cf46d577410152eabaae3f09570f5 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 8 Nov 2015 08:49:16 -0600 Subject: [PATCH 2/5] Add IS_PROD to logger utility --- website/src/libs/api-v3/logger.js | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/website/src/libs/api-v3/logger.js b/website/src/libs/api-v3/logger.js index b94d492b68..4583610d25 100644 --- a/website/src/libs/api-v3/logger.js +++ b/website/src/libs/api-v3/logger.js @@ -3,12 +3,11 @@ import winston from 'winston'; import nconf from 'nconf'; -// TODO move isProd to a single location -const isProd = nconf.get('NODE_ENV') === 'production'; +const IS_PROD = nconf.get('IS_PROD'); let logger = new winston.Logger(); -if (isProd) { +if (IS_PROD) { // TODO production logging, use loggly // log errors to console too } else { From 447d4c332d8bf608f33616e835476b40c14f55f0 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 8 Nov 2015 08:56:27 -0600 Subject: [PATCH 3/5] Remove extraneous nconf setup. --- website/src/libs/utils.js | 22 +++++++--------------- website/src/server.js | 15 ++++++++------- 2 files changed, 15 insertions(+), 22 deletions(-) diff --git a/website/src/libs/utils.js b/website/src/libs/utils.js index 1b1231e102..c8944c9ccc 100644 --- a/website/src/libs/utils.js +++ b/website/src/libs/utils.js @@ -4,8 +4,8 @@ var crypto = require('crypto'); var path = require("path"); var request = require('request'); -// Set when utils.setupConfig is run -var isProd, baseUrl; +const IS_PROD = nconf.get('IS_PROD'); +const BASE_URL = nconf.get('BASE_URL'); module.exports.sendEmail = function(mailData) { var smtpTransport = nodemailer.createTransport("SMTP",{ @@ -59,7 +59,7 @@ module.exports.txnEmail = function(mailingInfoArray, emailType, variables, perso var mailingInfoArray = Array.isArray(mailingInfoArray) ? mailingInfoArray : [mailingInfoArray]; var variables = [ - {name: 'BASE_URL', content: baseUrl} + {name: 'BASE_URL', content: BASE_URL} ].concat(variables || []); // It's important to pass at least a user with its `preferences` as we need to check if he unsubscribed @@ -120,7 +120,7 @@ module.exports.txnEmail = function(mailingInfoArray, emailType, variables, perso }); } - if(isProd && mailingInfoArray.length > 0){ + if(IS_PROD && mailingInfoArray.length > 0){ request({ url: nconf.get('EMAIL_SERVER:url') + '/job', method: 'POST', @@ -167,20 +167,12 @@ module.exports.analytics = { track: function() { }, trackPurchase: function() { * Load nconf and define default configuration values if config.json or ENV vars are not found */ module.exports.setupConfig = function(){ - nconf.argv() - .env() - //.file('defaults', path.join(path.resolve(__dirname, '../config.json.example'))) - .file('user', path.join(path.resolve(__dirname, './../../../config.json'))); - - if (nconf.get('NODE_ENV') === "development") + if (nconf.get('IS_DEV')) Error.stackTraceLimit = Infinity; - //if (nconf.get('NODE_ENV') === 'production') + //if (nconf.get('IS_PROD')) //require('newrelic'); - isProd = nconf.get('NODE_ENV') === 'production'; - baseUrl = nconf.get('BASE_URL'); - - var analytics = isProd && require('./analytics'); + var analytics = IS_PROD && require('./analytics'); var analyticsTokens = { amplitudeToken: nconf.get('AMPLITUDE_KEY'), googleAnalytics: nconf.get('GA_ID') diff --git a/website/src/server.js b/website/src/server.js index fad04b5ff1..bea0da1428 100644 --- a/website/src/server.js +++ b/website/src/server.js @@ -1,4 +1,5 @@ require('babel/register'); +require('./libs/api-v3/setupNconf')(); // Only do the minimal amount of work before forking just in case of a dyno restart var cluster = require("cluster"); var _ = require('lodash'); @@ -6,12 +7,12 @@ var nconf = require('nconf'); var utils = require('./libs/utils'); utils.setupConfig(); var logging = require('./libs/logging'); -var isProd = nconf.get('NODE_ENV') === 'production'; -var isDev = nconf.get('NODE_ENV') === 'development'; +var IS_PROD = nconf.get('IS_PROD'); +var IS_DEV = nconf.get('IS_DEV'); var DISABLE_LOGGING = nconf.get('DISABLE_REQUEST_LOGGING'); var cores = +nconf.get("WEB_CONCURRENCY") || 0; -if (cores!==0 && cluster.isMaster && (isDev || isProd)) { +if (cores!==0 && cluster.isMaster && (IS_DEV || IS_PROD)) { // Fork workers. If config.json has CORES=x, use that - otherwise, use all cpus-1 (production) for (var i = 0; i < cores; i += 1) { cluster.fork(); @@ -41,7 +42,7 @@ if (cores!==0 && cluster.isMaster && (isDev || isProd)) { var mongoose = require('mongoose'); // Use Q promises instead of mpromise in mongoose mongoose.Promise = require('q'); - var mongooseOptions = !isProd ? {} : { + var mongooseOptions = !IS_PROD ? {} : { replset: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } }, server: { socketOptions: { keepAlive: 1, connectTimeoutMS: 30000 } } }; @@ -113,7 +114,7 @@ if (cores!==0 && cluster.isMaster && (isDev || isProd)) { /* OLD APP IS DISABLED UNTIL COMPATIBLE WITH NEW MODELS //require('./middlewares/apiThrottle')(oldApp); oldApp.use(require('./middlewares/domain')(server,mongoose)); - if (!isProd && !DISABLE_LOGGING) oldApp.use(require('morgan')("dev")); + if (!IS_PROD && !DISABLE_LOGGING) oldApp.use(require('morgan')("dev")); oldApp.use(require('compression')()); oldApp.set("views", __dirname + "/../views"); oldApp.set("view engine", "jade"); @@ -161,7 +162,7 @@ if (cores!==0 && cluster.isMaster && (isDev || isProd)) { oldApp.use('/export', require('./routes/dataexport')); require('./routes/api-v2/swagger')(swagger, v2); - var maxAge = isProd ? 31536000000 : 0; + var maxAge = IS_PROD ? 31536000000 : 0; // Cache emojis without copying them to build, they are too many oldApp.use(express['static'](path.join(__dirname, "/../build"), { maxAge: maxAge })); oldApp.use('/common/dist', express['static'](publicDir + "/../../common/dist", { maxAge: maxAge })); @@ -172,7 +173,7 @@ if (cores!==0 && cluster.isMaster && (isDev || isProd)) { oldApp.use(require('./middlewares/errorHandler')); */ - + server.on('request', app); server.listen(app.get("port"), function() { return logging.info("Express server listening on port " + app.get("port")); From 9451e7239b72e8b6348bac04c9d648debad8cf23 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 8 Nov 2015 09:00:07 -0600 Subject: [PATCH 4/5] Add nconf setup to unit test helper --- test/helpers/globals.helper.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/helpers/globals.helper.js b/test/helpers/globals.helper.js index 32ec18f79e..4a166ea184 100644 --- a/test/helpers/globals.helper.js +++ b/test/helpers/globals.helper.js @@ -8,5 +8,9 @@ global.sinon = require("sinon"); chai.use(require("sinon-chai")) chai.use(require("chai-as-promised")); global.expect = chai.expect - global.sandbox = sinon.sandbox.create(); + +//------------------------------ +// Load nconf for unit tests +//------------------------------ +require('../../website/src/libs/api-v3/setupNconf')(); From 832e837f6a26c1f14bed0003cc89fcb893a2e28b Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Sun, 8 Nov 2015 09:07:13 -0600 Subject: [PATCH 5/5] Simplify nconf test --- test/api/v3/unit/libs/setupNconf.test.js | 28 ++++++++---------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/test/api/v3/unit/libs/setupNconf.test.js b/test/api/v3/unit/libs/setupNconf.test.js index 0dc0c3dbb5..e34d87c16b 100644 --- a/test/api/v3/unit/libs/setupNconf.test.js +++ b/test/api/v3/unit/libs/setupNconf.test.js @@ -3,31 +3,21 @@ import setupNconf from '../../../../../website/src/libs/api-v3/setupNconf'; import nconf from 'nconf'; describe('setupNconf', () => { - afterEach(() => { - sandbox.restore(); - }); - - it('sets up nconf to load command line arguments', () => { + before(() => { sandbox.spy(nconf, 'argv'); - - setupNconf(); - - expect(nconf.argv).to.be.calledOnce; - }); - - it('sets up nconf to load environmental variables', () => { sandbox.spy(nconf, 'env'); - - setupNconf(); - - expect(nconf.env).to.be.calledOnce; - }); - - it('sets up nconf to load variables from config file', () => { sandbox.spy(nconf, 'file'); setupNconf(); + }); + after(() => { + sandbox.restore(); + }); + + it('sets up nconf', () => { + expect(nconf.argv).to.be.calledOnce; + expect(nconf.env).to.be.calledOnce; expect(nconf.file).to.be.calledOnce; });