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 Busboy = require('busboy');
// For handling serving stored documents

View File

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

View File

@ -1,39 +1,56 @@
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');
var http = require('http');
var fs = require('fs');
var uglify = require('uglify-js');
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 KeyGenerator = require('./lib/key_generator');
var Store = require('./lib/document_store');
// 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.port = config.port || 7777;
// Set up the logger
if (config.logging) {
try {
Winston.remove(Winston.transports.Console);
winston.remove(winston.transports.Console);
} catch(e) {
// This is fine
/* was not present */
}
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);
winston.add(winston.transports[type], detail);
}
}
// Configure the data store
var Store = require('./lib/document_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
var KeyGenerator = require('./lib/key_generator');
var keyGenerator = new KeyGenerator();
// Configure the document handler
@ -44,46 +61,50 @@ var documentHandler = new DocumentHandler({
keyGenerator: keyGenerator
});
var app = Connect();
var app = connect();
// API
app.use(Route(function(router) {
// first look at API calls
app.use(route(function(router) {
// get raw documents - support getting with extension
router.get('/raw/:id', function(request, response) {
var key = request.params.id.split('.')[0];
return documentHandler.handleRawGet(key, response);
});
// add documents
router.post('/documents', function(request, response) {
return documentHandler.handlePost(request, response);
});
// get documents
router.get('/documents/:id', function(request, response) {
var key = request.params.id.split('.')[0];
return documentHandler.handleGet(key, response);
});
}));
// Static files
app.use(ConnectSt({
// Otherwise, try to match static files
app.use(connect_st({
path: __dirname + '/static',
content: { maxAge: config.staticMaxAge },
passthrough: true,
index: false
}));
// All the pastebin IDs
app.use(Route(function(router) {
// 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();
});
}));
// Index
app.use(ConnectSt({
// 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);
http.createServer(app).listen(config.port, config.host);
Winston.info('Listening on ' + config.host + ':' + config.port);
winston.info('listening on ' + config.host + ':' + config.port);