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

More work on the server

This commit is contained in:
John Crepezzi 2011-11-18 15:44:28 -05:00
parent b35c89a472
commit 6bac45efe2
4 changed files with 112 additions and 27 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
npm-debug.log
node_modules

50
package.json Normal file
View file

@ -0,0 +1,50 @@
{
"name": "haste",
"version": "0.0.1",
"private": true,
"description": "Private Paste",
"keywords": [ "paste", "pastebin" ],
"author": {
"name": "John Crepezzi",
"email": "john.crepezzi@gmail.com",
"url": "http://seejohncode.com/"
},
"main": "haste",
"dependencies": {
"winston": "*",
"hashlib": "*"
},
"devDependencies": {
"jasmine-node": "*"
},
"bundledDependencies": [],
"engines": {
"node": "*"
},
"bin": {
"haste-server": "./server.js"
},
"files": [ "server.js", "lib", "static" ],
"directories": {
"lib": "./lib"
},
"scripts": {
"start": "node server.js",
"test": "jasmine-node spec"
}
}

View file

@ -3,8 +3,14 @@ var fs = require('fs');
var path = require('path');
var url = require('url');
var winston = require('winston');
/////////////
// Configure loggin
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, { colorize: true, level: 'verbose' });
// TODO logging
// TODO preparse static instead of using exists
// TODO split into files
// TODO only parse url once for static files
@ -21,7 +27,10 @@ StaticHandler.contentTypeFor = function(ext) {
else if (ext == '.css') return 'text/css';
else if (ext == '.html') return 'text/html';
else if (ext == '.ico') return 'image/ico';
else console.log(ext);
else {
winston.error('unable to determine content type for static asset with extension: ' + ext);
return 'text/plain';
}
};
StaticHandler.prototype.handle = function(request, response) {
@ -31,10 +40,9 @@ StaticHandler.prototype.handle = function(request, response) {
if (exists) {
fs.readFile(filePath, function(error, content) {
if (error) {
// TODO make nice
console.log(error);
response.writeHead(500);
response.end();
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' }));
}
else {
response.writeHead(200, { 'content-type': StaticHandler.contentTypeFor(path.extname(filePath)) });
@ -43,9 +51,9 @@ StaticHandler.prototype.handle = function(request, response) {
});
}
else {
// TODO make nice
response.writeHead(404);
response.end();
winston.warn('file not found', { path: filePath });
response.writeHead(404, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'file not found' }));
}
});
};
@ -58,38 +66,62 @@ var DocumentHandler = function() {
};
DocumentHandler.prototype.handle = function(request, response) {
if (request.method == 'GET') {
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] }));
}
else if (request.method == 'POST') {
var key = '123';
request.on('data', function(data) {
if (!documents[key]) {
documents[key] = '';
}
documents[key] += data.toString();
});
request.on('end', function(end) {
response.end(JSON.stringify({ uuid: key }));
});
else {
winston.warn('document not found', { key: key });
response.writeHead(400, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'document not found' }));
}
};
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
});
};
///////////
http.createServer(function(request, response) {
var incoming = url.parse(request.url, false);
var handler = null;
if (incoming.pathname.indexOf('/documents') === 0) {
var match;
// Looking to add a new doc
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
handler = new DocumentHandler();
handler.handlePost(request, response);
}
// 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
else {
handler = new StaticHandler('./static');
handler.handle(request, response);
}
handler.handle(request, response);
}).listen(7777);

View file

@ -109,6 +109,7 @@ heist.prototype.configureShortcuts = function() {
};
// TODO handle not found gracefully
// TODO refuse to lock empty documents
// TODO support for browsers without pushstate
// TODO support for push state navigation