Skip to content
This repository has been archived by the owner on Apr 20, 2024. It is now read-only.

Combining GatekeeperMiddleware to specific routes and to all routes #24

Open
haraGADygyl opened this issue Oct 3, 2023 · 1 comment

Comments

@haraGADygyl
Copy link

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
Copy link
Author

haraGADygyl commented Oct 3, 2023

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?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant