Windshaft-cartodb/lib/cartodb/middleware/context/db-conn-setup.js

30 lines
984 B
JavaScript
Raw Normal View History

const _ = require('underscore');
module.exports = function dbConnSetupMiddleware(pgConnection) {
return function dbConnSetup(req, res, next) {
2017-10-03 23:47:57 +08:00
const user = res.locals.user;
2017-09-29 17:07:11 +08:00
pgConnection.setDBConn(user, res.locals, (err) => {
if (err) {
if (err.message && -1 !== err.message.indexOf('name not found')) {
err.http_status = 404;
}
req.profiler.done('req2params');
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
});
res.set('X-Served-By-DB-Host', res.locals.dbhost);
req.profiler.done('req2params');
next(null);
});
};
};