Skip to content

Commit

Permalink
moved route files to ./routes
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Dec 19, 2011
1 parent 315fe1a commit f13138f
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 106 deletions.
2 changes: 0 additions & 2 deletions app.js
Expand Up @@ -32,8 +32,6 @@ app.configure('development', function(){
});

require('./routes');
require('./events');
require('./palette');

http.createServer(app).listen(3000);

Expand Down
File renamed without changes.
4 changes: 4 additions & 0 deletions routes/index.js
@@ -0,0 +1,4 @@

require('./main');
require('./data');
require('./palette');
208 changes: 104 additions & 104 deletions routes.js → routes/main.js
@@ -1,104 +1,104 @@

var rasterize = require('./lib/rasterize')
, ratelimit = require('./lib/ratelimit')
, Batch = require('./lib/batch')
, utils = require('./lib/utils')
, path = require('path')
, join = path.join
, fs = require('fs');

var dir = app.get('screenshots')
, db = app.db;

/*
* GET home page.
*/

app.get('/', function(req, res, next){
res.render('index');
});

/**
* GET stats.
*/

app.get('/stats', function(req, res){
db.hgetall('screenshot:stats', function(err, obj){
if (err) return next(err);
res.send(obj);
});
});

/**
* GET screenshots by range.
*/

app.get('/screenshots/:from..:to', function(req, res, next){
var from = req.params.from
, to = req.params.to
, batch = new Batch;

db.zrange('screenshot:ids', from, to, function(err, ids){
if (err) return next(err);

// fetch
ids.forEach(function(id){
batch.push(function(fn){
db.hgetall('screenshot:' + id, fn);
});
});

// finished
batch.end(function(err, objs){
if (err) return next(err);
res.send(objs);
})
});
});

/**
* GET serve when already rasterized.
*/

app.get('/:url(*)', function(req, res, next){
var url = utils.url(req.params.url);
db.hget('screenshot:url:id', url, function(err, id){
if (err) return next(err);
if (!id) return next();
db.hget('screenshot:' + id, 'path', function(err, path){
if (err) return next(err);
console.log('screenshot - serving rasterized %s', url);
res.sendfile(path);
});
});
});

/*
* GET screenshot.
*/

app.get('/:url(*)', ratelimit(60, 10), function(req, res, next){
var url = utils.url(req.params.url);
if (!url) return res.send(400);

var id = utils.md5(url);

var options = {
path: join(dir, id + '.png')
, viewportWidth: req.query.width || app.get('default viewport width')
, viewportHeight: req.query.height || app.get('default viewport height')
};

console.log('screenshot - rasterizing %s %dx%d'
, url
, options.viewportWidth
, options.viewportHeight);

rasterize(url, options, function(err){
if (err) return next(err);
console.log('screenshot - rasterized %s', url);
app.emit('screenshot', url, options.path, id);
res.sendfile(options.path);
});
});


var rasterize = require('../lib/rasterize')
, ratelimit = require('../lib/ratelimit')
, Batch = require('../lib/batch')
, utils = require('../lib/utils')
, path = require('path')
, join = path.join
, fs = require('fs');

var dir = app.get('screenshots')
, db = app.db;

/*
* GET home page.
*/

app.get('/', function(req, res, next){
res.render('index');
});

/**
* GET stats.
*/

app.get('/stats', function(req, res){
db.hgetall('screenshot:stats', function(err, obj){
if (err) return next(err);
res.send(obj);
});
});

/**
* GET screenshots by range.
*/

app.get('/screenshots/:from..:to', function(req, res, next){
var from = req.params.from
, to = req.params.to
, batch = new Batch;

db.zrange('screenshot:ids', from, to, function(err, ids){
if (err) return next(err);

// fetch
ids.forEach(function(id){
batch.push(function(fn){
db.hgetall('screenshot:' + id, fn);
});
});

// finished
batch.end(function(err, objs){
if (err) return next(err);
res.send(objs);
})
});
});

/**
* GET serve when already rasterized.
*/

app.get('/:url(*)', function(req, res, next){
var url = utils.url(req.params.url);
db.hget('screenshot:url:id', url, function(err, id){
if (err) return next(err);
if (!id) return next();
db.hget('screenshot:' + id, 'path', function(err, path){
if (err) return next(err);
console.log('screenshot - serving rasterized %s', url);
res.sendfile(path);
});
});
});

/*
* GET screenshot.
*/

app.get('/:url(*)', ratelimit(60, 10), function(req, res, next){
var url = utils.url(req.params.url);
if (!url) return res.send(400);

var id = utils.md5(url);

var options = {
path: join(dir, id + '.png')
, viewportWidth: req.query.width || app.get('default viewport width')
, viewportHeight: req.query.height || app.get('default viewport height')
};

console.log('screenshot - rasterizing %s %dx%d'
, url
, options.viewportWidth
, options.viewportHeight);

rasterize(url, options, function(err){
if (err) return next(err);
console.log('screenshot - rasterized %s', url);
app.emit('screenshot', url, options.path, id);
res.sendfile(options.path);
});
});
File renamed without changes.

0 comments on commit f13138f

Please sign in to comment.