2011-11-18 16:17:41 +01:00
|
|
|
var http = require('http');
|
|
|
|
var url = require('url');
|
|
|
|
|
2011-11-18 21:44:28 +01:00
|
|
|
var winston = require('winston');
|
|
|
|
|
2011-11-18 21:51:38 +01:00
|
|
|
var StaticHandler = require('./lib/static_handler');
|
|
|
|
var DocumentHandler = require('./lib/document_handler');
|
2011-11-18 21:44:28 +01:00
|
|
|
|
|
|
|
/////////////
|
2011-11-18 21:51:38 +01:00
|
|
|
// Configure logging TODO
|
2011-11-18 21:44:28 +01:00
|
|
|
winston.remove(winston.transports.Console);
|
|
|
|
winston.add(winston.transports.Console, { colorize: true, level: 'verbose' });
|
2011-11-18 16:17:41 +01:00
|
|
|
|
|
|
|
// TODO preparse static instead of using exists
|
2011-11-18 21:18:38 +01:00
|
|
|
// TODO only parse url once for static files
|
2011-11-18 16:17:41 +01:00
|
|
|
|
2011-11-18 21:51:38 +01:00
|
|
|
// Set the server up
|
2011-11-18 16:17:41 +01:00
|
|
|
http.createServer(function(request, response) {
|
|
|
|
|
2011-11-18 16:49:00 +01:00
|
|
|
var incoming = url.parse(request.url, false);
|
2011-11-18 16:49:38 +01:00
|
|
|
var handler = null;
|
2011-11-18 21:44:28 +01:00
|
|
|
|
|
|
|
// Looking to add a new doc
|
|
|
|
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
|
2011-11-18 16:49:38 +01:00
|
|
|
handler = new DocumentHandler();
|
2011-11-18 21:51:38 +01:00
|
|
|
return handler.handlePost(request, response);
|
2011-11-18 16:49:00 +01:00
|
|
|
}
|
2011-11-18 21:44:28 +01:00
|
|
|
|
|
|
|
// Looking up a doc
|
2011-11-18 21:51:38 +01:00
|
|
|
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/);
|
|
|
|
if (request.method == 'GET' && match) {
|
2011-11-18 21:44:28 +01:00
|
|
|
handler = new DocumentHandler();
|
2011-11-18 21:51:38 +01:00
|
|
|
return handler.handleGet(match[1], response);
|
2011-11-18 21:44:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, look for static file
|
2011-11-18 21:51:38 +01:00
|
|
|
handler = new StaticHandler('./static');
|
2011-11-18 22:00:05 +01:00
|
|
|
handler.handle(incoming.pathname, response);
|
2011-11-18 16:17:41 +01:00
|
|
|
|
|
|
|
}).listen(7777);
|