1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-01 03:21:21 +00:00
haste-server/server.js

128 lines
3.6 KiB
JavaScript
Raw Normal View History

2011-11-18 15:17:41 +00:00
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
2011-11-18 20:44:28 +00:00
var winston = require('winston');
/////////////
// Configure loggin
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, { colorize: true, level: 'verbose' });
2011-11-18 15:17:41 +00:00
// TODO preparse static instead of using exists
2011-11-18 20:18:38 +00:00
// TODO split into files
// TODO only parse url once for static files
2011-11-18 15:17:41 +00:00
//////////////
var StaticHandler = function(path) {
this.path = path;
this.defaultPath = '/index.html';
};
StaticHandler.contentTypeFor = function(ext) {
if (ext == '.js') return 'text/javascript';
else if (ext == '.css') return 'text/css';
else if (ext == '.html') return 'text/html';
else if (ext == '.ico') return 'image/ico';
2011-11-18 20:44:28 +00:00
else {
winston.error('unable to determine content type for static asset with extension: ' + ext);
return 'text/plain';
}
2011-11-18 15:17:41 +00:00
};
StaticHandler.prototype.handle = function(request, response) {
var inc = url.parse(request.url, false);
var filePath = this.path + (inc.pathname == '/' ? this.defaultPath : inc.pathname);
path.exists(filePath, function(exists) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
2011-11-18 20:44:28 +00:00
winston.error('unable to read file', { path: filePath, error: error.message });
response.writeHead(500, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'IO: Unable to read file' }));
2011-11-18 15:17:41 +00:00
}
else {
response.writeHead(200, { 'content-type': StaticHandler.contentTypeFor(path.extname(filePath)) });
response.end(content, 'utf-8');
}
});
}
else {
2011-11-18 20:44:28 +00:00
winston.warn('file not found', { path: filePath });
response.writeHead(404, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'file not found' }));
2011-11-18 15:17:41 +00:00
}
});
};
///////////
2011-11-18 15:49:00 +00:00
var documents = {};
var DocumentHandler = function() {
};
2011-11-18 20:44:28 +00:00
DocumentHandler.prototype.handleGet = function(key, response) {
if (documents[key]) {
winston.verbose('retrieved document', { key: key });
response.writeHead(200, { 'content-type': 'application/json' });
response.end(JSON.stringify({ data: documents[key] }));
2011-11-18 15:49:00 +00:00
}
2011-11-18 20:44:28 +00:00
else {
winston.warn('document not found', { key: key });
response.writeHead(400, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'document not found' }));
2011-11-18 15:49:00 +00:00
}
};
2011-11-18 20:44:28 +00:00
DocumentHandler.prototype.handlePost = function(request, response) {
var key = '123';
request.on('data', function(data) {
if (!documents[key]) {
documents[key] = '';
}
documents[key] += data.toString();
});
request.on('end', function(end) {
winston.verbose('added document', { key: key });
response.end(JSON.stringify({ uuid: key }));
});
request.on('error', function(error) {
// TODO handle error
// TODO rename key to uuid everywhere behind the scenes
// TODO finish all TODOs
});
};
2011-11-18 15:49:00 +00:00
///////////
2011-11-18 15:17:41 +00:00
http.createServer(function(request, response) {
2011-11-18 15:49:00 +00:00
var incoming = url.parse(request.url, false);
2011-11-18 15:49:38 +00:00
var handler = null;
2011-11-18 20:44:28 +00:00
var match;
// Looking to add a new doc
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
2011-11-18 15:49:38 +00:00
handler = new DocumentHandler();
2011-11-18 20:44:28 +00:00
handler.handlePost(request, response);
2011-11-18 15:49:00 +00:00
}
2011-11-18 20:44:28 +00:00
// Looking up a doc
else if ((match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/)) && request.method == 'GET') {
handler = new DocumentHandler();
handler.handleGet(match[1], response);
}
// Otherwise, look for static file
2011-11-18 15:49:00 +00:00
else {
2011-11-18 15:49:38 +00:00
handler = new StaticHandler('./static');
2011-11-18 20:44:28 +00:00
handler.handle(request, response);
2011-11-18 15:49:00 +00:00
}
2011-11-18 15:17:41 +00:00
}).listen(7777);