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..e34d87c16b --- /dev/null +++ b/test/api/v3/unit/libs/setupNconf.test.js @@ -0,0 +1,31 @@ +import setupNconf from '../../../../../website/src/libs/api-v3/setupNconf'; + +import nconf from 'nconf'; + +describe('setupNconf', () => { + before(() => { + sandbox.spy(nconf, 'argv'); + sandbox.spy(nconf, 'env'); + 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; + }); + + 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/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')(); 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 { 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'); +} 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"));