2011-11-18 15:17:41 +00:00
|
|
|
var http = require('http');
|
2011-11-18 21:54:16 +00:00
|
|
|
var fs = require('fs');
|
2011-11-18 15:17:41 +00:00
|
|
|
|
2017-11-01 01:19:22 +00:00
|
|
|
var uglify = require('uglify-js');
|
2011-11-18 20:44:28 +00:00
|
|
|
var winston = require('winston');
|
2011-11-23 18:14:18 +00:00
|
|
|
var connect = require('connect');
|
2016-03-06 21:20:40 +00:00
|
|
|
var route = require('connect-route');
|
|
|
|
var connect_st = require('st');
|
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
|
2012-09-19 19:28:52 +01:00
|
|
|
var config = JSON.parse(fs.readFileSync('./config.js', 'utf8'));
|
2012-09-27 17:01:00 +01:00
|
|
|
config.port = process.env.PORT || config.port || 7777;
|
|
|
|
config.host = process.env.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);
|
2017-06-26 17:38:17 +01:00
|
|
|
} catch(e) {
|
|
|
|
/* was not present */
|
|
|
|
}
|
|
|
|
|
2011-11-18 22:26:25 +00:00
|
|
|
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
|
|
|
|
2020-06-08 20:56:17 +01:00
|
|
|
var Store = require('./lib/document_store');
|
|
|
|
store = new Store();
|
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 list = fs.readdirSync('./static');
|
2017-06-26 17:38:17 +01:00
|
|
|
for (var j = 0; j < list.length; j++) {
|
2017-07-12 02:13:33 +01:00
|
|
|
var item = list[j];
|
2017-06-26 17:38:17 +01:00
|
|
|
if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) {
|
|
|
|
var dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3);
|
2017-11-01 01:19:22 +00:00
|
|
|
var orig_code = fs.readFileSync('./static/' + item, 'utf8');
|
|
|
|
|
|
|
|
fs.writeFileSync('./static/' + dest, uglify.minify(orig_code).code, 'utf8');
|
2011-11-27 20:49:17 +00:00
|
|
|
winston.info('compressed ' + item + ' into ' + dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-07 16:35:11 +00:00
|
|
|
// Pick up a key generator
|
2020-06-08 20:56:17 +01:00
|
|
|
var KeyGenerator = require('./lib/key_generator');
|
|
|
|
var keyGenerator = new KeyGenerator();
|
2012-01-07 16:35:11 +00:00
|
|
|
|
2011-11-22 14:22:37 +00:00
|
|
|
// Configure the document handler
|
|
|
|
var documentHandler = new DocumentHandler({
|
2020-06-08 20:56:17 +01:00
|
|
|
store: store,
|
2011-11-22 14:22:37 +00:00
|
|
|
maxLength: config.maxLength,
|
2012-01-07 16:35:11 +00:00
|
|
|
keyLength: config.keyLength,
|
|
|
|
keyGenerator: keyGenerator
|
2011-11-22 14:22:37 +00:00
|
|
|
});
|
|
|
|
|
2016-03-06 21:20:40 +00:00
|
|
|
var app = connect();
|
|
|
|
|
|
|
|
// first look at API calls
|
|
|
|
app.use(route(function(router) {
|
|
|
|
// get raw documents - support getting with extension
|
2017-06-26 17:38:17 +01:00
|
|
|
router.get('/raw/:id', function(request, response) {
|
2016-03-06 21:20:40 +00:00
|
|
|
var key = request.params.id.split('.')[0];
|
2020-06-08 20:56:17 +01:00
|
|
|
return documentHandler.handleRawGet(key, response);
|
2016-03-06 21:20:40 +00:00
|
|
|
});
|
|
|
|
// add documents
|
2017-06-26 17:38:17 +01:00
|
|
|
router.post('/documents', function(request, response) {
|
2016-03-06 21:20:40 +00:00
|
|
|
return documentHandler.handlePost(request, response);
|
|
|
|
});
|
|
|
|
// get documents
|
2017-06-26 17:38:17 +01:00
|
|
|
router.get('/documents/:id', function(request, response) {
|
2017-01-28 16:57:25 +00:00
|
|
|
var key = request.params.id.split('.')[0];
|
2020-06-08 20:56:17 +01:00
|
|
|
return documentHandler.handleGet(key, response);
|
2016-03-06 21:20:40 +00:00
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Otherwise, try to match static files
|
|
|
|
app.use(connect_st({
|
|
|
|
path: __dirname + '/static',
|
|
|
|
content: { maxAge: config.staticMaxAge },
|
|
|
|
passthrough: true,
|
|
|
|
index: false
|
|
|
|
}));
|
|
|
|
|
|
|
|
// Then we can loop back - and everything else should be a token,
|
|
|
|
// so route it back to /
|
|
|
|
app.use(route(function(router) {
|
|
|
|
router.get('/:id', function(request, response, next) {
|
|
|
|
request.sturl = '/';
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
// And match index
|
|
|
|
app.use(connect_st({
|
|
|
|
path: __dirname + '/static',
|
|
|
|
content: { maxAge: config.staticMaxAge },
|
|
|
|
index: 'index.html'
|
|
|
|
}));
|
|
|
|
|
|
|
|
http.createServer(app).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);
|