index: configurable ratelimiter options
This commit is contained in:
parent
299344cf87
commit
b9c909ad40
11
lib/index.js
11
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
|
||||
|
||||
@ -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 = {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user