forked from mtusi2026/mtusi2026lab4
584 lines
19 KiB
Python
584 lines
19 KiB
Python
import base64
|
||
import binascii
|
||
import hashlib
|
||
import json
|
||
import logging
|
||
import re
|
||
import threading
|
||
import time
|
||
import uuid
|
||
from contextlib import asynccontextmanager
|
||
from datetime import datetime
|
||
from pathlib import Path
|
||
from typing import Literal
|
||
from urllib.parse import quote
|
||
from uuid import UUID
|
||
|
||
import uvicorn
|
||
from fastapi import FastAPI, Header, Query, Request, Response
|
||
from fastapi.exceptions import RequestValidationError
|
||
from fastapi.responses import JSONResponse, StreamingResponse
|
||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||
from starlette.concurrency import run_in_threadpool
|
||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||
|
||
import attachments
|
||
import auth
|
||
import db
|
||
import settings
|
||
import storage
|
||
import uploads
|
||
from errors import ApiError, bad_request, unauthorized, unavailable
|
||
|
||
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s")
|
||
log = logging.getLogger("messenger")
|
||
|
||
DRAINING = threading.Event()
|
||
|
||
|
||
class Server(uvicorn.Server):
|
||
"""Остановка по SIGTERM в два шага.
|
||
|
||
1. /health/ready сразу отвечает 503, а запросы ещё обслуживаются. За SHUTDOWN_DRAIN_SECONDS
|
||
HAProxy видит 503 (проверка раз в 0,5-1 с, 2 неудачи) и перестаёт слать сюда новые запросы.
|
||
2. Обычная остановка uvicorn: закрыть приём соединений и доделать начатые запросы
|
||
(не дольше SHUTDOWN_TIMEOUT_SECONDS). Без шага 1 запрос, пришедший в момент SIGTERM,
|
||
получал бы 502 от балансировщика.
|
||
Повторный сигнал останавливает сразу.
|
||
"""
|
||
|
||
def handle_exit(self, sig, frame):
|
||
if DRAINING.is_set():
|
||
return super().handle_exit(sig, frame)
|
||
DRAINING.set()
|
||
log.info("Signal %s: readiness -> 503, leaving the balancer, stop in %s s",
|
||
sig, settings.SHUTDOWN_DRAIN_SECONDS)
|
||
threading.Timer(settings.SHUTDOWN_DRAIN_SECONDS, super().handle_exit, (sig, frame)).start()
|
||
|
||
|
||
@asynccontextmanager
|
||
async def lifespan(app: FastAPI):
|
||
settings.check()
|
||
# Миграции применяет сервис migrate (migrate.py); реплика только ждёт готовую схему.
|
||
await run_in_threadpool(db.wait_for_schema, settings.SCHEMA_WAIT_SECONDS)
|
||
# Только для временных файлов текущих загрузок (удаляются в конце запроса); оригиналы живут в S3.
|
||
Path(settings.UPLOAD_TMP_DIR).mkdir(parents=True, exist_ok=True)
|
||
db.pool.open()
|
||
log.info("Started: instance=%s, version=%s, env=%s, session TTL=%s s, sessions in Redis, files in S3 bucket %s",
|
||
settings.INSTANCE_ID, settings.APP_VERSION, settings.APP_ENV, settings.SESSION_TTL_SECONDS,
|
||
settings.S3_BUCKET)
|
||
yield
|
||
db.pool.close()
|
||
auth.close()
|
||
log.info("Stopped: connections closed, data stays in PostgreSQL and Redis")
|
||
|
||
|
||
app = FastAPI(lifespan=lifespan)
|
||
|
||
|
||
USERNAME_RE = re.compile(r"[a-z0-9_]{3,32}")
|
||
IDEMPOTENCY_KEY_RE = re.compile(r"[A-Za-z0-9._:-]{1,128}")
|
||
|
||
|
||
class CreateUser(BaseModel):
|
||
model_config = ConfigDict(extra="forbid")
|
||
|
||
username: str = Field(pattern=r"^[a-z0-9_]{3,32}$")
|
||
display_name: str = Field(min_length=1, max_length=100)
|
||
password: str = Field(min_length=auth.PASSWORD_MIN_LENGTH, max_length=auth.PASSWORD_MAX_LENGTH)
|
||
|
||
@field_validator("display_name", "password")
|
||
@classmethod
|
||
def validate_not_blank(cls, value: str) -> str:
|
||
if not value.strip():
|
||
raise ValueError("value cannot be blank")
|
||
return value
|
||
|
||
|
||
class User(BaseModel):
|
||
id: UUID
|
||
username: str
|
||
display_name: str
|
||
created_at: datetime
|
||
|
||
|
||
class Session(BaseModel):
|
||
token: str
|
||
token_type: str
|
||
expires_at: datetime
|
||
user: User
|
||
|
||
|
||
class CreateChat(BaseModel):
|
||
model_config = ConfigDict(extra="forbid")
|
||
|
||
title: str = Field(min_length=1, max_length=100)
|
||
member_ids: list[UUID] = Field(min_length=1, max_length=100)
|
||
|
||
@field_validator("title")
|
||
@classmethod
|
||
def validate_title(cls, value: str) -> str:
|
||
if not value.strip():
|
||
raise ValueError("title cannot be empty")
|
||
return value
|
||
|
||
|
||
class Chat(BaseModel):
|
||
id: UUID
|
||
title: str
|
||
member_ids: list[UUID]
|
||
created_at: datetime
|
||
|
||
|
||
class ChatPage(BaseModel):
|
||
items: list[Chat]
|
||
next_cursor: str | None
|
||
|
||
|
||
class CreateMessage(BaseModel):
|
||
model_config = ConfigDict(extra="forbid")
|
||
|
||
text: str = Field(max_length=4000)
|
||
# С lab4: 0-10 уникальных вложений этого чата; пустой текст допустим только с вложением.
|
||
attachment_ids: list[UUID] = Field(default_factory=list, max_length=10)
|
||
|
||
@model_validator(mode="after")
|
||
def validate_content(self):
|
||
if len(set(self.attachment_ids)) != len(self.attachment_ids):
|
||
raise ValueError("attachment_ids must be unique")
|
||
if not self.attachment_ids and not self.text.strip():
|
||
raise ValueError("text cannot be empty without attachments")
|
||
return self
|
||
|
||
|
||
class Message(BaseModel):
|
||
id: UUID
|
||
chat_id: UUID
|
||
sender_id: UUID
|
||
text: str
|
||
created_at: datetime
|
||
attachment_ids: list[UUID]
|
||
|
||
|
||
class Variant(BaseModel):
|
||
name: str
|
||
content_type: str
|
||
size_bytes: int
|
||
width: int
|
||
height: int
|
||
sha256: str
|
||
|
||
|
||
class Attachment(BaseModel):
|
||
id: UUID
|
||
chat_id: UUID
|
||
owner_id: UUID
|
||
filename: str
|
||
content_type: str
|
||
size_bytes: int
|
||
sha256: str
|
||
status: Literal["ready"]
|
||
variants: list[Variant]
|
||
error_code: str | None
|
||
created_at: datetime
|
||
updated_at: datetime
|
||
|
||
|
||
class MessagePage(BaseModel):
|
||
items: list[Message]
|
||
next_cursor: str | None
|
||
|
||
|
||
def error_response(
|
||
request: Request,
|
||
code: str,
|
||
message: str,
|
||
status_code: int,
|
||
headers: dict | None = None,
|
||
):
|
||
request_id = request.state.request_id
|
||
|
||
return JSONResponse(
|
||
status_code=status_code,
|
||
content={
|
||
"error": {
|
||
"code": code,
|
||
"message": message,
|
||
"request_id": request_id,
|
||
}
|
||
},
|
||
headers={"X-Request-Id": request_id, **(headers or {})},
|
||
)
|
||
|
||
|
||
@app.exception_handler(ApiError)
|
||
async def api_error_handler(request: Request, exc: ApiError):
|
||
return error_response(request, exc.code, exc.message, exc.status_code, exc.headers)
|
||
|
||
|
||
@app.exception_handler(RequestValidationError)
|
||
async def validation_exception_handler(
|
||
request: Request,
|
||
exc: RequestValidationError,
|
||
):
|
||
return error_response(
|
||
request,
|
||
"invalid_request",
|
||
"Invalid request data",
|
||
400,
|
||
)
|
||
|
||
|
||
@app.exception_handler(StarletteHTTPException)
|
||
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
||
if exc.status_code == 404:
|
||
return error_response(request, "not_found", "Not found", 404)
|
||
return error_response(request, "invalid_request", "Request cannot be processed", exc.status_code, exc.headers)
|
||
|
||
|
||
@app.exception_handler(Exception)
|
||
async def general_exception_handler(
|
||
request: Request,
|
||
exc: Exception,
|
||
):
|
||
return error_response(
|
||
request,
|
||
"internal_error",
|
||
"Internal server error",
|
||
500,
|
||
)
|
||
|
||
PUBLIC_ENDPOINTS = {
|
||
("POST", "/api/v1/users"),
|
||
("POST", "/api/v1/auth/sessions"),
|
||
}
|
||
|
||
|
||
@app.middleware("http")
|
||
async def request_context(request: Request, call_next):
|
||
request_id = str(uuid.uuid4())
|
||
request.state.request_id = request_id
|
||
started = time.perf_counter()
|
||
path = request.url.path
|
||
|
||
try:
|
||
if path.startswith("/api/") and (request.method, path) not in PUBLIC_ENDPOINTS:
|
||
request.state.session = await run_in_threadpool(
|
||
auth.check_bearer, request.headers.get("Authorization")
|
||
)
|
||
response = await call_next(request)
|
||
except ApiError as exc:
|
||
response = error_response(request, exc.code, exc.message, exc.status_code, exc.headers)
|
||
except Exception:
|
||
log.exception("Unhandled error, request_id=%s", request_id)
|
||
response = error_response(request, "internal_error", "Internal server error", 500)
|
||
|
||
response.headers["X-Request-Id"] = request_id
|
||
response.headers["X-Instance-Id"] = settings.INSTANCE_ID # какая реплика ответила
|
||
# Проверки здоровья приходят каждую секунду от HAProxy и Docker, в лог пишем только ошибки.
|
||
level = logging.DEBUG if path.startswith("/health/") and response.status_code == 200 else logging.INFO
|
||
log.log(
|
||
level,
|
||
"%s %s -> %s (%.1f ms) request_id=%s",
|
||
request.method, path, response.status_code,
|
||
(time.perf_counter() - started) * 1000, request_id,
|
||
)
|
||
return response
|
||
|
||
|
||
def current_user_id(request: Request) -> UUID:
|
||
session = getattr(request.state, "session", None)
|
||
if session is None:
|
||
raise unauthorized("Bearer token is required")
|
||
return session.user_id
|
||
|
||
|
||
def encode_cursor(
|
||
created_at: datetime,
|
||
object_id: UUID,
|
||
*,
|
||
chat_id: UUID | None = None,
|
||
user_id: UUID | None = None,
|
||
) -> str:
|
||
data = {
|
||
"created_at": created_at.isoformat(),
|
||
"id": str(object_id),
|
||
}
|
||
|
||
if chat_id is not None:
|
||
data["chat_id"] = str(chat_id)
|
||
|
||
if user_id is not None:
|
||
data["user_id"] = str(user_id)
|
||
|
||
raw = json.dumps(data).encode("utf-8")
|
||
return base64.urlsafe_b64encode(raw).decode("ascii")
|
||
|
||
|
||
def decode_cursor(cursor: str):
|
||
try:
|
||
raw = base64.urlsafe_b64decode(cursor.encode("ascii"))
|
||
data = json.loads(raw.decode("utf-8"))
|
||
|
||
created_at = datetime.fromisoformat(data["created_at"])
|
||
|
||
if created_at.tzinfo is None:
|
||
raise ValueError("Invalid cursor")
|
||
|
||
if not isinstance(data["id"], str):
|
||
raise ValueError("Invalid cursor")
|
||
|
||
object_id = UUID(data["id"])
|
||
|
||
chat_id = UUID(data["chat_id"]) if "chat_id" in data else None
|
||
user_id = UUID(data["user_id"]) if "user_id" in data else None
|
||
|
||
return created_at, object_id, chat_id, user_id
|
||
|
||
except (
|
||
ValueError,
|
||
KeyError,
|
||
TypeError,
|
||
AttributeError,
|
||
json.JSONDecodeError,
|
||
binascii.Error,
|
||
):
|
||
raise ValueError("Invalid cursor")
|
||
|
||
|
||
def page_after(cursor: str | None, *, chat_id: UUID | None = None, user_id: UUID | None = None):
|
||
if cursor is None:
|
||
return None
|
||
try:
|
||
created_at, object_id, cursor_chat_id, cursor_user_id = decode_cursor(cursor)
|
||
except ValueError:
|
||
raise bad_request("Invalid cursor") from None
|
||
if cursor_chat_id != chat_id or cursor_user_id != user_id:
|
||
raise bad_request("Invalid cursor")
|
||
return created_at, object_id
|
||
|
||
|
||
@app.get("/health/live")
|
||
def health_live():
|
||
return {"status": "ok"}
|
||
|
||
|
||
@app.get("/health/ready")
|
||
def health_ready():
|
||
if DRAINING.is_set():
|
||
raise unavailable("Instance is shutting down")
|
||
db.ping()
|
||
auth.ping_redis()
|
||
return {"status": "ok"}
|
||
|
||
|
||
@app.post("/api/v1/users", response_model=User, status_code=201)
|
||
def create_user(user_data: CreateUser):
|
||
password_hash = auth.hash_password(user_data.password) # сам пароль нигде не сохраняется
|
||
return db.create_user(user_data.username, user_data.display_name, password_hash)
|
||
|
||
|
||
@app.post("/api/v1/auth/sessions", response_model=Session, status_code=201)
|
||
def create_session(
|
||
response: Response,
|
||
authorization: str | None = Header(default=None),
|
||
):
|
||
credentials = auth.parse_basic(authorization)
|
||
if credentials is None:
|
||
raise auth.login_failed()
|
||
username, password = credentials
|
||
|
||
found = None
|
||
if USERNAME_RE.fullmatch(username) and len(password) <= auth.PASSWORD_MAX_LENGTH:
|
||
found = db.get_user_with_password(username)
|
||
if not auth.password_matches(found["password_hash"] if found else None, password):
|
||
raise auth.login_failed()
|
||
|
||
token, expires_at = auth.create_session(found["id"])
|
||
response.headers["Cache-Control"] = "no-store"
|
||
user = {key: found[key] for key in ("id", "username", "display_name", "created_at")}
|
||
return {"token": token, "token_type": "Bearer", "expires_at": expires_at, "user": user}
|
||
|
||
|
||
@app.delete("/api/v1/auth/sessions/current", status_code=204)
|
||
def delete_session(request: Request):
|
||
current_user_id(request)
|
||
auth.delete_session(request.state.session)
|
||
return Response(status_code=204)
|
||
|
||
|
||
@app.get("/api/v1/users/me", response_model=User)
|
||
def get_current_user(request: Request):
|
||
user = db.get_user(current_user_id(request))
|
||
if user is None:
|
||
raise unauthorized("Session is invalid or expired")
|
||
return user
|
||
|
||
|
||
|
||
@app.post("/api/v1/chats", response_model=Chat, status_code=201)
|
||
def create_chat(request: Request, chat_data: CreateChat):
|
||
user_id = current_user_id(request)
|
||
|
||
if user_id not in chat_data.member_ids:
|
||
raise bad_request("Creator must be a chat member")
|
||
|
||
if len(chat_data.member_ids) != len(set(chat_data.member_ids)):
|
||
raise bad_request("Duplicate member IDs")
|
||
return db.create_chat(chat_data.title, chat_data.member_ids)
|
||
|
||
|
||
@app.get("/api/v1/chats", response_model=ChatPage)
|
||
def list_chats(
|
||
request: Request,
|
||
limit: int = Query(default=50, ge=1, le=100),
|
||
cursor: str | None = Query(default=None),
|
||
):
|
||
user_id = current_user_id(request)
|
||
after = page_after(cursor, user_id=user_id)
|
||
|
||
rows = db.list_chats(user_id, after, limit)
|
||
items = rows[:limit]
|
||
|
||
next_cursor = None
|
||
if len(rows) > limit:
|
||
last = items[-1]
|
||
next_cursor = encode_cursor(last["created_at"], last["id"], user_id=user_id)
|
||
|
||
return ChatPage(items=items, next_cursor=next_cursor)
|
||
|
||
|
||
@app.get("/api/v1/chats/{chat_id}", response_model=Chat)
|
||
def get_chat(request: Request, chat_id: UUID):
|
||
return db.get_chat(chat_id, current_user_id(request))
|
||
|
||
|
||
def request_fingerprint(data: BaseModel) -> bytes:
|
||
"""SHA-256 тела после разбора: порядок ключей и пробелы в JSON на него не влияют.
|
||
|
||
Пустой attachment_ids не входит в отпечаток: {"text": "x"} и {"text": "x", "attachment_ids": []}
|
||
равны (контракт), и хеши сообщений, сохранённых до lab4, остаются прежними.
|
||
"""
|
||
body = data.model_dump(mode="json")
|
||
if not body.get("attachment_ids"):
|
||
body.pop("attachment_ids", None)
|
||
canonical = json.dumps(body, sort_keys=True, ensure_ascii=False, separators=(",", ":"))
|
||
return hashlib.sha256(canonical.encode("utf-8")).digest()
|
||
|
||
|
||
@app.post(
|
||
"/api/v1/chats/{chat_id}/messages",
|
||
response_model=Message,
|
||
status_code=201,
|
||
)
|
||
def create_message(
|
||
request: Request,
|
||
message_data: CreateMessage,
|
||
chat_id: UUID,
|
||
idempotency_key: str | None = Header(default=None),
|
||
):
|
||
# С lab3 ключ обязателен: повтор того же запроса (после таймаута, через другую реплику)
|
||
# вернёт то же сообщение, а не создаст второе. См. db.add_message.
|
||
if idempotency_key is None or not IDEMPOTENCY_KEY_RE.fullmatch(idempotency_key):
|
||
raise bad_request("Idempotency-Key header is required: 1-128 characters A-Z a-z 0-9 . _ : -")
|
||
return db.add_message(chat_id, current_user_id(request), message_data.text, message_data.attachment_ids,
|
||
idempotency_key, request_fingerprint(message_data))
|
||
|
||
|
||
@app.get(
|
||
"/api/v1/chats/{chat_id}/messages",
|
||
response_model=MessagePage,
|
||
)
|
||
def list_messages(
|
||
request: Request,
|
||
chat_id: UUID,
|
||
limit: int = Query(default=50, ge=1, le=100),
|
||
cursor: str | None = Query(default=None),
|
||
):
|
||
user_id = current_user_id(request)
|
||
after = page_after(cursor, chat_id=chat_id)
|
||
|
||
rows = db.list_messages(chat_id, user_id, after, limit)
|
||
items = rows[:limit]
|
||
|
||
next_cursor = None
|
||
if len(rows) > limit:
|
||
last = items[-1]
|
||
next_cursor = encode_cursor(last["created_at"], last["id"], chat_id=chat_id)
|
||
|
||
return MessagePage(items=items, next_cursor=next_cursor)
|
||
|
||
|
||
@app.post(
|
||
"/api/v1/chats/{chat_id}/attachments",
|
||
response_model=Attachment,
|
||
status_code=201,
|
||
)
|
||
async def upload_attachment(
|
||
request: Request,
|
||
chat_id: UUID,
|
||
filename: str | None = Query(default=None),
|
||
):
|
||
"""Синхронная загрузка PNG/JPEG сырыми байтами. 201 — только когда байты в S3 и строка ready в БД.
|
||
|
||
Порядок проверок: сессия (middleware) -> участник чата (404) -> filename (400) -> Content-Type (415)
|
||
-> Content-Length (413) — всё до чтения тела; затем потоковое чтение с лимитом (413),
|
||
проверка содержимого (415/413) и запись (attachments.store).
|
||
"""
|
||
user_id = current_user_id(request)
|
||
try:
|
||
await run_in_threadpool(db.check_member, chat_id, user_id)
|
||
name = uploads.validate_filename(filename)
|
||
declared = uploads.declared_type(request.headers.get("content-type"))
|
||
uploads.declared_length(request.headers.get("content-length"))
|
||
except ApiError:
|
||
await uploads.drain(request) # чтобы клиент получил ответ с ошибкой, а не обрыв на отправке
|
||
raise
|
||
upload = await uploads.receive(request)
|
||
try:
|
||
image = await run_in_threadpool(uploads.inspect, upload, declared)
|
||
row = await run_in_threadpool(attachments.store, chat_id, user_id, name, upload, image)
|
||
finally:
|
||
upload.file.close() # временный файл удаляется сразу; оригинал остаётся только в S3
|
||
return attachments.public(row)
|
||
|
||
|
||
@app.get("/api/v1/attachments/{attachment_id}", response_model=Attachment)
|
||
def get_attachment(request: Request, attachment_id: UUID):
|
||
return attachments.public(db.get_attachment(attachment_id, current_user_id(request)))
|
||
|
||
|
||
@app.get("/api/v1/attachments/{attachment_id}/content")
|
||
def download_attachment(
|
||
request: Request,
|
||
attachment_id: UUID,
|
||
variant: Literal["original"] = Query(default="original"),
|
||
):
|
||
"""Байты оригинала через API (без redirect): сначала проверка участника чата, потом S3."""
|
||
row = db.get_attachment(attachment_id, current_user_id(request))
|
||
try:
|
||
stream = storage.open_read(row["object_key"])
|
||
except storage.ObjectMissing:
|
||
log.error("Object of ready attachment %s is missing in S3", row["id"])
|
||
raise unavailable("File is temporarily unavailable") from None
|
||
except storage.StorageUnavailable:
|
||
raise unavailable("File storage is temporarily unavailable") from None
|
||
headers = {
|
||
"Content-Length": str(row["size_bytes"]),
|
||
"Content-Disposition": "inline; filename*=UTF-8''" + quote(row["filename"], safe=""),
|
||
"X-Content-Type-Options": "nosniff",
|
||
"Cache-Control": "private, no-store",
|
||
"ETag": '"' + row["sha256"] + '"',
|
||
}
|
||
return StreamingResponse(storage.iter_chunks(stream), media_type=row["content_type"], headers=headers)
|
||
|
||
|
||
if __name__ == "__main__":
|
||
Server(uvicorn.Config(
|
||
app,
|
||
host=settings.APP_HOST,
|
||
port=settings.APP_PORT,
|
||
timeout_graceful_shutdown=settings.SHUTDOWN_TIMEOUT_SECONDS,
|
||
access_log=False,
|
||
)).run()
|