Updated the rate limit

This commit is contained in:
2026-01-29 13:37:13 +08:00
parent fe23f8424c
commit 1800ba4362
2 changed files with 13 additions and 2 deletions

View File

@@ -6,7 +6,7 @@ load_dotenv()
class Settings: class Settings:
PROJECT_NAME: str = "Storyline AI Gateway" PROJECT_NAME: str = "Storyline AI Gateway"
API_KEY: str = os.getenv("API_KEY", "storyline-secret-key-123") API_KEY: str = os.getenv("API_KEY", "storyline-secret-key-123")
RATE_LIMIT: str = "20/minute" RATE_LIMIT: str = "10/minute"
GOOGLE_API_KEY: str = os.getenv("GOOGLE_API_KEY") GOOGLE_API_KEY: str = os.getenv("GOOGLE_API_KEY")
OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY") OPENAI_API_KEY: str = os.getenv("OPENAI_API_KEY")
DATABASE_URL: str = os.getenv("DATABASE_URL") DATABASE_URL: str = os.getenv("DATABASE_URL")

View File

@@ -42,7 +42,18 @@ def create_application() -> FastAPI:
# Set up Rate Limiter # Set up Rate Limiter
application.state.limiter = limiter application.state.limiter = limiter
application.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)
@application.exception_handler(RateLimitExceeded)
async def custom_rate_limit_exceeded_handler(request, exc):
from fastapi.responses import JSONResponse
return JSONResponse(
status_code=429,
content={
"error": "Too Many Requests",
"message": "Chill out! You've reached the rate limit. Please wait a moment before trying again.",
"limit": str(exc.detail)
}
)
# Include routes # Include routes
application.include_router(api_router, prefix="/api/v1") application.include_router(api_router, prefix="/api/v1")