From 18ced04c9664446e11f5b0ac1b13bed4d330d655 Mon Sep 17 00:00:00 2001 From: Lloyd Hilaiel <lloyd@hilaiel.com> Date: Tue, 2 Aug 2011 18:31:38 -0600 Subject: [PATCH] rework deployment configuration (using less of the express mechanism) so that configuration options are available at all levels --- browserid/app.js | 9 ++--- browserid/lib/email.js | 5 ++- libs/configuration.js | 85 ++++++++++++++++++++++++++++++++++++++++++ libs/environment.js | 85 ------------------------------------------ run.js | 5 +-- 5 files changed, 92 insertions(+), 97 deletions(-) create mode 100644 libs/configuration.js delete mode 100644 libs/environment.js diff --git a/browserid/app.js b/browserid/app.js index bef67d256..930a472b1 100644 --- a/browserid/app.js +++ b/browserid/app.js @@ -16,7 +16,7 @@ sessions = require('cookie-sessions'), express = require('express'), secrets = require('./lib/secrets.js'), db = require('./lib/db.js'), -environment = require('../libs/environment.js'), +configuration = require('../libs/configuration.js'), substitution = require('../libs/substitute.js'); // looks unused, see run.js @@ -35,7 +35,7 @@ function router(app) { app.set("views", __dirname + '/views'); app.set('view options', { - production: app.enabled('use_minified_resources') + production: configuration.get('use_minified_resources') }); // this should probably be an internal redirect @@ -175,11 +175,8 @@ exports.setup = function(server) { next(); }); - // configure environment variables based on the deployment target ('NODE_ENV'); - environment.configure(server); - // add middleware to re-write urls if needed - environment.performSubstitution(server); + configuration.performSubstitution(server); // add the actual URL handlers other than static router(server); diff --git a/browserid/lib/email.js b/browserid/lib/email.js index 19fb5d40b..9a5f56475 100644 --- a/browserid/lib/email.js +++ b/browserid/lib/email.js @@ -3,12 +3,13 @@ db = require('./db'), emailer = require('nodemailer'), fs = require('fs'), path = require('path'), -mustache = require('mustache'); +mustache = require('mustache'), +config = require('../../libs/configuration.js'); const template = fs.readFileSync(path.join(__dirname, "prove_template.txt")).toString(); exports.sendVerificationEmail = function(email, site, secret) { - var url = "https://browserid.org/prove?token=" + encodeURIComponent(secret); + var url = config.get('URL') + "/prove?token=" + encodeURIComponent(secret); emailer.send_mail({ sender: "noreply@browserid.org", diff --git a/libs/configuration.js b/libs/configuration.js new file mode 100644 index 000000000..b3cfa84f7 --- /dev/null +++ b/libs/configuration.js @@ -0,0 +1,85 @@ +/* + * An abstraction which contains various pre-set deployment + * environments and adjusts runtime configuration appropriate for + * the current environmnet (specified via the NODE_ENV env var).. + * + * usage is + * exports.configure(app); + */ + +const substitution = require('./substitute.js'); + +var g_config = { +}; + +// get the value for a given key, this mechanism allows the rest of the +// application to reach in and set +exports.get = function(val) { + if (val === 'env') return process.env['NODE_ENV']; + return g_config[val]; +} + +// various deployment configurations +const g_configs = { + production: { + hostname: 'browserid.org', + port: '443', + scheme: 'https', + use_minified_resources: true + }, + development: { + hostname: 'dev.diresworb.org', + port: '443', + scheme: 'https', + use_minified_resources: true + }, + beta: { + hostname: 'diresworb.org', + port: '443', + scheme: 'https', + use_minified_resources: true + }, + local: { + hostname: '127.0.0.1', + port: '10002', + scheme: 'http', + use_minified_resources: false + } +}; + +// default deployment is local +if (undefined === process.env['NODE_ENV']) { + process.env['NODE_ENV'] = 'local'; +} + +g_config = g_configs[process.env['NODE_ENV']]; + +if (g_config === undefined) throw "unknown environment: " + exports.get('env'); + +function getPortForURL() { + if (g_config['scheme'] === 'https' && g_config['port'] === '443') return ""; + if (g_config['scheme'] === 'http' && g_config['port'] === '80') return ""; + return ":" + g_config['port']; +} + +g_config['URL'] = g_config['scheme'] + '://' + g_config['hostname'] + getPortForURL(); + +/* + * Install middleware that will perform textual replacement on served output + * to re-write urls as needed for this particular environment. + * + * Note, for a 'local' environment, no re-write is needed because this is + * handled at a higher level. For a 'production' env no rewrite is necc cause + * all source files are written for that environment. + */ +exports.performSubstitution = function(app) { + if (process.env['NODE_ENV'] !== 'production' && + process.env['NODE_ENV'] !== 'local') { + app.use(substitution.substitute({ + 'https://browserid.org': g_config['URL'], + 'browserid.org:443': g_config['hostname'] + ':' + g_config['port'], + 'browserid.org': g_config['hostname'] + })); + } +}; + diff --git a/libs/environment.js b/libs/environment.js deleted file mode 100644 index 3bbfec25b..000000000 --- a/libs/environment.js +++ /dev/null @@ -1,85 +0,0 @@ -/* - * An abstraction which contains various pre-set deployment - * environments and adjusts runtime configuration appropriate for - * the current environmnet (specified via the NODE_ENV env var).. - * - * usage is - * exports.configure(app); - */ - -const substitution = require('./substitute.js'); - -if (undefined === process.env['NODE_ENV']) { - process.env['NODE_ENV'] = 'local'; -} - -exports.configure = function(app) { - if (!app) throw "configure requires express app as argument"; - - var known = false; - - app.enable('use_minified_resources'); - - app.configure('production', function() { - app.set('hostname', 'browserid.org'); - app.set('port', '443'); - app.set('scheme', 'https'); - known = true; - }); - - app.configure('development', function() { - app.set('hostname', 'dev.diresworb.org'); - app.set('port', '443'); - app.set('scheme', 'https'); - known = true; - }); - - app.configure('beta', function() { - app.set('hostname', 'diresworb.org'); - app.set('port', '443'); - app.set('scheme', 'https'); - known = true; - }); - - app.configure('local', function() { - app.set('hostname', '127.0.0.1'); - app.set('port', '10001'); - app.set('scheme', 'http'); - app.disable('use_minified_resources'); - known = true; - }); - - if (!known) throw "unknown environment: " + process.env('NODE_ENV'); - - function getPortForURL() { - if (app.set('scheme') === 'https' && app.set('port') === '443') return ""; - if (app.set('scheme') === 'http' && app.set('port') === '80') return ""; - return ":" + app.set('port'); - } - - app.set('URL', - app.set('scheme') + - '://' + - app.set('hostname') + - getPortForURL()); -}; - -/* - * Install middleware that will perform textual replacement on served output - * to re-write urls as needed for this particular environment. - * - * Note, for a 'local' environment, no re-write is needed because this is - * handled at a higher level. For a 'production' env no rewrite is necc cause - * all source files are written for that environment. - */ -exports.performSubstitution = function(app) { - if (process.env['NODE_ENV'] !== 'production' && - process.env['NODE_ENV'] !== 'local') { - app.use(substitution.substitute({ - 'https://browserid.org': app.set('URL'), - 'browserid.org:443': app.set('hostname') + ':' + app.set('port'), - 'browserid.org': app.set('hostname') - })); - } -}; - diff --git a/run.js b/run.js index 95046e55d..c9e78c405 100755 --- a/run.js +++ b/run.js @@ -9,7 +9,7 @@ var sys = require("sys"), fs = require("fs"), express = require("express"), substitution = require('./libs/substitute.js'), - environment = require('./libs/environment.js'); +configuration = require('./libs/configuration.js'); var PRIMARY_HOST = "127.0.0.1"; @@ -62,9 +62,6 @@ function substitutionMiddleware(req, resp, next) { function createServer(obj) { var app = express.createServer(); - // configure the server based on the environment (NODE_ENV). - environment.configure(app); - app.use(express.logger()); // this file is a *test* harness, to make it go, we'll insert a little -- GitLab