import asyncio import base64 import binascii import json import logging import os import re import resource import signal import sys import uuid from contextlib import asynccontextmanager from typing import List, Optional, Tuple from fastapi import FastAPI, Request from fastapi.responses import JSONResponse, Response from starlette.concurrency import run_in_threadpool from app import config, passwords, repo from app.cursor import BadCursor, decode_cursor, encode_cursor from app.db import Database from app.errors import ApiError, bad, error_body, not_found, unauthorized, unavailable from app.sessions import SessionStore logging.basicConfig(stream=sys.stdout, level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s") log = logging.getLogger("messenger") USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$") ZERO_WIDTH = str.maketrans({char: None for char in "\u200b\u200c\u200d\u2060\ufeff"}) EXEMPT_PATHS = ("/health/live", "/health/ready") PAGE_SIZE = os.sysconf("SC_PAGE_SIZE") if hasattr(os, "sysconf") else 4096 db = Database() sessions = SessionStore(db) state = {"draining": False, "inflight": 0, "rejected": {}} def rss_mb() -> float: try: with open("/proc/self/statm") as handle: return int(handle.read().split()[1]) * PAGE_SIZE / 1048576 except OSError: peak = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss return peak / 1048576 if sys.platform == "darwin" else peak / 1024 def count_rejection(code: str) -> None: state["rejected"][code] = state["rejected"].get(code, 0) + 1 async def sweeper() -> None: while True: await asyncio.sleep(config.SESSION_SWEEP_SECONDS) try: removed = await sessions.sweep() if removed: log.info("sessions_expired removed=%d", removed) except Exception: log.warning("session sweep skipped: storage unavailable") @asynccontextmanager async def lifespan(_app: FastAPI): previous = {} def on_signal(signum, frame): if not state["draining"]: state["draining"] = True log.info("signal %s: draining, new requests get 503", signal.Signals(signum).name) handler = previous.get(signum) if callable(handler): handler(signum, frame) for sig in (signal.SIGTERM, signal.SIGINT): previous[sig] = signal.getsignal(sig) signal.signal(sig, on_signal) await db.open() await sessions.open() task = asyncio.create_task(sweeper()) log.info( "started: instance=%s session_store=%s session_ttl=%d cache_ttl=%d pool=%d..%d max_inflight=%d max_body=%d", config.INSTANCE_ID, config.SESSION_STORE, config.SESSION_TTL_SECONDS, config.SESSION_CACHE_TTL_SECONDS, config.DB_POOL_MIN, config.DB_POOL_MAX, config.MAX_INFLIGHT_REQUESTS, config.MAX_BODY_BYTES, ) yield task.cancel() await sessions.close() await db.close() log.info("stopped: cache=%s rejected=%s", sessions.stats, state["rejected"]) app = FastAPI(lifespan=lifespan, docs_url=None, redoc_url=None, openapi_url=None) @app.middleware("http") async def guard(request: Request, call_next): rid = uuid.uuid4().hex request.state.rid = rid exempt = request.url.path in EXEMPT_PATHS if state["draining"] and request.url.path != "/health/live": count_rejection("draining") return error_body(rid, config.INSTANCE_ID, 503, "unavailable", "Сервер завершает работу", {"Retry-After": "1", "Connection": "close"}) if not exempt and state["inflight"] >= config.MAX_INFLIGHT_REQUESTS: count_rejection("inflight") return error_body(rid, config.INSTANCE_ID, 429, "rate_limited", "Слишком много одновременных запросов", {"Retry-After": "1"}) state["inflight"] += 1 try: response = await asyncio.wait_for(call_next(request), config.REQUEST_TIMEOUT) except asyncio.TimeoutError: count_rejection("request_timeout") log.warning("request timeout request_id=%s method=%s path=%s", rid, request.method, request.url.path) response = error_body( rid, config.INSTANCE_ID, 503, "unavailable", "Запрос не удалось обработать за отведённое время", {"Retry-After": "1"}, ) except Exception: log.exception("unhandled error request_id=%s", rid) response = error_body(rid, config.INSTANCE_ID, 500, "internal_error", "Внутренняя ошибка сервера") finally: state["inflight"] -= 1 response.headers["X-Request-Id"] = rid response.headers["X-Instance-Id"] = config.INSTANCE_ID return response @app.exception_handler(ApiError) async def api_error_handler(request: Request, exc: ApiError): if exc.status in (413, 429, 503): count_rejection(exc.code) return error_body(request.state.rid, config.INSTANCE_ID, exc.status, exc.code, exc.message, exc.headers) def parse_uuid(value: object) -> Optional[str]: if not isinstance(value, str): return None try: return str(uuid.UUID(value)) except ValueError: return None def non_blank(value: object, max_len: int) -> bool: if not isinstance(value, str) or not 1 <= len(value) <= max_len: return False return value.translate(ZERO_WIDTH).strip() != "" async def read_json(request: Request, fields: Tuple[str, ...]) -> dict: if request.headers.get("content-type", "").split(";")[0].strip().lower() != "application/json": raise ApiError(415, "unsupported_media_type", "Ожидается application/json") too_large = ApiError(413, "payload_too_large", "Тело запроса слишком большое", {"Connection": "close"}) declared = request.headers.get("content-length") if declared and declared.isdigit() and int(declared) > config.MAX_BODY_BYTES: raise too_large raw = bytearray() async for chunk in request.stream(): raw += chunk if len(raw) > config.MAX_BODY_BYTES: raise too_large try: body = json.loads(bytes(raw).decode("utf-8")) except (UnicodeDecodeError, ValueError, RecursionError): raise bad("Некорректный JSON") from None if not isinstance(body, dict): raise bad("Ожидается JSON-объект") if set(body) - set(fields): raise bad("Лишние поля в запросе") if set(fields) - set(body): raise bad("Отсутствуют обязательные поля") return body def ensure_capacity() -> None: if config.MAX_RSS_MB and rss_mb() > config.MAX_RSS_MB: raise unavailable("Достигнут лимит памяти процесса, запись недоступна") def bearer_token(request: Request) -> str: scheme, _, value = request.headers.get("authorization", "").partition(" ") token = value.strip() if scheme.lower() != "bearer" or not token: raise unauthorized() return token async def actor(request: Request) -> dict: user = await sessions.resolve(bearer_token(request)) if user is None: raise unauthorized() return user def basic_credentials(request: Request) -> Tuple[str, str]: scheme, _, value = request.headers.get("authorization", "").partition(" ") if scheme.lower() != "basic" or not value.strip(): raise unauthorized("Basic") try: decoded = base64.b64decode(value.strip(), validate=True).decode("utf-8") except (binascii.Error, ValueError, UnicodeDecodeError): raise unauthorized("Basic") from None username, separator, password = decoded.partition(":") if not separator: raise unauthorized("Basic") return username, password async def member_chat(user: dict, chat_id: str) -> dict: cid = parse_uuid(chat_id) if cid is None: raise bad("chat_id должен быть UUID") async with db.connection() as conn: chat = await repo.chat_for_member(conn, cid, user["id"]) if chat is None: raise not_found() return chat def page_params(request: Request) -> Tuple[int, Optional[str]]: query = request.query_params limit = 50 if "limit" in query: if not re.fullmatch(r"[0-9]{1,3}", query["limit"]) or not 1 <= int(query["limit"]) <= 100: raise bad("limit должен быть целым 1..100") limit = int(query["limit"]) cursor = query.get("cursor") if cursor is not None and cursor == "": raise bad("Некорректный cursor") return limit, cursor def keyset(scope: str, cursor: Optional[str]): if cursor is None: return None try: return decode_cursor(scope, cursor) except BadCursor: raise bad("Некорректный cursor") from None def page(scope: str, items: List[dict], has_more: bool) -> JSONResponse: next_cursor = encode_cursor(scope, items[-1]["created_at"], items[-1]["id"]) if has_more and items else None return JSONResponse({"items": items, "next_cursor": next_cursor}) @app.get("/health/live") async def live(): return {"status": "ok"} @app.get("/health/ready") async def ready(): if not await db.ready(): raise unavailable("База данных недоступна") return {"status": "ok"} @app.post("/api/v1/users", status_code=201) async def create_user(request: Request): body = await read_json(request, ("username", "display_name", "password")) if not isinstance(body["username"], str) or not USERNAME_RE.fullmatch(body["username"]): raise bad("username должен соответствовать [a-z0-9_]{3,32}") if not non_blank(body["display_name"], 100): raise bad("display_name: 1..100 символов, не только пробелы") password = body["password"] if not isinstance(password, str) or not config.PASSWORD_MIN <= len(password) <= config.PASSWORD_MAX: raise bad("password: %d..%d символов" % (config.PASSWORD_MIN, config.PASSWORD_MAX)) ensure_capacity() password_hash = await run_in_threadpool(passwords.hash_password, password) async with db.connection() as conn: user = await repo.create_user(conn, body["username"], body["display_name"], password_hash) if user is None: raise ApiError(409, "conflict", "username уже занят") log.info("user_created id=%s", user["id"]) return JSONResponse(user, status_code=201) @app.post("/api/v1/auth/sessions", status_code=201) async def login(request: Request): if await request.body(): raise bad("POST /auth/sessions не принимает тело запроса") username, password = basic_credentials(request) row = None if USERNAME_RE.fullmatch(username) and config.PASSWORD_MIN <= len(password) <= config.PASSWORD_MAX: async with db.connection() as conn: row = await repo.credentials(conn, username) stored = row["password_hash"] if row else passwords.DUMMY_HASH matches = await run_in_threadpool(passwords.verify_password, stored, password) if row is None or not matches: log.info("login_failed username_known=%s", row is not None) raise unauthorized("Basic") user = repo.public_user(row) token, expires_at = await sessions.create(user) log.info("session_created user=%s", user["id"]) return JSONResponse({"token": token, "token_type": "Bearer", "expires_at": expires_at, "user": user}, status_code=201) @app.delete("/api/v1/auth/sessions/current", status_code=204) async def logout(request: Request): token = bearer_token(request) user = await sessions.resolve(token) if user is None: raise unauthorized() await sessions.revoke(token) log.info("session_revoked user=%s", user["id"]) return Response(status_code=204) @app.get("/api/v1/users/me") async def me(request: Request): return await actor(request) @app.post("/api/v1/chats", status_code=201) async def create_chat(request: Request): user = await actor(request) body = await read_json(request, ("title", "member_ids")) if not non_blank(body["title"], 100): raise bad("title: 1..100 символов, не только пробелы") raw_ids = body["member_ids"] if not isinstance(raw_ids, list) or not 1 <= len(raw_ids) <= 100: raise bad("member_ids: массив из 1..100 UUID") ids: List[str] = [] for value in raw_ids: parsed = parse_uuid(value) if parsed is None: raise bad("member_ids содержит некорректный UUID") ids.append(parsed) if len(set(ids)) != len(ids): raise bad("member_ids содержит дубликаты") if user["id"] not in ids: raise bad("Создатель должен входить в member_ids") ensure_capacity() async def insert(conn): return await repo.create_chat(conn, body["title"], ids) try: chat = await db.in_transaction(insert) except repo.UnknownMember: raise bad("member_ids содержит неизвестного пользователя") from None log.info("chat_created id=%s members=%d", chat["id"], len(ids)) return JSONResponse(chat, status_code=201) @app.get("/api/v1/chats") async def list_chats(request: Request): user = await actor(request) limit, cursor = page_params(request) scope = "chats:" + user["id"] after = keyset(scope, cursor) async with db.connection() as conn: items, has_more = await repo.list_chats(conn, user["id"], limit, after) return page(scope, items, has_more) @app.get("/api/v1/chats/{chat_id}") async def get_chat(chat_id: str, request: Request): return await member_chat(await actor(request), chat_id) @app.post("/api/v1/chats/{chat_id}/messages", status_code=201) async def create_message(chat_id: str, request: Request): user = await actor(request) cid = parse_uuid(chat_id) if cid is None: raise bad("chat_id должен быть UUID") body = await read_json(request, ("text",)) if not non_blank(body["text"], 4000): raise bad("text: 1..4000 символов, не только пробелы") ensure_capacity() async with db.connection() as conn: message = await repo.create_message(conn, cid, user["id"], body["text"]) if message is None: raise not_found() return JSONResponse(message, status_code=201) @app.get("/api/v1/chats/{chat_id}/messages") async def list_messages(chat_id: str, request: Request): chat = await member_chat(await actor(request), chat_id) limit, cursor = page_params(request) scope = "messages:" + chat["id"] after = keyset(scope, cursor) async with db.connection() as conn: items, has_more = await repo.list_messages(conn, chat["id"], limit, after) return page(scope, items, has_more) @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "PATCH", "DELETE"]) async def fallback(request: Request): raise not_found()