2019-07-17 13:08:13 +02:00
|
|
|
// vim: set ts=2 sw=2 sts=2 et:
|
2011-11-18 16:17:41 +01:00
|
|
|
var http = require('http');
|
2011-11-18 22:54:16 +01:00
|
|
|
var fs = require('fs');
|
2011-11-18 16:17:41 +01:00
|
|
|
|
2017-11-01 02:19:22 +01:00
|
|
|
var uglify = require('uglify-js');
|
2011-11-18 21:44:28 +01:00
|
|
|
var winston = require('winston');
|
2011-11-23 19:14:18 +01:00
|
|
|
var connect = require('connect');
|
2016-03-06 22:20:40 +01:00
|
|
|
var route = require('connect-route');
|
|
|
|
var connect_st = require('st');
|
|
|
|
var connect_rate_limit = require('connect-ratelimit');
|
2011-11-18 21:44:28 +01:00
|
|
|
|
2011-11-18 21:51:38 +01:00
|
|
|
var DocumentHandler = require('./lib/document_handler');
|
2011-11-18 21:44:28 +01:00
|
|
|
|
2011-11-18 22:57:23 +01:00
|
|
|
// Load the configuration and set some defaults
|
2019-07-27 17:10:50 +02:00
|
|
|
var config = require('./config.json');
|
2012-09-27 18:01:00 +02:00
|
|
|
config.port = process.env.PORT || config.port || 7777;
|
|
|
|
config.host = process.env.HOST || config.host || 'localhost';
|
2011-11-18 22:54:16 +01:00
|
|
|
|
2011-11-18 23:26:25 +01:00
|
|
|
// Set up the logger
|
|
|
|
if (config.logging) {
|
|
|
|
try {
|
|
|
|
winston.remove(winston.transports.Console);
|
2017-06-26 18:38:17 +02:00
|
|
|
} catch(e) {
|
|
|
|
/* was not present */
|
|
|
|
}
|
|
|
|
|
2011-11-18 23:26:25 +01: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 16:17:41 +01:00
|
|
|
|
2011-11-19 00:04:24 +01:00
|
|
|
// build the store from the config on-demand - so that we don't load it
|
|
|
|
// for statics
|
2011-11-22 04:03:50 +01:00
|
|
|
if (!config.storage) {
|
|
|
|
config.storage = { type: 'file' };
|
|
|
|
}
|
|
|
|
if (!config.storage.type) {
|
|
|
|
config.storage.type = 'file';
|
|
|
|
}
|
2012-09-27 17:46:53 +02:00
|
|
|
|
|
|
|
var Store, preferredStore;
|
2012-09-27 17:50:12 +02:00
|
|
|
|
2014-06-09 22:50:43 +02:00
|
|
|
if (process.env.REDISTOGO_URL && config.storage.type === 'redis') {
|
2012-09-27 17:46:53 +02:00
|
|
|
var redisClient = require('redis-url').connect(process.env.REDISTOGO_URL);
|
|
|
|
Store = require('./lib/document_stores/redis');
|
|
|
|
preferredStore = new Store(config.storage, redisClient);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Store = require('./lib/document_stores/' + config.storage.type);
|
|
|
|
preferredStore = new Store(config.storage);
|
|
|
|
}
|
2011-11-18 16:17:41 +01:00
|
|
|
|
2011-11-27 21:49:17 +01:00
|
|
|
// Compress the static javascript assets
|
|
|
|
if (config.recompressStaticAssets) {
|
|
|
|
var list = fs.readdirSync('./static');
|
2017-06-26 18:38:17 +02:00
|
|
|
for (var j = 0; j < list.length; j++) {
|
2017-07-12 03:13:33 +02:00
|
|
|
var item = list[j];
|
2017-06-26 18:38:17 +02: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 02:19:22 +01:00
|
|
|
var orig_code = fs.readFileSync('./static/' + item, 'utf8');
|
|
|
|
|
|
|
|
fs.writeFileSync('./static/' + dest, uglify.minify(orig_code).code, 'utf8');
|
2011-11-27 21:49:17 +01:00
|
|
|
winston.info('compressed ' + item + ' into ' + dest);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-22 15:22:37 +01:00
|
|
|
// Send the static documents into the preferred store, skipping expirations
|
2012-08-13 17:33:20 +02:00
|
|
|
var path, data;
|
2011-11-22 15:22:37 +01:00
|
|
|
for (var name in config.documents) {
|
2012-08-13 17:33:20 +02:00
|
|
|
path = config.documents[name];
|
|
|
|
data = fs.readFileSync(path, 'utf8');
|
|
|
|
winston.info('loading static document', { name: name, path: path });
|
|
|
|
if (data) {
|
|
|
|
preferredStore.set(name, data, function(cb) {
|
|
|
|
winston.debug('loaded static document', { success: cb });
|
|
|
|
}, true);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
winston.warn('failed to load static document', { name: name, path: path });
|
|
|
|
}
|
2011-11-22 15:22:37 +01:00
|
|
|
}
|
|
|
|
|
2012-01-07 17:35:11 +01:00
|
|
|
// Pick up a key generator
|
|
|
|
var pwOptions = config.keyGenerator || {};
|
|
|
|
pwOptions.type = pwOptions.type || 'random';
|
|
|
|
var gen = require('./lib/key_generators/' + pwOptions.type);
|
|
|
|
var keyGenerator = new gen(pwOptions);
|
|
|
|
|
2011-11-22 15:22:37 +01:00
|
|
|
// Configure the document handler
|
|
|
|
var documentHandler = new DocumentHandler({
|
|
|
|
store: preferredStore,
|
|
|
|
maxLength: config.maxLength,
|
2012-01-07 17:35:11 +01:00
|
|
|
keyLength: config.keyLength,
|
|
|
|
keyGenerator: keyGenerator
|
2011-11-22 15:22:37 +01:00
|
|
|
});
|
|
|
|
|
2016-03-06 22:20:40 +01:00
|
|
|
var app = connect();
|
|
|
|
|
|
|
|
// Rate limit all requests
|
|
|
|
if (config.rateLimits) {
|
|
|
|
config.rateLimits.end = true;
|
|
|
|
app.use(connect_rate_limit(config.rateLimits));
|
|
|
|
}
|
|
|
|
|
2019-07-17 13:08:13 +02:00
|
|
|
function raw(request, response) {
|
|
|
|
var key = request.params.id.split('.')[0];
|
|
|
|
var skipExpire = !!config.documents[key];
|
|
|
|
return documentHandler.handleRawGet(key, response, skipExpire);
|
|
|
|
}
|
|
|
|
|
2016-03-06 22:20:40 +01:00
|
|
|
// first look at API calls
|
|
|
|
app.use(route(function(router) {
|
|
|
|
// get raw documents - support getting with extension
|
2019-07-17 13:08:13 +02:00
|
|
|
router.get('/raw/:id', raw);
|
|
|
|
router.get('/:id/raw', raw);
|
2016-03-06 22:20:40 +01:00
|
|
|
// add documents
|
2017-06-26 18:38:17 +02:00
|
|
|
router.post('/documents', function(request, response) {
|
2016-03-06 22:20:40 +01:00
|
|
|
return documentHandler.handlePost(request, response);
|
|
|
|
});
|
|
|
|
// get documents
|
2017-06-26 18:38:17 +02:00
|
|
|
router.get('/documents/:id', function(request, response) {
|
2017-01-28 17:57:25 +01:00
|
|
|
var key = request.params.id.split('.')[0];
|
|
|
|
var skipExpire = !!config.documents[key];
|
|
|
|
return documentHandler.handleGet(key, response, skipExpire);
|
2016-03-06 22:20:40 +01: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) {
|
2019-07-17 13:08:13 +02:00
|
|
|
router.get('/:id', function(request, _response, next) {
|
2016-03-06 22:20:40 +01:00
|
|
|
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 22:57:23 +01:00
|
|
|
|
2011-11-18 22:58:21 +01:00
|
|
|
winston.info('listening on ' + config.host + ':' + config.port);
|