gatekeeper icon indicating copy to clipboard operation
gatekeeper copied to clipboard

Combining GatekeeperMiddleware to specific routes and to all routes

Open haraGADygyl opened this issue 2 years ago • 1 comments

Is it possible to have both? My idea is to rate limit all routes to 10 requests per second, except the /register and /login routes where I want a rate limit of 5 requests per minute?

haraGADygyl avatar Oct 03 '23 05:10 haraGADygyl

A strange behavior I observe:

In configure.swift I have:

app.caches.use(.memory)
app.gatekeeper.config = .init(maxRequests: 40, per: .minute)
app.middleware.use(GatekeeperMiddleware())

And then:

struct MyController: RouteCollection {
    func boot(routes: RoutesBuilder) throws {
        routes.group("api") { api in
            
            api.group(GatekeeperMiddleware(config: GatekeeperConfig(maxRequests: 10, per: .minute))) { limited in
                limited.post("register", use: register)
                limited.post("login", use: login)
            }
           another.group()
            ...
}

With this configuration the /register and /login routes get rate limited after 20 requests (half of what the 'maxRequests' value is in configure.swift . If I change the 'maxRequests' to 30 in configure.swift, then the /register and /login routes are rate limited after 15 requests.

Is this a bug or a feature? Can I somehow enforce the 'maxRequests' value in the api.group()? Can I use more than one GatekeeperMiddleware(config:) instance for the different route groups?

haraGADygyl avatar Oct 03 '23 05:10 haraGADygyl