new config params and using them in middleware

This commit is contained in:
Simon Martín
2018-02-20 18:18:15 +01:00
parent 7464d827fe
commit 08a5e57180
+19 -8
View File
@@ -5,23 +5,26 @@ const REDIS_DB = 8;
const RATE_LIMIT_ENDPOINTS_GROUPS = {
ENDPOINT_1: 'anonymous',
ENDPOINT_2: 'static',
ENDPOINT_3: 'static-named',
ENDPOINT_3: 'staticNamed',
ENDPOINT_5: 'dataview',
ENDPOINT_6: 'dataview-search',
ENDPOINT_6: 'dataviewSearch',
ENDPOINT_7: 'analysis',
ENDPOINT_8: 'tile',
ENDPOINT_9: 'attributes',
ENDPOINT_10: 'named-list',
ENDPOINT_11: 'named-create',
ENDPOINT_12: 'named-get',
ENDPOINT_10: 'namedList',
ENDPOINT_11: 'namedCreate',
ENDPOINT_12: 'namedGet',
ENDPOINT_13: 'named',
ENDPOINT_14: 'named-update',
ENDPOINT_15: 'named-delete',
ENDPOINT_17: 'named-tiles'
ENDPOINT_14: 'namedUpdate',
ENDPOINT_15: 'namedDelete',
ENDPOINT_17: 'namedTiles'
};
function rateLimitMiddleware(metadataBackend, endpointGroup = null) {
return function rateLimit(req, res, next) {
if (!global.environment.enabledFeatures.rateLimitsEnabled) {
return next();
}
const user = 'cdb';
// const user = res.locals.user;
@@ -29,6 +32,10 @@ function rateLimitMiddleware(metadataBackend, endpointGroup = null) {
endpointGroup = getEndpointGroup();
}
if (!endpointGroup || !isRateLimitEnabledByEndpoint(endpointGroup)) {
return next();
}
const redisParams = [
getLuaScript(),
2,
@@ -117,6 +124,10 @@ function getLuaScript() {
`;
}
function isRateLimitEnabledByEndpoint(endpointGroup) {
return global.environment.enabledFeatures.rateLimitsByEndpoint[endpointGroup] === true;
}
module.exports.rateLimitMiddleware = rateLimitMiddleware;
module.exports.RATE_LIMIT_ENDPOINTS_GROUPS = RATE_LIMIT_ENDPOINTS_GROUPS;
module.exports.getStoreKey = getStoreKey;