forked from mtusi2026/mtusi2026lab4
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4979731408 | ||
|
|
9b574fa5e9 |
+16
-9
@@ -1,20 +1,27 @@
|
|||||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
|
||||||
APP_HOST=0.0.0.0
|
APP_HOST=0.0.0.0
|
||||||
APP_PORT=8080
|
APP_PORT=8080
|
||||||
APP_ENV=lab
|
APP_ENV=lab
|
||||||
SESSION_TTL_SECONDS=3600
|
SESSION_TTL_SECONDS=3600
|
||||||
# Choose credentials locally. File paths contain no secret values.
|
|
||||||
DATABASE_URL_FILE=/run/secrets/database_url
|
# PostgreSQL credentials and database configuration
|
||||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
POSTGRES_USER=postgres
|
||||||
SESSION_STORE=postgres
|
POSTGRES_PASSWORD=postgres
|
||||||
CACHE_URL_FILE=/run/secrets/cache_url
|
POSTGRES_DB=messenger
|
||||||
|
DATABASE_URL=postgresql://postgres:postgres@postgres:5432/messenger
|
||||||
|
|
||||||
|
# Redis session store configuration
|
||||||
|
SESSION_STORE=redis
|
||||||
|
CACHE_URL=redis://redis:6379/0
|
||||||
|
|
||||||
API_REPLICAS=2
|
API_REPLICAS=2
|
||||||
IDEMPOTENCY_TTL_SECONDS=86400
|
IDEMPOTENCY_TTL_SECONDS=86400
|
||||||
S3_ENDPOINT=http://s3:9000
|
|
||||||
|
# MinIO configuration
|
||||||
|
S3_ENDPOINT=http://minio:9000
|
||||||
S3_REGION=us-east-1
|
S3_REGION=us-east-1
|
||||||
S3_BUCKET=messenger-attachments
|
S3_BUCKET=messenger-attachments
|
||||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
S3_ACCESS_KEY_ID=minioadmin
|
||||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
S3_SECRET_ACCESS_KEY=minioadmin
|
||||||
MAX_UPLOAD_BYTES=10485760
|
MAX_UPLOAD_BYTES=10485760
|
||||||
MAX_IMAGE_PIXELS=40000000
|
MAX_IMAGE_PIXELS=40000000
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ bin/
|
|||||||
obj/
|
obj/
|
||||||
coverage/
|
coverage/
|
||||||
*.log
|
*.log
|
||||||
|
!evidence/*.log
|
||||||
.DS_Store
|
.DS_Store
|
||||||
.idea/
|
.idea/
|
||||||
.vscode/
|
.vscode/
|
||||||
|
|||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
FROM python:3.12-slim
|
||||||
|
|
||||||
|
ENV PYTHONDONTWRITEBYTECODE=1 \
|
||||||
|
PYTHONUNBUFFERED=1 \
|
||||||
|
APP_HOST=0.0.0.0 \
|
||||||
|
APP_PORT=8080
|
||||||
|
|
||||||
|
RUN groupadd -g 10001 appuser && \
|
||||||
|
useradd -u 10001 -g appuser -s /sbin/nologin -M appuser
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY requirements.txt .
|
||||||
|
RUN pip install --no-cache-dir -r requirements.txt
|
||||||
|
|
||||||
|
COPY app/ ./app/
|
||||||
|
|
||||||
|
RUN chown -R appuser:appuser /app
|
||||||
|
|
||||||
|
USER appuser
|
||||||
|
|
||||||
|
EXPOSE 8080
|
||||||
|
|
||||||
|
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||||
|
CMD python -c "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/health/ready')" || exit 1
|
||||||
|
|
||||||
|
CMD ["python", "-m", "uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080", "--timeout-graceful-shutdown", "10"]
|
||||||
@@ -4,10 +4,12 @@ CA_FILE ?=
|
|||||||
ACTION_SCRIPT ?=
|
ACTION_SCRIPT ?=
|
||||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||||
|
|
||||||
.PHONY: help check validate test load test-persistence test-failover
|
.PHONY: help check validate test load test-persistence test-failover test-student up down restart
|
||||||
|
|
||||||
help:
|
help:
|
||||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||||
|
@echo "test-student: run student tests (pytest); test-persistence: test stack restart with data preservation"
|
||||||
|
@echo "test-failover: test replica failure handling; up: build and start stack; down: stop stack"
|
||||||
|
|
||||||
check:
|
check:
|
||||||
$(PYTHON) scripts/check_contract.py
|
$(PYTHON) scripts/check_contract.py
|
||||||
@@ -28,3 +30,16 @@ test-persistence:
|
|||||||
test-failover:
|
test-failover:
|
||||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||||
|
|
||||||
|
test-student:
|
||||||
|
BASE_URL="$(BASE_URL)" .venv/bin/pytest tests_student/ -v
|
||||||
|
|
||||||
|
up:
|
||||||
|
docker compose up -d --build
|
||||||
|
|
||||||
|
down:
|
||||||
|
docker compose down
|
||||||
|
|
||||||
|
restart:
|
||||||
|
docker compose restart
|
||||||
|
|
||||||
|
|||||||
@@ -1,37 +1,175 @@
|
|||||||
# Отчёт — лабораторная 4
|
# Отчёт — лабораторная 4
|
||||||
|
|
||||||
- Автор, группа: TODO
|
- Автор, группа: Герейханов Ильяс, БВТ2301
|
||||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
- Целевая оценка: 4
|
||||||
- Commit/PR: TODO после публикации
|
- Commit/PR: ветка `solution/bvt2301-Gereykhanov`. PR: `ЛР 4 — БВТ2301 — Герейханов — на 4`
|
||||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
- Использованные источники, библиотеки и помощь ИИ: Python 3.12, FastAPI, Uvicorn, PostgreSQL 16, Redis 7, Nginx 1.27, MinIO, boto3, Pillow, asyncpg, argon2-cffi, Pydantic, Pytest, Docker Compose; документация AWS S3 API, RFC 7231, OpenAPI 3.1.0. ИИ использовался для подготовки скриптов, отладки конфигурации nginx и расширения тестов pytest.
|
||||||
|
|
||||||
## Запуск из чистого clone
|
## Запуск из чистого clone
|
||||||
|
|
||||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
1. **Версии runtime и образов:**
|
||||||
|
- Локальный runtime: Linux, Docker 29.9.1, Docker Compose v5.6.0
|
||||||
|
- Load Balancer: `nginx:1.27-alpine`
|
||||||
|
- API контейнеры: `python:3.12-slim`
|
||||||
|
- PostgreSQL: `postgres:16-alpine`
|
||||||
|
- Redis: `redis:7-alpine`
|
||||||
|
- S3-совместимое хранилище: `cleanstart/minio:0.20260330.001845`
|
||||||
|
|
||||||
|
2. **Подготовка окружения и запуск стека:**
|
||||||
|
```sh
|
||||||
|
git checkout solution/bvt2301-Gereykhanov
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# Сборка и запуск всех сервисов
|
||||||
|
make up
|
||||||
|
```
|
||||||
|
|
||||||
|
3. **Ожидание готовности и запуск проверок:**
|
||||||
|
```sh
|
||||||
|
# Проверка структуры и синтаксиса
|
||||||
|
make check
|
||||||
|
|
||||||
|
# Валидация схемы OpenAPI
|
||||||
|
make validate PYTHON=.venv/bin/python
|
||||||
|
|
||||||
|
# Проверка smoke теста
|
||||||
|
make test
|
||||||
|
|
||||||
|
# Проверка сценария сохранности данных и сессий при рестарте
|
||||||
|
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||||
|
|
||||||
|
# Проверка отказоустойчивости при отключении одной API-реплики
|
||||||
|
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||||
|
|
||||||
|
# Восстановление остановленной реплики после теста
|
||||||
|
docker compose up -d
|
||||||
|
|
||||||
|
# Запуск собственных тестов
|
||||||
|
make test-student
|
||||||
|
|
||||||
|
# Запуск нагрузочного теста
|
||||||
|
python3 tests/load.py --duration 30 --concurrency 4 --output evidence/load.json
|
||||||
|
```
|
||||||
|
|
||||||
|
4. **Остановка и очистка:**
|
||||||
|
```sh
|
||||||
|
# Остановка с сохранением данных volumes
|
||||||
|
docker compose down
|
||||||
|
|
||||||
|
# Остановка с удалением volumes
|
||||||
|
docker compose down -v
|
||||||
|
```
|
||||||
|
|
||||||
## Архитектура и решения
|
## Архитектура и решения
|
||||||
|
|
||||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
- **Топология процессов и распределение ролей:**
|
||||||
|
- **Клиент** отправляет все HTTP-запросы в единую точку входа **Nginx Load Balancer** на порт `8080`.
|
||||||
|
- **Nginx** осуществляет Round-Robin балансировку между двумя независимыми репликами API (`api-1` и `api-2`).
|
||||||
|
- Реплики API работают в stateless-режиме, не имеют открытых портов на хост и доступны только через балансировщик.
|
||||||
|
- Каждая реплика API обращается к общему **PostgreSQL**, **Redis** и **MinIO S3**.
|
||||||
|
- Отдельный одноразовый контейнер **migration** применяет `init.sql` перед стартом API-реплик.
|
||||||
|
|
||||||
|
- **Хранилища данных и источник истины:**
|
||||||
|
- **PostgreSQL:** источник истины для реляционных данных пользователей, чатов, сообщений и метаданных вложений в таблице `attachments` (`id`, `chat_id`, `owner_id`, `filename`, `content_type`, `size_bytes`, `sha256`, `s3_key`, `status`, `error_code`, `created_at`, `updated_at`).
|
||||||
|
- **MinIO S3:** хранилище бинарного контента в приватном бакете `messenger-attachments`. Ключи объектов формируются по схеме `chats/{chat_id}/{attachment_id}/{filename}`.
|
||||||
|
- **Redis:** сессионный кэш Bearer-токенов с TTL 3600 с и сохранением на volume `redis_data` через `--appendonly yes`.
|
||||||
|
|
||||||
|
- **Что перенесено из лабы 3:**
|
||||||
|
- Балансировщик Nginx с Round-robin 50/50 и `proxy_next_upstream` для быстрого переключения при сбое.
|
||||||
|
- Сквозные заголовки `X-Request-Id` и `X-Instance-Id` в ответах API.
|
||||||
|
- Единое хранилище сессий в Redis.
|
||||||
|
- Идемпотентность отправки сообщений по заголовку `Idempotency-Key` с сохранением `request_hash` в БД.
|
||||||
|
- Keyset-пагинация сообщений по индексу.
|
||||||
|
|
||||||
|
- **Интеграция S3 и изоляция сети:**
|
||||||
|
- MinIO включен во внутреннюю Docker-сеть с постоянным volume `minio_data`.
|
||||||
|
- Порты MinIO не проброшены на хост наружу, консоль закрыта от общей сети.
|
||||||
|
- При старте API выполняется автоматический идемпотентный bootstrap: проверка и создание бакета `messenger-attachments` через `boto3`.
|
||||||
|
|
||||||
|
- **Загрузка, валидация и защита от атак:**
|
||||||
|
- Эндпоинт `POST /api/v1/chats/{chat_id}/attachments?filename=...` принимает сырое бинарное тело (PNG/JPEG) до 10 MiB.
|
||||||
|
- **Проверка MIME по содержимому:** проверяются магические сигнатуры байтов. Невалидные форматы отклоняются с кодом `415`.
|
||||||
|
- **Защита от decompression bomb:** через библиотеку `Pillow` считываются размеры изображения из заголовков IHDR/SOF без декомпрессии растра в память. Превышение лимита 40 млн пикселей возвращает `413 payload_too_large`.
|
||||||
|
- **Валидация имени файла (`filename`):** длина 1–255 символов; отклоняются path-traversal последовательности, NUL-байт (`\0`) и управляющие символы с кодом `400 invalid_request`.
|
||||||
|
|
||||||
|
- **Компенсация сиротских объектов и прямой download:**
|
||||||
|
- Файл сначала сохраняется в S3 через `boto3.put_object`, после чего выполняется запись метаданных в базу. При ошибке в БД блок `except` вызывает `s3_client.delete_object`, удаляя объект из S3.
|
||||||
|
- Скачивание через `GET /api/v1/attachments/{id}/content` возвращает байты напрямую из S3 без 302/307 редиректов. Запрос от постороннего возвращает строго `404 not_found`.
|
||||||
|
|
||||||
|
- **Связывание с сообщениями и настройки Nginx:**
|
||||||
|
- В таблицу `messages` добавлено поле `attachment_ids UUID[]`.
|
||||||
|
- Разрешена отправка сообщения с пустым текстом `text=""`, если прикреплены файлы.
|
||||||
|
- В `infra/nginx.conf` задана директива `client_max_body_size 15M;` для пропуска 10 MiB загрузок.
|
||||||
|
- Все параметры согласованы с `.env.example`: `S3_ENDPOINT`, `S3_BUCKET`, `MAX_UPLOAD_BYTES`, `MAX_IMAGE_PIXELS`.
|
||||||
|
|
||||||
## Доказательства критериев
|
## Доказательства критериев
|
||||||
|
|
||||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| Публичный smoke | make test | TODO | TODO |
|
| Проверка синтаксиса и контракта | `make check` | `PASS: offline skeleton structure, local refs and Python syntax` | [`evidence/check_contract.log`](evidence/check_contract.log) |
|
||||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
| Полная OpenAPI 3.1 валидация | `make validate PYTHON=.venv/bin/python` | `PASS: full OpenAPI 3.1 and JSON Schema validation` | [`evidence/validate_openapi.log`](evidence/validate_openapi.log) |
|
||||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
| Публичный smoke тест | `make test` | `PASS: lab4 public API scenario` | [`evidence/smoke_test.log`](evidence/smoke_test.log) |
|
||||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
| Чередование реплик | `for i in {1..10}; do curl -sI http://localhost:8080/health/live \| grep -i x-instance-id; done` | 50 на 50 ответы `api-1` и `api-2` | [`evidence/round_robin.log`](evidence/round_robin.log) |
|
||||||
|
| Сохранение данных и сессий при рестарте | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | Стек перезапущен, токен, сообщения и вложения в S3 сохранены | [`evidence/persistence_test.log`](evidence/persistence_test.log) |
|
||||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
| Отказ API-реплики под нагрузкой | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh` | Восстановление записи за **0.778 с**, 105 успешных запросов, 0 сбоев | [`evidence/failover_test.log`](evidence/failover_test.log) |
|
||||||
|
| Защита от decompression bomb (>40M px) | `make test-student` | Запрос с 50M px изображением отклонен с `413 payload_too_large` | [`evidence/student_tests.log`](evidence/student_tests.log) |
|
||||||
|
| Проверка MIME по магическим байтам | `make test-student` | Поддельные и поврежденные изображения отклоняются с `415` | [`evidence/student_tests.log`](evidence/student_tests.log) |
|
||||||
|
| Компенсация сирот S3 при отказе БД | `make test-student` | При сбое PostgreSQL объект удаляется из S3, в хранилище нет сирот | [`evidence/student_tests.log`](evidence/student_tests.log) |
|
||||||
|
| Закрытый бакет и лимит RAM (10 загрузок) | `make test-student` | Порт 9000 закрыт снаружи, 10 параллельных загрузок успешны, RAM стабильна | [`evidence/student_tests.log`](evidence/student_tests.log) |
|
||||||
|
| Собственные тесты | `make test-student` | **34 теста проходят**: валидация вложений, компенсация S3, курсор, идемпотентность | [`evidence/student_tests.log`](evidence/student_tests.log) |
|
||||||
|
| Статусы и изоляция контейнеров | `docker compose ps` | Все 6 сервисов healthy, API и MinIO не открывают порты на хост | [`evidence/docker_services.log`](evidence/docker_services.log) |
|
||||||
|
| Нагрузочный тест | `make load` | **309.4 RPS**, p50 = 7.9 ms, p95 = 40.8 ms, p99 = 63.0 ms, 0 ошибок | [`evidence/load.json`](evidence/load.json) |
|
||||||
|
|
||||||
## Эксперименты и отказы
|
## Эксперименты и отказы
|
||||||
|
|
||||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
- **Профиль нагрузки:** скрипт `tests/load.py`, закрытый цикл параллельных POST-запросов отправки сообщений с уникальными `Idempotency-Key`.
|
||||||
|
- **Параметры:** длительность 30 секунд, 4 параллельных потока.
|
||||||
|
- **Результаты нагрузки (`evidence/load.json`):**
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"lab": 4,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 30.0,
|
||||||
|
"elapsed_seconds": 30.015,
|
||||||
|
"concurrency": 4,
|
||||||
|
"successful_requests": 9286,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 309.375,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 7.928,
|
||||||
|
"p95": 40.768,
|
||||||
|
"p99": 62.963
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||||
| --- | --- | --- | --- |
|
| --- | --- | --- | --- |
|
||||||
| TODO | TODO | TODO | TODO |
|
| Остановка одной реплики API (`scripts/stop-one.sh`) | Nginx переключает трафик на живую реплику, продолжение записи $\le 10$ с, нет дубликатов | 105 успешных записей во время остановки, 0 ошибок, переключение за 0.778 с | 0.778 сек, потерь данных нет |
|
||||||
|
| Рестарт всего стека (`scripts/restart.sh`) при активной сессии| Сохранение подтверждённых данных в Postgres, токенов сессий в Redis и файлов в MinIO | Контейнеры перезапущены, токен валиден, история сообщений и файлы сохранены | Восстановление около 5 сек, потеря подтверждённых данных: 0 |
|
||||||
|
| Загрузка декомпрессионной бомбы (изображение 10000x5000) | Отклонение запроса с кодом 413 `payload_too_large` без исчерпания RAM | Запрос отклонен мгновенно по метаданным IHDR без распаковки растра | Мгновенно (< 2 мс) |
|
||||||
|
| Загрузка файла размером более 10 MiB | Отклонение запроса с кодом 413 `payload_too_large` | Запрос отклонен до сохранения в S3 | Мгновенно |
|
||||||
|
| Попытка загрузки файла с поддельным расширением | Отклонение с кодом 415 `unsupported_media_type` | Проверка сигнатуры байтов выявляет подмену и возвращает 415 | Мгновенно |
|
||||||
|
| Попытка доступа к метаданным или контенту посторонним пользователем | Защита приватности: возврат строго 404 Not Found без утечки факта существования файла | Посторонний получает 404 на `GET /attachments/{id}` и `/content` | Мгновенно |
|
||||||
|
| Попытка передачи опасного имени файла | Отклонение запроса с кодом 400 `invalid_request` | Валидатор отклоняет некорректное имя до обращения к хранилищу | Мгновенно |
|
||||||
|
| Сбой сохранения метаданных в БД после успешного `PUT` в S3 | Автоматическое удаление объекта из S3 | В S3 не остается мусорных файлов | Мгновенно |
|
||||||
|
| Конкурентная загрузка ≥10 файлов с контролем RAM | Все файлы сохраняются в S3, память API стабильна | 10 параллельных загрузок вернули 201 ready, утечек памяти нет | Мгновенно (< 1 с) |
|
||||||
|
| Попытка прямого обращения к MinIO на порт 9000 с хоста | Соединение отклоняется (Connection Refused), консоль закрыта | Порты MinIO не проброшены наружу, прямой доступ невозможен | Мгновенно |
|
||||||
|
|
||||||
## Ограничения и следующие шаги
|
## Ограничения
|
||||||
|
|
||||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
- **Какие отказы выдерживаются и какие нет:**
|
||||||
|
1. *Выдерживаются:*
|
||||||
|
- Внезапная остановка любой одной API-реплики под нагрузкой: балансировщик Nginx переключает входящие запросы на оставшуюся реплику.
|
||||||
|
- Полный рестарт стека: все подтверждённые данные, активные токены и файлы в S3 сохраняются благодаря volumes.
|
||||||
|
- Конкурентная отправка сообщений с одинаковым `Idempotency-Key` в разные реплики.
|
||||||
|
- Попытки атак декомпрессионными бомбами и внедрения вредоносных имен файлов.
|
||||||
|
- Сбои транзакций после записи в S3: механизм компенсации удаляет сиротские объекты.
|
||||||
|
2. *Не выдерживаются:*
|
||||||
|
- Одновременное падение обеих API-реплик (HTTP 502 Bad Gateway).
|
||||||
|
- Падение единственного экземпляра PostgreSQL, Redis или MinIO.
|
||||||
|
- Удаление через `docker compose down -v`.
|
||||||
|
- **Учебные упрощения:**
|
||||||
|
1. Nginx, PostgreSQL, Redis и MinIO запущены в единственном экземпляре без распределенной репликации.
|
||||||
|
2. Скачивание контента реализовано через проксирование байтов API-репликой.
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
"""Messenger application package."""
|
||||||
+27
@@ -0,0 +1,27 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import secrets
|
||||||
|
from argon2 import PasswordHasher
|
||||||
|
from argon2.exceptions import VerifyMismatchError
|
||||||
|
|
||||||
|
_hasher = PasswordHasher()
|
||||||
|
|
||||||
|
|
||||||
|
def hash_password(password: str) -> str:
|
||||||
|
"""Хэширует пароль с использованием Argon2id."""
|
||||||
|
return _hasher.hash(password)
|
||||||
|
|
||||||
|
|
||||||
|
def verify_password(hash_str: str, password: str) -> bool:
|
||||||
|
"""Проверяет пароль на соответствие хэшу Argon2id."""
|
||||||
|
try:
|
||||||
|
return _hasher.verify(hash_str, password)
|
||||||
|
except VerifyMismatchError:
|
||||||
|
return False
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
def generate_session_token() -> str:
|
||||||
|
"""Генерирует криптографически стойкий непрозрачный токен сессии (256 бит энтропии)."""
|
||||||
|
return secrets.token_urlsafe(32)
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import socket
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
|
||||||
|
def _get_val(env_name: str, file_env_name: str | None = None, default: str = "") -> str:
|
||||||
|
if file_env_name and file_env_name in os.environ:
|
||||||
|
file_path = Path(os.environ[file_env_name])
|
||||||
|
if file_path.is_file():
|
||||||
|
return file_path.read_text(encoding="utf-8").strip()
|
||||||
|
return os.getenv(env_name, default)
|
||||||
|
|
||||||
|
|
||||||
|
APP_HOST: str = os.getenv("APP_HOST", "0.0.0.0")
|
||||||
|
APP_PORT: int = int(os.getenv("APP_PORT", "8080"))
|
||||||
|
APP_ENV: str = os.getenv("APP_ENV", "lab")
|
||||||
|
LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
|
||||||
|
|
||||||
|
SESSION_TTL_SECONDS: int = int(os.getenv("SESSION_TTL_SECONDS", "3600"))
|
||||||
|
SESSION_STORE: str = os.getenv("SESSION_STORE", "redis").lower()
|
||||||
|
INSTANCE_ID: str = os.getenv("INSTANCE_ID") or socket.gethostname() or "api-1"
|
||||||
|
IDEMPOTENCY_TTL_SECONDS: int = int(os.getenv("IDEMPOTENCY_TTL_SECONDS", "86400"))
|
||||||
|
|
||||||
|
DATABASE_URL: str = _get_val(
|
||||||
|
"DATABASE_URL",
|
||||||
|
"DATABASE_URL_FILE",
|
||||||
|
"postgresql://postgres:postgres@postgres:5432/messenger",
|
||||||
|
)
|
||||||
|
|
||||||
|
CACHE_URL: str = _get_val(
|
||||||
|
"CACHE_URL",
|
||||||
|
"CACHE_URL_FILE",
|
||||||
|
"redis://redis:6379/0",
|
||||||
|
)
|
||||||
|
|
||||||
|
S3_ENDPOINT: str = os.getenv("S3_ENDPOINT", "http://minio:9000")
|
||||||
|
S3_REGION: str = os.getenv("S3_REGION", "us-east-1")
|
||||||
|
S3_BUCKET: str = os.getenv("S3_BUCKET", "messenger-attachments")
|
||||||
|
S3_ACCESS_KEY_ID: str = _get_val("S3_ACCESS_KEY_ID", "S3_ACCESS_KEY_ID_FILE", "minioadmin")
|
||||||
|
S3_SECRET_ACCESS_KEY: str = _get_val("S3_SECRET_ACCESS_KEY", "S3_SECRET_ACCESS_KEY_FILE", "minioadmin")
|
||||||
|
MAX_UPLOAD_BYTES: int = int(os.getenv("MAX_UPLOAD_BYTES", "10485760"))
|
||||||
|
MAX_IMAGE_PIXELS: int = int(os.getenv("MAX_IMAGE_PIXELS", "40000000"))
|
||||||
@@ -0,0 +1,30 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from fastapi import Depends, Header, Request
|
||||||
|
|
||||||
|
from app.errors import UnauthorizedError
|
||||||
|
from app.schemas import UserResponse
|
||||||
|
from app.storage import storage
|
||||||
|
|
||||||
|
|
||||||
|
async def get_current_token(
|
||||||
|
authorization: str | None = Header(default=None, alias="Authorization"),
|
||||||
|
) -> str:
|
||||||
|
if not authorization or not authorization.strip():
|
||||||
|
raise UnauthorizedError("Отсутствует заголовок Authorization")
|
||||||
|
|
||||||
|
parts = authorization.strip().split(maxsplit=1)
|
||||||
|
if len(parts) != 2 or parts[0].lower() != "bearer":
|
||||||
|
raise UnauthorizedError("Неверная схема авторизации (ожидается Bearer)")
|
||||||
|
|
||||||
|
token = parts[1].strip()
|
||||||
|
if not token:
|
||||||
|
raise UnauthorizedError("Пустой Bearer токен")
|
||||||
|
return token
|
||||||
|
|
||||||
|
|
||||||
|
async def get_current_user(token: str = Depends(get_current_token)) -> UserResponse:
|
||||||
|
user = await storage.get_session_user(token)
|
||||||
|
if not user:
|
||||||
|
raise UnauthorizedError("Недействительная или истекшая сессия")
|
||||||
|
return user
|
||||||
+260
@@ -0,0 +1,260 @@
|
|||||||
|
import asyncio
|
||||||
|
import logging
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
import asyncpg
|
||||||
|
import botocore.exceptions
|
||||||
|
from fastapi import FastAPI, Request, status
|
||||||
|
from fastapi.exceptions import RequestValidationError
|
||||||
|
from fastapi.responses import JSONResponse
|
||||||
|
import redis.exceptions
|
||||||
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
|
|
||||||
|
from app.config import INSTANCE_ID
|
||||||
|
from app.schemas import ErrorCode, ErrorDetail, ErrorResponse
|
||||||
|
|
||||||
|
|
||||||
|
class StorageError(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UnavailableError(StorageError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidRequestError(StorageError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class NotFoundError(StorageError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ConflictError(StorageError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class PayloadTooLargeError(StorageError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UnsupportedMediaTypeError(StorageError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class UnauthorizedError(StorageError):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
logger = logging.getLogger("app.errors")
|
||||||
|
|
||||||
|
|
||||||
|
def get_request_id(request: Request) -> str:
|
||||||
|
req_id = getattr(request.state, "request_id", None)
|
||||||
|
if not req_id:
|
||||||
|
req_id = request.headers.get("X-Request-Id") or str(uuid.uuid4())
|
||||||
|
return req_id
|
||||||
|
|
||||||
|
|
||||||
|
def make_error_response(
|
||||||
|
status_code: int,
|
||||||
|
code: ErrorCode,
|
||||||
|
message: str,
|
||||||
|
request_id: str,
|
||||||
|
headers: dict[str, str] | None = None,
|
||||||
|
) -> JSONResponse:
|
||||||
|
payload = ErrorResponse(
|
||||||
|
error=ErrorDetail(code=code, message=message, request_id=request_id)
|
||||||
|
).model_dump()
|
||||||
|
resp_headers = {
|
||||||
|
"X-Request-Id": request_id,
|
||||||
|
"X-Instance-Id": INSTANCE_ID,
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
}
|
||||||
|
if headers:
|
||||||
|
resp_headers.update(headers)
|
||||||
|
return JSONResponse(
|
||||||
|
status_code=status_code,
|
||||||
|
content=payload,
|
||||||
|
headers=resp_headers,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def register_error_handlers(app: FastAPI) -> None:
|
||||||
|
@app.exception_handler(InvalidRequestError)
|
||||||
|
async def invalid_request_handler(
|
||||||
|
request: Request, exc: InvalidRequestError
|
||||||
|
) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_400_BAD_REQUEST, ErrorCode.INVALID_REQUEST, str(exc), req_id
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(UnauthorizedError)
|
||||||
|
async def unauthorized_handler(
|
||||||
|
request: Request, exc: UnauthorizedError
|
||||||
|
) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
scheme = (
|
||||||
|
'Basic realm="messenger", charset="UTF-8"'
|
||||||
|
if request.url.path == "/api/v1/auth/sessions"
|
||||||
|
else 'Bearer realm="messenger"'
|
||||||
|
)
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_401_UNAUTHORIZED,
|
||||||
|
ErrorCode.UNAUTHORIZED,
|
||||||
|
str(exc),
|
||||||
|
req_id,
|
||||||
|
headers={"WWW-Authenticate": scheme},
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(NotFoundError)
|
||||||
|
async def not_found_handler(request: Request, exc: NotFoundError) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_404_NOT_FOUND, ErrorCode.NOT_FOUND, str(exc), req_id
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(ConflictError)
|
||||||
|
async def conflict_handler(request: Request, exc: ConflictError) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_409_CONFLICT, ErrorCode.CONFLICT, str(exc), req_id
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(PayloadTooLargeError)
|
||||||
|
async def payload_too_large_handler(
|
||||||
|
request: Request, exc: PayloadTooLargeError
|
||||||
|
) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_413_REQUEST_ENTITY_TOO_LARGE,
|
||||||
|
ErrorCode.PAYLOAD_TOO_LARGE,
|
||||||
|
str(exc),
|
||||||
|
req_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(UnsupportedMediaTypeError)
|
||||||
|
async def unsupported_media_type_handler(
|
||||||
|
request: Request, exc: UnsupportedMediaTypeError
|
||||||
|
) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||||
|
ErrorCode.UNSUPPORTED_MEDIA_TYPE,
|
||||||
|
str(exc),
|
||||||
|
req_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(RequestValidationError)
|
||||||
|
async def validation_error_handler(
|
||||||
|
request: Request, exc: RequestValidationError
|
||||||
|
) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
errors = exc.errors()
|
||||||
|
messages = []
|
||||||
|
for err in errors:
|
||||||
|
loc = ".".join(str(x) for x in err.get("loc", []) if x != "body")
|
||||||
|
msg = err.get("msg", "invalid")
|
||||||
|
messages.append(f"{loc}: {msg}" if loc else msg)
|
||||||
|
detail_msg = "; ".join(messages) or "Некорректное тело запроса или параметры"
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_400_BAD_REQUEST, ErrorCode.INVALID_REQUEST, detail_msg, req_id
|
||||||
|
)
|
||||||
|
|
||||||
|
@app.exception_handler(StarletteHTTPException)
|
||||||
|
async def http_exception_handler(
|
||||||
|
request: Request, exc: StarletteHTTPException
|
||||||
|
) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
code = ErrorCode.INTERNAL_ERROR
|
||||||
|
extra_headers: dict[str, str] = {}
|
||||||
|
if exc.status_code == 400:
|
||||||
|
code = ErrorCode.INVALID_REQUEST
|
||||||
|
elif exc.status_code == 401:
|
||||||
|
code = ErrorCode.UNAUTHORIZED
|
||||||
|
scheme = (
|
||||||
|
'Basic realm="messenger", charset="UTF-8"'
|
||||||
|
if request.url.path == "/api/v1/auth/sessions"
|
||||||
|
else 'Bearer realm="messenger"'
|
||||||
|
)
|
||||||
|
extra_headers["WWW-Authenticate"] = scheme
|
||||||
|
elif exc.status_code == 404:
|
||||||
|
code = ErrorCode.NOT_FOUND
|
||||||
|
elif exc.status_code == 409:
|
||||||
|
code = ErrorCode.CONFLICT
|
||||||
|
elif exc.status_code == 413:
|
||||||
|
code = ErrorCode.PAYLOAD_TOO_LARGE
|
||||||
|
elif exc.status_code == 415:
|
||||||
|
code = ErrorCode.UNSUPPORTED_MEDIA_TYPE
|
||||||
|
elif exc.status_code == 429:
|
||||||
|
code = ErrorCode.RATE_LIMITED
|
||||||
|
elif exc.status_code == 503:
|
||||||
|
code = ErrorCode.UNAVAILABLE
|
||||||
|
|
||||||
|
msg = exc.detail if isinstance(exc.detail, str) else "HTTP ошибка"
|
||||||
|
return make_error_response(
|
||||||
|
exc.status_code, code, msg, req_id, headers=extra_headers
|
||||||
|
)
|
||||||
|
|
||||||
|
async def dependency_unavailable_handler(
|
||||||
|
request: Request, exc: Exception
|
||||||
|
) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
logger.warning(
|
||||||
|
"Upstream storage dependency unavailable for request %s: %s",
|
||||||
|
req_id,
|
||||||
|
exc,
|
||||||
|
)
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||||
|
ErrorCode.UNAVAILABLE,
|
||||||
|
"Зависимость недоступна",
|
||||||
|
req_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
for exc_cls in (
|
||||||
|
UnavailableError,
|
||||||
|
asyncpg.PostgresError,
|
||||||
|
asyncpg.InterfaceError,
|
||||||
|
redis.exceptions.RedisError,
|
||||||
|
botocore.exceptions.BotoCoreError,
|
||||||
|
botocore.exceptions.ClientError,
|
||||||
|
TimeoutError,
|
||||||
|
asyncio.TimeoutError,
|
||||||
|
ConnectionError,
|
||||||
|
OSError,
|
||||||
|
):
|
||||||
|
app.add_exception_handler(exc_cls, dependency_unavailable_handler)
|
||||||
|
|
||||||
|
@app.exception_handler(Exception)
|
||||||
|
async def unhandled_exception_handler(
|
||||||
|
request: Request, exc: Exception
|
||||||
|
) -> JSONResponse:
|
||||||
|
req_id = get_request_id(request)
|
||||||
|
if isinstance(
|
||||||
|
exc,
|
||||||
|
(
|
||||||
|
UnavailableError,
|
||||||
|
asyncpg.PostgresError,
|
||||||
|
asyncpg.InterfaceError,
|
||||||
|
redis.exceptions.RedisError,
|
||||||
|
botocore.exceptions.BotoCoreError,
|
||||||
|
botocore.exceptions.ClientError,
|
||||||
|
TimeoutError,
|
||||||
|
asyncio.TimeoutError,
|
||||||
|
ConnectionError,
|
||||||
|
OSError,
|
||||||
|
),
|
||||||
|
):
|
||||||
|
return await dependency_unavailable_handler(request, exc)
|
||||||
|
|
||||||
|
logger.error(
|
||||||
|
"Unhandled exception for request %s: %s", req_id, exc, exc_info=True
|
||||||
|
)
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
ErrorCode.INTERNAL_ERROR,
|
||||||
|
"Внутренняя ошибка сервера",
|
||||||
|
req_id,
|
||||||
|
)
|
||||||
+61
@@ -0,0 +1,61 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
|
||||||
|
import uvicorn
|
||||||
|
from fastapi import FastAPI
|
||||||
|
|
||||||
|
from app.config import APP_HOST, APP_PORT, LOG_LEVEL
|
||||||
|
from app.errors import register_error_handlers
|
||||||
|
from app.middleware import RequestIdAndLoggingMiddleware
|
||||||
|
from app.routes.attachments import router as attachments_router
|
||||||
|
from app.routes.auth import router as auth_router
|
||||||
|
from app.routes.chats import router as chats_router
|
||||||
|
from app.routes.health import router as health_router
|
||||||
|
from app.routes.messages import router as messages_router
|
||||||
|
from app.routes.users import router as users_router
|
||||||
|
from app.storage import storage
|
||||||
|
|
||||||
|
logger = logging.getLogger("app.lifecycle")
|
||||||
|
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def lifespan(app: FastAPI):
|
||||||
|
logger.info(
|
||||||
|
"Starting messenger server on %s:%d (env: %s)", APP_HOST, APP_PORT, LOG_LEVEL
|
||||||
|
)
|
||||||
|
await storage.connect()
|
||||||
|
yield
|
||||||
|
logger.info("Graceful shutdown: closing connections...")
|
||||||
|
await storage.disconnect()
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI(
|
||||||
|
title="highload-messenger",
|
||||||
|
version="1.4.0",
|
||||||
|
docs_url="/docs",
|
||||||
|
redoc_url=None,
|
||||||
|
lifespan=lifespan,
|
||||||
|
)
|
||||||
|
|
||||||
|
app.add_middleware(RequestIdAndLoggingMiddleware)
|
||||||
|
|
||||||
|
register_error_handlers(app)
|
||||||
|
|
||||||
|
app.include_router(health_router)
|
||||||
|
app.include_router(auth_router)
|
||||||
|
app.include_router(users_router)
|
||||||
|
app.include_router(chats_router)
|
||||||
|
app.include_router(messages_router)
|
||||||
|
app.include_router(attachments_router)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
uvicorn.run(
|
||||||
|
"app.main:app",
|
||||||
|
host=APP_HOST,
|
||||||
|
port=APP_PORT,
|
||||||
|
log_level=LOG_LEVEL.lower(),
|
||||||
|
timeout_graceful_shutdown=10,
|
||||||
|
)
|
||||||
@@ -0,0 +1,93 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import logging
|
||||||
|
import sys
|
||||||
|
import time
|
||||||
|
import uuid
|
||||||
|
|
||||||
|
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
|
||||||
|
from starlette.requests import Request
|
||||||
|
from starlette.responses import Response
|
||||||
|
|
||||||
|
from app.config import INSTANCE_ID
|
||||||
|
|
||||||
|
logging.basicConfig(
|
||||||
|
level=logging.INFO,
|
||||||
|
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
|
||||||
|
stream=sys.stdout,
|
||||||
|
)
|
||||||
|
logger = logging.getLogger("app.access")
|
||||||
|
|
||||||
|
|
||||||
|
class RequestIdAndLoggingMiddleware(BaseHTTPMiddleware):
|
||||||
|
async def dispatch(
|
||||||
|
self, request: Request, call_next: RequestResponseEndpoint
|
||||||
|
) -> Response:
|
||||||
|
# Получаем X-Request-Id или генерируем новый
|
||||||
|
request_id = request.headers.get("X-Request-Id")
|
||||||
|
if not request_id or not request_id.strip():
|
||||||
|
request_id = str(uuid.uuid4())
|
||||||
|
|
||||||
|
request.state.request_id = request_id
|
||||||
|
start_time = time.monotonic()
|
||||||
|
|
||||||
|
if request.method in ("POST", "PUT", "PATCH") and request.url.path != "/api/v1/auth/sessions":
|
||||||
|
is_attachment_upload = (
|
||||||
|
request.method == "POST"
|
||||||
|
and "/attachments" in request.url.path
|
||||||
|
and request.url.path.startswith("/api/v1/chats/")
|
||||||
|
)
|
||||||
|
ct = request.headers.get("content-type")
|
||||||
|
ct_clean = ct.split(";")[0].strip().lower() if ct else ""
|
||||||
|
|
||||||
|
if is_attachment_upload:
|
||||||
|
if not ct or ct_clean not in ("image/png", "image/jpeg"):
|
||||||
|
from fastapi import status
|
||||||
|
from app.errors import make_error_response
|
||||||
|
from app.schemas import ErrorCode
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||||
|
ErrorCode.UNSUPPORTED_MEDIA_TYPE,
|
||||||
|
f"Неподдерживаемый тип содержимого '{ct}'. Ожидается image/png или image/jpeg",
|
||||||
|
request_id,
|
||||||
|
)
|
||||||
|
elif ct and ct_clean != "application/json":
|
||||||
|
from fastapi import status
|
||||||
|
from app.errors import make_error_response
|
||||||
|
from app.schemas import ErrorCode
|
||||||
|
return make_error_response(
|
||||||
|
status.HTTP_415_UNSUPPORTED_MEDIA_TYPE,
|
||||||
|
ErrorCode.UNSUPPORTED_MEDIA_TYPE,
|
||||||
|
f"Неподдерживаемый тип содержимого '{ct}'. Ожидается application/json",
|
||||||
|
request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await call_next(request)
|
||||||
|
except Exception:
|
||||||
|
# Исключение перехватится exception_handler
|
||||||
|
duration_ms = (time.monotonic() - start_time) * 1000
|
||||||
|
logger.error(
|
||||||
|
"%s %s -> 500 error | duration=%.2fms | request_id=%s",
|
||||||
|
request.method,
|
||||||
|
request.url.path,
|
||||||
|
duration_ms,
|
||||||
|
request_id,
|
||||||
|
)
|
||||||
|
raise
|
||||||
|
|
||||||
|
duration_ms = (time.monotonic() - start_time) * 1000
|
||||||
|
response.headers["X-Request-Id"] = request_id
|
||||||
|
response.headers["X-Instance-Id"] = INSTANCE_ID
|
||||||
|
|
||||||
|
# лог без логирования тела запросов и текстов сообщений
|
||||||
|
logger.info(
|
||||||
|
"%s %s -> %d | duration=%.2fms | request_id=%s",
|
||||||
|
request.method,
|
||||||
|
request.url.path,
|
||||||
|
response.status_code,
|
||||||
|
duration_ms,
|
||||||
|
request_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
return response
|
||||||
@@ -0,0 +1,85 @@
|
|||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, Query, Request, Response, status
|
||||||
|
|
||||||
|
from app.dependencies import get_current_user
|
||||||
|
from app.errors import InvalidRequestError
|
||||||
|
from app.schemas import AttachmentResponse, UserResponse
|
||||||
|
from app.storage import storage
|
||||||
|
|
||||||
|
router = APIRouter(tags=["attachments"])
|
||||||
|
|
||||||
|
|
||||||
|
def validate_uuid(value: str, field_name: str = "id") -> str:
|
||||||
|
try:
|
||||||
|
UUID(value)
|
||||||
|
return value
|
||||||
|
except ValueError as exc:
|
||||||
|
raise InvalidRequestError(f"Поле '{field_name}' не является валидным UUID") from exc
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/api/v1/chats/{chat_id}/attachments",
|
||||||
|
response_model=AttachmentResponse,
|
||||||
|
status_code=status.HTTP_201_CREATED,
|
||||||
|
)
|
||||||
|
async def upload_attachment(
|
||||||
|
chat_id: str,
|
||||||
|
request: Request,
|
||||||
|
filename: str = Query(..., min_length=1, max_length=255),
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> AttachmentResponse:
|
||||||
|
"""Загрузка бинарного вложения в чат."""
|
||||||
|
valid_chat_id = validate_uuid(chat_id, "chat_id")
|
||||||
|
content_type = request.headers.get("content-type") or ""
|
||||||
|
data = await request.body()
|
||||||
|
|
||||||
|
return await storage.upload_attachment(
|
||||||
|
caller_id=current_user.id,
|
||||||
|
chat_id=valid_chat_id,
|
||||||
|
filename=filename,
|
||||||
|
content_type=content_type,
|
||||||
|
data=data,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/api/v1/attachments/{attachment_id}",
|
||||||
|
response_model=AttachmentResponse,
|
||||||
|
status_code=status.HTTP_200_OK,
|
||||||
|
)
|
||||||
|
async def get_attachment_metadata(
|
||||||
|
attachment_id: str,
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> AttachmentResponse:
|
||||||
|
"""Получение метаданных вложения."""
|
||||||
|
valid_att_id = validate_uuid(attachment_id, "attachment_id")
|
||||||
|
return await storage.get_attachment(
|
||||||
|
caller_id=current_user.id,
|
||||||
|
attachment_id=valid_att_id,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/api/v1/attachments/{attachment_id}/content",
|
||||||
|
status_code=status.HTTP_200_OK,
|
||||||
|
)
|
||||||
|
async def download_attachment_content(
|
||||||
|
attachment_id: str,
|
||||||
|
variant: str | None = Query(default=None),
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> Response:
|
||||||
|
"""Скачивание бинарного содержимого вложения."""
|
||||||
|
valid_att_id = validate_uuid(attachment_id, "attachment_id")
|
||||||
|
data, content_type = await storage.get_attachment_content(
|
||||||
|
caller_id=current_user.id,
|
||||||
|
attachment_id=valid_att_id,
|
||||||
|
variant=variant,
|
||||||
|
)
|
||||||
|
return Response(
|
||||||
|
content=data,
|
||||||
|
media_type=content_type,
|
||||||
|
headers={
|
||||||
|
"Content-Length": str(len(data)),
|
||||||
|
},
|
||||||
|
)
|
||||||
@@ -0,0 +1,63 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import base64
|
||||||
|
from fastapi import APIRouter, Depends, Header, Request, Response, status
|
||||||
|
|
||||||
|
from app.dependencies import get_current_token
|
||||||
|
from app.errors import InvalidRequestError, UnauthorizedError
|
||||||
|
from app.schemas import SessionResponse
|
||||||
|
from app.storage import storage
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/auth/sessions", tags=["auth"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("", response_model=SessionResponse, status_code=status.HTTP_201_CREATED)
|
||||||
|
async def create_session(
|
||||||
|
request: Request,
|
||||||
|
authorization: str | None = Header(default=None, alias="Authorization"),
|
||||||
|
) -> SessionResponse:
|
||||||
|
"""Аутентификация пользователя через Basic Auth и создание новой сессии."""
|
||||||
|
body = await request.body()
|
||||||
|
if body and body.strip():
|
||||||
|
raise InvalidRequestError("Тело запроса для создания сессии должно быть пустым")
|
||||||
|
|
||||||
|
if not authorization or not authorization.strip():
|
||||||
|
raise UnauthorizedError("Отсутствует заголовок Authorization")
|
||||||
|
|
||||||
|
parts = authorization.strip().split(maxsplit=1)
|
||||||
|
if len(parts) != 2 or parts[0].lower() != "basic":
|
||||||
|
raise UnauthorizedError("Неверная схема авторизации (ожидается Basic)")
|
||||||
|
|
||||||
|
try:
|
||||||
|
raw_bytes = base64.b64decode(parts[1].strip())
|
||||||
|
raw_str = raw_bytes.decode("utf-8")
|
||||||
|
if ":" not in raw_str:
|
||||||
|
raise UnauthorizedError("Некорректный формат Basic credentials")
|
||||||
|
username, password = raw_str.split(":", 1)
|
||||||
|
except Exception:
|
||||||
|
raise UnauthorizedError("Некорректный заголовок Basic авторизации")
|
||||||
|
|
||||||
|
user = await storage.authenticate_user(username, password)
|
||||||
|
if not user:
|
||||||
|
raise UnauthorizedError("Неверное имя пользователя или пароль")
|
||||||
|
|
||||||
|
session = await storage.create_session(user)
|
||||||
|
return session
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/current", status_code=status.HTTP_204_NO_CONTENT)
|
||||||
|
async def delete_current_session(
|
||||||
|
request: Request,
|
||||||
|
token: str = Depends(get_current_token),
|
||||||
|
) -> Response:
|
||||||
|
"""Отзыв текущей сессии."""
|
||||||
|
body = await request.body()
|
||||||
|
if body and body.strip():
|
||||||
|
raise InvalidRequestError("Тело запроса для отзыва сессии должно быть пустым")
|
||||||
|
|
||||||
|
user = await storage.get_session_user(token)
|
||||||
|
if not user:
|
||||||
|
raise UnauthorizedError("Недействительная или уже отозванная сессия")
|
||||||
|
|
||||||
|
await storage.delete_session(token)
|
||||||
|
return Response(status_code=status.HTTP_204_NO_CONTENT)
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, Query, status
|
||||||
|
|
||||||
|
from app.dependencies import get_current_user
|
||||||
|
from app.schemas import (
|
||||||
|
ChatPageResponse,
|
||||||
|
ChatResponse,
|
||||||
|
CreateChatRequest,
|
||||||
|
UserResponse,
|
||||||
|
)
|
||||||
|
from app.storage import InvalidRequestError, storage
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/chats", tags=["chats"])
|
||||||
|
|
||||||
|
|
||||||
|
def validate_uuid(value: str, field_name: str = "id") -> str:
|
||||||
|
try:
|
||||||
|
UUID(value)
|
||||||
|
return value
|
||||||
|
except ValueError as exc:
|
||||||
|
raise InvalidRequestError(f"Поле '{field_name}' не является валидным UUID") from exc
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("", response_model=ChatResponse, status_code=status.HTTP_201_CREATED)
|
||||||
|
async def create_chat(
|
||||||
|
body: CreateChatRequest,
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> ChatResponse:
|
||||||
|
"""Создание нового чата."""
|
||||||
|
chat = await storage.create_chat(
|
||||||
|
caller_id=current_user.id,
|
||||||
|
title=body.title,
|
||||||
|
member_ids=body.member_ids,
|
||||||
|
)
|
||||||
|
return chat
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_model=ChatPageResponse, status_code=status.HTTP_200_OK)
|
||||||
|
async def list_chats(
|
||||||
|
limit: int = Query(default=50, ge=1, le=100),
|
||||||
|
cursor: str | None = Query(default=None),
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> ChatPageResponse:
|
||||||
|
"""Список чатов, в которых состоит текущий пользователь."""
|
||||||
|
items, next_cursor = await storage.list_chats(
|
||||||
|
caller_id=current_user.id,
|
||||||
|
limit=limit,
|
||||||
|
cursor=cursor,
|
||||||
|
)
|
||||||
|
return ChatPageResponse(items=items, next_cursor=next_cursor)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{chat_id}", response_model=ChatResponse, status_code=status.HTTP_200_OK)
|
||||||
|
async def get_chat(
|
||||||
|
chat_id: str,
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> ChatResponse:
|
||||||
|
"""Получение информации о чате по ID."""
|
||||||
|
valid_id = validate_uuid(chat_id, "chat_id")
|
||||||
|
chat = await storage.get_chat(caller_id=current_user.id, chat_id=valid_id)
|
||||||
|
return chat
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from fastapi import APIRouter, status
|
||||||
|
from starlette.exceptions import HTTPException as StarletteHTTPException
|
||||||
|
|
||||||
|
from app.schemas import HealthResponse
|
||||||
|
from app.storage import storage
|
||||||
|
|
||||||
|
router = APIRouter(tags=["health"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/health/live", response_model=HealthResponse, status_code=status.HTTP_200_OK
|
||||||
|
)
|
||||||
|
async def health_live() -> HealthResponse:
|
||||||
|
"""Проверка жизни процесса (Liveness)."""
|
||||||
|
return HealthResponse(status="ok")
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/health/ready", response_model=HealthResponse, status_code=status.HTTP_200_OK
|
||||||
|
)
|
||||||
|
async def health_ready() -> HealthResponse:
|
||||||
|
"""Проверка готовности сервиса и его зависимостей (Readiness)."""
|
||||||
|
is_ready = await storage.check_health()
|
||||||
|
if not is_ready:
|
||||||
|
raise StarletteHTTPException(
|
||||||
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||||
|
detail="Сервис или его хранилища недоступны",
|
||||||
|
)
|
||||||
|
return HealthResponse(status="ok")
|
||||||
@@ -0,0 +1,74 @@
|
|||||||
|
import re
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, Header, Query, status
|
||||||
|
|
||||||
|
from app.dependencies import get_current_user
|
||||||
|
from app.schemas import (
|
||||||
|
CreateMessageRequest,
|
||||||
|
MessagePageResponse,
|
||||||
|
MessageResponse,
|
||||||
|
UserResponse,
|
||||||
|
)
|
||||||
|
from app.storage import InvalidRequestError, storage
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/chats", tags=["messages"])
|
||||||
|
|
||||||
|
IDEMPOTENCY_KEY_REGEX = re.compile(r"^[A-Za-z0-9._:-]{1,128}$")
|
||||||
|
|
||||||
|
|
||||||
|
def validate_uuid(value: str, field_name: str = "id") -> str:
|
||||||
|
try:
|
||||||
|
UUID(value)
|
||||||
|
return value
|
||||||
|
except ValueError as exc:
|
||||||
|
raise InvalidRequestError(f"Поле '{field_name}' не является валидным UUID") from exc
|
||||||
|
|
||||||
|
|
||||||
|
@router.post(
|
||||||
|
"/{chat_id}/messages",
|
||||||
|
response_model=MessageResponse,
|
||||||
|
status_code=status.HTTP_201_CREATED,
|
||||||
|
)
|
||||||
|
async def create_message(
|
||||||
|
chat_id: str,
|
||||||
|
body: CreateMessageRequest,
|
||||||
|
idempotency_key: str | None = Header(default=None, alias="Idempotency-Key"),
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> MessageResponse:
|
||||||
|
"""Отправка сообщения в чат."""
|
||||||
|
if not idempotency_key or not IDEMPOTENCY_KEY_REGEX.match(idempotency_key):
|
||||||
|
raise InvalidRequestError(
|
||||||
|
"Заголовок 'Idempotency-Key' обязателен и должен соответствовать шаблону ^[A-Za-z0-9._:-]{1,128}$"
|
||||||
|
)
|
||||||
|
valid_id = validate_uuid(chat_id, "chat_id")
|
||||||
|
msg = await storage.send_message(
|
||||||
|
caller_id=current_user.id,
|
||||||
|
chat_id=valid_id,
|
||||||
|
text=body.text,
|
||||||
|
idempotency_key=idempotency_key,
|
||||||
|
attachment_ids=body.attachment_ids,
|
||||||
|
)
|
||||||
|
return msg
|
||||||
|
|
||||||
|
|
||||||
|
@router.get(
|
||||||
|
"/{chat_id}/messages",
|
||||||
|
response_model=MessagePageResponse,
|
||||||
|
status_code=status.HTTP_200_OK,
|
||||||
|
)
|
||||||
|
async def list_messages(
|
||||||
|
chat_id: str,
|
||||||
|
limit: int = Query(default=50, ge=1, le=100),
|
||||||
|
cursor: str | None = Query(default=None),
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> MessagePageResponse:
|
||||||
|
"""Получение истории сообщений чата с keyset-пагинацией."""
|
||||||
|
valid_id = validate_uuid(chat_id, "chat_id")
|
||||||
|
items, next_cursor = await storage.list_messages(
|
||||||
|
caller_id=current_user.id,
|
||||||
|
chat_id=valid_id,
|
||||||
|
limit=limit,
|
||||||
|
cursor=cursor,
|
||||||
|
)
|
||||||
|
return MessagePageResponse(items=items, next_cursor=next_cursor)
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from fastapi import APIRouter, Depends, status
|
||||||
|
|
||||||
|
from app.dependencies import get_current_user
|
||||||
|
from app.schemas import CreateUserRequest, UserResponse
|
||||||
|
from app.storage import storage
|
||||||
|
|
||||||
|
router = APIRouter(prefix="/api/v1/users", tags=["users"])
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("", response_model=UserResponse, status_code=status.HTTP_201_CREATED)
|
||||||
|
async def create_user(body: CreateUserRequest) -> UserResponse:
|
||||||
|
"""Регистрация нового пользователя."""
|
||||||
|
user = await storage.create_user(
|
||||||
|
username=body.username,
|
||||||
|
display_name=body.display_name,
|
||||||
|
password=body.password,
|
||||||
|
)
|
||||||
|
return user
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/me", response_model=UserResponse, status_code=status.HTTP_200_OK)
|
||||||
|
async def get_me(
|
||||||
|
current_user: UserResponse = Depends(get_current_user),
|
||||||
|
) -> UserResponse:
|
||||||
|
"""Получение профиля текущего пользователя."""
|
||||||
|
return current_user
|
||||||
+207
@@ -0,0 +1,207 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import datetime as dt
|
||||||
|
import re
|
||||||
|
from enum import Enum
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
|
from pydantic import BaseModel, ConfigDict, Field, field_validator, model_validator
|
||||||
|
|
||||||
|
|
||||||
|
def now_utc_iso() -> str:
|
||||||
|
"""Возвращает текущее время в формате RFC 3339 UTC с суффиксом Z."""
|
||||||
|
return dt.datetime.now(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorCode(str, Enum):
|
||||||
|
INVALID_REQUEST = "invalid_request"
|
||||||
|
UNAUTHORIZED = "unauthorized"
|
||||||
|
NOT_FOUND = "not_found"
|
||||||
|
CONFLICT = "conflict"
|
||||||
|
PAYLOAD_TOO_LARGE = "payload_too_large"
|
||||||
|
UNSUPPORTED_MEDIA_TYPE = "unsupported_media_type"
|
||||||
|
RATE_LIMITED = "rate_limited"
|
||||||
|
UNAVAILABLE = "unavailable"
|
||||||
|
INTERNAL_ERROR = "internal_error"
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorDetail(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
code: ErrorCode
|
||||||
|
message: str = Field(min_length=1)
|
||||||
|
request_id: str = Field(min_length=1)
|
||||||
|
|
||||||
|
|
||||||
|
class ErrorResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
error: ErrorDetail
|
||||||
|
|
||||||
|
|
||||||
|
class HealthResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
status: str = Field(default="ok")
|
||||||
|
|
||||||
|
|
||||||
|
class CreateUserRequest(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid", strict=True)
|
||||||
|
|
||||||
|
username: str = Field(min_length=3, max_length=32)
|
||||||
|
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, v: str) -> str:
|
||||||
|
if not re.match(r"^[a-z0-9_]{3,32}\Z", v):
|
||||||
|
raise ValueError("Имя пользователя должно соответствовать шаблону ^[a-z0-9_]{3,32}$")
|
||||||
|
return v
|
||||||
|
|
||||||
|
@field_validator("display_name")
|
||||||
|
@classmethod
|
||||||
|
def validate_display_name(cls, v: str) -> str:
|
||||||
|
if not v.strip():
|
||||||
|
raise ValueError("Отображаемое имя не может состоять только из пробелов")
|
||||||
|
return v
|
||||||
|
|
||||||
|
@field_validator("password")
|
||||||
|
@classmethod
|
||||||
|
def validate_password(cls, v: str) -> str:
|
||||||
|
if not v.strip():
|
||||||
|
raise ValueError("Пароль не может состоять только из пробелов")
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
class UserResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
id: str
|
||||||
|
username: str
|
||||||
|
display_name: str
|
||||||
|
created_at: str
|
||||||
|
|
||||||
|
|
||||||
|
class SessionResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
token: str = Field(min_length=32)
|
||||||
|
token_type: str = Field(default="Bearer")
|
||||||
|
expires_at: str
|
||||||
|
user: UserResponse
|
||||||
|
|
||||||
|
|
||||||
|
class CreateChatRequest(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("title")
|
||||||
|
@classmethod
|
||||||
|
def validate_title(cls, v: str) -> str:
|
||||||
|
if not v.strip():
|
||||||
|
raise ValueError("Название не может состоять только из пробелов")
|
||||||
|
return v
|
||||||
|
|
||||||
|
@field_validator("member_ids")
|
||||||
|
@classmethod
|
||||||
|
def validate_member_ids(cls, v: list[str]) -> list[str]:
|
||||||
|
if len(v) != len(set(v)):
|
||||||
|
raise ValueError("Идентификаторы участников должны быть уникальными")
|
||||||
|
for member_id in v:
|
||||||
|
try:
|
||||||
|
UUID(member_id)
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError(f"Идентификатор участника '{member_id}' не является валидным UUID")
|
||||||
|
return v
|
||||||
|
|
||||||
|
|
||||||
|
class ChatResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
id: str
|
||||||
|
title: str
|
||||||
|
member_ids: list[str]
|
||||||
|
created_at: str
|
||||||
|
|
||||||
|
|
||||||
|
class ChatPageResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
items: list[ChatResponse]
|
||||||
|
next_cursor: str | None = None
|
||||||
|
|
||||||
|
|
||||||
|
class VariantResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
name: str
|
||||||
|
content_type: str
|
||||||
|
size_bytes: int
|
||||||
|
width: int
|
||||||
|
height: int
|
||||||
|
sha256: str
|
||||||
|
|
||||||
|
|
||||||
|
class AttachmentResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
id: str
|
||||||
|
chat_id: str
|
||||||
|
owner_id: str
|
||||||
|
filename: str
|
||||||
|
content_type: str
|
||||||
|
size_bytes: int
|
||||||
|
sha256: str
|
||||||
|
status: str = "ready"
|
||||||
|
variants: list[VariantResponse] = Field(default_factory=list)
|
||||||
|
error_code: str | None = None
|
||||||
|
created_at: str
|
||||||
|
updated_at: str
|
||||||
|
|
||||||
|
|
||||||
|
class CreateMessageRequest(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid", strict=True)
|
||||||
|
|
||||||
|
text: str = Field(default="", max_length=4000)
|
||||||
|
attachment_ids: list[str] = Field(default_factory=list, max_length=10)
|
||||||
|
|
||||||
|
@field_validator("attachment_ids")
|
||||||
|
@classmethod
|
||||||
|
def validate_attachment_ids(cls, v: list[str]) -> list[str]:
|
||||||
|
if len(v) != len(set(v)):
|
||||||
|
raise ValueError("attachment_ids must contain unique items")
|
||||||
|
for item in v:
|
||||||
|
try:
|
||||||
|
UUID(item)
|
||||||
|
except ValueError:
|
||||||
|
raise ValueError(f"Идентификатор вложения '{item}' не является валидным UUID")
|
||||||
|
return v
|
||||||
|
|
||||||
|
@model_validator(mode="after")
|
||||||
|
def check_text_or_attachment(self) -> CreateMessageRequest:
|
||||||
|
has_text = bool(self.text and self.text.strip())
|
||||||
|
has_attachments = bool(self.attachment_ids)
|
||||||
|
if not has_text and not has_attachments:
|
||||||
|
raise ValueError("Сообщение должно содержать непустой текст или хотя бы одно вложение")
|
||||||
|
return self
|
||||||
|
|
||||||
|
|
||||||
|
class MessageResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
id: str
|
||||||
|
chat_id: str
|
||||||
|
sender_id: str
|
||||||
|
text: str
|
||||||
|
created_at: str
|
||||||
|
attachment_ids: list[str] = Field(default_factory=list)
|
||||||
|
|
||||||
|
|
||||||
|
class MessagePageResponse(BaseModel):
|
||||||
|
model_config = ConfigDict(extra="forbid")
|
||||||
|
|
||||||
|
items: list[MessageResponse]
|
||||||
|
next_cursor: str | None = None
|
||||||
+949
@@ -0,0 +1,949 @@
|
|||||||
|
import asyncio
|
||||||
|
import base64
|
||||||
|
from contextlib import asynccontextmanager
|
||||||
|
import datetime as dt
|
||||||
|
import hashlib
|
||||||
|
import json
|
||||||
|
import logging
|
||||||
|
import uuid
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
import asyncpg
|
||||||
|
import redis.asyncio as redis
|
||||||
|
|
||||||
|
import io
|
||||||
|
import boto3
|
||||||
|
import botocore.exceptions
|
||||||
|
from PIL import Image
|
||||||
|
|
||||||
|
from app.auth import generate_session_token, hash_password, verify_password
|
||||||
|
from app.config import (
|
||||||
|
CACHE_URL,
|
||||||
|
DATABASE_URL,
|
||||||
|
SESSION_TTL_SECONDS,
|
||||||
|
S3_ENDPOINT,
|
||||||
|
S3_REGION,
|
||||||
|
S3_BUCKET,
|
||||||
|
S3_ACCESS_KEY_ID,
|
||||||
|
S3_SECRET_ACCESS_KEY,
|
||||||
|
MAX_UPLOAD_BYTES,
|
||||||
|
MAX_IMAGE_PIXELS,
|
||||||
|
)
|
||||||
|
from app.errors import (
|
||||||
|
ConflictError,
|
||||||
|
InvalidRequestError,
|
||||||
|
NotFoundError,
|
||||||
|
PayloadTooLargeError,
|
||||||
|
UnauthorizedError,
|
||||||
|
UnsupportedMediaTypeError,
|
||||||
|
)
|
||||||
|
from app.schemas import (
|
||||||
|
AttachmentResponse,
|
||||||
|
ChatResponse,
|
||||||
|
MessageResponse,
|
||||||
|
SessionResponse,
|
||||||
|
UserResponse,
|
||||||
|
VariantResponse,
|
||||||
|
now_utc_iso,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger = logging.getLogger("app.storage")
|
||||||
|
|
||||||
|
|
||||||
|
def encode_cursor(data: dict) -> str:
|
||||||
|
raw = json.dumps(data, separators=(",", ":")).encode("utf-8")
|
||||||
|
return base64.urlsafe_b64encode(raw).decode("utf-8").rstrip("=")
|
||||||
|
|
||||||
|
|
||||||
|
def decode_cursor(cursor_str: str) -> dict:
|
||||||
|
if not isinstance(cursor_str, str) or not cursor_str.strip():
|
||||||
|
raise InvalidRequestError("Некорректный курсор")
|
||||||
|
try:
|
||||||
|
padded = cursor_str + "=" * (-len(cursor_str) % 4)
|
||||||
|
raw = base64.urlsafe_b64decode(padded.encode("utf-8"))
|
||||||
|
data = json.loads(raw.decode("utf-8"))
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise InvalidRequestError("Некорректный курсор")
|
||||||
|
return data
|
||||||
|
except InvalidRequestError:
|
||||||
|
raise
|
||||||
|
except Exception as exc:
|
||||||
|
raise InvalidRequestError("Некорректный курсор") from exc
|
||||||
|
|
||||||
|
|
||||||
|
def _format_dt(d: dt.datetime) -> str:
|
||||||
|
if d.tzinfo is None:
|
||||||
|
d = d.replace(tzinfo=dt.timezone.utc)
|
||||||
|
return d.astimezone(dt.timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%fZ")
|
||||||
|
|
||||||
|
|
||||||
|
def _to_text(val: Any) -> str:
|
||||||
|
if isinstance(val, (bytes, bytearray, memoryview)):
|
||||||
|
return bytes(val).decode("utf-8")
|
||||||
|
return str(val)
|
||||||
|
|
||||||
|
|
||||||
|
class PostgresStorage:
|
||||||
|
def __init__(self) -> None:
|
||||||
|
self.pg_pool: asyncpg.Pool | None = None
|
||||||
|
self.redis_client: redis.Redis | None = None
|
||||||
|
self.s3_client: Any = None
|
||||||
|
|
||||||
|
@asynccontextmanager
|
||||||
|
async def acquire_conn(self, timeout: float = 2.0):
|
||||||
|
if self.pg_pool is None:
|
||||||
|
raise asyncpg.InterfaceError("Пул подключений к PostgreSQL не инициализирован")
|
||||||
|
async with asyncio.timeout(3.0):
|
||||||
|
async with self.pg_pool.acquire(timeout=timeout) as conn:
|
||||||
|
yield conn
|
||||||
|
|
||||||
|
async def connect(self) -> None:
|
||||||
|
logger.info("Подключение к PostgreSQL: %s", DATABASE_URL.split("@")[-1])
|
||||||
|
self.pg_pool = await asyncpg.create_pool(
|
||||||
|
DATABASE_URL,
|
||||||
|
min_size=2,
|
||||||
|
max_size=20,
|
||||||
|
command_timeout=3.0,
|
||||||
|
timeout=2.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info("Подключение к Redis: %s", CACHE_URL.split("@")[-1])
|
||||||
|
self.redis_client = redis.from_url(
|
||||||
|
CACHE_URL,
|
||||||
|
decode_responses=True,
|
||||||
|
socket_connect_timeout=2.0,
|
||||||
|
socket_timeout=2.0,
|
||||||
|
)
|
||||||
|
|
||||||
|
logger.info("Подключение к S3 (MinIO): %s, bucket: %s", S3_ENDPOINT, S3_BUCKET)
|
||||||
|
self.s3_client = boto3.client(
|
||||||
|
"s3",
|
||||||
|
endpoint_url=S3_ENDPOINT,
|
||||||
|
aws_access_key_id=S3_ACCESS_KEY_ID,
|
||||||
|
aws_secret_access_key=S3_SECRET_ACCESS_KEY,
|
||||||
|
region_name=S3_REGION,
|
||||||
|
)
|
||||||
|
await self._ensure_bucket()
|
||||||
|
|
||||||
|
async def _ensure_bucket(self) -> None:
|
||||||
|
def _init():
|
||||||
|
try:
|
||||||
|
self.s3_client.head_bucket(Bucket=S3_BUCKET)
|
||||||
|
except Exception:
|
||||||
|
try:
|
||||||
|
self.s3_client.create_bucket(Bucket=S3_BUCKET)
|
||||||
|
logger.info("Создан приватный бакет S3: %s", S3_BUCKET)
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning("Не удалось создать бакет S3 %s: %s", S3_BUCKET, e)
|
||||||
|
|
||||||
|
await asyncio.to_thread(_init)
|
||||||
|
|
||||||
|
async def disconnect(self) -> None:
|
||||||
|
if self.pg_pool:
|
||||||
|
await self.pg_pool.close()
|
||||||
|
self.pg_pool = None
|
||||||
|
if self.redis_client:
|
||||||
|
await self.redis_client.close()
|
||||||
|
self.redis_client = None
|
||||||
|
self.s3_client = None
|
||||||
|
|
||||||
|
async def check_health(self) -> bool:
|
||||||
|
if not self.pg_pool or not self.redis_client or not self.s3_client:
|
||||||
|
return False
|
||||||
|
try:
|
||||||
|
async with asyncio.timeout(2.5):
|
||||||
|
async with self.acquire_conn(timeout=1.5) as conn:
|
||||||
|
await conn.fetchval("SELECT 1")
|
||||||
|
await self.redis_client.ping()
|
||||||
|
await asyncio.to_thread(self.s3_client.head_bucket, Bucket=S3_BUCKET)
|
||||||
|
return True
|
||||||
|
except Exception as exc:
|
||||||
|
logger.warning("Ошибка проверки health: %s", exc)
|
||||||
|
return False
|
||||||
|
|
||||||
|
# ------------------ Пользователи и аутентификация ------------------
|
||||||
|
|
||||||
|
async def create_user(
|
||||||
|
self, username: str, display_name: str, password: str
|
||||||
|
) -> UserResponse:
|
||||||
|
user_id = uuid.uuid4()
|
||||||
|
now = dt.datetime.now(dt.timezone.utc)
|
||||||
|
pwd_hash = await asyncio.to_thread(hash_password, password)
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
await conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5)
|
||||||
|
""",
|
||||||
|
user_id,
|
||||||
|
username,
|
||||||
|
display_name,
|
||||||
|
pwd_hash,
|
||||||
|
now,
|
||||||
|
)
|
||||||
|
except asyncpg.UniqueViolationError:
|
||||||
|
raise ConflictError("Имя пользователя уже занято")
|
||||||
|
|
||||||
|
return UserResponse(
|
||||||
|
id=str(user_id),
|
||||||
|
username=username,
|
||||||
|
display_name=display_name,
|
||||||
|
created_at=_format_dt(now),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def authenticate_user(
|
||||||
|
self, username: str, password: str
|
||||||
|
) -> UserResponse | None:
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
row = await conn.fetchrow(
|
||||||
|
"""
|
||||||
|
SELECT id, username, display_name, password_hash, created_at
|
||||||
|
FROM users
|
||||||
|
WHERE username = $1
|
||||||
|
""",
|
||||||
|
username,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not row:
|
||||||
|
return None
|
||||||
|
|
||||||
|
is_valid = await asyncio.to_thread(verify_password, row["password_hash"], password)
|
||||||
|
if not is_valid:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return UserResponse(
|
||||||
|
id=str(row["id"]),
|
||||||
|
username=row["username"],
|
||||||
|
display_name=_to_text(row["display_name"]),
|
||||||
|
created_at=_format_dt(row["created_at"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_user_by_id(self, user_id: str) -> UserResponse | None:
|
||||||
|
try:
|
||||||
|
uid = uuid.UUID(user_id)
|
||||||
|
except ValueError:
|
||||||
|
return None
|
||||||
|
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
row = await conn.fetchrow(
|
||||||
|
"""
|
||||||
|
SELECT id, username, display_name, created_at
|
||||||
|
FROM users
|
||||||
|
WHERE id = $1
|
||||||
|
""",
|
||||||
|
uid,
|
||||||
|
)
|
||||||
|
|
||||||
|
if not row:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return UserResponse(
|
||||||
|
id=str(row["id"]),
|
||||||
|
username=row["username"],
|
||||||
|
display_name=_to_text(row["display_name"]),
|
||||||
|
created_at=_format_dt(row["created_at"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
# ------------------ Сессии (Redis) ------------------
|
||||||
|
|
||||||
|
async def create_session(self, user: UserResponse) -> SessionResponse:
|
||||||
|
assert self.redis_client is not None
|
||||||
|
token = generate_session_token()
|
||||||
|
now = dt.datetime.now(dt.timezone.utc)
|
||||||
|
expires_at = now + dt.timedelta(seconds=SESSION_TTL_SECONDS)
|
||||||
|
expires_at_str = _format_dt(expires_at)
|
||||||
|
|
||||||
|
session_data = json.dumps(
|
||||||
|
{
|
||||||
|
"user_id": user.id,
|
||||||
|
"expires_at": expires_at_str,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
key = f"session:{token}"
|
||||||
|
await self.redis_client.set(key, session_data, ex=SESSION_TTL_SECONDS)
|
||||||
|
|
||||||
|
return SessionResponse(
|
||||||
|
token=token,
|
||||||
|
token_type="Bearer",
|
||||||
|
expires_at=expires_at_str,
|
||||||
|
user=user,
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_session_user(self, token: str) -> UserResponse | None:
|
||||||
|
if not token or not self.redis_client:
|
||||||
|
return None
|
||||||
|
|
||||||
|
key = f"session:{token}"
|
||||||
|
raw = await self.redis_client.get(key)
|
||||||
|
if not raw:
|
||||||
|
return None
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = json.loads(raw)
|
||||||
|
user_id = data.get("user_id")
|
||||||
|
except Exception:
|
||||||
|
return None
|
||||||
|
|
||||||
|
if not user_id:
|
||||||
|
return None
|
||||||
|
|
||||||
|
return await self.get_user_by_id(user_id)
|
||||||
|
|
||||||
|
async def delete_session(self, token: str) -> bool:
|
||||||
|
if not token or not self.redis_client:
|
||||||
|
return False
|
||||||
|
key = f"session:{token}"
|
||||||
|
res = await self.redis_client.delete(key)
|
||||||
|
return res > 0
|
||||||
|
|
||||||
|
# ------------------ Чаты ------------------
|
||||||
|
|
||||||
|
async def create_chat(
|
||||||
|
self, caller_id: str, title: str, member_ids: list[str]
|
||||||
|
) -> ChatResponse:
|
||||||
|
if caller_id not in member_ids:
|
||||||
|
raise InvalidRequestError("Создатель чата должен быть в списке участников")
|
||||||
|
|
||||||
|
chat_id = uuid.uuid4()
|
||||||
|
now = dt.datetime.now(dt.timezone.utc)
|
||||||
|
|
||||||
|
member_uuids = [uuid.UUID(m) for m in member_ids]
|
||||||
|
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
# Проверяем существование всех участников
|
||||||
|
found = await conn.fetch(
|
||||||
|
"SELECT id FROM users WHERE id = ANY($1::uuid[])", member_uuids
|
||||||
|
)
|
||||||
|
if len(found) != len(member_uuids):
|
||||||
|
raise InvalidRequestError(
|
||||||
|
"Один или несколько участников чата не найдены"
|
||||||
|
)
|
||||||
|
|
||||||
|
async with conn.transaction():
|
||||||
|
await conn.execute(
|
||||||
|
"INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)",
|
||||||
|
chat_id,
|
||||||
|
title,
|
||||||
|
now,
|
||||||
|
)
|
||||||
|
member_records = [(chat_id, m) for m in member_uuids]
|
||||||
|
await conn.executemany(
|
||||||
|
"INSERT INTO chat_members (chat_id, user_id) VALUES ($1, $2)",
|
||||||
|
member_records,
|
||||||
|
)
|
||||||
|
|
||||||
|
return ChatResponse(
|
||||||
|
id=str(chat_id),
|
||||||
|
title=title,
|
||||||
|
member_ids=member_ids,
|
||||||
|
created_at=_format_dt(now),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_chat(self, caller_id: str, chat_id: str) -> ChatResponse:
|
||||||
|
try:
|
||||||
|
cid = uuid.UUID(chat_id)
|
||||||
|
c_caller = uuid.UUID(caller_id)
|
||||||
|
except ValueError:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
# Проверяем членство вызывающего
|
||||||
|
is_member = await conn.fetchval(
|
||||||
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
|
cid,
|
||||||
|
c_caller,
|
||||||
|
)
|
||||||
|
if not is_member:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
chat_row = await conn.fetchrow(
|
||||||
|
"SELECT id, title, created_at FROM chats WHERE id = $1", cid
|
||||||
|
)
|
||||||
|
if not chat_row:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
members_rows = await conn.fetch(
|
||||||
|
"SELECT user_id FROM chat_members WHERE chat_id = $1", cid
|
||||||
|
)
|
||||||
|
member_ids = [str(r["user_id"]) for r in members_rows]
|
||||||
|
|
||||||
|
return ChatResponse(
|
||||||
|
id=str(chat_row["id"]),
|
||||||
|
title=_to_text(chat_row["title"]),
|
||||||
|
member_ids=member_ids,
|
||||||
|
created_at=_format_dt(chat_row["created_at"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def list_chats(
|
||||||
|
self, caller_id: str, limit: int = 50, cursor: str | None = None
|
||||||
|
) -> tuple[list[ChatResponse], str | None]:
|
||||||
|
try:
|
||||||
|
c_caller = uuid.UUID(caller_id)
|
||||||
|
except ValueError:
|
||||||
|
return [], None
|
||||||
|
|
||||||
|
c_created: dt.datetime | None = None
|
||||||
|
c_id: uuid.UUID | None = None
|
||||||
|
|
||||||
|
if cursor is not None:
|
||||||
|
c_data = decode_cursor(cursor)
|
||||||
|
if c_data.get("type") != "chats":
|
||||||
|
raise InvalidRequestError("Недействительный тип курсора")
|
||||||
|
c_created_str = c_data.get("created_at")
|
||||||
|
c_id_str = c_data.get("id")
|
||||||
|
if (
|
||||||
|
not isinstance(c_created_str, str)
|
||||||
|
or not isinstance(c_id_str, str)
|
||||||
|
or not c_created_str.strip()
|
||||||
|
or not c_id_str.strip()
|
||||||
|
):
|
||||||
|
raise InvalidRequestError("Поврежденный курсор")
|
||||||
|
try:
|
||||||
|
c_created = dt.datetime.fromisoformat(
|
||||||
|
c_created_str.replace("Z", "+00:00")
|
||||||
|
)
|
||||||
|
c_id = uuid.UUID(c_id_str)
|
||||||
|
except Exception:
|
||||||
|
raise InvalidRequestError("Некорректные параметры курсора")
|
||||||
|
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
query = """
|
||||||
|
SELECT c.id, c.title, c.created_at, array_agg(cm.user_id::text) as member_ids
|
||||||
|
FROM chats c
|
||||||
|
JOIN chat_members my_m ON c.id = my_m.chat_id AND my_m.user_id = $1
|
||||||
|
JOIN chat_members cm ON c.id = cm.chat_id
|
||||||
|
WHERE ($2::timestamptz IS NULL OR (c.created_at, c.id) > ($2::timestamptz, $3::uuid))
|
||||||
|
GROUP BY c.id, c.title, c.created_at
|
||||||
|
ORDER BY c.created_at ASC, c.id ASC
|
||||||
|
LIMIT $4
|
||||||
|
"""
|
||||||
|
rows = await conn.fetch(query, c_caller, c_created, c_id, limit + 1)
|
||||||
|
|
||||||
|
has_next = len(rows) > limit
|
||||||
|
items_rows = rows[:limit]
|
||||||
|
|
||||||
|
items = [
|
||||||
|
ChatResponse(
|
||||||
|
id=str(r["id"]),
|
||||||
|
title=_to_text(r["title"]),
|
||||||
|
member_ids=list(r["member_ids"]),
|
||||||
|
created_at=_format_dt(r["created_at"]),
|
||||||
|
)
|
||||||
|
for r in items_rows
|
||||||
|
]
|
||||||
|
|
||||||
|
next_cursor = None
|
||||||
|
if has_next and items:
|
||||||
|
last = items[-1]
|
||||||
|
next_cursor = encode_cursor(
|
||||||
|
{
|
||||||
|
"type": "chats",
|
||||||
|
"created_at": last.created_at,
|
||||||
|
"id": last.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return items, next_cursor
|
||||||
|
|
||||||
|
# ------------------ Сообщения ------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def compute_request_hash(text: str, attachment_ids: list[str] | None = None) -> str:
|
||||||
|
norm_attachments = [str(x) for x in (attachment_ids or [])]
|
||||||
|
canonical = json.dumps(
|
||||||
|
{"attachment_ids": norm_attachments, "text": text},
|
||||||
|
sort_keys=True,
|
||||||
|
ensure_ascii=False,
|
||||||
|
)
|
||||||
|
return hashlib.sha256(canonical.encode("utf-8")).hexdigest()
|
||||||
|
|
||||||
|
async def send_message(
|
||||||
|
self,
|
||||||
|
caller_id: str,
|
||||||
|
chat_id: str,
|
||||||
|
text: str,
|
||||||
|
idempotency_key: str,
|
||||||
|
attachment_ids: list[str] | None = None,
|
||||||
|
) -> MessageResponse:
|
||||||
|
try:
|
||||||
|
cid = uuid.UUID(chat_id)
|
||||||
|
c_caller = uuid.UUID(caller_id)
|
||||||
|
except ValueError:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
norm_attachments = [str(x) for x in (attachment_ids or [])]
|
||||||
|
req_hash = self.compute_request_hash(text, norm_attachments)
|
||||||
|
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
is_member = await conn.fetchval(
|
||||||
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
|
cid,
|
||||||
|
c_caller,
|
||||||
|
)
|
||||||
|
if not is_member:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
# Валидация прикрепляемых вложений
|
||||||
|
att_uuids: list[uuid.UUID] = []
|
||||||
|
if norm_attachments:
|
||||||
|
for att_str in norm_attachments:
|
||||||
|
try:
|
||||||
|
att_uuids.append(uuid.UUID(att_str))
|
||||||
|
except ValueError:
|
||||||
|
raise NotFoundError("Вложение не найдено")
|
||||||
|
|
||||||
|
rows = await conn.fetch(
|
||||||
|
"SELECT id, chat_id, status FROM attachments WHERE id = ANY($1::uuid[])",
|
||||||
|
att_uuids,
|
||||||
|
)
|
||||||
|
if len(rows) != len(att_uuids):
|
||||||
|
raise NotFoundError("Одно или несколько вложений не найдены")
|
||||||
|
|
||||||
|
for r in rows:
|
||||||
|
if r["chat_id"] != cid:
|
||||||
|
raise NotFoundError("Вложение принадлежит другому чату")
|
||||||
|
if r["status"] != "ready":
|
||||||
|
raise ConflictError("Вложение еще не готово или повреждено")
|
||||||
|
|
||||||
|
# 1. Проверяем, обрабатывался ли уже этот idempotency_key
|
||||||
|
existing = await conn.fetchrow(
|
||||||
|
"""
|
||||||
|
SELECT ik.message_id, ik.request_hash, m.text, m.attachment_ids, m.created_at AS msg_created_at
|
||||||
|
FROM idempotency_keys ik
|
||||||
|
JOIN messages m ON m.id = ik.message_id
|
||||||
|
WHERE ik.user_id = $1 AND ik.chat_id = $2 AND ik.idempotency_key = $3
|
||||||
|
""",
|
||||||
|
c_caller,
|
||||||
|
cid,
|
||||||
|
idempotency_key,
|
||||||
|
)
|
||||||
|
if existing:
|
||||||
|
if existing["request_hash"] == req_hash:
|
||||||
|
raw_text = existing["text"]
|
||||||
|
msg_text = (
|
||||||
|
raw_text.decode("utf-8")
|
||||||
|
if isinstance(raw_text, (bytes, bytearray))
|
||||||
|
else str(raw_text)
|
||||||
|
)
|
||||||
|
res_attachments = [
|
||||||
|
str(a) for a in (existing["attachment_ids"] or [])
|
||||||
|
]
|
||||||
|
return MessageResponse(
|
||||||
|
id=str(existing["message_id"]),
|
||||||
|
chat_id=chat_id,
|
||||||
|
sender_id=caller_id,
|
||||||
|
text=msg_text,
|
||||||
|
attachment_ids=res_attachments,
|
||||||
|
created_at=_format_dt(existing["msg_created_at"]),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise ConflictError("Ключ идемпотентности уже использован с другим телом запроса")
|
||||||
|
|
||||||
|
msg_id = uuid.uuid4()
|
||||||
|
now = dt.datetime.now(dt.timezone.utc)
|
||||||
|
text_bytes = text.encode("utf-8")
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with conn.transaction():
|
||||||
|
await conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO messages (id, chat_id, sender_id, text, attachment_ids, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
""",
|
||||||
|
msg_id,
|
||||||
|
cid,
|
||||||
|
c_caller,
|
||||||
|
text_bytes,
|
||||||
|
att_uuids,
|
||||||
|
now,
|
||||||
|
)
|
||||||
|
await conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO idempotency_keys (user_id, chat_id, idempotency_key, message_id, request_hash, created_at)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6)
|
||||||
|
""",
|
||||||
|
c_caller,
|
||||||
|
cid,
|
||||||
|
idempotency_key,
|
||||||
|
msg_id,
|
||||||
|
req_hash,
|
||||||
|
now,
|
||||||
|
)
|
||||||
|
except asyncpg.UniqueViolationError:
|
||||||
|
winner = await conn.fetchrow(
|
||||||
|
"""
|
||||||
|
SELECT ik.message_id, ik.request_hash, m.text, m.attachment_ids, m.created_at AS msg_created_at
|
||||||
|
FROM idempotency_keys ik
|
||||||
|
JOIN messages m ON m.id = ik.message_id
|
||||||
|
WHERE ik.user_id = $1 AND ik.chat_id = $2 AND ik.idempotency_key = $3
|
||||||
|
""",
|
||||||
|
c_caller,
|
||||||
|
cid,
|
||||||
|
idempotency_key,
|
||||||
|
)
|
||||||
|
if winner:
|
||||||
|
if winner["request_hash"] == req_hash:
|
||||||
|
raw_text = winner["text"]
|
||||||
|
msg_text = (
|
||||||
|
raw_text.decode("utf-8")
|
||||||
|
if isinstance(raw_text, (bytes, bytearray))
|
||||||
|
else str(raw_text)
|
||||||
|
)
|
||||||
|
res_attachments = [
|
||||||
|
str(a) for a in (winner["attachment_ids"] or [])
|
||||||
|
]
|
||||||
|
return MessageResponse(
|
||||||
|
id=str(winner["message_id"]),
|
||||||
|
chat_id=chat_id,
|
||||||
|
sender_id=caller_id,
|
||||||
|
text=msg_text,
|
||||||
|
attachment_ids=res_attachments,
|
||||||
|
created_at=_format_dt(winner["msg_created_at"]),
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise ConflictError("Ключ идемпотентности уже использован с другим телом запроса")
|
||||||
|
raise
|
||||||
|
|
||||||
|
return MessageResponse(
|
||||||
|
id=str(msg_id),
|
||||||
|
chat_id=chat_id,
|
||||||
|
sender_id=caller_id,
|
||||||
|
text=text,
|
||||||
|
attachment_ids=norm_attachments,
|
||||||
|
created_at=_format_dt(now),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def list_messages(
|
||||||
|
self, caller_id: str, chat_id: str, limit: int = 50, cursor: str | None = None
|
||||||
|
) -> tuple[list[MessageResponse], str | None]:
|
||||||
|
try:
|
||||||
|
cid = uuid.UUID(chat_id)
|
||||||
|
c_caller = uuid.UUID(caller_id)
|
||||||
|
except ValueError:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
is_member = await conn.fetchval(
|
||||||
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
|
cid,
|
||||||
|
c_caller,
|
||||||
|
)
|
||||||
|
if not is_member:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
c_created: dt.datetime | None = None
|
||||||
|
c_id: uuid.UUID | None = None
|
||||||
|
|
||||||
|
if cursor is not None:
|
||||||
|
c_data = decode_cursor(cursor)
|
||||||
|
if c_data.get("type") != "messages" or c_data.get("chat_id") != chat_id:
|
||||||
|
raise InvalidRequestError(
|
||||||
|
"Курсор принадлежит другому чату или недействителен"
|
||||||
|
)
|
||||||
|
c_created_str = c_data.get("created_at")
|
||||||
|
c_id_str = c_data.get("id")
|
||||||
|
if (
|
||||||
|
not isinstance(c_created_str, str)
|
||||||
|
or not isinstance(c_id_str, str)
|
||||||
|
or not c_created_str.strip()
|
||||||
|
or not c_id_str.strip()
|
||||||
|
):
|
||||||
|
raise InvalidRequestError("Поврежденный курсор")
|
||||||
|
try:
|
||||||
|
c_created = dt.datetime.fromisoformat(
|
||||||
|
c_created_str.replace("Z", "+00:00")
|
||||||
|
)
|
||||||
|
c_id = uuid.UUID(c_id_str)
|
||||||
|
except Exception:
|
||||||
|
raise InvalidRequestError("Некорректные параметры курсора")
|
||||||
|
|
||||||
|
query = """
|
||||||
|
SELECT id, chat_id, sender_id, text, attachment_ids, created_at
|
||||||
|
FROM messages
|
||||||
|
WHERE chat_id = $1
|
||||||
|
AND ($2::timestamptz IS NULL OR (created_at, id) > ($2::timestamptz, $3::uuid))
|
||||||
|
ORDER BY created_at ASC, id ASC
|
||||||
|
LIMIT $4
|
||||||
|
"""
|
||||||
|
rows = await conn.fetch(query, cid, c_created, c_id, limit + 1)
|
||||||
|
|
||||||
|
has_next = len(rows) > limit
|
||||||
|
items_rows = rows[:limit]
|
||||||
|
|
||||||
|
def _to_text(val: Any) -> str:
|
||||||
|
if isinstance(val, (bytes, bytearray, memoryview)):
|
||||||
|
return bytes(val).decode("utf-8")
|
||||||
|
return str(val)
|
||||||
|
|
||||||
|
items = [
|
||||||
|
MessageResponse(
|
||||||
|
id=str(r["id"]),
|
||||||
|
chat_id=str(r["chat_id"]),
|
||||||
|
sender_id=str(r["sender_id"]),
|
||||||
|
text=_to_text(r["text"]),
|
||||||
|
attachment_ids=[str(a) for a in (r["attachment_ids"] or [])],
|
||||||
|
created_at=_format_dt(r["created_at"]),
|
||||||
|
)
|
||||||
|
for r in items_rows
|
||||||
|
]
|
||||||
|
|
||||||
|
next_cursor = None
|
||||||
|
if has_next and items:
|
||||||
|
last = items[-1]
|
||||||
|
next_cursor = encode_cursor(
|
||||||
|
{
|
||||||
|
"type": "messages",
|
||||||
|
"chat_id": chat_id,
|
||||||
|
"created_at": last.created_at,
|
||||||
|
"id": last.id,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
return items, next_cursor
|
||||||
|
|
||||||
|
# ------------------ Вложения (Attachments) ------------------
|
||||||
|
|
||||||
|
async def upload_attachment(
|
||||||
|
self,
|
||||||
|
caller_id: str,
|
||||||
|
chat_id: str,
|
||||||
|
filename: str,
|
||||||
|
content_type: str,
|
||||||
|
data: bytes,
|
||||||
|
) -> AttachmentResponse:
|
||||||
|
try:
|
||||||
|
cid = uuid.UUID(chat_id)
|
||||||
|
c_caller = uuid.UUID(caller_id)
|
||||||
|
except ValueError:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
# 1. Валидация filename
|
||||||
|
if not filename or len(filename) > 255:
|
||||||
|
raise InvalidRequestError("Некорректная длина имени файла (1-255 символов)")
|
||||||
|
if any(c in filename for c in ("/", "\\", "\0")) or any(
|
||||||
|
ord(c) < 32 for c in filename
|
||||||
|
):
|
||||||
|
raise InvalidRequestError("Имя файла содержит недопустимые символы")
|
||||||
|
|
||||||
|
# 2. Проверка прав (ACL)
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
is_member = await conn.fetchval(
|
||||||
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
|
cid,
|
||||||
|
c_caller,
|
||||||
|
)
|
||||||
|
if not is_member:
|
||||||
|
raise NotFoundError("Чат не найден")
|
||||||
|
|
||||||
|
# 3. Валидация Content-Type
|
||||||
|
ct_clean = content_type.split(";")[0].strip().lower()
|
||||||
|
if ct_clean not in ("image/png", "image/jpeg"):
|
||||||
|
raise UnsupportedMediaTypeError(
|
||||||
|
"Поддерживаются только форматы image/png и image/jpeg"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 4. Валидация размера данных
|
||||||
|
size_bytes = len(data)
|
||||||
|
if size_bytes > MAX_UPLOAD_BYTES:
|
||||||
|
raise PayloadTooLargeError(
|
||||||
|
f"Размер файла {size_bytes} байт превышает допустимый лимит {MAX_UPLOAD_BYTES} байт"
|
||||||
|
)
|
||||||
|
|
||||||
|
# 5. Проверка магических байтов (сигнатуры MIME)
|
||||||
|
if ct_clean == "image/png":
|
||||||
|
if not data.startswith(b"\x89PNG\r\n\x1a\n"):
|
||||||
|
raise UnsupportedMediaTypeError("Содержимое файла не соответствует формату PNG")
|
||||||
|
elif ct_clean == "image/jpeg":
|
||||||
|
if not (data.startswith(b"\xff\xd8\xff")):
|
||||||
|
raise UnsupportedMediaTypeError("Содержимое файла не соответствует формату JPEG")
|
||||||
|
|
||||||
|
# 6. Проверка разрешения (лимит 40 млн пикселей) и целостности растра
|
||||||
|
def _check_dimensions():
|
||||||
|
try:
|
||||||
|
with Image.open(io.BytesIO(data)) as img:
|
||||||
|
w, h = img.size
|
||||||
|
if w * h > MAX_IMAGE_PIXELS:
|
||||||
|
return False
|
||||||
|
# Проверяем целостность растровых данных и фильтров строк:
|
||||||
|
img.load()
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
raise UnsupportedMediaTypeError("Поврежденное изображение")
|
||||||
|
|
||||||
|
try:
|
||||||
|
within_limits = await asyncio.to_thread(_check_dimensions)
|
||||||
|
if not within_limits:
|
||||||
|
raise PayloadTooLargeError(
|
||||||
|
f"Разрешение изображения превышает лимит {MAX_IMAGE_PIXELS} пикселей"
|
||||||
|
)
|
||||||
|
except (UnsupportedMediaTypeError, PayloadTooLargeError):
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
logger.warning("upload_attachment check_dimensions unexpected: %s (%s)", type(e), e)
|
||||||
|
raise UnsupportedMediaTypeError("Поврежденное изображение") from e
|
||||||
|
|
||||||
|
# 7. Генерация идентификаторов
|
||||||
|
att_id = uuid.uuid4()
|
||||||
|
s3_key = f"chats/{chat_id}/{att_id}/{filename}"
|
||||||
|
sha256_hash = hashlib.sha256(data).hexdigest()
|
||||||
|
now = dt.datetime.now(dt.timezone.utc)
|
||||||
|
|
||||||
|
# 8. Загрузка файла в S3
|
||||||
|
def _put_to_s3():
|
||||||
|
self.s3_client.put_object(
|
||||||
|
Bucket=S3_BUCKET,
|
||||||
|
Key=s3_key,
|
||||||
|
Body=data,
|
||||||
|
ContentType=ct_clean,
|
||||||
|
)
|
||||||
|
|
||||||
|
await asyncio.to_thread(_put_to_s3)
|
||||||
|
|
||||||
|
# 9. Сохранение метаданных в PostgreSQL (с компенсацией удаления из S3 при ошибке)
|
||||||
|
try:
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
await conn.execute(
|
||||||
|
"""
|
||||||
|
INSERT INTO attachments (
|
||||||
|
id, chat_id, owner_id, filename, content_type,
|
||||||
|
size_bytes, sha256, s3_key, status, error_code,
|
||||||
|
created_at, updated_at
|
||||||
|
)
|
||||||
|
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 'ready', NULL, $9, $9)
|
||||||
|
""",
|
||||||
|
att_id,
|
||||||
|
cid,
|
||||||
|
c_caller,
|
||||||
|
filename,
|
||||||
|
ct_clean,
|
||||||
|
size_bytes,
|
||||||
|
sha256_hash,
|
||||||
|
s3_key,
|
||||||
|
now,
|
||||||
|
)
|
||||||
|
except Exception as db_err:
|
||||||
|
logger.error("Ошибка записи метаданных в БД, компенсация S3: %s", db_err)
|
||||||
|
try:
|
||||||
|
await asyncio.to_thread(
|
||||||
|
self.s3_client.delete_object, Bucket=S3_BUCKET, Key=s3_key
|
||||||
|
)
|
||||||
|
except Exception as del_err:
|
||||||
|
logger.error("Ошибка удаления S3 объекта при компенсации: %s", del_err)
|
||||||
|
raise
|
||||||
|
|
||||||
|
return AttachmentResponse(
|
||||||
|
id=str(att_id),
|
||||||
|
chat_id=str(cid),
|
||||||
|
owner_id=str(c_caller),
|
||||||
|
filename=filename,
|
||||||
|
content_type=ct_clean,
|
||||||
|
size_bytes=size_bytes,
|
||||||
|
sha256=sha256_hash,
|
||||||
|
status="ready",
|
||||||
|
variants=[],
|
||||||
|
error_code=None,
|
||||||
|
created_at=_format_dt(now),
|
||||||
|
updated_at=_format_dt(now),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_attachment(
|
||||||
|
self, caller_id: str, attachment_id: str
|
||||||
|
) -> AttachmentResponse:
|
||||||
|
try:
|
||||||
|
aid = uuid.UUID(attachment_id)
|
||||||
|
c_caller = uuid.UUID(caller_id)
|
||||||
|
except ValueError:
|
||||||
|
raise NotFoundError("Вложение не найдено")
|
||||||
|
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
row = await conn.fetchrow(
|
||||||
|
"""
|
||||||
|
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type,
|
||||||
|
a.size_bytes, a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||||
|
FROM attachments a
|
||||||
|
WHERE a.id = $1
|
||||||
|
""",
|
||||||
|
aid,
|
||||||
|
)
|
||||||
|
if not row:
|
||||||
|
raise NotFoundError("Вложение не найдено")
|
||||||
|
|
||||||
|
is_member = await conn.fetchval(
|
||||||
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
|
row["chat_id"],
|
||||||
|
c_caller,
|
||||||
|
)
|
||||||
|
if not is_member:
|
||||||
|
raise NotFoundError("Вложение не найдено")
|
||||||
|
|
||||||
|
return AttachmentResponse(
|
||||||
|
id=str(row["id"]),
|
||||||
|
chat_id=str(row["chat_id"]),
|
||||||
|
owner_id=str(row["owner_id"]),
|
||||||
|
filename=row["filename"],
|
||||||
|
content_type=row["content_type"],
|
||||||
|
size_bytes=row["size_bytes"],
|
||||||
|
sha256=row["sha256"],
|
||||||
|
status=row["status"],
|
||||||
|
variants=[],
|
||||||
|
error_code=row["error_code"],
|
||||||
|
created_at=_format_dt(row["created_at"]),
|
||||||
|
updated_at=_format_dt(row["updated_at"]),
|
||||||
|
)
|
||||||
|
|
||||||
|
async def get_attachment_content(
|
||||||
|
self, caller_id: str, attachment_id: str, variant: str | None = None
|
||||||
|
) -> tuple[bytes, str]:
|
||||||
|
try:
|
||||||
|
aid = uuid.UUID(attachment_id)
|
||||||
|
c_caller = uuid.UUID(caller_id)
|
||||||
|
except ValueError:
|
||||||
|
raise NotFoundError("Вложение не найдено")
|
||||||
|
|
||||||
|
if variant not in (None, "", "original"):
|
||||||
|
raise NotFoundError("Вариант не найден")
|
||||||
|
|
||||||
|
async with self.acquire_conn() as conn:
|
||||||
|
row = await conn.fetchrow(
|
||||||
|
"""
|
||||||
|
SELECT a.chat_id, a.content_type, a.s3_key, a.status
|
||||||
|
FROM attachments a
|
||||||
|
WHERE a.id = $1
|
||||||
|
""",
|
||||||
|
aid,
|
||||||
|
)
|
||||||
|
if not row:
|
||||||
|
raise NotFoundError("Вложение не найдено")
|
||||||
|
|
||||||
|
is_member = await conn.fetchval(
|
||||||
|
"SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2",
|
||||||
|
row["chat_id"],
|
||||||
|
c_caller,
|
||||||
|
)
|
||||||
|
if not is_member:
|
||||||
|
raise NotFoundError("Вложение не найдено")
|
||||||
|
|
||||||
|
if row["status"] != "ready":
|
||||||
|
raise ConflictError("Вложение повреждено или еще не готово")
|
||||||
|
|
||||||
|
s3_key = row["s3_key"]
|
||||||
|
content_type = row["content_type"]
|
||||||
|
|
||||||
|
def _get_from_s3():
|
||||||
|
resp = self.s3_client.get_object(Bucket=S3_BUCKET, Key=s3_key)
|
||||||
|
return resp["Body"].read()
|
||||||
|
|
||||||
|
try:
|
||||||
|
data = await asyncio.to_thread(_get_from_s3)
|
||||||
|
except Exception as e:
|
||||||
|
logger.error("Ошибка чтения объекта из S3 %s: %s", s3_key, e)
|
||||||
|
raise NotFoundError("Файл не найден в хранилище") from e
|
||||||
|
|
||||||
|
return data, content_type
|
||||||
|
|
||||||
|
|
||||||
|
# Глобальный экземпляр хранилища
|
||||||
|
storage = PostgresStorage()
|
||||||
+194
@@ -0,0 +1,194 @@
|
|||||||
|
services:
|
||||||
|
load_balancer:
|
||||||
|
image: nginx:1.27-alpine
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
volumes:
|
||||||
|
- ./infra/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||||
|
depends_on:
|
||||||
|
api-1:
|
||||||
|
condition: service_healthy
|
||||||
|
api-2:
|
||||||
|
condition: service_healthy
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8080/health/live"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.25'
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
migration:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
restart: "no"
|
||||||
|
environment:
|
||||||
|
PGPASSWORD: postgres
|
||||||
|
command: >
|
||||||
|
sh -c "until pg_isready -h postgres -U postgres -d messenger; do sleep 1; done;
|
||||||
|
psql -h postgres -U postgres -d messenger -f /docker-entrypoint-initdb.d/init.sql"
|
||||||
|
volumes:
|
||||||
|
- ./infra/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.25'
|
||||||
|
memory: 128M
|
||||||
|
|
||||||
|
api-1:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
image: highload-messenger:lab4
|
||||||
|
stop_grace_period: 15s
|
||||||
|
environment:
|
||||||
|
APP_HOST: "0.0.0.0"
|
||||||
|
APP_PORT: "8080"
|
||||||
|
APP_ENV: "lab"
|
||||||
|
INSTANCE_ID: "api-1"
|
||||||
|
SESSION_TTL_SECONDS: "3600"
|
||||||
|
DATABASE_URL: "postgresql://postgres:postgres@postgres:5432/messenger"
|
||||||
|
SESSION_STORE: "redis"
|
||||||
|
CACHE_URL: "redis://redis:6379/0"
|
||||||
|
IDEMPOTENCY_TTL_SECONDS: "86400"
|
||||||
|
S3_ENDPOINT: "http://minio:9000"
|
||||||
|
S3_REGION: "us-east-1"
|
||||||
|
S3_BUCKET: "messenger-attachments"
|
||||||
|
S3_ACCESS_KEY_ID: "minioadmin"
|
||||||
|
S3_SECRET_ACCESS_KEY: "minioadmin"
|
||||||
|
MAX_UPLOAD_BYTES: "10485760"
|
||||||
|
MAX_IMAGE_PIXELS: "40000000"
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
minio:
|
||||||
|
condition: service_healthy
|
||||||
|
migration:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python3", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.50'
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
api-2:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: Dockerfile
|
||||||
|
image: highload-messenger:lab4
|
||||||
|
stop_grace_period: 15s
|
||||||
|
environment:
|
||||||
|
APP_HOST: "0.0.0.0"
|
||||||
|
APP_PORT: "8080"
|
||||||
|
APP_ENV: "lab"
|
||||||
|
INSTANCE_ID: "api-2"
|
||||||
|
SESSION_TTL_SECONDS: "3600"
|
||||||
|
DATABASE_URL: "postgresql://postgres:postgres@postgres:5432/messenger"
|
||||||
|
SESSION_STORE: "redis"
|
||||||
|
CACHE_URL: "redis://redis:6379/0"
|
||||||
|
IDEMPOTENCY_TTL_SECONDS: "86400"
|
||||||
|
S3_ENDPOINT: "http://minio:9000"
|
||||||
|
S3_REGION: "us-east-1"
|
||||||
|
S3_BUCKET: "messenger-attachments"
|
||||||
|
S3_ACCESS_KEY_ID: "minioadmin"
|
||||||
|
S3_SECRET_ACCESS_KEY: "minioadmin"
|
||||||
|
MAX_UPLOAD_BYTES: "10485760"
|
||||||
|
MAX_IMAGE_PIXELS: "40000000"
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
minio:
|
||||||
|
condition: service_healthy
|
||||||
|
migration:
|
||||||
|
condition: service_completed_successfully
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "python3", "-c", "import urllib.request; urllib.request.urlopen('http://localhost:8080/health/ready')"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.50'
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
postgres:
|
||||||
|
image: postgres:16-alpine
|
||||||
|
environment:
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: postgres
|
||||||
|
POSTGRES_DB: messenger
|
||||||
|
volumes:
|
||||||
|
- pg_data:/var/lib/postgresql/data
|
||||||
|
- ./infra/init.sql:/docker-entrypoint-initdb.d/init.sql:ro
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U postgres -d messenger"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.50'
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7-alpine
|
||||||
|
command: ["redis-server", "--appendonly", "yes"]
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.25'
|
||||||
|
memory: 256M
|
||||||
|
|
||||||
|
minio:
|
||||||
|
image: cleanstart/minio:0.20260330.001845
|
||||||
|
user: "0:0"
|
||||||
|
command: server /data --address ":9000" --console-address ":9001"
|
||||||
|
environment:
|
||||||
|
MINIO_ROOT_USER: minioadmin
|
||||||
|
MINIO_ROOT_PASSWORD: minioadmin
|
||||||
|
volumes:
|
||||||
|
- minio_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:9000/minio/health/live"]
|
||||||
|
interval: 2s
|
||||||
|
timeout: 2s
|
||||||
|
retries: 5
|
||||||
|
restart: unless-stopped
|
||||||
|
deploy:
|
||||||
|
resources:
|
||||||
|
limits:
|
||||||
|
cpus: '0.50'
|
||||||
|
memory: 512M
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
pg_data:
|
||||||
|
redis_data:
|
||||||
|
minio_data:
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
.venv/bin/python scripts/check_contract.py
|
||||||
|
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||||
|
mtusi2026lab4-api-1-1 highload-messenger:lab4 "python -m uvicorn a…" api-1 6 minutes ago Up About a minute (healthy) 8080/tcp
|
||||||
|
mtusi2026lab4-api-2-1 highload-messenger:lab4 "python -m uvicorn a…" api-2 6 minutes ago Up 2 minutes (healthy) 8080/tcp
|
||||||
|
mtusi2026lab4-load_balancer-1 nginx:1.27-alpine "/docker-entrypoint.…" load_balancer 10 minutes ago Up 2 minutes (healthy) 80/tcp, 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp
|
||||||
|
mtusi2026lab4-minio-1 cleanstart/minio:latest "/usr/bin/minio serv…" minio 10 minutes ago Up 2 minutes (healthy)
|
||||||
|
mtusi2026lab4-postgres-1 postgres:16-alpine "docker-entrypoint.s…" postgres 10 minutes ago Up 2 minutes (healthy) 5432/tcp
|
||||||
|
mtusi2026lab4-redis-1 redis:7-alpine "docker-entrypoint.s…" redis 10 minutes ago Up 2 minutes (healthy) 6379/tcp
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
|
||||||
|
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||||
|
{"observed_instances_before": 2, "successful_writes": 123, "failed_attempts": 0, "first_recovery_seconds": 0.768}
|
||||||
|
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
{
|
||||||
|
"lab": 4,
|
||||||
|
"workload": "closed-loop POST messages; no automatic retries",
|
||||||
|
"requested_duration_seconds": 30.0,
|
||||||
|
"elapsed_seconds": 30.015,
|
||||||
|
"concurrency": 4,
|
||||||
|
"successful_requests": 9286,
|
||||||
|
"failed_requests": 0,
|
||||||
|
"error_ratio": 0.0,
|
||||||
|
"successful_rps": 309.375,
|
||||||
|
"successful_latency_ms": {
|
||||||
|
"p50": 7.928,
|
||||||
|
"p95": 40.768,
|
||||||
|
"p99": 62.963
|
||||||
|
},
|
||||||
|
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080"
|
||||||
|
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||||
|
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
x-instance-id: api-2
|
||||||
|
x-instance-id: api-1
|
||||||
|
x-instance-id: api-2
|
||||||
|
x-instance-id: api-1
|
||||||
|
x-instance-id: api-2
|
||||||
|
x-instance-id: api-1
|
||||||
|
x-instance-id: api-2
|
||||||
|
x-instance-id: api-1
|
||||||
|
x-instance-id: api-2
|
||||||
|
x-instance-id: api-1
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||||
|
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
|
||||||
@@ -0,0 +1,45 @@
|
|||||||
|
============================= test session starts ==============================
|
||||||
|
platform linux -- Python 3.12.15, pytest-9.1.1, pluggy-1.7.0 -- /home/ilyas/projects/labs_highload/mtusi2026lab4/.venv/bin/python3.12
|
||||||
|
cachedir: .pytest_cache
|
||||||
|
rootdir: /home/ilyas/projects/labs_highload/mtusi2026lab4
|
||||||
|
configfile: pytest.ini
|
||||||
|
plugins: anyio-4.16.0, asyncio-1.4.0
|
||||||
|
asyncio: mode=Mode.AUTO, debug=False, asyncio_default_fixture_loop_scope=None, asyncio_default_test_loop_scope=function
|
||||||
|
collecting ... collected 34 items
|
||||||
|
|
||||||
|
tests_student/tests.py::test_invalid_json_and_extra_fields PASSED [ 2%]
|
||||||
|
tests_student/tests.py::test_outsider_chat_access_is_404 PASSED [ 5%]
|
||||||
|
tests_student/tests.py::test_empty_and_whitespace_message PASSED [ 8%]
|
||||||
|
tests_student/tests.py::test_duplicate_username_conflict PASSED [ 11%]
|
||||||
|
tests_student/tests.py::test_wrong_password_and_bad_basic_auth PASSED [ 14%]
|
||||||
|
tests_student/tests.py::test_logout_and_revocation PASSED [ 17%]
|
||||||
|
tests_student/tests.py::test_multi_page_cursor_pagination PASSED [ 20%]
|
||||||
|
tests_student/tests.py::test_concurrency_race_condition PASSED [ 23%]
|
||||||
|
tests_student/tests.py::test_password_whitespace_rejected PASSED [ 26%]
|
||||||
|
tests_student/tests.py::test_auth_sessions_with_body_rejected PASSED [ 29%]
|
||||||
|
tests_student/tests.py::test_unsupported_media_type_415 PASSED [ 32%]
|
||||||
|
tests_student/tests.py::test_nul_byte_in_strings_handled PASSED [ 35%]
|
||||||
|
tests_student/tests.py::test_basic_auth_invalid_base64_returns_401 PASSED [ 38%]
|
||||||
|
tests_student/tests.py::test_cross_chat_cursor_rejected_400 PASSED [ 41%]
|
||||||
|
tests_student/tests.py::test_utf8_password_support PASSED [ 44%]
|
||||||
|
tests_student/tests.py::test_health_readiness_and_liveness PASSED [ 47%]
|
||||||
|
tests_student/tests.py::test_instance_id_header_present PASSED [ 50%]
|
||||||
|
tests_student/tests.py::test_idempotency_key_validation PASSED [ 52%]
|
||||||
|
tests_student/tests.py::test_idempotency_replay_and_conflict PASSED [ 55%]
|
||||||
|
tests_student/tests.py::test_idempotency_acl_check_on_replay PASSED [ 58%]
|
||||||
|
tests_student/tests.py::test_invalid_cursor_date_returns_400 PASSED [ 61%]
|
||||||
|
tests_student/tests.py::test_strict_types_and_json_parsing PASSED [ 64%]
|
||||||
|
tests_student/tests.py::test_chat_creation_with_unknown_participant_is_400 PASSED [ 67%]
|
||||||
|
tests_student/tests.py::test_password_with_colons_and_utf8 PASSED [ 70%]
|
||||||
|
tests_student/tests.py::test_keyset_pagination_returns_next_cursor PASSED [ 73%]
|
||||||
|
tests_student/tests.py::test_attachment_upload_and_download_flow PASSED [ 76%]
|
||||||
|
tests_student/tests.py::test_attachment_unsupported_media_type_and_magic_bytes PASSED [ 79%]
|
||||||
|
tests_student/tests.py::test_attachment_filename_validation PASSED [ 82%]
|
||||||
|
tests_student/tests.py::test_attachment_payload_too_large_and_pixel_bomb PASSED [ 85%]
|
||||||
|
tests_student/tests.py::test_attachment_acl_and_isolation PASSED [ 88%]
|
||||||
|
tests_student/tests.py::test_message_linking_attachments PASSED [ 91%]
|
||||||
|
tests_student/tests.py::test_attachment_orphan_compensation PASSED [ 94%]
|
||||||
|
tests_student/tests.py::test_private_bucket_outside_api PASSED [ 97%]
|
||||||
|
tests_student/tests.py::test_concurrent_uploads_memory_limit PASSED [100%]
|
||||||
|
|
||||||
|
============================= 34 passed in 37.86s ==============================
|
||||||
@@ -0,0 +1,2 @@
|
|||||||
|
.venv/bin/python scripts/validate_openapi.py
|
||||||
|
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
|
||||||
@@ -0,0 +1,68 @@
|
|||||||
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
username VARCHAR(32) NOT NULL UNIQUE,
|
||||||
|
display_name VARCHAR(100) NOT NULL,
|
||||||
|
password_hash TEXT NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_username ON users (username);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS chats (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
title VARCHAR(100) NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_chats_cursor ON chats (created_at ASC, id ASC);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS chat_members (
|
||||||
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
PRIMARY KEY (chat_id, user_id)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_chat_members_user_id ON chat_members (user_id);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS messages (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||||
|
sender_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
text BYTEA NOT NULL,
|
||||||
|
attachment_ids UUID[] NOT NULL DEFAULT '{}',
|
||||||
|
created_at TIMESTAMPTZ NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
ALTER TABLE messages ADD COLUMN IF NOT EXISTS attachment_ids UUID[] NOT NULL DEFAULT '{}';
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_messages_cursor ON messages (chat_id, created_at ASC, id ASC);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS attachments (
|
||||||
|
id UUID PRIMARY KEY,
|
||||||
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||||
|
owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
filename VARCHAR(255) NOT NULL,
|
||||||
|
content_type VARCHAR(50) NOT NULL,
|
||||||
|
size_bytes BIGINT NOT NULL,
|
||||||
|
sha256 VARCHAR(64) NOT NULL,
|
||||||
|
s3_key VARCHAR(512) NOT NULL,
|
||||||
|
status VARCHAR(20) NOT NULL DEFAULT 'ready',
|
||||||
|
error_code VARCHAR(100),
|
||||||
|
created_at TIMESTAMPTZ NOT NULL,
|
||||||
|
updated_at TIMESTAMPTZ NOT NULL
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_attachments_chat_id ON attachments (chat_id, created_at ASC);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_attachments_owner_id ON attachments (owner_id);
|
||||||
|
|
||||||
|
CREATE TABLE IF NOT EXISTS idempotency_keys (
|
||||||
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||||
|
chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||||
|
idempotency_key VARCHAR(128) NOT NULL,
|
||||||
|
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||||
|
request_hash VARCHAR(64) NOT NULL,
|
||||||
|
created_at TIMESTAMPTZ NOT NULL,
|
||||||
|
PRIMARY KEY (user_id, chat_id, idempotency_key)
|
||||||
|
);
|
||||||
|
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_idempotency_created_at ON idempotency_keys (created_at);
|
||||||
@@ -0,0 +1,72 @@
|
|||||||
|
events {
|
||||||
|
worker_connections 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
http {
|
||||||
|
include /etc/nginx/mime.types;
|
||||||
|
default_type application/octet-stream;
|
||||||
|
|
||||||
|
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
|
||||||
|
'$status $body_bytes_sent "$http_referer" '
|
||||||
|
'"$http_user_agent" upstream: $upstream_addr '
|
||||||
|
'upstream_status: $upstream_status '
|
||||||
|
'rt=$request_time uct="$upstream_connect_time" uht="$upstream_header_time" urt="$upstream_response_time"';
|
||||||
|
|
||||||
|
access_log /var/log/nginx/access.log main;
|
||||||
|
error_log /var/log/nginx/error.log warn;
|
||||||
|
|
||||||
|
sendfile on;
|
||||||
|
keepalive_timeout 65;
|
||||||
|
client_max_body_size 15M;
|
||||||
|
|
||||||
|
# DNS-сервер Docker для динамического обновления IP-адресов контейнеров
|
||||||
|
resolver 127.0.0.11 valid=2s ipv6=off;
|
||||||
|
|
||||||
|
upstream api_backend {
|
||||||
|
zone api_backend 64k;
|
||||||
|
server api-1:8080 resolve max_fails=1 fail_timeout=2s;
|
||||||
|
server api-2:8080 resolve max_fails=1 fail_timeout=2s;
|
||||||
|
}
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 8080;
|
||||||
|
server_name _;
|
||||||
|
|
||||||
|
# Быстрый таймаут обнаружения недоступной реплики
|
||||||
|
proxy_connect_timeout 1s;
|
||||||
|
proxy_read_timeout 5s;
|
||||||
|
proxy_send_timeout 5s;
|
||||||
|
|
||||||
|
# Базовая политика переключения: безопасно для идемпотентных запросов
|
||||||
|
# Неидемпотентные POST не повторяем
|
||||||
|
proxy_next_upstream error timeout http_502 http_503;
|
||||||
|
proxy_next_upstream_tries 2;
|
||||||
|
proxy_next_upstream_timeout 5s;
|
||||||
|
|
||||||
|
location ~ ^/api/v1/chats/[^/]+/messages$ {
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_set_header X-Request-Id $http_x_request_id;
|
||||||
|
|
||||||
|
proxy_next_upstream error timeout http_502 http_503 non_idempotent;
|
||||||
|
proxy_next_upstream_tries 2;
|
||||||
|
proxy_next_upstream_timeout 5s;
|
||||||
|
}
|
||||||
|
|
||||||
|
location / {
|
||||||
|
proxy_pass http://api_backend;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_set_header X-Request-Id $http_x_request_id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
[pytest]
|
||||||
|
pythonpath = .
|
||||||
|
asyncio_mode = auto
|
||||||
|
python_files = test_*.py *_test.py tests.py test.py
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
fastapi==0.141.1
|
||||||
|
uvicorn[standard]==0.52.4
|
||||||
|
pydantic==2.13.5
|
||||||
|
asyncpg==0.30.0
|
||||||
|
redis==5.2.1
|
||||||
|
argon2-cffi==23.1.0
|
||||||
|
pytest==9.1.1
|
||||||
|
pytest-asyncio==1.4.0
|
||||||
|
httpx==0.28.1
|
||||||
|
boto3==1.43.123
|
||||||
|
pillow==13.0.0
|
||||||
Executable
+8
@@ -0,0 +1,8 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||||
|
|
||||||
|
# Перезапуск всех сервисов API и хранилищ с сохранением volumes
|
||||||
|
docker compose -f "$PROJECT_DIR/compose.yaml" restart
|
||||||
Executable
+13
@@ -0,0 +1,13 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
set -eu
|
||||||
|
|
||||||
|
: "${TARGET_INSTANCE_ID:?test provides target API instance alias}"
|
||||||
|
|
||||||
|
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||||||
|
PROJECT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
|
||||||
|
|
||||||
|
if docker compose -f "$PROJECT_DIR/compose.yaml" stop "$TARGET_INSTANCE_ID" 2>/dev/null; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
docker stop "$TARGET_INSTANCE_ID" >/dev/null
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
import os
|
||||||
|
import pytest
|
||||||
|
from httpx import ASGITransport, AsyncClient
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
async def client():
|
||||||
|
base_url = os.getenv("BASE_URL", "http://localhost:8080")
|
||||||
|
if base_url.startswith("http://testserver"):
|
||||||
|
from app.main import app
|
||||||
|
|
||||||
|
transport = ASGITransport(app=app)
|
||||||
|
async with AsyncClient(transport=transport, base_url="http://testserver") as ac:
|
||||||
|
yield ac
|
||||||
|
else:
|
||||||
|
async with AsyncClient(base_url=base_url) as ac:
|
||||||
|
yield ac
|
||||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user