mirror of
https://github.com/laurentS/slowapi.git
synced 2026-03-13 09:10:20 +08:00
Link to docs from README
This commit is contained in:
43
README.md
43
README.md
@@ -4,6 +4,8 @@ A rate limiting library for Starlette and FastAPI adapted from [flask-limiter](h
|
||||
|
||||
Note: this is alpha quality code still, the API may change, and things may fall apart while you try it.
|
||||
|
||||
The documentation is on [read the docs](https://slowapi.readthedocs.io/en/latest/).
|
||||
|
||||
# Quick start
|
||||
|
||||
## Installation
|
||||
@@ -14,47 +16,6 @@ Note: this is alpha quality code still, the API may change, and things may fall
|
||||
$ pip install slowapi
|
||||
```
|
||||
|
||||
## Starlette
|
||||
|
||||
```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)
|
||||
app = Starlette()
|
||||
app.state.limiter = limiter
|
||||
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
||||
|
||||
@limiter.limit("5/minute")
|
||||
async def homepage(request: Request):
|
||||
return PlainTextResponse("test")
|
||||
|
||||
app.add_route("/home", homepage)
|
||||
```
|
||||
|
||||
The above app will have a route `t1` that will accept up to 5 requests per minute. Requests beyond this limit will be answered with an HTTP 429 error, and the body of the view will not run.
|
||||
|
||||
## FastAPI
|
||||
|
||||
```python
|
||||
from fastapi import FastAPI
|
||||
from slowapi import Limiter, _rate_limit_exceeded_handler
|
||||
from slowapi.util import get_remote_address
|
||||
|
||||
limiter = Limiter(key_func=get_remote_address)
|
||||
app = FastAPI()
|
||||
app.state.limiter = limiter
|
||||
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
||||
|
||||
@app.get("/home")
|
||||
@limiter.limit("5/minute")
|
||||
async def homepage(request: Request):
|
||||
return PlainTextResponse("test")
|
||||
```
|
||||
|
||||
This will provide the same result, but with a FastAPI app.
|
||||
|
||||
# Features
|
||||
|
||||
Most feature are coming from (will come from) FlaskLimiter and the underlying [limits](https://limits.readthedocs.io/).
|
||||
|
||||
Reference in New Issue
Block a user