diff --git a/README.md b/README.md index 43040f1..8101e5d 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,10 @@ [![Readme Score](http://readme-score-api.herokuapp.com/score.svg?url=https://github.com/nodes-vapor/gatekeeper)](http://clayallsopp.github.io/readme-score?url=https://github.com/nodes-vapor/gatekeeper) [![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](https://raw.githubusercontent.com/nodes-vapor/gatekeeper/master/LICENSE) -Rate Limiter middleware. +GateKeeper is a middleware that restricts the number of requests from clients, based on their IP address. +It works by adding the clients IP address to the cache and count how many requests the clients can make during the GateKeepers 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 + +**Please take into consideration that multiple clients can be using the same IP address. eg. public wifi** ## 📦 Installation @@ -21,12 +24,55 @@ Update your `Package.swift` file. ## Getting started 🚀 -`RateLimiter` has two configurable fields: the maximum rate and the cache to use. If you don't supply your own cache the limiter will create its own, in-memory cache. +`Gatekeeper` has two configurable fields: the maximum rate and the cache to use. If you don't supply your own cache the limiter will create its own, in-memory cache. + +```swift +let gatekeeper = GateKeeper(rate: Rate(10, per: .minute)) +``` + +### Adding middleware +You can add the middleware either globally or to a route group. +#### Adding Middleware Globally + +#### `Sources/App/Config+Setup.swift` ```swift -let limiter = RateLimiter(rate: Rate(10, per: .minute)) +import Gatekeeper +``` + +```swift +public func setup() throws { + // ... + + addConfigurable(middleware: GateKeeper(rate: Rate(10, per: .minute)), name: "gatekeeper") +} ``` +#### `Config/droplet.json` + +Add `gatekeeper` to the middleware array + +```json +"middleware": [ + "error", + "date", + "file", + "gatekeeper" +] +``` + + +#### Adding Middleware to a Route Group + +```Swift +let gatekeeper = Gatekeeper(rate: Rate(10, per: .minute)) + +drop.group(gatekeeper) { group in + // Routes +} +``` + + ### The `Rate.Interval` enumeration The currently implemented intervals are: