move server routes into /system
This commit is contained in:
@@ -6,24 +6,17 @@ exports = module.exports = {
|
||||
passwordResetRequest,
|
||||
passwordReset,
|
||||
setupAccount,
|
||||
reboot,
|
||||
isRebootRequired,
|
||||
getConfig,
|
||||
getDisks,
|
||||
getDiskUsage,
|
||||
updateDiskUsage,
|
||||
getMemory,
|
||||
getLogs,
|
||||
getLogStream,
|
||||
|
||||
updateDashboardDomain,
|
||||
prepareDashboardDomain,
|
||||
getLanguages,
|
||||
getSystemGraphs,
|
||||
getPlatformStatus,
|
||||
getBlockDevices,
|
||||
|
||||
getPlatformStatus,
|
||||
|
||||
getLanguages,
|
||||
getLanguage,
|
||||
setLanguage,
|
||||
|
||||
getTimeZone,
|
||||
setTimeZone
|
||||
};
|
||||
@@ -35,13 +28,11 @@ const assert = require('assert'),
|
||||
constants = require('../constants.js'),
|
||||
debug = require('debug')('box:routes/cloudron'),
|
||||
eventlog = require('../eventlog.js'),
|
||||
graphs = require('../graphs.js'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
platform = require('../platform.js'),
|
||||
safe = require('safetydance'),
|
||||
speakeasy = require('speakeasy'),
|
||||
system = require('../system.js'),
|
||||
tokens = require('../tokens.js'),
|
||||
translation = require('../translation.js'),
|
||||
users = require('../users.js');
|
||||
@@ -141,20 +132,6 @@ async function setupAccount(req, res, next) {
|
||||
next(new HttpSuccess(201, { accessToken }));
|
||||
}
|
||||
|
||||
async function reboot(req, res, next) {
|
||||
// Finish the request, to let the appstore know we triggered the reboot
|
||||
next(new HttpSuccess(202, {}));
|
||||
|
||||
await safe(cloudron.reboot());
|
||||
}
|
||||
|
||||
async function isRebootRequired(req, res, next) {
|
||||
const [error, rebootRequired] = await safe(cloudron.isRebootRequired());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { rebootRequired }));
|
||||
}
|
||||
|
||||
async function getConfig(req, res, next) {
|
||||
const [error, cloudronConfig] = await safe(cloudron.getConfig());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
@@ -162,97 +139,6 @@ async function getConfig(req, res, next) {
|
||||
next(new HttpSuccess(200, cloudronConfig));
|
||||
}
|
||||
|
||||
async function getDisks(req, res, next) {
|
||||
const [getDisksError, disks] = await safe(system.getDisks());
|
||||
if (getDisksError) return next(BoxError.toHttpError(getDisksError));
|
||||
|
||||
let [getSwapsError, swaps] = await safe(system.getSwaps());
|
||||
if (getSwapsError) return next(BoxError.toHttpError(getSwapsError));
|
||||
|
||||
next(new HttpSuccess(200, { disks, swaps }));
|
||||
}
|
||||
|
||||
async function getDiskUsage(req, res, next) {
|
||||
const [error, result] = await safe(system.getDiskUsage());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { usage: result }));
|
||||
}
|
||||
|
||||
async function updateDiskUsage(req, res, next) {
|
||||
const [error, taskId] = await safe(cloudron.updateDiskUsage());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(201, { taskId }));
|
||||
}
|
||||
|
||||
async function getMemory(req, res, next) {
|
||||
const [error, result] = await safe(system.getMemory());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, result));
|
||||
}
|
||||
|
||||
async function getLogs(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.unit, 'string');
|
||||
|
||||
const 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'));
|
||||
|
||||
const options = {
|
||||
lines: lines,
|
||||
follow: false,
|
||||
format: req.query.format || 'json'
|
||||
};
|
||||
|
||||
const [error, logStream] = await safe(cloudron.getLogs(req.params.unit, options));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'application/x-logs',
|
||||
'Content-Disposition': `attachment; filename="${req.params.unit}.log"`,
|
||||
'Cache-Control': 'no-cache',
|
||||
'X-Accel-Buffering': 'no' // disable nginx buffering
|
||||
});
|
||||
logStream.pipe(res);
|
||||
}
|
||||
|
||||
async function getLogStream(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.unit, 'string');
|
||||
|
||||
const 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'));
|
||||
|
||||
const options = {
|
||||
lines: lines,
|
||||
follow: true,
|
||||
format: req.query.format || 'json'
|
||||
};
|
||||
|
||||
const [error, logStream] = await safe(cloudron.getLogs(req.params.unit, options));
|
||||
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) {
|
||||
const obj = JSON.parse(data);
|
||||
res.write(sse(obj.realtimeTimestamp, JSON.stringify(obj))); // send timestamp as id
|
||||
});
|
||||
logStream.on('end', res.end.bind(res));
|
||||
logStream.on('error', res.end.bind(res, null));
|
||||
}
|
||||
|
||||
async function updateDashboardDomain(req, res, next) {
|
||||
if (!req.body.domain || typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));
|
||||
|
||||
@@ -278,28 +164,10 @@ async function getLanguages(req, res, next) {
|
||||
next(new HttpSuccess(200, { languages }));
|
||||
}
|
||||
|
||||
async function getSystemGraphs(req, res, next) {
|
||||
if (!req.query.fromMinutes || !parseInt(req.query.fromMinutes)) return next(new HttpError(400, 'fromMinutes must be a number'));
|
||||
|
||||
const fromMinutes = parseInt(req.query.fromMinutes);
|
||||
const noNullPoints = !!req.query.noNullPoints;
|
||||
const [error, result] = await safe(graphs.getSystem(fromMinutes, noNullPoints));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
|
||||
next(new HttpSuccess(200, result));
|
||||
}
|
||||
|
||||
async function getPlatformStatus(req, res, next) {
|
||||
next(new HttpSuccess(200, platform.getStatus()));
|
||||
}
|
||||
|
||||
async function getBlockDevices(req, res, next) {
|
||||
const [error, devices] = await safe(cloudron.getBlockDevices());
|
||||
if (error) return next(new HttpError(500, error));
|
||||
|
||||
next(new HttpSuccess(200, { devices }));
|
||||
}
|
||||
|
||||
async function getTimeZone(req, res, next) {
|
||||
const [error, timeZone] = await safe(cloudron.getTimeZone());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
Reference in New Issue
Block a user