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

31 lines
911 B
JavaScript
Raw Normal View History

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) {
if (err.message && -1 !== err.message.indexOf('name not found')) {
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();
});
};
};