Skip to content

Commit

Permalink
✨ Feat: added in-memory rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
Mr-Sunglasses committed Dec 28, 2023
1 parent 484f070 commit 34f9ea8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
94 changes: 93 additions & 1 deletion pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies = [
"fastapi[all]>=0.104.1",
"sqlalchemy>=2.0.23",
"jinja2>=3.1.2",
"slowapi>=0.1.8",
]
requires-python = ">=3.10"
readme = "README.md"
Expand Down
12 changes: 10 additions & 2 deletions src/paste/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
from fastapi import FastAPI
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
from slowapi.errors import RateLimitExceeded
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from .utils import generate_uuid

limiter = Limiter(key_func=get_remote_address)
app = FastAPI(title="paste.py 🐍")
app.state.limiter = limiter
app.add_exception_handler(RateLimitExceeded, _rate_limit_exceeded_handler)

origins = ["*"]

Expand All @@ -28,7 +34,8 @@


@app.post("/file")
async def post_as_a_file(file: UploadFile = File(...)):
@limiter.limit("100/minute")
async def post_as_a_file(request: Request, file: UploadFile = File(...)):
try:
uuid = generate_uuid()
if uuid in large_uuid_storage:
Expand Down Expand Up @@ -91,7 +98,8 @@ async def web(request: Request):


@app.post("/web", response_class=PlainTextResponse)
async def web_post(content: str = Form(...)):
@limiter.limit("100/minute")
async def web_post(request: Request, content: str = Form(...)):
try:
file_content = content.encode()
uuid = generate_uuid()
Expand Down

0 comments on commit 34f9ea8

Please sign in to comment.