Episodes

Resources

Summary

# Gemfile
gem 'rack-attack'

# config/application.rb
module Template
  class Application < Rails::Application
    ...
    config.middleware.use Rack::Attack
  end
end

# config/initializers/rack_attack.rb
class Rack::Attack
  Rack::Attack.cache.store = ActiveSupport::Cache::MemoryStore.new 

  throttle('api/ip', limit: 3, period: 10) do |req|
    req.ip if req.subdomain == 'api'
  end

  class Request < ::Rack::Request
    def subdomain
      host.split('.').first
    end
  end
end