Skip to content

Commit

Permalink
add quart-rate-limiter
Browse files Browse the repository at this point in the history
  • Loading branch information
rtk-rnjn committed Sep 8, 2023
1 parent cf7bc64 commit 59546f0
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

config = Config()
config.bind = ["0.0.0.0:5000"]
config.loglevel = "INFO"
config.accesslog = "-"
config.errorlog = "-"
config.use_reloader = True


def runner():
Expand Down
5 changes: 5 additions & 0 deletions app/quart_app.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
from __future__ import annotations

from datetime import timedelta
from os import environ

from discord.ext.ipc.client import Client
from quart import Quart
from quart_rate_limiter import RateLimit, RateLimiter

from core.Parrot import IPC_PORT

IPC_SECRET_KEY = environ.get("IPC_KEY")

app = Quart("discord_bot_ipc_api")
rate_limiter = RateLimiter(default_limits=[RateLimit(10, timedelta(seconds=1))])
rate_limiter.init_app(app)

ipc = Client(standard_port=IPC_PORT, secret_key=IPC_SECRET_KEY)


Expand Down
13 changes: 12 additions & 1 deletion app/routes/misc_routes.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
from __future__ import annotations

import logging
from datetime import timedelta

from quart import Response, jsonify, request
from quart_rate_limiter import rate_limit

from utilities.converters import convert_bool

from ..quart_app import app, ipc

from utilities.converters import convert_bool
log = logging.getLogger("api")


@app.route("/")
@rate_limit(limit=5, period=timedelta(seconds=1))
async def index() -> Response:
return jsonify({"status": "running"})

Expand Down Expand Up @@ -43,6 +51,7 @@ async def error_handler(e: Exception) -> Response:


@app.route("/nsfw_links/<count>")
@rate_limit(limit=5, period=timedelta(seconds=1))
async def nsfw_links(count: str) -> Response:
if count.isdigit():
count = int(count)
Expand All @@ -59,4 +68,6 @@ async def nsfw_links(count: str) -> Response:
ipc_response = await ipc.request("nsfw_links", count=int(count), type=tp, gif=gif)
if not ipc_response:
return jsonify({"status": "error", "message": "No guilds found"})

log.info("returning %s links to %s", count, request.remote_addr)
return jsonify({"status": "success", **ipc_response.response})
2 changes: 1 addition & 1 deletion cogs/ipc/ipc.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ async def nsfw_links(self, data: ClientPayload) -> dict[str, list[str]]:
if limit is None and count is None:
return {"links": []}

limit = max(1, min(10, limit or count)) # type: ignore
limit = max(1, min(100, limit or count)) # type: ignore
return await self.get_nsfw_links(limit, tp, gif=gif)

async def get_nsfw_links(self, limit: int, tp: str | None = None, *, gif: bool = True) -> dict[str, list[str]]:
Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -178,4 +178,5 @@ wsproto
yapf
yarl
youtube-dl
uvloop; sys_platform != "win32"
uvloop; sys_platform != "win32"
quart-rate-limiter

0 comments on commit 59546f0

Please sign in to comment.