add basic rate limiting

This commit is contained in:
2025-07-01 11:38:38 -04:00
parent 1991e5b31b
commit c3f753a4f0
9 changed files with 64 additions and 20 deletions

View File

@@ -4,7 +4,8 @@ import os
import json
import random
from typing import Optional, Annotated
from fastapi import FastAPI, Request, UploadFile, Response, HTTPException, Form
from fastapi import FastAPI, Request, UploadFile, Response, HTTPException, Form, Depends
from fastapi_throttle import RateLimiter
from fastapi.responses import JSONResponse
import redis.asyncio as redis
from lyric_search.sources import private, cache as LyricsCache, redis_cache
@@ -40,11 +41,16 @@ class Misc(FastAPI):
for endpoint, handler in self.endpoints.items():
app.add_api_route(
f"/{endpoint}", handler, methods=["GET"], include_in_schema=True
f"/{endpoint}",
handler,
methods=["GET"],
include_in_schema=True,
dependencies=[Depends(RateLimiter(times=2, seconds=5))],
)
app.add_api_route(
"/misc/upload_activity_image", self.upload_activity_image, methods=["POST"]
"/misc/upload_activity_image", self.upload_activity_image, methods=["POST"],
dependencies=[Depends(RateLimiter(times=2, seconds=5))],
)
logging.debug("Loading NaaS reasons")
@@ -66,6 +72,7 @@ class Misc(FastAPI):
except Exception as e:
logging.debug("Exception: %s", str(e))
return "No."
async def no(self) -> JSONResponse:
"""NaaS"""