summaryrefslogtreecommitdiff
path: root/config/initializers/rack_attack.rb
blob: a4631e88f8485e00c56d40c5db0d1f96a3e595b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
Rails.application.config.middleware.use Rack::Attack
# Always allow requests from localhost
# (blacklist & throttles are skipped)
Rack::Attack.safelist('allow from localhost') do |request|
  # Requests are allowed if the return value is truthy
  '127.0.0.1' == request.ip
end

# Throttle requests to 5 requests per second per ip
Rack::Attack.throttle('req/ip', limit: 5, period: 1.second) do |request|
  # If the return value is truthy, the cache key for the return value
  # is incremented and compared with the limit. In this case:
  #   "rack::attack:#{Time.now.to_i/1.second}:req/ip:#{req.ip}"
  #
  # If falsy, the cache key is neither incremented nor checked.
  request.ip
end