ButlerBin/server.js

90 lines
2.2 KiB
JavaScript
Raw Normal View History

2020-06-08 20:56:17 +01:00
var Connect = require('connect');
var ConnectSt = require('st');
var Fs = require('fs');
var Http = require('http');
var Route = require('connect-route');
var Winston = require('winston');
2011-11-18 20:44:28 +00:00
2011-11-18 20:51:38 +00:00
var DocumentHandler = require('./lib/document_handler');
2020-06-08 20:56:17 +01:00
var KeyGenerator = require('./lib/key_generator');
var Store = require('./lib/document_store');
2011-11-18 20:44:28 +00:00
2011-11-18 21:57:23 +00:00
// Load the configuration and set some defaults
2020-06-08 20:56:17 +01:00
var config = JSON.parse(Fs.readFileSync('./config.js', 'utf8'));
config.host = config.host || '127.0.0.1';
config.port = config.port || 7777;
2011-11-18 22:26:25 +00:00
// Set up the logger
if (config.logging) {
try {
2020-06-08 20:56:17 +01:00
Winston.remove(Winston.transports.Console);
2017-06-26 17:38:17 +01:00
} catch(e) {
2020-06-08 20:56:17 +01:00
// This is fine
2017-06-26 17:38:17 +01:00
}
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;
2020-06-08 20:56:17 +01:00
Winston.add(Winston.transports[type], detail);
2011-11-27 20:49:17 +00:00
}
}
2020-06-08 20:56:17 +01:00
// Configure the data store
store = new Store();
2011-11-22 14:22:37 +00:00
// Pick up a key generator
2020-06-08 20:56:17 +01:00
var keyGenerator = new KeyGenerator();
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,
keyLength: config.keyLength,
keyGenerator: keyGenerator
2011-11-22 14:22:37 +00:00
});
2020-06-08 20:56:17 +01:00
var app = Connect();
2016-03-06 21:20:40 +00:00
2020-06-08 20:56:17 +01:00
// API
app.use(Route(function(router) {
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
});
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);
});
2017-06-26 17:38:17 +01:00
router.get('/documents/:id', function(request, response) {
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
});
}));
2020-06-08 20:56:17 +01:00
// Static files
app.use(ConnectSt({
2016-03-06 21:20:40 +00:00
path: __dirname + '/static',
content: { maxAge: config.staticMaxAge },
passthrough: true,
index: false
}));
2020-06-08 20:56:17 +01:00
// All the pastebin IDs
app.use(Route(function(router) {
2016-03-06 21:20:40 +00:00
router.get('/:id', function(request, response, next) {
request.sturl = '/';
next();
});
}));
2020-06-08 20:56:17 +01:00
// Index
app.use(ConnectSt({
2016-03-06 21:20:40 +00:00
path: __dirname + '/static',
content: { maxAge: config.staticMaxAge },
index: 'index.html'
}));
2020-06-08 20:56:17 +01:00
Http.createServer(app).listen(config.port, config.host);
2011-11-18 21:57:23 +00:00
2020-06-08 20:56:17 +01:00
Winston.info('Listening on ' + config.host + ':' + config.port);