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 (req, res, next) {
const user = req.context.user;
2017-09-28 17:43:12 +08:00
res.locals.db = {}
pgConnection.setDBConn(user, res.locals.db, (err) => {
if (err) {
if (err.message && -1 !== err.message.indexOf('name not found')) {
err.http_status = 404;
}
req.profiler.done('req2params');
return next(err, req);
}
// Add default database connection parameters
// if none given
2017-09-28 17:43:12 +08:00
_.defaults(res.locals.db, {
dbuser: global.environment.postgres.user,
dbpassword: global.environment.postgres.password,
dbhost: global.environment.postgres.host,
dbport: global.environment.postgres.port
});
2017-09-28 17:43:12 +08:00
req.profiler.done('req2params');
next(null, req);
});
};
};