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

83 lines
2.5 KiB
JavaScript
Raw Normal View History

2011-11-18 15:17:41 +00:00
var http = require('http');
var url = require('url');
var fs = require('fs');
2011-11-18 15:17:41 +00:00
2011-11-18 20:44:28 +00:00
var winston = require('winston');
2011-11-18 20:51:38 +00:00
var StaticHandler = require('./lib/static_handler');
var DocumentHandler = require('./lib/document_handler');
2011-11-18 20:44:28 +00:00
2011-11-18 21:57:23 +00:00
// Load the configuration and set some defaults
var config = JSON.parse(fs.readFileSync('config.js', 'utf8'));
2011-11-18 21:57:23 +00:00
config.port = config.port || 7777;
config.host = config.host || 'localhost';
2011-11-18 22:26:25 +00:00
// Set up the logger
if (config.logging) {
try {
winston.remove(winston.transports.Console);
} catch(er) { }
var detail, type;
for (var i = 0; i < config.logging.length; i++) {
detail = config.logging[i];
type = detail.type;
delete detail.type;
winston.add(winston.transports[type], detail);
}
}
2011-11-18 15:17:41 +00:00
2011-11-18 23:04:24 +00:00
// build the store from the config on-demand - so that we don't load it
// for statics
2011-11-22 03:03:50 +00:00
if (!config.storage) {
config.storage = { type: 'file' };
}
if (!config.storage.type) {
config.storage.type = 'file';
}
var Store = require('./lib/' + config.storage.type + '_document_store');
var preferredStore = new Store(config.storage);
2011-11-18 15:17:41 +00:00
2011-11-22 14:22:37 +00:00
// Send the static documents into the preferred store, skipping expirations
for (var name in config.documents) {
var path = config.documents[name];
fs.readFile(path, 'utf8', function(err, data) {
if (data && !err) {
preferredStore.set(name, data, function(cb) {
winston.info('loaded static document', { name: name, path: path });
}, true);
}
else {
winston.warn('failed to load static document', { name: name, path: path });
}
});
}
2011-11-22 04:00:28 +00:00
// Configure a static handler for the static files
var staticHandler = new StaticHandler('./static', !!config.cacheStaticAssets);
2011-11-22 14:22:37 +00:00
// Configure the document handler
var documentHandler = new DocumentHandler({
store: preferredStore,
maxLength: config.maxLength,
keyLength: config.keyLength
});
2011-11-18 23:04:24 +00:00
// Set the server up and listen forever
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
// Looking to add a new doc
if (incoming.pathname.match(/^\/documents$/) && request.method == 'POST') {
2011-11-22 14:22:37 +00:00
return documentHandler.handlePost(request, response);
2011-11-18 15:49:00 +00:00
}
2011-11-18 20:44:28 +00:00
// Looking up a doc
2011-11-18 20:51:38 +00:00
var match = incoming.pathname.match(/^\/documents\/([A-Za-z0-9]+)$/);
if (request.method == 'GET' && match) {
2011-11-22 14:22:37 +00:00
return documentHandler.handleGet(match[1], response);
2011-11-18 20:44:28 +00:00
}
// Otherwise, look for static file
2011-11-22 04:00:28 +00:00
staticHandler.handle(incoming.pathname, response);
2011-11-18 21:57:23 +00:00
}).listen(config.port, config.host);
2011-11-18 21:58:21 +00:00
winston.info('listening on ' + config.host + ':' + config.port);