|
|
@ -3,8 +3,8 @@ var url = require('url'); |
|
|
|
var fs = require('fs'); |
|
|
|
var fs = require('fs'); |
|
|
|
|
|
|
|
|
|
|
|
var winston = require('winston'); |
|
|
|
var winston = require('winston'); |
|
|
|
|
|
|
|
var connect = require('connect'); |
|
|
|
|
|
|
|
|
|
|
|
var StaticHandler = require('./lib/static_handler'); |
|
|
|
|
|
|
|
var DocumentHandler = require('./lib/document_handler'); |
|
|
|
var DocumentHandler = require('./lib/document_handler'); |
|
|
|
|
|
|
|
|
|
|
|
// Load the configuration and set some defaults
|
|
|
|
// Load the configuration and set some defaults
|
|
|
@ -52,9 +52,6 @@ for (var name in config.documents) { |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Configure a static handler for the static files
|
|
|
|
|
|
|
|
var staticHandler = new StaticHandler('./static', !!config.cacheStaticAssets); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Configure the document handler
|
|
|
|
// Configure the document handler
|
|
|
|
var documentHandler = new DocumentHandler({ |
|
|
|
var documentHandler = new DocumentHandler({ |
|
|
|
store: preferredStore, |
|
|
|
store: preferredStore, |
|
|
@ -62,21 +59,31 @@ var documentHandler = new DocumentHandler({ |
|
|
|
keyLength: config.keyLength |
|
|
|
keyLength: config.keyLength |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
// Set the server up and listen forever
|
|
|
|
// Set the server up with a static cache
|
|
|
|
http.createServer(function(request, response) { |
|
|
|
connect.createServer( |
|
|
|
var incoming = url.parse(request.url, false); |
|
|
|
connect.router(function(app) { |
|
|
|
var handler = null; |
|
|
|
// add documents
|
|
|
|
// Looking to add a new doc
|
|
|
|
app.post('/documents', function(request, response, next) { |
|
|
|
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') { |
|
|
|
return documentHandler.handlePost(request, response); |
|
|
|
return documentHandler.handlePost(request, response); |
|
|
|
}); |
|
|
|
} |
|
|
|
// get documents
|
|
|
|
// Looking up a doc
|
|
|
|
app.get('/documents/:id', function(request, response, next) { |
|
|
|
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/); |
|
|
|
return documentHandler.handleGet(req.params.id, response); |
|
|
|
if (request.method == 'GET' && match) { |
|
|
|
}); |
|
|
|
return documentHandler.handleGet(match[1], response); |
|
|
|
}), |
|
|
|
} |
|
|
|
// Otherwise, static
|
|
|
|
// Otherwise, look for static file
|
|
|
|
connect.staticCache(), |
|
|
|
staticHandler.handle(incoming.pathname, response); |
|
|
|
connect.static(__dirname + '/static', { maxAge: config.staticMaxAge }), |
|
|
|
}).listen(config.port, config.host); |
|
|
|
// Then we can loop back - and change these into '/' TODO
|
|
|
|
|
|
|
|
connect.router(function(app) { |
|
|
|
|
|
|
|
app.get('/:id', function(request, response, next) { |
|
|
|
|
|
|
|
request.url = request.originalUrl = '/'; |
|
|
|
|
|
|
|
next(); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
}), |
|
|
|
|
|
|
|
// Static
|
|
|
|
|
|
|
|
connect.staticCache(), |
|
|
|
|
|
|
|
connect.static(__dirname + '/static', { maxAge: config.staticMaxAge }) |
|
|
|
|
|
|
|
).listen(config.port, config.host); |
|
|
|
|
|
|
|
|
|
|
|
winston.info('listening on ' + config.host + ':' + config.port); |
|
|
|
winston.info('listening on ' + config.host + ':' + config.port); |
|
|
|