1
0
Fork 0
mirror of https://github.com/SunRed/haste-server.git synced 2024-11-23 17:50:19 +01:00

Separate logic into proper places

This commit is contained in:
John Crepezzi 2011-11-18 15:51:38 -05:00
parent 6bac45efe2
commit 13e497993f
3 changed files with 107 additions and 97 deletions

44
lib/document_handler.js Normal file
View file

@ -0,0 +1,44 @@
var winston = require('winston');
// For handling serving stored documents
var DocumentHandler = function() {
};
// TODO implement with FS backend
DocumentHandler.documents = {};
DocumentHandler.prototype.handleGet = function(key, response) {
if (DocumentHandler.documents[key]) {
winston.verbose('retrieved document', { key: key });
response.writeHead(200, { 'content-type': 'application/json' });
response.end(JSON.stringify({ data: DocumentHandler.documents[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 (!DocumentHandler.documents[key]) {
DocumentHandler.documents[key] = '';
}
DocumentHandler.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
});
};
module.exports = DocumentHandler;

53
lib/static_handler.js Normal file
View file

@ -0,0 +1,53 @@
var url = require('url'); // TODO not needed
var path = require('path');
var fs = require('fs');
var winston = require('winston');
// For serving static assets
var StaticHandler = function(path) {
this.path = path;
this.defaultPath = '/index.html';
};
// Determine the content type for a given extension
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';
else {
winston.error('unable to determine content type for static asset with extension: ' + ext);
return 'text/plain';
}
};
// Handle a request, and serve back the asset if it exists
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) {
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 {
var contentType = StaticHandler.contentTypeFor(path.extname(filePath));
response.writeHead(200, { 'content-type': contentType });
response.end(content, 'utf-8');
}
});
}
else {
winston.warn('file not found', { path: filePath });
response.writeHead(404, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'file not found' }));
}
});
};
module.exports = StaticHandler;

107
server.js
View file

@ -1,127 +1,40 @@
var http = require('http');
var fs = require('fs');
var path = require('path');
var url = require('url');
var winston = require('winston');
var StaticHandler = require('./lib/static_handler');
var DocumentHandler = require('./lib/document_handler');
/////////////
// Configure loggin
// Configure logging TODO
winston.remove(winston.transports.Console);
winston.add(winston.transports.Console, { colorize: true, level: 'verbose' });
// TODO preparse static instead of using exists
// TODO split into files
// TODO only parse url once for static files
//////////////
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';
else {
winston.error('unable to determine content type for static asset with extension: ' + ext);
return 'text/plain';
}
};
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) {
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)) });
response.end(content, 'utf-8');
}
});
}
else {
winston.warn('file not found', { path: filePath });
response.writeHead(404, { 'content-type': 'application/json' });
response.end(JSON.stringify({ message: 'file not found' }));
}
});
};
///////////
var documents = {};
var DocumentHandler = function() {
};
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 {
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
});
};
///////////
// Set the server up
http.createServer(function(request, response) {
var incoming = url.parse(request.url, false);
var handler = null;
var match;
// Looking to add a new doc
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
handler = new DocumentHandler();
handler.handlePost(request, response);
return handler.handlePost(request, response);
}
// Looking up a doc
else if ((match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/)) && request.method == 'GET') {
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/);
if (request.method == 'GET' && match) {
handler = new DocumentHandler();
handler.handleGet(match[1], response);
return handler.handleGet(match[1], response);
}
// Otherwise, look for static file
else {
handler = new StaticHandler('./static');
handler.handle(request, response);
}
handler = new StaticHandler('./static');
handler.handle(request, response);
}).listen(7777);