Files
sqlalchemy-fastapi-pydentic…/server/backend/rate_limit.py
2025-10-05 15:15:32 +03:00

15 lines
560 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from slowapi import Limiter
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded
from fastapi.responses import JSONResponse
from fastapi import Request
# создаём limiter с глобальным лимитом
limiter = Limiter(key_func=get_remote_address, default_limits=["10/minute"])
# обработчик ошибок
async def ratelimit_handler(request: Request, exc: RateLimitExceeded):
return JSONResponse(
status_code=429,
content={"detail": "Too many requests, try again later."},
)