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

33 lines
1.0 KiB
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);
}
// Add default database connection parameters
// if none given
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', req.params.dbhost);
req.profiler.done('req2params');
next(null);
});
};
};