Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rate limiting added #11

Merged
merged 3 commits into from
Dec 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/paste/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@
from fastapi.templating import Jinja2Templates
from fastapi.middleware.cors import CORSMiddleware
from .utils import generate_uuid
from slowapi import Limiter, _rate_limit_exceeded_handler
from slowapi.util import get_remote_address
from slowapi.errors import RateLimitExceeded


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,6 +35,7 @@


@app.post("/file")
@limiter.limit("150/minute")
def post_as_a_file(file: UploadFile = File(...)):
try:
uuid = generate_uuid()
Expand Down Expand Up @@ -85,9 +93,8 @@ def web(request: Request):


@app.post("/web", response_class=PlainTextResponse)
@limiter.limit("500/minute")
def web_post(content: str = Form(...)):
# print(content)
# return PlainTextResponse(content=content)
try:
file_content = content.encode()
uuid = generate_uuid()
Expand Down