This repository has been archived by the owner on Jul 18, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
50 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
module Robinet | ||
class Limiter | ||
|
||
def initialize(app) | ||
@app = app | ||
end | ||
|
||
def execute(request) | ||
raise Errors::RateLimitExceeded | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Robinet | ||
class Middleware | ||
def initialize(app) | ||
@app = app | ||
@limiter = Robinet::Limiter.new(@app) | ||
end | ||
|
||
def call(env) | ||
request = Rack::Request.new(env) | ||
begin | ||
check_limits(request) | ||
@app.call(env) | ||
rescue Errors::RateLimitExceeded => e | ||
e.http_response | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_limits(request) | ||
@limiter.execute(request) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module Robinet | ||
class Railtie < Rails::Railtie | ||
initializer 'robinet.add_middleware' do |application| | ||
if Robinet.config.enabled | ||
Robinet.logger.info('Enabling Robinet Rate Limiter') | ||
application.middleware.use Robinet::Middleware | ||
end | ||
end | ||
end if defined?(Rails) | ||
end |