390 lines
16 KiB
Python
390 lines
16 KiB
Python
import asyncio
|
||
import ipaddress
|
||
import logging
|
||
import os
|
||
import re
|
||
import time
|
||
from collections import defaultdict
|
||
from contextlib import asynccontextmanager
|
||
from pathlib import Path
|
||
from fastapi import FastAPI, Request, HTTPException
|
||
from fastapi.middleware.cors import CORSMiddleware
|
||
from fastapi.responses import JSONResponse
|
||
from fastapi.staticfiles import StaticFiles
|
||
from config import FRONTEND_ORIGINS, UPLOADS_DIR, CF_IPS
|
||
from database import init_db
|
||
from routers import users, game, hints, leaderboard, sentences, achievements, wordquiz, bubblewords, pronunciation, assistant, learning, tts, typing, teacher, support, admin, feed, admin_content
|
||
from routers import lectures, admin_auth, admin_wordsearch
|
||
from routers.sentences import init_sentences
|
||
from routers.achievements import init_achievements
|
||
from routers.learning_data import init_learning_data
|
||
|
||
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
||
logger = logging.getLogger(__name__)
|
||
|
||
# ============ Версия ============
|
||
APP_VERSION = os.getenv("APP_VERSION", "1.0.0")
|
||
|
||
# ============ Sentry ============
|
||
SENTRY_DSN = os.getenv("SENTRY_DSN", "")
|
||
if SENTRY_DSN:
|
||
try:
|
||
import sentry_sdk
|
||
from sentry_sdk.integrations.fastapi import FastApiIntegration
|
||
from sentry_sdk.integrations.starlette import StarletteIntegration
|
||
|
||
sentry_sdk.init(
|
||
dsn=SENTRY_DSN,
|
||
integrations=[
|
||
StarletteIntegration(transaction_style="endpoint"),
|
||
FastApiIntegration(transaction_style="endpoint"),
|
||
],
|
||
traces_sample_rate=0.2,
|
||
environment=os.getenv("ENV", "production"),
|
||
release=f"filwords@{APP_VERSION}",
|
||
send_default_pii=False,
|
||
)
|
||
logger.info(f"Sentry initialized (release=filwords@{APP_VERSION})")
|
||
except Exception as e:
|
||
logger.error(f"Failed to initialize Sentry: {e}")
|
||
else:
|
||
logger.warning("SENTRY_DSN not set — Sentry disabled")
|
||
|
||
|
||
# ============ Rate Limiting ============
|
||
class RateLimiter:
|
||
"""In-memory rate limiter.
|
||
|
||
⚠️ ВАЖНО: работает корректно только при ОДНОМ воркере uvicorn.
|
||
Если запускать с --workers N (N>1), лимиты считаются отдельно в каждом
|
||
процессе, и фактический лимит умножается на N.
|
||
|
||
🔧 TODO (23.09.2026): перевести на Redis, когда появится необходимость
|
||
масштабирования. Сейчас воркер один — проблема неактуальна.
|
||
"""
|
||
def __init__(self):
|
||
self.requests = defaultdict(list)
|
||
self.limits = {
|
||
"default": {"max_requests": 60, "window": 60},
|
||
"/game/check-word": {"max_requests": 30, "window": 60},
|
||
"/game/complete-level": {"max_requests": 10, "window": 60},
|
||
"/sentences/check": {"max_requests": 20, "window": 60},
|
||
"/leaderboard": {"max_requests": 10, "window": 60},
|
||
"/assistant/chat": {"max_requests": 20, "window": 60},
|
||
"/learning/answer": {"max_requests": 30, "window": 60},
|
||
"/tts": {"max_requests": 30, "window": 60},
|
||
"/review": {"max_requests": 10, "window": 60},
|
||
"/typing/check": {"max_requests": 20, "window": 60},
|
||
"/teacher/answer": {"max_requests": 30, "window": 60},
|
||
"/teacher/start": {"max_requests": 10, "window": 60},
|
||
"/teacher/explain": {"max_requests": 10, "window": 60},
|
||
"/teacher/translate": {"max_requests": 10, "window": 60},
|
||
"/teacher/example": {"max_requests": 10, "window": 60},
|
||
"/support/send": {"max_requests": 10, "window": 60},
|
||
"/support/admin-reply": {"max_requests": 30, "window": 60},
|
||
"/support/translate": {"max_requests": 20, "window": 60},
|
||
"/admin/feed/post": {"max_requests": 20, "window": 60},
|
||
"/admin/feed/upload-image": {"max_requests": 10, "window": 60},
|
||
"/admin/feed/translate": {"max_requests": 10, "window": 60},
|
||
# 🔧 ФИКС (22.09.2026): дорогой AI-эндпоинт без лимита выжигал бюджет.
|
||
"/learning/analytics": {"max_requests": 5, "window": 60},
|
||
"/learning/practice/start": {"max_requests": 10, "window": 60},
|
||
}
|
||
self._cleanup_task = None
|
||
|
||
def _cleanup(self):
|
||
# 🔧 ФИКС (25.09.2026): rate_limiter_cleanup_hardcoded_window —
|
||
# раньше жёстко 60с. Если добавить лимит с window > 60,
|
||
# записи удалялись бы раньше времени, и лимит не работал.
|
||
# Теперь: max(window) из self.limits.
|
||
now = time.time()
|
||
max_window = max(cfg["window"] for cfg in self.limits.values())
|
||
for key in list(self.requests.keys()):
|
||
self.requests[key] = [t for t in self.requests[key] if now - t < max_window]
|
||
if not self.requests[key]:
|
||
del self.requests[key]
|
||
|
||
async def start_cleanup(self):
|
||
while True:
|
||
await asyncio.sleep(60)
|
||
self._cleanup()
|
||
|
||
def is_allowed(self, key: str, path: str) -> bool:
|
||
now = time.time()
|
||
limit_config = self.limits.get(path, self.limits["default"])
|
||
max_requests = limit_config["max_requests"]
|
||
window = limit_config["window"]
|
||
self.requests[key] = [t for t in self.requests[key] if now - t < window]
|
||
if len(self.requests[key]) >= max_requests:
|
||
return False
|
||
self.requests[key].append(now)
|
||
return True
|
||
|
||
def get_retry_after(self, key: str, path: str) -> int:
|
||
if not self.requests[key]:
|
||
return 0
|
||
limit_config = self.limits.get(path, self.limits["default"])
|
||
window = limit_config["window"]
|
||
oldest = min(self.requests[key])
|
||
return max(0, int(window - (time.time() - oldest)))
|
||
|
||
|
||
rate_limiter = RateLimiter()
|
||
|
||
|
||
# 🔧 ФИКС (25.09.2026): cf_ip_check_per_request —
|
||
# компилируем CF_IPS один раз при старте, а не парсим 22 CIDR на каждый запрос.
|
||
_CF_NETWORKS = []
|
||
for _cidr in CF_IPS:
|
||
try:
|
||
_CF_NETWORKS.append(ipaddress.ip_network(_cidr, strict=False))
|
||
except ValueError:
|
||
logger.warning(f"Invalid CF_IPS entry skipped: {_cidr}")
|
||
|
||
|
||
def _is_cloudflare_ip(ip_str: str) -> bool:
|
||
"""Проверяет, что IP входит в предкомпилированные сети Cloudflare."""
|
||
if not ip_str:
|
||
return False
|
||
try:
|
||
ip = ipaddress.ip_address(ip_str)
|
||
except ValueError:
|
||
return False
|
||
return any(ip in net for net in _CF_NETWORKS)
|
||
|
||
|
||
# ============ Фоновое обновление кэша рейтинга ============
|
||
async def refresh_leaderboard_cache_loop():
|
||
await asyncio.sleep(10)
|
||
while True:
|
||
try:
|
||
from database import get_pool
|
||
pool = await get_pool()
|
||
async with pool.acquire() as conn:
|
||
await conn.execute("REFRESH MATERIALIZED VIEW CONCURRENTLY leaderboard_cache")
|
||
except Exception as e:
|
||
logger.error(f"Failed to refresh leaderboard cache: {e}")
|
||
# 🔧 ФИКС (23.09.2026): leaderboard_refresh_30s — refresh каждые 30с
|
||
# нагружал БД ради рейтинга, который меняется редко. Теперь 5 минут.
|
||
await asyncio.sleep(300)
|
||
|
||
|
||
# 🔧 ФИКС (23.09.2026): on_event_deprecated — переходим на lifespan-контекст.
|
||
# @app.on_event("startup") устарел и будет удалён в новых версиях FastAPI.
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
# ============ Startup ============
|
||
await init_db()
|
||
await init_sentences()
|
||
await init_achievements()
|
||
await init_learning_data()
|
||
|
||
# 🔧 ФИКС (25.09.2026): cleanup_task_leak_on_startup_failure —
|
||
# создаём cleanup_task ПОСЛЕ успешной инициализации БД и matview.
|
||
# Если что-то упадёт до этого — задача не создастся, утечки не будет.
|
||
from database import get_pool
|
||
pool = await get_pool()
|
||
async with pool.acquire() as conn:
|
||
# 🔧 ФИКС (23.09.2026): drop_matview_on_startup — раньше DROP + CREATE
|
||
# на каждом рестарте. Теперь только CREATE IF NOT EXISTS.
|
||
# При изменении определения — отдельная миграция.
|
||
await conn.execute("""
|
||
CREATE MATERIALIZED VIEW IF NOT EXISTS leaderboard_cache AS
|
||
SELECT
|
||
u.telegram_id,
|
||
u.username,
|
||
u.current_level,
|
||
COALESCE(ws.total, 0) + COALESCE(s.total, 0) + COALESCE(wq.total, 0) + COALESCE(bw.total, 0) as total_score,
|
||
COALESCE(ws.total, 0) as wordsearch_score,
|
||
COALESCE(s.total, 0) as sentence_score,
|
||
COALESCE(wq.total, 0) as wordquiz_score,
|
||
COALESCE(wq.words_guessed, 0) as words_guessed,
|
||
COALESCE(bw.total, 0) as bubblewords_score,
|
||
COALESCE(bw.levels_completed, 0) as bubble_levels_completed
|
||
FROM users u
|
||
LEFT JOIN (
|
||
SELECT user_id, SUM(score) as total
|
||
FROM user_progress
|
||
WHERE completed_at IS NOT NULL
|
||
GROUP BY user_id
|
||
) ws ON u.telegram_id = ws.user_id
|
||
LEFT JOIN (
|
||
SELECT user_id, SUM(score) as total
|
||
FROM sentence_progress
|
||
WHERE completed_at IS NOT NULL
|
||
GROUP BY user_id
|
||
) s ON u.telegram_id = s.user_id
|
||
LEFT JOIN (
|
||
SELECT user_id, total_score as total, words_guessed
|
||
FROM word_quiz_progress
|
||
) wq ON u.telegram_id = wq.user_id
|
||
LEFT JOIN (
|
||
SELECT user_id, total_score as total, levels_completed
|
||
FROM bubble_words_progress
|
||
) bw ON u.telegram_id = bw.user_id
|
||
WHERE COALESCE(ws.total, 0) + COALESCE(s.total, 0) + COALESCE(wq.total, 0) + COALESCE(bw.total, 0) > 0
|
||
ORDER BY total_score DESC
|
||
""")
|
||
|
||
await conn.execute("""
|
||
CREATE UNIQUE INDEX IF NOT EXISTS idx_leaderboard_cache_user
|
||
ON leaderboard_cache (telegram_id)
|
||
""")
|
||
|
||
logger.info("Leaderboard cache created")
|
||
|
||
refresh_task = asyncio.create_task(refresh_leaderboard_cache_loop())
|
||
# 🔧 ФИКС (25.09.2026): cleanup_task_leak_on_startup_failure —
|
||
# создаём cleanup_task только после успешной инициализации.
|
||
cleanup_task = asyncio.create_task(rate_limiter.start_cleanup())
|
||
|
||
logger.info(f"Filwords API v{APP_VERSION} started")
|
||
|
||
yield
|
||
|
||
# ============ Shutdown ============
|
||
# 🔧 ФИКС (23.09.2026): отменяем фоновые задачи при остановке.
|
||
refresh_task.cancel()
|
||
try:
|
||
await refresh_task
|
||
except asyncio.CancelledError:
|
||
logger.info("Leaderboard refresh task cancelled")
|
||
|
||
# 🔧 ФИКС (24.09.2026): cleanup_task_not_cancelled —
|
||
# отменяем cleanup-задачу rate limiter.
|
||
cleanup_task.cancel()
|
||
try:
|
||
await cleanup_task
|
||
except asyncio.CancelledError:
|
||
logger.info("Rate limiter cleanup task cancelled")
|
||
|
||
logger.info(f"Filwords API v{APP_VERSION} stopped")
|
||
|
||
|
||
# 🔧 ФИКС (25.09.2026): fastapi_docs_exposed —
|
||
# в проде скрываем /docs, /redoc, /openapi.json.
|
||
_ENV = os.getenv("ENV", "production")
|
||
_is_prod = _ENV == "production"
|
||
|
||
app = FastAPI(
|
||
title="Filwords PL API",
|
||
lifespan=lifespan,
|
||
docs_url=None if _is_prod else "/docs",
|
||
redoc_url=None if _is_prod else "/redoc",
|
||
openapi_url=None if _is_prod else "/openapi.json",
|
||
)
|
||
|
||
|
||
# ============ Rate Limiting Middleware ============
|
||
@app.middleware("http")
|
||
async def rate_limit_middleware(request: Request, call_next):
|
||
path = request.url.path
|
||
|
||
if path in ["/", "/health"]:
|
||
return await call_next(request)
|
||
|
||
# 🔧 ФИКС (24.09.2026): cf_connecting_ip_spoof —
|
||
# доверяем cf-connecting-ip ТОЛЬКО если запрос пришёл с IP Cloudflare.
|
||
# Иначе — берём request.client.host (реальный IP клиента).
|
||
real_client_ip = request.client.host if request.client else "unknown"
|
||
|
||
if _is_cloudflare_ip(real_client_ip):
|
||
# Запрос от Cloudflare — доверяем cf-connecting-ip
|
||
client_ip = (
|
||
request.headers.get("cf-connecting-ip")
|
||
or request.headers.get("x-real-ip")
|
||
or real_client_ip
|
||
)
|
||
else:
|
||
# Запрос не от Cloudflare — игнорируем заголовки, берём реальный IP
|
||
client_ip = real_client_ip
|
||
|
||
rate_key = f"{client_ip}:{path}"
|
||
|
||
if not rate_limiter.is_allowed(rate_key, path):
|
||
retry_after = rate_limiter.get_retry_after(rate_key, path)
|
||
return JSONResponse(
|
||
status_code=429,
|
||
content={
|
||
"error": "Too many requests",
|
||
"retry_after": retry_after
|
||
},
|
||
headers={"Retry-After": str(retry_after)}
|
||
)
|
||
|
||
start_time = time.time()
|
||
response = await call_next(request)
|
||
duration = time.time() - start_time
|
||
|
||
if duration > 1.0:
|
||
logger.warning(f"Slow request: {request.method} {path} - {duration:.2f}s")
|
||
|
||
return response
|
||
|
||
|
||
# ============ CORS ============
|
||
# 🔧 ФИКС (26.09.2026): admin_cors — добавлен admin.filwordspl.com в origins.
|
||
# Также добавлен X-Admin-Token в allow_headers для админ-авторизации.
|
||
app.add_middleware(
|
||
CORSMiddleware,
|
||
allow_origins=FRONTEND_ORIGINS,
|
||
allow_credentials=False,
|
||
allow_methods=["GET", "POST", "PUT", "DELETE", "OPTIONS"],
|
||
# 🔧 ФИКС (23.09.2026): INIT_DATA_IN_URL — добавлен Authorization в явный список.
|
||
# Starlette с allow_headers=["*"] НЕ пропускает Authorization через preflight
|
||
# (баг/фича фреймворка). Перечисляем явно.
|
||
allow_headers=[
|
||
"Authorization",
|
||
"Content-Type",
|
||
"X-Telegram-InitData",
|
||
"X-Admin-Token",
|
||
],
|
||
)
|
||
|
||
# ============ Статика для картинок feed-постов ============
|
||
app.mount("/uploads", StaticFiles(directory=UPLOADS_DIR), name="uploads")
|
||
|
||
# ============ Роутеры ============
|
||
app.include_router(users.router)
|
||
app.include_router(game.router)
|
||
app.include_router(hints.router)
|
||
app.include_router(leaderboard.router)
|
||
app.include_router(sentences.router)
|
||
app.include_router(achievements.router)
|
||
app.include_router(wordquiz.router)
|
||
app.include_router(bubblewords.router)
|
||
app.include_router(pronunciation.router)
|
||
app.include_router(assistant.router)
|
||
app.include_router(learning.router)
|
||
app.include_router(tts.router)
|
||
app.include_router(typing.router)
|
||
app.include_router(teacher.router)
|
||
app.include_router(support.router)
|
||
app.include_router(admin.router)
|
||
app.include_router(feed.router)
|
||
app.include_router(admin_content.router)
|
||
app.include_router(lectures.router)
|
||
app.include_router(admin_auth.router)
|
||
app.include_router(admin_wordsearch.router)
|
||
|
||
|
||
# ============ Базовые эндпоинты ============
|
||
@app.get("/")
|
||
async def root():
|
||
return {"status": "ok", "app": "Filwords PL", "version": APP_VERSION}
|
||
|
||
|
||
@app.get("/health")
|
||
async def health():
|
||
try:
|
||
from database import get_pool
|
||
pool = await get_pool()
|
||
async with pool.acquire() as conn:
|
||
await conn.fetchval("SELECT 1")
|
||
return {"status": "ok", "database": "connected"}
|
||
except Exception as e:
|
||
logger.error(f"Healthcheck failed: {e}")
|
||
return JSONResponse(
|
||
status_code=503,
|
||
content={"status": "error", "database": "disconnected"}
|
||
) |