Swiftpack.co - Package - attheodo/Kitura-RateLimit

Kitura-RateLimit

macOS Linux license Language

Logo

Kitura middleware for basic request rate-limiting. Useful for limiting repeated/automated requests to public APIs and mitigating brute-force and scraping attempts.

Installation

Add KituraRateLimit as a dependency in your project's Package.swift file and import KituraRateLimit wherever appropriate in your project files.

Custom Key-Value Stores

Currently KituraRateLimit uses a thread-safe in-memory key-value store on top of Kitura-Cache. Thus, the hits per ip dataset cannot be shared by other servers and processes. In case you need that, I recommend writing a custom keystore conforming to RateLimitKeyStore using kitura-redis and the Redis key-value database.

Usage

Check /Example application.

import Kitura
import KituraRateLimit

let router = Router()

// Apply rate limiting with default configuration in "/" path
router.get("/", allowPartialMatch: false, middleware: RateLimit(config: .defaultConfig, keyStore: MemoryCacheRateLimitKeyStore()))
router.get("/") {
    request, response, next in

    try response.status(.OK).send("Landing Page").end()

}

// Custom rate limit configuration for "/hello"
extension RateLimitConfig {
    static var helloRouteConfig: RateLimitConfig {
        return RateLimitConfig(window: 100, maxRequests: 10, includeHeaders: true)
    }
}

// Apply rate limiting with custom configuratioon in "/hello"
router.get("/hello", allowPartialMatch: false, middleware: RateLimit(config: .helloRouteConfig, keyStore: MemoryCacheRateLimitKeyStore()))
router.get("/hello") {
    request, response, next in

    try response.status(.OK).send("Hello there!").end()

}

Kitura.addHTTPServer(onPort: 3000, with: router)
Kitura.run()

Contributors

  • attheodo, at@atworks.gr

License

kitura-RateLimit is available under the MIT license. See the LICENSE file for more info.

Changelog

  • v0.1.0, September 2016
    • Initial release

Github

link
Stars: 0
Help us keep the lights on

Dependencies

Used By

Total: 0