Compare commits

..

1 Commits

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

View File

@ -1,5 +1,5 @@
var Busboy = require('busboy');
var Winston = require('winston'); var Winston = require('winston');
var Busboy = require('busboy');
// 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,39 +1,56 @@
var Connect = require('connect'); var http = require('http');
var ConnectSt = require('st'); var fs = require('fs');
var Fs = require('fs');
var Http = require('http'); var uglify = require('uglify-js');
var Route = require('connect-route'); var winston = require('winston');
var Winston = require('winston'); var connect = require('connect');
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) {
// This is fine /* was not present */
} }
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);
} }
} }
// Configure the data store var Store = require('./lib/document_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
@ -44,46 +61,50 @@ var documentHandler = new DocumentHandler({
keyGenerator: keyGenerator keyGenerator: keyGenerator
}); });
var app = Connect(); var app = connect();
// API // first look at API calls
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);
}); });
})); }));
// Static files // Otherwise, try to match static files
app.use(ConnectSt({ app.use(connect_st({
path: __dirname + '/static', path: __dirname + '/static',
content: { maxAge: config.staticMaxAge }, content: { maxAge: config.staticMaxAge },
passthrough: true, passthrough: true,
index: false index: false
})); }));
// All the pastebin IDs // Then we can loop back - and everything else should be a token,
app.use(Route(function(router) { // so route it back to /
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();
}); });
})); }));
// Index // And match index
app.use(ConnectSt({ app.use(connect_st({
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);