1
0
Fork 0
mirror of https://github.com/seejohnrun/haste-server.git synced 2024-11-25 05:51:22 +00:00
haste-server/server.js

166 lines
4.9 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-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');
var connect_rate_limit = require('connect-ratelimit');
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 18:28:52 +00:00
var config = JSON.parse(fs.readFileSync('./config.js', 'utf8'));
2012-09-27 16:01:00 +00:00
config.port = process.env.PORT || config.port || 7777;
config.host = process.env.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';
}
2012-09-27 15:46:53 +00:00
var Store, preferredStore;
2012-09-27 15:50:12 +00:00
2014-06-09 20:50:43 +00:00
if (process.env.REDISTOGO_URL && config.storage.type === 'redis') {
2012-09-27 15:46:53 +00: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 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);
2011-11-27 20:49:17 +00:00
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
var path, data;
2011-11-22 14:22:37 +00:00
for (var name in config.documents) {
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 14:22:37 +00: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 14:22:37 +00:00
// Configure the document handler
var documentHandler = new DocumentHandler({
store: preferredStore,
maxLength: config.maxLength,
keyLength: config.keyLength,
keyGenerator: keyGenerator
2011-11-22 14:22:37 +00:00
});
2016-03-06 21:20:40 +00:00
var app = connect();
// Rate limit all requests
if (config.rateLimits) {
config.rateLimits.end = true;
app.use(connect_rate_limit(config.rateLimits));
}
// first look at API calls
app.use(route(function(router) {
// get raw documents - support getting with extension
router.get('/raw/:id', function(request, response, next) {
var skipExpire = !!config.documents[request.params.id];
var key = request.params.id.split('.')[0];
return documentHandler.handleRawGet(key, response, skipExpire);
});
// add documents
router.post('/documents', function(request, response, next) {
return documentHandler.handlePost(request, response);
});
// get documents
router.get('/documents/:id', function(request, response, next) {
var skipExpire = !!config.documents[request.params.id];
return documentHandler.handleGet(
request.params.id,
response,
skipExpire
);
});
router.get('/documents/:id/remove/:secret', function(request, response, next) {
return documentHandler.handleDelete(request.params.id, request.params.secret, 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);