Files
hastebin-ansi/server.js

169 lines
5.4 KiB
JavaScript
Raw Normal View History

2023-01-20 14:10:02 +01:00
const http = require('http');
const fs = require('fs');
2011-11-18 10:17:41 -05:00
2023-01-20 14:10:02 +01:00
const uglify = require('uglify-js');
const winston = require('winston');
const connect = require('connect');
const route = require('connect-route');
const connect_st = require('st');
const connect_rate_limit = require('connect-ratelimit');
2011-11-18 15:44:28 -05:00
2023-01-20 14:10:02 +01:00
const DocumentHandler = require('./lib/document_handler');
2011-11-18 15:44:28 -05:00
2011-11-18 16:57:23 -05:00
// Load the configuration and set some defaults
2023-01-20 14:10:02 +01:00
const configPath = process.argv.length <= 2 ? 'config.json' : process.argv[2];
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
2012-09-27 12:01:00 -04:00
config.port = process.env.PORT || config.port || 7777;
config.host = process.env.HOST || config.host || 'localhost';
2011-11-18 17:26:25 -05:00
// Set up the logger
if (config.logging) {
2023-01-20 14:10:02 +01:00
try {
winston.remove(winston.transports.Console);
} catch (e) {
/* was not present */
}
let detail, type;
for (let 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 17:26:25 -05:00
}
2011-11-18 10:17:41 -05:00
2011-11-18 18:04:24 -05:00
// build the store from the config on-demand - so that we don't load it
// for statics
2011-11-21 22:03:50 -05:00
if (!config.storage) {
2023-01-20 14:10:02 +01:00
config.storage = {type: 'file'};
2011-11-21 22:03:50 -05:00
}
if (!config.storage.type) {
2023-01-20 14:10:02 +01:00
config.storage.type = 'file';
2011-11-21 22:03:50 -05:00
}
2012-09-27 11:46:53 -04:00
2023-01-20 14:10:02 +01:00
let Store, preferredStore;
2012-09-27 11:50:12 -04:00
2014-06-09 16:50:43 -04:00
if (process.env.REDISTOGO_URL && config.storage.type === 'redis') {
2023-01-20 14:10:02 +01:00
const 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);
2012-09-27 11:46:53 -04:00
}
2011-11-18 10:17:41 -05:00
2023-01-20 14:10:02 +01:00
winston.info('Using storage type: ' + config.storage.type);
2011-11-27 15:49:17 -05:00
// Compress the static javascript assets
2023-01-20 14:10:02 +01:00
if (false && config.recompressStaticAssets) { // hack, don't do this shit
winston.info('Recompressing static assets');
const list = fs.readdirSync('./static');
list.forEach(item => {
if ((item.indexOf('.js') === item.length - 3) && (item.indexOf('.min.js') === -1)) {
const dest = item.substring(0, item.length - 3) + '.min' + item.substring(item.length - 3);
const orig_code = fs.readFileSync('./static/' + item, 'utf8');
fs.writeFileSync('./static/' + dest, uglify.minify(orig_code).code, 'utf8');
winston.info('compressed ' + item + ' into ' + dest);
}
});
} else {
winston.warn('Not recompressing static assets');
2011-11-27 15:49:17 -05:00
}
2011-11-22 09:22:37 -05:00
// Send the static documents into the preferred store, skipping expirations
2023-01-20 14:10:02 +01:00
let path, data;
for (let 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 09:22:37 -05:00
}
// Pick up a key generator
2023-01-20 14:10:02 +01:00
const pwOptions = config.keyGenerator || {};
pwOptions.type = pwOptions.type || 'random';
2023-01-20 14:10:02 +01:00
const gen = require('./lib/key_generators/' + pwOptions.type);
winston.info('Using key generator: ' + pwOptions.type);
const keyGenerator = new gen(pwOptions);
2011-11-22 09:22:37 -05:00
// Configure the document handler
2023-01-20 14:10:02 +01:00
const documentHandler = new DocumentHandler({
store: preferredStore, maxLength: config.maxLength, keyLength: config.keyLength, keyGenerator: keyGenerator
2011-11-22 09:22:37 -05:00
});
2023-01-20 14:10:02 +01:00
const app = connect();
2016-03-06 16:20:40 -05:00
// Rate limit all requests
if (config.rateLimits) {
2023-01-20 14:10:02 +01:00
config.rateLimits.end = true;
app.use(connect_rate_limit(config.rateLimits));
2016-03-06 16:20:40 -05:00
}
// first look at API calls
2023-01-20 14:10:02 +01:00
app.use(route(function (router) {
// get raw documents - support getting with extension
2023-01-20 14:10:02 +01:00
router.get('/raw/:id', function (request, response) {
return documentHandler.handleRawGet(request, response, config);
});
2023-01-20 14:10:02 +01:00
router.head('/raw/:id', function (request, response) {
return documentHandler.handleRawGet(request, response, config);
});
// add logs, via PUT and POST, for scripting
router.post('/log', function (request, response) {
return documentHandler.handlePutLog(request, response);
});
router.put('/log', function (request, response) {
return documentHandler.handlePutLog(request, response);
});
2023-01-20 14:10:02 +01:00
// add documents
2023-01-20 14:10:02 +01:00
router.post('/documents', function (request, response) {
return documentHandler.handlePost(request, response);
});
2023-01-20 14:10:02 +01:00
// get documents
router.get('/documents/:id', function (request, response) {
return documentHandler.handleGet(request, response, config);
});
2023-01-20 14:10:02 +01:00
router.head('/documents/:id', function (request, response) {
return documentHandler.handleGet(request, response, config);
});
2016-03-06 16:20:40 -05:00
}));
// Otherwise, try to match static files
app.use(connect_st({
2023-01-20 14:10:02 +01:00
path: __dirname + '/static', content: {maxAge: config.staticMaxAge}, passthrough: true, index: false
2016-03-06 16:20:40 -05:00
}));
// Then we can loop back - and everything else should be a token,
// so route it back to /
2023-01-20 14:10:02 +01:00
app.use(route(function (router) {
router.get('/:id', function (request, response, next) {
request.sturl = '/';
next();
});
2016-03-06 16:20:40 -05:00
}));
// And match index
app.use(connect_st({
2023-01-20 14:10:02 +01:00
path: __dirname + '/static', content: {maxAge: config.staticMaxAge}, index: 'index.html'
2016-03-06 16:20:40 -05:00
}));
http.createServer(app).listen(config.port, config.host);
2011-11-18 16:57:23 -05:00
2011-11-18 16:58:21 -05:00
winston.info('listening on ' + config.host + ':' + config.port);