Files
mtusi2026lab3/app/main.py
drunkdiente 3aba500a0e Реализовать стенд лабораторной 3: две API-реплики и балансировка
FastAPI + PostgreSQL как единый источник истины: пользователи, Basic/Bearer-сессии, чаты, сообщения. Сессии и идемпотентные ключи хранятся в БД, поэтому реплики взаимозаменяемы. Общая идемпотентность POST сообщения по Idempotency-Key, включая конкурентный повтор через разные реплики. compose.yaml: postgres, one-shot migrate, api1, api2 и nginx как единственная публичная точка входа. Добавлены scripts/stop-one.sh и scripts/restart.sh, student-tests, evidence и заполненный REPORT.md.
2026-11-07 16:04:29 +03:00

600 lines
23 KiB
Python

"""HTTP API for laboratory 3: shared state, two interchangeable replicas."""
from __future__ import annotations
import base64
import hashlib
import json
import logging
import uuid
from contextlib import asynccontextmanager
from datetime import datetime, timezone
from typing import Any
from fastapi import FastAPI, Request, Response
from fastapi.responses import JSONResponse
from psycopg import errors as pg_errors
from psycopg.rows import dict_row
from psycopg.types.json import Jsonb
from psycopg_pool import AsyncConnectionPool
from starlette.exceptions import HTTPException as StarletteHTTPException
from .errors import ApiError
from .security import hash_password, new_session_token, token_digest, verify_password
from .settings import load_settings
from .validation import (
invalid,
parse_limit,
parse_uuid,
reject_unknown_fields,
require_string,
validate_display_name,
validate_idempotency_key,
validate_member_ids,
validate_message_text,
validate_password,
validate_title,
validate_username,
)
logger = logging.getLogger("messenger.api")
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
settings = load_settings()
BASIC_CHALLENGE = {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
BEARER_CHALLENGE = {"WWW-Authenticate": 'Bearer realm="messenger"'}
@asynccontextmanager
async def lifespan(app: FastAPI):
pool = AsyncConnectionPool(
settings.database_url,
min_size=1,
max_size=10,
timeout=5,
open=False,
kwargs={"application_name": settings.instance_id},
)
await pool.open(wait=False)
app.state.pool = pool
try:
yield
finally:
await pool.close()
app = FastAPI(title="MTUSI Messenger lab3", lifespan=lifespan, docs_url=None, redoc_url=None)
def error_response(
request: Request,
status_code: int,
code: str,
message: str,
headers: dict[str, str] | None = None,
) -> JSONResponse:
request_id = getattr(request.state, "request_id", None) or str(uuid.uuid4())
response = JSONResponse(
status_code=status_code,
content={"error": {"code": code, "message": message, "request_id": request_id}},
)
for name, value in (headers or {}).items():
response.headers[name] = value
return response
@app.middleware("http")
async def add_instance_and_request_headers(request: Request, call_next):
request_id = str(uuid.uuid4())
request.state.request_id = request_id
try:
response = await call_next(request)
except ApiError as exc: # defensive: handlers normally convert this first
response = error_response(request, exc.status_code, exc.code, exc.message, exc.headers)
except Exception:
logger.exception("unhandled error while serving request")
response = error_response(request, 500, "internal_error", "Internal server error")
response.headers["X-Request-Id"] = request_id
response.headers["X-Instance-Id"] = settings.instance_id
return response
@app.exception_handler(ApiError)
async def api_error_handler(request: Request, exc: ApiError) -> JSONResponse:
return error_response(request, exc.status_code, exc.code, exc.message, exc.headers)
@app.exception_handler(StarletteHTTPException)
async def http_error_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse:
code = "not_found" if exc.status_code == 404 else "invalid_request"
return error_response(request, exc.status_code, code, "Request could not be processed")
@app.exception_handler(Exception)
async def unhandled_error_handler(request: Request, exc: Exception) -> JSONResponse:
logger.exception("unhandled error: %s", type(exc).__name__)
return error_response(request, 500, "internal_error", "Internal server error")
def iso_utc(value: datetime) -> str:
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z")
def user_json(row: dict[str, Any]) -> dict[str, Any]:
return {
"id": str(row["id"]),
"username": row["username"],
"display_name": row["display_name"],
"created_at": iso_utc(row["created_at"]),
}
def message_json(row: dict[str, Any]) -> dict[str, Any]:
return {
"id": str(row["id"]),
"chat_id": str(row["chat_id"]),
"sender_id": str(row["sender_id"]),
"text": row["text"],
"created_at": iso_utc(row["created_at"]),
}
def chat_json(row: dict[str, Any], member_ids: list[str]) -> dict[str, Any]:
return {
"id": str(row["id"]),
"title": row["title"],
"member_ids": member_ids,
"created_at": iso_utc(row["created_at"]),
}
async def read_json_object(request: Request) -> dict[str, Any]:
raw = await request.body()
try:
value = json.loads(raw)
except (ValueError, UnicodeDecodeError) as exc:
raise invalid("Request body must be valid JSON") from exc
if not isinstance(value, dict):
raise invalid("Request body must be a JSON object")
return value
async def authenticate(request: Request) -> dict[str, Any]:
header = request.headers.get("authorization", "")
scheme, _, token = header.partition(" ")
token = token.strip()
if scheme.lower() != "bearer" or not token:
raise ApiError(401, "unauthorized", "Bearer token required", BEARER_CHALLENGE)
digest = token_digest(token)
async with request.app.state.pool.connection() as conn:
async with conn.cursor(row_factory=dict_row) as cur:
await cur.execute(
"""
SELECT u.id, u.username, u.display_name, u.created_at
FROM sessions s
JOIN users u ON u.id = s.user_id
WHERE s.token_hash = %s AND s.expires_at > now()
""",
(digest,),
)
row = await cur.fetchone()
if row is None:
raise ApiError(401, "unauthorized", "Session is not valid", BEARER_CHALLENGE)
request.state.token_hash = digest
return row
async def require_member(pool: AsyncConnectionPool, chat_id: uuid.UUID, user_id: uuid.UUID) -> None:
async with pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s",
(chat_id, user_id),
)
if await cur.fetchone() is None:
raise ApiError(404, "not_found", "Chat not found")
async def chat_members(pool: AsyncConnectionPool, chat_ids: list[uuid.UUID]) -> dict[uuid.UUID, list[str]]:
result: dict[uuid.UUID, list[str]] = {chat_id: [] for chat_id in chat_ids}
if not chat_ids:
return result
async with pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute(
"""
SELECT chat_id, user_id
FROM chat_members
WHERE chat_id = ANY(%s::uuid[])
ORDER BY chat_id, user_id
""",
(chat_ids,),
)
for chat_id, user_id in await cur.fetchall():
result[chat_id].append(str(user_id))
return result
def encode_cursor(scope: str, created_at: datetime, item_id: uuid.UUID) -> str:
payload = {"v": 1, "s": scope, "t": iso_utc(created_at), "i": str(item_id)}
raw = json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode("utf-8")
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
def decode_cursor(raw: str) -> tuple[str, datetime, uuid.UUID]:
try:
padded = raw + "=" * (-len(raw) % 4)
payload = json.loads(base64.urlsafe_b64decode(padded.encode("ascii")))
if not isinstance(payload, dict) or payload.get("v") != 1:
raise ValueError
scope = payload["s"]
created_at = datetime.fromisoformat(str(payload["t"]).replace("Z", "+00:00"))
item_id = uuid.UUID(str(payload["i"]))
if not isinstance(scope, str):
raise ValueError
except (ValueError, KeyError, TypeError, UnicodeDecodeError) as exc:
raise invalid("cursor is invalid") from exc
if created_at.tzinfo is None:
created_at = created_at.replace(tzinfo=timezone.utc)
return scope, created_at, item_id
@app.get("/health/live")
async def health_live() -> dict[str, str]:
return {"status": "ok"}
@app.get("/health/ready")
async def health_ready(request: Request) -> dict[str, str]:
try:
async with request.app.state.pool.connection(timeout=2) as conn:
async with conn.cursor() as cur:
await cur.execute("SELECT 1")
await cur.fetchone()
except Exception as exc:
logger.warning("readiness check failed: %s", type(exc).__name__)
raise ApiError(503, "unavailable", "Database is not available") from exc
return {"status": "ok"}
@app.post("/api/v1/users")
async def register_user(request: Request) -> JSONResponse:
data = await read_json_object(request)
reject_unknown_fields(data, {"username", "display_name", "password"})
username = validate_username(require_string(data, "username"))
display_name = validate_display_name(require_string(data, "display_name"))
password = validate_password(require_string(data, "password"))
password_hash = hash_password(password)
try:
async with request.app.state.pool.connection() as conn:
async with conn.cursor(row_factory=dict_row) as cur:
await cur.execute(
"""
INSERT INTO users (id, username, display_name, password_hash)
VALUES (%s, %s, %s, %s)
RETURNING id, username, display_name, created_at
""",
(uuid.uuid4(), username, display_name, password_hash),
)
row = await cur.fetchone()
await conn.commit()
except pg_errors.UniqueViolation as exc:
raise ApiError(409, "conflict", "username is already taken") from exc
return JSONResponse(status_code=201, content=user_json(row))
@app.post("/api/v1/auth/sessions")
async def create_session(request: Request) -> JSONResponse:
header = request.headers.get("authorization", "")
scheme, _, credential = header.partition(" ")
if scheme.lower() != "basic" or not credential.strip():
raise ApiError(401, "unauthorized", "Basic credentials required", BASIC_CHALLENGE)
try:
decoded = base64.b64decode(credential.strip(), validate=True).decode("utf-8")
username, password = decoded.split(":", 1)
except (ValueError, UnicodeDecodeError) as exc:
raise ApiError(401, "unauthorized", "Invalid credentials", BASIC_CHALLENGE) from exc
if not username or not password:
raise ApiError(401, "unauthorized", "Invalid credentials", BASIC_CHALLENGE)
async with request.app.state.pool.connection() as conn:
async with conn.cursor(row_factory=dict_row) as cur:
await cur.execute(
"SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = %s",
(username,),
)
row = await cur.fetchone()
if row is None or not verify_password(password, row["password_hash"]):
raise ApiError(401, "unauthorized", "Invalid credentials", BASIC_CHALLENGE)
token = new_session_token()
await cur.execute(
"""
INSERT INTO sessions (token_hash, user_id, expires_at)
VALUES (%s, %s, now() + make_interval(secs => %s))
RETURNING expires_at
""",
(token_digest(token), row["id"], settings.session_ttl_seconds),
)
expires_at = (await cur.fetchone())["expires_at"]
await conn.commit()
return JSONResponse(
status_code=201,
content={
"token": token,
"token_type": "Bearer",
"expires_at": iso_utc(expires_at),
"user": user_json(row),
},
)
@app.delete("/api/v1/auth/sessions/current")
async def delete_current_session(request: Request) -> Response:
await authenticate(request)
async with request.app.state.pool.connection() as conn:
async with conn.cursor() as cur:
await cur.execute("DELETE FROM sessions WHERE token_hash = %s", (request.state.token_hash,))
await conn.commit()
return Response(status_code=204)
@app.get("/api/v1/users/me")
async def current_user(request: Request) -> dict[str, Any]:
user = await authenticate(request)
return user_json(user)
@app.post("/api/v1/chats")
async def create_chat(request: Request) -> JSONResponse:
user = await authenticate(request)
data = await read_json_object(request)
reject_unknown_fields(data, {"title", "member_ids"})
title = validate_title(require_string(data, "title"))
member_ids = validate_member_ids(data.get("member_ids"))
user_id = user["id"]
if user_id not in member_ids:
raise invalid("Caller must be a member of the new chat")
chat_id = uuid.uuid4()
async with request.app.state.pool.connection() as conn:
async with conn.transaction():
async with conn.cursor(row_factory=dict_row) as cur:
await cur.execute("SELECT id FROM users WHERE id = ANY(%s::uuid[])", (member_ids,))
known = {row["id"] for row in await cur.fetchall()}
if known != set(member_ids):
raise invalid("member_ids contains unknown users")
await cur.execute(
"INSERT INTO chats (id, title) VALUES (%s, %s) RETURNING id, title, created_at",
(chat_id, title),
)
chat = await cur.fetchone()
await cur.executemany(
"INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)",
[(chat_id, member_id) for member_id in member_ids],
)
return JSONResponse(
status_code=201,
content=chat_json(chat, sorted(str(member_id) for member_id in member_ids)),
)
@app.get("/api/v1/chats")
async def list_chats(request: Request) -> dict[str, Any]:
user = await authenticate(request)
limit = parse_limit(request)
cursor_raw = request.query_params.get("cursor")
conditions = ["m.user_id = %s"]
params: list[Any] = [user["id"]]
if cursor_raw:
scope, created_at, item_id = decode_cursor(cursor_raw)
if scope != "chats":
raise invalid("cursor does not belong to this collection")
conditions.append("(c.created_at, c.id) > (%s::timestamptz, %s::uuid)")
params.extend([created_at, item_id])
params.append(limit + 1)
async with request.app.state.pool.connection() as conn:
async with conn.cursor(row_factory=dict_row) as cur:
await cur.execute(
"""
SELECT c.id, c.title, c.created_at
FROM chats c
JOIN chat_members m ON m.chat_id = c.id
WHERE """
+ " AND ".join(conditions)
+ " ORDER BY c.created_at ASC, c.id ASC LIMIT %s",
params,
)
rows = await cur.fetchall()
has_more = len(rows) > limit
rows = rows[:limit]
members = await chat_members(request.app.state.pool, [row["id"] for row in rows])
next_cursor = (
encode_cursor("chats", rows[-1]["created_at"], rows[-1]["id"]) if has_more and rows else None
)
return {
"items": [chat_json(row, members[row["id"]]) for row in rows],
"next_cursor": next_cursor,
}
@app.get("/api/v1/chats/{chat_id}")
async def get_chat(chat_id: str, request: Request) -> dict[str, Any]:
user = await authenticate(request)
parsed_chat_id = parse_uuid(chat_id, "chat_id")
await require_member(request.app.state.pool, parsed_chat_id, user["id"])
async with request.app.state.pool.connection() as conn:
async with conn.cursor(row_factory=dict_row) as cur:
await cur.execute(
"SELECT id, title, created_at FROM chats WHERE id = %s",
(parsed_chat_id,),
)
row = await cur.fetchone()
if row is None:
raise ApiError(404, "not_found", "Chat not found")
members = await chat_members(request.app.state.pool, [parsed_chat_id])
return chat_json(row, members[parsed_chat_id])
def canonical_request_hash(payload: dict[str, Any]) -> str:
canonical = json.dumps(payload, sort_keys=True, separators=(",", ":"), ensure_ascii=False)
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
async def create_message_idempotent(
pool: AsyncConnectionPool,
user_id: uuid.UUID,
chat_id: uuid.UUID,
idempotency_key: str,
text: str,
) -> tuple[int, dict[str, Any]]:
path = f"/api/v1/chats/{chat_id}/messages"
request_hash = canonical_request_hash({"text": text})
for _ in range(5):
async with pool.connection() as conn:
async with conn.transaction():
async with conn.cursor(row_factory=dict_row) as cur:
await cur.execute(
"SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s",
(chat_id, user_id),
)
if await cur.fetchone() is None:
raise ApiError(404, "not_found", "Chat not found")
await cur.execute(
"""
INSERT INTO idempotency_keys
(user_id, method, path, key, request_hash, expires_at)
VALUES (%s, 'POST', %s, %s, %s, now() + make_interval(secs => %s))
ON CONFLICT DO NOTHING
RETURNING user_id
""",
(
user_id,
path,
idempotency_key,
request_hash,
settings.idempotency_ttl_seconds,
),
)
if await cur.fetchone() is not None:
await cur.execute(
"""
INSERT INTO messages (id, chat_id, sender_id, text)
VALUES (%s, %s, %s, %s)
RETURNING id, chat_id, sender_id, text, created_at
""",
(uuid.uuid4(), chat_id, user_id, text),
)
message = message_json(await cur.fetchone())
await cur.execute(
"""
UPDATE idempotency_keys
SET status_code = 201, response_body = %s
WHERE user_id = %s AND method = 'POST' AND path = %s AND key = %s
""",
(Jsonb(message), user_id, path, idempotency_key),
)
return 201, message
await cur.execute(
"""
SELECT request_hash, status_code, response_body, expires_at, now() AS now_ts
FROM idempotency_keys
WHERE user_id = %s AND method = 'POST' AND path = %s AND key = %s
""",
(user_id, path, idempotency_key),
)
existing = await cur.fetchone()
if existing is None:
continue
if existing["expires_at"] <= existing["now_ts"]:
await cur.execute(
"""
DELETE FROM idempotency_keys
WHERE user_id = %s AND method = 'POST' AND path = %s AND key = %s
""",
(user_id, path, idempotency_key),
)
continue
if existing["request_hash"] != request_hash:
raise ApiError(
409,
"conflict",
"Idempotency-Key was reused with a different request body",
)
if existing["status_code"] is None or existing["response_body"] is None:
continue
return existing["status_code"], existing["response_body"]
raise ApiError(503, "unavailable", "Concurrent idempotent request did not complete")
@app.post("/api/v1/chats/{chat_id}/messages")
async def create_message(chat_id: str, request: Request) -> JSONResponse:
user = await authenticate(request)
parsed_chat_id = parse_uuid(chat_id, "chat_id")
await require_member(request.app.state.pool, parsed_chat_id, user["id"])
idempotency_key = validate_idempotency_key(request)
data = await read_json_object(request)
reject_unknown_fields(data, {"text"})
text = validate_message_text(require_string(data, "text"))
status_code, message = await create_message_idempotent(
request.app.state.pool, user["id"], parsed_chat_id, idempotency_key, text
)
return JSONResponse(status_code=status_code, content=message)
@app.get("/api/v1/chats/{chat_id}/messages")
async def list_messages(chat_id: str, request: Request) -> dict[str, Any]:
user = await authenticate(request)
parsed_chat_id = parse_uuid(chat_id, "chat_id")
await require_member(request.app.state.pool, parsed_chat_id, user["id"])
limit = parse_limit(request)
cursor_raw = request.query_params.get("cursor")
conditions = ["chat_id = %s"]
params: list[Any] = [parsed_chat_id]
if cursor_raw:
scope, created_at, item_id = decode_cursor(cursor_raw)
if scope != str(parsed_chat_id):
raise invalid("cursor does not belong to this chat")
conditions.append("(created_at, id) > (%s::timestamptz, %s::uuid)")
params.extend([created_at, item_id])
params.append(limit + 1)
async with request.app.state.pool.connection() as conn:
async with conn.cursor(row_factory=dict_row) as cur:
await cur.execute(
"""
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE """
+ " AND ".join(conditions)
+ " ORDER BY created_at ASC, id ASC LIMIT %s",
params,
)
rows = await cur.fetchall()
has_more = len(rows) > limit
rows = rows[:limit]
next_cursor = (
encode_cursor(str(parsed_chat_id), rows[-1]["created_at"], rows[-1]["id"])
if has_more and rows
else None
)
return {"items": [message_json(row) for row in rows], "next_cursor": next_cursor}