Skip to content

Commit dd7d90d

Browse files
committed
feat: wire in rate limiter with HTTPException handler and env var disable
1 parent 4a5852c commit dd7d90d

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

tensormap-backend/app/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Settings(BaseSettings):
1515
max_content_length: int = 200 * 1024 * 1024
1616
api_base: str = "/api/v1"
1717
debug: bool = True
18+
rate_limit_enabled: bool = True
1819

1920
model_config = {"env_file": ".env"}
2021

tensormap-backend/app/main.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
from contextlib import asynccontextmanager
88

99
import socketio
10-
from fastapi import FastAPI
10+
from fastapi import FastAPI, HTTPException
1111
from fastapi.middleware.cors import CORSMiddleware
12+
from fastapi.responses import JSONResponse
1213

1314
from app.config import get_settings
1415
from app.exceptions import AppException, app_exception_handler, generic_exception_handler
1516
from app.middleware import RequestLoggingMiddleware
17+
from app.rate_limiter import RateLimitMiddleware, get_training_limiter
1618
from app.routers import data_process, data_upload, deep_learning, project
1719
from app.shared.logging_config import get_logger
1820
from app.socketio_instance import sio
@@ -45,7 +47,18 @@ async def lifespan(app: FastAPI):
4547
)
4648
app.add_middleware(RequestLoggingMiddleware)
4749

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+
4854
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+
)
4962
app.add_exception_handler(Exception, generic_exception_handler)
5063

5164
app.include_router(data_upload.router, prefix=settings.api_base)

0 commit comments

Comments
 (0)