diff --git a/lib/index.js b/lib/index.js index 189ff36..5317ba3 100644 --- a/lib/index.js +++ b/lib/index.js @@ -46,6 +46,8 @@ var InsightAPI = function(options) { this.cacheShortSeconds = options.cacheShortSeconds; this.cacheLongSeconds = options.cacheLongSeconds; + this.rateLimiterOptions = options.rateLimiterOptions; + this.blockSummaryCacheSize = options.blockSummaryCacheSize || BlockController.DEFAULT_BLOCKSUMMARY_CACHE_SIZE; this.blockCacheSize = options.blockCacheSize || BlockController.DEFAULT_BLOCK_CACHE_SIZE; @@ -116,12 +118,19 @@ InsightAPI.prototype.getRemoteAddress = function(req) { return req.socket.remoteAddress; }; +InsightAPI.prototype._getRateLimiter = function() { + var rateLimiterOptions = _.isUndefined(this.rateLimiterOptions) ? {} : _.clone(this.rateLimiterOptions); + rateLimiterOptions.node = this.node; + var limiter = new RateLimiter(rateLimiterOptions); + return limiter; +}; + InsightAPI.prototype.setupRoutes = function(app) { var self = this; //Enable rate limiter - var limiter = new RateLimiter({node: this.node}); + var limiter = this._getRateLimiter(); app.use(limiter.middleware()); //Setup logging diff --git a/test/index.js b/test/index.js index 4975877..26e1a76 100644 --- a/test/index.js +++ b/test/index.js @@ -5,6 +5,31 @@ var sinon = require('sinon'); var InsightAPI = require('../lib/index'); describe('Index', function() { + describe('@constructor', function() { + it('will set rate limiter options', function() { + var options = {}; + var node = {}; + var index = new InsightAPI({ + rateLimiterOptions: options, + node: node + }); + index.rateLimiterOptions.should.equal(options); + }); + }); + describe('#_getRateLimiter', function() { + it('will pass options to rate limiter', function() { + var options = { + whitelist: ['127.0.0.1'] + }; + var node = {}; + var index = new InsightAPI({ + rateLimiterOptions: options, + node: node + }); + var limiter = index._getRateLimiter(); + limiter.whitelist.should.eql(['127.0.0.1']); + }); + }); describe('#cache', function() { it('will set cache control header', function(done) { var node = { diff --git a/test/ratelimeter.js b/test/ratelimiter.js similarity index 100% rename from test/ratelimeter.js rename to test/ratelimiter.js