'use strict'; exports = module.exports = { getAll, get, configure, getLogs, getLogStream, restart, rebuild }; const assert = require('assert'), BoxError = require('../boxerror.js'), HttpError = require('connect-lastmile').HttpError, HttpSuccess = require('connect-lastmile').HttpSuccess, services = require('../services.js'); function getAll(req, res, next) { services.getServiceIds(function (error, result) { if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(200, { services: result })); }); } function get(req, res, next) { assert.strictEqual(typeof req.params.service, 'string'); req.clearTimeout(); services.getServiceStatus(req.params.service, function (error, result) { if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(200, { service: result })); }); } function configure(req, res, next) { assert.strictEqual(typeof req.params.service, 'string'); if (typeof req.body.memoryLimit !== 'number') return next(new HttpError(400, 'memoryLimit must be a number')); const data = { memoryLimit: req.body.memoryLimit }; if (req.params.service === 'sftp') { if (typeof req.body.requireAdmin !== 'boolean') return next(new HttpError(400, 'requireAdmin must be a boolean')); data.requireAdmin = req.body.requireAdmin; } services.configureService(req.params.service, data, function (error) { if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(202, {})); }); } function getLogs(req, res, next) { assert.strictEqual(typeof req.params.service, 'string'); var lines = 'lines' in req.query ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id if (isNaN(lines)) return next(new HttpError(400, 'lines must be a number')); var options = { lines: lines, follow: false, format: req.query.format || 'json' }; services.getServiceLogs(req.params.service, options, function (error, logStream) { if (error) return next(BoxError.toHttpError(error)); res.writeHead(200, { 'Content-Type': 'application/x-logs', 'Content-Disposition': `attachment; filename="${req.params.service}.log"`, 'Cache-Control': 'no-cache', 'X-Accel-Buffering': 'no' // disable nginx buffering }); logStream.pipe(res); }); } // this route is for streaming logs function getLogStream(req, res, next) { assert.strictEqual(typeof req.params.service, 'string'); var lines = 'lines' in req.query ? parseInt(req.query.lines, 10) : 10; // we ignore last-event-id if (isNaN(lines)) return next(new HttpError(400, 'lines must be a valid number')); function sse(id, data) { return 'id: ' + id + '\ndata: ' + data + '\n\n'; } if (req.headers.accept !== 'text/event-stream') return next(new HttpError(400, 'This API call requires EventStream')); var options = { lines: lines, follow: true, format: 'json' }; services.getServiceLogs(req.params.service, options, function (error, logStream) { if (error) return next(BoxError.toHttpError(error)); res.writeHead(200, { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', 'Connection': 'keep-alive', 'X-Accel-Buffering': 'no', // disable nginx buffering 'Access-Control-Allow-Origin': '*' }); res.write('retry: 3000\n'); res.on('close', logStream.close); logStream.on('data', function (data) { var obj = JSON.parse(data); res.write(sse(obj.monotonicTimestamp, JSON.stringify(obj))); // send timestamp as id }); logStream.on('end', res.end.bind(res)); logStream.on('error', res.end.bind(res, null)); }); } function restart(req, res, next) { assert.strictEqual(typeof req.params.service, 'string'); services.restartService(req.params.service, function (error) { if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(202, {})); }); } function rebuild(req, res, next) { assert.strictEqual(typeof req.params.service, 'string'); services.rebuildService(req.params.service, function (error) { if (error) return next(BoxError.toHttpError(error)); next(new HttpSuccess(202, {})); }); }