2011-11-18 15:17:41 +00:00
|
|
|
var http = require('http');
|
|
|
|
var url = require('url');
|
2011-11-18 21:54:16 +00:00
|
|
|
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-23 18:14:18 +00:00
|
|
|
var connect = require('connect');
|
2011-11-18 20:44:28 +00:00
|
|
|
|
2011-11-18 20:51:38 +00:00
|
|
|
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
|
2011-11-18 21:54:16 +00:00
|
|
|
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 21:54:16 +00:00
|
|
|
|
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-27 20:49:17 +00:00
|
|
|
// Compress the static javascript assets
|
|
|
|
if (config.recompressStaticAssets) {
|
|
|
|
var jsp = require("uglify-js").parser;
|
|
|
|
var pro = require("uglify-js").uglify;
|
|
|
|
var list = fs.readdirSync('./static');
|
|
|
|
for (var i = 0; i < list.length; i++) {
|
|
|
|
var item = list[i];
|
|
|
|
var orig_code, ast;
|
|
|
|
if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) {
|
|
|
|
dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3);
|
|
|
|
orig_code = fs.readFileSync('./static/' + item, 'utf8');
|
|
|
|
ast = jsp.parse(orig_code);
|
|
|
|
ast = pro.ast_mangle(ast);
|
|
|
|
ast = pro.ast_squeeze(ast);
|
|
|
|
fs.writeFileSync('./static/' + dest, pro.gen_code(ast), 'utf8');
|
|
|
|
winston.info('compressed ' + item + ' into ' + dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure the document handler
|
|
|
|
var documentHandler = new DocumentHandler({
|
|
|
|
store: preferredStore,
|
|
|
|
maxLength: config.maxLength,
|
|
|
|
keyLength: config.keyLength
|
|
|
|
});
|
|
|
|
|
2011-11-23 18:14:18 +00:00
|
|
|
// Set the server up with a static cache
|
|
|
|
connect.createServer(
|
2011-11-23 21:34:22 +00:00
|
|
|
// First look for api calls
|
2011-11-23 18:14:18 +00:00
|
|
|
connect.router(function(app) {
|
|
|
|
// add documents
|
|
|
|
app.post('/documents', function(request, response, next) {
|
|
|
|
return documentHandler.handlePost(request, response);
|
|
|
|
});
|
|
|
|
// get documents
|
|
|
|
app.get('/documents/:id', function(request, response, next) {
|
2011-11-23 20:45:28 +00:00
|
|
|
return documentHandler.handleGet(request.params.id, response);
|
2011-11-23 18:14:18 +00:00
|
|
|
});
|
|
|
|
}),
|
|
|
|
// Otherwise, static
|
2011-11-27 19:47:36 +00:00
|
|
|
connect.staticCache(),
|
2011-11-23 18:14:18 +00:00
|
|
|
connect.static(__dirname + '/static', { maxAge: config.staticMaxAge }),
|
2011-11-23 21:34:22 +00:00
|
|
|
// Then we can loop back - and everything else should be a token,
|
|
|
|
// so route it back to /index.html
|
2011-11-23 18:14:18 +00:00
|
|
|
connect.router(function(app) {
|
|
|
|
app.get('/:id', function(request, response, next) {
|
2011-11-23 21:34:22 +00:00
|
|
|
request.url = request.originalUrl = '/index.html';
|
2011-11-23 18:14:18 +00:00
|
|
|
next();
|
|
|
|
});
|
|
|
|
}),
|
|
|
|
connect.static(__dirname + '/static', { maxAge: config.staticMaxAge })
|
|
|
|
).listen(config.port, config.host);
|
2011-11-18 21:57:23 +00:00
|
|
|
|
2011-11-18 21:58:21 +00:00
|
|
|
winston.info('listening on ' + config.host + ':' + config.port);
|