implemented routes to edit and delete modules and domains

This commit is contained in:
tigerbot 2017-10-11 17:13:33 -06:00
parent 2a57a1e12c
commit 503da9efd0
2 changed files with 178 additions and 73 deletions

View File

@ -385,7 +385,7 @@ module.exports.create = function (deps, conf) {
part = part[req.params.group]; part = part[req.params.group];
} }
if (part && req.params.domId) { if (part && req.params.domId) {
part = part.domains.find(req.params.domId); part = part.domains.findId(req.params.domId);
} }
if (part && req.params.mod) { if (part && req.params.mod) {
part = part[req.params.mod]; part = part[req.params.mod];
@ -394,7 +394,7 @@ module.exports.create = function (deps, conf) {
part = part[req.params.modGrp]; part = part[req.params.modGrp];
} }
if (part && req.params.modId) { if (part && req.params.modId) {
part = part.find(req.params.modId); part = part.findId(req.params.modId);
} }
if (part) { if (part) {
@ -404,8 +404,20 @@ module.exports.create = function (deps, conf) {
} }
}; };
config.restful.saveBaseConfig = function (req, res) { config.save = function (changer) {
var errors = changer.validate();
if (errors.length) {
throw Object.assign(new Error(), errors[0], {statusCode: 400});
}
return deps.storage.config.save(changer);
};
config.restful.saveBaseConfig = function (req, res, next) {
console.log('config POST body', JSON.stringify(req.body)); console.log('config POST body', JSON.stringify(req.body));
if (req.params.group === 'domains') {
next();
return;
}
deps.PromiseA.resolve().then(function () { deps.PromiseA.resolve().then(function () {
var update; var update;
@ -417,12 +429,8 @@ module.exports.create = function (deps, conf) {
} }
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
var errors = changer.update(update); changer.update(update);
if (errors.length) { return config.save(changer);
throw Object.assign(new Error(), errors[0], {statusCode: 400});
}
return deps.storage.config.save(changer);
}).then(function (config) { }).then(function (config) {
if (req.params.group) { if (req.params.group) {
config = config[req.params.group]; config = config[req.params.group];
@ -434,60 +442,96 @@ module.exports.create = function (deps, conf) {
res.end(JSON.stringify({error: {message: err.message, code: err.code}})); res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
}; };
config.restful.createModule = function (req, res) {
var group = req.params.group; config.extractModList = function (changer, params) {
var err; var err;
deps.PromiseA.resolve().then(function () { if (params.domId) {
var changer = new (require('./config').ConfigChanger)(conf); var dom = changer.domains.find(function (dom) {
var modList; return dom.id === params.domId;
if (req.params.domId) { });
var dom = changer.domains.find(req.params.domId);
if (!dom) { if (!dom) {
err = new Error("no domain with ID '"+req.params.domId+"'"); err = new Error("no domain with ID '"+params.domId+"'");
} else if (!dom.modules[group]) { } else if (!dom.modules[params.group]) {
err = new Error("domains don't contain '"+group+"' modules"); err = new Error("domains don't contain '"+params.group+"' modules");
} else { } else {
modList = dom.modules[group]; return dom.modules[params.group];
} }
} else { } else {
if (!changer[group] || !changer[group].modules) { if (!changer[params.group] || !changer[params.group].modules) {
err = new Error("'"+group+"' is not a valid settings group or doesn't support modules"); err = new Error("'"+params.group+"' is not a valid settings group or doesn't support modules");
} else { } else {
modList = changer[group].modules; return changer[params.group].modules;
} }
} }
if (err) {
err.statusCode = 404; err.statusCode = 404;
throw err; throw err;
};
config.restful.createModule = function (req, res, next) {
if (req.params.group === 'domains') {
next();
return;
} }
deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf);
var modList = config.extractModList(changer, req.params);
var update = req.body; var update = req.body;
if (!Array.isArray(update)) { if (!Array.isArray(update)) {
update = [ update ]; update = [ update ];
} }
update.forEach(modList.add, modList); update.forEach(modList.add, modList);
var errors = changer.validate(); return config.save(changer);
if (errors.length) { }).then(function (newConf) {
throw Object.assign(new Error(), errors[0], {statusCode: 400}); res.send(deps.recase.snakeCopy(config.extractModList(newConf, req.params)));
}
return deps.storage.config.save(changer);
}).then(function (config) {
var result;
if (!req.params.domId) {
result = config[group].modules;
} else {
result = config.domains.find(function (dom) { return dom.id === req.params.domId; }).modules[group];
}
res.send(deps.recase.snakeCopy(result));
}, function (err) { }, function (err) {
res.statusCode = err.statusCode || 500; res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString(); err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}})); res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
}); });
}; };
config.restful.updateModule = function (req, res, next) {
if (req.params.group === 'domains') {
next();
return;
}
deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf);
var modList = config.extractModList(changer, req.params);
modList.update(req.params.modId, req.body);
return config.save(changer);
}).then(function (newConf) {
res.send(deps.recase.snakeCopy(config.extractModList(newConf, req.params)));
}, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
});
};
config.restful.removeModule = function (req, res, next) {
if (req.params.group === 'domains') {
next();
return;
}
deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf);
var modList = config.extractModList(changer, req.params);
modList.remove(req.params.modId);
return config.save(changer);
}).then(function (newConf) {
res.send(deps.recase.snakeCopy(config.extractModList(newConf, req.params)));
}, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
});
};
config.restful.createDomain = function (req, res) { config.restful.createDomain = function (req, res) {
deps.PromiseA.resolve().then(function () { deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf); var changer = new (require('./config').ConfigChanger)(conf);
@ -497,13 +541,37 @@ module.exports.create = function (deps, conf) {
update = [ update ]; update = [ update ];
} }
update.forEach(changer.domains.add, changer.domains); update.forEach(changer.domains.add, changer.domains);
return config.save(changer);
var errors = changer.validate(); }).then(function (config) {
if (errors.length) { res.send(deps.recase.snakeCopy(config.domains));
throw Object.assign(new Error(), errors[0], {statusCode: 400}); }, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
});
};
config.restful.updateDomain = function (req, res) {
deps.PromiseA.resolve().then(function () {
if (req.body.modules) {
throw Object.assign(new Error('do not add modules with this route'), {statusCode: 400});
} }
return deps.storage.config.save(changer); var changer = new (require('./config').ConfigChanger)(conf);
changer.domains.update(req.params.domId, req.body);
return config.save(changer);
}).then(function (config) {
res.send(deps.recase.snakeCopy(config.domains));
}, function (err) {
res.statusCode = err.statusCode || 500;
err.message = err.message || err.toString();
res.end(JSON.stringify({error: {message: err.message, code: err.code}}));
});
};
config.restful.removeDomain = function (req, res) {
deps.PromiseA.resolve().then(function () {
var changer = new (require('./config').ConfigChanger)(conf);
changer.domains.remove(req.params.domId);
return config.save(changer);
}).then(function (config) { }).then(function (config) {
res.send(deps.recase.snakeCopy(config.domains)); res.send(deps.recase.snakeCopy(config.domains));
}, function (err) { }, function (err) {
@ -521,7 +589,8 @@ module.exports.create = function (deps, conf) {
app.use('/', isAuthorized, jsonParser); app.use('/', isAuthorized, jsonParser);
app.use( '/config', makeCorsHandler()); // Not all config routes support PUT or DELETE, but not worth making this more specific
app.use( '/config', makeCorsHandler(['GET', 'POST', 'PUT', 'DELETE']));
app.get( '/config', config.restful.readConfig); app.get( '/config', config.restful.readConfig);
app.get( '/config/:group', config.restful.readConfig); app.get( '/config/:group', config.restful.readConfig);
app.get( '/config/:group/:mod(modules)/:modId?', config.restful.readConfig); app.get( '/config/:group/:mod(modules)/:modId?', config.restful.readConfig);
@ -529,10 +598,19 @@ module.exports.create = function (deps, conf) {
app.get( '/config/domains/:domId/:mod(modules)/:modGrp/:modId?', config.restful.readConfig); app.get( '/config/domains/:domId/:mod(modules)/:modGrp/:modId?', config.restful.readConfig);
app.post( '/config', config.restful.saveBaseConfig); app.post( '/config', config.restful.saveBaseConfig);
app.post( '/config/:group(?!domains)', config.restful.saveBaseConfig); app.post( '/config/:group', config.restful.saveBaseConfig);
app.post( '/config/:group(?!domains)/modules', config.restful.createModule);
app.post( '/config/domains', config.restful.createDomain); app.post( '/config/:group/modules', config.restful.createModule);
app.put( '/config/:group/modules/:modId', config.restful.updateModule);
app.delete('/config/:group/modules/:modId', config.restful.removeModule);
app.post( '/config/domains/:domId/modules/:group', config.restful.createModule); app.post( '/config/domains/:domId/modules/:group', config.restful.createModule);
app.put( '/config/domains/:domId/modules/:group/:modId', config.restful.updateModule);
app.delete('/config/domains/:domId/modules/:group/:modId', config.restful.removeModule);
app.post( '/config/domains', config.restful.createDomain);
app.put( '/config/domains/:domId', config.restful.updateDomain);
app.delete('/config/domains/:domId', config.restful.removeDomain);
return app; return app;
}; };

View File

@ -207,20 +207,54 @@ function validate(config) {
} }
module.exports.validate = validate; module.exports.validate = validate;
class IdList extends Array {
class ModuleList extends Array {
constructor(rawList) { constructor(rawList) {
super(); super();
if (Array.isArray(rawList)) { if (Array.isArray(rawList)) {
Object.assign(this, JSON.parse(JSON.stringify(rawList))); Object.assign(this, JSON.parse(JSON.stringify(rawList)));
} }
this._itemName = 'item';
} }
find(id) { findId(id) {
return Array.prototype.find.call(this, function (mod) { return Array.prototype.find.call(this, function (dom) {
return mod.id === id; return dom.id === id;
}); });
} }
add(item) {
item.id = require('crypto').randomBytes(4).toString('hex');
this.push(item);
}
update(id, update) {
var item = this.findId(id);
if (!item) {
var error = new Error("no "+this._itemName+" with ID '"+id+"'");
error.statusCode = 404;
throw error;
}
Object.assign(this.findId(id), update);
}
remove(id) {
var index = this.findIndex(function (dom) {
return dom.id === id;
});
if (index < 0) {
var error = new Error("no "+this._itemName+" with ID '"+id+"'");
error.statusCode = 404;
throw error;
}
this.splice(index, 1);
}
}
class ModuleList extends IdList {
constructor(rawList) {
super(rawList);
this._itemName = 'module';
}
add(mod) { add(mod) {
if (!mod.type) { if (!mod.type) {
throw new Error("module must have a 'type' defined"); throw new Error("module must have a 'type' defined");
@ -233,12 +267,10 @@ class ModuleList extends Array {
this.push(mod); this.push(mod);
} }
} }
class DomainList extends Array { class DomainList extends IdList {
constructor(rawList) { constructor(rawList) {
super(); super(rawList);
if (Array.isArray(rawList)) { this._itemName = 'domain';
Object.assign(this, JSON.parse(JSON.stringify(rawList)));
}
this.forEach(function (dom) { this.forEach(function (dom) {
dom.modules = { dom.modules = {
http: new ModuleList((dom.modules || {}).http), http: new ModuleList((dom.modules || {}).http),
@ -247,11 +279,6 @@ class DomainList extends Array {
}); });
} }
find(id) {
return Array.prototype.find.call(this, function (dom) {
return dom.id === id;
});
}
add(dom) { add(dom) {
if (!Array.isArray(dom.names) || !dom.names.length) { if (!Array.isArray(dom.names) || !dom.names.length) {
throw new Error("domains must have a non-empty array for 'names'"); throw new Error("domains must have a non-empty array for 'names'");