Merge branch 'middlewarify' into stats-middleware

This commit is contained in:
Daniel García Aubert 2017-10-05 18:21:02 +02:00
commit 242e63716f
4 changed files with 8 additions and 14 deletions

View File

@ -4,10 +4,6 @@ function BaseController() {
module.exports = BaseController; module.exports = BaseController;
BaseController.prototype.send = function(req, res, body, status, headers) { BaseController.prototype.send = function(req, res, body, status, headers) {
if (req.params.dbhost) {
res.set('X-Served-By-DB-Host', req.params.dbhost);
}
if (headers) { if (headers) {
res.set(headers); res.set(headers);
} }

View File

@ -1,7 +1,7 @@
const _ = require('underscore'); const _ = require('underscore');
module.exports = function dbConnSetupMiddleware(pgConnection) { module.exports = function dbConnSetupMiddleware(pgConnection) {
return function (req, res, next) { return function dbConnSetup(req, res, next) {
const user = req.context.user; const user = req.context.user;
// FIXME: this function shouldn't be able to change `req.params`. It should return an // FIXME: this function shouldn't be able to change `req.params`. It should return an
@ -12,7 +12,7 @@ module.exports = function dbConnSetupMiddleware(pgConnection) {
err.http_status = 404; err.http_status = 404;
} }
req.profiler.done('req2params'); req.profiler.done('req2params');
return next(err, req); return next(err);
} }
// Add default database connection parameters // Add default database connection parameters
@ -24,6 +24,8 @@ module.exports = function dbConnSetupMiddleware(pgConnection) {
dbport: global.environment.postgres.port dbport: global.environment.postgres.port
}); });
res.set('X-Served-By-DB-Host', req.params.dbhost);
// FIXME: Temporary hack to share data between middlewares. Express overrides req.params to // FIXME: Temporary hack to share data between middlewares. Express overrides req.params to
// parse url params to an object and it's performed after matching path and controller. // parse url params to an object and it's performed after matching path and controller.
if (!res.locals) { if (!res.locals) {
@ -33,7 +35,7 @@ module.exports = function dbConnSetupMiddleware(pgConnection) {
req.profiler.done('req2params'); req.profiler.done('req2params');
next(null, req); next(null);
}); });
}; };
}; };

View File

@ -31,10 +31,6 @@ module.exports = function errorMiddleware (/* options */) {
errors_with_context: allErrors.map(errorMessageWithContext) errors_with_context: allErrors.map(errorMessageWithContext)
}; };
if (res.locals && res.locals.dbhost) {
res.set('X-Served-By-DB-Host', res.locals.dbhost);
}
res.status(statusCode); res.status(statusCode);
if (req.query && req.query.callback) { if (req.query && req.query.callback) {

View File

@ -68,7 +68,7 @@ describe('prepare-context', function() {
it('sets dbname from redis metadata', function(done){ it('sets dbname from redis metadata', function(done){
var req = {headers: { host:'localhost' }, query: {}, locals: {} }; var req = {headers: { host:'localhost' }, query: {}, locals: {} };
var res = {}; var res = { set: function () {} };
dbConnSetup(prepareRequest(req), res, function(err) { dbConnSetup(prepareRequest(req), res, function(err) {
if ( err ) { done(err); return; } if ( err ) { done(err); return; }
@ -84,7 +84,7 @@ describe('prepare-context', function() {
it('sets also dbuser for authenticated requests', function(done){ it('sets also dbuser for authenticated requests', function(done){
var req = { headers: { host: 'localhost' }, query: { map_key: '1234' }, locals: {} }; var req = { headers: { host: 'localhost' }, query: { map_key: '1234' }, locals: {} };
var res = {}; var res = { set: function () {} };
// FIXME: review authorize-pgconnsetup workflow, It might we are doing authorization twice. // FIXME: review authorize-pgconnsetup workflow, It might we are doing authorization twice.
authorize(prepareRequest(req), res, function (err) { authorize(prepareRequest(req), res, function (err) {
@ -108,7 +108,7 @@ describe('prepare-context', function() {
locals: {} locals: {}
}; };
dbConnSetup(prepareRequest(req), res, function(err, req) { dbConnSetup(prepareRequest(req), res, function () {
// wrong key resets params to no user // wrong key resets params to no user
assert.ok(req.params.dbuser === test_pubuser, 'could inject dbuser ('+req.params.dbuser+')'); assert.ok(req.params.dbuser === test_pubuser, 'could inject dbuser ('+req.params.dbuser+')');
done(); done();