From cfdcadb2bcf57f2f6aad306ad40b4b4cee1528a4 Mon Sep 17 00:00:00 2001 From: Kevin Gisi Date: Thu, 30 Jul 2015 13:20:24 -0400 Subject: [PATCH] Added redirects to habitica for non-API urls --- website/src/middleware.js | 15 +++++++++++++++ website/src/server.js | 1 + 2 files changed, 16 insertions(+) diff --git a/website/src/middleware.js b/website/src/middleware.js index 8de23aab6c..a65b3991ab 100644 --- a/website/src/middleware.js +++ b/website/src/middleware.js @@ -115,6 +115,21 @@ module.exports.forceSSL = function(req, res, next){ 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. + +function nonApiUrl(req) { + return req.url.search(/\/api\//) === -1; +} + +module.exports.forceHabitica = function(req, res, next) { + if(nconf.get('NODE_ENV') === 'production' && req.hostname !== 'habitica.com' && nonApiUrl(req)) { + return res.redirect('https://habitica.com' + req.url); + } + next(); +}; + module.exports.cors = function(req, res, next) { res.header("Access-Control-Allow-Origin", req.headers.origin || "*"); res.header("Access-Control-Allow-Methods", "OPTIONS,GET,POST,PUT,HEAD,DELETE"); diff --git a/website/src/server.js b/website/src/server.js index 1447823a4d..b108d13f2f 100644 --- a/website/src/server.js +++ b/website/src/server.js @@ -97,6 +97,7 @@ if (cores!==0 && cluster.isMaster && (isDev || isProd)) { app.set("view engine", "jade"); app.use(express.favicon(publicDir + '/favicon.ico')); app.use(middleware.cors); + app.use(middleware.forceHabitica); app.use(middleware.forceSSL); app.use(express.urlencoded()); app.use(express.json());