Windshaft-cartodb/lib/api/middlewares/db-conn-setup.js

33 lines
926 B
JavaScript
Raw Normal View History

'use strict';
const _ = require('underscore');
2018-03-01 02:26:47 +08:00
module.exports = function dbConnSetup (pgConnection) {
2018-03-01 02:29:10 +08:00
return function dbConnSetupMiddleware (req, res, next) {
const { user } = res.locals;
2018-03-01 02:27:49 +08:00
2017-09-29 17:07:11 +08:00
pgConnection.setDBConn(user, res.locals, (err) => {
2018-03-01 02:29:53 +08:00
req.profiler.done('dbConnSetup');
2018-03-01 02:27:49 +08:00
if (err) {
2019-10-22 01:07:24 +08:00
if (err.message && err.message.indexOf('name not found') !== -1) {
err.http_status = 404;
}
2018-03-01 02:27:49 +08:00
return next(err);
}
2017-09-29 17:07:11 +08:00
_.defaults(res.locals, {
dbuser: global.environment.postgres.user,
dbpassword: global.environment.postgres.password,
dbhost: global.environment.postgres.host,
dbport: global.environment.postgres.port
});
2018-03-01 02:26:47 +08:00
res.set('X-Served-By-DB-Host', res.locals.dbhost);
2018-03-01 02:29:10 +08:00
next();
});
};
};