3 var dolph = require('dolph');
4 var redis = require('gitter-web-utils/lib/redis');
5 var redisClient = redis.getClient();
7 var env = require('gitter-web-env');
8 var config = env.config;
10 var rateLimiter = dolph({
12 limit: config.get('web:apiRateLimit') || 100,
14 applyLimit: function(req) {
15 if (req.user) return true;
16 if (req.authInfo && req.authInfo.accessToken) return true;
19 keyFunction: function(req) {
21 if (req.authInfo && req.authInfo.client) {
22 return req.user.id + ':' + req.authInfo.client.id;
28 // Anonymous access tokens
29 if (req.authInfo && req.authInfo.accessToken) {
30 return req.authInfo.accessToken;
33 /* Should never get here */
36 redisClient: redisClient
39 // Hacky workaround for avatar rate limit issues in our dev environments
40 // until we come up with a better solution
41 var rateLimiterMiddleware;
42 if (process.env.NODE_ENV === 'dev') {
43 rateLimiterMiddleware = function(req, res, next) {
44 if (req.originalUrl.indexOf('/api/private/avatars') === 0) {
48 return rateLimiter(req, res, next);
51 rateLimiterMiddleware = rateLimiter;
54 module.exports = rateLimiterMiddleware;