remove redis stuff again because v2 of fastapi_limiter doesn't need it anymore
This commit is contained in:
+1
-9
@@ -8,7 +8,6 @@ services:
|
|||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
depends_on:
|
depends_on:
|
||||||
- mariadb
|
- mariadb
|
||||||
- redis
|
|
||||||
env_file: ".env"
|
env_file: ".env"
|
||||||
|
|
||||||
mariadb:
|
mariadb:
|
||||||
@@ -23,12 +22,5 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- mariadb_data:/var/lib/mysql
|
- mariadb_data:/var/lib/mysql
|
||||||
|
|
||||||
redis:
|
|
||||||
image: valkey/valkey:9
|
|
||||||
restart: unless-stopped
|
|
||||||
volumes:
|
|
||||||
- redis_data:/data
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
mariadb_data:
|
mariadb_data:
|
||||||
redis_data:
|
|
||||||
+1
-9
@@ -8,7 +8,6 @@ services:
|
|||||||
- "8000:8000"
|
- "8000:8000"
|
||||||
depends_on:
|
depends_on:
|
||||||
- mariadb
|
- mariadb
|
||||||
- redis
|
|
||||||
env_file: ".env"
|
env_file: ".env"
|
||||||
|
|
||||||
mariadb:
|
mariadb:
|
||||||
@@ -23,12 +22,5 @@ services:
|
|||||||
volumes:
|
volumes:
|
||||||
- mariadb_data:/var/lib/mysql
|
- mariadb_data:/var/lib/mysql
|
||||||
|
|
||||||
redis:
|
|
||||||
image: valkey/valkey:9
|
|
||||||
restart: unless-stopped
|
|
||||||
volumes:
|
|
||||||
- redis_data:/data
|
|
||||||
|
|
||||||
volumes:
|
volumes:
|
||||||
mariadb_data:
|
mariadb_data:
|
||||||
redis_data:
|
|
||||||
@@ -1,9 +1,9 @@
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
from fastapi import FastAPI, HTTPException, Request
|
from fastapi import FastAPI, HTTPException, Request
|
||||||
from fastapi.responses import FileResponse
|
from fastapi.responses import FileResponse
|
||||||
from fastapi_limiter import FastAPILimiter
|
from fastapi import Depends, FastAPI
|
||||||
|
from pyrate_limiter import Duration, Limiter, Rate
|
||||||
from fastapi_limiter.depends import RateLimiter
|
from fastapi_limiter.depends import RateLimiter
|
||||||
import redis.asyncio as redis
|
|
||||||
from fastapi import Depends
|
from fastapi import Depends
|
||||||
import asyncmy
|
import asyncmy
|
||||||
import asyncio
|
import asyncio
|
||||||
@@ -43,8 +43,6 @@ async def connect_db(app: FastAPI):
|
|||||||
maxsize=20
|
maxsize=20
|
||||||
)
|
)
|
||||||
await create_tables(app.state.pool)
|
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())
|
task = asyncio.create_task(fetch_images())
|
||||||
try:
|
try:
|
||||||
yield
|
yield
|
||||||
@@ -56,10 +54,9 @@ async def connect_db(app: FastAPI):
|
|||||||
pass
|
pass
|
||||||
app.state.pool.close()
|
app.state.pool.close()
|
||||||
await app.state.pool.wait_closed()
|
await app.state.pool.wait_closed()
|
||||||
await FastAPILimiter.close()
|
|
||||||
await redis_client.close()
|
|
||||||
|
|
||||||
app = FastAPI(lifespan=connect_db)
|
app = FastAPI(lifespan=connect_db)
|
||||||
|
limiter = Limiter(Rate(50, Duration.MINUTE))
|
||||||
|
|
||||||
async def create_tables(pool):
|
async def create_tables(pool):
|
||||||
async with pool.acquire() as conn:
|
async with pool.acquire() as conn:
|
||||||
@@ -79,7 +76,7 @@ async def create_tables(pool):
|
|||||||
async def root():
|
async def root():
|
||||||
return {"message": "yes the api works, maybe i will create a small landing page later here"}
|
return {"message": "yes the api works, maybe i will create a small landing page later here"}
|
||||||
|
|
||||||
@app.get("/random", dependencies=[Depends(RateLimiter(times=25, seconds=60))])
|
@app.get("/random", dependencies=[Depends(RateLimiter(limiter=limiter))])
|
||||||
async def get_random_bnuy(request: Request):
|
async def get_random_bnuy(request: Request):
|
||||||
async with app.state.pool.acquire() as conn:
|
async with app.state.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor() as cursor:
|
||||||
@@ -94,7 +91,7 @@ async def get_random_bnuy(request: Request):
|
|||||||
else:
|
else:
|
||||||
raise HTTPException(status_code=404, detail="No available images found")
|
raise HTTPException(status_code=404, detail="No available images found")
|
||||||
|
|
||||||
@app.get("/images/{filename}", dependencies=[Depends(RateLimiter(times=25, seconds=60))])
|
@app.get("/images/{filename}", dependencies=[Depends(RateLimiter(limiter=limiter))])
|
||||||
async def get_image(filename: str):
|
async def get_image(filename: str):
|
||||||
async with app.state.pool.acquire() as conn:
|
async with app.state.pool.acquire() as conn:
|
||||||
async with conn.cursor() as cursor:
|
async with conn.cursor() as cursor:
|
||||||
|
|||||||
+1
-2
@@ -1,5 +1,4 @@
|
|||||||
fastapi[standard]
|
fastapi[standard]
|
||||||
fastapi_limiter
|
fastapi_limiter
|
||||||
asyncmy
|
asyncmy
|
||||||
aiohttp
|
aiohttp
|
||||||
redis
|
|
||||||
Reference in New Issue
Block a user