Перенести исходную базу лабораторной 4 для контракта лабораторной 5
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
* text=auto
|
||||
*.sh text eol=lf
|
||||
*.py text eol=lf
|
||||
*.sql text eol=lf
|
||||
*.yaml text eol=lf
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
FROM python:3.11.10-slim-bookworm@sha256:840e180ebcc6e5c8efab209c43f5e40fd2af98cb49db5c7103c90539c56bb30e
|
||||
WORKDIR /app
|
||||
# Приложение работает без прав root.
|
||||
RUN groupadd -r appuser && useradd -r -g appuser appuser
|
||||
COPY requirements.lock.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.lock.txt
|
||||
COPY src/ src/
|
||||
COPY migrations/ migrations/
|
||||
USER appuser
|
||||
HEALTHCHECK --interval=2s --timeout=2s --start-period=10s --retries=3 CMD python -c "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready',timeout=1)"
|
||||
EXPOSE 8080
|
||||
CMD ["python", "-m", "uvicorn", "src.app:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-graceful-shutdown", "8", "--timeout-keep-alive", "2", "--no-access-log"]
|
||||
@@ -0,0 +1,17 @@
|
||||
# Reproducible fallback: official MinIO images are unavailable from registries.
|
||||
FROM golang:1.24.6-bookworm@sha256:ab1d1823abb55a9504d2e3e003b75b36dbeb1cbcc4c92593d85a84ee46becc6c AS build
|
||||
WORKDIR /build
|
||||
ADD --checksum=sha256:8819e3e7817e46b7b3798f8f200ead208562e571563c2e040352378031abe9f2 https://github.com/minio/minio/archive/07c3a429bfed433e49018cb0f78a52145d4bedeb.tar.gz /tmp/minio.tar.gz
|
||||
RUN tar -xzf /tmp/minio.tar.gz --strip-components=1 && CGO_ENABLED=0 go build -trimpath -o /out/minio .
|
||||
WORKDIR /build-mc
|
||||
ADD --checksum=sha256:526ec538e7f83e4bfa631788129b08ec85898ca572ce0523ebf9a597946bea27 https://github.com/minio/mc/archive/refs/tags/RELEASE.2025-04-16T18-13-26Z.tar.gz /tmp/mc.tar.gz
|
||||
RUN tar -xzf /tmp/mc.tar.gz --strip-components=1 && CGO_ENABLED=0 go build -trimpath -o /out/mc .
|
||||
FROM debian:bookworm-20251020-slim@sha256:78d2f66e0fec9e5a39fb2c72ea5e052b548df75602b5215ed01a17171529f706
|
||||
LABEL org.opencontainers.image.source="https://github.com/minio/minio" org.opencontainers.image.revision="07c3a429bfed433e49018cb0f78a52145d4bedeb"
|
||||
COPY --from=build /build/LICENSE /usr/share/doc/minio/LICENSE
|
||||
COPY --from=build /build-mc/LICENSE /usr/share/doc/mc/LICENSE
|
||||
COPY --from=build /out/minio /out/mc /usr/bin/
|
||||
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||
COPY infra/s3-entrypoint.sh /usr/local/bin/s3-entrypoint.sh
|
||||
ENTRYPOINT ["/bin/sh","/usr/local/bin/s3-entrypoint.sh"]
|
||||
CMD ["server","/data"]
|
||||
@@ -0,0 +1,20 @@
|
||||
#!/bin/sh
|
||||
|
||||
set -eu
|
||||
|
||||
# Не включать set -x: аргументы содержат учебные credentials.
|
||||
|
||||
until mc alias set local http://s3:9000 "$(cat /run/secrets/s3_root_user)" "$(cat /run/secrets/s3_root_password)" >/dev/null 2>&1; do sleep 1; done
|
||||
|
||||
mc mb --ignore-existing local/attachments >/dev/null
|
||||
|
||||
mc anonymous set none local/attachments >/dev/null
|
||||
|
||||
mc admin user add local "$(cat /run/secrets/s3_access_key)" "$(cat /run/secrets/s3_secret_key)" >/dev/null
|
||||
|
||||
mc admin policy create local attachments-app /bootstrap/policy.json >/dev/null
|
||||
|
||||
mc admin policy attach local attachments-app --user "$(cat /run/secrets/s3_access_key)" >/dev/null
|
||||
|
||||
echo 'Private bucket and restricted application user ready'
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
global
|
||||
log stdout format raw local0
|
||||
maxconn 1024
|
||||
defaults
|
||||
mode http
|
||||
log global
|
||||
timeout connect 1s
|
||||
timeout client 60s
|
||||
timeout server 45s
|
||||
timeout check 1s
|
||||
retries 0
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 2
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold obsolete 1s
|
||||
frontend public
|
||||
bind :8080
|
||||
default_backend application
|
||||
backend application
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
default-server inter 1s fastinter 500ms downinter 1s fall 2 rise 1
|
||||
server-template api 1-4 api:8080 check resolvers docker init-addr none
|
||||
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
export MINIO_ROOT_USER="$(cat "$MINIO_ROOT_USER_FILE")"
|
||||
export MINIO_ROOT_PASSWORD="$(cat "$MINIO_ROOT_PASSWORD_FILE")"
|
||||
exec minio "$@"
|
||||
@@ -0,0 +1 @@
|
||||
{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["s3:ListBucket"],"Resource":["arn:aws:s3:::attachments"],"Condition":{"StringLike":{"s3:prefix":["originals/*"]}}},{"Effect":"Allow","Action":["s3:GetObject","s3:PutObject","s3:DeleteObject"],"Resource":["arn:aws:s3:::attachments/originals/*"]}]}
|
||||
@@ -0,0 +1,33 @@
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name varchar(100) NOT NULL CHECK (length(btrim(display_name)) > 0),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title varchar(100) NOT NULL CHECK (length(btrim(title)) > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp()
|
||||
);
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id);
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
sender_id uuid NOT NULL REFERENCES users(id),
|
||||
text varchar(4000) NOT NULL CHECK (length(btrim(text)) > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
FOREIGN KEY (chat_id, sender_id) REFERENCES chat_members(chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX messages_history_idx ON messages (chat_id, created_at, id);
|
||||
CREATE TABLE sessions (
|
||||
token_hash char(64) PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX sessions_expiry_idx ON sessions (expires_at);
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE TABLE message_requests (
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
key varchar(128) NOT NULL,
|
||||
body text NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages(id),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (user_id, chat_id, key)
|
||||
);
|
||||
CREATE INDEX message_requests_expiry_idx ON message_requests(expires_at);
|
||||
@@ -0,0 +1,17 @@
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_limit CHECK (length(text)<=4000);
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY, chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
owner_id uuid NOT NULL REFERENCES users(id), object_key text NOT NULL UNIQUE,
|
||||
filename text NOT NULL, content_type text NOT NULL, size_bytes integer NOT NULL,
|
||||
sha256 char(64) NOT NULL, state text NOT NULL CHECK(state IN ('ready','missing')),
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
updated_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
FOREIGN KEY(chat_id,owner_id) REFERENCES chat_members(chat_id,user_id)
|
||||
);
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages(id), attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
position integer NOT NULL, PRIMARY KEY(message_id,attachment_id), UNIQUE(message_id,position)
|
||||
);
|
||||
-- Старые сохранённые ключи теперь сравнивают полное нормализованное тело.
|
||||
UPDATE message_requests SET body=json_build_object('text',body,'attachment_ids',json_build_array())::text;
|
||||
@@ -0,0 +1,34 @@
|
||||
annotated-types==0.8.0
|
||||
anyio==4.15.1
|
||||
argon2-cffi==23.1.0
|
||||
argon2-cffi-bindings==26.1.0
|
||||
cffi==2.1.1
|
||||
click==8.5.0
|
||||
fastapi==0.115.0
|
||||
h11==0.16.0
|
||||
httptools==0.8.0
|
||||
idna==3.20
|
||||
psycopg==3.2.3
|
||||
psycopg-binary==3.2.3
|
||||
psycopg-pool==3.2.4
|
||||
pycparser==3.1
|
||||
pydantic==2.9.2
|
||||
pydantic_core==2.23.4
|
||||
python-dotenv==1.2.4
|
||||
PyYAML==6.0.3
|
||||
starlette==0.38.6
|
||||
typing_extensions==4.16.0
|
||||
uvicorn==0.32.0
|
||||
uvloop==0.23.0
|
||||
watchfiles==1.3.0
|
||||
websockets==17.2
|
||||
redis==5.2.0
|
||||
|
||||
boto3==1.40.61
|
||||
Pillow==12.0.0
|
||||
botocore==1.40.76
|
||||
s3transfer==0.14.0
|
||||
jmespath==1.1.0
|
||||
python-dateutil==2.9.0.post0
|
||||
six==1.17.0
|
||||
urllib3==2.8.0
|
||||
@@ -0,0 +1,10 @@
|
||||
fastapi==0.115.0
|
||||
uvicorn[standard]==0.32.0
|
||||
pydantic==2.9.2
|
||||
psycopg[binary]==3.2.3
|
||||
psycopg-pool==3.2.4
|
||||
argon2-cffi==23.1.0
|
||||
redis==5.2.0
|
||||
|
||||
boto3==1.40.61
|
||||
Pillow==12.0.0
|
||||
+166
@@ -0,0 +1,166 @@
|
||||
import uuid
|
||||
import os
|
||||
import socket
|
||||
from contextlib import asynccontextmanager
|
||||
import psycopg
|
||||
from psycopg_pool import PoolTimeout, TooManyRequests
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
|
||||
from fastapi import FastAPI, Request
|
||||
from fastapi.exceptions import RequestValidationError
|
||||
from fastapi.responses import JSONResponse
|
||||
from pydantic import ValidationError as PydanticValidationError
|
||||
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||
|
||||
from src.models import ErrorResponse
|
||||
from src.routes import chats, health, messages, users, attachments
|
||||
from botocore.exceptions import BotoCoreError, ClientError
|
||||
from src.storage import storage
|
||||
|
||||
|
||||
def create_app() -> FastAPI:
|
||||
"""Создать и настроить приложение FastAPI."""
|
||||
@asynccontextmanager
|
||||
async def lifespan(app):
|
||||
await run_in_threadpool(storage.pool.open, wait=True)
|
||||
try:
|
||||
yield
|
||||
finally:
|
||||
await run_in_threadpool(storage.pool.close)
|
||||
|
||||
instance_id = os.getenv("INSTANCE_ID", socket.gethostname()) + "-" + uuid.uuid4().hex[:8]
|
||||
app = FastAPI(title="MTUSI Messenger Lab5", version="1.5.0", lifespan=lifespan)
|
||||
|
||||
@app.exception_handler(BotoCoreError)
|
||||
@app.exception_handler(ClientError)
|
||||
@app.exception_handler(psycopg.OperationalError)
|
||||
@app.exception_handler(psycopg.errors.QueryCanceled)
|
||||
@app.exception_handler(psycopg.errors.LockNotAvailable)
|
||||
@app.exception_handler(PoolTimeout)
|
||||
@app.exception_handler(TooManyRequests)
|
||||
async def dependency_error(request, exc):
|
||||
return JSONResponse(status_code=503, content=ErrorResponse(error={
|
||||
"code": "unavailable", "message": "хранилище временно недоступно",
|
||||
"request_id": request.state.request_id}).model_dump(),
|
||||
headers={"X-Request-Id": request.state.request_id})
|
||||
|
||||
@app.middleware("http")
|
||||
async def add_request_id(request: Request, call_next):
|
||||
"""Добавляет идентификаторы запроса и процесса, ограничивает очередь."""
|
||||
request_id = request.headers.get("X-Request-Id", "") or str(uuid.uuid4())
|
||||
request.state.request_id = request_id
|
||||
|
||||
# Ограничение числа обслуживаемых запросов в процессе.
|
||||
if not storage.acquire_request_slot():
|
||||
return JSONResponse(
|
||||
status_code=503,
|
||||
content=ErrorResponse(
|
||||
error={
|
||||
"code": "unavailable",
|
||||
"message": "сервер перегружен, попробуйте позже",
|
||||
"request_id": request_id,
|
||||
}
|
||||
).model_dump(),
|
||||
headers={"X-Request-Id": request_id, "X-Instance-Id": instance_id},
|
||||
)
|
||||
|
||||
try:
|
||||
response = await call_next(request)
|
||||
response.headers["X-Request-Id"] = request_id
|
||||
response.headers["X-Instance-Id"] = instance_id
|
||||
return response
|
||||
finally:
|
||||
storage.release_request_slot()
|
||||
|
||||
@app.exception_handler(StarletteHTTPException)
|
||||
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
|
||||
"""Обработчик HTTPException: возвращает ошибку в формате контракта {"error": ...}."""
|
||||
request_id = getattr(request.state, "request_id", str(uuid.uuid4()))
|
||||
if isinstance(exc.detail, dict) and "error" in exc.detail:
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content=exc.detail,
|
||||
headers={"X-Request-Id": request_id, **(exc.headers or {})},
|
||||
)
|
||||
return JSONResponse(
|
||||
status_code=exc.status_code,
|
||||
content=ErrorResponse(
|
||||
error={
|
||||
"code": "internal_error",
|
||||
"message": str(exc.detail),
|
||||
"request_id": request_id,
|
||||
}
|
||||
).model_dump(),
|
||||
headers={"X-Request-Id": request_id, "X-Instance-Id": instance_id},
|
||||
)
|
||||
|
||||
@app.exception_handler(RequestValidationError)
|
||||
async def request_validation_handler(request: Request, exc: RequestValidationError):
|
||||
"""Перехватываем ошибки валидации запроса (422 -> 400 по контракту)."""
|
||||
request_id = getattr(request.state, "request_id", str(uuid.uuid4()))
|
||||
errors = exc.errors()
|
||||
messages = []
|
||||
for err in errors:
|
||||
loc = ".".join(str(x) for x in err.get("loc", []))
|
||||
messages.append(f"{loc}: {err.get('msg', 'invalid')}")
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content=ErrorResponse(
|
||||
error={
|
||||
"code": "invalid_request",
|
||||
"message": "; ".join(messages),
|
||||
"request_id": request_id,
|
||||
}
|
||||
).model_dump(),
|
||||
headers={"X-Request-Id": request_id, "X-Instance-Id": instance_id},
|
||||
)
|
||||
|
||||
@app.exception_handler(PydanticValidationError)
|
||||
async def validation_exception_handler(request: Request, exc: PydanticValidationError):
|
||||
"""Перехватываем ошибки Pydantic (422 -> 400 по контракту)."""
|
||||
request_id = getattr(request.state, "request_id", str(uuid.uuid4()))
|
||||
errors = exc.errors()
|
||||
messages = []
|
||||
for err in errors:
|
||||
loc = ".".join(str(x) for x in err.get("loc", []))
|
||||
messages.append(f"{loc}: {err.get('msg', 'invalid')}")
|
||||
return JSONResponse(
|
||||
status_code=400,
|
||||
content=ErrorResponse(
|
||||
error={
|
||||
"code": "invalid_request",
|
||||
"message": "; ".join(messages),
|
||||
"request_id": request_id,
|
||||
}
|
||||
).model_dump(),
|
||||
headers={"X-Request-Id": request_id, "X-Instance-Id": instance_id},
|
||||
)
|
||||
|
||||
@app.exception_handler(Exception)
|
||||
async def generic_exception_handler(request: Request, exc: Exception):
|
||||
"""Обработчик непредвиденных исключений: скрываем детали, возвращаем 500."""
|
||||
request_id = getattr(request.state, "request_id", str(uuid.uuid4()))
|
||||
return JSONResponse(
|
||||
status_code=500,
|
||||
content=ErrorResponse(
|
||||
error={
|
||||
"code": "internal_error",
|
||||
"message": "внутренняя ошибка сервера",
|
||||
"request_id": request_id,
|
||||
}
|
||||
).model_dump(),
|
||||
headers={"X-Request-Id": request_id, "X-Instance-Id": instance_id},
|
||||
)
|
||||
|
||||
# Подключаем роуты
|
||||
app.include_router(health.router)
|
||||
app.include_router(users.router)
|
||||
app.include_router(chats.router)
|
||||
app.include_router(messages.router)
|
||||
app.include_router(attachments.router)
|
||||
|
||||
return app
|
||||
|
||||
|
||||
# Экземпляр приложения для uvicorn
|
||||
app = create_app()
|
||||
@@ -0,0 +1,60 @@
|
||||
"""Необязательный кэш неизменяемого публичного профиля; ошибки обходятся через БД."""
|
||||
import logging
|
||||
import os
|
||||
from concurrent.futures import ThreadPoolExecutor, TimeoutError as FutureTimeout
|
||||
import threading
|
||||
from redis import Redis, RedisError
|
||||
from pydantic import ValidationError
|
||||
from src.config import setting
|
||||
from src.models import User
|
||||
|
||||
|
||||
class ProfileCache:
|
||||
def __init__(self):
|
||||
url = setting('CACHE_URL')
|
||||
self.client = Redis.from_url(url, decode_responses=True, socket_connect_timeout=0.2,
|
||||
socket_timeout=0.2, max_connections=16) if url else None
|
||||
self.ttl = int(os.getenv('PROFILE_CACHE_TTL_SECONDS','60'))
|
||||
self.hits = self.misses = self.errors = 0
|
||||
# DNS может ждать дольше socket timeout; один фоновый вызов ограничивает и время, и очередь.
|
||||
self._executor = ThreadPoolExecutor(max_workers=1, thread_name_prefix='profile-cache')
|
||||
self._pending = None
|
||||
self._guard = threading.Lock()
|
||||
|
||||
def _bounded(self, action, *args, **kwargs):
|
||||
with self._guard:
|
||||
if self._pending is not None and not self._pending.done():
|
||||
raise RedisError('кэш занят')
|
||||
self._pending = self._executor.submit(action, *args, **kwargs)
|
||||
future = self._pending
|
||||
try:
|
||||
return future.result(timeout=0.25)
|
||||
except FutureTimeout:
|
||||
raise RedisError('истекло время ожидания кэша') from None
|
||||
|
||||
def get(self, user_id):
|
||||
if not self.client:
|
||||
return None
|
||||
try:
|
||||
value = self._bounded(self.client.get, 'profile:v1:'+user_id)
|
||||
if value:
|
||||
user=User.model_validate_json(value)
|
||||
if user.id != user_id:
|
||||
raise ValueError('неверный ключ кэша')
|
||||
self.hits += 1
|
||||
return user
|
||||
self.misses += 1
|
||||
except (RedisError, ValidationError, ValueError):
|
||||
self.errors += 1
|
||||
logging.getLogger(__name__).warning('Кэш профиля недоступен; чтение из PostgreSQL')
|
||||
return None
|
||||
|
||||
def put(self, user):
|
||||
if self.client:
|
||||
try:
|
||||
self._bounded(self.client.set, 'profile:v1:'+user.id,user.model_dump_json(),ex=self.ttl)
|
||||
except RedisError:
|
||||
self.errors += 1
|
||||
|
||||
|
||||
profile_cache = ProfileCache()
|
||||
@@ -0,0 +1,10 @@
|
||||
"""Настройки окружения и чтение секретов из файлов."""
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
|
||||
def setting(name, default=None):
|
||||
path = os.getenv(name + '_FILE')
|
||||
if path:
|
||||
return Path(path).read_text(encoding='utf-8').strip()
|
||||
return os.getenv(name, default)
|
||||
@@ -0,0 +1,19 @@
|
||||
"""Версионируемые миграции: одна транзакция, блокировка от параллельного запуска."""
|
||||
from pathlib import Path
|
||||
import psycopg
|
||||
from src.config import setting
|
||||
|
||||
|
||||
def main():
|
||||
with psycopg.connect(setting('DATABASE_URL'), connect_timeout=5) as conn:
|
||||
conn.execute('SELECT pg_advisory_xact_lock(2301002)')
|
||||
conn.execute('CREATE TABLE IF NOT EXISTS schema_migrations (version text PRIMARY KEY)')
|
||||
for path in sorted(Path('/app/migrations' if Path('/app/migrations').exists() else 'migrations').glob('*.sql')):
|
||||
if not conn.execute('SELECT 1 FROM schema_migrations WHERE version=%s', (path.name,)).fetchone():
|
||||
conn.execute(path.read_text(encoding='utf-8'))
|
||||
conn.execute('INSERT INTO schema_migrations VALUES (%s)', (path.name,))
|
||||
print('Применена миграция:', path.name)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
+126
@@ -0,0 +1,126 @@
|
||||
from datetime import datetime, timezone
|
||||
from typing import Optional
|
||||
import uuid
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||
|
||||
|
||||
def utc_now() -> str:
|
||||
"""Возвращает текущее время в формате RFC 3339 UTC с суффиксом Z."""
|
||||
return datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%SZ")
|
||||
|
||||
|
||||
class ErrorDetail(BaseModel):
|
||||
"""Детали ошибки API."""
|
||||
code: str
|
||||
message: str
|
||||
request_id: str
|
||||
|
||||
|
||||
class ErrorResponse(BaseModel):
|
||||
"""Стандартный ответ об ошибке."""
|
||||
error: ErrorDetail
|
||||
|
||||
|
||||
class Health(BaseModel):
|
||||
"""Ответ проверки работоспособности."""
|
||||
status: str = "ok"
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
"""Модель пользователя."""
|
||||
id: str
|
||||
username: str
|
||||
display_name: str
|
||||
created_at: str
|
||||
|
||||
|
||||
class CreateUser(BaseModel):
|
||||
"""Данные для создания пользователя."""
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
password: str = Field(..., min_length=12, max_length=128)
|
||||
username: str = Field(..., pattern=r"^[a-z0-9_]{3,32}$")
|
||||
display_name: str = Field(..., min_length=1, max_length=100)
|
||||
|
||||
@field_validator("display_name")
|
||||
@classmethod
|
||||
def display_name_not_whitespace_only(cls, v: str) -> str:
|
||||
"""Запрещаем display_name, состоящий только из пробельных символов."""
|
||||
if v.strip() == "":
|
||||
raise ValueError("display_name не может состоять только из пробелов")
|
||||
return v
|
||||
|
||||
|
||||
class Chat(BaseModel):
|
||||
"""Модель чата."""
|
||||
id: str
|
||||
title: str
|
||||
member_ids: list[str]
|
||||
created_at: str
|
||||
|
||||
|
||||
class CreateChat(BaseModel):
|
||||
"""Данные для создания чата."""
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
title: str = Field(..., min_length=1, max_length=100)
|
||||
member_ids: list[str] = Field(..., min_length=1, max_length=100)
|
||||
|
||||
@field_validator("member_ids")
|
||||
@classmethod
|
||||
def valid_members(cls, values):
|
||||
"""Проверить UUID и привести их к единому представлению."""
|
||||
return [str(uuid.UUID(value)) for value in values]
|
||||
|
||||
@field_validator("title")
|
||||
@classmethod
|
||||
def title_not_whitespace_only(cls, v: str) -> str:
|
||||
"""Запрещаем title, состоящий только из пробельных символов."""
|
||||
if v.strip() == "":
|
||||
raise ValueError("title не может состоять только из пробелов")
|
||||
return v
|
||||
|
||||
|
||||
class Message(BaseModel):
|
||||
"""Модель сообщения."""
|
||||
id: str
|
||||
chat_id: str
|
||||
sender_id: str
|
||||
text: str
|
||||
created_at: str
|
||||
attachment_ids: list[str] = Field(default_factory=list)
|
||||
|
||||
|
||||
class CreateMessage(BaseModel):
|
||||
"""Данные для отправки сообщения."""
|
||||
model_config = ConfigDict(extra="forbid", strict=True)
|
||||
|
||||
text: str = Field(..., max_length=4000)
|
||||
attachment_ids: list[str] = Field(default_factory=list, max_length=10)
|
||||
|
||||
@field_validator('attachment_ids')
|
||||
@classmethod
|
||||
def valid_attachments(cls, values):
|
||||
ids = [str(uuid.UUID(v)) for v in values]
|
||||
if len(set(ids)) != len(ids):
|
||||
raise ValueError('duplicate attachment_id')
|
||||
return ids
|
||||
|
||||
@model_validator(mode='after')
|
||||
def valid_content(self):
|
||||
if not self.text.strip() and not self.attachment_ids:
|
||||
raise ValueError('empty message')
|
||||
return self
|
||||
|
||||
|
||||
class ChatPage(BaseModel):
|
||||
"""Страница списка чатов с курсорной пагинацией."""
|
||||
items: list[Chat]
|
||||
next_cursor: Optional[str] = None
|
||||
|
||||
|
||||
class MessagePage(BaseModel):
|
||||
"""Страница истории сообщений с курсорной пагинацией."""
|
||||
items: list[Message]
|
||||
next_cursor: Optional[str] = None
|
||||
@@ -0,0 +1,17 @@
|
||||
"""S3 operations with bounded timeouts and immutable server-generated keys."""
|
||||
import boto3
|
||||
from botocore.config import Config
|
||||
from src.config import setting
|
||||
|
||||
def client():
|
||||
return boto3.client('s3', endpoint_url=setting('S3_ENDPOINT','http://s3:9000'),
|
||||
aws_access_key_id=setting('S3_ACCESS_KEY'), aws_secret_access_key=setting('S3_SECRET_KEY'),
|
||||
region_name='us-east-1', config=Config(connect_timeout=2,read_timeout=5,
|
||||
retries={'total_max_attempts':1},s3={'addressing_style':'path'}))
|
||||
|
||||
BUCKET = setting('S3_BUCKET','attachments')
|
||||
|
||||
def object_lock(conn, key, wait=True):
|
||||
fn = 'pg_advisory_xact_lock' if wait else 'pg_try_advisory_xact_lock'
|
||||
row = conn.execute('SELECT '+fn+'(hashtextextended(%s,42)) AS locked',(key,)).fetchone()
|
||||
return wait or row['locked']
|
||||
@@ -0,0 +1,139 @@
|
||||
"""Bounded synchronous image uploads; one decode per process."""
|
||||
import asyncio
|
||||
import hashlib
|
||||
import tempfile
|
||||
import unicodedata
|
||||
import uuid
|
||||
import warnings
|
||||
from PIL import Image, UnidentifiedImageError
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
from starlette.responses import StreamingResponse
|
||||
from starlette.requests import ClientDisconnect
|
||||
from src.routes.common import require_user, make_error, get_request_id
|
||||
from src.storage import storage, timestamp
|
||||
from src.objects import client, BUCKET, object_lock
|
||||
|
||||
router = APIRouter()
|
||||
slots = asyncio.Semaphore(1)
|
||||
MAX_BYTES=10*1024*1024
|
||||
Image.MAX_IMAGE_PIXELS=40_000_000
|
||||
|
||||
class InvalidImage(Exception):
|
||||
def __init__(self,status):
|
||||
self.status=status
|
||||
|
||||
def error(request, status, code, message):
|
||||
return HTTPException(status, detail=make_error(code,message,get_request_id(request)).model_dump())
|
||||
|
||||
def inspect_image(stream, declared):
|
||||
try:
|
||||
with warnings.catch_warnings():
|
||||
warnings.simplefilter('error', Image.DecompressionBombWarning)
|
||||
with Image.open(stream) as image:
|
||||
if image.width*image.height>40_000_000:
|
||||
raise InvalidImage(413)
|
||||
actual={'PNG':'image/png','JPEG':'image/jpeg'}.get(image.format)
|
||||
if not actual or actual != declared:
|
||||
raise InvalidImage(415)
|
||||
image.verify()
|
||||
stream.seek(0)
|
||||
with Image.open(stream) as image:
|
||||
image.load()
|
||||
except (Image.DecompressionBombError,Image.DecompressionBombWarning):
|
||||
raise InvalidImage(413) from None
|
||||
except (UnidentifiedImageError,OSError,SyntaxError,ValueError):
|
||||
raise InvalidImage(415) from None
|
||||
finally:
|
||||
stream.seek(0)
|
||||
|
||||
def metadata(row):
|
||||
return {k: str(row[k]) for k in ('id','chat_id','owner_id')} | {
|
||||
k:row[k] for k in ('filename','content_type','size_bytes','sha256')} | {
|
||||
'status':'ready','variants':[],'error_code':None,
|
||||
'created_at':timestamp(row['created_at']),'updated_at':timestamp(row['updated_at'])}
|
||||
|
||||
def save(stream, chat_id, owner, filename, content_type, size, digest):
|
||||
attachment_id=uuid.uuid4()
|
||||
key='originals/'+str(attachment_id)
|
||||
# Keep this lock through S3 PUT and DB COMMIT/ROLLBACK. Reconciler shares it.
|
||||
with storage.pool.connection() as conn:
|
||||
object_lock(conn,key)
|
||||
client().put_object(Bucket=BUCKET,Key=key,Body=stream,ContentLength=size,
|
||||
ContentType=content_type,Metadata={'sha256':digest},IfNoneMatch='*')
|
||||
row=conn.execute("INSERT INTO attachments(id,chat_id,owner_id,object_key,filename,content_type,size_bytes,sha256,state) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,'ready') RETURNING *",
|
||||
(attachment_id,chat_id,owner,key,filename,content_type,size,digest)).fetchone()
|
||||
# Never blindly delete after ambiguous COMMIT: grace-period reconciliation handles it.
|
||||
return metadata(row)
|
||||
|
||||
@router.post('/api/v1/chats/{chat_id}/attachments',status_code=201)
|
||||
async def upload(request:Request,chat_id:uuid.UUID,filename:str='',actor:str=Depends(require_user)):
|
||||
if not await run_in_threadpool(storage.is_chat_member,str(chat_id),actor):
|
||||
raise error(request,404,'not_found','chat not found')
|
||||
if not 1<=len(filename)<=255 or any(c in '/\\' or unicodedata.category(c)=='Cc' for c in filename):
|
||||
raise error(request,400,'invalid_request','invalid filename')
|
||||
mime=request.headers.get('content-type','').lower()
|
||||
if mime not in ('image/png','image/jpeg'):
|
||||
raise error(request,415,'unsupported_media_type','PNG/JPEG required')
|
||||
length=request.headers.get('content-length')
|
||||
if length is not None:
|
||||
if not length.isdecimal():
|
||||
raise error(request,400,'invalid_request','invalid length')
|
||||
if int(length)>MAX_BYTES:
|
||||
raise error(request,413,'payload_too_large','limit 10 MiB')
|
||||
try:
|
||||
await asyncio.wait_for(slots.acquire(),timeout=25)
|
||||
except asyncio.TimeoutError:
|
||||
raise error(request,503,'unavailable','upload queue busy') from None
|
||||
try:
|
||||
with tempfile.TemporaryFile() as stream:
|
||||
size=0; digest=hashlib.sha256()
|
||||
async def receive():
|
||||
nonlocal size
|
||||
async for chunk in request.stream():
|
||||
size+=len(chunk)
|
||||
if size>MAX_BYTES:
|
||||
raise error(request,413,'payload_too_large','limit 10 MiB')
|
||||
digest.update(chunk)
|
||||
await run_in_threadpool(stream.write,chunk)
|
||||
try:
|
||||
await asyncio.wait_for(receive(),timeout=20)
|
||||
await run_in_threadpool(stream.seek,0)
|
||||
await run_in_threadpool(inspect_image,stream,mime)
|
||||
except ClientDisconnect:
|
||||
raise error(request,400,'invalid_request','upload disconnected') from None
|
||||
except asyncio.TimeoutError:
|
||||
raise error(request,400,'invalid_request','upload timed out') from None
|
||||
except InvalidImage as exc:
|
||||
raise error(request,exc.status,'payload_too_large' if exc.status==413 else 'unsupported_media_type','invalid image') from None
|
||||
return await run_in_threadpool(save,stream,str(chat_id),actor,filename,mime,size,digest.hexdigest())
|
||||
finally:
|
||||
slots.release()
|
||||
|
||||
def accessible(request,attachment_id,actor):
|
||||
with storage.pool.connection() as conn:
|
||||
row=conn.execute('SELECT a.* FROM attachments a JOIN chat_members m ON a.chat_id=m.chat_id WHERE a.id=%s AND m.user_id=%s',(attachment_id,actor)).fetchone()
|
||||
if not row:
|
||||
raise error(request,404,'not_found','attachment not found')
|
||||
if row['state']!='ready':
|
||||
raise error(request,503,'unavailable','original temporarily unavailable')
|
||||
return row
|
||||
|
||||
@router.get('/api/v1/attachments/{attachment_id}')
|
||||
def get_attachment(request:Request,attachment_id:uuid.UUID,actor:str=Depends(require_user)):
|
||||
return metadata(accessible(request,attachment_id,actor))
|
||||
|
||||
@router.get('/api/v1/attachments/{attachment_id}/content')
|
||||
def content(request:Request,attachment_id:uuid.UUID,variant:str='original',actor:str=Depends(require_user)):
|
||||
row=accessible(request,attachment_id,actor)
|
||||
if variant not in ('original','thumbnail','crop','optimized'):
|
||||
raise error(request,400,'invalid_request','invalid variant')
|
||||
if variant!='original':
|
||||
raise error(request,404,'not_found','variant not found')
|
||||
obj=client().get_object(Bucket=BUCKET,Key=row['object_key'])
|
||||
def chunks():
|
||||
try:
|
||||
yield from obj['Body'].iter_chunks(chunk_size=64*1024)
|
||||
finally:
|
||||
obj['Body'].close()
|
||||
return StreamingResponse(chunks(),media_type=row['content_type'],headers={'Content-Length':str(row['size_bytes'])})
|
||||
@@ -0,0 +1,79 @@
|
||||
from uuid import UUID
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
|
||||
from src.models import Chat, ChatPage, CreateChat
|
||||
from src.routes.common import get_request_id, make_error, require_user
|
||||
from src.storage import LimitExceeded, ValidationError, storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/api/v1/chats", response_model=Chat, status_code=201)
|
||||
def create_chat(
|
||||
request: Request,
|
||||
body: CreateChat,
|
||||
x_user_id: str = Depends(require_user),
|
||||
):
|
||||
"""Создать чат. Создатель обязательно должен входить в member_ids."""
|
||||
request_id = get_request_id(request)
|
||||
|
||||
try:
|
||||
chat = storage.create_chat(body.title, body.member_ids, x_user_id)
|
||||
return chat
|
||||
except ValidationError as e:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=make_error("invalid_request", str(e), request_id).model_dump(),
|
||||
)
|
||||
except LimitExceeded as e:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=make_error("rate_limited", str(e), request_id).model_dump(),
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats", response_model=ChatPage)
|
||||
def list_chats(
|
||||
request: Request,
|
||||
x_user_id: str = Depends(require_user),
|
||||
limit: int = 50,
|
||||
cursor: str | None = None,
|
||||
):
|
||||
"""Список чатов текущего пользователя с курсорной пагинацией."""
|
||||
request_id = get_request_id(request)
|
||||
|
||||
if limit < 1 or limit > 100:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=make_error("invalid_request", "limit должен быть от 1 до 100", request_id).model_dump(),
|
||||
)
|
||||
|
||||
try:
|
||||
items, next_cursor = storage.list_chats_for_user(x_user_id, cursor, limit)
|
||||
return ChatPage(items=items, next_cursor=next_cursor)
|
||||
except ValidationError as e:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=make_error("invalid_request", str(e), request_id).model_dump(),
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}", response_model=Chat)
|
||||
def get_chat(
|
||||
request: Request,
|
||||
chat_id: UUID,
|
||||
x_user_id: str = Depends(require_user),
|
||||
):
|
||||
"""Получить чат по id. Доступен только участникам чата (чужой — 404)."""
|
||||
request_id = get_request_id(request)
|
||||
|
||||
chat_id_str = str(chat_id)
|
||||
chat = storage.get_chat(chat_id_str)
|
||||
if not chat or x_user_id not in chat.member_ids:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=make_error("not_found", "чат не найден", request_id).model_dump(),
|
||||
)
|
||||
|
||||
return chat
|
||||
@@ -0,0 +1,29 @@
|
||||
"""Общие ошибки и проверка Bearer-сессии; X-User-Id не используется."""
|
||||
from fastapi import HTTPException, Request
|
||||
from src.models import ErrorResponse
|
||||
from src.storage import storage
|
||||
|
||||
|
||||
def make_error(code, message, request_id):
|
||||
return ErrorResponse(error={'code': code, 'message': message, 'request_id': request_id})
|
||||
|
||||
|
||||
def get_request_id(request):
|
||||
return request.state.request_id
|
||||
|
||||
|
||||
def unauthorized(request, basic=False):
|
||||
challenge = 'Basic realm="messenger", charset="UTF-8"' if basic else 'Bearer realm="messenger"'
|
||||
raise HTTPException(401, detail=make_error('unauthorized', 'неверные учётные данные', get_request_id(request)).model_dump(), headers={'WWW-Authenticate': challenge})
|
||||
|
||||
|
||||
def require_user(request: Request):
|
||||
authorization = request.headers.get('Authorization', '')
|
||||
parts = authorization.split()
|
||||
if len(parts) != 2 or parts[0].lower() != 'bearer':
|
||||
unauthorized(request)
|
||||
user_id = storage.session_user(parts[1])
|
||||
if not user_id:
|
||||
unauthorized(request)
|
||||
request.state.token = parts[1]
|
||||
return user_id
|
||||
@@ -0,0 +1,23 @@
|
||||
"""Жизнь процесса и готовность критичного хранилища проверяются отдельно."""
|
||||
from fastapi import APIRouter
|
||||
from src.models import Health
|
||||
from src.storage import storage
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.get('/health/live')
|
||||
def liveness():
|
||||
return Health()
|
||||
|
||||
|
||||
@router.get('/health/ready')
|
||||
def readiness():
|
||||
storage.ready()
|
||||
from src.objects import client, BUCKET
|
||||
client().list_objects_v2(Bucket=BUCKET,Prefix="originals/",MaxKeys=1)
|
||||
return Health()
|
||||
|
||||
|
||||
@router.get('/stats')
|
||||
def stats():
|
||||
return storage.get_stats()
|
||||
@@ -0,0 +1,85 @@
|
||||
from uuid import UUID
|
||||
import re
|
||||
import os
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Request
|
||||
|
||||
from src.models import CreateMessage, Message, MessagePage
|
||||
from src.routes.common import get_request_id, make_error, require_user
|
||||
from src.storage import NotFoundError, ConflictError, LimitExceeded, ValidationError, storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post("/api/v1/chats/{chat_id}/messages", response_model=Message, status_code=201)
|
||||
def create_message(
|
||||
request: Request,
|
||||
chat_id: UUID,
|
||||
body: CreateMessage,
|
||||
x_user_id: str = Depends(require_user),
|
||||
):
|
||||
"""Отправить сообщение от владельца Bearer-сессии с общей защитой повторов."""
|
||||
request_id = get_request_id(request)
|
||||
|
||||
chat_id_str = str(chat_id)
|
||||
if os.getenv('OPTIMIZED_WRITES','1') == '1':
|
||||
allowed = storage.is_chat_member(chat_id_str, x_user_id)
|
||||
else:
|
||||
chat = storage.get_chat(chat_id_str)
|
||||
allowed = bool(chat and x_user_id in chat.member_ids)
|
||||
if not allowed:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=make_error("not_found", "чат не найден", request_id).model_dump(),
|
||||
)
|
||||
|
||||
try:
|
||||
key = request.headers.get('Idempotency-Key', '')
|
||||
if not re.fullmatch(r'[A-Za-z0-9._:-]{1,128}', key):
|
||||
raise HTTPException(400, detail=make_error('invalid_request', 'неверный Idempotency-Key', request_id).model_dump())
|
||||
msg = storage.create_message(chat_id_str, x_user_id, body.text, key, body.attachment_ids)
|
||||
return msg
|
||||
except NotFoundError as e:
|
||||
raise HTTPException(404, detail=make_error('not_found', str(e), request_id).model_dump())
|
||||
except ConflictError as e:
|
||||
raise HTTPException(409, detail=make_error('conflict', str(e), request_id).model_dump())
|
||||
except LimitExceeded as e:
|
||||
raise HTTPException(
|
||||
status_code=429,
|
||||
detail=make_error("rate_limited", str(e), request_id).model_dump(),
|
||||
)
|
||||
|
||||
|
||||
@router.get("/api/v1/chats/{chat_id}/messages", response_model=MessagePage)
|
||||
def list_messages(
|
||||
request: Request,
|
||||
chat_id: UUID,
|
||||
x_user_id: str = Depends(require_user),
|
||||
limit: int = 50,
|
||||
cursor: str | None = None,
|
||||
):
|
||||
"""История сообщений чата с курсорной пагинацией. Доступно только участникам."""
|
||||
request_id = get_request_id(request)
|
||||
|
||||
chat_id_str = str(chat_id)
|
||||
chat = storage.get_chat(chat_id_str)
|
||||
if not chat or x_user_id not in chat.member_ids:
|
||||
raise HTTPException(
|
||||
status_code=404,
|
||||
detail=make_error("not_found", "чат не найден", request_id).model_dump(),
|
||||
)
|
||||
|
||||
if limit < 1 or limit > 100:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=make_error("invalid_request", "limit должен быть от 1 до 100", request_id).model_dump(),
|
||||
)
|
||||
|
||||
try:
|
||||
items, next_cursor = storage.list_messages(chat_id_str, cursor, limit)
|
||||
return MessagePage(items=items, next_cursor=next_cursor)
|
||||
except ValidationError as e:
|
||||
raise HTTPException(
|
||||
status_code=400,
|
||||
detail=make_error("invalid_request", str(e), request_id).model_dump(),
|
||||
)
|
||||
@@ -0,0 +1,49 @@
|
||||
"""Регистрация, вход, текущий пользователь и отзыв сессии."""
|
||||
import base64
|
||||
import binascii
|
||||
from fastapi import APIRouter, Request, Response, HTTPException
|
||||
from src.models import CreateUser, User
|
||||
from src.routes.common import get_request_id, make_error, require_user, unauthorized
|
||||
from src.storage import ConflictError, storage
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/users', response_model=User, status_code=201)
|
||||
def register_user(request: Request, body: CreateUser):
|
||||
try:
|
||||
return storage.create_user(body.username,body.display_name,body.password)
|
||||
except ConflictError as exc:
|
||||
raise HTTPException(409,detail=make_error('conflict',str(exc),get_request_id(request)).model_dump())
|
||||
|
||||
|
||||
@router.post('/api/v1/auth/sessions', status_code=201)
|
||||
async def login(request: Request):
|
||||
if await request.body():
|
||||
raise HTTPException(400, detail=make_error('invalid_request','тело запроса не допускается',get_request_id(request)).model_dump())
|
||||
parts = request.headers.get('Authorization','').split()
|
||||
try:
|
||||
if len(parts) != 2 or parts[0].lower() != 'basic':
|
||||
unauthorized(request, basic=True)
|
||||
username,password = base64.b64decode(parts[1],validate=True).decode('utf-8').split(':',1)
|
||||
username.encode('ascii')
|
||||
except (ValueError, UnicodeError, binascii.Error):
|
||||
unauthorized(request,basic=True)
|
||||
# Вычисление KDF и синхронные запросы выполняются вне цикла событий.
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
result = await run_in_threadpool(storage.login,username,password)
|
||||
if not result:
|
||||
unauthorized(request,basic=True)
|
||||
return result
|
||||
|
||||
|
||||
@router.get('/api/v1/users/me',response_model=User)
|
||||
def current_user(request: Request):
|
||||
return storage.get_user(require_user(request))
|
||||
|
||||
|
||||
@router.delete('/api/v1/auth/sessions/current',status_code=204)
|
||||
def logout(request: Request):
|
||||
require_user(request)
|
||||
storage.logout(request.state.token)
|
||||
return Response(status_code=204)
|
||||
+276
@@ -0,0 +1,276 @@
|
||||
"""PostgreSQL — источник истины; каждый контекст подключения завершает транзакцию."""
|
||||
import base64
|
||||
import binascii
|
||||
from datetime import datetime, timezone
|
||||
import hashlib
|
||||
import json
|
||||
import os
|
||||
import secrets
|
||||
import threading
|
||||
import uuid
|
||||
|
||||
from argon2 import PasswordHasher
|
||||
from argon2.exceptions import VerifyMismatchError, VerificationError, InvalidHashError
|
||||
from psycopg.errors import UniqueViolation, ForeignKeyViolation
|
||||
from psycopg.rows import dict_row
|
||||
from psycopg_pool import ConnectionPool
|
||||
from src.config import setting
|
||||
from src.cache import profile_cache
|
||||
from src.models import User, Chat, Message
|
||||
|
||||
_CURSOR_FIELDS = {"scope", "resource_id", "created_at", "id"}
|
||||
PASSWORDS = PasswordHasher(time_cost=2, memory_cost=19456, parallelism=1)
|
||||
DUMMY_HASH = PASSWORDS.hash('synthetic-dummy-password')
|
||||
|
||||
|
||||
class ValidationError(Exception):
|
||||
"""Неверный запрос."""
|
||||
|
||||
|
||||
class ConflictError(Exception):
|
||||
"""Имя уже занято."""
|
||||
|
||||
|
||||
class LimitExceeded(Exception):
|
||||
"""Перегрузка сервера."""
|
||||
|
||||
|
||||
class NotFoundError(Exception):
|
||||
"""Объект отсутствует или недоступен."""
|
||||
|
||||
|
||||
def timestamp(value):
|
||||
return value.astimezone(timezone.utc).isoformat(timespec='microseconds').replace('+00:00', 'Z')
|
||||
|
||||
|
||||
def public_row(row):
|
||||
return {k: timestamp(v) if isinstance(v, datetime) else str(v) if isinstance(v, uuid.UUID) else v
|
||||
for k, v in row.items() if k != 'password_hash'}
|
||||
|
||||
|
||||
class PostgresStorage:
|
||||
def __init__(self):
|
||||
self.pool = ConnectionPool(setting('DATABASE_URL', ''), min_size=1, max_size=int(os.getenv("POOL_SIZE", "10")),
|
||||
timeout=3, max_waiting=32, open=False, check=ConnectionPool.check_connection,
|
||||
kwargs={'row_factory': dict_row, 'connect_timeout': 3,
|
||||
'options': '-c statement_timeout=3000 -c lock_timeout=2000'})
|
||||
self._lock = threading.Lock()
|
||||
self._requests = 0
|
||||
|
||||
@staticmethod
|
||||
def _encode_cursor(scope: str, resource_id: str, created_at: str, item_id: str) -> str:
|
||||
"""Собрать непрозрачный курсор, привязанный к конкретному ресурсу."""
|
||||
payload = {
|
||||
"scope": scope,
|
||||
"resource_id": resource_id,
|
||||
"created_at": created_at,
|
||||
"id": item_id,
|
||||
}
|
||||
raw = json.dumps(payload, separators=(",", ":"), sort_keys=True).encode("utf-8")
|
||||
return base64.urlsafe_b64encode(raw).decode("ascii")
|
||||
|
||||
@staticmethod
|
||||
def _decode_cursor(cursor: str, expected_scope: str, expected_resource_id: str) -> tuple[str, str]:
|
||||
"""Разобрать и проверить курсор.
|
||||
|
||||
Клиент видит курсор как непрозрачную строку, поэтому любые пустые,
|
||||
повреждённые или относящиеся к другому ресурсу значения отклоняем.
|
||||
"""
|
||||
if cursor == "":
|
||||
raise ValidationError("некорректный cursor")
|
||||
|
||||
allowed_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_="
|
||||
if any(char not in allowed_chars for char in cursor) or "=" in cursor.rstrip("="):
|
||||
raise ValidationError("некорректный cursor")
|
||||
|
||||
try:
|
||||
raw = base64.b64decode(cursor.encode("ascii"), altchars=b"-_", validate=True)
|
||||
payload = json.loads(raw.decode("utf-8"))
|
||||
except (UnicodeEncodeError, UnicodeDecodeError, binascii.Error, json.JSONDecodeError):
|
||||
raise ValidationError("некорректный cursor")
|
||||
|
||||
if not isinstance(payload, dict) or set(payload) != _CURSOR_FIELDS:
|
||||
raise ValidationError("некорректный cursor")
|
||||
|
||||
if not all(isinstance(payload[field], str) for field in _CURSOR_FIELDS):
|
||||
raise ValidationError("некорректный cursor")
|
||||
|
||||
if payload["scope"] != expected_scope or payload["resource_id"] != expected_resource_id:
|
||||
raise ValidationError("некорректный cursor")
|
||||
|
||||
try:
|
||||
uuid.UUID(payload["resource_id"])
|
||||
uuid.UUID(payload["id"])
|
||||
datetime.fromisoformat(payload["created_at"].replace("Z", "+00:00"))
|
||||
except ValueError:
|
||||
raise ValidationError("некорректный cursor")
|
||||
|
||||
return payload["created_at"], payload["id"]
|
||||
|
||||
|
||||
def create_user(self, username, display_name, password):
|
||||
password_hash = PASSWORDS.hash(password)
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute('INSERT INTO users (id,username,display_name,password_hash) VALUES (%s,%s,%s,%s) RETURNING id,username,display_name,created_at',
|
||||
(uuid.uuid4(), username, display_name, password_hash)).fetchone()
|
||||
return User(**public_row(row))
|
||||
except UniqueViolation:
|
||||
raise ConflictError('имя пользователя уже занято') from None
|
||||
|
||||
def get_user(self, user_id):
|
||||
cached = profile_cache.get(user_id)
|
||||
if cached:
|
||||
return cached
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute('SELECT id,username,display_name,created_at FROM users WHERE id=%s', (user_id,)).fetchone()
|
||||
user = User(**public_row(row)) if row else None
|
||||
if user:
|
||||
profile_cache.put(user)
|
||||
return user
|
||||
|
||||
def login(self, username, password):
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute('SELECT * FROM users WHERE username=%s', (username,)).fetchone()
|
||||
try:
|
||||
PASSWORDS.verify(row['password_hash'] if row else DUMMY_HASH, password)
|
||||
except (VerifyMismatchError, VerificationError, InvalidHashError):
|
||||
return None
|
||||
if not row:
|
||||
return None
|
||||
token = secrets.token_urlsafe(32)
|
||||
with self.pool.connection() as conn:
|
||||
session = conn.execute("INSERT INTO sessions VALUES (%s,%s,clock_timestamp() + %s * interval '1 second') RETURNING expires_at",
|
||||
(self.token_hash(token), row['id'], int(os.getenv('SESSION_TTL_SECONDS', '3600')))).fetchone()
|
||||
return {'token': token, 'token_type': 'Bearer', 'expires_at': timestamp(session['expires_at']), 'user': User(**public_row(row)).model_dump()}
|
||||
|
||||
@staticmethod
|
||||
def token_hash(token):
|
||||
return hashlib.sha256(token.encode()).hexdigest()
|
||||
|
||||
def session_user(self, token):
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute('SELECT user_id FROM sessions WHERE token_hash=%s AND expires_at > clock_timestamp()', (self.token_hash(token),)).fetchone()
|
||||
return str(row['user_id']) if row else None
|
||||
|
||||
def logout(self, token):
|
||||
with self.pool.connection() as conn:
|
||||
conn.execute('DELETE FROM sessions WHERE token_hash=%s', (self.token_hash(token),))
|
||||
|
||||
def create_chat(self, title, member_ids, creator_id):
|
||||
if len(set(member_ids)) != len(member_ids) or creator_id not in member_ids:
|
||||
raise ValidationError('неверный список участников')
|
||||
chat_id = uuid.uuid4()
|
||||
try:
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute('INSERT INTO chats (id,title) VALUES (%s,%s) RETURNING *', (chat_id,title)).fetchone()
|
||||
for member in member_ids:
|
||||
conn.execute('INSERT INTO chat_members VALUES (%s,%s)', (chat_id,member))
|
||||
return Chat(**public_row(row), member_ids=sorted(member_ids))
|
||||
except ForeignKeyViolation:
|
||||
raise ValidationError('участник не существует') from None
|
||||
|
||||
def get_chat(self, chat_id):
|
||||
with self.pool.connection() as conn:
|
||||
row = conn.execute('SELECT * FROM chats WHERE id=%s', (chat_id,)).fetchone()
|
||||
if not row:
|
||||
return None
|
||||
members = conn.execute('SELECT user_id FROM chat_members WHERE chat_id=%s ORDER BY user_id', (chat_id,)).fetchall()
|
||||
return Chat(**public_row(row), member_ids=[str(x['user_id']) for x in members])
|
||||
|
||||
def is_chat_member(self, chat_id, user_id):
|
||||
"""Проверить доступ без загрузки всего чата и списка участников."""
|
||||
with self.pool.connection() as conn:
|
||||
return conn.execute('SELECT 1 FROM chat_members WHERE chat_id=%s AND user_id=%s', (chat_id,user_id)).fetchone() is not None
|
||||
|
||||
def list_chats_for_user(self, user_id, cursor, limit):
|
||||
with self.pool.connection() as conn:
|
||||
params = [user_id]
|
||||
boundary = ''
|
||||
if cursor is not None:
|
||||
when, item = self._decode_cursor(cursor, 'chats', user_id)
|
||||
marker = conn.execute('SELECT c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE c.id=%s AND m.user_id=%s', (item,user_id)).fetchone()
|
||||
if not marker or timestamp(marker['created_at']) != when:
|
||||
raise ValidationError('некорректный cursor')
|
||||
boundary = ' AND (c.created_at,c.id) > (%s,%s)'
|
||||
params += [when,item]
|
||||
rows = conn.execute('SELECT c.* FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=%s' + boundary + ' ORDER BY c.created_at,c.id LIMIT %s', params+[limit+1]).fetchall()
|
||||
page = [self.get_chat(str(row['id'])) for row in rows[:limit]]
|
||||
next_cursor = self._encode_cursor('chats',user_id,page[-1].created_at,page[-1].id) if len(rows)>limit else None
|
||||
return page,next_cursor
|
||||
|
||||
def create_message(self, chat_id, sender_id, text, key, attachment_ids=None):
|
||||
attachment_ids = attachment_ids or []
|
||||
normalized = {'text': text, 'attachment_ids': attachment_ids}
|
||||
# Блокировка сериализует один ключ на всех репликах; результат и сообщение атомарны.
|
||||
with self.pool.connection() as conn:
|
||||
scope = json.dumps([sender_id, chat_id, key])
|
||||
conn.execute('SELECT pg_advisory_xact_lock(hashtextextended(%s, 0))', (scope,))
|
||||
old = conn.execute('SELECT * FROM message_requests WHERE user_id=%s AND chat_id=%s AND key=%s AND expires_at>clock_timestamp()', (sender_id,chat_id,key)).fetchone()
|
||||
if old:
|
||||
if json.loads(old['body']) != normalized:
|
||||
raise ConflictError('ключ уже использован с другим сообщением')
|
||||
row = conn.execute('SELECT * FROM messages WHERE id=%s', (old['message_id'],)).fetchone()
|
||||
return self.message_model(conn, row)
|
||||
for attachment_id in sorted(attachment_ids):
|
||||
attachment = conn.execute('SELECT state FROM attachments WHERE id=%s AND chat_id=%s FOR SHARE', (attachment_id,chat_id)).fetchone()
|
||||
if not attachment:
|
||||
raise NotFoundError('вложение не найдено')
|
||||
if attachment['state'] != 'ready':
|
||||
raise ConflictError('вложение недоступно')
|
||||
optimized = os.getenv('OPTIMIZED_WRITES','1') == '1'
|
||||
if not optimized:
|
||||
conn.execute('DELETE FROM message_requests WHERE user_id=%s AND chat_id=%s AND key=%s', (sender_id,chat_id,key))
|
||||
row = conn.execute('INSERT INTO messages (id,chat_id,sender_id,text) VALUES (%s,%s,%s,%s) RETURNING *', (uuid.uuid4(),chat_id,sender_id,text)).fetchone()
|
||||
# Под той же блокировкой UPSERT заменяет просроченный результат без отдельного DELETE.
|
||||
suffix = ' ON CONFLICT (user_id,chat_id,key) DO UPDATE SET body=EXCLUDED.body,message_id=EXCLUDED.message_id,expires_at=EXCLUDED.expires_at' if optimized else ''
|
||||
conn.execute("INSERT INTO message_requests VALUES (%s,%s,%s,%s,%s,clock_timestamp()+interval '24 hours')"+suffix, (sender_id,chat_id,key,json.dumps(normalized,ensure_ascii=False),row['id']))
|
||||
for position, attachment_id in enumerate(attachment_ids):
|
||||
conn.execute('INSERT INTO message_attachments VALUES (%s,%s,%s)', (row['id'],attachment_id,position))
|
||||
result = self.message_model(conn,row)
|
||||
return result
|
||||
|
||||
@staticmethod
|
||||
def message_model(conn, row):
|
||||
ids = conn.execute('SELECT attachment_id FROM message_attachments WHERE message_id=%s ORDER BY position', (row['id'],)).fetchall()
|
||||
return Message(**public_row(row), attachment_ids=[str(x['attachment_id']) for x in ids])
|
||||
|
||||
def list_messages(self, chat_id, cursor, limit):
|
||||
with self.pool.connection() as conn:
|
||||
params = [chat_id]
|
||||
boundary = ''
|
||||
if cursor is not None:
|
||||
when,item = self._decode_cursor(cursor,'messages',chat_id)
|
||||
marker = conn.execute('SELECT created_at FROM messages WHERE id=%s AND chat_id=%s', (item,chat_id)).fetchone()
|
||||
if not marker or timestamp(marker['created_at']) != when:
|
||||
raise ValidationError('некорректный cursor')
|
||||
boundary = ' AND (created_at,id) > (%s,%s)'
|
||||
params += [when,item]
|
||||
rows = conn.execute('SELECT * FROM messages WHERE chat_id=%s' + boundary + ' ORDER BY created_at,id LIMIT %s', params+[limit+1]).fetchall()
|
||||
page = [self.message_model(conn,row) for row in rows[:limit]]
|
||||
next_cursor = self._encode_cursor('messages',chat_id,page[-1].created_at,page[-1].id) if len(rows)>limit else None
|
||||
return page,next_cursor
|
||||
|
||||
def ready(self):
|
||||
with self.pool.connection() as conn:
|
||||
conn.execute('SELECT 1 FROM schema_migrations LIMIT 1')
|
||||
|
||||
def acquire_request_slot(self):
|
||||
with self._lock:
|
||||
if self._requests >= 100:
|
||||
return False
|
||||
self._requests += 1
|
||||
return True
|
||||
|
||||
def release_request_slot(self):
|
||||
with self._lock:
|
||||
self._requests -= 1
|
||||
|
||||
def get_stats(self):
|
||||
with self.pool.connection() as conn:
|
||||
result = {table: conn.execute('SELECT count(*) AS n FROM '+table).fetchone()['n'] for table in ('users','chats','messages')}
|
||||
result['profile_cache'] = {'hits': profile_cache.hits, 'misses': profile_cache.misses, 'errors': profile_cache.errors, 'ttl_seconds': profile_cache.ttl}
|
||||
return result
|
||||
|
||||
|
||||
storage = PostgresStorage()
|
||||
Reference in New Issue
Block a user