From 79770c4c809326323cce933ca6cc0056d3dbd09a Mon Sep 17 00:00:00 2001 From: Julien Crestin Date: Tue, 7 Jun 2016 10:57:41 +0200 Subject: [PATCH 1/4] refacto(config): allow custom redis config --- config/config.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/config/config.js b/config/config.js index e2b6d77..6aa75e9 100644 --- a/config/config.js +++ b/config/config.js @@ -40,7 +40,8 @@ module.exports = function () { limit: { doc: 'The bundle transmission size limit, in kb.', format: 'nat', - default: 500 + default: 500, + env: 'LIMIT' }, password: { doc: 'The password that is used to create Freight bundles.', @@ -65,17 +66,20 @@ module.exports = function () { port: { doc: 'Redis Port', format: 'port', - default: 6379 + default: 6379, + env: 'REDIS_PORT' }, host: { doc: 'Redis IP address to bind.', - format: 'ipaddress', - default: '127.0.0.1' + format: String, + default: '127.0.0.1', + env: 'REDIS_HOST' }, auth: { doc: 'Redis Password.', format: String, - default: '' + default: '', + env: 'REDIS_PASSWORD' }, options: { doc: 'Redis Options.', From 9083f2ab8cf292da9292753c4e4fce24c4a0cbbf Mon Sep 17 00:00:00 2001 From: Julien Crestin Date: Tue, 7 Jun 2016 11:19:57 +0200 Subject: [PATCH 2/4] refacto(config): load config from env variables only --- config/config.js | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/config/config.js b/config/config.js index 6aa75e9..da87abd 100644 --- a/config/config.js +++ b/config/config.js @@ -6,12 +6,6 @@ var convict = require('convict'); module.exports = function () { - // Check if we need to auto configure for a fast start. - var env = process.env.NODE_ENV || 'dev'; - var configFile = process.env.FREIGHT_CONFIG || __dirname + '/' + env + '.json'; - - require('./autoconfig')(configFile); - var conf = convict({ env: { doc: 'The applicaton environment.', @@ -46,20 +40,23 @@ module.exports = function () { password: { doc: 'The password that is used to create Freight bundles.', format: String, - default: '' + default: '', + env: 'PASSWORD' }, storage: { // TODO: You need to create this directory if it does not exist. // This directory is also used as a static file directory for Freight bundles. doc: 'Default bundle storage directory. Make sure it is somewhere in the Freight Server directory.', format: String, - default: __dirname + '/../storage' + default: __dirname + '/../storage', + env: 'STORAGE_DIRECTORY' }, tempDir: { // TODO: You need to create this directory if it does not exist. doc: 'Default directory for temporary files.', format: String, - default: __dirname + '/../temp' + default: __dirname + '/../temp', + env: 'TEMP_DIRECTORY' }, // Redis config, see https://github.com/learnboost/kue#redis-connection-settings redis: { @@ -80,25 +77,18 @@ module.exports = function () { format: String, default: '', env: 'REDIS_PASSWORD' - }, - options: { - doc: 'Redis Options.', - format: Object, - default: {} } }, track: { delay: { doc: 'Repository update check delay in milliseconds', format: 'nat', - default: 60 * 60000 + default: 60 * 60000, + env: 'TRACK_DELAY' } } }); - // load environment dependent configuration - // TODO: development only for now, change it later. - conf.loadFile(configFile); // perform configuration validation conf.validate(); From 69ad3013a3d6a48b6ae69b825b0beb0c201420bd Mon Sep 17 00:00:00 2001 From: Julien Crestin Date: Wed, 8 Jun 2016 08:38:28 +0200 Subject: [PATCH 3/4] refacto(config): load file only if it exists --- config/autoconfig.js | 22 ---------------------- config/config.js | 17 ++++++++++++++++- 2 files changed, 16 insertions(+), 23 deletions(-) delete mode 100644 config/autoconfig.js diff --git a/config/autoconfig.js b/config/autoconfig.js deleted file mode 100644 index 7c7142d..0000000 --- a/config/autoconfig.js +++ /dev/null @@ -1,22 +0,0 @@ -var fs = require('fs'); -var crypto = require('crypto'); - -module.exports = function (expectedConfigFile) { - - if (! fs.existsSync(expectedConfigFile)) { - var buf = crypto.randomBytes(256); - var hash = crypto.createHash('sha1').update(buf).digest('hex'); - - // TODO: refactor - console.log('***** NOTICE ****** \n'); - console.log('You are missing "' + expectedConfigFile + '"'); - console.log('Creating a configuration automatically for you....'); - console.log('Your Freight Server password is: \n'); - console.log(hash); - console.log('\n Use the password above to generate bundles.'); - var devSampleFile = JSON.parse(fs.readFileSync(__dirname + '/dev.json-dist')); - devSampleFile.password = hash; - fs.writeFileSync(expectedConfigFile, JSON.stringify(devSampleFile), null, 4); - } - -}; diff --git a/config/config.js b/config/config.js index da87abd..f36f5cb 100644 --- a/config/config.js +++ b/config/config.js @@ -3,9 +3,13 @@ * See https://github.com/mozilla/node-convict/blob/master/README.md for details. */ var convict = require('convict'); +var fs = require('fs'); module.exports = function () { + var env = process.env.NODE_ENV || 'dev'; + var configFile = process.env.FREIGHT_CONFIG || __dirname + '/' + env + '.json'; + var conf = convict({ env: { doc: 'The applicaton environment.', @@ -41,7 +45,7 @@ module.exports = function () { doc: 'The password that is used to create Freight bundles.', format: String, default: '', - env: 'PASSWORD' + env: 'FREIGHT_PASSWORD' }, storage: { // TODO: You need to create this directory if it does not exist. @@ -77,6 +81,12 @@ module.exports = function () { format: String, default: '', env: 'REDIS_PASSWORD' + }, + // TODO : see how we can extract redis options from env variables + options: { + doc: 'Redis Options.', + format: Object, + default: {} } }, track: { @@ -91,6 +101,11 @@ module.exports = function () { // perform configuration validation conf.validate(); + // load environment dependent configuration + if (fs.existsSync(configFile)) { + // TODO: development only for now, change it later. + conf.loadFile(configFile); + } return conf; }; From ffd1ed3dd45cbc0d9451b54e6ff63b57f502f8f4 Mon Sep 17 00:00:00 2001 From: Julien Crestin Date: Wed, 8 Jun 2016 08:47:21 +0200 Subject: [PATCH 4/4] fix(config): only validate after load --- config/config.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config/config.js b/config/config.js index f36f5cb..35ddd3b 100644 --- a/config/config.js +++ b/config/config.js @@ -99,13 +99,13 @@ module.exports = function () { } }); - // perform configuration validation - conf.validate(); // load environment dependent configuration if (fs.existsSync(configFile)) { // TODO: development only for now, change it later. conf.loadFile(configFile); } + // perform configuration validation + conf.validate(); return conf; };