Files
desungongpai/app/extensions.py
default d54e53e0b7 feat: KKS P240/P241 蓝牙工牌管理系统初始提交
FastAPI + SQLAlchemy + asyncio TCP 服务器,支持设备管理、实时定位、
告警、考勤打卡、蓝牙记录、指令下发、TTS语音播报等功能。
2026-03-27 10:19:34 +00:00

23 lines
706 B
Python

"""
Shared extension instances (rate limiter, etc.) to avoid circular imports.
"""
from starlette.requests import Request
from slowapi import Limiter
from app.config import settings
def _get_real_client_ip(request: Request) -> str:
"""Extract real client IP from X-Forwarded-For (behind Cloudflare/nginx) or fallback."""
forwarded = request.headers.get("X-Forwarded-For")
if forwarded:
return forwarded.split(",")[0].strip()
cf_ip = request.headers.get("CF-Connecting-IP")
if cf_ip:
return cf_ip.strip()
return request.client.host if request.client else "127.0.0.1"
limiter = Limiter(key_func=_get_real_client_ip, default_limits=[settings.RATE_LIMIT_DEFAULT])