forked from mtusi2026/mtusi2026lab2
Messages survive restart in Postgres, and bearer sessions persist in Valkey with AOF. Co-authored-by: Cursor <cursoragent@cursor.com>
474 lines
18 KiB
Python
474 lines
18 KiB
Python
#!/usr/bin/env python3
|
|
"""Messenger lab2. PostgreSQL хранит данные, Valkey хранит сессии."""
|
|
from __future__ import annotations
|
|
|
|
import base64
|
|
import json
|
|
import os
|
|
import re
|
|
import secrets
|
|
import signal
|
|
import socketserver
|
|
import sys
|
|
import threading
|
|
import uuid
|
|
from datetime import datetime, timedelta, timezone
|
|
from http.server import BaseHTTPRequestHandler
|
|
from urllib.parse import parse_qs, urlparse
|
|
|
|
import db
|
|
import passwords
|
|
import sessions
|
|
from errors import ApiError
|
|
|
|
APP_HOST = os.environ.get("APP_HOST", "0.0.0.0")
|
|
APP_PORT = int(os.environ.get("APP_PORT", "8080"))
|
|
APP_ENV = os.environ.get("APP_ENV", "lab")
|
|
|
|
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}\Z")
|
|
UUID_RE = re.compile(
|
|
r"^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-"
|
|
r"[0-9a-fA-F]{4}-[0-9a-fA-F]{12}\Z")
|
|
TIMESTAMP_RE = re.compile(
|
|
r"^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,6})?Z\Z")
|
|
MAX_BODY = 16 * 1024 * 1024
|
|
|
|
|
|
def now_utc() -> datetime:
|
|
current = datetime.now(timezone.utc)
|
|
return current.replace(microsecond=(current.microsecond // 1000) * 1000)
|
|
|
|
|
|
def is_blank(value: str) -> bool:
|
|
return value.strip() == ""
|
|
|
|
|
|
def require_object(body):
|
|
if not isinstance(body, dict):
|
|
raise ApiError(400, "invalid_request", "Тело должно быть JSON-объектом")
|
|
|
|
|
|
def reject_extra(body: dict, allowed: set):
|
|
extra = set(body) - allowed
|
|
if extra:
|
|
raise ApiError(400, "invalid_request",
|
|
"Лишние поля: " + ", ".join(sorted(extra)))
|
|
|
|
|
|
def parse_uuid(value, field: str) -> str:
|
|
if not isinstance(value, str) or not UUID_RE.match(value):
|
|
raise ApiError(400, "invalid_request", f"Некорректный UUID: {field}")
|
|
return value.lower()
|
|
|
|
|
|
def parse_display_name(value) -> str:
|
|
if not isinstance(value, str):
|
|
raise ApiError(400, "invalid_request", "display_name должен быть строкой")
|
|
if not (1 <= len(value) <= 100):
|
|
raise ApiError(400, "invalid_request", "display_name: 1..100 code points")
|
|
if is_blank(value):
|
|
raise ApiError(400, "invalid_request", "display_name не может быть пустым")
|
|
return value
|
|
|
|
|
|
def parse_username(value) -> str:
|
|
if not isinstance(value, str) or not USERNAME_RE.match(value):
|
|
raise ApiError(400, "invalid_request", "username: ^[a-z0-9_]{3,32}$")
|
|
return value
|
|
|
|
|
|
def parse_password(value) -> str:
|
|
if not isinstance(value, str):
|
|
raise ApiError(400, "invalid_request", "password должен быть строкой")
|
|
if not (12 <= len(value) <= 128):
|
|
raise ApiError(400, "invalid_request", "password: 12..128 code points")
|
|
if is_blank(value):
|
|
raise ApiError(400, "invalid_request", "password не может быть пустым")
|
|
return value
|
|
|
|
|
|
def parse_title(value) -> str:
|
|
if not isinstance(value, str):
|
|
raise ApiError(400, "invalid_request", "title должен быть строкой")
|
|
if not (1 <= len(value) <= 100):
|
|
raise ApiError(400, "invalid_request", "title: 1..100 code points")
|
|
if is_blank(value):
|
|
raise ApiError(400, "invalid_request", "title не может быть пустым")
|
|
return value
|
|
|
|
|
|
def parse_text(value) -> str:
|
|
if not isinstance(value, str):
|
|
raise ApiError(400, "invalid_request", "text должен быть строкой")
|
|
if not (1 <= len(value) <= 4000):
|
|
raise ApiError(400, "invalid_request", "text: 1..4000 code points")
|
|
if is_blank(value):
|
|
raise ApiError(400, "invalid_request", "text не может быть пустым")
|
|
return value
|
|
|
|
|
|
def parse_member_ids(value) -> list:
|
|
if not isinstance(value, list):
|
|
raise ApiError(400, "invalid_request", "member_ids должен быть массивом")
|
|
if not (1 <= len(value) <= 100):
|
|
raise ApiError(400, "invalid_request", "member_ids: 1..100")
|
|
seen = set()
|
|
result = []
|
|
for item in value:
|
|
uid = parse_uuid(item, "member_ids[]")
|
|
if uid in seen:
|
|
raise ApiError(400, "invalid_request", "member_ids содержит дубликаты")
|
|
seen.add(uid)
|
|
result.append(uid)
|
|
return result
|
|
|
|
|
|
def parse_limit(raw, default=50) -> int:
|
|
if raw is None:
|
|
return default
|
|
if isinstance(raw, str) and raw.strip() == "":
|
|
raise ApiError(400, "invalid_request", "limit должен быть целым")
|
|
try:
|
|
value = int(raw)
|
|
except (TypeError, ValueError):
|
|
raise ApiError(400, "invalid_request", "limit должен быть целым")
|
|
if not (1 <= value <= 100):
|
|
raise ApiError(400, "invalid_request", "limit: 1..100")
|
|
return value
|
|
|
|
|
|
def parse_optional_cursor(raw):
|
|
if raw is None:
|
|
return None
|
|
if not isinstance(raw, str) or raw.strip() == "":
|
|
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
|
return raw
|
|
|
|
|
|
def parse_cursor_timestamp(value: str) -> datetime:
|
|
if not isinstance(value, str) or not TIMESTAMP_RE.match(value):
|
|
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
|
try:
|
|
parsed = datetime.fromisoformat(value.replace("Z", "+00:00"))
|
|
except ValueError as exc:
|
|
raise ApiError(400, "invalid_request", "Некорректный cursor") from exc
|
|
return parsed
|
|
|
|
|
|
def challenge_for(path: str) -> str:
|
|
if path == "/api/v1/auth/sessions":
|
|
return 'Basic realm="messenger", charset="UTF-8"'
|
|
return 'Bearer realm="messenger"'
|
|
|
|
|
|
def bearer_token(headers) -> str:
|
|
auth = headers.get("Authorization") or ""
|
|
parts = auth.split(None, 1)
|
|
if len(parts) != 2 or parts[0].lower() != "bearer" or not parts[1].strip():
|
|
raise ApiError(401, "unauthorized", "Требуется Bearer")
|
|
return parts[1].strip()
|
|
|
|
|
|
def current_user(headers) -> dict:
|
|
token = bearer_token(headers)
|
|
user = db.get_user(sessions.user_id(token))
|
|
if user is None:
|
|
raise ApiError(401, "unauthorized", "Сессия недействительна")
|
|
return user
|
|
|
|
|
|
def basic_credentials(headers) -> tuple[str, str]:
|
|
auth = headers.get("Authorization") or ""
|
|
parts = auth.split(None, 1)
|
|
if len(parts) != 2 or parts[0].lower() != "basic" or not parts[1].strip():
|
|
raise ApiError(401, "unauthorized", "Требуется Basic")
|
|
try:
|
|
decoded = base64.b64decode(parts[1].strip(), validate=True).decode("utf-8")
|
|
except (ValueError, UnicodeDecodeError) as exc:
|
|
raise ApiError(401, "unauthorized", "Неверные учётные данные") from exc
|
|
if ":" not in decoded:
|
|
raise ApiError(401, "unauthorized", "Неверные учётные данные")
|
|
username, password = decoded.split(":", 1)
|
|
return username, password
|
|
|
|
|
|
def encode_cursor(chat_id: str, created_at: str, msg_id: str) -> str:
|
|
raw = f"{chat_id}|{created_at}|{msg_id}".encode("utf-8")
|
|
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
|
|
|
|
|
|
def decode_cursor(cursor: str, chat_id: str):
|
|
pad = "=" * (-len(cursor) % 4)
|
|
try:
|
|
raw = base64.urlsafe_b64decode(cursor + pad).decode("utf-8")
|
|
except Exception as exc:
|
|
raise ApiError(400, "invalid_request", "Некорректный cursor") from exc
|
|
parts = raw.split("|")
|
|
if len(parts) != 3:
|
|
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
|
c_chat, c_created, c_id = parts
|
|
if not UUID_RE.match(c_chat):
|
|
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
|
if c_chat.lower() != chat_id:
|
|
raise ApiError(400, "invalid_request", "cursor от другого чата")
|
|
return parse_cursor_timestamp(c_created), parse_uuid(c_id, "cursor")
|
|
|
|
|
|
def encode_chat_cursor(created_at: str, chat_id: str) -> str:
|
|
raw = f"chat|{created_at}|{chat_id}".encode("utf-8")
|
|
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
|
|
|
|
|
|
def decode_chat_cursor(cursor: str):
|
|
pad = "=" * (-len(cursor) % 4)
|
|
try:
|
|
raw = base64.urlsafe_b64decode(cursor + pad).decode("utf-8")
|
|
except Exception as exc:
|
|
raise ApiError(400, "invalid_request", "Некорректный cursor") from exc
|
|
parts = raw.split("|")
|
|
if len(parts) != 3 or parts[0] != "chat":
|
|
raise ApiError(400, "invalid_request", "Некорректный cursor")
|
|
return parse_cursor_timestamp(parts[1]), parse_uuid(parts[2], "cursor")
|
|
|
|
|
|
def create_user(body) -> dict:
|
|
require_object(body)
|
|
reject_extra(body, {"username", "display_name", "password"})
|
|
username = parse_username(body.get("username"))
|
|
display_name = parse_display_name(body.get("display_name"))
|
|
password = parse_password(body.get("password"))
|
|
return db.create_user(username, display_name, passwords.hash_password(password), now_utc())
|
|
|
|
|
|
def create_session(headers) -> dict:
|
|
username, password = basic_credentials(headers)
|
|
row = None
|
|
try:
|
|
row = db.user_for_login(username)
|
|
except ApiError:
|
|
raise
|
|
stored = row[3] if row is not None else passwords.DUMMY_HASH
|
|
password_ok = passwords.verify_password(password, stored)
|
|
if row is None or not password_ok:
|
|
raise ApiError(401, "unauthorized", "Неверные учётные данные")
|
|
token = secrets.token_urlsafe(32)
|
|
ttl = sessions.session_ttl()
|
|
expires = now_utc() + timedelta(seconds=ttl)
|
|
sessions.create(token, str(row[0]), ttl)
|
|
return {
|
|
"token": token,
|
|
"token_type": "Bearer",
|
|
"expires_at": db.format_ts(expires),
|
|
"user": db.user_json((row[0], row[1], row[2], row[4])),
|
|
}
|
|
|
|
|
|
def logout(headers) -> None:
|
|
token = bearer_token(headers)
|
|
sessions.user_id(token)
|
|
sessions.revoke(token)
|
|
|
|
|
|
def create_chat(body, actor: dict) -> dict:
|
|
require_object(body)
|
|
reject_extra(body, {"title", "member_ids"})
|
|
title = parse_title(body.get("title"))
|
|
member_ids = parse_member_ids(body.get("member_ids"))
|
|
if actor["id"] not in member_ids:
|
|
raise ApiError(400, "invalid_request", "вызывающий должен быть в member_ids")
|
|
return db.create_chat(title, member_ids, now_utc())
|
|
|
|
|
|
def list_chats(actor: dict, limit: int, cursor) -> dict:
|
|
decoded = decode_chat_cursor(cursor) if cursor else None
|
|
page = db.list_chats(actor["id"], limit, decoded)
|
|
next_cursor = None
|
|
if page["next_cursor_parts"] is not None:
|
|
created, chat_id = page["next_cursor_parts"]
|
|
next_cursor = encode_chat_cursor(db.format_ts(created), chat_id)
|
|
return {"items": page["items"], "next_cursor": next_cursor}
|
|
|
|
|
|
def create_message(chat_id: str, body, actor: dict) -> dict:
|
|
cid = parse_uuid(chat_id, "chat_id")
|
|
require_object(body)
|
|
reject_extra(body, {"text"})
|
|
text = parse_text(body.get("text"))
|
|
return db.create_message(cid, actor["id"], text, now_utc())
|
|
|
|
|
|
def list_messages(chat_id: str, actor: dict, limit: int, cursor) -> dict:
|
|
cid = parse_uuid(chat_id, "chat_id")
|
|
decoded = decode_cursor(cursor, cid) if cursor else None
|
|
page = db.list_messages(cid, actor["id"], limit, decoded)
|
|
next_cursor = None
|
|
if page["next_cursor_parts"] is not None:
|
|
created, message_id = page["next_cursor_parts"]
|
|
next_cursor = encode_cursor(cid, db.format_ts(created), message_id)
|
|
return {"items": page["items"], "next_cursor": next_cursor}
|
|
|
|
|
|
class FastThreadingHTTPServer(socketserver.ThreadingMixIn, socketserver.TCPServer):
|
|
daemon_threads = True
|
|
block_on_close = False
|
|
allow_reuse_address = True
|
|
request_queue_size = 256
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
server_version = "mtusi-messenger/2.0"
|
|
protocol_version = "HTTP/1.0"
|
|
|
|
def log_message(self, fmt, *args):
|
|
return
|
|
|
|
def _request_id(self) -> str:
|
|
return self.headers.get("X-Request-Id") or str(uuid.uuid4())
|
|
|
|
def _send_json(self, status: int, payload, request_id: str, extra_headers=None):
|
|
body = b""
|
|
if status != 204:
|
|
body = json.dumps(payload, ensure_ascii=False).encode("utf-8")
|
|
self.send_response(status)
|
|
if status != 204:
|
|
self.send_header("Content-Type", "application/json; charset=utf-8")
|
|
self.send_header("Content-Length", str(len(body)))
|
|
self.send_header("X-Request-Id", request_id)
|
|
for key, value in (extra_headers or {}).items():
|
|
self.send_header(key, value)
|
|
self.end_headers()
|
|
if body:
|
|
self.wfile.write(body)
|
|
|
|
def _send_error(self, status: int, code: str, message: str, request_id: str, extra_headers=None):
|
|
self._send_json(
|
|
status,
|
|
{"error": {"code": code, "message": message, "request_id": request_id}},
|
|
request_id,
|
|
extra_headers,
|
|
)
|
|
|
|
def _read_body(self):
|
|
length = self.headers.get("Content-Length")
|
|
if not length:
|
|
return None
|
|
try:
|
|
size = int(length)
|
|
except ValueError as exc:
|
|
raise ApiError(400, "invalid_request", "Некорректный Content-Length") from exc
|
|
if size > MAX_BODY:
|
|
raise ApiError(413, "payload_too_large", "Тело слишком большое")
|
|
raw = self.rfile.read(size)
|
|
ctype = (self.headers.get("Content-Type") or "").split(";")[0].strip()
|
|
if ctype and ctype != "application/json":
|
|
raise ApiError(415, "unsupported_media_type", "Ожидается application/json")
|
|
if not raw:
|
|
raise ApiError(400, "invalid_request", "Пустое тело")
|
|
try:
|
|
return json.loads(raw.decode("utf-8"))
|
|
except (UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
raise ApiError(400, "invalid_request", "Malformed JSON") from exc
|
|
|
|
def do_GET(self):
|
|
self._dispatch("GET")
|
|
|
|
def do_POST(self):
|
|
self._dispatch("POST")
|
|
|
|
def do_DELETE(self):
|
|
self._dispatch("DELETE")
|
|
|
|
def _dispatch(self, method: str):
|
|
rid = self._request_id()
|
|
parsed = urlparse(self.path)
|
|
path = parsed.path.rstrip("/") or "/"
|
|
query = parse_qs(parsed.query, keep_blank_values=True)
|
|
try:
|
|
self._route(method, path, query, rid)
|
|
except ApiError as exc:
|
|
headers = {}
|
|
if exc.status == 401:
|
|
headers["WWW-Authenticate"] = challenge_for(path)
|
|
self._send_error(exc.status, exc.code, exc.message, rid, headers)
|
|
except BrokenPipeError:
|
|
pass
|
|
except Exception:
|
|
sys.stderr.write("internal error on %s %s\n" % (method, path))
|
|
self._send_error(500, "internal_error", "Внутренняя ошибка", rid)
|
|
|
|
def _route(self, method: str, path: str, query: dict, rid: str):
|
|
if method == "GET" and path == "/health/live":
|
|
return self._send_json(200, {"status": "ok"}, rid)
|
|
if method == "GET" and path == "/health/ready":
|
|
if db.ping() and sessions.ping():
|
|
return self._send_json(200, {"status": "ok"}, rid)
|
|
raise ApiError(503, "unavailable", "Зависимость недоступна")
|
|
|
|
if method == "POST" and path == "/api/v1/users":
|
|
return self._send_json(201, create_user(self._read_body()), rid)
|
|
if method == "POST" and path == "/api/v1/auth/sessions":
|
|
return self._send_json(201, create_session(self.headers), rid)
|
|
if method == "DELETE" and path == "/api/v1/auth/sessions/current":
|
|
logout(self.headers)
|
|
return self._send_json(204, None, rid)
|
|
if method == "GET" and path == "/api/v1/users/me":
|
|
return self._send_json(200, current_user(self.headers), rid)
|
|
|
|
if method == "POST" and path == "/api/v1/chats":
|
|
actor = current_user(self.headers)
|
|
return self._send_json(201, create_chat(self._read_body(), actor), rid)
|
|
if method == "GET" and path == "/api/v1/chats":
|
|
actor = current_user(self.headers)
|
|
limit = parse_limit(query.get("limit", [None])[0])
|
|
cursor = parse_optional_cursor(query.get("cursor", [None])[0])
|
|
return self._send_json(200, list_chats(actor, limit, cursor), rid)
|
|
|
|
match = re.fullmatch(r"/api/v1/chats/([^/]+)", path)
|
|
if match and method == "GET":
|
|
actor = current_user(self.headers)
|
|
return self._send_json(200, db.get_chat(parse_uuid(match.group(1), "chat_id"), actor["id"]), rid)
|
|
|
|
match = re.fullmatch(r"/api/v1/chats/([^/]+)/messages", path)
|
|
if match and method == "POST":
|
|
actor = current_user(self.headers)
|
|
return self._send_json(201, create_message(match.group(1), self._read_body(), actor), rid)
|
|
if match and method == "GET":
|
|
actor = current_user(self.headers)
|
|
limit = parse_limit(query.get("limit", [None])[0])
|
|
cursor = parse_optional_cursor(query.get("cursor", [None])[0])
|
|
return self._send_json(200, list_messages(match.group(1), actor, limit, cursor), rid)
|
|
|
|
raise ApiError(404, "not_found", "Маршрут не найден")
|
|
|
|
|
|
def main():
|
|
try:
|
|
db.init()
|
|
db.migrate_with_retry()
|
|
except Exception:
|
|
sys.stderr.write("database is not ready at startup\n")
|
|
try:
|
|
sessions.init()
|
|
except Exception:
|
|
sys.stderr.write("session store is not configured\n")
|
|
|
|
server = FastThreadingHTTPServer((APP_HOST, APP_PORT), Handler)
|
|
|
|
def shutdown(signum, frame):
|
|
sys.stderr.write("SIGTERM received, shutting down\n")
|
|
threading.Thread(target=server.shutdown, daemon=True).start()
|
|
|
|
signal.signal(signal.SIGTERM, shutdown)
|
|
signal.signal(signal.SIGINT, shutdown)
|
|
sys.stderr.write("listening on %s:%s env=%s\n" % (APP_HOST, APP_PORT, APP_ENV))
|
|
try:
|
|
server.serve_forever(poll_interval=0.05)
|
|
finally:
|
|
server.server_close()
|
|
db.close()
|
|
sys.stderr.write("stopped\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|