Files
mtusi2026lab3/app/main.py
T

579 lines
20 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""HTTP API for laboratory 3 (balanced replicas + shared idempotency)."""
from __future__ import annotations
import hashlib
import json
import logging
import time
import uuid
from datetime import datetime, timedelta, timezone
import psycopg
import psycopg_pool
import redis
from argon2 import PasswordHasher
from argon2.exceptions import InvalidHashError, VerifyMismatchError
from flask import Flask, Response, g, request
from psycopg.types.json import Jsonb
from . import db, sessions
from .config import settings
from .errors import ApiError, bad_request, conflict, not_found, unauthorized, unavailable
from .validation import (
cursor_time,
decode_cursor,
display_name,
encode_cursor,
exact_keys,
idempotency_key,
json_object,
limit_arg,
message_text,
non_blank,
password,
username,
uuid_list,
uuid_value,
)
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s %(message)s")
log = logging.getLogger("messenger.api")
app = Flask(__name__)
app.config["MAX_CONTENT_LENGTH"] = settings.max_body_bytes
hasher = PasswordHasher()
# ---------------------------------------------------------------------------
# Response helpers and middleware
# ---------------------------------------------------------------------------
def _json(payload: object, status: int = 200, headers: dict[str, str] | None = None) -> Response:
body = json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
response = Response(body, status=status, mimetype="application/json")
for key, value in (headers or {}).items():
response.headers[key] = value
return response
def _error_response(error: ApiError, request_id: str) -> Response:
return _json(
{
"error": {
"code": error.code,
"message": error.message,
"request_id": request_id,
}
},
error.status,
error.headers,
)
@app.before_request
def _request_context() -> None:
g.request_id = uuid.uuid4().hex
g.started_at = time.monotonic()
db.start_background_initialisation()
@app.after_request
def _response_headers(response: Response) -> Response:
response.headers["X-Request-Id"] = g.get("request_id", uuid.uuid4().hex)
response.headers["X-Instance-Id"] = settings.instance_id
response.headers["Cache-Control"] = "no-store"
return response
@app.errorhandler(ApiError)
def _handle_api_error(error: ApiError) -> Response:
return _error_response(error, g.get("request_id", uuid.uuid4().hex))
@app.errorhandler(413)
def _handle_too_large(_error: object) -> Response:
return _error_response(
ApiError(413, "payload_too_large", "Тело запроса слишком велико"),
g.get("request_id", uuid.uuid4().hex),
)
@app.errorhandler(404)
def _handle_not_found(_error: object) -> Response:
return _error_response(not_found(), g.get("request_id", uuid.uuid4().hex))
@app.errorhandler(405)
def _handle_method(_error: object) -> Response:
return _error_response(bad_request("Метод не поддерживается"), g.get("request_id", uuid.uuid4().hex))
@app.errorhandler(Exception)
def _handle_unexpected(error: Exception) -> Response:
request_id = g.get("request_id", uuid.uuid4().hex)
if isinstance(error, (psycopg.OperationalError, psycopg_pool.PoolTimeout, psycopg_pool.PoolClosed)):
log.warning("database unavailable on %s %s: %s", request.method, request.path, type(error).__name__)
return _error_response(unavailable("База данных недоступна"), request_id)
if isinstance(error, redis.RedisError):
log.warning("session store unavailable on %s %s: %s", request.method, request.path, type(error).__name__)
return _error_response(unavailable("Хранилище сессий недоступно"), request_id)
log.exception("unhandled error on %s %s", request.method, request.path)
return _error_response(
ApiError(500, "internal_error", "Внутренняя ошибка сервера"), request_id
)
# ---------------------------------------------------------------------------
# Serialisation
# ---------------------------------------------------------------------------
def format_time(value: datetime) -> str:
return value.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
def user_json(row: dict) -> dict:
return {
"id": str(row["id"]),
"username": row["username"],
"display_name": row["display_name"],
"created_at": format_time(row["created_at"]),
}
def chat_json(row: dict) -> dict:
member_ids = sorted(str(item) for item in (row.get("member_ids") or []))
return {
"id": str(row["id"]),
"title": row["title"],
"member_ids": member_ids,
"created_at": format_time(row["created_at"]),
}
def message_json(row: dict) -> dict:
return {
"id": str(row["id"]),
"chat_id": str(row["chat_id"]),
"sender_id": str(row["sender_id"]),
"text": row["text"],
"created_at": format_time(row["created_at"]),
}
# ---------------------------------------------------------------------------
# Authentication
# ---------------------------------------------------------------------------
def _bearer_token() -> str:
header = request.headers.get("Authorization", "")
parts = header.split(None, 1)
if len(parts) != 2 or parts[0].lower() != "bearer" or not parts[1].strip():
raise unauthorized()
return parts[1].strip()
def current_user() -> dict:
cached = g.get("current_user")
if cached is not None:
return cached
token = _bearer_token()
user_id, token_digest = sessions.resolve(token)
if not user_id:
raise unauthorized()
try:
parsed = uuid.UUID(user_id)
except ValueError as exc:
raise unauthorized() from exc
with db.pool().connection() as conn:
row = conn.execute(
"SELECT id, username, display_name, created_at FROM users WHERE id = %s",
(parsed,),
).fetchone()
if row is None:
sessions.revoke(token_digest)
raise unauthorized()
g.current_user = row
g.session_token_digest = token_digest
return row
def _basic_credentials() -> tuple[str, str]:
header = request.headers.get("Authorization", "")
parts = header.split(None, 1)
if len(parts) != 2 or parts[0].lower() != "basic":
raise unauthorized(basic=True)
import base64
try:
decoded = base64.b64decode(parts[1].strip(), validate=True).decode("utf-8")
except (ValueError, UnicodeError) as exc:
raise unauthorized(basic=True) from exc
name, separator, secret = decoded.partition(":")
if not separator or not name or not secret:
raise unauthorized(basic=True)
if len(name) > 32 or any(ord(char) > 127 for char in name):
raise unauthorized(basic=True)
return name, secret
def _authenticate_basic() -> dict:
name, secret = _basic_credentials()
with db.pool().connection() as conn:
row = conn.execute(
"SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = %s",
(name,),
).fetchone()
if row is None:
raise unauthorized(basic=True)
try:
hasher.verify(row["password_hash"], secret)
except (VerifyMismatchError, InvalidHashError):
raise unauthorized(basic=True)
return row
# ---------------------------------------------------------------------------
# Health
# ---------------------------------------------------------------------------
@app.get("/health/live")
def health_live() -> Response:
return _json({"status": "ok"})
@app.get("/health/ready")
def health_ready() -> Response:
if not db.is_migrated():
raise unavailable("База данных ещё не готова")
try:
with db.pool().connection(timeout=1.0) as conn:
conn.execute("SELECT 1")
except Exception as exc:
raise unavailable("База данных недоступна") from exc
sessions.ping()
return _json({"status": "ok"})
# ---------------------------------------------------------------------------
# Users and sessions
# ---------------------------------------------------------------------------
@app.post("/api/v1/users")
def register_user() -> Response:
body = json_object(request, settings.max_body_bytes)
exact_keys(body, ("username", "display_name", "password"))
name = username(body["username"])
shown_name = display_name(body["display_name"])
secret = password(body["password"])
user_id = uuid.uuid4()
try:
with db.pool().connection() as conn:
row = conn.execute(
"""
INSERT INTO users (id, username, display_name, password_hash)
VALUES (%s, %s, %s, %s)
RETURNING id, username, display_name, created_at
""",
(user_id, name, shown_name, hasher.hash(secret)),
).fetchone()
except psycopg.errors.UniqueViolation as exc:
raise conflict("Username уже занят") from exc
return _json(user_json(row), 201)
@app.post("/api/v1/auth/sessions")
def create_session() -> Response:
row = _authenticate_basic()
token, expires_at = sessions.create(str(row["id"]))
return _json(
{
"token": token,
"token_type": "Bearer",
"expires_at": format_time(expires_at),
"user": user_json(row),
},
201,
)
@app.delete("/api/v1/auth/sessions/current")
def revoke_session() -> Response:
current_user() # authentication must happen before revocation
sessions.revoke(g.session_token_digest)
return Response(status=204)
@app.get("/api/v1/users/me")
def current_user_endpoint() -> Response:
return _json(user_json(current_user()))
# ---------------------------------------------------------------------------
# Chats
# ---------------------------------------------------------------------------
def _require_chat_member(chat_id: uuid.UUID, user_id: uuid.UUID) -> None:
with db.pool().connection() as conn:
row = conn.execute(
"SELECT 1 FROM chat_members WHERE chat_id = %s AND user_id = %s",
(chat_id, user_id),
).fetchone()
if row is None:
raise not_found()
@app.post("/api/v1/chats")
def create_chat() -> Response:
actor = current_user()
body = json_object(request, settings.max_body_bytes)
exact_keys(body, ("title", "member_ids"))
title = non_blank(body["title"], "title", 1, 100)
member_ids = uuid_list(body["member_ids"], "member_ids")
actor_id = str(actor["id"])
if actor_id not in member_ids:
raise bad_request("Создатель должен входить в member_ids")
member_uuids = [uuid.UUID(item) for item in member_ids]
chat_id = uuid.uuid4()
with db.pool().connection() as conn:
existing = conn.execute(
"SELECT id FROM users WHERE id = ANY(%s)", (member_uuids,)
).fetchall()
if len(existing) != len(member_uuids):
raise bad_request("Указан неизвестный участник")
row = conn.execute(
"INSERT INTO chats (id, title) VALUES (%s, %s) RETURNING id, title, created_at",
(chat_id, title),
).fetchone()
with conn.cursor() as cursor:
cursor.executemany(
"INSERT INTO chat_members (chat_id, user_id) VALUES (%s, %s)",
[(chat_id, member) for member in member_uuids],
)
payload = dict(row)
payload["member_ids"] = member_uuids
return _json(chat_json(payload), 201)
@app.get("/api/v1/chats")
def list_chats() -> Response:
actor = current_user()
limit = limit_arg(request.args.get("limit"))
cursor = decode_cursor(request.args.get("cursor"))
parameters: list[object] = [actor["id"]]
predicate = ""
if cursor is not None:
if cursor.get("kind") != "chats" or cursor.get("user_id") != str(actor["id"]):
raise bad_request("Некорректный cursor")
try:
cursor_created = cursor_time(cursor.get("created_at"))
cursor_id = uuid_value(cursor.get("id"), "cursor")
except ApiError:
raise bad_request("Некорректный cursor")
predicate = "AND (c.created_at, c.id) > (%s, %s)"
parameters.extend([cursor_created, cursor_id])
parameters.append(limit + 1)
query = f"""
SELECT c.id, c.title, c.created_at,
ARRAY(
SELECT cm.user_id FROM chat_members cm
WHERE cm.chat_id = c.id ORDER BY cm.user_id
) AS member_ids
FROM chats c
JOIN chat_members me ON me.chat_id = c.id AND me.user_id = %s
WHERE TRUE {predicate}
ORDER BY c.created_at, c.id
LIMIT %s
"""
with db.pool().connection() as conn:
rows = conn.execute(query, tuple(parameters)).fetchall()
page = rows[:limit]
next_cursor = None
if len(rows) > limit and page:
last = page[-1]
next_cursor = encode_cursor(
{
"v": 1,
"kind": "chats",
"user_id": str(actor["id"]),
"created_at": format_time(last["created_at"]),
"id": str(last["id"]),
}
)
return _json({"items": [chat_json(row) for row in page], "next_cursor": next_cursor})
@app.get("/api/v1/chats/<chat_id>")
def get_chat(chat_id: str) -> Response:
actor = current_user()
parsed_chat_id = uuid_value(chat_id, "chat_id")
with db.pool().connection() as conn:
row = conn.execute(
"""
SELECT c.id, c.title, c.created_at,
ARRAY(
SELECT cm.user_id FROM chat_members cm
WHERE cm.chat_id = c.id ORDER BY cm.user_id
) AS member_ids
FROM chats c
JOIN chat_members me ON me.chat_id = c.id AND me.user_id = %s
WHERE c.id = %s
""",
(actor["id"], parsed_chat_id),
).fetchone()
if row is None:
raise not_found()
return _json(chat_json(row))
# ---------------------------------------------------------------------------
# Messages
# ---------------------------------------------------------------------------
def _message_request_hash(text: str) -> str:
"""Canonical body fingerprint; key order and absent optional arrays are equal."""
canonical = json.dumps(
{"text": text, "attachment_ids": []},
ensure_ascii=False,
sort_keys=True,
separators=(",", ":"),
)
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
def _idempotent_message(actor: dict, chat_id: uuid.UUID, text: str, key: str) -> Response:
"""Create one message and remember its exact response atomically.
The unique constraint on (user_id, method, path, key) is the cross-replica
lock. A concurrent request with the same key waits for the first
transaction; after commit it reads the stored 201 response. If the first
transaction rolls back, the waiter inserts instead. A transient 5xx never
leaves a completed key without its message because both writes commit
together.
"""
digest = _message_request_hash(text)
path = request.path
expires_at = datetime.now(timezone.utc) + timedelta(
seconds=settings.idempotency_ttl_seconds
)
for _attempt in range(2):
with db.pool().connection() as conn:
inserted = conn.execute(
"""
INSERT INTO idempotency_keys
(user_id, method, path, key, request_hash, expires_at)
VALUES (%s, %s, %s, %s, %s, %s)
ON CONFLICT (user_id, method, path, key) DO NOTHING
RETURNING id
""",
(actor["id"], "POST", path, key, digest, expires_at),
).fetchone()
if inserted is not None:
row = conn.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, actor["id"], text),
).fetchone()
payload = message_json(row)
conn.execute(
"""
UPDATE idempotency_keys
SET status_code = 201, response_body = %s, message_id = %s
WHERE id = %s
""",
(Jsonb(payload), row["id"], inserted["id"]),
)
return _json(payload, 201)
existing = conn.execute(
"""
SELECT request_hash, status_code, response_body, expires_at
FROM idempotency_keys
WHERE user_id = %s AND method = %s AND path = %s AND key = %s
FOR UPDATE
""",
(actor["id"], "POST", path, key),
).fetchone()
if existing is None:
# The conflicting row was removed by an expiry cleanup between
# our insert and select; retry the insert once.
continue
if existing["expires_at"] <= datetime.now(timezone.utc):
conn.execute(
"""
DELETE FROM idempotency_keys
WHERE user_id = %s AND method = %s AND path = %s AND key = %s
""",
(actor["id"], "POST", path, key),
)
continue
if existing["request_hash"] != digest:
raise conflict("Idempotency-Key уже использован с другим телом")
if existing["status_code"] is None or existing["response_body"] is None:
raise conflict("Запрос с этим Idempotency-Key ещё выполняется")
return _json(existing["response_body"], existing["status_code"])
raise unavailable("Не удалось согласовать повтор запроса")
@app.post("/api/v1/chats/<chat_id>/messages")
def create_message(chat_id: str) -> Response:
actor = current_user()
parsed_chat_id = uuid_value(chat_id, "chat_id")
_require_chat_member(parsed_chat_id, actor["id"])
key = idempotency_key(request.headers.get("Idempotency-Key"))
body = json_object(request, settings.max_body_bytes)
exact_keys(body, ("text",))
text = message_text(body["text"])
return _idempotent_message(actor, parsed_chat_id, text, key)
@app.get("/api/v1/chats/<chat_id>/messages")
def list_messages(chat_id: str) -> Response:
actor = current_user()
parsed_chat_id = uuid_value(chat_id, "chat_id")
_require_chat_member(parsed_chat_id, actor["id"])
limit = limit_arg(request.args.get("limit"))
cursor = decode_cursor(request.args.get("cursor"))
parameters: list[object] = [parsed_chat_id]
predicate = ""
if cursor is not None:
if cursor.get("kind") != "messages" or cursor.get("chat_id") != str(parsed_chat_id):
raise bad_request("Некорректный cursor")
try:
cursor_created = cursor_time(cursor.get("created_at"))
cursor_id = uuid_value(cursor.get("id"), "cursor")
except ApiError:
raise bad_request("Некорректный cursor")
predicate = "AND (created_at, id) > (%s, %s)"
parameters.extend([cursor_created, cursor_id])
parameters.append(limit + 1)
query = f"""
SELECT id, chat_id, sender_id, text, created_at
FROM messages
WHERE chat_id = %s {predicate}
ORDER BY created_at, id
LIMIT %s
"""
with db.pool().connection() as conn:
rows = conn.execute(query, tuple(parameters)).fetchall()
page = rows[:limit]
next_cursor = None
if len(rows) > limit and page:
last = page[-1]
next_cursor = encode_cursor(
{
"v": 1,
"kind": "messages",
"chat_id": str(parsed_chat_id),
"created_at": format_time(last["created_at"]),
"id": str(last["id"]),
}
)
return _json({"items": [message_json(row) for row in page], "next_cursor": next_cursor})