lab3: Compose+Nginx, две API-реплики и идемпотентность

This commit is contained in:
2026-11-11 22:14:42 +03:00
parent 0ab3a341aa
commit d4adcce4ff
26 changed files with 1409 additions and 20 deletions
View File
+245
View File
@@ -0,0 +1,245 @@
from __future__ import annotations
import base64
import binascii
import os
import re
import socket
import uuid
from contextlib import asynccontextmanager
from typing import Optional
from uuid import UUID
from fastapi import Depends, FastAPI, Header, Query, Request
from fastapi.exceptions import RequestValidationError
from fastapi.responses import JSONResponse, Response
from starlette.exceptions import HTTPException as StarletteHTTPException
from .cursors import decode_cursor, encode_cursor
from .db import close_engine, init_engine, ping
from .errors import AppError, error_response, request_id_of
from .models import CreateChat, CreateMessage, CreateUser
from .store import store
BASIC_CHALLENGE = {"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'}
BEARER_CHALLENGE = {"WWW-Authenticate": 'Bearer realm="messenger"'}
IDEMPOTENCY_KEY_RE = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
INSTANCE_ID = os.environ.get("INSTANCE_ID") or socket.gethostname()
@asynccontextmanager
async def lifespan(_app: FastAPI):
init_engine()
yield
close_engine()
app = FastAPI(title="MTUSI Messenger lab3", version="1.3.0", lifespan=lifespan)
@app.middleware("http")
async def attach_request_and_instance(request: Request, call_next):
incoming = request.headers.get("x-request-id")
request.state.request_id = incoming.strip() if incoming and incoming.strip() else str(uuid.uuid4())
response = await call_next(request)
response.headers["X-Request-Id"] = request.state.request_id
response.headers["X-Instance-Id"] = INSTANCE_ID
return response
@app.exception_handler(AppError)
async def app_error_handler(request: Request, exc: AppError) -> JSONResponse:
return error_response(
exc.status_code,
exc.code,
exc.message,
request_id_of(request),
headers=exc.headers,
)
@app.exception_handler(RequestValidationError)
async def validation_error_handler(request: Request, exc: RequestValidationError) -> JSONResponse:
return error_response(
400,
"invalid_request",
"Request validation failed",
request_id_of(request),
)
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException) -> JSONResponse:
code = "not_found" if exc.status_code == 404 else "invalid_request"
headers = None
if exc.status_code == 401:
code = "unauthorized"
headers = BEARER_CHALLENGE
elif exc.status_code >= 500:
code = "internal_error"
message = exc.detail if isinstance(exc.detail, str) else "Request failed"
return error_response(
exc.status_code,
code,
message,
request_id_of(request),
headers=headers,
)
@app.exception_handler(Exception)
async def unhandled_error_handler(request: Request, exc: Exception) -> JSONResponse:
return error_response(500, "internal_error", "Internal server error", request_id_of(request))
def current_user_id(authorization: Optional[str] = Header(default=None)) -> str:
if authorization is None or not authorization.strip():
raise AppError(401, "unauthorized", "Missing bearer token", headers=BEARER_CHALLENGE)
scheme, _, credential = authorization.partition(" ")
if scheme.lower() != "bearer" or not credential.strip():
raise AppError(401, "unauthorized", "Bearer token required", headers=BEARER_CHALLENGE)
return store.resolve_bearer(credential.strip())
def require_chat_member(chat_id: UUID, user_id: str):
chat = store.get_chat(str(chat_id))
if chat is None or user_id not in chat.member_ids:
raise AppError(404, "not_found", "Chat not found")
return chat
def parse_basic_credentials(authorization: Optional[str]) -> tuple[str, str]:
if authorization is None or not authorization.strip():
raise AppError(401, "unauthorized", "Missing basic credentials", headers=BASIC_CHALLENGE)
scheme, _, credential = authorization.partition(" ")
if scheme.lower() != "basic" or not credential.strip():
raise AppError(401, "unauthorized", "Basic credentials required", headers=BASIC_CHALLENGE)
try:
decoded = base64.b64decode(credential.strip(), validate=True).decode("utf-8")
except (binascii.Error, UnicodeDecodeError, ValueError) as exc:
raise AppError(401, "unauthorized", "Invalid basic credentials", headers=BASIC_CHALLENGE) from exc
if ":" not in decoded:
raise AppError(401, "unauthorized", "Invalid basic credentials", headers=BASIC_CHALLENGE)
username, password = decoded.split(":", 1)
if not username or not username.isascii():
raise AppError(401, "unauthorized", "Invalid basic credentials", headers=BASIC_CHALLENGE)
return username, password
def require_idempotency_key(idempotency_key: Optional[str] = Header(default=None, alias="Idempotency-Key")) -> str:
if idempotency_key is None or not idempotency_key.strip():
raise AppError(400, "invalid_request", "Idempotency-Key is required")
key = idempotency_key.strip()
if not IDEMPOTENCY_KEY_RE.fullmatch(key):
raise AppError(400, "invalid_request", "Idempotency-Key format is invalid")
return key
@app.get("/health/live")
def health_live():
return {"status": "ok"}
@app.get("/health/ready")
def health_ready():
if not ping():
raise AppError(503, "unavailable", "Database is unavailable")
return {"status": "ok"}
@app.post("/api/v1/users", status_code=201)
def register_user(body: CreateUser):
user = store.create_user(body.username, body.display_name, body.password)
return user.to_dict()
@app.post("/api/v1/auth/sessions", status_code=201)
def create_session(authorization: Optional[str] = Header(default=None)):
username, password = parse_basic_credentials(authorization)
session = store.create_session(username, password)
return session.to_dict()
@app.delete("/api/v1/auth/sessions/current", status_code=204)
def revoke_session(authorization: Optional[str] = Header(default=None)):
if authorization is None or not authorization.strip():
raise AppError(401, "unauthorized", "Missing bearer token", headers=BEARER_CHALLENGE)
scheme, _, credential = authorization.partition(" ")
if scheme.lower() != "bearer" or not credential.strip():
raise AppError(401, "unauthorized", "Bearer token required", headers=BEARER_CHALLENGE)
store.revoke_session(credential.strip())
return Response(status_code=204)
@app.get("/api/v1/users/me")
def get_me(user_id: str = Depends(current_user_id)):
user = store.get_user(user_id)
if user is None:
raise AppError(401, "unauthorized", "Unknown user", headers=BEARER_CHALLENGE)
return user.to_dict()
@app.post("/api/v1/chats", status_code=201)
def create_chat(body: CreateChat, user_id: str = Depends(current_user_id)):
member_ids = [str(m) for m in body.member_ids]
if user_id not in member_ids:
raise AppError(400, "invalid_request", "Caller must be included in member_ids")
chat = store.create_chat(body.title, member_ids)
return chat.to_dict()
@app.get("/api/v1/chats")
def list_chats(
user_id: str = Depends(current_user_id),
limit: int = Query(default=50, ge=1, le=100),
cursor: Optional[str] = Query(default=None, min_length=1),
):
resource = f"chats:{user_id}"
after = decode_cursor(cursor, resource) if cursor else None
page, last_for_cursor = store.list_chats_for_user(user_id, limit, after)
next_cursor = None
if last_for_cursor is not None:
next_cursor = encode_cursor(last_for_cursor.created_at, last_for_cursor.id, resource)
return {"items": [c.to_dict() for c in page], "next_cursor": next_cursor}
@app.get("/api/v1/chats/{chat_id}")
def get_chat(chat_id: UUID, user_id: str = Depends(current_user_id)):
chat = require_chat_member(chat_id, user_id)
return chat.to_dict()
@app.post("/api/v1/chats/{chat_id}/messages", status_code=201)
def create_message(
chat_id: UUID,
body: CreateMessage,
request: Request,
user_id: str = Depends(current_user_id),
idempotency_key: str = Depends(require_idempotency_key),
):
require_chat_member(chat_id, user_id)
message = store.create_message(
str(chat_id),
user_id,
body.text,
idempotency_key=idempotency_key,
request_path=request.url.path,
)
return message.to_dict()
@app.get("/api/v1/chats/{chat_id}/messages")
def list_messages(
chat_id: UUID,
user_id: str = Depends(current_user_id),
limit: int = Query(default=50, ge=1, le=100),
cursor: Optional[str] = Query(default=None, min_length=1),
):
require_chat_member(chat_id, user_id)
resource = f"messages:{chat_id}"
after = decode_cursor(cursor, resource) if cursor else None
page, last_for_cursor = store.list_messages(str(chat_id), limit, after)
next_cursor = None
if last_for_cursor is not None:
next_cursor = encode_cursor(last_for_cursor.created_at, last_for_cursor.id, resource)
return {"items": [m.to_dict() for m in page], "next_cursor": next_cursor}
+34
View File
@@ -0,0 +1,34 @@
from __future__ import annotations
import base64
import json
from typing import Tuple
from .errors import AppError
def encode_cursor(created_at: str, item_id: str, resource: str) -> str:
raw = json.dumps(
{"t": created_at, "i": item_id, "r": resource},
separators=(",", ":"),
ensure_ascii=False,
).encode("utf-8")
return base64.urlsafe_b64encode(raw).decode("ascii").rstrip("=")
def decode_cursor(cursor: str, expected_resource: str) -> Tuple[str, str]:
try:
padded = cursor + "=" * (-len(cursor) % 4)
data = json.loads(base64.urlsafe_b64decode(padded.encode("ascii")).decode("utf-8"))
created_at = data["t"]
item_id = data["i"]
resource = data["r"]
if not isinstance(created_at, str) or not isinstance(item_id, str):
raise ValueError("bad cursor fields")
if resource != expected_resource:
raise AppError(400, "invalid_request", "Cursor does not belong to this resource")
return created_at, item_id
except AppError:
raise
except Exception as exc:
raise AppError(400, "invalid_request", "Invalid cursor") from exc
+115
View File
@@ -0,0 +1,115 @@
from __future__ import annotations
import os
from contextlib import contextmanager
from pathlib import Path
from typing import Iterator, Optional
from sqlalchemy import create_engine, text
from sqlalchemy.engine import Engine
from sqlalchemy.orm import Session, sessionmaker
_engine: Optional[Engine] = None
_SessionLocal: Optional[sessionmaker[Session]] = None
def _read_database_url() -> str:
url_file = os.environ.get("DATABASE_URL_FILE")
if url_file:
url = Path(url_file).read_text(encoding="utf-8").strip()
else:
url = os.environ.get("DATABASE_URL")
if not url:
raise RuntimeError("DATABASE_URL or DATABASE_URL_FILE is required")
url = url.strip()
if url.startswith("postgresql://"):
url = "postgresql+psycopg://" + url[len("postgresql://") :]
elif url.startswith("postgres://"):
url = "postgresql+psycopg://" + url[len("postgres://") :]
return url
def init_engine() -> Engine:
global _engine, _SessionLocal
if _engine is not None:
return _engine
_engine = create_engine(
_read_database_url(),
pool_size=5,
max_overflow=5,
pool_pre_ping=True,
pool_timeout=10,
)
_SessionLocal = sessionmaker(bind=_engine, expire_on_commit=False)
return _engine
def get_engine() -> Engine:
if _engine is None:
raise RuntimeError("database engine is not initialized")
return _engine
@contextmanager
def session_scope() -> Iterator[Session]:
if _SessionLocal is None:
raise RuntimeError("database engine is not initialized")
session = _SessionLocal()
try:
yield session
session.commit()
except Exception:
session.rollback()
raise
finally:
session.close()
def close_engine() -> None:
global _engine, _SessionLocal
if _engine is not None:
_engine.dispose()
_engine = None
_SessionLocal = None
def migrations_dir() -> Path:
return Path(__file__).resolve().parents[2] / "migrations"
def apply_migrations() -> None:
engine = get_engine()
with engine.begin() as conn:
conn.execute(
text(
"""
CREATE TABLE IF NOT EXISTS schema_migrations (
version TEXT PRIMARY KEY,
applied_at TIMESTAMPTZ NOT NULL DEFAULT NOW()
)
"""
)
)
applied = {
row[0]
for row in conn.execute(text("SELECT version FROM schema_migrations")).fetchall()
}
for path in sorted(migrations_dir().glob("*.sql")):
version = path.name
if version in applied:
continue
conn.exec_driver_sql(path.read_text(encoding="utf-8"))
conn.execute(
text("INSERT INTO schema_migrations (version) VALUES (:version)"),
{"version": version},
)
def ping() -> bool:
try:
engine = get_engine()
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
return True
except Exception:
return False
+45
View File
@@ -0,0 +1,45 @@
from __future__ import annotations
from typing import Optional
from fastapi import Request
from fastapi.responses import JSONResponse
class AppError(Exception):
def __init__(
self,
status_code: int,
code: str,
message: str,
headers: Optional[dict] = None,
) -> None:
self.status_code = status_code
self.code = code
self.message = message
self.headers = headers
super().__init__(message)
def request_id_of(request: Request) -> str:
return getattr(request.state, "request_id", "unknown")
def error_response(
status_code: int,
code: str,
message: str,
request_id: str,
headers: Optional[dict] = None,
) -> JSONResponse:
payload = {
"error": {
"code": code,
"message": message,
"request_id": request_id,
}
}
response_headers = {"X-Request-Id": request_id}
if headers:
response_headers.update(headers)
return JSONResponse(status_code=status_code, content=payload, headers=response_headers)
+72
View File
@@ -0,0 +1,72 @@
from __future__ import annotations
import re
from typing import List
from uuid import UUID
from pydantic import BaseModel, ConfigDict, Field, field_validator
USERNAME_RE = re.compile(r"^[a-z0-9_]{3,32}$")
def _not_blank(value: str, field_name: str) -> str:
if value.strip() == "":
raise ValueError(f"{field_name} must not be blank")
return value
class CreateUser(BaseModel):
model_config = ConfigDict(extra="forbid")
username: str
display_name: str = Field(min_length=1, max_length=100)
password: str = Field(min_length=12, max_length=128)
@field_validator("username")
@classmethod
def validate_username(cls, value: str) -> str:
if not USERNAME_RE.fullmatch(value):
raise ValueError("username must match [a-z0-9_]{3,32}")
return value
@field_validator("display_name")
@classmethod
def validate_display_name(cls, value: str) -> str:
return _not_blank(value, "display_name")
@field_validator("password")
@classmethod
def validate_password(cls, value: str) -> str:
if len(value) < 12 or len(value) > 128:
raise ValueError("password length must be 12-128")
return value
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:
return _not_blank(value, "title")
@field_validator("member_ids")
@classmethod
def validate_members(cls, value: List[UUID]) -> List[UUID]:
if len(value) != len(set(value)):
raise ValueError("member_ids must be unique")
return value
class CreateMessage(BaseModel):
model_config = ConfigDict(extra="forbid")
text: str = Field(min_length=1, max_length=4000)
@field_validator("text")
@classmethod
def validate_text(cls, value: str) -> str:
return _not_blank(value, "text")
+404
View File
@@ -0,0 +1,404 @@
from __future__ import annotations
import hashlib
import json
import os
import secrets
import uuid
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import List, Optional, Tuple
from uuid import UUID
from argon2 import PasswordHasher
from argon2.exceptions import VerifyMismatchError
from sqlalchemy import delete, func, select, tuple_
from sqlalchemy.exc import IntegrityError
from .db import session_scope
from .errors import AppError
from .tables import (
ChatMemberRow,
ChatRow,
IdempotencyRow,
MessageRow,
SessionRow,
UserRow,
)
password_hasher = PasswordHasher()
def utc_now() -> datetime:
return datetime.now(timezone.utc)
def format_ts(value: datetime) -> str:
if value.tzinfo is None:
value = value.replace(tzinfo=timezone.utc)
return value.astimezone(timezone.utc).isoformat(timespec="microseconds").replace("+00:00", "Z")
def parse_ts(value: str) -> datetime:
return datetime.fromisoformat(value.replace("Z", "+00:00"))
def session_ttl_seconds() -> int:
raw = os.environ.get("SESSION_TTL_SECONDS", "3600")
return max(1, int(raw))
def idempotency_ttl_seconds() -> int:
raw = os.environ.get("IDEMPOTENCY_TTL_SECONDS", "86400")
return max(1, int(raw))
def hash_token(token: str) -> str:
return hashlib.sha256(token.encode("utf-8")).hexdigest()
def request_body_hash(text: str) -> str:
normalized = json.dumps({"text": text, "attachment_ids": []}, separators=(",", ":"), ensure_ascii=False)
return hashlib.sha256(normalized.encode("utf-8")).hexdigest()
def idempotency_scope(user_id: str, method: str, path: str, key: str) -> str:
return f"{user_id}|{method.upper()}|{path}|{key}"
@dataclass
class User:
id: str
username: str
display_name: str
created_at: str
def to_dict(self) -> dict:
return {
"id": self.id,
"username": self.username,
"display_name": self.display_name,
"created_at": self.created_at,
}
@dataclass
class Chat:
id: str
title: str
member_ids: List[str]
created_at: str
def to_dict(self) -> dict:
return {
"id": self.id,
"title": self.title,
"member_ids": list(self.member_ids),
"created_at": self.created_at,
}
@dataclass
class Message:
id: str
chat_id: str
sender_id: str
text: str
created_at: str
def to_dict(self) -> dict:
return {
"id": self.id,
"chat_id": self.chat_id,
"sender_id": self.sender_id,
"text": self.text,
"created_at": self.created_at,
}
@dataclass
class Session:
token: str
token_type: str
expires_at: str
user: User
def to_dict(self) -> dict:
return {
"token": self.token,
"token_type": self.token_type,
"expires_at": self.expires_at,
"user": self.user.to_dict(),
}
def _user_from_row(row: UserRow) -> User:
return User(
id=str(row.id),
username=row.username,
display_name=row.display_name,
created_at=format_ts(row.created_at),
)
def _chat_from_row(row: ChatRow, member_ids: List[str]) -> Chat:
return Chat(
id=str(row.id),
title=row.title,
member_ids=member_ids,
created_at=format_ts(row.created_at),
)
def _message_from_row(row: MessageRow) -> Message:
return Message(
id=str(row.id),
chat_id=str(row.chat_id),
sender_id=str(row.sender_id),
text=row.text,
created_at=format_ts(row.created_at),
)
def _member_ids(session, chat_id: UUID) -> List[str]:
rows = session.scalars(
select(ChatMemberRow.user_id)
.where(ChatMemberRow.chat_id == chat_id)
.order_by(ChatMemberRow.user_id)
).all()
return [str(user_id) for user_id in rows]
def _aware(value: datetime) -> datetime:
if value.tzinfo is None:
return value.replace(tzinfo=timezone.utc)
return value
class Store:
def create_user(self, username: str, display_name: str, password: str) -> User:
row = UserRow(
id=uuid.uuid4(),
username=username,
display_name=display_name,
password_hash=password_hasher.hash(password),
created_at=utc_now(),
)
try:
with session_scope() as session:
session.add(row)
session.flush()
return _user_from_row(row)
except IntegrityError as exc:
raise AppError(409, "conflict", "Username is already taken") from exc
def get_user(self, user_id: str) -> Optional[User]:
with session_scope() as session:
row = session.get(UserRow, UUID(user_id))
return _user_from_row(row) if row else None
def create_session(self, username: str, password: str) -> Session:
with session_scope() as session:
row = session.scalar(select(UserRow).where(UserRow.username == username))
if row is None:
raise AppError(
401,
"unauthorized",
"Invalid credentials",
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'},
)
try:
password_hasher.verify(row.password_hash, password)
except VerifyMismatchError as exc:
raise AppError(
401,
"unauthorized",
"Invalid credentials",
headers={"WWW-Authenticate": 'Basic realm="messenger", charset="UTF-8"'},
) from exc
token = secrets.token_urlsafe(32)
expires_at = utc_now() + timedelta(seconds=session_ttl_seconds())
session.add(
SessionRow(
token_hash=hash_token(token),
user_id=row.id,
expires_at=expires_at,
created_at=utc_now(),
)
)
user = _user_from_row(row)
return Session(
token=token,
token_type="Bearer",
expires_at=format_ts(expires_at),
user=user,
)
def resolve_bearer(self, token: str) -> str:
with session_scope() as session:
row = session.get(SessionRow, hash_token(token))
challenge = {"WWW-Authenticate": 'Bearer realm="messenger"'}
if row is None:
raise AppError(401, "unauthorized", "Invalid session", headers=challenge)
if row.revoked_at is not None:
raise AppError(401, "unauthorized", "Session revoked", headers=challenge)
if _aware(row.expires_at) <= utc_now():
raise AppError(401, "unauthorized", "Session expired", headers=challenge)
return str(row.user_id)
def revoke_session(self, token: str) -> None:
with session_scope() as session:
row = session.get(SessionRow, hash_token(token), with_for_update=True)
challenge = {"WWW-Authenticate": 'Bearer realm="messenger"'}
if row is None:
raise AppError(401, "unauthorized", "Invalid session", headers=challenge)
if row.revoked_at is not None:
raise AppError(401, "unauthorized", "Session revoked", headers=challenge)
if _aware(row.expires_at) <= utc_now():
raise AppError(401, "unauthorized", "Session expired", headers=challenge)
row.revoked_at = utc_now()
def create_chat(self, title: str, member_ids: List[str]) -> Chat:
chat_id = uuid.uuid4()
member_uuids = [UUID(m) for m in member_ids]
with session_scope() as session:
existing = session.scalar(
select(func.count()).select_from(UserRow).where(UserRow.id.in_(member_uuids))
)
if existing != len(member_uuids):
raise AppError(400, "invalid_request", "Unknown member in member_ids")
chat = ChatRow(id=chat_id, title=title, created_at=utc_now())
session.add(chat)
for member_id in member_uuids:
session.add(ChatMemberRow(chat_id=chat_id, user_id=member_id))
session.flush()
return _chat_from_row(chat, list(member_ids))
def get_chat(self, chat_id: str) -> Optional[Chat]:
with session_scope() as session:
row = session.get(ChatRow, UUID(chat_id))
if row is None:
return None
return _chat_from_row(row, _member_ids(session, row.id))
def list_chats_for_user(
self,
user_id: str,
limit: int,
after: Optional[Tuple[str, str]] = None,
) -> Tuple[List[Chat], Optional[Chat]]:
with session_scope() as session:
stmt = (
select(ChatRow)
.join(ChatMemberRow, ChatMemberRow.chat_id == ChatRow.id)
.where(ChatMemberRow.user_id == UUID(user_id))
)
if after is not None:
stmt = stmt.where(
tuple_(ChatRow.created_at, ChatRow.id)
> (parse_ts(after[0]), UUID(after[1]))
)
stmt = stmt.order_by(ChatRow.created_at.asc(), ChatRow.id.asc()).limit(limit + 1)
rows = list(session.scalars(stmt).all())
chats = [_chat_from_row(row, _member_ids(session, row.id)) for row in rows]
page = chats[:limit]
has_more = len(chats) > limit
last = page[-1] if page and has_more else None
return page, last
def create_message(
self,
chat_id: str,
sender_id: str,
text: str,
*,
idempotency_key: str,
request_path: str,
) -> Message:
scope = idempotency_scope(sender_id, "POST", request_path, idempotency_key)
body_hash = request_body_hash(text)
for _ in range(5):
try:
with session_scope() as session:
existing = session.get(IdempotencyRow, scope, with_for_update=True)
if existing is not None:
if _aware(existing.expires_at) > utc_now():
if existing.request_hash != body_hash:
raise AppError(
409,
"conflict",
"Idempotency-Key reused with a different request body",
)
message = session.get(MessageRow, existing.message_id)
if message is None:
raise AppError(500, "internal_error", "Idempotent message missing")
return _message_from_row(message)
session.execute(
delete(IdempotencyRow).where(IdempotencyRow.scope_key == scope)
)
session.flush()
message = MessageRow(
id=uuid.uuid4(),
chat_id=UUID(chat_id),
sender_id=UUID(sender_id),
text=text,
created_at=utc_now(),
)
session.add(message)
session.flush()
session.add(
IdempotencyRow(
scope_key=scope,
request_hash=body_hash,
message_id=message.id,
created_at=utc_now(),
expires_at=utc_now() + timedelta(seconds=idempotency_ttl_seconds()),
)
)
session.flush()
return _message_from_row(message)
except IntegrityError:
continue
with session_scope() as session:
existing = session.get(IdempotencyRow, scope)
if existing is not None and _aware(existing.expires_at) > utc_now():
if existing.request_hash != body_hash:
raise AppError(
409,
"conflict",
"Idempotency-Key reused with a different request body",
)
message = session.get(MessageRow, existing.message_id)
if message is not None:
return _message_from_row(message)
raise AppError(503, "unavailable", "Could not complete idempotent write")
def list_messages(
self,
chat_id: str,
limit: int,
after: Optional[Tuple[str, str]] = None,
) -> Tuple[List[Message], Optional[Message]]:
with session_scope() as session:
stmt = select(MessageRow).where(MessageRow.chat_id == UUID(chat_id))
if after is not None:
stmt = stmt.where(
tuple_(MessageRow.created_at, MessageRow.id)
> (parse_ts(after[0]), UUID(after[1]))
)
stmt = stmt.order_by(MessageRow.created_at.asc(), MessageRow.id.asc()).limit(
limit + 1
)
rows = list(session.scalars(stmt).all())
messages = [_message_from_row(row) for row in rows]
page = messages[:limit]
has_more = len(messages) > limit
last = page[-1] if page and has_more else None
return page, last
store = Store()
+73
View File
@@ -0,0 +1,73 @@
from __future__ import annotations
import uuid
from datetime import datetime
from typing import List, Optional
from sqlalchemy import DateTime, ForeignKey, String, Text, Uuid
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
pass
class UserRow(Base):
__tablename__ = "users"
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True)
username: Mapped[str] = mapped_column(Text, nullable=False, unique=True)
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 ChatRow(Base):
__tablename__ = "chats"
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True)
title: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
members: Mapped[List["ChatMemberRow"]] = relationship(back_populates="chat")
class ChatMemberRow(Base):
__tablename__ = "chat_members"
chat_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("chats.id"), primary_key=True
)
user_id: Mapped[uuid.UUID] = mapped_column(
Uuid, ForeignKey("users.id"), primary_key=True
)
chat: Mapped[ChatRow] = relationship(back_populates="members")
class MessageRow(Base):
__tablename__ = "messages"
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True)
chat_id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("chats.id"), nullable=False)
sender_id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("users.id"), nullable=False)
text: Mapped[str] = mapped_column(Text, nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class SessionRow(Base):
__tablename__ = "sessions"
token_hash: Mapped[str] = mapped_column(String, primary_key=True)
user_id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("users.id"), nullable=False)
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
revoked_at: Mapped[Optional[datetime]] = mapped_column(DateTime(timezone=True), nullable=True)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
class IdempotencyRow(Base):
__tablename__ = "idempotency_records"
scope_key: Mapped[str] = mapped_column(Text, primary_key=True)
request_hash: Mapped[str] = mapped_column(Text, nullable=False)
message_id: Mapped[uuid.UUID] = mapped_column(Uuid, ForeignKey("messages.id"), nullable=False)
created_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)
expires_at: Mapped[datetime] = mapped_column(DateTime(timezone=True), nullable=False)