Swiftpack.co - Package - nodes-vapor/gatekeeper

Gatekeeper 👮

Swift Version Vapor Version Linux Build Status macOS Build Status 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

Github

link
Stars: 7
Help us keep the lights on

Dependencies

Used By

Total: 0

Releases

2.0.0 - Oct 24, 2017

Changed

  • Removed the SSL enforcing functionality to simplify the responsibility of this package (to deal with rate limiting)

1.0.0 - May 30, 2017

  • Vapor 2 released 🎉

0.1.0 - Jan 27, 2017

New:

  • Rate limiter
  • SSLEnforcer