Spaces:
Running
Running
Jiya3177 commited on
Commit Β·
e4249bd
1
Parent(s): aaff8ef
feat: add slowapi rate limiting
Browse files- backend/app/main.py +15 -1
- backend/app/rate_limit.py +24 -0
- backend/app/routes/chat.py +6 -1
- backend/requirements.txt +1 -0
backend/app/main.py
CHANGED
|
@@ -9,11 +9,15 @@ from contextlib import asynccontextmanager
|
|
| 9 |
from fastapi import FastAPI
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from fastapi.staticfiles import StaticFiles
|
| 12 |
-
from fastapi.responses import FileResponse
|
| 13 |
from sqlalchemy import select
|
| 14 |
from sqlalchemy.exc import SQLAlchemyError
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
from app.config import get_settings
|
|
|
|
| 17 |
from app.database import init_db, get_db
|
| 18 |
from app.rag.vectorstore import get_chroma_client
|
| 19 |
|
|
@@ -63,6 +67,16 @@ app = FastAPI(
|
|
| 63 |
lifespan=lifespan,
|
| 64 |
)
|
| 65 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
# ββ CORS (allow frontend dev server) βββββββββββββββββ
|
| 67 |
app.add_middleware(
|
| 68 |
CORSMiddleware,
|
|
|
|
| 9 |
from fastapi import FastAPI
|
| 10 |
from fastapi.middleware.cors import CORSMiddleware
|
| 11 |
from fastapi.staticfiles import StaticFiles
|
| 12 |
+
from fastapi.responses import FileResponse, JSONResponse
|
| 13 |
from sqlalchemy import select
|
| 14 |
from sqlalchemy.exc import SQLAlchemyError
|
| 15 |
|
| 16 |
+
from slowapi.errors import RateLimitExceeded
|
| 17 |
+
from slowapi.middleware import SlowAPIMiddleware
|
| 18 |
+
|
| 19 |
from app.config import get_settings
|
| 20 |
+
from app.rate_limit import limiter
|
| 21 |
from app.database import init_db, get_db
|
| 22 |
from app.rag.vectorstore import get_chroma_client
|
| 23 |
|
|
|
|
| 67 |
lifespan=lifespan,
|
| 68 |
)
|
| 69 |
|
| 70 |
+
app.state.limiter = limiter
|
| 71 |
+
app.add_exception_handler(
|
| 72 |
+
RateLimitExceeded,
|
| 73 |
+
lambda request, exc: JSONResponse(
|
| 74 |
+
status_code=429,
|
| 75 |
+
content={"detail": "Rate limit exceeded. Please try again later."},
|
| 76 |
+
),
|
| 77 |
+
)
|
| 78 |
+
app.add_middleware(SlowAPIMiddleware)
|
| 79 |
+
|
| 80 |
# ββ CORS (allow frontend dev server) βββββββββββββββββ
|
| 81 |
app.add_middleware(
|
| 82 |
CORSMiddleware,
|
backend/app/rate_limit.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
SlowAPI rate limiting configuration.
|
| 3 |
+
"""
|
| 4 |
+
from fastapi import Request
|
| 5 |
+
from slowapi import Limiter
|
| 6 |
+
from slowapi.util import get_remote_address
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def rate_limit_key_func(request: Request) -> str:
|
| 10 |
+
"""Use authenticated user id when available, otherwise fall back to client IP."""
|
| 11 |
+
authorization = request.headers.get("authorization", "")
|
| 12 |
+
if authorization.lower().startswith("bearer "):
|
| 13 |
+
try:
|
| 14 |
+
from app.auth import decode_token
|
| 15 |
+
|
| 16 |
+
user_id = decode_token(authorization.split(" ", 1)[1])
|
| 17 |
+
if user_id:
|
| 18 |
+
return f"user:{user_id}"
|
| 19 |
+
except Exception:
|
| 20 |
+
pass
|
| 21 |
+
return f"ip:{get_remote_address(request)}"
|
| 22 |
+
|
| 23 |
+
|
| 24 |
+
limiter = Limiter(key_func=rate_limit_key_func)
|
backend/app/routes/chat.py
CHANGED
|
@@ -5,7 +5,7 @@ import json
|
|
| 5 |
import logging
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
-
from fastapi import APIRouter, Depends, HTTPException
|
| 9 |
from fastapi.responses import StreamingResponse
|
| 10 |
from sqlalchemy.orm import Session
|
| 11 |
|
|
@@ -14,6 +14,7 @@ from app.models import User, ChatMessage, Document
|
|
| 14 |
from app.schemas import ChatRequest, ChatResponse, ChatMessageResponse, ChatHistoryResponse, SourceChunk
|
| 15 |
from app.auth import get_current_user
|
| 16 |
from app.rag.agent import generate_answer, generate_answer_stream
|
|
|
|
| 17 |
|
| 18 |
logger = logging.getLogger(__name__)
|
| 19 |
|
|
@@ -21,7 +22,9 @@ router = APIRouter(prefix="/chat", tags=["Chat"])
|
|
| 21 |
|
| 22 |
|
| 23 |
@router.post("/ask", response_model=ChatResponse)
|
|
|
|
| 24 |
def ask_question(
|
|
|
|
| 25 |
payload: ChatRequest,
|
| 26 |
user: User = Depends(get_current_user),
|
| 27 |
db: Session = Depends(get_db),
|
|
@@ -88,7 +91,9 @@ def ask_question(
|
|
| 88 |
|
| 89 |
|
| 90 |
@router.post("/ask/stream")
|
|
|
|
| 91 |
def ask_question_stream(
|
|
|
|
| 92 |
payload: ChatRequest,
|
| 93 |
user: User = Depends(get_current_user),
|
| 94 |
db: Session = Depends(get_db),
|
|
|
|
| 5 |
import logging
|
| 6 |
from typing import Optional
|
| 7 |
|
| 8 |
+
from fastapi import APIRouter, Depends, HTTPException, Request
|
| 9 |
from fastapi.responses import StreamingResponse
|
| 10 |
from sqlalchemy.orm import Session
|
| 11 |
|
|
|
|
| 14 |
from app.schemas import ChatRequest, ChatResponse, ChatMessageResponse, ChatHistoryResponse, SourceChunk
|
| 15 |
from app.auth import get_current_user
|
| 16 |
from app.rag.agent import generate_answer, generate_answer_stream
|
| 17 |
+
from app.rate_limit import limiter
|
| 18 |
|
| 19 |
logger = logging.getLogger(__name__)
|
| 20 |
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
@router.post("/ask", response_model=ChatResponse)
|
| 25 |
+
@limiter.limit("10/minute")
|
| 26 |
def ask_question(
|
| 27 |
+
request: Request,
|
| 28 |
payload: ChatRequest,
|
| 29 |
user: User = Depends(get_current_user),
|
| 30 |
db: Session = Depends(get_db),
|
|
|
|
| 91 |
|
| 92 |
|
| 93 |
@router.post("/ask/stream")
|
| 94 |
+
@limiter.limit("10/minute")
|
| 95 |
def ask_question_stream(
|
| 96 |
+
request: Request,
|
| 97 |
payload: ChatRequest,
|
| 98 |
user: User = Depends(get_current_user),
|
| 99 |
db: Session = Depends(get_db),
|
backend/requirements.txt
CHANGED
|
@@ -40,6 +40,7 @@ huggingface-hub
|
|
| 40 |
|
| 41 |
# Production
|
| 42 |
gunicorn
|
|
|
|
| 43 |
|
| 44 |
# File Validation
|
| 45 |
#sudo apt-get install libmagic1 // for Debian/Ubuntu
|
|
|
|
| 40 |
|
| 41 |
# Production
|
| 42 |
gunicorn
|
| 43 |
+
slowapi
|
| 44 |
|
| 45 |
# File Validation
|
| 46 |
#sudo apt-get install libmagic1 // for Debian/Ubuntu
|