Skip to content

Commit

Permalink
fix(routing): add redirection routes with tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienjoly committed Mar 3, 2019
1 parent d392fff commit 71a6b58
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 67 deletions.
44 changes: 44 additions & 0 deletions app/controllers/redir.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* redir controller
* redirects to external resources, while tracking the page view
*/

var config = require('../models/config.js');

var GITHUB_URL = 'https://github.com/openwhyd/openwhyd';
var FAQ_PAGE = GITHUB_URL + '/blob/master/docs/FAQ.md';
var SUPPORT_PAGE =
GITHUB_URL + '/blob/master/docs/FAQ.md#how-to-contact-openwhyds-team';
var DONATE_PAGE = 'https://opencollective.com/openwhyd';
var PRIVACY_PAGE = config.urlPrefix + '/privacy';

var REDIRECTIONS = {
'/about': [
'https://medium.com/@adrienjoly/music-amongst-other-topics-a4f41657d6d',
"Openwhyd's story"
],
'/api': [GITHUB_URL + '/blob/master/docs/API.md', 'API Documentation'],
'/community': [GITHUB_URL, 'Community'],
'/contact': [SUPPORT_PAGE, 'Contact'],
'/contribute': [
FAQ_PAGE + '#id-love-to-contribute-to-openwhyd-how-can-i-help',
'Contribute'
],
'/donate': [DONATE_PAGE, 'Donate'],
'/faq': [FAQ_PAGE, 'Frequently Asked Questions'],
'/help': [SUPPORT_PAGE, 'Help'],
'/sponsor': [DONATE_PAGE, 'Sponsor'],
'/support': [SUPPORT_PAGE, 'Support'],
'/team': [SUPPORT_PAGE, 'Team'],
'/tos': [PRIVACY_PAGE, 'Terms of Service']
};

exports.controller = function(request, reqParams, response) {
var path = request.url.split('?')[0];
var [redirUrl, redirTitle] = REDIRECTIONS[path] || [];
if (redirUrl) {
response.redirectWithTracking(redirUrl, redirTitle);
} else {
response.notFound();
}
};
26 changes: 1 addition & 25 deletions app/controllers/static.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,13 @@
* redirects to static files
* @author adrienjoly, whyd
*/
/**
* subdir controller
* maps to another controller, based on the path
* @author adrienjoly, whyd
*/

var config = require('../models/config.js');
var runsLocally = config.urlPrefix.indexOf('localhost') != -1;

var GITHUB_URL = 'https://github.com/openwhyd/openwhyd';
var SUPPORT_PAGE =
GITHUB_URL + '/blob/master/docs/FAQ.md#how-to-contact-openwhyds-team';
var DONATE_PAGE = 'https://opencollective.com/openwhyd';
var PRIVACY_PAGE = config.urlPrefix + '/privacy';

var STATIC_FILES = {
'/favicon.ico': '/images/favicon' + (runsLocally ? '_orange' : '') + '.ico',
'/favicon.png': '/images/favicon' + (runsLocally ? '_orange' : '') + '.png',
'/community': GITHUB_URL,
'/api': GITHUB_URL + '/blob/master/docs/API.md',
'/faq': GITHUB_URL + '/blob/master/docs/FAQ.md',
'/contribute':
GITHUB_URL +
'/blob/master/docs/FAQ.md#id-love-to-contribute-to-openwhyd-how-can-i-help',
'/help': SUPPORT_PAGE,
'/team': SUPPORT_PAGE,
'/support': SUPPORT_PAGE,
'/contact': SUPPORT_PAGE,
'/sponsor': DONATE_PAGE,
'/donate': DONATE_PAGE,
'/tos': PRIVACY_PAGE
'/favicon.png': '/images/favicon' + (runsLocally ? '_orange' : '') + '.png'
};

exports.controller = function(request, reqParams, response) {
Expand Down
6 changes: 6 additions & 0 deletions app/models/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ http.ServerResponse.prototype.redirect = function(url) {
return this.renderHTML(loggingTemplate.htmlRedirect(url));
};

http.ServerResponse.prototype.redirectWithTracking = function(url, title) {
return this.renderHTML(
loggingTemplate.renderRedirectPageWithTracking(url, title)
);
};

http.ServerResponse.prototype.status = function(code, head, body) {
this.writeHead(code, head || undefined);
body ? this.end(body) : this.end();
Expand Down
33 changes: 32 additions & 1 deletion app/templates/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ exports.htmlRedirect = function(url) {
'<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">',
'<html>',
'<head>',
'<title>whyd is redirecting...</title>',
'<title>Openwhyd is redirecting...</title>',
'<meta http-equiv="REFRESH" content="3;url=' + url + '"></HEAD>',
'<BODY>',
'You are being redirected to: <a href="' + url + '">' + url + '</a>...',
Expand All @@ -117,3 +117,34 @@ exports.htmlRedirect = function(url) {
'</HTML>'
].join('\n');
};

exports.renderRedirectPageWithTracking = function(url, title) {
return `<!DOCTYPE html>
<html>
<head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# whydapp: http://ogp.me/ns/fb/whydapp#">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta property="og:image" content="${mainTemplate.defaultPageMeta.img}">
<meta property="og:description" content="${
mainTemplate.defaultPageMeta.desc
}">
<meta property="fb:app_id" content="169250156435902">
<meta property="fb:admins" content="510739408">
<meta property="og:type" content="website">
<meta property="og:url" content="${config.urlPrefix}">
<meta http-equiv="REFRESH" content="3;url=${url}">
<title>${title || 'Openwhyd'} - redirecting...</title>
<link href="/favicon.ico" rel="shortcut icon" type="image/x-icon">
<link href="/favicon.png" rel="icon" type="image/png">
<link rel="image_src" href="${mainTemplate.defaultPageMeta.img}">
<script src="/js/whydtr.js"></script>
</head>
<body class="pgRedirect">
<p>Redirecting to <a href="${url}">${title || url}</a>...</p>
<script>
setTimeout(function(){ window.location.href = "${url}"; }, 2000);
</script>
</body>
</html>
`;
};
23 changes: 12 additions & 11 deletions config/app.route
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ GET /now -> controllers.now
GET /favicon.ico -> controllers.static
GET /favicon.png -> controllers.static
GET /download -> controllers.static
GET /about -> controllers.static
GET /press -> controllers.static
GET /privacy -> controllers.static
GET /investors -> controllers.static
Expand All @@ -16,16 +15,18 @@ GET /mobile -> controllers.static
GET /iphone -> controllers.static
POST /fbcanvas/ -> controllers.static

GET /faq -> controllers.static
GET /api -> controllers.static
GET /help -> controllers.static
GET /team -> controllers.static
GET /contact -> controllers.static
GET /support -> controllers.static
GET /sponsor -> controllers.static
GET /donate -> controllers.static
GET /community -> controllers.static
GET /contribute -> controllers.static
GET /about -> controllers.redir
GET /api -> controllers.redir
GET /community -> controllers.redir
GET /contact -> controllers.redir
GET /contribute -> controllers.redir
GET /donate -> controllers.redir
GET /faq -> controllers.redir
GET /help -> controllers.redir
GET /sponsor -> controllers.redir
GET /support -> controllers.redir
GET /team -> controllers.redir
GET /tos -> controllers.redir

GET /paris -> controllers.api.city
GET /paris/ -> controllers.api.city
Expand Down
30 changes: 0 additions & 30 deletions public/about/index.html

This file was deleted.

0 comments on commit 71a6b58

Please sign in to comment.