fix(api): changed rate limit library, now play well with testing
This commit is contained in:
		@@ -1,4 +1,7 @@
 | 
			
		||||
from fastapi import APIRouter
 | 
			
		||||
from starlette.requests import Request
 | 
			
		||||
 | 
			
		||||
from src.neo_neo_todo.utils.rate_limit import limiter
 | 
			
		||||
 | 
			
		||||
router = APIRouter(
 | 
			
		||||
    prefix="/control",
 | 
			
		||||
@@ -7,5 +10,6 @@ router = APIRouter(
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@router.get("/ping")
 | 
			
		||||
async def ping():
 | 
			
		||||
@limiter.exempt
 | 
			
		||||
async def ping(request: Request):
 | 
			
		||||
    return {"ping": "pong"}
 | 
			
		||||
 
 | 
			
		||||
@@ -1,20 +1,14 @@
 | 
			
		||||
from contextlib import asynccontextmanager
 | 
			
		||||
 | 
			
		||||
import redis.asyncio as redis
 | 
			
		||||
from fastapi import FastAPI
 | 
			
		||||
from fastapi_limiter import FastAPILimiter
 | 
			
		||||
from slowapi import _rate_limit_exceeded_handler
 | 
			
		||||
from slowapi.errors import RateLimitExceeded
 | 
			
		||||
from slowapi.middleware import SlowAPIMiddleware
 | 
			
		||||
 | 
			
		||||
from src.neo_neo_todo.control import control
 | 
			
		||||
from src.neo_neo_todo.utils.rate_limit import limiter
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@asynccontextmanager
 | 
			
		||||
async def lifespan(_: FastAPI):
 | 
			
		||||
    redis_connection = redis.from_url("redis://localhost:6379", encoding="utf8")
 | 
			
		||||
    await FastAPILimiter.init(redis_connection)
 | 
			
		||||
    yield
 | 
			
		||||
    await FastAPILimiter.close()
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
app = FastAPI(lifespan=lifespan)
 | 
			
		||||
 | 
			
		||||
app = FastAPI()
 | 
			
		||||
app.include_router(control.router)
 | 
			
		||||
 | 
			
		||||
app.state.limiter = limiter
 | 
			
		||||
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)  # type: ignore
 | 
			
		||||
app.add_middleware(SlowAPIMiddleware)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										8
									
								
								backend/src/neo_neo_todo/utils/rate_limit.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								backend/src/neo_neo_todo/utils/rate_limit.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,8 @@
 | 
			
		||||
from slowapi import Limiter
 | 
			
		||||
from slowapi.util import get_remote_address
 | 
			
		||||
 | 
			
		||||
limiter = Limiter(
 | 
			
		||||
    key_func=get_remote_address,
 | 
			
		||||
    default_limits=["70/minute"],
 | 
			
		||||
    storage_uri="redis://localhost:6379/1",
 | 
			
		||||
)
 | 
			
		||||
		Reference in New Issue
	
	Block a user