Giter VIP home page Giter VIP logo

gatekeeper's Introduction

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

.package(url: "https://github.com/nodes-vapor/gatekeeper.git", from: "3.0.0"),

as well as to your target (e.g. "App"):

targets: [
    .target(name: "App", dependencies: [..., "Gatekeeper", ...]),
// ...
]

Getting started ๐Ÿš€

Configuration

in configure.swift:

import Gatekeeper

// [...]

// Register providers first
try services.register(
    GatekeeperProvider(
        config: GatekeeperConfig(maxRequests: 10, per: .second),
        cacheFactory: { container -> KeyedCache in
            return try container.make()
        }
    )
)

Add to routes

You can add the GatekeeperMiddleware to specific routes or to all.

Specific routes in routes.swift:

let protectedRoutes = router.grouped(GatekeeperMiddleware.self)
protectedRoutes.get("protected/hello") { req in
    return "Protected Hello, World!"
}

For all requests in configure.swift:

// Register middleware
var middlewares = MiddlewareConfig() // Create _empty_ middleware config
middlewares.use(GatekeeperMiddleware.self)
services.register(middlewares)

Credits ๐Ÿ†

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

License ๐Ÿ“„

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.