changed config API to use an express router

This commit is contained in:
tigerbot 2017-10-03 19:11:49 -06:00
parent 3d3fac5087
commit f25a0191bd
2 changed files with 47 additions and 32 deletions

View File

@ -38,6 +38,13 @@ module.exports.create = function (deps, conf) {
return true; return true;
} }
} }
function makeCorsHandler(methods) {
return function corsHandler(req, res, next) {
if (!handleCors(req, res, methods)) {
next();
}
};
}
function isAuthorized(req, res, fn) { function isAuthorized(req, res, fn) {
var auth = jwt.decode((req.headers.authorization||'').replace(/^bearer\s+/i, '')); var auth = jwt.decode((req.headers.authorization||'').replace(/^bearer\s+/i, ''));
@ -139,7 +146,10 @@ module.exports.create = function (deps, conf) {
; ;
} }
return { // This object contains all of the API endpoints written before we changed how
// the API routing is handled. Eventually it will hopefully disappear, but for
// now we're focusing on the things that need changing more.
var oldEndPoints = {
init: function (req, res) { init: function (req, res) {
if (handleCors(req, res, ['GET', 'POST'])) { if (handleCors(req, res, ['GET', 'POST'])) {
return; return;
@ -262,28 +272,6 @@ module.exports.create = function (deps, conf) {
}); });
}); });
} }
, config: function (req, res) {
if (handleCors(req, res)) {
return;
}
isAuthorized(req, res, function () {
if ('POST' !== req.method) {
res.setHeader('Content-Type', 'application/json;');
res.end(JSON.stringify(deps.recase.snakeCopy(conf)));
return;
}
jsonParser(req, res, function () {
console.log('config POST body', req.body);
// Since we are sending the changes to another process we don't really
// have a good way of seeing if it worked, so always report success
deps.storage.config.save(req.body);
res.setHeader('Content-Type', 'application/json;');
res.end('{"success":true}');
});
});
}
, request: function (req, res) { , request: function (req, res) {
if (handleCors(req, res, '*')) { if (handleCors(req, res, '*')) {
return; return;
@ -381,4 +369,38 @@ module.exports.create = function (deps, conf) {
}); });
} }
}; };
function handleOldApis(req, res, next) {
if (typeof oldEndPoints[req.params.name] === 'function') {
oldEndPoints[req.params.name](req, res);
} else {
next();
}
}
var config = { restful: {} };
config.restful.readConfig = function (req, res) {
res.send(deps.recase.snakeCopy(conf));
};
config.restful.saveConfig = function (req, res) {
console.log('config POST body', req.body);
// Since we are sending the changes to another process we don't really
// have a good way of seeing if it worked, so always report success
deps.storage.config.save(req.body);
res.send({ success: true });
};
var app = require('express')();
// Handle all of the API endpoints using the old definition style, and then we can
// add middleware without worrying too much about the consequences to older code.
app.use('/:name', handleOldApis);
app.use('/', isAuthorized, jsonParser);
app.use( '/config', makeCorsHandler());
app.get( '/config', config.restful.readConfig);
app.post( '/config', config.restful.saveConfig);
return app;
}; };

View File

@ -15,15 +15,8 @@ module.exports.create = function (deps, conf) {
var app = express(); var app = express();
var apis = require('./apis').create(deps, conf); var apis = require('./apis').create(deps, conf);
function handleApis(req, res, next) { app.use('/api/goldilocks@daplie.com', apis);
if (typeof apis[req.params.name] === 'function') { app.use('/api/com.daplie.goldilocks', apis);
apis[req.params.name](req, res);
} else {
next();
}
}
app.use('/api/goldilocks@daplie.com/:name', handleApis);
app.use('/api/com.daplie.goldilocks/:name', handleApis);
// Serve the static assets for the UI (even though it probably won't be used very // Serve the static assets for the UI (even though it probably won't be used very
// often since it only works on localhost domains). Note that we are using the default // often since it only works on localhost domains). Note that we are using the default