Compare commits

..

1 Commits

Author SHA1 Message Date
Jack Hadrill 17320ee1dd Simplify for Butlersaurus
TerribleCodeClub/ButlerBin/pipeline/head This commit looks good Details
2020-06-08 22:59:10 +01:00
3 changed files with 26 additions and 47 deletions

View File

@ -1,5 +1,5 @@
var Winston = require('winston');
var Busboy = require('busboy'); var Busboy = require('busboy');
var Winston = require('winston');
// For handling serving stored documents // For handling serving stored documents

View File

@ -33,7 +33,7 @@ FileDocumentStore.prototype.set = function(key, data, callback) {
FileDocumentStore.prototype.get = function(key, callback) { FileDocumentStore.prototype.get = function(key, callback) {
var _this = this; var _this = this;
var fn = this.basePath + '/' + FileDocumentStore.md5(key); var fn = _this.basePath + '/' + FileDocumentStore.md5(key);
fs.readFile(fn, 'utf8', function(err, data) { fs.readFile(fn, 'utf8', function(err, data) {
if (err) { if (err) {
callback(false); callback(false);

View File

@ -1,56 +1,39 @@
var http = require('http'); var Connect = require('connect');
var fs = require('fs'); var ConnectSt = require('st');
var Fs = require('fs');
var uglify = require('uglify-js'); var Http = require('http');
var winston = require('winston'); var Route = require('connect-route');
var connect = require('connect'); var Winston = require('winston');
var route = require('connect-route');
var connect_st = require('st');
var DocumentHandler = require('./lib/document_handler'); var DocumentHandler = require('./lib/document_handler');
var KeyGenerator = require('./lib/key_generator');
var Store = require('./lib/document_store');
// Load the configuration and set some defaults // Load the configuration and set some defaults
var config = JSON.parse(fs.readFileSync('./config.js', 'utf8')); var config = JSON.parse(Fs.readFileSync('./config.js', 'utf8'));
config.host = config.host || '127.0.0.1'; config.host = config.host || '127.0.0.1';
config.port = config.port || 7777; config.port = config.port || 7777;
// Set up the logger // Set up the logger
if (config.logging) { if (config.logging) {
try { try {
winston.remove(winston.transports.Console); Winston.remove(Winston.transports.Console);
} catch(e) { } catch(e) {
/* was not present */ // This is fine
} }
var detail, type; var detail, type;
for (var i = 0; i < config.logging.length; i++) { for (var i = 0; i < config.logging.length; i++) {
detail = config.logging[i]; detail = config.logging[i];
type = detail.type; type = detail.type;
delete detail.type; delete detail.type;
winston.add(winston.transports[type], detail); Winston.add(Winston.transports[type], detail);
} }
} }
var Store = require('./lib/document_store'); // Configure the data store
store = new Store(); store = new Store();
// Compress the static javascript assets
if (config.recompressStaticAssets) {
var list = fs.readdirSync('./static');
for (var j = 0; j < list.length; j++) {
var item = list[j];
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);
var orig_code = fs.readFileSync('./static/' + item, 'utf8');
fs.writeFileSync('./static/' + dest, uglify.minify(orig_code).code, 'utf8');
winston.info('compressed ' + item + ' into ' + dest);
}
}
}
// Pick up a key generator // Pick up a key generator
var KeyGenerator = require('./lib/key_generator');
var keyGenerator = new KeyGenerator(); var keyGenerator = new KeyGenerator();
// Configure the document handler // Configure the document handler
@ -61,50 +44,46 @@ var documentHandler = new DocumentHandler({
keyGenerator: keyGenerator keyGenerator: keyGenerator
}); });
var app = connect(); var app = Connect();
// first look at API calls // API
app.use(route(function(router) { app.use(Route(function(router) {
// get raw documents - support getting with extension
router.get('/raw/:id', function(request, response) { router.get('/raw/:id', function(request, response) {
var key = request.params.id.split('.')[0]; var key = request.params.id.split('.')[0];
return documentHandler.handleRawGet(key, response); return documentHandler.handleRawGet(key, response);
}); });
// add documents
router.post('/documents', function(request, response) { router.post('/documents', function(request, response) {
return documentHandler.handlePost(request, response); return documentHandler.handlePost(request, response);
}); });
// get documents
router.get('/documents/:id', function(request, response) { router.get('/documents/:id', function(request, response) {
var key = request.params.id.split('.')[0]; var key = request.params.id.split('.')[0];
return documentHandler.handleGet(key, response); return documentHandler.handleGet(key, response);
}); });
})); }));
// Otherwise, try to match static files // Static files
app.use(connect_st({ app.use(ConnectSt({
path: __dirname + '/static', path: __dirname + '/static',
content: { maxAge: config.staticMaxAge }, content: { maxAge: config.staticMaxAge },
passthrough: true, passthrough: true,
index: false index: false
})); }));
// Then we can loop back - and everything else should be a token, // All the pastebin IDs
// so route it back to / app.use(Route(function(router) {
app.use(route(function(router) {
router.get('/:id', function(request, response, next) { router.get('/:id', function(request, response, next) {
request.sturl = '/'; request.sturl = '/';
next(); next();
}); });
})); }));
// And match index // Index
app.use(connect_st({ app.use(ConnectSt({
path: __dirname + '/static', path: __dirname + '/static',
content: { maxAge: config.staticMaxAge }, content: { maxAge: config.staticMaxAge },
index: 'index.html' index: 'index.html'
})); }));
http.createServer(app).listen(config.port, config.host); Http.createServer(app).listen(config.port, config.host);
winston.info('listening on ' + config.host + ':' + config.port); Winston.info('Listening on ' + config.host + ':' + config.port);