Use req/res logger instead of the one bound to global object

This commit is contained in:
Daniel García Aubert
2020-06-02 12:31:18 +02:00
parent ffe19827fd
commit b60116410a
5 changed files with 34 additions and 32 deletions
+22 -22
View File
@@ -1,29 +1,29 @@
'use strict';
function CdbRequest () {
this.RE_USER_FROM_HOST = new RegExp(global.environment.user_from_host ||
'^([^\\.]+)\\.' // would extract "strk" from "strk.cartodb.com"
);
}
module.exports = CdbRequest;
CdbRequest.prototype.userByReq = function (req) {
var host = req.headers.host || '';
if (req.params.user) {
return req.params.user;
module.exports = class CdbRequest {
constructor ({ logger }) {
this.logger = logger;
// would extract "strk" from "strk.cartodb.com"
this.RE_USER_FROM_HOST = new RegExp(global.environment.user_from_host || '^([^\\.]+)\\.');
}
var mat = host.match(this.RE_USER_FROM_HOST);
userByReq (req) {
const host = req.headers.host || '';
if (!mat) {
return global.logger.error(new Error(`Pattern '${this.RE_USER_FROM_HOST}' does not match hostname '${host}'`));
if (req.params.user) {
return req.params.user;
}
const mat = host.match(this.RE_USER_FROM_HOST);
if (!mat) {
return this.logger.error(new Error(`Pattern '${this.RE_USER_FROM_HOST}' does not match hostname '${host}'`));
}
if (mat.length !== 2) {
return this.logger.error(new Error(`Pattern '${this.RE_USER_FROM_HOST}' gave unexpected matches against '${host}': ${mat}`));
}
return mat[1];
}
if (mat.length !== 2) {
return global.logger.error(new Error(`Pattern '${this.RE_USER_FROM_HOST}' gave unexpected matches against '${host}': ${mat}`));
}
return mat[1];
};