mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-22 04:31:21 +00:00
More work on the server
This commit is contained in:
parent
b35c89a472
commit
6bac45efe2
4 changed files with 112 additions and 27 deletions
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
npm-debug.log
|
||||
node_modules
|
50
package.json
Normal file
50
package.json
Normal 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"
|
||||
}
|
||||
|
||||
}
|
68
server.js
68
server.js
|
@ -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,11 +66,20 @@ 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') {
|
||||
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]) {
|
||||
|
@ -71,9 +88,14 @@ DocumentHandler.prototype.handle = function(request, response) {
|
|||
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
|
||||
});
|
||||
};
|
||||
|
||||
///////////
|
||||
|
@ -81,15 +103,25 @@ DocumentHandler.prototype.handle = function(request, response) {
|
|||
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);
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue