Last active
August 29, 2015 14:11
-
-
Save twalker/2e469c21db564f7c14e8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Consolidates routes into a single router for using. | |
* | |
* Nearly all routes re-use the single page application (spa) view; | |
* Specific routes are to use different middleware per url (i.e. restrictTo, title, bootstrap). | |
* | |
*/ | |
var express = require('express'); | |
var router = express.Router(); | |
var middle = require('../middleware'); | |
var request = require('request'); | |
var routes = { | |
// primary route for single page application, | |
// where the client manages it's own routing. | |
spa: require('./spa'), | |
// client-tests. | |
clientTests: require('./client-tests') | |
}; | |
// repeated spa routes, to enforce user roles per route | |
router.get('/', | |
middle.auth, middle.bootstrap, routes.spa({ title: 'Dashboard' })) | |
router.get('/campaigns/subscriber-list', | |
middle.auth, middle.bootstrap, middle.restrictTo('subscribers'), routes.spa()) | |
router.get('/reporting/*', | |
middle.auth, middle.bootstrap, middle.restrictTo('reporting'), routes.spa()) | |
router.get(/campaigns|sms\/subscriber-list\/segmentation/, | |
middle.auth, middle.bootstrap, middle.restrictTo('email'), routes.spa()) | |
router.get('/campaigns/testing/litmus/*', | |
middle.auth, middle.bootstrap, middle.restrictTo('email'), routes.spa()) | |
router.get('/sms*', | |
middle.auth, middle.bootstrap, middle.restrictTo('sms'), routes.spa()) | |
router.get('/emails*', | |
middle.auth, middle.bootstrap, middle.restrictTo('email'), routes.spa()) | |
router.get('/subscribers/*', | |
middle.auth, middle.bootstrap, middle.restrictTo('subscribers'), routes.spa()) | |
router.get('/account/*', | |
middle.auth, middle.bootstrap, middle.restrictTo('account'), routes.spa()) | |
router.get('/invite/:token', function (req, res) { | |
res.render('independent-spa', { title: 'Trial Invite', name: 'trial-invite' }); | |
}); | |
router.get('/trial', | |
middle.auth, middle.bootstrap, routes.spa({title: 'Your Trial Account'})); | |
// render screenshot image or pdf for url | |
router.get('/screenshot', function(req, res, next){ | |
if('app.wordfly.com' === req.hostname) { | |
var redirectUrl = 'https://web2.app.wordfly.com' + req.url.replace(req.hostname, 'web2.app.wordfly.com'); | |
console.log('forwarding request to', redirectUrl); | |
console.log(req.headers.cookie) | |
request.get({ | |
headers: {'cookie': req.headers.cookie}, | |
url: redirectUrl, | |
followRedirect: true | |
}, function(error, response, body){ | |
console.log('response') | |
req.pipe(response); | |
}); | |
} else { | |
require('./screenshot')(req, res, next); | |
} | |
}); | |
router.get('/styleguide', middle.auth, middle.bootstrap, function (req, res) { | |
res.render('styleguide', { title: 'Styleguide' }); | |
}); | |
// routes for browser based user and interactive tests | |
router.get('/client-tests/:type?/:name?', routes.clientTests); | |
// export single usable router | |
module.exports = router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* screenshot sends an image or pdf of the provided url | |
* to the response. | |
* | |
* Files will be saved to the configured `mediaServerRoot` path. | |
* Files will be re-used for subsequent requests | |
* Unless the files are older than MaxAge or file is older than the timestamp in the querystring. | |
* | |
*/ | |
var childProcess = require('child_process'), | |
path = require('path'), | |
fs = require('fs'), | |
config = require('./../config'); | |
var maxAge = new Date('1/1/2000').getTime() | |
module.exports = function(req, res, next){ | |
console.log('---------SCREENSHOT------------'); | |
var url = req.query.url, | |
filename = url.replace(/\W/g, '_') + '.' + ('format' in req.query ? req.query.format : 'png'), | |
filepath = path.join(config.get('mediaServerRoot'), 'screenshots', filename), | |
cookie = req.get('cookie'), | |
childArgs = [ | |
'--ignore-ssl-errors=true', | |
'--web-security=false', | |
path.join(__dirname, '../', 'pjs', 'render-page.js'), | |
url, | |
filepath | |
], | |
// last updated timestamp to ensure file freshness | |
timestamp = 'timestamp' in req.query ? parseInt(req.query.timestamp) : maxAge; | |
// pass cookie arg if this is a WordFly url | |
if(cookie && (/wordfly/i).test(url)) childArgs.push(cookie) | |
console.log('url: ' + url); | |
console.log('filename: ' + filename); | |
console.log('filepath: ' + filepath); | |
console.log('cookie: ', cookie); | |
fs.stat(filepath, function(err, stats){ | |
// file doesn't exist error? | |
if(err) console.log('err', err) | |
var noImg = err && /ENOENT/.test(err.code); | |
// check for unknown io error; | |
console.log('noImg', noImg) | |
if(err && !noImg) return next(err); | |
console.log('set Content-Location: ', filepath) | |
// pass the physical location for debugging | |
res.set('Content-Location', filepath); | |
// only create new files that don't exist and are no older than the timestamp param. | |
console.log('create a new file?', noImg || stats.mtime < timestamp); | |
//console.log('file mtime', stats.mtime); | |
console.log('timestamp', timestamp); | |
if(noImg || stats.mtime < timestamp) { | |
childProcess.execFile('phantomjs', childArgs, function(spawnErr, stdout, stderr){ | |
console.log('trying to create a fresh screenshot image') | |
if(spawnErr) console.log('spawnErr', spawnErr) | |
if(stderr) console.log('stderr', stderr) | |
if(spawnErr) return next(spawnErr); | |
if(stderr) return next(stderr); | |
console.log('res.sendFile', filepath) | |
res.sendFile(filepath); | |
}); | |
} else { | |
console.log('sending existing screenshot image'); | |
res.sendFile(filepath); | |
} | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment