Skip to content

Commit bf3f285

Browse files
committed
Initial Commit
0 parents  commit bf3f285

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = require('./lib');

lib/index.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
var redis = require('redis');
2+
3+
var internals = {};
4+
5+
internals.defaults = {
6+
global:{limit: -1 , bucketLength: 1},
7+
routes: {},
8+
redis:{}
9+
};
10+
11+
exports.register = function(plugin, options, next) {
12+
var settings = plugin.hapi.utils.applyToDefaults(internals.defaults, options);
13+
var redisClient = redis.createClient(options.redis.port, options.redis.host, options.redis.options);
14+
plugin.ext('request', function(request, callback) {
15+
var ipts = request.info.remoteAddress+':'+Math.floor((new Date()).getTime()/1000)
16+
redisClient.get(ipts,function(err, token){
17+
if(token && token > 1) {
18+
return request.reply(429);
19+
}else{
20+
rediClient.multi([['INCR',ipts],['EXPIRE', 120]).exec(function(err) {
21+
return next();
22+
});
23+
}
24+
});
25+
});
26+
};

package.json

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"name": "cl-hapi-rate",
3+
"version": "0.0.0",
4+
"description": "A rate limiting plugin for HAPI.",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [
10+
"rate-limit",
11+
"rate",
12+
"limit",
13+
"HAPI",
14+
"REST"
15+
],
16+
"author": "Aaron Elligsen",
17+
"license": "BSD-2-Clause"
18+
}

0 commit comments

Comments
 (0)