|
7 | 7 | from contextlib import asynccontextmanager |
8 | 8 |
|
9 | 9 | import socketio |
10 | | -from fastapi import FastAPI |
| 10 | +from fastapi import FastAPI, HTTPException |
11 | 11 | from fastapi.middleware.cors import CORSMiddleware |
| 12 | +from fastapi.responses import JSONResponse |
12 | 13 |
|
13 | 14 | from app.config import get_settings |
14 | 15 | from app.exceptions import AppException, app_exception_handler, generic_exception_handler |
15 | 16 | from app.middleware import RequestLoggingMiddleware |
| 17 | +from app.rate_limiter import RateLimitMiddleware, get_training_limiter |
16 | 18 | from app.routers import data_process, data_upload, deep_learning, project |
17 | 19 | from app.shared.logging_config import get_logger |
18 | 20 | from app.socketio_instance import sio |
@@ -45,7 +47,18 @@ async def lifespan(app: FastAPI): |
45 | 47 | ) |
46 | 48 | app.add_middleware(RequestLoggingMiddleware) |
47 | 49 |
|
| 50 | +if settings.rate_limit_enabled: |
| 51 | + training_limiter = get_training_limiter() |
| 52 | + app.add_middleware(RateLimitMiddleware, limiter=training_limiter, paths=["/api/v1/deep_learning/train"]) |
| 53 | + |
48 | 54 | app.add_exception_handler(AppException, app_exception_handler) |
| 55 | +app.add_exception_handler( |
| 56 | + HTTPException, |
| 57 | + lambda request, exc: JSONResponse( |
| 58 | + status_code=exc.status_code, |
| 59 | + content={"success": False, "message": exc.detail, "data": None}, |
| 60 | + ), |
| 61 | +) |
49 | 62 | app.add_exception_handler(Exception, generic_exception_handler) |
50 | 63 |
|
51 | 64 | app.include_router(data_upload.router, prefix=settings.api_base) |
|
0 commit comments