mirror of
https://github.com/laurentS/slowapi.git
synced 2026-03-13 09:10:20 +08:00
Updates docs
This commit is contained in:
33
docs/examples.md
Normal file
33
docs/examples.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Examples
|
||||
|
||||
Here are some examples of setup to get you started. Please open an issue if you have a use case that is not included here.
|
||||
|
||||
The tests show a lot of different use cases that are not all covered here.
|
||||
|
||||
## Apply a global (default) limit to all routes
|
||||
|
||||
```python
|
||||
from starlette.applications import Starlette
|
||||
from slowapi import Limiter, _rate_limit_exceeded_handler
|
||||
from slowapi.util import get_remote_address
|
||||
|
||||
limiter = Limiter(key_func=get_remote_address, default_limits=["1/minute"])
|
||||
app = Starlette()
|
||||
app.state.limiter = limiter
|
||||
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
||||
|
||||
# this will be limited by the default_limits
|
||||
async def homepage(request: Request):
|
||||
return PlainTextResponse("Only once per minute")
|
||||
|
||||
app.add_route("/home", homepage)
|
||||
```
|
||||
|
||||
## Exempt a route from the global limit
|
||||
|
||||
```python
|
||||
@app.route("/someroute")
|
||||
@limiter.exempt
|
||||
def t(request: Request):
|
||||
return PlainTextResponse("I'm unlimited")
|
||||
```
|
||||
Reference in New Issue
Block a user