From a48cfce63d038329a73a8f3b3d25a3f3443a8048 Mon Sep 17 00:00:00 2001 From: Michelle Date: Wed, 13 May 2026 00:01:26 +0200 Subject: [PATCH] try to add rate limiting with FastAPI_Limiter --- compose.dev.yml | 10 +++++++++- compose.yml | 10 +++++++++- main.py | 12 ++++++++++-- requirements.txt | 4 +++- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/compose.dev.yml b/compose.dev.yml index 4731e4f..6143118 100644 --- a/compose.dev.yml +++ b/compose.dev.yml @@ -19,5 +19,13 @@ services: - MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD} volumes: - mariadb_data:/var/lib/mysql + + redis: + image: valkey:9 + restart: unless-stopped + volumes: + - redis_data:/data + volumes: - mariadb_data: \ No newline at end of file + mariadb_data: + redis_data: \ No newline at end of file diff --git a/compose.yml b/compose.yml index 8ff1fed..f91bfaf 100644 --- a/compose.yml +++ b/compose.yml @@ -19,5 +19,13 @@ services: - MARIADB_ROOT_PASSWORD=${DB_ROOT_PASSWORD} volumes: - mariadb_data:/var/lib/mysql + + redis: + image: valkey:9 + restart: unless-stopped + volumes: + - redis_data:/data + volumes: - mariadb_data: \ No newline at end of file + mariadb_data: + redis_data: \ No newline at end of file diff --git a/main.py b/main.py index cd4b3ac..979142e 100644 --- a/main.py +++ b/main.py @@ -1,6 +1,10 @@ import aiohttp from fastapi import FastAPI, HTTPException, Request from fastapi.responses import FileResponse +from fastapi_limiter import FastAPILimiter +from fastapi_limiter.depends import RateLimiter +import redis.asyncio as redis +from fastapi import Depends import asyncmy import asyncio import os @@ -39,6 +43,8 @@ async def connect_db(app: FastAPI): maxsize=20 ) await create_tables(app.state.pool) + redis_client = await redis.from_url("redis://localhost") + await FastAPILimiter.init(redis_client) task = asyncio.create_task(fetch_images()) try: yield @@ -50,6 +56,8 @@ async def connect_db(app: FastAPI): pass app.state.pool.close() await app.state.pool.wait_closed() + await FastAPILimiter.close() + await redis_client.close() app = FastAPI(lifespan=connect_db) @@ -71,7 +79,7 @@ async def create_tables(pool): async def root(): return {"message": "yes the api works, maybe i will create a small landing page later here"} -@app.get("/random") +@app.get("/random", dependencies=[Depends(RateLimiter(times=25, seconds=60))]) async def get_random_bnuy(request: Request): async with app.state.pool.acquire() as conn: async with conn.cursor() as cursor: @@ -86,7 +94,7 @@ async def get_random_bnuy(request: Request): else: raise HTTPException(status_code=404, detail="No available images found") -@app.get("/images/{filename}") +@app.get("/images/{filename}", dependencies=[Depends(RateLimiter(times=25, seconds=60))]) async def get_image(filename: str): async with app.state.pool.acquire() as conn: async with conn.cursor() as cursor: diff --git a/requirements.txt b/requirements.txt index 5ef1b0d..506a563 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,5 @@ fastapi[standard] +fastapi_limiter asyncmy -aiohttp \ No newline at end of file +aiohttp +redis \ No newline at end of file