You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 20, 2024. It is now read-only.
Gatekeeper is a middleware that restricts the number of requests from clients, based on their IP address.
11
-
It works by adding the clients IP address to the cache and count how many requests the clients can make during the Gatekeeper's defined lifespan and give back an HTTP 429(Too Many Requests) if the limit has been reached. The number of requests left will be reset when the defined timespan has been reached.
6
+
Gatekeeper is a middleware that restricts the number of requests from clients, based on their IP address**(can be customized)**.
7
+
It works by adding the clients identifier to the cache and count how many requests the clients can make during the Gatekeeper's defined lifespan and give back an HTTP 429(Too Many Requests) if the limit has been reached. The number of requests left will be reset when the defined timespan has been reached.
12
8
13
9
**Please take into consideration that multiple clients can be using the same IP address. eg. public wifi**
14
10
@@ -18,15 +14,15 @@ It works by adding the clients IP address to the cache and count how many reques
@@ -58,7 +47,7 @@ You can add the `GatekeeperMiddleware` to specific routes or to all.
58
47
**Specific routes**
59
48
in routes.swift:
60
49
```swift
61
-
let protectedRoutes = router.grouped(GatekeeperMiddleware.self)
50
+
let protectedRoutes = router.grouped(GatekeeperMiddleware())
62
51
protectedRoutes.get("protected/hello") { req in
63
52
return"Protected Hello, World!"
64
53
}
@@ -68,15 +57,67 @@ protectedRoutes.get("protected/hello") { req in
68
57
in configure.swift:
69
58
```swift
70
59
// Register middleware
71
-
var middlewares =MiddlewareConfig() // Create _empty_ middleware config
72
-
middlewares.use(GatekeeperMiddleware.self)
73
-
services.register(middlewares)
60
+
app.middlewares.use(GatekeeperMiddleware())
61
+
```
62
+
63
+
#### Customizing config
64
+
By default `GatekeeperMiddleware` uses `app.gatekeeper.config` as its configuration.
65
+
However, you can pass a custom configuration to each `GatekeeperMiddleware` type via the initializer
66
+
`GatekeeperMiddleware(config:)`. This allows you to set configuration on a per-route basis.
67
+
68
+
## Key Makers 🔑
69
+
By default Gatekeeper uses the client's hostname (IP address) to identify them. This can cause issues where multiple clients are connected from the same network. Therefore, you can customize how Gatekeeper should identify the client by using the `GatekeeperKeyMaker` protocol.
70
+
71
+
`GatekeeperHostnameKeyMaker` is used by default.
72
+
73
+
You can configure which key maker Gatekeeper should use in `configure.swift`:
Gatekeeper uses the same cache as configured by `app.caches.use()` from Vapor, by default.
105
+
Therefore it is **important** to set up Vapor's cache if you're using this default behaviour. You can use an in-memory cache for Vapor like so:
106
+
107
+
**configure.swift**:
108
+
```swift
109
+
app.cache.use(.memory)
110
+
```
111
+
112
+
### Custom cache
113
+
You can override which cache to use by creating your own type that conforms to the `Cache` protocol from Vapor. Use `app.gatekeeper.caches.use()` to configure which cache to use.
114
+
115
+
76
116
## Credits 🏆
77
117
78
118
This package is developed and maintained by the Vapor team at [Nodes](https://www.nodesagency.com).
79
119
The package owner for this project is [Christian](https://github.com/cweinberger).
120
+
Special thanks goes to [madsodgaard](https://github.com/madsodgaard) for his work on the Vapor 4 version!
0 commit comments