rails_api_guard 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +14 -7
- data/lib/rails_api_guard/generators/install/install_generator.rb +15 -0
- data/lib/rails_api_guard/generators/install/templates/rails_api_guard_initializer.rb +41 -0
- data/lib/rails_api_guard/version.rb +1 -1
- data/lib/rails_api_guard.rb +5 -2
- metadata +17 -9
- data/lib/rails_api_guard/config_manager.rb +0 -39
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7b4d8d394e924a76d3aa00b2fdb2fdf782747545d8ffc2542506bc854ea6a65
|
4
|
+
data.tar.gz: 9dfa228e433502c43df3e0ed50348af2581c73cfa7376d88e1a491310ca8eac5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc940368929aa36574ff9fac9326eaf2c2d6a104c42081491bf0552857becbfb5ae9dbe51f2fc63f518f5ad771e59f63ac3ed1c0b8f37fdf20c91de17ca06906
|
7
|
+
data.tar.gz: 94144edae5b36b29d9319d152a1d80b95d524f9e66e9b22d8880805baf74f692e6645cf051e352442a4b698698d7056011d9a0263cafaeed502d75e5c3977791
|
data/README.md
CHANGED
@@ -1,19 +1,18 @@
|
|
1
1
|
# RailsApiGuard
|
2
|
-
Short description and motivation.
|
3
2
|
|
4
|
-
|
5
|
-
|
3
|
+
A pluggable, configurable rate limiting middleware for Rails APIs with Redis backend, Slack alerts, and endpoint exclusions.
|
4
|
+
|
5
|
+
## 🚀 Installation
|
6
6
|
|
7
|
-
## Installation
|
8
7
|
Add this line to your application's Gemfile:
|
9
8
|
|
10
9
|
```ruby
|
11
|
-
gem
|
10
|
+
gem 'rails_api_guard'
|
12
11
|
```
|
13
12
|
|
14
13
|
And then execute:
|
15
14
|
```bash
|
16
|
-
$ bundle
|
15
|
+
$ bundle install
|
17
16
|
```
|
18
17
|
|
19
18
|
Or install it yourself as:
|
@@ -21,8 +20,16 @@ Or install it yourself as:
|
|
21
20
|
$ gem install rails_api_guard
|
22
21
|
```
|
23
22
|
|
23
|
+
## Generate the Initializer
|
24
|
+
```bash
|
25
|
+
rails generate rails_api_guard:install
|
26
|
+
```
|
27
|
+
|
28
|
+
## Configuration
|
29
|
+
Edit `config/initializers/rails_api_guard.rb` to set request limits, expiry time, Slack alerts, and excluded endpoint patterns.
|
30
|
+
|
24
31
|
## Contributing
|
25
|
-
Contribution directions
|
32
|
+
Contribution directions are yet to add.
|
26
33
|
|
27
34
|
## License
|
28
35
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require "rails/generators"
|
2
|
+
|
3
|
+
module RailsApiGuard
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
source_root File.expand_path("templates", __dir__)
|
7
|
+
|
8
|
+
desc "Creates a RailsApiGuard initializer file."
|
9
|
+
|
10
|
+
def copy_initializer
|
11
|
+
template "rails_api_guard_initializer.rb", "config/initializers/rails_api_guard.rb"
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# config/initializers/rails_api_guard.rb
|
2
|
+
|
3
|
+
# RailsApiGuard configuration initializer
|
4
|
+
#
|
5
|
+
# This file allows you to customize rate limiting behavior for your Rails API.
|
6
|
+
# You can adjust request limits, expiry time, Slack notifications, and exclusion rules
|
7
|
+
# for specific endpoints from rate limiting.
|
8
|
+
#
|
9
|
+
# You can safely add, remove, or modify these settings as per your application needs.
|
10
|
+
|
11
|
+
RailsApiGuard.configure do |config|
|
12
|
+
# 🚦 Maximum number of allowed requests within the expiry_time window per IP (or per key)
|
13
|
+
config.limit = 5
|
14
|
+
|
15
|
+
# 🕒 Expiry time (in seconds) for the rate limiting window.
|
16
|
+
# After this time window passes, the request count for an IP or key resets.
|
17
|
+
config.expiry_time = 60
|
18
|
+
|
19
|
+
# 🔔 Slack webhook URL for sending alerts when a client exceeds the rate limit.
|
20
|
+
# Best practice: set this securely via environment variables.
|
21
|
+
config.slack_webhook_url = ENV['SLACK_WEBHOOK_URL']
|
22
|
+
|
23
|
+
# 🚫 List of endpoint patterns to be excluded from rate limiting.
|
24
|
+
#
|
25
|
+
# Supports:
|
26
|
+
# - Exact string paths (e.g. '/healthcheck')
|
27
|
+
# - Wildcard patterns (e.g. '/public/*')
|
28
|
+
# - Regular expressions (must start with '^', e.g. '^/user/\d+/document/\w+$')
|
29
|
+
#
|
30
|
+
# 👉 This list is **fully extendable** — feel free to add any additional paths
|
31
|
+
# or dynamic route patterns you want to bypass the rate limiter.
|
32
|
+
#
|
33
|
+
# Example:
|
34
|
+
# config.excluded_patterns << '/open-api/*'
|
35
|
+
#
|
36
|
+
config.excluded_patterns = [
|
37
|
+
'/healthcheck',
|
38
|
+
'/public/*',
|
39
|
+
'^/user/\\d+/document/\\w+$'
|
40
|
+
]
|
41
|
+
end
|
data/lib/rails_api_guard.rb
CHANGED
@@ -3,14 +3,17 @@ require "rails_api_guard/engine"
|
|
3
3
|
require "rails_api_guard/middleware/rate_limiter"
|
4
4
|
require "rails_api_guard/config"
|
5
5
|
require "redis"
|
6
|
+
require "rails_api_guard/generators/install/install_generator"
|
7
|
+
|
6
8
|
|
7
9
|
module RailsApiGuard
|
8
10
|
class << self
|
9
11
|
attr_accessor :config
|
10
12
|
end
|
11
13
|
|
14
|
+
self.config ||= Config.new
|
15
|
+
|
12
16
|
def self.configure
|
13
|
-
|
14
|
-
yield(config)
|
17
|
+
yield(config) if block_given?
|
15
18
|
end
|
16
19
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails_api_guard
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- sugat dhole
|
@@ -14,28 +14,34 @@ dependencies:
|
|
14
14
|
name: rails
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- - "
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
- - "<"
|
18
21
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
22
|
+
version: '8.0'
|
20
23
|
type: :runtime
|
21
24
|
prerelease: false
|
22
25
|
version_requirements: !ruby/object:Gem::Requirement
|
23
26
|
requirements:
|
24
|
-
- - "
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '6.0'
|
30
|
+
- - "<"
|
25
31
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
32
|
+
version: '8.0'
|
27
33
|
- !ruby/object:Gem::Dependency
|
28
34
|
name: redis
|
29
35
|
requirement: !ruby/object:Gem::Requirement
|
30
36
|
requirements:
|
31
|
-
- - "
|
37
|
+
- - ">="
|
32
38
|
- !ruby/object:Gem::Version
|
33
39
|
version: '5.0'
|
34
40
|
type: :runtime
|
35
41
|
prerelease: false
|
36
42
|
version_requirements: !ruby/object:Gem::Requirement
|
37
43
|
requirements:
|
38
|
-
- - "
|
44
|
+
- - ">="
|
39
45
|
- !ruby/object:Gem::Version
|
40
46
|
version: '5.0'
|
41
47
|
- !ruby/object:Gem::Dependency
|
@@ -74,8 +80,9 @@ files:
|
|
74
80
|
- config/routes.rb
|
75
81
|
- lib/rails_api_guard.rb
|
76
82
|
- lib/rails_api_guard/config.rb
|
77
|
-
- lib/rails_api_guard/config_manager.rb
|
78
83
|
- lib/rails_api_guard/engine.rb
|
84
|
+
- lib/rails_api_guard/generators/install/install_generator.rb
|
85
|
+
- lib/rails_api_guard/generators/install/templates/rails_api_guard_initializer.rb
|
79
86
|
- lib/rails_api_guard/middleware/rate_limiter.rb
|
80
87
|
- lib/rails_api_guard/services/exclusion_checker.rb
|
81
88
|
- lib/rails_api_guard/services/rate_limit_store.rb
|
@@ -89,6 +96,7 @@ licenses:
|
|
89
96
|
metadata:
|
90
97
|
homepage_uri: https://github.com/sudo0809/rails_api_guard
|
91
98
|
source_code_uri: https://github.com/sudo0809/rails_api_guard
|
99
|
+
changelog_uri: https://github.com/sudo0809/rails_api_guard/blob/main/CHANGELOG.md
|
92
100
|
post_install_message:
|
93
101
|
rdoc_options: []
|
94
102
|
require_paths:
|
@@ -97,7 +105,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
97
105
|
requirements:
|
98
106
|
- - ">="
|
99
107
|
- !ruby/object:Gem::Version
|
100
|
-
version: '0'
|
108
|
+
version: '3.0'
|
101
109
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
110
|
requirements:
|
103
111
|
- - ">="
|
@@ -1,39 +0,0 @@
|
|
1
|
-
require "yaml"
|
2
|
-
|
3
|
-
module RailsApiGuard
|
4
|
-
class ConfigManager
|
5
|
-
def self.excluded_patterns
|
6
|
-
config["excluded_patterns"] || []
|
7
|
-
end
|
8
|
-
|
9
|
-
def self.limit
|
10
|
-
config["limit"] || 5
|
11
|
-
end
|
12
|
-
|
13
|
-
def self.expiry_time
|
14
|
-
config["expiry_time"] || 60
|
15
|
-
end
|
16
|
-
|
17
|
-
def self.slack_webhook_url
|
18
|
-
config["slack_webhook_url"]
|
19
|
-
end
|
20
|
-
|
21
|
-
def self.config
|
22
|
-
@config ||= load_config
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.load_config
|
26
|
-
config_file = Rails.root.join("config/rails_api_guard.yml")
|
27
|
-
unless File.exist?(config_file)
|
28
|
-
Rails.logger.warn "⚠️ Rails API Guard config file missing at #{config_file}"
|
29
|
-
return {}
|
30
|
-
end
|
31
|
-
|
32
|
-
YAML.load_file(config_file) || {}
|
33
|
-
end
|
34
|
-
|
35
|
-
def self.reload!
|
36
|
-
@config = load_config
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|