Add API to get cloudron logs

part of #304
This commit is contained in:
Girish Ramakrishnan
2017-04-18 15:15:35 -07:00
parent 5f888341ea
commit 0c706cffc0
4 changed files with 89 additions and 1 deletions

View File

@@ -13,7 +13,8 @@ exports = module.exports = {
getDisks: getDisks,
update: update,
feedback: feedback,
checkForUpdates: checkForUpdates
checkForUpdates: checkForUpdates,
getLogs: getLogs
};
var assert = require('assert'),
@@ -223,3 +224,24 @@ function feedback(req, res, next) {
next(new HttpSuccess(201, {}));
}
function getLogs(req, res, next) {
var lines = req.query.lines ? parseInt(req.query.lines, 10) : 100;
if (isNaN(lines)) return next(new HttpError(400, 'lines must be a number'));
var units = req.query.units || 'all';
debug('Getting logs of unit:%s', units);
cloudron.getLogs(units.split(','), lines, false /* follow */, function (error, logStream) {
if (error && error.reason === CloudronError.BAD_FIELD) return next(new HttpError(404, 'Invalid type'));
if (error) return next(new HttpError(500, error));
res.writeHead(200, {
'Content-Type': 'application/x-logs',
'Content-Disposition': 'attachment; filename="log.txt"',
'Cache-Control': 'no-cache',
'X-Accel-Buffering': 'no' // disable nginx buffering
});
logStream.pipe(res);
});
}