Centralize common headers, this will help up to move biz metrics out of the process

This commit is contained in:
Daniel García Aubert
2020-06-04 17:45:15 +02:00
parent b2da00900f
commit adeffd2018
6 changed files with 162 additions and 34 deletions
+2 -4
View File
@@ -346,11 +346,9 @@ function incrementMapViews ({ metadataBackend }) {
return next();
}
const statTag = mapConfig.obj().stat_tag;
res.locals.mapConfig = mapConfig;
if (statTag) {
res.set('Carto-Stat-Tag', `${statTag}`);
}
const statTag = mapConfig.obj().stat_tag;
metadataBackend.incMapviewCount(user, statTag, (err) => {
if (err) {
+16 -12
View File
@@ -1,5 +1,7 @@
'use strict';
const setCommonHeaders = require('../../utils/common-headers');
module.exports = function errorMiddleware (/* options */) {
return function error (err, req, res, next) {
const { logger } = res.locals;
@@ -7,21 +9,23 @@ module.exports = function errorMiddleware (/* options */) {
logger.error({ error: errors });
const errorResponseBody = {
errors: errors.map(errorMessage),
errors_with_context: errors.map(errorMessageWithContext)
};
setCommonHeaders(req, res, () => {
const errorResponseBody = {
errors: errors.map(errorMessage),
errors_with_context: errors.map(errorMessageWithContext)
};
// If a callback was requested, force status to 200
res.status(req.query.callback ? 200 : findStatusCode(errors[0]));
// If a callback was requested, force status to 200
res.status(req.query.callback ? 200 : findStatusCode(errors[0]));
if (req.query && req.query.callback) {
res.jsonp(errorResponseBody);
} else {
res.json(errorResponseBody);
}
if (req.query && req.query.callback) {
res.jsonp(errorResponseBody);
} else {
res.json(errorResponseBody);
}
return next();
return next();
});
};
};
@@ -5,11 +5,6 @@ module.exports = function incrementMapViewCount (metadataBackend) {
const { mapConfig, user, logger } = res.locals;
const statTag = mapConfig.obj().stat_tag;
if (statTag) {
res.set('Carto-Stat-Tag', `${statTag}`);
}
// Error won't blow up, just be logged.
metadataBackend.incMapviewCount(user, statTag, (err) => {
if (err) {
err.message = `Failed to increment mapview count for user '${user}'. ${err.message}`;
+16 -12
View File
@@ -1,20 +1,24 @@
'use strict';
const setCommonHeaders = require('../../utils/common-headers');
module.exports = function sendResponse () {
return function sendResponseMiddleware (req, res, next) {
res.status(res.statusCode);
setCommonHeaders(req, res, () => {
res.status(res.statusCode);
if (Buffer.isBuffer(res.body)) {
res.send(res.body);
if (Buffer.isBuffer(res.body)) {
res.send(res.body);
return next();
}
if (req.query.callback) {
res.jsonp(res.body);
return next();
}
res.json(res.body);
return next();
}
if (req.query.callback) {
res.jsonp(res.body);
return next();
}
res.json(res.body);
return next();
});
};
};
+1 -1
View File
@@ -18,7 +18,7 @@ module.exports = function user (metadataBackend) {
}
res.locals.userId = userId;
res.set('Carto-User-Id', `${userId}`);
return next();
});
};