Files
cloudron-box/src/routes/settings.js
T

60 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2020-03-18 17:13:41 -07:00
set,
get,
};
2021-05-14 15:07:29 -07:00
const assert = require('assert'),
2019-10-25 15:58:11 -07:00
BoxError = require('../boxerror.js'),
2018-10-12 17:04:04 -07:00
docker = require('../docker.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
2021-08-18 15:40:28 -07:00
safe = require('safetydance'),
2019-10-22 11:03:56 -07:00
settings = require('../settings.js');
2021-08-19 13:24:38 -07:00
async function getRegistryConfig(req, res, next) {
const [error, registryConfig] = await safe(settings.getRegistryConfig());
if (error) return next(BoxError.toHttpError(error));
2019-10-22 22:07:44 -07:00
2021-08-19 13:24:38 -07:00
next(new HttpSuccess(200, docker.removePrivateFields(registryConfig)));
2019-10-22 22:07:44 -07:00
}
2021-08-19 13:24:38 -07:00
async function setRegistryConfig(req, res, next) {
2018-10-12 17:04:04 -07:00
assert.strictEqual(typeof req.body, 'object');
2021-03-02 18:21:35 -08:00
if (!req.body.provider || typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
if (req.body.provider !== 'noop') {
if (typeof req.body.serverAddress !== 'string') return next(new HttpError(400, 'serverAddress is required'));
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username is required'));
if ('email' in req.body && typeof req.body.email !== 'string') return next(new HttpError(400, 'email is required'));
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password is required'));
}
2018-10-12 17:04:04 -07:00
2021-08-19 13:24:38 -07:00
const [error] = await safe(settings.setRegistryConfig(req.body));
if (error) return next(BoxError.toHttpError(error));
2018-10-12 17:04:04 -07:00
2021-08-19 13:24:38 -07:00
next(new HttpSuccess(200));
2018-10-12 17:04:04 -07:00
}
2019-01-25 16:33:22 -08:00
function get(req, res, next) {
assert.strictEqual(typeof req.params.setting, 'string');
switch (req.params.setting) {
2019-10-22 22:07:44 -07:00
case settings.REGISTRY_CONFIG_KEY: return getRegistryConfig(req, res, next);
2019-01-25 16:33:22 -08:00
default: return next(new HttpError(404, 'No such setting'));
}
}
function set(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
switch (req.params.setting) {
2019-10-22 22:07:44 -07:00
case settings.REGISTRY_CONFIG_KEY: return setRegistryConfig(req, res, next);
2019-01-25 14:57:07 -08:00
2019-01-25 16:33:22 -08:00
default: return next(new HttpError(404, 'No such setting'));
}
}