Swift
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.circleci
Sources
Tests
.codebeatignore
.codecov.yml
.gitignore
.swiftlint.yml
LICENSE
Package.swift
README.md

README.md

Gatekeeper 👮

Swift Version Vapor Version Circle CI codebeat badge codecov Readme Score GitHub license

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 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

Please take into consideration that multiple clients can be using the same IP address. eg. public wifi

📦 Installation

Update your Package.swift file.

.Package(url: "https://github.com/nodes-vapor/gatekeeper", majorVersion: 0)

Getting started 🚀

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.

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

import Gatekeeper
public func setup() throws {
    // ...

    addConfigurable(middleware: Gatekeeper(rate: Rate(10, per: .minute)), name: "gatekeeper")
}

Config/droplet.json

Add gatekeeper to the middleware array

"middleware": [
    "error",
    "date",
    "file",
    "gatekeeper"
]

Adding Middleware to a Route Group

let gatekeeper = Gatekeeper(rate: Rate(10, per: .minute))

drop.group(gatekeeper) { group in
   // Routes
}

The Rate.Interval enumeration

The currently implemented intervals are:

case .second
case .minute
case .hour
case .day

Credits 🏆

This package is developed and maintained by the Vapor team at Nodes. The package owner for this project is Tom.

License 📄

This package is open-sourced software licensed under the MIT license