2018-10-23 23:45:42 +08:00
|
|
|
'use strict';
|
|
|
|
|
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) => {
|
2017-09-25 19:40:22 +08:00
|
|
|
if (err) {
|
2019-10-22 01:07:24 +08:00
|
|
|
if (err.message && err.message.indexOf('name not found') !== -1) {
|
2017-09-25 19:40:22 +08:00
|
|
|
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
|
|
|
});
|
|
|
|
};
|
|
|
|
};
|