impress/config/initializers/attack.rb

22 lines
811 B
Ruby
Raw Permalink Normal View History

2013-07-02 20:50:34 -07:00
Rack::Attack.throttle('pets/ip', limit: 3, period: 30.seconds) do |req|
2013-07-02 14:10:01 -07:00
Rails.logger.debug "Pets hit? #{req.path.inspect} #{req.ip.inspect}"
req.ip if req.path.start_with?('/pets/load')
end
PETS_THROTTLE_MESSAGE = "We've received a lot of pet names from you " +
"recently, so we're giving our servers a break. Try " +
"again in a minute or so. Thanks!"
Rack::Attack.throttled_responder = lambda do |req|
if req.env['rack.attack.matched'] == 'pets/ip'
2013-07-02 14:10:01 -07:00
if req.path.end_with?('.json')
[503, {}, [PETS_THROTTLE_MESSAGE]]
else
flash = req.env['action_dispatch.request.flash_hash']
2013-07-02 14:10:01 -07:00
flash[:warning] = PETS_THROTTLE_MESSAGE
[302, {"Location" => "/"}, [PETS_THROTTLE_MESSAGE]]
end
else
[503, {}, ["Retry later"]]
end
end