A forked version of the Grape::Attack middleware focused exclusively on global throttling.
It's Rack::Attack for Grape. That's great! But there was a need for global throttling as the default. Like its ancestor, this fork is meant to be dead simple with minimal configuration.
There's a DSL to throttle your Grape API endpoints that works a lot like Wine Bouncer.
Read up on Grape::Attack's installation and default usage.
Add this line to your application's Gemfile:
gem "grape-attack", github: "nschneble/grape-attack"
Then execute:
$ bundle install
Mount the middleware in your API:
class MyApi < Grape::API
use Grape::Attack::Throttle
end
Define global throttling in an initializer (defaults below):
# config/initializers/grape_attack.rb
Grape::Attack.configure do |config|
config.throttle_limit = 600
config.throttle_interval = 1.hour
end
Then enable for your Grape endpoints using the throttle
DSL:
class MyApi < Grape::API
use Grape::Attack::Throttle
resources :contacts do
throttle
get do
Contact.all
end
end
end
That's it!
Refer to Grape::Attack for information on how exceptions are raised, expected HTTP responses, and more.
The gem is available as open source under the terms of the MIT License.