diff --git a/app/middlewares/rate-limit.js b/app/middlewares/rate-limit.js index 018827f3..3e4d2f47 100644 --- a/app/middlewares/rate-limit.js +++ b/app/middlewares/rate-limit.js @@ -51,10 +51,8 @@ function rateLimit(userLimits, endpointGroup = null) { } function isRateLimitEnabled(endpointGroup) { - return global.settings.ratelimits && - global.settings.ratelimits.rateLimitsEnabled && + return global.settings.ratelimits.rateLimitsEnabled && endpointGroup && - global.settings.ratelimits.endpoints && global.settings.ratelimits.endpoints[endpointGroup]; } diff --git a/app/server.js b/app/server.js index 940f0183..d80fd596 100644 --- a/app/server.js +++ b/app/server.js @@ -137,9 +137,10 @@ function App(statsClient) { var userDatabaseService = new UserDatabaseService(metadataBackend); + UserLimitsService.configure(); const userLimitsServiceOptions = { limits: { - rateLimitsEnabled: (global.settings.ratelimits && global.settings.ratelimits.rateLimitsEnabled) || false + rateLimitsEnabled: global.settings.ratelimits.rateLimitsEnabled } }; const userLimitsService = new UserLimitsService(metadataBackend, userLimitsServiceOptions); diff --git a/app/services/user_limits.js b/app/services/user_limits.js index 837e7e83..4f80c172 100644 --- a/app/services/user_limits.js +++ b/app/services/user_limits.js @@ -1,3 +1,14 @@ +const DEFAULT_RATE_LIMITS_OPTIONS = { + rateLimitsEnabled: false, + endpoints: { + query: false, + query_format: false, + job_create: false, + job_get: false, + job_delete: false + } +}; + /** * UserLimits * @param {cartodb-redis} metadataBackend @@ -11,6 +22,13 @@ class UserLimits { this.preprareRateLimit(); } + static configure() { + // default rate limits + if(!global.settings.ratelimits) { + global.settings.ratelimits = DEFAULT_RATE_LIMITS_OPTIONS; + } + } + preprareRateLimit() { if (this.options.limits.rateLimitsEnabled) { this.metadataBackend.loadRateLimitsScript(); diff --git a/test/acceptance/rate-limit.js b/test/acceptance/rate-limit.js index 34731b41..f8b8f4d2 100644 --- a/test/acceptance/rate-limit.js +++ b/test/acceptance/rate-limit.js @@ -72,10 +72,6 @@ function assertRequest (status, limit, remaining, reset, retry, done = null) { describe('rate limit', function() { before(function() { - if (!global.settings.ratelimits) { - global.settings.ratelimits = {}; - global.settings.ratelimits.endpoints = {}; - } global.settings.ratelimits.rateLimitsEnabled = true; global.settings.ratelimits.endpoints.query = true; @@ -89,10 +85,6 @@ describe('rate limit', function() { }); after(function() { - if (!global.settings.ratelimits) { - global.settings.ratelimits = {}; - global.settings.ratelimits.endpoints = {}; - } global.settings.ratelimits.rateLimitsEnabled = false; global.settings.ratelimits.endpoints.query = false;