2026-04-15 17:02:00 -07:00
|
|
|
"""Shared SlowAPI limiter instance used by app.py and route modules."""
|
|
|
|
|
|
2026-04-16 02:13:52 -07:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-04-15 17:02:00 -07:00
|
|
|
from limits.storage import MemoryStorage
|
|
|
|
|
from slowapi import Limiter
|
2026-04-16 02:13:52 -07:00
|
|
|
from starlette.requests import Request
|
2026-04-15 17:02:00 -07:00
|
|
|
|
|
|
|
|
from app.config import config
|
|
|
|
|
|
2026-04-16 02:13:52 -07:00
|
|
|
|
|
|
|
|
def get_real_client_ip(request: Request) -> str:
|
|
|
|
|
"""Extract the real client IP behind Cloudflare / reverse proxies.
|
|
|
|
|
|
|
|
|
|
Priority: CF-Connecting-IP > X-Real-IP > X-Forwarded-For (first entry) > socket peer.
|
|
|
|
|
"""
|
|
|
|
|
cf_ip = request.headers.get("cf-connecting-ip")
|
|
|
|
|
if cf_ip:
|
|
|
|
|
return cf_ip.strip()
|
|
|
|
|
real_ip = request.headers.get("x-real-ip")
|
|
|
|
|
if real_ip:
|
|
|
|
|
return real_ip.strip()
|
|
|
|
|
forwarded = request.headers.get("x-forwarded-for")
|
|
|
|
|
if forwarded:
|
|
|
|
|
return forwarded.split(",")[0].strip()
|
|
|
|
|
return request.client.host if request.client else "127.0.0.1"
|
|
|
|
|
|
|
|
|
|
|
2026-04-15 17:02:00 -07:00
|
|
|
limiter = Limiter(
|
2026-04-16 02:13:52 -07:00
|
|
|
key_func=get_real_client_ip,
|
2026-04-15 17:02:00 -07:00
|
|
|
storage_uri=config.REDIS_APP_URL,
|
|
|
|
|
default_limits=["1024/minute"],
|
|
|
|
|
in_memory_fallback_enabled=True,
|
|
|
|
|
in_memory_fallback=[MemoryStorage()],
|
|
|
|
|
)
|