From 0c92c4a04806e3675c251ddb166743d087a6e245 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 4 Aug 2015 08:42:53 -0500 Subject: [PATCH 1/6] Switch redirect to baseUrl --- website/src/middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/middleware.js b/website/src/middleware.js index f00b163b3b..e9b2a4efff 100644 --- a/website/src/middleware.js +++ b/website/src/middleware.js @@ -125,7 +125,7 @@ function nonApiUrl(req) { module.exports.forceHabitica = function(req, res, next) { if(nconf.get('NODE_ENV') === 'production' && !isProxied(req) && nonApiUrl(req)) { - return res.redirect('https://habitica.com' + req.url); + return res.redirect(baseUrl + req.url); } next(); }; From 5731f0073632fbf39c369c87bfc60e3a19dd0b87 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 4 Aug 2015 12:09:54 -0500 Subject: [PATCH 2/6] Add baseUrl variable --- website/src/middleware.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/website/src/middleware.js b/website/src/middleware.js index e9b2a4efff..c560188d82 100644 --- a/website/src/middleware.js +++ b/website/src/middleware.js @@ -124,6 +124,8 @@ function nonApiUrl(req) { } module.exports.forceHabitica = function(req, res, next) { + var baseUrl = nconf.get("BASE_URL"); + if(nconf.get('NODE_ENV') === 'production' && !isProxied(req) && nonApiUrl(req)) { return res.redirect(baseUrl + req.url); } From 675808e54ebc735d4405c3c0e40aea66e1c34406 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 4 Aug 2015 18:16:36 -0500 Subject: [PATCH 3/6] Catch habitrpg.com specifically --- website/src/middleware.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/website/src/middleware.js b/website/src/middleware.js index c560188d82..5aab362dfb 100644 --- a/website/src/middleware.js +++ b/website/src/middleware.js @@ -124,9 +124,11 @@ function nonApiUrl(req) { } module.exports.forceHabitica = function(req, res, next) { - var baseUrl = nconf.get("BASE_URL"); + var baseUrl = nconf.get('BASE_URL'); + var isProd = nconf.get('NODE_ENV') === 'production'; + var isHabitRPG = req.headers.host === 'habitrpg.com'; - if(nconf.get('NODE_ENV') === 'production' && !isProxied(req) && nonApiUrl(req)) { + if(isProd && isHabitRPG && !isProxied(req) && nonApiUrl(req)) { return res.redirect(baseUrl + req.url); } next(); From 93a2d3d660df567edfda8b2acc1564f010b619ad Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 4 Aug 2015 18:30:27 -0500 Subject: [PATCH 4/6] Use constants for is_prod and base_url --- website/src/middleware.js | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/website/src/middleware.js b/website/src/middleware.js index 5aab362dfb..329ec7aa61 100644 --- a/website/src/middleware.js +++ b/website/src/middleware.js @@ -14,8 +14,11 @@ var os = require('os'); var moment = require('moment'); var utils = require('./utils'); +var IS_PROD = nconf.get('NODE_ENV') === 'production'; +var BASE_URL = nconf.get("BASE_URL"); + module.exports.apiThrottle = function(app) { - if (nconf.get('NODE_ENV') !== 'production') return; + if (!IS_PROD) return; app.use(limiter({ end:false, catagories:{ @@ -33,7 +36,7 @@ module.exports.apiThrottle = function(app) { } module.exports.domainMiddleware = function(server,mongoose) { - if (nconf.get('NODE_ENV')=='production') { + if (IS_PROD) { var mins = 3, // how often to run this check useAvg = false, // use average over 3 minutes, or simply the last minute's report url = 'https://api.newrelic.com/v2/applications/'+nconf.get('NEW_RELIC_APPLICATION_ID')+'/metrics/data.json?names[]=Apdex&values[]=score'; @@ -88,13 +91,11 @@ module.exports.errorHandler = function(err, req, res, next) { } function isHTTP(req) { - var baseUrl = nconf.get("BASE_URL"); - return ( req.headers['x-forwarded-proto'] && req.headers['x-forwarded-proto'] !== 'https' && - nconf.get('NODE_ENV') === 'production' && - baseUrl.indexOf('https') === 0 + IS_PROD && + BASE_URL.indexOf('https') === 0 ); } @@ -106,30 +107,26 @@ function isProxied(req) { } module.exports.forceSSL = function(req, res, next){ - var baseUrl = nconf.get("BASE_URL"); - if(isHTTP(req) && !isProxied(req)) { - return res.redirect(baseUrl + req.url); + return res.redirect(BASE_URL + req.url); } next(); } // Redirect to habitica for non-api urls -// NOTE: Currently using a static 'habitica.com' string, rather than baseUrl, -// to make rollback easy. Eventually, baseUrl should be migrated. +// NOTE: Currently using a static 'habitica.com' string, rather than BASE_URL, +// to make rollback easy. Eventually, BASE_URL should be migrated. function nonApiUrl(req) { return req.url.search(/\/api\//) === -1; } module.exports.forceHabitica = function(req, res, next) { - var baseUrl = nconf.get('BASE_URL'); - var isProd = nconf.get('NODE_ENV') === 'production'; var isHabitRPG = req.headers.host === 'habitrpg.com'; - if(isProd && isHabitRPG && !isProxied(req) && nonApiUrl(req)) { - return res.redirect(baseUrl + req.url); + if(IS_PROD && isHabitRPG && !isProxied(req) && nonApiUrl(req)) { + return res.redirect('https://habitica.com' + req.url); } next(); }; @@ -192,7 +189,7 @@ var getManifestFiles = function(page){ var code = ''; - if(nconf.get('NODE_ENV') === 'production'){ + if(IS_PROD){ code += ''; code += ''; }else{ From e6c1b69cb02f36e93a2f94cdef674720b7444752 Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 4 Aug 2015 18:55:26 -0500 Subject: [PATCH 5/6] Swap out isHabitrpg for ignoreRedirect --- website/src/middleware.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/website/src/middleware.js b/website/src/middleware.js index 329ec7aa61..153fb57ace 100644 --- a/website/src/middleware.js +++ b/website/src/middleware.js @@ -123,9 +123,9 @@ function nonApiUrl(req) { } module.exports.forceHabitica = function(req, res, next) { - var isHabitRPG = req.headers.host === 'habitrpg.com'; + var ignoreRedirect = nconf.get('IGNORE_REDIRECT'); - if(IS_PROD && isHabitRPG && !isProxied(req) && nonApiUrl(req)) { + if(IS_PROD && !ignoreRedirect && !isProxied(req) && nonApiUrl(req)) { return res.redirect('https://habitica.com' + req.url); } next(); From 82d2d6507f092dff58f90326230f924bdeae9cdc Mon Sep 17 00:00:00 2001 From: Blade Barringer Date: Tue, 4 Aug 2015 18:55:55 -0500 Subject: [PATCH 6/6] Add missing space --- website/src/middleware.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/website/src/middleware.js b/website/src/middleware.js index 153fb57ace..d0e1579d6e 100644 --- a/website/src/middleware.js +++ b/website/src/middleware.js @@ -125,7 +125,7 @@ function nonApiUrl(req) { module.exports.forceHabitica = function(req, res, next) { var ignoreRedirect = nconf.get('IGNORE_REDIRECT'); - if(IS_PROD && !ignoreRedirect && !isProxied(req) && nonApiUrl(req)) { + if (IS_PROD && !ignoreRedirect && !isProxied(req) && nonApiUrl(req)) { return res.redirect('https://habitica.com' + req.url); } next();