2017-09-25 19:40:22 +08:00
|
|
|
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:27:49 +08:00
|
|
|
req.profiler.done('setDBConn');
|
|
|
|
|
2017-09-25 19:40:22 +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
|
|
|
|
2017-10-05 23:28:41 +08:00
|
|
|
return next(err);
|
2017-09-25 19:40:22 +08:00
|
|
|
}
|
|
|
|
|
2017-09-29 17:07:11 +08:00
|
|
|
_.defaults(res.locals, {
|
2017-09-25 19:40:22 +08:00
|
|
|
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
|
|
|
|
2017-10-09 18:27:58 +08:00
|
|
|
res.set('X-Served-By-DB-Host', res.locals.dbhost);
|
2017-10-06 00:05:46 +08:00
|
|
|
|
2018-03-01 02:29:10 +08:00
|
|
|
next();
|
2017-09-25 19:40:22 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|