forked from mtusi2026/mtusi2026lab3
ЛР 3 — БПИ2302 — Щербацевич — на 5
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import logging
|
||||
import sys
|
||||
|
||||
import uvicorn
|
||||
|
||||
from app.config import load_settings
|
||||
|
||||
|
||||
def main() -> None:
|
||||
settings = load_settings()
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
stream=sys.stdout,
|
||||
force=True,
|
||||
)
|
||||
logging.getLogger("sqlalchemy.engine").setLevel(logging.WARNING)
|
||||
logging.getLogger("sqlalchemy.pool").setLevel(logging.WARNING)
|
||||
logging.getLogger("asyncpg").setLevel(logging.WARNING)
|
||||
logging.getLogger("app").info(
|
||||
"starting host=%s port=%s env=%s instance_id=%s",
|
||||
settings.host,
|
||||
settings.port,
|
||||
settings.env,
|
||||
settings.instance_id,
|
||||
)
|
||||
uvicorn.run(
|
||||
"app.main:app",
|
||||
host=settings.host,
|
||||
port=settings.port,
|
||||
workers=1,
|
||||
access_log=False,
|
||||
timeout_keep_alive=5,
|
||||
timeout_graceful_shutdown=5,
|
||||
log_config=None,
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,12 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from app.api.auth import router as auth_router
|
||||
from app.api.chats import router as chats_router
|
||||
from app.api.messages import router as messages_router
|
||||
from app.api.users import router as users_router
|
||||
|
||||
api_router = APIRouter(prefix="/api/v1")
|
||||
api_router.include_router(users_router)
|
||||
api_router.include_router(auth_router)
|
||||
api_router.include_router(chats_router)
|
||||
api_router.include_router(messages_router)
|
||||
@@ -0,0 +1,37 @@
|
||||
from fastapi import APIRouter, Depends, Header
|
||||
from starlette.responses import Response
|
||||
|
||||
from app.crud.users import authenticate
|
||||
from app.deps import BASIC_CHALLENGE, BEARER_CHALLENGE, require_token, unauthorized
|
||||
from app.schemas.session import SessionOut
|
||||
from app.security import parse_basic, verify_password
|
||||
from app.sessions import create_session, revoke_session
|
||||
from app.timeutil import format_time
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/auth/sessions", status_code=201, response_model=SessionOut)
|
||||
async def create_session_route(authorization: str | None = Header(default=None)) -> dict:
|
||||
parsed = parse_basic(authorization or "")
|
||||
if parsed is None:
|
||||
verify_password("invalid-basic-auth", None)
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
username, password = parsed
|
||||
user = await authenticate(username, password)
|
||||
if user is None:
|
||||
raise unauthorized(BASIC_CHALLENGE)
|
||||
token, expires_at = await create_session(user.id)
|
||||
return {
|
||||
"token": token,
|
||||
"token_type": "Bearer",
|
||||
"expires_at": format_time(expires_at),
|
||||
"user": user.to_dict(),
|
||||
}
|
||||
|
||||
|
||||
@router.delete("/auth/sessions/current", status_code=204)
|
||||
async def revoke_current_session(token: str = Depends(require_token)) -> Response:
|
||||
if not await revoke_session(token):
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
return Response(status_code=204)
|
||||
@@ -0,0 +1,31 @@
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Query
|
||||
|
||||
from app.crud.chats import chat_for_member, chats_for_user, create_chat as save_chat
|
||||
from app.deps import get_current_user
|
||||
from app.present import PublicUser
|
||||
from app.schemas.chat import ChatOut, ChatPage, CreateChat
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/chats", status_code=201, response_model=ChatOut)
|
||||
async def create_chat(body: CreateChat, user: PublicUser = Depends(get_current_user)) -> dict:
|
||||
chat = await save_chat(user.id, body.title, tuple(body.member_ids))
|
||||
return chat.to_dict()
|
||||
|
||||
|
||||
@router.get("/chats", response_model=ChatPage)
|
||||
async def list_chats(
|
||||
user: PublicUser = Depends(get_current_user),
|
||||
limit: int = Query(default=50, ge=1, le=100),
|
||||
cursor: str | None = Query(default=None, min_length=1),
|
||||
) -> dict:
|
||||
return await chats_for_user(user.id, limit, cursor)
|
||||
|
||||
|
||||
@router.get("/chats/{chat_id}", response_model=ChatOut)
|
||||
async def get_chat(chat_id: UUID, user: PublicUser = Depends(get_current_user)) -> dict:
|
||||
chat = await chat_for_member(chat_id, user.id)
|
||||
return chat.to_dict()
|
||||
@@ -0,0 +1,39 @@
|
||||
import logging
|
||||
|
||||
from fastapi import APIRouter, Request
|
||||
from pydantic import BaseModel
|
||||
from sqlalchemy import text
|
||||
|
||||
from app.db import is_dependency_error
|
||||
from app.errors import AppError
|
||||
|
||||
router = APIRouter()
|
||||
logger = logging.getLogger("app.health")
|
||||
|
||||
|
||||
class HealthOut(BaseModel):
|
||||
status: str
|
||||
|
||||
|
||||
@router.get("/health/live", response_model=HealthOut)
|
||||
async def live() -> dict:
|
||||
return {"status": "ok"}
|
||||
|
||||
|
||||
@router.get("/health/ready", response_model=HealthOut)
|
||||
async def ready(request: Request) -> dict:
|
||||
if not getattr(request.app.state, "accepting", False):
|
||||
raise AppError(503, "unavailable", "Сервер не готов")
|
||||
engine = getattr(request.app.state, "engine", None)
|
||||
redis = getattr(request.app.state, "redis", None)
|
||||
if engine is None or redis is None:
|
||||
raise AppError(503, "unavailable", "Сервис временно недоступен")
|
||||
try:
|
||||
async with engine.connect() as conn:
|
||||
await conn.execute(text("SELECT 1"))
|
||||
await redis.ping()
|
||||
except Exception as exc:
|
||||
if not is_dependency_error(exc):
|
||||
logger.warning("readiness check failed type=%s", type(exc).__name__)
|
||||
raise AppError(503, "unavailable", "Сервис временно недоступен") from None
|
||||
return {"status": "ok"}
|
||||
@@ -0,0 +1,43 @@
|
||||
import re
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, Query, Request
|
||||
|
||||
from app.crud.messages import create_message as save_message
|
||||
from app.crud.messages import messages_for_member
|
||||
from app.deps import get_current_user
|
||||
from app.errors import AppError
|
||||
from app.present import PublicUser
|
||||
from app.schemas.message import CreateMessage, MessageOut, MessagePage
|
||||
|
||||
router = APIRouter()
|
||||
_IDEMPOTENCY_KEY = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
|
||||
|
||||
|
||||
def require_idempotency_key(request: Request) -> str:
|
||||
value = request.headers.get("idempotency-key")
|
||||
if value is None or _IDEMPOTENCY_KEY.fullmatch(value) is None:
|
||||
raise AppError(400, "invalid_request", "Некорректный Idempotency-Key")
|
||||
return value
|
||||
|
||||
|
||||
@router.post("/chats/{chat_id}/messages", status_code=201, response_model=MessageOut)
|
||||
async def create_message(
|
||||
chat_id: UUID,
|
||||
body: CreateMessage,
|
||||
request: Request,
|
||||
user: PublicUser = Depends(get_current_user),
|
||||
) -> dict:
|
||||
key = require_idempotency_key(request)
|
||||
message = await save_message(chat_id, user.id, body.text, key)
|
||||
return message.to_dict()
|
||||
|
||||
|
||||
@router.get("/chats/{chat_id}/messages", response_model=MessagePage)
|
||||
async def list_messages(
|
||||
chat_id: UUID,
|
||||
user: PublicUser = Depends(get_current_user),
|
||||
limit: int = Query(default=50, ge=1, le=100),
|
||||
cursor: str | None = Query(default=None, min_length=1),
|
||||
) -> dict:
|
||||
return await messages_for_member(chat_id, user.id, limit, cursor)
|
||||
@@ -0,0 +1,19 @@
|
||||
from fastapi import APIRouter, Depends
|
||||
|
||||
from app.crud.users import create_user
|
||||
from app.deps import get_current_user
|
||||
from app.present import PublicUser
|
||||
from app.schemas.user import CreateUser, UserOut
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/users", status_code=201, response_model=UserOut)
|
||||
async def register(body: CreateUser) -> dict:
|
||||
user = await create_user(body.username, body.display_name, body.password)
|
||||
return user.to_dict()
|
||||
|
||||
|
||||
@router.get("/users/me", response_model=UserOut)
|
||||
async def me(user: PublicUser = Depends(get_current_user)) -> dict:
|
||||
return user.to_dict()
|
||||
@@ -0,0 +1,130 @@
|
||||
import os
|
||||
import socket
|
||||
from dataclasses import dataclass
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class Settings:
|
||||
host: str
|
||||
port: int
|
||||
env: str
|
||||
app_role: str
|
||||
instance_id: str
|
||||
run_migrations: bool
|
||||
session_ttl_seconds: int
|
||||
idempotency_ttl_seconds: int
|
||||
database_url: str
|
||||
cache_url: str
|
||||
migrations_dir: Path
|
||||
db_pool_size: int
|
||||
db_max_overflow: int
|
||||
db_pool_timeout_seconds: float
|
||||
db_connect_timeout_seconds: float
|
||||
db_command_timeout_seconds: float
|
||||
redis_socket_timeout_seconds: float
|
||||
redis_max_connections: int
|
||||
|
||||
|
||||
def _int(name: str, default: int, low: int, high: int) -> int:
|
||||
raw = os.environ.get(name, str(default)).strip()
|
||||
try:
|
||||
value = int(raw)
|
||||
except ValueError as exc:
|
||||
raise SystemExit(f"{name} must be an integer") from exc
|
||||
if not low <= value <= high:
|
||||
raise SystemExit(f"{name} is out of range")
|
||||
return value
|
||||
|
||||
|
||||
def _float(name: str, default: float, low: float, high: float) -> float:
|
||||
raw = os.environ.get(name, str(default)).strip()
|
||||
try:
|
||||
value = float(raw)
|
||||
except ValueError as exc:
|
||||
raise SystemExit(f"{name} must be a number") from exc
|
||||
if not low <= value <= high:
|
||||
raise SystemExit(f"{name} is out of range")
|
||||
return value
|
||||
|
||||
|
||||
def _secret(direct_name: str, file_name: str) -> str:
|
||||
direct = os.environ.get(direct_name, "").strip()
|
||||
if direct:
|
||||
return direct
|
||||
path = os.environ.get(file_name, "").strip()
|
||||
if not path:
|
||||
raise SystemExit(f"set {direct_name} or {file_name}")
|
||||
try:
|
||||
value = Path(path).read_text(encoding="utf-8").strip()
|
||||
except OSError as exc:
|
||||
raise SystemExit(f"cannot read {file_name}") from exc
|
||||
if not value:
|
||||
raise SystemExit(f"{file_name} is empty")
|
||||
return value
|
||||
|
||||
|
||||
def _migrations_dir() -> Path:
|
||||
configured = os.environ.get("MIGRATIONS_DIR", "").strip()
|
||||
if configured:
|
||||
return Path(configured)
|
||||
return Path(__file__).resolve().parents[2] / "migrations"
|
||||
|
||||
|
||||
def _flag(name: str, default: bool) -> bool:
|
||||
raw = os.environ.get(name, "1" if default else "0").strip().lower()
|
||||
if raw in {"1", "true", "yes", "on"}:
|
||||
return True
|
||||
if raw in {"0", "false", "no", "off"}:
|
||||
return False
|
||||
raise SystemExit(f"{name} must be a boolean")
|
||||
|
||||
|
||||
def _instance_id() -> str:
|
||||
configured = os.environ.get("INSTANCE_ID", "").strip()
|
||||
if configured:
|
||||
return configured
|
||||
host = os.environ.get("HOSTNAME", "").strip() or socket.gethostname()
|
||||
if not host:
|
||||
raise SystemExit("INSTANCE_ID is empty")
|
||||
return host
|
||||
|
||||
|
||||
def load_settings() -> Settings:
|
||||
host = os.environ.get("APP_HOST", "0.0.0.0").strip() or "0.0.0.0"
|
||||
env_name = os.environ.get("APP_ENV", "lab").strip() or "lab"
|
||||
role = os.environ.get("APP_ROLE", "api").strip() or "api"
|
||||
if role not in {"api", "migrate"}:
|
||||
raise SystemExit("APP_ROLE must be api or migrate")
|
||||
port = _int("APP_PORT", 8080, 1, 65535)
|
||||
database_url = _secret("DATABASE_URL", "DATABASE_URL_FILE")
|
||||
cache_url = ""
|
||||
if role != "migrate":
|
||||
cache_url = _secret("REDIS_URL", "CACHE_URL_FILE")
|
||||
if not cache_url.startswith(("redis://", "rediss://")):
|
||||
raise SystemExit("REDIS_URL must start with redis:// or rediss://")
|
||||
if not database_url.startswith("postgresql+asyncpg://"):
|
||||
raise SystemExit("DATABASE_URL must start with postgresql+asyncpg://")
|
||||
migrations = _migrations_dir()
|
||||
if not migrations.is_dir():
|
||||
raise SystemExit("migrations directory is missing")
|
||||
return Settings(
|
||||
host=host,
|
||||
port=port,
|
||||
env=env_name,
|
||||
app_role=role,
|
||||
instance_id=_instance_id(),
|
||||
run_migrations=_flag("RUN_MIGRATIONS", True),
|
||||
session_ttl_seconds=_int("SESSION_TTL_SECONDS", 3600, 1, 30 * 24 * 3600),
|
||||
idempotency_ttl_seconds=_int("IDEMPOTENCY_TTL_SECONDS", 86400, 60, 30 * 24 * 3600),
|
||||
database_url=database_url,
|
||||
cache_url=cache_url,
|
||||
migrations_dir=migrations,
|
||||
db_pool_size=_int("DB_POOL_SIZE", 10, 1, 50),
|
||||
db_max_overflow=_int("DB_MAX_OVERFLOW", 10, 0, 50),
|
||||
db_pool_timeout_seconds=_float("DB_POOL_TIMEOUT_SECONDS", 5, 0.1, 30),
|
||||
db_connect_timeout_seconds=_float("DB_CONNECT_TIMEOUT_SECONDS", 2, 0.1, 30),
|
||||
db_command_timeout_seconds=_float("DB_COMMAND_TIMEOUT_SECONDS", 5, 0.1, 60),
|
||||
redis_socket_timeout_seconds=_float("REDIS_SOCKET_TIMEOUT_SECONDS", 1, 0.1, 30),
|
||||
redis_max_connections=_int("REDIS_MAX_CONNECTIONS", 50, 1, 200),
|
||||
)
|
||||
@@ -0,0 +1 @@
|
||||
"""Чтение и изменение PostgreSQL. Сессии живут в Redis, не в этих таблицах."""
|
||||
@@ -0,0 +1,101 @@
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from sqlalchemy import select, tuple_
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app.db import map_integrity, session_scope
|
||||
from app.errors import AppError
|
||||
from app.models import Chat, ChatMember, User
|
||||
from app.pagination import decode_position, slice_page
|
||||
from app.present import PublicChat
|
||||
from app.timeutil import utcnow
|
||||
|
||||
|
||||
def _invalid_members() -> AppError:
|
||||
return AppError(400, "invalid_request", "Некорректный список участников")
|
||||
|
||||
|
||||
async def create_chat(
|
||||
owner_id: UUID,
|
||||
title: str,
|
||||
member_ids: tuple[UUID, ...],
|
||||
*,
|
||||
fail_after_insert: bool = False,
|
||||
) -> PublicChat:
|
||||
if (
|
||||
not member_ids
|
||||
or len(member_ids) > 100
|
||||
or len(set(member_ids)) != len(member_ids)
|
||||
or owner_id not in member_ids
|
||||
):
|
||||
raise _invalid_members()
|
||||
chat_id = uuid4()
|
||||
created_at = utcnow()
|
||||
async with session_scope() as session:
|
||||
try:
|
||||
async with session.begin():
|
||||
found = set((await session.scalars(select(User.id).where(User.id.in_(member_ids)))).all())
|
||||
if found != set(member_ids):
|
||||
raise _invalid_members()
|
||||
session.add(Chat(id=chat_id, title=title, created_at=created_at))
|
||||
await session.flush()
|
||||
# Точка для проверки отката: строка чата уже вставлена, COMMIT ещё нет.
|
||||
if fail_after_insert:
|
||||
raise RuntimeError("rollback-check")
|
||||
for ordinal, member_id in enumerate(member_ids):
|
||||
session.add(ChatMember(chat_id=chat_id, user_id=member_id, ordinal=ordinal))
|
||||
await session.flush()
|
||||
except IntegrityError as exc:
|
||||
raise map_integrity(exc, "Конфликт записи") from None
|
||||
return PublicChat(id=chat_id, title=title, member_ids=tuple(member_ids), created_at=created_at)
|
||||
|
||||
|
||||
async def _member_ids(session, chat_id: UUID) -> tuple[UUID, ...]:
|
||||
rows = await session.execute(
|
||||
select(ChatMember.user_id).where(ChatMember.chat_id == chat_id).order_by(ChatMember.ordinal)
|
||||
)
|
||||
return tuple(row[0] for row in rows)
|
||||
|
||||
|
||||
async def chat_for_member(chat_id: UUID, user_id: UUID) -> PublicChat:
|
||||
async with session_scope() as session:
|
||||
async with session.begin():
|
||||
member = await session.scalar(
|
||||
select(ChatMember.user_id).where(ChatMember.chat_id == chat_id, ChatMember.user_id == user_id)
|
||||
)
|
||||
chat = await session.get(Chat, chat_id)
|
||||
if member is None or chat is None:
|
||||
raise AppError(404, "not_found", "Объект не найден")
|
||||
return PublicChat(
|
||||
id=chat.id,
|
||||
title=chat.title,
|
||||
member_ids=await _member_ids(session, chat.id),
|
||||
created_at=chat.created_at,
|
||||
)
|
||||
|
||||
|
||||
async def chats_for_user(user_id: UUID, limit: int, cursor: str | None) -> dict:
|
||||
position = decode_position(cursor, "c", user_id) if cursor else None
|
||||
async with session_scope() as session:
|
||||
async with session.begin():
|
||||
stmt = (
|
||||
select(Chat)
|
||||
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||
.where(ChatMember.user_id == user_id)
|
||||
)
|
||||
if position is not None:
|
||||
created_at, cursor_id = position
|
||||
stmt = stmt.where(tuple_(Chat.created_at, Chat.id) > tuple_(created_at, cursor_id))
|
||||
stmt = stmt.order_by(Chat.created_at.asc(), Chat.id.asc()).limit(limit + 1)
|
||||
chats = (await session.scalars(stmt)).unique().all()
|
||||
public = []
|
||||
for chat in chats:
|
||||
public.append(
|
||||
PublicChat(
|
||||
id=chat.id,
|
||||
title=chat.title,
|
||||
member_ids=await _member_ids(session, chat.id),
|
||||
created_at=chat.created_at,
|
||||
)
|
||||
)
|
||||
return slice_page(public, limit, "c", user_id)
|
||||
@@ -0,0 +1,145 @@
|
||||
import hashlib
|
||||
import json
|
||||
from datetime import timedelta
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from sqlalchemy import delete, select, text, tuple_
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app.db import map_integrity, session_scope
|
||||
from app.errors import AppError
|
||||
from app.models import ChatMember, IdempotencyRecord, Message
|
||||
from app.pagination import decode_position, slice_page
|
||||
from app.present import PublicMessage
|
||||
from app.runtime import require_settings
|
||||
from app.timeutil import utcnow
|
||||
|
||||
|
||||
def message_path(chat_id: UUID) -> str:
|
||||
return "/api/v1/chats/%s/messages" % chat_id
|
||||
|
||||
|
||||
def request_fingerprint(text_value: str) -> str:
|
||||
payload = json.dumps({"text": text_value}, ensure_ascii=False, separators=(",", ":"))
|
||||
return hashlib.sha256(payload.encode("utf-8")).hexdigest()
|
||||
|
||||
|
||||
def _lock_keys(scope: str) -> tuple[int, int]:
|
||||
digest = hashlib.sha256(scope.encode("utf-8")).digest()
|
||||
first = int.from_bytes(digest[:4], "big", signed=True)
|
||||
second = int.from_bytes(digest[4:8], "big", signed=True)
|
||||
return first, second
|
||||
|
||||
|
||||
def _public(row: Message) -> PublicMessage:
|
||||
return PublicMessage(
|
||||
id=row.id,
|
||||
chat_id=row.chat_id,
|
||||
sender_id=row.sender_id,
|
||||
text=row.text,
|
||||
created_at=row.created_at,
|
||||
)
|
||||
|
||||
|
||||
async def _require_member(session, chat_id: UUID, user_id: UUID) -> None:
|
||||
member = await session.scalar(
|
||||
select(ChatMember.user_id).where(ChatMember.chat_id == chat_id, ChatMember.user_id == user_id)
|
||||
)
|
||||
if member is None:
|
||||
raise AppError(404, "not_found", "Объект не найден")
|
||||
|
||||
|
||||
async def create_message(
|
||||
chat_id: UUID,
|
||||
sender_id: UUID,
|
||||
text_value: str,
|
||||
idempotency_key: str,
|
||||
) -> PublicMessage:
|
||||
settings = require_settings()
|
||||
path = message_path(chat_id)
|
||||
request_hash = request_fingerprint(text_value)
|
||||
lock_a, lock_b = _lock_keys("POST:%s:%s:%s" % (sender_id, path, idempotency_key))
|
||||
ttl = timedelta(seconds=settings.idempotency_ttl_seconds)
|
||||
async with session_scope() as session:
|
||||
try:
|
||||
async with session.begin():
|
||||
await session.execute(
|
||||
text("SELECT pg_advisory_xact_lock(:a, :b)"),
|
||||
{"a": lock_a, "b": lock_b},
|
||||
)
|
||||
await _require_member(session, chat_id, sender_id)
|
||||
existing = await session.scalar(
|
||||
select(IdempotencyRecord).where(
|
||||
IdempotencyRecord.user_id == sender_id,
|
||||
IdempotencyRecord.method == "POST",
|
||||
IdempotencyRecord.request_path == path,
|
||||
IdempotencyRecord.idempotency_key == idempotency_key,
|
||||
)
|
||||
)
|
||||
now = utcnow()
|
||||
if existing is not None and existing.created_at + ttl >= now:
|
||||
if existing.request_hash != request_hash:
|
||||
raise AppError(409, "conflict", "Повтор ключа с другим сообщением")
|
||||
row = await session.get(Message, existing.message_id)
|
||||
if row is None:
|
||||
raise AppError(500, "internal_error", "Внутренняя ошибка")
|
||||
return _public(row)
|
||||
if existing is not None:
|
||||
await session.execute(
|
||||
delete(IdempotencyRecord).where(
|
||||
IdempotencyRecord.user_id == existing.user_id,
|
||||
IdempotencyRecord.method == existing.method,
|
||||
IdempotencyRecord.request_path == existing.request_path,
|
||||
IdempotencyRecord.idempotency_key == existing.idempotency_key,
|
||||
)
|
||||
)
|
||||
await session.flush()
|
||||
message_id = uuid4()
|
||||
created_at = now
|
||||
session.add(
|
||||
Message(
|
||||
id=message_id,
|
||||
chat_id=chat_id,
|
||||
sender_id=sender_id,
|
||||
text=text_value,
|
||||
created_at=created_at,
|
||||
)
|
||||
)
|
||||
await session.flush()
|
||||
session.add(
|
||||
IdempotencyRecord(
|
||||
user_id=sender_id,
|
||||
method="POST",
|
||||
request_path=path,
|
||||
idempotency_key=idempotency_key,
|
||||
request_hash=request_hash,
|
||||
status_code=201,
|
||||
message_id=message_id,
|
||||
created_at=created_at,
|
||||
)
|
||||
)
|
||||
await session.flush()
|
||||
except IntegrityError as exc:
|
||||
raise map_integrity(exc, "Конфликт записи") from None
|
||||
return PublicMessage(
|
||||
id=message_id,
|
||||
chat_id=chat_id,
|
||||
sender_id=sender_id,
|
||||
text=text_value,
|
||||
created_at=created_at,
|
||||
)
|
||||
|
||||
|
||||
async def messages_for_member(chat_id: UUID, user_id: UUID, limit: int, cursor: str | None) -> dict:
|
||||
async with session_scope() as session:
|
||||
async with session.begin():
|
||||
await _require_member(session, chat_id, user_id)
|
||||
position = decode_position(cursor, "m", chat_id) if cursor else None
|
||||
stmt = select(Message).where(Message.chat_id == chat_id)
|
||||
if position is not None:
|
||||
created_at, message_id = position
|
||||
stmt = stmt.where(tuple_(Message.created_at, Message.id) > tuple_(created_at, message_id))
|
||||
stmt = stmt.order_by(Message.created_at.asc(), Message.id.asc()).limit(limit + 1)
|
||||
rows = (await session.scalars(stmt)).all()
|
||||
public = [_public(row) for row in rows]
|
||||
return slice_page(public, limit, "m", chat_id)
|
||||
@@ -0,0 +1,78 @@
|
||||
import re
|
||||
from uuid import UUID, uuid4
|
||||
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from app.db import map_integrity, session_scope
|
||||
from app.models import User
|
||||
from app.present import PublicUser
|
||||
from app.security import hash_password, verify_password
|
||||
from app.timeutil import utcnow
|
||||
|
||||
_USERNAME = re.compile(r"^[a-z0-9_]{3,32}$")
|
||||
|
||||
|
||||
async def get_user(user_id: UUID) -> PublicUser | None:
|
||||
async with session_scope() as session:
|
||||
async with session.begin():
|
||||
row = (
|
||||
await session.execute(
|
||||
select(User.id, User.username, User.display_name, User.created_at).where(User.id == user_id)
|
||||
)
|
||||
).one_or_none()
|
||||
if row is None:
|
||||
return None
|
||||
return PublicUser(id=row.id, username=row.username, display_name=row.display_name, created_at=row.created_at)
|
||||
|
||||
|
||||
async def create_user(username: str, display_name: str, password: str) -> PublicUser:
|
||||
# Argon2 считается до открытия транзакции, чтобы не держать соединение пула.
|
||||
password_hash = hash_password(password)
|
||||
user_id = uuid4()
|
||||
created_at = utcnow()
|
||||
async with session_scope() as session:
|
||||
try:
|
||||
async with session.begin():
|
||||
session.add(
|
||||
User(
|
||||
id=user_id,
|
||||
username=username,
|
||||
display_name=display_name,
|
||||
password_hash=password_hash,
|
||||
created_at=created_at,
|
||||
)
|
||||
)
|
||||
await session.flush()
|
||||
except IntegrityError as exc:
|
||||
raise map_integrity(exc, "Имя пользователя занято") from None
|
||||
return PublicUser(id=user_id, username=username, display_name=display_name, created_at=created_at)
|
||||
|
||||
|
||||
async def authenticate(username: str, password: str) -> PublicUser | None:
|
||||
if _USERNAME.fullmatch(username) is None or not 12 <= len(password) <= 128:
|
||||
verify_password("rejected-credential", None)
|
||||
return None
|
||||
async with session_scope() as session:
|
||||
async with session.begin():
|
||||
row = (
|
||||
await session.execute(
|
||||
select(User.id, User.username, User.display_name, User.created_at, User.password_hash).where(
|
||||
User.username == username
|
||||
)
|
||||
)
|
||||
).one_or_none()
|
||||
if row is None:
|
||||
stored = None
|
||||
snapshot = None
|
||||
else:
|
||||
stored = row.password_hash
|
||||
snapshot = PublicUser(
|
||||
id=row.id,
|
||||
username=row.username,
|
||||
display_name=row.display_name,
|
||||
created_at=row.created_at,
|
||||
)
|
||||
if not verify_password(password, stored):
|
||||
return None
|
||||
return snapshot
|
||||
@@ -0,0 +1,79 @@
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from redis.exceptions import RedisError
|
||||
from sqlalchemy.exc import DBAPIError, IntegrityError, InterfaceError, OperationalError
|
||||
from sqlalchemy.exc import TimeoutError as SATimeout
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession, async_sessionmaker, create_async_engine
|
||||
|
||||
from app.config import Settings
|
||||
from app.errors import AppError
|
||||
|
||||
logger = logging.getLogger("app.db")
|
||||
|
||||
|
||||
def build_engine(settings: Settings) -> AsyncEngine:
|
||||
command_ms = str(int(settings.db_command_timeout_seconds * 1000))
|
||||
lock_ms = str(int(settings.db_pool_timeout_seconds * 1000))
|
||||
return create_async_engine(
|
||||
settings.database_url,
|
||||
pool_size=settings.db_pool_size,
|
||||
max_overflow=settings.db_max_overflow,
|
||||
pool_timeout=settings.db_pool_timeout_seconds,
|
||||
pool_pre_ping=True,
|
||||
pool_recycle=1800,
|
||||
echo=False,
|
||||
connect_args={
|
||||
"timeout": settings.db_connect_timeout_seconds,
|
||||
"command_timeout": settings.db_command_timeout_seconds,
|
||||
"server_settings": {
|
||||
"statement_timeout": command_ms,
|
||||
"lock_timeout": lock_ms,
|
||||
"idle_in_transaction_session_timeout": "10000",
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
def build_session_factory(engine: AsyncEngine) -> async_sessionmaker[AsyncSession]:
|
||||
return async_sessionmaker(engine, expire_on_commit=False, autoflush=False)
|
||||
|
||||
|
||||
def is_dependency_error(exc: BaseException) -> bool:
|
||||
if isinstance(exc, (OperationalError, InterfaceError, SATimeout, RedisError, ConnectionError, TimeoutError)):
|
||||
return True
|
||||
return isinstance(exc, DBAPIError) and bool(getattr(exc, "connection_invalidated", False))
|
||||
|
||||
|
||||
def map_integrity(exc: IntegrityError, duplicate_message: str) -> AppError:
|
||||
orig = getattr(exc, "orig", None)
|
||||
code = getattr(orig, "sqlstate", None) or getattr(orig, "pgcode", None)
|
||||
if code == "23505":
|
||||
return AppError(409, "conflict", duplicate_message)
|
||||
if code in {"23503", "23514", "23502"}:
|
||||
return AppError(400, "invalid_request", "Некорректный запрос")
|
||||
return AppError(500, "internal_error", "Внутренняя ошибка")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def session_scope():
|
||||
from app.runtime import require_factory
|
||||
|
||||
factory = require_factory()
|
||||
try:
|
||||
session = factory()
|
||||
except Exception as exc:
|
||||
if is_dependency_error(exc):
|
||||
logger.warning("database unavailable type=%s", type(exc).__name__)
|
||||
raise AppError(503, "unavailable", "Сервис временно недоступен") from None
|
||||
raise
|
||||
try:
|
||||
async with session:
|
||||
yield session
|
||||
except AppError:
|
||||
raise
|
||||
except Exception as exc:
|
||||
if is_dependency_error(exc):
|
||||
logger.warning("database unavailable type=%s", type(exc).__name__)
|
||||
raise AppError(503, "unavailable", "Сервис временно недоступен") from None
|
||||
raise
|
||||
@@ -0,0 +1,50 @@
|
||||
from fastapi import Depends, Header, Request
|
||||
from app.crud.users import get_user
|
||||
from app.errors import AppError
|
||||
from app.present import PublicUser
|
||||
from app.sessions import resolve_session
|
||||
|
||||
BEARER_CHALLENGE = 'Bearer realm="messenger"'
|
||||
BASIC_CHALLENGE = 'Basic realm="messenger", charset="UTF-8"'
|
||||
|
||||
|
||||
def unauthorized(challenge: str) -> AppError:
|
||||
return AppError(
|
||||
401,
|
||||
"unauthorized",
|
||||
"Требуется идентификация",
|
||||
headers={"WWW-Authenticate": challenge},
|
||||
)
|
||||
|
||||
|
||||
async def get_current_user(
|
||||
request: Request,
|
||||
authorization: str | None = Header(default=None),
|
||||
) -> PublicUser:
|
||||
# Нет заголовка — клиент анонимный. Redis для этого не нужен и не делает его доверенным.
|
||||
if authorization is None:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
scheme, _, token = authorization.partition(" ")
|
||||
token = token.strip()
|
||||
if scheme.lower() != "bearer" or not token or len(token) > 512:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
user_id = await resolve_session(token)
|
||||
if user_id is None:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
user = await get_user(user_id)
|
||||
if user is None:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
request.state.session_token = token
|
||||
return user
|
||||
|
||||
|
||||
def current_token(request: Request) -> str:
|
||||
token = getattr(request.state, "session_token", "")
|
||||
if not token:
|
||||
raise unauthorized(BEARER_CHALLENGE)
|
||||
return token
|
||||
|
||||
|
||||
def require_token(request: Request, user: PublicUser = Depends(get_current_user)) -> str:
|
||||
del user
|
||||
return current_token(request)
|
||||
@@ -0,0 +1,54 @@
|
||||
import logging
|
||||
|
||||
from fastapi import Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
|
||||
class AppError(Exception):
|
||||
def __init__(self, status: int, code: str, message: str, headers: dict | None = None) -> None:
|
||||
super().__init__(message)
|
||||
self.status = status
|
||||
self.code = code
|
||||
self.message = message
|
||||
self.headers = headers
|
||||
|
||||
|
||||
def request_id_of(request: Request) -> str:
|
||||
return getattr(request.state, "request_id", "") or "missing"
|
||||
|
||||
|
||||
def error_response(
|
||||
status: int,
|
||||
code: str,
|
||||
message: str,
|
||||
request_id: str,
|
||||
headers: dict | None = None,
|
||||
) -> JSONResponse:
|
||||
return JSONResponse(
|
||||
status_code=status,
|
||||
content={"error": {"code": code, "message": message, "request_id": request_id}},
|
||||
headers=headers,
|
||||
)
|
||||
|
||||
|
||||
async def handle_app_error(request: Request, exc: AppError) -> JSONResponse:
|
||||
return error_response(exc.status, exc.code, exc.message, request_id_of(request), exc.headers)
|
||||
|
||||
|
||||
async def handle_dependency(request: Request, exc: Exception) -> JSONResponse:
|
||||
# Текст исключения может содержать SQL или параметры. В ответ и лог его не кладём.
|
||||
logging.getLogger("app").warning("dependency unavailable type=%s", type(exc).__name__)
|
||||
return error_response(503, "unavailable", "Сервис временно недоступен", request_id_of(request))
|
||||
|
||||
|
||||
async def handle_validation_error(request: Request, _exc: RequestValidationError) -> JSONResponse:
|
||||
return error_response(400, "invalid_request", "Некорректный запрос", request_id_of(request))
|
||||
|
||||
|
||||
async def handle_http_error(request: Request, exc: HTTPException) -> JSONResponse:
|
||||
# Тело не в UTF-8 FastAPI превращает в HTTPException 400 с полем detail.
|
||||
if exc.status_code == 400:
|
||||
return error_response(400, "invalid_request", "Некорректный запрос", request_id_of(request))
|
||||
return JSONResponse(status_code=exc.status_code, content={"detail": exc.detail}, headers=exc.headers)
|
||||
@@ -0,0 +1,97 @@
|
||||
import asyncio
|
||||
import logging
|
||||
from contextlib import asynccontextmanager
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from redis.exceptions import RedisError
|
||||
from sqlalchemy.exc import InterfaceError, OperationalError, ProgrammingError
|
||||
from sqlalchemy.exc import TimeoutError as SATimeout
|
||||
from starlette.exceptions import HTTPException
|
||||
|
||||
from app.api import api_router
|
||||
from app.api.health import router as health_router
|
||||
from app.config import load_settings
|
||||
from app.db import build_engine, build_session_factory, is_dependency_error
|
||||
from app.errors import AppError, handle_app_error, handle_dependency, handle_http_error, handle_validation_error
|
||||
from app.migrate import apply_migrations, wait_for_database, wait_for_schema
|
||||
from app.middleware import request_context
|
||||
from app.runtime import configure
|
||||
from app.security import init_security
|
||||
from app.sessions import build_redis
|
||||
|
||||
logger = logging.getLogger("app")
|
||||
|
||||
|
||||
async def open_stores(engine, redis, settings) -> None:
|
||||
await wait_for_database(engine)
|
||||
for attempt in range(40):
|
||||
try:
|
||||
await redis.ping()
|
||||
break
|
||||
except Exception as exc:
|
||||
if not is_dependency_error(exc):
|
||||
logger.error("redis probe failed type=%s", type(exc).__name__)
|
||||
raise SystemExit("redis probe failed") from None
|
||||
logger.warning("redis not ready type=%s attempt=%s", type(exc).__name__, attempt + 1)
|
||||
if attempt == 39:
|
||||
raise SystemExit("redis is not ready") from None
|
||||
await asyncio.sleep(0.5)
|
||||
if settings.run_migrations:
|
||||
try:
|
||||
applied = await apply_migrations(engine, settings.migrations_dir)
|
||||
logger.info("migrations newly_applied=%s", applied)
|
||||
except ProgrammingError as exc:
|
||||
sqlstate = getattr(getattr(exc, "orig", None), "sqlstate", "")
|
||||
logger.error("migration rejected type=%s sqlstate=%s", type(exc).__name__, sqlstate)
|
||||
raise SystemExit("migration failed") from None
|
||||
else:
|
||||
await wait_for_schema(engine, settings.migrations_dir)
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app: FastAPI):
|
||||
settings = load_settings()
|
||||
init_security()
|
||||
engine = build_engine(settings)
|
||||
redis = build_redis(settings)
|
||||
app.state.engine = engine
|
||||
app.state.redis = redis
|
||||
app.state.settings = settings
|
||||
app.state.instance_id = settings.instance_id
|
||||
app.state.accepting = False
|
||||
try:
|
||||
await open_stores(engine, redis, settings)
|
||||
configure(build_session_factory(engine), redis, settings)
|
||||
app.state.accepting = True
|
||||
logger.info(
|
||||
"ready env=%s instance_id=%s session_ttl_seconds=%s db_pool=%s",
|
||||
settings.env,
|
||||
settings.instance_id,
|
||||
settings.session_ttl_seconds,
|
||||
settings.db_pool_size,
|
||||
)
|
||||
yield
|
||||
finally:
|
||||
app.state.accepting = False
|
||||
logger.info("shutdown: new requests are no longer accepted")
|
||||
await redis.aclose()
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
app = FastAPI(title="MTUSI Messenger", version="1.3.0", lifespan=lifespan)
|
||||
app.state.instance_id = "starting"
|
||||
app.state.accepting = False
|
||||
app.add_exception_handler(AppError, handle_app_error)
|
||||
app.add_exception_handler(RequestValidationError, handle_validation_error)
|
||||
app.add_exception_handler(HTTPException, handle_http_error)
|
||||
for cls in (OperationalError, InterfaceError, SATimeout, RedisError):
|
||||
app.add_exception_handler(cls, handle_dependency)
|
||||
app.middleware("http")(request_context)
|
||||
app.include_router(health_router)
|
||||
app.include_router(api_router)
|
||||
return app
|
||||
|
||||
|
||||
app = create_app()
|
||||
@@ -0,0 +1,52 @@
|
||||
import logging
|
||||
import time
|
||||
from uuid import uuid4
|
||||
|
||||
from fastapi import Request
|
||||
|
||||
from app.db import is_dependency_error
|
||||
from app.errors import error_response
|
||||
|
||||
logger = logging.getLogger("app.access")
|
||||
|
||||
|
||||
def _instance_id(request: Request) -> str:
|
||||
settings = getattr(request.app.state, "settings", None)
|
||||
if settings is not None:
|
||||
return settings.instance_id
|
||||
return getattr(request.app.state, "instance_id", "starting")
|
||||
|
||||
|
||||
async def request_context(request: Request, call_next):
|
||||
request_id = uuid4().hex
|
||||
request.state.request_id = request_id
|
||||
started = time.perf_counter()
|
||||
status = 500
|
||||
try:
|
||||
accepting = getattr(request.app.state, "accepting", True)
|
||||
if not accepting and request.url.path != "/health/live":
|
||||
response = error_response(503, "unavailable", "Сервер останавливается", request_id)
|
||||
else:
|
||||
try:
|
||||
response = await call_next(request)
|
||||
except Exception as exc:
|
||||
if is_dependency_error(exc):
|
||||
logger.warning("dependency unavailable type=%s", type(exc).__name__)
|
||||
response = error_response(503, "unavailable", "Сервис временно недоступен", request_id)
|
||||
else:
|
||||
logger.error("internal error request_id=%s type=%s", request_id, type(exc).__name__)
|
||||
response = error_response(500, "internal_error", "Внутренняя ошибка", request_id)
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
response.headers["X-Instance-Id"] = _instance_id(request)
|
||||
status = response.status_code
|
||||
return response
|
||||
finally:
|
||||
elapsed_ms = int((time.perf_counter() - started) * 1000)
|
||||
logger.info(
|
||||
"request method=%s path=%s status=%s request_id=%s duration_ms=%s",
|
||||
request.method,
|
||||
request.url.path,
|
||||
status,
|
||||
request_id,
|
||||
elapsed_ms,
|
||||
)
|
||||
@@ -0,0 +1,141 @@
|
||||
import asyncio
|
||||
import logging
|
||||
import sys
|
||||
from pathlib import Path
|
||||
|
||||
from sqlalchemy import text
|
||||
from sqlalchemy.exc import ProgrammingError
|
||||
from sqlalchemy.ext.asyncio import AsyncEngine
|
||||
|
||||
from app.config import load_settings
|
||||
from app.db import build_engine, is_dependency_error
|
||||
|
||||
logger = logging.getLogger("app.migrate")
|
||||
|
||||
# Один номер на все процессы, чтобы две миграции не применяли DDL одновременно.
|
||||
_ADVISORY_LOCK = 842021
|
||||
|
||||
|
||||
def sql_statements(script: str) -> list[str]:
|
||||
lines = []
|
||||
for line in script.splitlines():
|
||||
if line.strip().startswith("--"):
|
||||
continue
|
||||
lines.append(line)
|
||||
statements = []
|
||||
for part in "\n".join(lines).split(";"):
|
||||
statement = part.strip()
|
||||
if statement:
|
||||
statements.append(statement)
|
||||
return statements
|
||||
|
||||
|
||||
def expected_versions(directory: Path) -> list[str]:
|
||||
files = sorted(path for path in directory.glob("*.sql") if path.is_file())
|
||||
if not files:
|
||||
raise SystemExit("no SQL migrations found")
|
||||
return [path.name for path in files]
|
||||
|
||||
|
||||
async def apply_migrations(engine: AsyncEngine, directory: Path) -> int:
|
||||
files = sorted(path for path in directory.glob("*.sql") if path.is_file())
|
||||
if not files:
|
||||
raise SystemExit("no SQL migrations found")
|
||||
applied = 0
|
||||
async with engine.begin() as conn:
|
||||
await conn.execute(text("SELECT pg_advisory_xact_lock(:lock_id)"), {"lock_id": _ADVISORY_LOCK})
|
||||
await conn.execute(
|
||||
text(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
version TEXT PRIMARY KEY,
|
||||
applied_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
||||
)
|
||||
"""
|
||||
)
|
||||
)
|
||||
for path in files:
|
||||
version = path.name
|
||||
exists = await conn.scalar(
|
||||
text("SELECT version FROM schema_migrations WHERE version = :version"),
|
||||
{"version": version},
|
||||
)
|
||||
if exists:
|
||||
continue
|
||||
for statement in sql_statements(path.read_text(encoding="utf-8")):
|
||||
await conn.execute(text(statement))
|
||||
await conn.execute(
|
||||
text("INSERT INTO schema_migrations (version) VALUES (:version)"),
|
||||
{"version": version},
|
||||
)
|
||||
applied += 1
|
||||
logger.info("migration applied version=%s", version)
|
||||
return applied
|
||||
|
||||
|
||||
async def wait_for_database(engine: AsyncEngine) -> None:
|
||||
for attempt in range(40):
|
||||
try:
|
||||
async with engine.connect() as conn:
|
||||
await conn.execute(text("SELECT 1"))
|
||||
return
|
||||
except Exception as exc:
|
||||
if not is_dependency_error(exc):
|
||||
logger.error("database probe failed type=%s", type(exc).__name__)
|
||||
raise SystemExit("database probe failed") from None
|
||||
logger.warning("database not ready type=%s attempt=%s", type(exc).__name__, attempt + 1)
|
||||
if attempt == 39:
|
||||
raise SystemExit("database is not ready") from None
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
|
||||
async def wait_for_schema(engine: AsyncEngine, directory: Path) -> None:
|
||||
needed = set(expected_versions(directory))
|
||||
for attempt in range(40):
|
||||
try:
|
||||
async with engine.connect() as conn:
|
||||
rows = (await conn.execute(text("SELECT version FROM schema_migrations"))).all()
|
||||
found = {row[0] for row in rows}
|
||||
if needed <= found:
|
||||
logger.info("schema ready versions=%s", ",".join(sorted(found)))
|
||||
return
|
||||
logger.warning("schema incomplete attempt=%s", attempt + 1)
|
||||
except ProgrammingError:
|
||||
logger.warning("schema_migrations missing attempt=%s", attempt + 1)
|
||||
except Exception as exc:
|
||||
if not is_dependency_error(exc):
|
||||
logger.error("schema probe failed type=%s", type(exc).__name__)
|
||||
raise SystemExit("schema probe failed") from None
|
||||
logger.warning("schema probe unavailable type=%s attempt=%s", type(exc).__name__, attempt + 1)
|
||||
if attempt == 39:
|
||||
raise SystemExit("schema is not ready")
|
||||
await asyncio.sleep(0.5)
|
||||
|
||||
|
||||
async def run_job() -> None:
|
||||
settings = load_settings()
|
||||
engine = build_engine(settings)
|
||||
try:
|
||||
await wait_for_database(engine)
|
||||
applied = await apply_migrations(engine, settings.migrations_dir)
|
||||
logger.info("migrations newly_applied=%s", applied)
|
||||
except ProgrammingError as exc:
|
||||
sqlstate = getattr(getattr(exc, "orig", None), "sqlstate", "")
|
||||
logger.error("migration rejected type=%s sqlstate=%s", type(exc).__name__, sqlstate)
|
||||
raise SystemExit("migration failed") from None
|
||||
finally:
|
||||
await engine.dispose()
|
||||
|
||||
|
||||
def main() -> None:
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format="%(asctime)s %(levelname)s %(name)s %(message)s",
|
||||
stream=sys.stdout,
|
||||
force=True,
|
||||
)
|
||||
asyncio.run(run_job())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,119 @@
|
||||
import uuid
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import (
|
||||
CheckConstraint,
|
||||
DateTime,
|
||||
ForeignKey,
|
||||
Index,
|
||||
SmallInteger,
|
||||
Text,
|
||||
UniqueConstraint,
|
||||
)
|
||||
from sqlalchemy.dialects.postgresql import UUID
|
||||
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
|
||||
|
||||
|
||||
class Base(DeclarativeBase):
|
||||
pass
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("username", name="uq_users_username"),
|
||||
CheckConstraint("username ~ '^[a-z0-9_]{3,32}$'", name="ck_users_username_format"),
|
||||
CheckConstraint(
|
||||
"char_length(btrim(display_name)) >= 1 AND char_length(display_name) <= 100",
|
||||
name="ck_users_display_name_len",
|
||||
),
|
||||
CheckConstraint("char_length(password_hash) >= 20", name="ck_users_password_hash_len"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
username: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
display_name: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
password_hash: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class Chat(Base):
|
||||
__tablename__ = "chats"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"char_length(btrim(title)) >= 1 AND char_length(title) <= 100",
|
||||
name="ck_chats_title",
|
||||
),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
title: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class ChatMember(Base):
|
||||
__tablename__ = "chat_members"
|
||||
__table_args__ = (
|
||||
CheckConstraint("ordinal >= 0 AND ordinal < 100", name="ck_chat_members_ordinal"),
|
||||
Index("ix_chat_members_user", "user_id", "chat_id"),
|
||||
)
|
||||
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("chats.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
ordinal: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
||||
|
||||
|
||||
class Message(Base):
|
||||
__tablename__ = "messages"
|
||||
__table_args__ = (
|
||||
CheckConstraint(
|
||||
"char_length(btrim(body)) >= 1 AND char_length(body) <= 4000",
|
||||
name="ck_messages_text",
|
||||
),
|
||||
# Keyset: WHERE chat_id = $1 AND (created_at, id) > ($2, $3) ORDER BY created_at, id LIMIT $4
|
||||
Index("ix_messages_history", "chat_id", "created_at", "id"),
|
||||
)
|
||||
|
||||
id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), primary_key=True)
|
||||
chat_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("chats.id"), nullable=False)
|
||||
sender_id: Mapped[uuid.UUID] = mapped_column(UUID(as_uuid=True), ForeignKey("users.id"), nullable=False)
|
||||
text: Mapped[str] = mapped_column("body", Text, nullable=False)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
|
||||
|
||||
class IdempotencyRecord(Base):
|
||||
__tablename__ = "idempotency_records"
|
||||
__table_args__ = (
|
||||
CheckConstraint("method = 'POST'", name="ck_idem_method"),
|
||||
CheckConstraint("status_code = 201", name="ck_idem_status"),
|
||||
CheckConstraint("idempotency_key ~ '^[A-Za-z0-9._:-]{1,128}$'", name="ck_idem_key"),
|
||||
CheckConstraint("char_length(request_path) BETWEEN 1 AND 300", name="ck_idem_path"),
|
||||
CheckConstraint("char_length(request_hash) = 64", name="ck_idem_hash"),
|
||||
Index("ix_idempotency_message", "message_id"),
|
||||
Index("ix_idempotency_created", "created_at"),
|
||||
)
|
||||
|
||||
user_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("users.id"),
|
||||
primary_key=True,
|
||||
)
|
||||
method: Mapped[str] = mapped_column(Text, primary_key=True)
|
||||
request_path: Mapped[str] = mapped_column(Text, primary_key=True)
|
||||
idempotency_key: Mapped[str] = mapped_column(Text, primary_key=True)
|
||||
request_hash: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
status_code: Mapped[int] = mapped_column(SmallInteger, nullable=False)
|
||||
message_id: Mapped[uuid.UUID] = mapped_column(
|
||||
UUID(as_uuid=True),
|
||||
ForeignKey("messages.id"),
|
||||
nullable=False,
|
||||
)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
|
||||
@@ -0,0 +1,50 @@
|
||||
import base64
|
||||
import json
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from app.errors import AppError
|
||||
from app.timeutil import format_time, parse_time
|
||||
|
||||
|
||||
def encode_cursor(kind: str, resource_id: UUID, created_at: datetime, entity_id: UUID) -> str:
|
||||
payload = {
|
||||
"i": str(entity_id),
|
||||
"k": kind,
|
||||
"r": str(resource_id),
|
||||
"t": format_time(created_at),
|
||||
}
|
||||
raw = json.dumps(payload, separators=(",", ":"), sort_keys=True).encode("ascii")
|
||||
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
|
||||
|
||||
|
||||
def decode_position(token: str, kind: str, resource_id: UUID) -> tuple[datetime, UUID]:
|
||||
if len(token) > 1024:
|
||||
raise AppError(400, "invalid_request", "Некорректный cursor")
|
||||
padding = "=" * (-len(token) % 4)
|
||||
try:
|
||||
data = json.loads(base64.urlsafe_b64decode(token + padding))
|
||||
if (
|
||||
not isinstance(data, dict)
|
||||
or data.get("k") != kind
|
||||
or data.get("r") != str(resource_id)
|
||||
or not isinstance(data.get("t"), str)
|
||||
or not isinstance(data.get("i"), str)
|
||||
):
|
||||
raise ValueError("cursor")
|
||||
created_at = parse_time(data["t"])
|
||||
entity_id = UUID(data["i"])
|
||||
except (KeyError, TypeError, ValueError, AttributeError, json.JSONDecodeError):
|
||||
raise AppError(400, "invalid_request", "Некорректный cursor") from None
|
||||
return created_at, entity_id
|
||||
|
||||
|
||||
def slice_page(rows: list, limit: int, kind: str, resource_id: UUID) -> dict:
|
||||
# SQL уже вернул не больше limit + 1 строк, отфильтрованных keyset-условием.
|
||||
has_more = len(rows) > limit
|
||||
page = rows[:limit]
|
||||
next_cursor = None
|
||||
if has_more and page:
|
||||
last = page[-1]
|
||||
next_cursor = encode_cursor(kind, resource_id, last.created_at, last.id)
|
||||
return {"items": [row.to_dict() for row in page], "next_cursor": next_cursor}
|
||||
@@ -0,0 +1,55 @@
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from uuid import UUID
|
||||
|
||||
from app.timeutil import format_time
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PublicUser:
|
||||
id: UUID
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: datetime
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"id": self.id,
|
||||
"username": self.username,
|
||||
"display_name": self.display_name,
|
||||
"created_at": format_time(self.created_at),
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PublicChat:
|
||||
id: UUID
|
||||
title: str
|
||||
member_ids: tuple[UUID, ...]
|
||||
created_at: datetime
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"id": self.id,
|
||||
"title": self.title,
|
||||
"member_ids": list(self.member_ids),
|
||||
"created_at": format_time(self.created_at),
|
||||
}
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
class PublicMessage:
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: datetime
|
||||
|
||||
def to_dict(self) -> dict:
|
||||
return {
|
||||
"id": self.id,
|
||||
"chat_id": self.chat_id,
|
||||
"sender_id": self.sender_id,
|
||||
"text": self.text,
|
||||
"created_at": format_time(self.created_at),
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
from app.config import Settings
|
||||
|
||||
_factory = None
|
||||
_redis = None
|
||||
_settings: Settings | None = None
|
||||
|
||||
|
||||
def configure(factory, redis, settings: Settings) -> None:
|
||||
global _factory, _redis, _settings
|
||||
_factory = factory
|
||||
_redis = redis
|
||||
_settings = settings
|
||||
|
||||
|
||||
def require_factory():
|
||||
if _factory is None:
|
||||
from app.errors import AppError
|
||||
|
||||
raise AppError(503, "unavailable", "Сервис временно недоступен")
|
||||
return _factory
|
||||
|
||||
|
||||
def require_redis():
|
||||
if _redis is None:
|
||||
from app.errors import AppError
|
||||
|
||||
raise AppError(503, "unavailable", "Сервис временно недоступен")
|
||||
return _redis
|
||||
|
||||
|
||||
def require_settings() -> Settings:
|
||||
if _settings is None:
|
||||
from app.errors import AppError
|
||||
|
||||
raise AppError(503, "unavailable", "Сервис временно недоступен")
|
||||
return _settings
|
||||
@@ -0,0 +1,35 @@
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator
|
||||
|
||||
|
||||
class CreateChat(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
title: str
|
||||
member_ids: list[UUID] = Field(min_length=1, max_length=100)
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def check_title(cls, value: str) -> str:
|
||||
if value.strip() == "" or len(value) > 100:
|
||||
raise ValueError("title")
|
||||
return value
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def unique_members(cls, value: list[UUID]) -> list[UUID]:
|
||||
if len(set(value)) != len(value):
|
||||
raise ValueError("duplicate members")
|
||||
return value
|
||||
|
||||
|
||||
class ChatOut(BaseModel):
|
||||
id: UUID
|
||||
title: str
|
||||
member_ids: list[UUID]
|
||||
created_at: str
|
||||
|
||||
|
||||
class ChatPage(BaseModel):
|
||||
items: list[ChatOut]
|
||||
next_cursor: str | None
|
||||
@@ -0,0 +1,28 @@
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, field_validator
|
||||
|
||||
|
||||
class CreateMessage(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
text: str
|
||||
|
||||
@field_validator("text")
|
||||
@classmethod
|
||||
def check_text(cls, value: str) -> str:
|
||||
if value.strip() == "" or len(value) > 4000:
|
||||
raise ValueError("text")
|
||||
return value
|
||||
|
||||
|
||||
class MessageOut(BaseModel):
|
||||
id: UUID
|
||||
chat_id: UUID
|
||||
sender_id: UUID
|
||||
text: str
|
||||
created_at: str
|
||||
|
||||
|
||||
class MessagePage(BaseModel):
|
||||
items: list[MessageOut]
|
||||
next_cursor: str | None
|
||||
@@ -0,0 +1,10 @@
|
||||
from pydantic import BaseModel
|
||||
|
||||
from app.schemas.user import UserOut
|
||||
|
||||
|
||||
class SessionOut(BaseModel):
|
||||
token: str
|
||||
token_type: str
|
||||
expires_at: str
|
||||
user: UserOut
|
||||
@@ -0,0 +1,41 @@
|
||||
import re
|
||||
|
||||
from uuid import UUID
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, field_validator
|
||||
|
||||
|
||||
class CreateUser(BaseModel):
|
||||
model_config = ConfigDict(extra="forbid")
|
||||
username: str
|
||||
display_name: str
|
||||
password: str
|
||||
|
||||
@field_validator("username")
|
||||
@classmethod
|
||||
def check_username(cls, value: str) -> str:
|
||||
if re.fullmatch(r"[a-z0-9_]{3,32}", value) is None:
|
||||
raise ValueError("username")
|
||||
return value
|
||||
|
||||
@field_validator("display_name")
|
||||
@classmethod
|
||||
def check_display_name(cls, value: str) -> str:
|
||||
if value.strip() == "" or len(value) > 100:
|
||||
raise ValueError("display_name")
|
||||
return value
|
||||
|
||||
@field_validator("password")
|
||||
@classmethod
|
||||
def check_password(cls, value: str) -> str:
|
||||
# Длина в code points. Пароль не обрезается и не нормализуется.
|
||||
if not 12 <= len(value) <= 128:
|
||||
raise ValueError("password")
|
||||
return value
|
||||
|
||||
|
||||
class UserOut(BaseModel):
|
||||
id: UUID
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: str
|
||||
@@ -0,0 +1,65 @@
|
||||
import base64
|
||||
import binascii
|
||||
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import InvalidHashError, VerifyMismatchError
|
||||
from argon2.low_level import Type
|
||||
|
||||
# Учебные параметры Argon2id: устойчивее SHA-256, но подъём на ноутбуке
|
||||
_TIME_COST = 2
|
||||
_MEMORY_COST = 32768
|
||||
_PARALLELISM = 1
|
||||
|
||||
_hasher: PasswordHasher | None = None
|
||||
_dummy_hash: str | None = None
|
||||
|
||||
|
||||
def get_hasher() -> PasswordHasher:
|
||||
global _hasher
|
||||
if _hasher is None:
|
||||
_hasher = PasswordHasher(
|
||||
time_cost=_TIME_COST,
|
||||
memory_cost=_MEMORY_COST,
|
||||
parallelism=_PARALLELISM,
|
||||
hash_len=32,
|
||||
salt_len=16,
|
||||
type=Type.ID,
|
||||
)
|
||||
return _hasher
|
||||
|
||||
|
||||
def init_security() -> None:
|
||||
global _dummy_hash
|
||||
if _dummy_hash is None:
|
||||
_dummy_hash = get_hasher().hash("messenger-dummy-password-not-used-for-login")
|
||||
|
||||
|
||||
def hash_password(password: str) -> str:
|
||||
return get_hasher().hash(password)
|
||||
|
||||
|
||||
def verify_password(password: str, password_hash: str | None) -> bool:
|
||||
init_security()
|
||||
try:
|
||||
get_hasher().verify(password_hash or _dummy_hash, password)
|
||||
matched = True
|
||||
except (VerifyMismatchError, InvalidHashError):
|
||||
matched = False
|
||||
return password_hash is not None and matched
|
||||
|
||||
|
||||
def parse_basic(header: str) -> tuple[str, str] | None:
|
||||
if not header:
|
||||
return None
|
||||
scheme, _, rest = header.partition(" ")
|
||||
if scheme.lower() != "basic" or not rest.strip():
|
||||
return None
|
||||
try:
|
||||
decoded = base64.b64decode(rest.strip(), validate=True)
|
||||
text = decoded.decode("utf-8")
|
||||
except (ValueError, UnicodeError, binascii.Error):
|
||||
return None
|
||||
username, separator, password = text.partition(":")
|
||||
if separator != ":" or len(username) > 256 or len(password) > 1024:
|
||||
return None
|
||||
return username, password
|
||||
@@ -0,0 +1,90 @@
|
||||
import hashlib
|
||||
import json
|
||||
import logging
|
||||
import secrets
|
||||
from datetime import timedelta
|
||||
from uuid import UUID
|
||||
|
||||
from redis.asyncio import Redis
|
||||
from redis.exceptions import RedisError
|
||||
|
||||
from app.config import Settings
|
||||
from app.errors import AppError
|
||||
from app.runtime import require_redis, require_settings
|
||||
from app.timeutil import format_time, parse_time, utcnow
|
||||
|
||||
logger = logging.getLogger("app.sessions")
|
||||
|
||||
|
||||
def build_redis(settings: Settings) -> Redis:
|
||||
return Redis.from_url(
|
||||
settings.cache_url,
|
||||
decode_responses=True,
|
||||
socket_timeout=settings.redis_socket_timeout_seconds,
|
||||
socket_connect_timeout=settings.redis_socket_timeout_seconds,
|
||||
health_check_interval=30,
|
||||
max_connections=settings.redis_max_connections,
|
||||
)
|
||||
|
||||
|
||||
def session_key(token: str) -> str:
|
||||
digest = hashlib.sha256(token.encode("utf-8")).hexdigest()
|
||||
return "sess:" + digest
|
||||
|
||||
|
||||
async def _call(awaitable):
|
||||
try:
|
||||
return await awaitable
|
||||
except RedisError:
|
||||
logger.warning("redis unavailable type=%s", "RedisError")
|
||||
raise AppError(503, "unavailable", "Сервис временно недоступен") from None
|
||||
|
||||
|
||||
def _read_payload(raw: str) -> tuple[UUID, object] | None:
|
||||
try:
|
||||
data = json.loads(raw)
|
||||
if not isinstance(data, dict):
|
||||
raise ValueError("payload")
|
||||
return UUID(str(data["user_id"])), parse_time(data["expires_at"])
|
||||
except (ValueError, KeyError, TypeError):
|
||||
return None
|
||||
|
||||
|
||||
async def create_session(user_id: UUID) -> tuple[str, object]:
|
||||
settings = require_settings()
|
||||
token = secrets.token_urlsafe(32)
|
||||
expires_at = utcnow() + timedelta(seconds=settings.session_ttl_seconds)
|
||||
payload = json.dumps(
|
||||
{"user_id": str(user_id), "expires_at": format_time(expires_at)},
|
||||
separators=(",", ":"),
|
||||
)
|
||||
# PXAT — абсолютное время. Повтор AOF после рестарта не продлевает TTL.
|
||||
await _call(require_redis().set(session_key(token), payload, pxat=int(expires_at.timestamp() * 1000)))
|
||||
return token, expires_at
|
||||
|
||||
|
||||
async def resolve_session(token: str) -> UUID | None:
|
||||
key = session_key(token)
|
||||
raw = await _call(require_redis().get(key))
|
||||
if raw is None:
|
||||
return None
|
||||
parsed = _read_payload(raw)
|
||||
if parsed is None:
|
||||
await _call(require_redis().delete(key))
|
||||
return None
|
||||
user_id, expires_at = parsed
|
||||
if expires_at <= utcnow():
|
||||
await _call(require_redis().delete(key))
|
||||
return None
|
||||
return user_id
|
||||
|
||||
|
||||
async def revoke_session(token: str) -> bool:
|
||||
raw = await _call(require_redis().getdel(session_key(token)))
|
||||
if raw is None:
|
||||
return False
|
||||
parsed = _read_payload(raw)
|
||||
if parsed is None:
|
||||
return False
|
||||
_, expires_at = parsed
|
||||
return expires_at > utcnow()
|
||||
@@ -0,0 +1 @@
|
||||
"""Состояние лабораторной 2 хранится в PostgreSQL и Redis, не в памяти процесса."""
|
||||
@@ -0,0 +1,5 @@
|
||||
"""Память процесса больше не хранит пользователей, чаты и сообщения.
|
||||
|
||||
Источник истины для этих данных — PostgreSQL.
|
||||
Источник истины для сессий — Redis с AOF и абсолютным PXAT.
|
||||
"""
|
||||
@@ -0,0 +1,20 @@
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
def utcnow() -> datetime:
|
||||
return datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def format_time(value: datetime) -> str:
|
||||
if value.tzinfo is None:
|
||||
value = value.replace(tzinfo=timezone.utc)
|
||||
return value.astimezone(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
||||
|
||||
|
||||
def parse_time(value: str) -> datetime:
|
||||
if not isinstance(value, str) or not value.endswith("Z"):
|
||||
raise ValueError("time")
|
||||
parsed = datetime.fromisoformat(value[:-1] + "+00:00")
|
||||
if parsed.tzinfo is None:
|
||||
raise ValueError("time")
|
||||
return parsed.astimezone(timezone.utc)
|
||||
Reference in New Issue
Block a user