mirror of
https://github.com/NovaOSS/nova-api.git
synced 2024-11-25 16:13:58 +01:00
24 lines
820 B
Python
24 lines
820 B
Python
from fastapi import FastAPI
|
|
from fastapi.responses import PlainTextResponse
|
|
from fastapi.requests import Request
|
|
from fastapi.responses import Response
|
|
from slowapi import Limiter, _rate_limit_exceeded_handler
|
|
from slowapi.util import get_remote_address
|
|
from slowapi.errors import RateLimitExceeded
|
|
|
|
limiter = Limiter(key_func=lambda: "test", default_limits=["5/minute"])
|
|
app = FastAPI()
|
|
app.state.limiter = limiter
|
|
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
|
|
|
|
# Note: the route decorator must be above the limit decorator, not below it
|
|
@app.get("/home")
|
|
@limiter.limit("5/minute")
|
|
async def homepage(request: Request):
|
|
return PlainTextResponse("test")
|
|
|
|
@app.get("/mars")
|
|
@limiter.limit("5/minute")
|
|
async def homepage(request: Request, response: Response):
|
|
return {"key": "value"}
|