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('./routes');
require('./events');
require('./palette');


http.createServer(app).listen(3000); 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') var rasterize = require('../lib/rasterize')
, Batch = require('./lib/batch') , ratelimit = require('../lib/ratelimit')
, utils = require('./lib/utils') , Batch = require('../lib/batch')
, path = require('path') , utils = require('../lib/utils')
, join = path.join , path = require('path')
, fs = require('fs'); , join = path.join

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

, db = app.db;
/*
* GET home page. /*
*/ * GET home page.

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

});
/**
* GET stats. /**
*/ * GET stats.

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

});
/**
* GET screenshots by range. /**
*/ * GET screenshots by range.

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

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

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

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

});
/**
* GET serve when already rasterized. /**
*/ * GET serve when already rasterized.

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

});
/*
* GET screenshot. /*
*/ * GET screenshot.

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

if (!url) return res.send(400);
var id = utils.md5(url);

var id = utils.md5(url);
var options = {
path: join(dir, id + '.png') var options = {
, viewportWidth: req.query.width || app.get('default viewport width') path: join(dir, id + '.png')
, viewportHeight: req.query.height || app.get('default viewport height') , 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 console.log('screenshot - rasterizing %s %dx%d'
, options.viewportWidth , url
, options.viewportHeight); , options.viewportWidth

, options.viewportHeight);
rasterize(url, options, function(err){
if (err) return next(err); rasterize(url, options, function(err){
console.log('screenshot - rasterized %s', url); if (err) return next(err);
app.emit('screenshot', url, options.path, id); console.log('screenshot - rasterized %s', url);
res.sendfile(options.path); 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.