diff --git a/.env.example b/.env.example index d99d26a2..6448c547 100644 --- a/.env.example +++ b/.env.example @@ -18,3 +18,10 @@ S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 +MINIO_ROOT_USER=... +MINIO_CPUS=0.5 +MINIO_MEMORY=512M +# --- Reconciler --- +RECONCILER_ENABLED=true +RECONCILER_INTERVAL_SECONDS=300 +RECONCILER_GRACE_SECONDS=3600 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..4adaa807 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,16 @@ +# Контейнеры Linux; shell-скрипты, Dockerfile, конфиги nginx/yaml +# должны иметь LF независимо от core.autocrlf на стороне разработчика. +*.sh text eol=lf +*.bash text eol=lf +Dockerfile text eol=lf +Dockerfile.* text eol=lf +*.yml text eol=lf +*.yaml text eol=lf +*.go text eol=lf +*.sql text eol=lf +*.md text eol=lf +*.conf text eol=lf + +# Windows-специфичные - CRLF +*.bat text eol=crlf +*.cmd text eol=crlf \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..16229253 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,29 @@ +FROM golang:1.26.1-alpine AS builder + +WORKDIR /src + +COPY backend/go.mod backend/go.sum ./ +RUN go mod download + +COPY backend/ . + +RUN CGO_ENABLED=0 GOOS=linux go build \ + -trimpath \ + -ldflags="-s -w" \ + -o /out/server \ + ./cmd/server + +FROM alpine:3.20 + +RUN adduser -D -u 1000 app + +COPY --from=builder /out/server /server + +EXPOSE 8080 + +USER app + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD wget -q -O - http://127.0.0.1:8080/health/live >/dev/null || exit 1 + +ENTRYPOINT ["/server"] \ No newline at end of file diff --git a/REPORT.md b/REPORT.md index 9ad2b84f..5eb82452 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,444 @@ -# Отчёт — лабораторная 4 +# Отчёт - лабораторная 4 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Голиков Антон, БВТ2303 +- Целевая оценка: на 4 (уровни 3+4, без трека 5) +- Commit/PR: ветка `solution/BVT2303-Golikov`, PR будет открыт после публикации +- Использованные источники, библиотеки и помощь ИИ: + - Скелет лабы 4 и материалы курса: COURSE.md, contracts/openapi.json, + contracts/README.md, contracts/infrastructure.json + - Документация MinIO (mc admin user/policy, bucket policy), S3 API + (PutObject/GetObject/ListObjects), pgx v5, goose, gin + - Собственная реализация лабы 3: Go 1.26, gin, pgx, goose, Redis, + nginx, две API-реплики, X-Instance-Id, Idempotency-Key + - ИИ (ChatGPT) использован для структуры отчёта, вычитки формулировок + и подсказок по рутинным местам (политика MinIO, healthcheck). + Весь код, конфигурация, эксперименты и финальные тексты проверены + и объясняются автором на защите. ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Prerequisites: + +- Docker Desktop 4.30+ (WSL2 backend) или Docker Engine с Compose v2.20+ +- Go 1.26+ (для локальной сборки без Docker; в Docker-варианте не нужен) +- Python 3.9+ (для публичных тестов из scaffold) +- git, make + +Шаги запуска: + +1. Клонировать fork и перейти на ветку solution: + + git clone https://gitea.privcloud.tech/Maximillan_Mercer/mtusi2026lab4-fork.git + cd mtusi2026lab4 + git checkout solution/BVT2303-Golikov + +2. Создать локальные secrets. Значения генерируются оператором, в Git + не попадают. Добавились четыре секрета для S3 (в дополнение к трём + из lab2-3): + + mkdir -p secrets + + # PostgreSQL (как в lab2-3) + PW="$(openssl rand -hex 16)" + printf "%s" "$PW" > secrets/postgres_password + printf "%s" "postgres://messenger:$PW@postgres:5432/messenger?sslmode=disable" \ + > secrets/database_url + printf "%s" "redis://redis:6379/0" > secrets/cache_url + + # MinIO root (только для mc-init, API их не видит) + printf "%s" "messenger-minio-root" > secrets/minio_root_user + openssl rand -base64 32 | tr -d '\r\n' > secrets/minio_root_password + + # S3 API-креды (для сервиса api) + printf "%s" "messenger-api" > secrets/s3_access_key_id + openssl rand -hex 32 > secrets/s3_secret_access_key + + chmod 600 secrets/* + +3. Создать .env из .env.example: + + cp .env.example .env + + Переменные и дефолты, которые изменились относительно lab3: + + - S3_ENDPOINT=http://minio:9000 + - S3_BUCKET=messenger-attachments + - S3_REGION=us-east-1 + - S3_USE_SSL=false + - S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id + - S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key + - MAX_UPLOAD_BYTES=10485760 (10 MiB) + - MAX_IMAGE_PIXELS=40000000 (40 млн) + - RECONCILER_ENABLED=true + - RECONCILER_INTERVAL_SECONDS=300 + - RECONCILER_GRACE_SECONDS=3600 (1 час) + - MINIO_ROOT_USER=messenger-minio-root + - MINIO_CPUS=0.5, MINIO_MEMORY=512M + + Остальные переменные (SESSION_STORE=redis, SESSION_TTL_SECONDS, + IDEMPOTENCY_TTL_SECONDS, SHUTDOWN_DRAIN_SECONDS, лимиты CPU/RAM, + API_REPLICAS=2, LB_PORT=8080) без изменений от lab3. + +4. Собрать образ API и запустить стек: + + docker compose build api + docker compose up -d + +5. Дождаться, пока mc-init создаст bucket и API-пользователя, + и применились миграции 0001–0004: + + docker compose logs migrate + # ожидать: goose: successfully migrated database to version: 4 + # при первом запуске: OK 0001..0004 + docker compose logs mc-init + # ожидать: minio bootstrap ok: bucket=messenger-attachments user=messenger-api + + curl -sS http://localhost:8080/health/ready + # ожидать: {"status":"ok"} + + Сервис mc-init идемпотентен: повторный `docker compose run --rm mc-init` + не падает и не дублирует bucket или policy. API использует отдельные + S3-креды (messenger-api, policy только на messenger-attachments/*); + root-креды MinIO видят только minio и mc-init. + +6. Прогнать публичный smoke и проверки от scaffold: + + PYTHONUTF8=1 make check + PYTHONUTF8=1 make test + +7. Проверки сохранности и отказа (требуют scripts/restart.sh и + scripts/stop-one.sh из lab3, они перенесены): + + PYTHONUTF8=1 make test-persistence ACTION_SCRIPT=scripts/restart.sh + PYTHONUTF8=1 make test-failover ACTION_SCRIPT=scripts/stop-one.sh + + +Версии образов (закреплены в compose.yaml): + +- pgsty/silo:RELEASE.2026-09-16T00-00-00Z — drop-in MinIO fork. + Оригинальный minio/minio заархивирован в 2025-м, официальные + community-образы сняты с Docker Hub; pgsty/silo сохраняет S3 API, + mc и healthcheck без изменений. +- postgres:16.4-alpine +- redis:7.4-alpine +- nginx:1.27.4-alpine +- mtusi-messenger-api:lab3 — собственный образ, собирается из Dockerfile + +Безопасная остановка: + + docker compose stop # остановить контейнеры, volumes сохранить + docker compose down # удалить контейнеры и сеть, volumes сохранить + +Остановка с удалением учебных данных (необратимо, удалит все volumes): + + docker compose down -v + +Отдельно про .env и secrets: оба пути в .gitignore, в репозиторий не +попадают. Значения паролей синтетические, для локального стенда. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +Схема: + + client (curl / python / smoke) + | + v + nginx LB (:8080) единственная публичная точка + | + +-> api-1 :8080 реплика 1 + +-> api-2 :8080 реплика 2 + | + +-> postgres :5432 users, chats, chat_members, messages, + | sessions, idempotency_keys, attachments, + | message_attachments + +-> redis :6379 сессии (SESSION_STORE=redis) + +-> minio :9000 S3, приватный bucket messenger-attachments + (порты наружу не публикуются) + | + +-- mc-init (одноразовый) bootstrap: bucket + policy + API-user + +-- migrate (одноразовый) goose up + +Источник истины - PostgreSQL для метаданных и S3 для бинарных байтов. +Разделение намеренное: байты не хранятся в БД (не BLOB, не локальный +каталог api), только в приватном bucket; в Postgres - object_key, +sha256, content_type, size_bytes, status, привязка к chat_id и к +message_attachments. Через S3 любая API-реплика читает любую вложенность +одинаково; локальный volume API не годится, потому что файл лежит только +на одной реплике и теряется при её остановке - это критерий README +уровня 3 "Upload → остановка обслужившей API → download через другую +возвращает тот же файл". + +Сессии - в Redis (без изменений от lab2-3). Без sticky session на LB +любая реплика видит любую сессию. + +Транзакционные границы: + +- Создание чата - одна транзакция в Postgres (chats + chat_members). +- Идемпотентная отправка сообщения - одна транзакция: слот в + idempotency_keys, проверка и линковка attachments, INSERT в messages, + INSERT в message_attachments. Либо всё, либо ничего. +- Загрузка вложения - две точки сохранения без общей транзакции (S3 + и Postgres - разные системы). Последовательность: + + INSERT attachments (status='pending', object_key, sha256) → COMMIT + PutObject в S3 → OK/FAIL + UPDATE attachments status='ready' → COMMIT + + Это ключевая граница lab4. Ниже - как каждый отказ на этой границе + компенсируется. + +- Регистрация - один INSERT с UNIQUE-индексом на username (23505 → + 409 через mapError). + +Компенсация и сверка (уровень 4): + +- Отказ между INSERT и PutObject. Строка pending осталась, объекта в + S3 нет. Handler ловит ошибку PutObject и сразу вызывает + MarkFailed(id, "upload_failed") - компенсация в том же запросе, без + ожидания reconciler. + +- Отказ между PutObject и UPDATE status='ready'. Объект в S3 есть, + строка pending. Это и есть "вечный неучтённый объект", если ничего + не делать. Закрывает reconciler: ListStalePending(now - grace) → + Delete(object_key) → DeletePending(id). Grace 1 час: свежие pending + (реальные upload'ы) не трогаются, только застрявшие. + +- Обратный случай: PutObject прошёл, INSERT в Postgres упал. Объект + в S3 есть, строки в БД нет - orphan. Закрывает reconciler: + List(S3) → отфильтровать LastModified < now - grace → ExistingKeys(DB) + → Delete тех, которых нет в БД. Grace 1 час защищает свежий Put, + чей INSERT ещё не закоммичен. + +- Reconciler запускается во всех репликах, но использует + pg_try_advisory_lock: одна реплика на tick берёт session-level + advisory lock, остальные пропускают tick. Postgres автоматически + освобождает лок, если процесс упал. + +Отдельные S3-креды и приватность bucket: + +- Root-креды MinIO (minio_root_user/minio_root_password) видят только + minio и mc-init. API-контейнер их не получает. +- API использует messenger-api с policy только на messenger-attachments + (GetObject, PutObject, DeleteObject на bucket/*, ListBucket на bucket). + Создание/удаление bucket, policy, пользователей недоступны. +- Bucket приватный: без подписи GET возвращает 403. Проверяется в + evidence/s3-private.txt. +- MINIO_BROWSER=off: административная консоль не слушается. Порты + 9000/9001 не публикуются на хост (только внутри internal-сети). + +Что перенесено из lab3 без изменений: + +- Схема БД: users, chats, chat_members, messages, sessions +- idempotency_keys с TTL 24 часа, Idempotency-Key обязателен +- X-Instance-Id в каждом ответе API (os.Hostname()) +- Миграции goose, отдельный процесс /server migrate +- Graceful drain на SIGTERM, atomic ready-флаг, SHUTDOWN_DRAIN_SECONDS +- CPU/RAM лимиты на api, postgres, redis через deploy.resources +- nginx LB перед двумя репликами, upstream keepalive +- Горизонтальное масштабирование: две реплики, общие сессии в Redis + +Что добавлено/изменено в lab4: + +1. Отдельный сервис minio (pgsty/silo), persistent volume minio_data. +2. Одноразовый сервис mc-init: создаёт bucket, policy, API-user. + Идемпотентен. +3. Миграция 0003: таблицы attachments и message_attachments, индексы, + ограничения (status, size, sha256, content_type, filename). +4. Миграция 0004: ослаблен CHECK messages_text_not_blank - пустой + текст разрешён при наличии вложений (lab4 anyOf), но + whitespace-only по-прежнему запрещён. +5. Три endpoint: POST /chats/{chat_id}/attachments (raw bytes PNG/JPEG, + max 10 MiB, max 40M пикселей), GET /attachments/{id} и + GET /attachments/{id}/content (без redirect, только участникам чата). +6. POST /messages принимает attachment_ids (0-10), все вложения должны + принадлежать этому чату и быть ready; anyOf text или attachments. +7. RequestHash расширен: sha256(text) без attachments (обратная + совместимость с lab3), sha256(text + 0x00 + ids) с attachments. +8. Reconciler в API, pg advisory lock, две задачи (stale pending, + orphan objects), grace 1 час. +9. Healthcheck /health/ready пингует S3 (BucketExists). +10. nginx client_max_body_size=12m (10 MiB upload + запас на 11 MiB + negative-тест, чтобы 413 шёл от API с X-Request-Id, а не от nginx). + +Соответствие переменных .env.example реальной конфигурации (новое +в lab4): + +- S3_ENDPOINT, S3_BUCKET, S3_REGION, S3_USE_SSL - читаются в + config.Load, идут в s3.Config. Для внутреннего MinIO + http://minio:9000, SSL=false. +- S3_ACCESS_KEY_ID_FILE, S3_SECRET_ACCESS_KEY_FILE - secrets в + /run/secrets, читаются через readSecretOptional. +- MAX_UPLOAD_BYTES - идёт в AttachmentService (io.LimitReader body = + maxBytes+1, 413 при превышении). +- MAX_IMAGE_PIXELS - идёт в AttachmentService (image.DecodeConfig, + 413 при W*H > maxPixels). +- RECONCILER_ENABLED, RECONCILER_INTERVAL_SECONDS, + RECONCILER_GRACE_SECONDS - читаются в config.Load, идут в + service.Reconciler. +- MINIO_ROOT_USER - только для minio и mc-init, не попадает в api. ## Доказательства критериев -| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | -| --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | +Все evidence-файлы лежат в `evidence/`, содержат только обезличенные +результаты и не включают токены, пароли, signed URL или приватные +ключи. -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +Уровень 3: + +| Критерий | Команда | Наблюдаемый результат | Файл | +| --- | --- | --- | --- | +| Публичный smoke | `PYTHONUTF8=1 make test` | PASS: lab4 public API scenario | evidence/smoke.txt | +| >= 2 API-реплики за LB | `docker compose ps` | api-1 и api-2 Up (healthy), LB публикует :8080 | evidence/container-state.txt | +| Остановка одной реплики <= 10 с | `make test-failover` | first_recovery_seconds 5.48 | evidence/failover-1.txt | +| Поочерёдная остановка каждой (автотест) | `make test-failover` дважды | recovery 5.48 и 5.34, дублей нет | evidence/failover-1.txt, failover-2.txt | +| Поочерёдная остановка каждой (ручной) | `docker stop api-1`, потом `api-2` | после остановки каждой отвечает ровно один X-Instance-Id | evidence/failover-manual.txt | +| Идемпотентность POST | smoke + failover (replay с тем же ключом) | тот же ответ, дублей нет | evidence/smoke.txt, failover-*.txt | +| Persistence после restart | `make test-persistence` | PASS, StartedAt всех контейнеров обновился | evidence/persistence.txt | +| S3-сервер в общем стенде, приватный bucket | `docker compose ps`, `mc ls local` | minio Up (healthy), bucket messenger-attachments (0 B) | evidence/container-state.txt | +| Метаданные в Postgres, байты в S3 | `psql \d attachments`, `mc ls` | таблица attachments; object_key в S3 | evidence/s3-private.txt, container-state.txt | +| POST вложения возвращает 201 ready | smoke, test_attachments | 201 ready, sha256/size совпадают | evidence/attachments-negative.txt | +| Download побайтно, sha256 совпадает | smoke | download.bytes == original | evidence/smoke.txt | +| Чужой участник получает 404 на метаданные и байты | test_attachments | 404 без 403 | evidence/attachments-negative.txt | +| Прямой GET S3 без подписи -> 403 | `wget http://minio:9000/messenger-attachments/` | HTTP/1.1 403 Forbidden | evidence/s3-private.txt | +| Upload -> остановка обслужившей API -> download через другую | failover (файл приложен до отказа) | download через другую реплику == оригинал | evidence/failover-1.txt, failover-2.txt | +| Restart с сохранением volumes сохраняет оригинал | persistence | PASS, download == исходный файл | evidence/persistence.txt | +| S3 недоступен -> 503 без утечек | `docker compose stop minio` | /health/ready 503 unavailable; LB снимает реплики | evidence/s3-down.txt | +| Не более 10 MiB, не более 40M пикселей | test_attachments | 11 MiB -> 413 payload_too_large | evidence/attachments-negative.txt | +| MIME по содержимому | test_attachments | PNG-байты с Content-Type image/jpeg -> 201, content_type image/png | evidence/attachments-negative.txt | +| Опасные filename отклоняются | test_attachments | /, \\, NUL -> 400 invalid_request | evidence/attachments-negative.txt | + +Уровень 4: + +| Критерий | Команда | Наблюдаемый результат | Файл | +| --- | --- | --- | --- | +| Тест слишком большого файла | test_attachments (11 MiB) | 413 payload_too_large, не 500 | evidence/attachments-negative.txt | +| Тест повреждённого изображения | test_attachments (signature без IHDR) | 415 unsupported_media_type | evidence/attachments-negative.txt | +| Тест подмены MIME | test_attachments (PNG как image/jpeg) | 201, content_type определён по magic bytes | evidence/attachments-negative.txt | +| Тест чужого attachment_id | test_attachments (eve -> metadata и bytes) | 404 без утечки существования | evidence/attachments-negative.txt | +| Тест attachment_id из другого чата | test_attachments (message с чужим id) | 404 not_found | evidence/attachments-negative.txt | +| Пользовательская ошибка не превращается в 500 | test_attachments | все отказы 4xx; ни одна не 500 | evidence/attachments-negative.txt | +| Отказ S3/БД не оставляет вечных неучтённых объектов | reconciler с ускоренными интервалами | stale pending и orphan-объект удалены; pending_left=0 | evidence/reconciler.txt | +| Документированный grace period | README/REPORT, плюс evidence | ListStalePending(now - 1h), orphan только старше 1h | этот отчёт, evidence/reconciler.txt | +| Лимиты до неограниченного чтения | код + memory test | io.LimitReader(maxBytes+1), image.DecodeConfig до декодирования | student-tests/test_memory.py | +| >=10 одновременных загрузок с измерением пика RAM | test_memory.py + cgroups v2 | пик 141 MiB на каждой реплике <= 256 MiB лимита | evidence/memory-10x10mb.txt | +| Bucket bootstrap идемпотентен | `docker compose run --rm mc-init` дважды | exit 0, "no such bucket / user exists" не срабатывает | этот отчёт | +| API использует отдельные S3-креды с ограниченными правами | `mc admin user list`, policy | только messenger-api, GetObject/PutObject/DeleteObject на bucket/* | evidence/s3-private.txt | +| Административная консоль не открыта в общей сети | `docker compose port minio 9001`, `printenv MINIO_BROWSER` | порт не проброшен, браузер off | evidence/s3-private.txt | +| Проверка private bucket вне API | `wget http://minio:9000/messenger-attachments/` | 403 Forbidden без подписи | evidence/s3-private.txt | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Общая среда: Windows 10, Docker Desktop (WSL2 backend), одна физическая +машина, на которой работают и весь стенд, и клиенты тестов. +Все пользователи и изображения синтетические. Версии: PostgreSQL 16.4, +Redis 7.4, nginx 1.27.4, pgsty/silo RELEASE.2026-09-16T00-00-00Z +(MinIO fork), Go 1.26. -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | +| Воздействие | Что ожидаем | Что наблюдали | Время восстановления, потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| Остановка одной API-реплики | writes продолжаются через LB, без дублей | recovery 5.48 / 5.34 с, повтор с тем же ключом даёт то же сообщение | 5–6 с, потерь нет | +| Поочерёдная остановка api-1 и api-2 вручную | каждый раз обслуживает оставшаяся | после stop api-1 отвечает один X-Instance-Id, после stop api-2 другой | <20 с суммарно, потерь нет | +| Перезапуск всего стека (`scripts/restart.sh`) | данные, сессии и файлы сохранены | StartedAt всех контейнеров обновился, PASS, прежний токен работает | в пределах бюджета теста, потерь нет | +| Остановка minio | /health/ready 503, LB снимает реплики; после start всё возвращается | ровно так; после start оба X-Instance-Id вернулись | ~15–20 с, потерь нет | +| Stale pending (2 минуты) при grace 30 с (для теста) | reconciler удаляет строку и объект | "reconciler: sweep done pending=1 orphans=0", pending_left=0 | на следующем tick (≤10 с в тестовом прогоне) | +| Orphan-объект в S3 старше grace | reconciler удаляет объект | лог orphans=N, объект удалён | аналогично | +| 10 параллельных upload по 10 MiB | RAM каждой реплики в пределах 256 MiB | 10 upload за 1.79 с, peak 138 / 141.5 MiB | пик не превышен, OOM нет | + +Пояснения: + +- Ошибки в окне переключения nginx при остановке API-реплики - нормальны + и разрешены README уровня 3 ("краткие ошибки во время обнаружения + отказа измеряются и допустимы"). После 5 секунд writes продолжаются. +- Reconciler в тесте запускался с `RECONCILER_INTERVAL_SECONDS=10` и + `RECONCILER_GRACE_SECONDS=30`, чтобы уложиться в минуту. В дефолтной + конфигурации интервал 5 минут, grace 1 час - для реальной работы + это безопаснее, потому что не трогает свежие upload'ы и только что + загруженные объекты, чей INSERT ещё в полёте. +- Пик RAM 141 MiB на реплике - не от upload'ов, а от Argon2id при + регистрации пользователей (6 операций × 64 MiB malloc при bootstrap). + Upload'ы добавили только +1..4 MiB к peak, потому что Go heap уже + раздут GC и `io.LimitReader` ограничивает каждый upload 10 MiB. + Худший сценарий 141 + 100 = 240 MiB < 256 MiB - укладывается с + запасом. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Что выдерживает: + +- Отказ одной API-реплики при живом S3 и Postgres. +- Перезапуск API и всех хранилищ (Postgres, Redis, MinIO) с сохранением + volumes - данные, сессии и файлы. +- Отказ S3: /health/ready 503 без утечек, LB снимает реплики, после + восстановления всё возвращается. +- Отказ между INSERT и PutObject: компенсация MarkFailed в том же + запросе, без ожидания reconciler. +- Отказ между PutObject и UPDATE ready: reconciler через grace period + (1 час) удаляет застрявшие pending и объект. +- Orphan-объекты (Put прошёл, INSERT упал): reconciler через grace + period удаляет объекты в S3, которых нет в БД. +- 10 параллельных upload по 10 MiB - peak RAM каждой реплики + укладывается в лимит 256 MiB. + +Что не выдерживает: + +- Отказ узла. Всё работает на одном хосте, потеря хоста = потеря + системы. PostgreSQL и MinIO без репликации, nginx LB без резерва. +- Отказ MinIO на длительное время (например, потеря volume): файлы + не восстановятся, потому что второй копии нет. Метаданные в + Postgres остаются, скачивание отдаёт 404/503. +- Grace period reconciler (1 час) защищает от случайного удаления + живых загрузок, но не является транзакционной гарантией. В + редком случае ровно на границе grace и очень медленной загрузки + возможен false-positive, теоретически удаление свежего объекта. + Вероятность пренебрежимо мала для учебного стенда, но в реальной + работе grace стоит увеличивать до нескольких часов и добавлять + heartbeat-обновление updated_at у активных загрузок. +- Медленная сеть к S3: reconciler обходит ListObjects и Delete + по одному, что на больших бакетах станет дорого. На учебном + бакете это не проявляется. + +Не реализовано в этой работе: + +- Трек 5 (presigned upload или повторяемый reconciler с dry-run и + инъекцией ошибок) - целевая оценка 4, трек 5 не заявлен. +- Background worker и thumbnail (это уже lab5). +- Argon2id вместо текущего KDF в lab7, TLS/mTLS, OpenBao. +- HTTPS. По контракту до lab7 достаточно HTTP для локального стенда. +- Rate limiting: схема допускает `429`, реализация не требуется. + +Учебные упрощения: + +- MinIO fork pgsty/silo: `minio/minio` заархивирован, официальных + community-образов с 2025 нет. pgsty/silo - drop-in замена с тем же + S3 API и mc; версия образа закреплена. +- Роль reconciler в одной реплике через pg advisory lock. Не является + распределённой HA-сверкой; при потере хоста обе реплики и + reconciler исчезают. +- БД и S3 в одном экземпляре, оба на том же хосте, что и API. Для + уровня 3-4 достаточно, для продакшена нужны репликация БД, + версионирование S3, отдельный хост под reconciler/worker. + +Подтверждённые улучшения (сводка по lab4): + +- Метаданные и бинарные данные разнесены: Postgres - источник истины + для структуры, S3 - источник истины для байтов. Файл в локальном + volume API не равен объектному хранилищу, потому что при остановке + реплики файл недоступен через другую; S3 решает это. +- Grace period reconciler (1 час) не трогает живые загрузки и свежие + Put-объекты: stale pending и orphans обрабатываются только после + истечения срока. +- Отдельные S3-креды с ограниченной policy плюс приватный bucket + плюс MINIO_BROWSER=off: административный доступ к S3 полностью + изолирован от API. Проверяется в evidence/s3-private.txt. +- nginx client_max_body_size=12m: иначе nginx режет на 1 MiB по + умолчанию, 413 летит без X-Request-Id (это ответ LB, а не API), + и публичный тест падает "missing X-Request-Id". +- Миграция 0004 ослабляет CHECK messages_text_not_blank до + `text !~ '^[[:space:]]+$'`: `text=""` с attachments валиден по + lab4 anyOf, а whitespace-only по-прежнему запрещён на уровне БД. + +Следующий шаг для полной картины: увеличить grace period reconciler +до нескольких часов и добавить heartbeat от активных загрузок +(обновление updated_at), чтобы устранить теоретический false-positive +на границе grace. Для production-варианта - вынести reconciler в +отдельный процесс/реплику и добавить репликацию БД и S3. \ No newline at end of file diff --git a/backend/cmd/server/main.go b/backend/cmd/server/main.go new file mode 100644 index 00000000..c0c74af0 --- /dev/null +++ b/backend/cmd/server/main.go @@ -0,0 +1,205 @@ +package main + +import ( + "context" + "errors" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "strings" + "sync/atomic" + "syscall" + "time" + + "high-loud-apps-labs/internal/config" + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi" + "high-loud-apps-labs/internal/httpapi/handler" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" + "high-loud-apps-labs/internal/storage/postgres" + redisstore "high-loud-apps-labs/internal/storage/redis" + s3store "high-loud-apps-labs/internal/storage/s3" +) + +func main() { + if len(os.Args) > 1 && os.Args[1] == "migrate" { + if err := runMigrate(); err != nil { + log.Fatalf("миграции: %v", err) + } + return + } + + cfg, err := config.Load() + if err != nil { + log.Fatalf("конфигурация: %v", err) + } + + addr := cfg.Host + ":" + cfg.Port + + ctx := context.Background() + pool, err := postgres.Connect(ctx, postgres.Config{ + URL: cfg.DatabaseURL, + MaxConns: 10, + MinConns: 1, + MaxConnLifetime: time.Hour, + MaxConnIdleTime: 30 * time.Minute, + ConnectTimeout: 5 * time.Second, + }) + if err != nil { + log.Fatalf("БД: %v", err) + } + defer pool.Close() + + store := postgres.New(pool) + + sessionRepo, cachePing, closeSession := newSessionRepo(cfg, store) + defer closeSession() + + hasher := service.NewArgon2idHasher() + + userSvc := service.NewUserService(store.Users(), hasher) + sessionSvc := service.NewSessionService(store.Users(), sessionRepo, hasher, cfg.SessionTTL) + chatSvc := service.NewChatService(store.Chats(), store.Users()) + messageSvc := service.NewMessageService(store.Messages(), store.Chats(), cfg.IdempotencyTTL) + + if !cfg.S3Configured() { + log.Fatal("S3 не настроен: задайте S3_ENDPOINT, S3_BUCKET, S3_ACCESS_KEY_ID_FILE/S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY_FILE/S3_SECRET_ACCESS_KEY") + } + + objectStorage, err := s3store.New(ctx, s3store.Config{ + Endpoint: cfg.S3Endpoint, + Bucket: cfg.S3Bucket, + AccessKey: cfg.S3AccessKey, + SecretKey: cfg.S3SecretKey, + UseSSL: cfg.S3UseSSL, + Region: cfg.S3Region, + }) + if err != nil { + var unavailable *domain.UnavailableError + if errors.As(err, &unavailable) { + log.Fatalf("s3: %v (reason: %s)", err, unavailable.Reason) + } + log.Fatalf("s3: %v", err) + } + + attachmentSvc := service.NewAttachmentService( + store.Attachments(), + store.Chats(), + objectStorage, + cfg.MaxUploadBytes, + cfg.MaxImagePixels, + ) + + healthChecks := []handler.HealthCheck{ + func(ctx context.Context) error { return pool.Ping(ctx) }, + func(ctx context.Context) error { return objectStorage.Ping(ctx) }, + } + if cachePing != nil { + healthChecks = append(healthChecks, cachePing) + } + + var ready atomic.Bool + ready.Store(true) + + router := httpapi.NewRouter( + httpapi.Handlers{ + Health: handler.NewHealthHandler(healthChecks...).WithReadyFlag(&ready), + User: handler.NewUserHandler(userSvc), + Chat: handler.NewChatHandler(chatSvc), + Message: handler.NewMessageHandler(messageSvc), + Auth: handler.NewAuthHandler(sessionSvc), + Attachment: handler.NewAttachmentHandler(attachmentSvc), + }, + middleware.BearerAuth(sessionSvc), + ) + + srv := &http.Server{Addr: addr, Handler: router} + + reconcilerCtx, reconcilerCancel := context.WithCancel(context.Background()) + defer reconcilerCancel() + if cfg.ReconcilerEnabled { + reconciler := service.NewReconciler( + store.Attachments(), + objectStorage, + pool, + cfg.ReconcilerGracePeriod, + ) + go reconciler.Run(reconcilerCtx, cfg.ReconcilerInterval) + } + + go func() { + log.Printf("Запускаем на %s (SESSION_STORE=%s, RECONCILER_ENABLED=%v)", addr, cfg.SessionStore, cfg.ReconcilerEnabled) + if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("Ошибка запуска: %v", err) + } + }() + + quit := make(chan os.Signal, 1) + signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) + <-quit + + log.Printf("Получен сигнал завершения; readiness выключен, drain %s", cfg.ShutdownDrain) + ready.Store(false) + reconcilerCancel() + + if cfg.ShutdownDrain > 0 { + time.Sleep(cfg.ShutdownDrain) + } + + log.Println("Drain завершён, останавливаем HTTP-сервер...") + shutdownCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + if err := srv.Shutdown(shutdownCtx); err != nil { + log.Fatalf("Ошибка при остановке: %v", err) + } + log.Println("Сервер остановлен") +} + +func runMigrate() error { + cfg, err := config.Load() + if err != nil { + return fmt.Errorf("конфигурация: %w", err) + } + + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + + pool, err := postgres.Connect(ctx, postgres.Config{ + URL: cfg.DatabaseURL, + ConnectTimeout: 5 * time.Second, + }) + if err != nil { + return fmt.Errorf("БД: %w", err) + } + defer pool.Close() + + if err := postgres.RunMigrations(ctx, pool); err != nil { + return fmt.Errorf("применение миграций: %w", err) + } + return nil +} + +func newSessionRepo(cfg *config.Config, store *postgres.Storage) (domain.SessionRepository, handler.HealthCheck, func()) { + switch strings.ToLower(cfg.SessionStore) { + case "", "postgres": + return store.Sessions(), nil, func() {} + case "redis": + if cfg.CacheURL == "" { + log.Fatalf("SESSION_STORE=%s требует CACHE_URL_FILE или CACHE_URL", cfg.SessionStore) + } + client, err := redisstore.NewClient(cfg.CacheURL) + if err != nil { + log.Fatalf("redis: %v", err) + } + ping := func(ctx context.Context) error { + return client.Ping(ctx).Err() + } + return redisstore.NewSessionRepo(client), ping, func() { _ = client.Close() } + default: + log.Fatalf("SESSION_STORE=%q не поддерживается", cfg.SessionStore) + return nil, nil, nil + } +} diff --git a/backend/go.mod b/backend/go.mod new file mode 100644 index 00000000..f4390989 --- /dev/null +++ b/backend/go.mod @@ -0,0 +1,65 @@ +module high-loud-apps-labs + +go 1.26.1 + +require ( + github.com/gin-gonic/gin v1.12.0 + github.com/google/uuid v1.6.0 + github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6 + github.com/jackc/pgx/v5 v5.7.1 + github.com/minio/minio-go/v7 v7.4.1 + github.com/pressly/goose/v3 v3.22.1 + github.com/redis/go-redis/v9 v9.22.0 + golang.org/x/crypto v0.57.0 +) + +require ( + github.com/bytedance/gopkg v0.1.3 // indirect + github.com/bytedance/sonic v1.15.0 // indirect + github.com/bytedance/sonic/loader v0.5.0 // indirect + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/cloudwego/base64x v0.1.6 // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/gabriel-vasile/mimetype v1.4.12 // indirect + github.com/gin-contrib/sse v1.1.0 // indirect + github.com/go-playground/locales v0.14.1 // indirect + github.com/go-playground/universal-translator v0.18.1 // indirect + github.com/go-playground/validator/v10 v10.30.1 // indirect + github.com/goccy/go-json v0.10.5 // indirect + github.com/goccy/go-yaml v1.19.2 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.19.2 // indirect + github.com/klauspost/cpuid/v2 v2.4.0 // indirect + github.com/klauspost/crc32 v1.3.0 // indirect + github.com/leodido/go-urn v1.4.0 // indirect + github.com/mattn/go-isatty v0.0.22 // indirect + github.com/mfridman/interpolate v0.0.2 // indirect + github.com/minio/crc64nvme v1.1.1 // indirect + github.com/minio/md5-simd v1.1.2 // indirect + github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/pelletier/go-toml/v2 v2.4.3 // indirect + github.com/philhofer/fwd v1.2.0 // indirect + github.com/quic-go/qpack v0.6.0 // indirect + github.com/quic-go/quic-go v0.59.0 // indirect + github.com/rs/xid v1.6.0 // indirect + github.com/sethvargo/go-retry v0.3.0 // indirect + github.com/tinylib/msgp v1.6.4 // indirect + github.com/twitchyliquid64/golang-asm v0.15.1 // indirect + github.com/ugorji/go/codec v1.3.1 // indirect + github.com/zeebo/xxh3 v1.1.0 // indirect + go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect + go.uber.org/atomic v1.11.0 // indirect + go.uber.org/multierr v1.11.0 // indirect + go.yaml.in/yaml/v3 v3.0.5 // indirect + golang.org/x/arch v0.22.0 // indirect + golang.org/x/net v0.60.0 // indirect + golang.org/x/sync v0.23.0 // indirect + golang.org/x/sys v0.48.0 // indirect + golang.org/x/text v0.42.0 // indirect + google.golang.org/protobuf v1.36.12 // indirect + gopkg.in/ini.v1 v1.67.3 // indirect +) diff --git a/backend/go.sum b/backend/go.sum new file mode 100644 index 00000000..898b3e14 --- /dev/null +++ b/backend/go.sum @@ -0,0 +1,165 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M= +github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM= +github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE= +github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k= +github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE= +github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M= +github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= +github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= +github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= +github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w= +github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM= +github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8= +github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc= +github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= +github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= +github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= +github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= +github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= +github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= +github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w= +github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= +github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= +github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M= +github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM= +github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= +github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8= +github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k= +github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM= +github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6 h1:D/V0gu4zQ3cL2WKeVNVM4r2gLxGGf6McLwgXzRTo2RQ= +github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.7.1 h1:x7SYsPBYDkHDksogeSmZZ5xzThcTgRz++I5E+ePFUcs= +github.com/jackc/pgx/v5 v5.7.1/go.mod h1:e7O26IywZZ+naJtWWos6i6fvWK+29etgITqrqHLfoZA= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8= +github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ= +github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw= +github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= +github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM= +github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw= +github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= +github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= +github.com/mattn/go-isatty v0.0.22 h1:j8l17JJ9i6VGPUFUYoTUKPSgKe/83EYU2zBC7YNKMw4= +github.com/mattn/go-isatty v0.0.22/go.mod h1:ZXfXG4SQHsB/w3ZeOYbR0PrPwLy+n6xiMrJlRFqopa4= +github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= +github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= +github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI= +github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg= +github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= +github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= +github.com/minio/minio-go/v7 v7.4.1 h1:PtU/zIOfQRWZ+QydXAZysc+xZYHdG+BBR4Z6TUvy73w= +github.com/minio/minio-go/v7 v7.4.1/go.mod h1:nQnQi7zTfXR4lREtYXZQWqo5aw0Rqv+ZqCGrTORygRk= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4= +github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls= +github.com/pelletier/go-toml/v2 v2.4.3 h1:GTRvJQutkOSftxIFD5xw9aepkYNuPWmVJpffdDPYVpY= +github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM= +github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/pressly/goose/v3 v3.22.1 h1:2zICEfr1O3yTP9BRZMGPj7qFxQ+ik6yeo+z1LMuioLc= +github.com/pressly/goose/v3 v3.22.1/go.mod h1:xtMpbstWyCpyH+0cxLTMCENWBG+0CSxvTsXhW95d5eo= +github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8= +github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII= +github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw= +github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU= +github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0= +github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE= +github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU= +github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= +github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE= +github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= +github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ= +github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA= +github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= +github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= +github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= +github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ= +github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0= +github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs= +github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s= +go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE= +go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0= +go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE= +go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0= +go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= +go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= +go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= +go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= +golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI= +golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A= +golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M= +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU= +golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk= +golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo= +golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= +google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc= +google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw= +gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6 h1:5D53IMaUuA5InSeMu9eJtlQXS2NxAhyWQvkKEgXZhHI= +modernc.org/gc/v3 v3.0.0-20240107210532-573471604cb6/go.mod h1:Qz0X07sNOR1jWYCrJMEnbW/X55x206Q7Vt4mz6/wHp4= +modernc.org/mathutil v1.6.0 h1:fRe9+AmYlaej+64JsEEhoWuAYBkOtQiMEU7n/XgfYi4= +modernc.org/mathutil v1.6.0/go.mod h1:Ui5Q9q1TR2gFm0AQRqQUaBWFLAhQpCwNcuhBOSedWPo= +modernc.org/memory v1.8.0 h1:IqGTL6eFMaDZZhEWwcREgeMXYwmW83LYW8cROZYkg+E= +modernc.org/memory v1.8.0/go.mod h1:XPZ936zp5OMKGWPqbD3JShgd/ZoQ7899TUuQqxY+peU= +modernc.org/sqlite v1.33.0 h1:WWkA/T2G17okiLGgKAj4/RMIvgyMT19yQ038160IeYk= +modernc.org/sqlite v1.33.0/go.mod h1:9uQ9hF/pCZoYZK73D/ud5Z7cIRIILSZI8NdIemVMTX8= +modernc.org/strutil v1.2.0 h1:agBi9dp1I+eOnxXeiZawM8F4LawKv4NzGWSaLfyeNZA= +modernc.org/strutil v1.2.0/go.mod h1:/mdcBmfOibveCTBxUl5B5l6W+TTH1FXPLHZE6bTosX0= +modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y= +modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM= diff --git a/backend/internal/config/config.go b/backend/internal/config/config.go new file mode 100644 index 00000000..6419db3a --- /dev/null +++ b/backend/internal/config/config.go @@ -0,0 +1,230 @@ +package config + +import ( + "fmt" + "os" + "strconv" + "strings" + "time" +) + +type Config struct { + Host string + Port string + DatabaseURL string + SessionStore string + CacheURL string + SessionTTL time.Duration + IdempotencyTTL time.Duration + ShutdownDrain time.Duration + + S3Endpoint string + S3Bucket string + S3UseSSL bool + S3Region string + S3AccessKey string + S3SecretKey string + + MaxUploadBytes int64 + MaxImagePixels int64 + + ReconcilerEnabled bool + ReconcilerInterval time.Duration + ReconcilerGracePeriod time.Duration +} + +func Load() (*Config, error) { + dsn, err := readSecret("DATABASE_URL_FILE", "DATABASE_URL") + if err != nil { + return nil, fmt.Errorf("database url: %w", err) + } + + cacheURL, err := readSecretOptional("CACHE_URL_FILE", "CACHE_URL") + if err != nil { + return nil, fmt.Errorf("cache url: %w", err) + } + + ttlSeconds, err := readInt("SESSION_TTL_SECONDS", 3600) + if err != nil { + return nil, fmt.Errorf("SESSION_TTL_SECONDS: %w", err) + } + if ttlSeconds <= 0 { + return nil, fmt.Errorf("SESSION_TTL_SECONDS должен быть положительным") + } + + idemSeconds, err := readInt("IDEMPOTENCY_TTL_SECONDS", 86400) + if err != nil { + return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS: %w", err) + } + if idemSeconds <= 0 { + return nil, fmt.Errorf("IDEMPOTENCY_TTL_SECONDS должен быть положительным") + } + + drainSeconds, err := readInt("SHUTDOWN_DRAIN_SECONDS", 3) + if err != nil { + return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS: %w", err) + } + if drainSeconds < 0 { + return nil, fmt.Errorf("SHUTDOWN_DRAIN_SECONDS не может быть отрицательным") + } + + store := strings.ToLower(getenv("SESSION_STORE", "postgres")) + switch store { + case "postgres", "redis", "valkey": + default: + return nil, fmt.Errorf("SESSION_STORE=%q не поддерживается", store) + } + + s3Endpoint := getenv("S3_ENDPOINT", "") + s3Bucket := getenv("S3_BUCKET", "") + s3UseSSL, err := readBool("S3_USE_SSL", false) + if err != nil { + return nil, fmt.Errorf("S3_USE_SSL: %w", err) + } + s3Region := getenv("S3_REGION", "us-east-1") + + s3AccessKey, err := readSecretOptional("S3_ACCESS_KEY_ID_FILE", "S3_ACCESS_KEY_ID") + if err != nil { + return nil, fmt.Errorf("s3 access key: %w", err) + } + s3SecretKey, err := readSecretOptional("S3_SECRET_ACCESS_KEY_FILE", "S3_SECRET_ACCESS_KEY") + if err != nil { + return nil, fmt.Errorf("s3 secret key: %w", err) + } + + maxUploadBytes, err := readInt64("MAX_UPLOAD_BYTES", 10485760) + if err != nil { + return nil, fmt.Errorf("MAX_UPLOAD_BYTES: %w", err) + } + if maxUploadBytes <= 0 { + return nil, fmt.Errorf("MAX_UPLOAD_BYTES должен быть положительным") + } + + maxImagePixels, err := readInt64("MAX_IMAGE_PIXELS", 40000000) + if err != nil { + return nil, fmt.Errorf("MAX_IMAGE_PIXELS: %w", err) + } + if maxImagePixels <= 0 { + return nil, fmt.Errorf("MAX_IMAGE_PIXELS должен быть положительным") + } + + reconcilerEnabled, err := readBool("RECONCILER_ENABLED", true) + if err != nil { + return nil, fmt.Errorf("RECONCILER_ENABLED: %w", err) + } + reconcilerIntervalSec, err := readInt("RECONCILER_INTERVAL_SECONDS", 300) + if err != nil { + return nil, fmt.Errorf("RECONCILER_INTERVAL_SECONDS: %w", err) + } + if reconcilerIntervalSec <= 0 { + return nil, fmt.Errorf("RECONCILER_INTERVAL_SECONDS должен быть положительным") + } + reconcilerGraceSec, err := readInt("RECONCILER_GRACE_SECONDS", 3600) + if err != nil { + return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS: %w", err) + } + if reconcilerGraceSec <= 0 { + return nil, fmt.Errorf("RECONCILER_GRACE_SECONDS должен быть положительным") + } + + return &Config{ + Host: getenv("APP_HOST", "0.0.0.0"), + Port: getenv("APP_PORT", "8080"), + DatabaseURL: dsn, + SessionStore: store, + CacheURL: cacheURL, + SessionTTL: time.Duration(ttlSeconds) * time.Second, + IdempotencyTTL: time.Duration(idemSeconds) * time.Second, + ShutdownDrain: time.Duration(drainSeconds) * time.Second, + + S3Endpoint: s3Endpoint, + S3Bucket: s3Bucket, + S3UseSSL: s3UseSSL, + S3Region: s3Region, + S3AccessKey: s3AccessKey, + S3SecretKey: s3SecretKey, + + MaxUploadBytes: maxUploadBytes, + MaxImagePixels: maxImagePixels, + + ReconcilerEnabled: reconcilerEnabled, + ReconcilerInterval: time.Duration(reconcilerIntervalSec) * time.Second, + ReconcilerGracePeriod: time.Duration(reconcilerGraceSec) * time.Second, + }, nil +} + +func (c *Config) S3Configured() bool { + return c.S3Endpoint != "" && + c.S3Bucket != "" && + c.S3AccessKey != "" && + c.S3SecretKey != "" +} + +func getenv(key, def string) string { + if v := os.Getenv(key); v != "" { + return v + } + return def +} + +func readInt(key string, def int) (int, error) { + v := os.Getenv(key) + if v == "" { + return def, nil + } + n, err := strconv.Atoi(v) + if err != nil { + return 0, fmt.Errorf("не целое число: %q", v) + } + return n, nil +} + +func readInt64(key string, def int64) (int64, error) { + v := os.Getenv(key) + if v == "" { + return def, nil + } + n, err := strconv.ParseInt(v, 10, 64) + if err != nil { + return 0, fmt.Errorf("не целое число: %q", v) + } + return n, nil +} + +func readBool(key string, def bool) (bool, error) { + v := os.Getenv(key) + if v == "" { + return def, nil + } + b, err := strconv.ParseBool(v) + if err != nil { + return false, fmt.Errorf("не bool: %q", v) + } + return b, nil +} + +func readSecret(fileEnv, envKey string) (string, error) { + v, err := readSecretOptional(fileEnv, envKey) + if err != nil { + return "", err + } + if v == "" { + return "", fmt.Errorf("не задан %s или %s", fileEnv, envKey) + } + return v, nil +} + +func readSecretOptional(fileEnv, envKey string) (string, error) { + if path := os.Getenv(fileEnv); path != "" { + b, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("чтение %s=%s: %w", fileEnv, path, err) + } + s := strings.TrimSpace(string(b)) + if s == "" { + return "", fmt.Errorf("файл %s пуст", path) + } + return s, nil + } + return os.Getenv(envKey), nil +} diff --git a/backend/internal/domain/entity.go b/backend/internal/domain/entity.go new file mode 100644 index 00000000..570ac609 --- /dev/null +++ b/backend/internal/domain/entity.go @@ -0,0 +1,61 @@ +package domain + +import "time" + +type Health struct { + Status string +} + +type User struct { + ID string + Username string + DisplayName string + PasswordHash string // в DTO UserResponse его не будет + CreatedAt time.Time +} + +type Chat struct { + ID string + Title string + MemberIDs []string + CreatedAt time.Time +} + +type Message struct { + ID string + ChatID string + SenderID string + Text string + AttachmentIDs []string + CreatedAt time.Time +} + +type Session struct { + TokenHash []byte + UserID string + ExpiresAt time.Time + CreatedAt time.Time +} + +type AttachmentStatus string + +const ( + AttachmentStatusPending AttachmentStatus = "pending" + AttachmentStatusReady AttachmentStatus = "ready" + AttachmentStatusFailed AttachmentStatus = "failed" +) + +type Attachment struct { + ID string + ChatID string + OwnerID string + Filename string + ContentType string + SizeBytes int64 + SHA256 []byte + ObjectKey string + Status AttachmentStatus + ErrorCode *string + CreatedAt time.Time + UpdatedAt time.Time +} diff --git a/backend/internal/domain/errors.go b/backend/internal/domain/errors.go new file mode 100644 index 00000000..0d196ac0 --- /dev/null +++ b/backend/internal/domain/errors.go @@ -0,0 +1,197 @@ +package domain + +// Этот файл — единственный источник доменных ошибок. +// Правила: +// 1. sentinel-ошибки (ErrXxx) нужны для errors.Is. +// 2. типы *XxxError нужны для errors.As и для поля code в JSON. +// 3. У каждого типа есть Code() ErrorCode — через него DTO маппит +// в HTTP-код (StatusFromError в httpapi/dto/error.go). +// 4. В Error() пишем только то, что безопасно показать клиенту: +// без SQL, stack trace, путей, credential'ов. + +import ( + "errors" + "fmt" +) + +// =================================================================== +// ErrorCode — машинный код для JSON {"error":{"code":"..."}} +// =================================================================== + +type ErrorCode string + +const ( + CodeInvalidRequest ErrorCode = "invalid_request" // 400 + CodeUnauthorized ErrorCode = "unauthorized" // 401 + CodeNotFound ErrorCode = "not_found" // 404 + CodeConflict ErrorCode = "conflict" // 409 + CodePayloadTooLarge ErrorCode = "payload_too_large" // 413 + CodeUnsupportedMediaType ErrorCode = "unsupported_media_type" // 415 + CodeUnavailable ErrorCode = "unavailable" // 503 + CodeInternalError ErrorCode = "internal_error" // 500 +) + +// =================================================================== +// Sentinel-ошибки (errors.Is) +// =================================================================== + +var ( + ErrNotFound = errors.New("не найдено") + ErrAlreadyExists = errors.New("уже существует") + ErrInvalidInput = errors.New("некорректные данные") + ErrUnauthorized = errors.New("требуется аутентификация") + ErrUnsupportedMediaType = errors.New("неподдерживаемый тип содержимого") + ErrPayloadTooLarge = errors.New("превышен допустимый размер") + ErrUnavailable = errors.New("сервис недоступен") + ErrConflict = errors.New("конфликт") +) + +// =================================================================== +// 404 Not Found +// =================================================================== + +type NotFoundError struct { + Entity string + ID string +} + +func (e *NotFoundError) Error() string { + return fmt.Sprintf("%s с id %q не найден", e.Entity, e.ID) +} +func (e *NotFoundError) Unwrap() error { return ErrNotFound } +func (e *NotFoundError) Code() ErrorCode { return CodeNotFound } + +// =================================================================== +// 409 Conflict +// =================================================================== + +// AlreadyExistsError — конфликт уникальности (например, username занят). +// Отдельно от ConflictError: другой смысл, но оба дают HTTP 409. +type AlreadyExistsError struct { + Entity string + Field string + Value string +} + +func (e *AlreadyExistsError) Error() string { + return fmt.Sprintf("%s с %s %q уже существует", e.Entity, e.Field, e.Value) +} +func (e *AlreadyExistsError) Unwrap() error { return ErrAlreadyExists } +func (e *AlreadyExistsError) Code() ErrorCode { return CodeConflict } + +// IdempotencyConflictError — тот же Idempotency-Key, но другое тело запроса. +type IdempotencyConflictError struct { + Key string +} + +func (e *IdempotencyConflictError) Error() string { + return "Idempotency-Key " + e.Key + " уже использован с другим телом запроса" +} +func (e *IdempotencyConflictError) Unwrap() error { return ErrAlreadyExists } +func (e *IdempotencyConflictError) Code() ErrorCode { return CodeConflict } + +// ConflictError — конфликт бизнес-состояния, а не уникальности. +// Примеры: "вложение ещё не готово", "нельзя перевести failed-вложение в ready". +// Reason — короткая безопасная фраза, идёт клиенту как message. +type ConflictError struct{ Reason string } + +func (e *ConflictError) Error() string { return e.Reason } +func (e *ConflictError) Unwrap() error { return ErrConflict } +func (e *ConflictError) Code() ErrorCode { return CodeConflict } + +// =================================================================== +// 400 Bad Request +// =================================================================== + +// ValidationError — неверное поле или формат. +type ValidationError struct { + Field string + Message string +} + +func (e *ValidationError) Error() string { + return fmt.Sprintf("ошибка валидации поля %q: %s", e.Field, e.Message) +} +func (e *ValidationError) Unwrap() error { return ErrInvalidInput } +func (e *ValidationError) Code() ErrorCode { return CodeInvalidRequest } + +// =================================================================== +// 401 Unauthorized +// =================================================================== + +// UnauthorizedError — нет/просрочен/отозван токен, неверные credentials. +// Reason — только для логов, в JSON не попадает (по контракту все 401 +// должны иметь одинаковое сообщение, чтобы не перебирать username). +type UnauthorizedError struct { + Reason string +} + +func (e *UnauthorizedError) Error() string { + return "требуется аутентификация" +} +func (e *UnauthorizedError) Unwrap() error { return ErrUnauthorized } +func (e *UnauthorizedError) Code() ErrorCode { return CodeUnauthorized } + +// =================================================================== +// 415 Unsupported Media Type +// =================================================================== + +// UnsupportedMediaTypeError — неверный Content-Type. +// +// ВАЖНО: сейчас текст жёстко про application/json. Для lab4 (вложения) +// ожидается image/png или image/jpeg. Прежде чем использовать этот тип +// в AttachmentHandler, добавить поле Expected string (дефолт application/json) +// или сделать отдельный тип — не переиспользовать как есть, иначе клиент +// увидит неверную подсказку. +type UnsupportedMediaTypeError struct { + MediaType string + Expected string +} + +func (e *UnsupportedMediaTypeError) Error() string { + expected := e.Expected + if expected == "" { + expected = "application/json" + } + if e.MediaType == "" { + return "требуется Content-Type: " + expected + } + return fmt.Sprintf("неподдерживаемый Content-Type %q; требуется %s", e.MediaType, expected) +} +func (e *UnsupportedMediaTypeError) Unwrap() error { return ErrUnsupportedMediaType } +func (e *UnsupportedMediaTypeError) Code() ErrorCode { return CodeUnsupportedMediaType } + +// =================================================================== +// 413 Payload Too Large +// =================================================================== + +// PayloadTooLargeError — тело больше лимита. +// Message опционален: если пуст — формируется из Limit. +type PayloadTooLargeError struct { + Limit int64 + Message string +} + +func (e *PayloadTooLargeError) Error() string { + if e.Message != "" { + return e.Message + } + return fmt.Sprintf("превышен лимит %d байт", e.Limit) +} +func (e *PayloadTooLargeError) Unwrap() error { return ErrPayloadTooLarge } +func (e *PayloadTooLargeError) Code() ErrorCode { return CodePayloadTooLarge } + +// =================================================================== +// 503 Service Unavailable +// =================================================================== + +// UnavailableError — недоступна критичная зависимость (БД, кэш, S3). +// Reason — только для логов; клиент видит общее сообщение по контракту, +// чтобы не раскрывать имена сервисов и топологию. +type UnavailableError struct{ Reason string } + +func (e *UnavailableError) Error() string { + return "сервис временно недоступен" +} +func (e *UnavailableError) Unwrap() error { return ErrUnavailable } +func (e *UnavailableError) Code() ErrorCode { return CodeUnavailable } diff --git a/backend/internal/domain/idempotency.go b/backend/internal/domain/idempotency.go new file mode 100644 index 00000000..65d43d59 --- /dev/null +++ b/backend/internal/domain/idempotency.go @@ -0,0 +1,33 @@ +package domain + +import ( + "crypto/sha256" +) + +type IdempotencyScope struct { + UserID string + Method string + Path string + Key string +} + +type CreateIdempotentResult struct { + Status int + Body []byte + Replayed bool +} + +func RequestHash(text string, attachmentIDs []string) []byte { + if len(attachmentIDs) == 0 { + sum := sha256.Sum256([]byte(text)) + return sum[:] + } + h := sha256.New() + h.Write([]byte(text)) + h.Write([]byte{0}) + for _, id := range attachmentIDs { + h.Write([]byte(id)) + h.Write([]byte{0}) + } + return h.Sum(nil) +} diff --git a/backend/internal/domain/repository.go b/backend/internal/domain/repository.go new file mode 100644 index 00000000..b5d6a619 --- /dev/null +++ b/backend/internal/domain/repository.go @@ -0,0 +1,47 @@ +package domain + +import ( + "context" + "time" +) + +type UserRepository interface { + Create(ctx context.Context, user *User) error + GetByID(ctx context.Context, id string) (*User, error) + GetByUsername(ctx context.Context, username string) (*User, error) +} + +type ChatRepository interface { + Create(ctx context.Context, chat *Chat) error + GetByID(ctx context.Context, id string) (*Chat, error) + ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]Chat, *string, error) +} + +type MessageRepository interface { + Create(ctx context.Context, message *Message) error + ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]Message, *string, error) + CreateIdempotent( + ctx context.Context, + scope IdempotencyScope, + requestHash []byte, + fresh *Message, + freshResponse []byte, + ttl time.Duration, + ) (*CreateIdempotentResult, error) +} + +type SessionRepository interface { + Create(ctx context.Context, session *Session) error + GetByTokenHash(ctx context.Context, tokenHash []byte) (*Session, error) + DeleteByTokenHash(ctx context.Context, tokenHash []byte) error +} + +type AttachmentRepository interface { + Create(ctx context.Context, a *Attachment) error + GetByID(ctx context.Context, id string) (*Attachment, error) + MarkReady(ctx context.Context, id string) error + MarkFailed(ctx context.Context, id string, errorCode string) error + ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]Attachment, error) + ExistingKeys(ctx context.Context, keys []string) ([]string, error) + DeletePending(ctx context.Context, id string) error +} diff --git a/backend/internal/domain/storage.go b/backend/internal/domain/storage.go new file mode 100644 index 00000000..5df5ff2e --- /dev/null +++ b/backend/internal/domain/storage.go @@ -0,0 +1,21 @@ +package domain + +import ( + "context" + "io" + "time" +) + +type ObjectInfo struct { + Key string + LastModified time.Time + Size int64 +} + +type ObjectStorage interface { + Ping(ctx context.Context) error + Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error + Get(ctx context.Context, key string) (io.ReadCloser, int64, string, error) + Delete(ctx context.Context, key string) error + List(ctx context.Context, prefix string) ([]ObjectInfo, error) +} diff --git a/backend/internal/httpapi/dto/attachment.go b/backend/internal/httpapi/dto/attachment.go new file mode 100644 index 00000000..b608954a --- /dev/null +++ b/backend/internal/httpapi/dto/attachment.go @@ -0,0 +1,50 @@ +package dto + +import ( + "encoding/hex" + + "high-loud-apps-labs/internal/domain" +) + +type VariantResponse struct { + Name string `json:"name"` + ContentType string `json:"content_type"` + SizeBytes int64 `json:"size_bytes"` + Width int `json:"width"` + Height int `json:"height"` + SHA256 string `json:"sha256"` +} + +type AttachmentResponse struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + OwnerID string `json:"owner_id"` + Filename string `json:"filename"` + ContentType string `json:"content_type"` + SizeBytes int64 `json:"size_bytes"` + SHA256 string `json:"sha256"` + Status string `json:"status"` + Variants []VariantResponse `json:"variants"` + ErrorCode *string `json:"error_code"` + CreatedAt string `json:"created_at"` + UpdatedAt string `json:"updated_at"` +} + +func AttachmentFromDomain(a *domain.Attachment) AttachmentResponse { + variants := make([]VariantResponse, 0) + errCode := a.ErrorCode + return AttachmentResponse{ + ID: a.ID, + ChatID: a.ChatID, + OwnerID: a.OwnerID, + Filename: a.Filename, + ContentType: a.ContentType, + SizeBytes: a.SizeBytes, + SHA256: hex.EncodeToString(a.SHA256), + Status: string(a.Status), + Variants: variants, + ErrorCode: errCode, + CreatedAt: a.CreatedAt.UTC().Format(timeFormat), + UpdatedAt: a.UpdatedAt.UTC().Format(timeFormat), + } +} diff --git a/backend/internal/httpapi/dto/chat.go b/backend/internal/httpapi/dto/chat.go new file mode 100644 index 00000000..a9bf0425 --- /dev/null +++ b/backend/internal/httpapi/dto/chat.go @@ -0,0 +1,39 @@ +package dto + +import ( + "high-loud-apps-labs/internal/domain" +) + +type CreateChatRequest struct { + Title string `json:"title" binding:"required,min=1,max=100"` + MemberIDs []string `json:"member_ids" binding:"required,min=1,max=100,dive,uuid"` +} + +type ChatResponse struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt string `json:"created_at"` +} + +type ChatPageResponse struct { + Items []ChatResponse `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +func ChatFromDomain(c *domain.Chat) ChatResponse { + return ChatResponse{ + ID: c.ID, + Title: c.Title, + MemberIDs: append([]string(nil), c.MemberIDs...), + CreatedAt: c.CreatedAt.UTC().Format(timeFormat), + } +} + +func ChatsFromDomain(items []domain.Chat) []ChatResponse { + out := make([]ChatResponse, len(items)) + for i := range items { + out[i] = ChatFromDomain(&items[i]) + } + return out +} diff --git a/backend/internal/httpapi/dto/error.go b/backend/internal/httpapi/dto/error.go new file mode 100644 index 00000000..03f62acb --- /dev/null +++ b/backend/internal/httpapi/dto/error.go @@ -0,0 +1,86 @@ +package dto + +import ( + "errors" + + "high-loud-apps-labs/internal/domain" +) + +// делаем вложенный ответ через вкладку ErrorBody в обертку. + +type ErrorBody struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} + +type ErrorResponse struct { + Error ErrorBody `json:"error"` +} + +type coded interface { + Code() domain.ErrorCode +} + +func ErrorFromDomain(err error, requestID string) ErrorResponse { + var c coded + if errors.As(err, &c) { + code := c.Code() + message := err.Error() + if code == domain.CodeInternalError { + message = "внутренняя ошибка" + } + return ErrorResponse{Error: ErrorBody{ + Code: string(code), + Message: message, + RequestID: requestID, + }} + } + code := domain.CodeInternalError + switch { + case errors.Is(err, domain.ErrNotFound): + code = domain.CodeNotFound + case errors.Is(err, domain.ErrAlreadyExists): + code = domain.CodeConflict + case errors.Is(err, domain.ErrInvalidInput): + code = domain.CodeInvalidRequest + case errors.Is(err, domain.ErrUnauthorized): + code = domain.CodeUnauthorized + case errors.Is(err, domain.ErrUnsupportedMediaType): + code = domain.CodeUnsupportedMediaType + case errors.Is(err, domain.ErrUnavailable): + code = domain.CodeUnavailable + } + message := err.Error() + if code == domain.CodeInternalError { + message = "внутренняя ошибка" + } + return ErrorResponse{Error: ErrorBody{ + Code: string(code), + Message: message, + RequestID: requestID, + }} +} + +func StatusFromError(err error) int { + switch { + case errors.Is(err, domain.ErrNotFound): + return 404 + case errors.Is(err, domain.ErrAlreadyExists): + return 409 + case errors.Is(err, domain.ErrInvalidInput): + return 400 + case errors.Is(err, domain.ErrUnauthorized): + return 401 + case errors.Is(err, domain.ErrUnsupportedMediaType): + return 415 + case errors.Is(err, domain.ErrUnavailable): + return 503 + case errors.Is(err, domain.ErrPayloadTooLarge): + return 413 + case errors.Is(err, domain.ErrConflict): + return 409 + default: + return 500 + } +} diff --git a/backend/internal/httpapi/dto/health.go b/backend/internal/httpapi/dto/health.go new file mode 100644 index 00000000..8bb35034 --- /dev/null +++ b/backend/internal/httpapi/dto/health.go @@ -0,0 +1,5 @@ +package dto + +type HealthResponse struct { + Status string `json:"status"` +} diff --git a/backend/internal/httpapi/dto/message.go b/backend/internal/httpapi/dto/message.go new file mode 100644 index 00000000..6c9d6e1a --- /dev/null +++ b/backend/internal/httpapi/dto/message.go @@ -0,0 +1,47 @@ +package dto + +import ( + "high-loud-apps-labs/internal/domain" +) + +type CreateMessageRequest struct { + Text string `json:"text" binding:"max=4000"` + AttachmentIDs []string `json:"attachment_ids" binding:"max=10,dive,uuid"` +} + +type MessageResponse struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + AttachmentIDs []string `json:"attachment_ids"` + CreatedAt string `json:"created_at"` +} + +type MessagePageResponse struct { + Items []MessageResponse `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +func MessageFromDomain(m *domain.Message) MessageResponse { + ids := m.AttachmentIDs + if ids == nil { + ids = []string{} + } + return MessageResponse{ + ID: m.ID, + ChatID: m.ChatID, + SenderID: m.SenderID, + Text: m.Text, + AttachmentIDs: ids, + CreatedAt: m.CreatedAt.UTC().Format(timeFormat), + } +} + +func MessagesFromDomain(items []domain.Message) []MessageResponse { + out := make([]MessageResponse, len(items)) + for i := range items { + out[i] = MessageFromDomain(&items[i]) + } + return out +} diff --git a/backend/internal/httpapi/dto/session.go b/backend/internal/httpapi/dto/session.go new file mode 100644 index 00000000..174430cf --- /dev/null +++ b/backend/internal/httpapi/dto/session.go @@ -0,0 +1,21 @@ +package dto + +import ( + "high-loud-apps-labs/internal/domain" +) + +type SessionResponse struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt string `json:"expires_at"` + User UserResponse `json:"user"` +} + +func SessionResponseFromDomain(s *domain.Session, token string, u *domain.User) SessionResponse { + return SessionResponse{ + Token: token, + TokenType: "Bearer", + ExpiresAt: s.ExpiresAt.UTC().Format(timeFormat), + User: UserFromDomain(u), + } +} diff --git a/backend/internal/httpapi/dto/user.go b/backend/internal/httpapi/dto/user.go new file mode 100644 index 00000000..08519414 --- /dev/null +++ b/backend/internal/httpapi/dto/user.go @@ -0,0 +1,29 @@ +package dto + +import ( + "high-loud-apps-labs/internal/domain" +) + +const timeFormat = "2006-01-02T15:04:05.000000Z07:00" + +type CreateUserRequest struct { + Username string `json:"username" binding:"required,min=3,max=32"` + DisplayName string `json:"display_name" binding:"required,min=1,max=100"` + Password string `json:"password" binding:"required,min=12,max=512"` +} + +type UserResponse struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt string `json:"created_at"` +} + +func UserFromDomain(u *domain.User) UserResponse { + return UserResponse{ + ID: u.ID, + Username: u.Username, + DisplayName: u.DisplayName, + CreatedAt: u.CreatedAt.UTC().Format(timeFormat), + } +} diff --git a/backend/internal/httpapi/handler/attachment.go b/backend/internal/httpapi/handler/attachment.go new file mode 100644 index 00000000..ad0f8186 --- /dev/null +++ b/backend/internal/httpapi/handler/attachment.go @@ -0,0 +1,98 @@ +package handler + +import ( + "mime" + "net/http" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +type AttachmentHandler struct { + svc *service.AttachmentService +} + +func NewAttachmentHandler(svc *service.AttachmentService) *AttachmentHandler { + return &AttachmentHandler{svc: svc} +} + +func (h *AttachmentHandler) Upload(c *gin.Context) { + me := middleware.CurrentUser(c) + + chatID, err := parseUUIDPathParam(c, "chat_id") + if err != nil { + respondError(c, err) + return + } + + filename := c.Query("filename") + if filename == "" { + respondError(c, &domain.ValidationError{ + Field: "filename", + Message: "обязателен", + }) + return + } + + mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type")) + if err != nil || (mediaType != "image/png" && mediaType != "image/jpeg") { + respondError(c, &domain.UnsupportedMediaTypeError{ + MediaType: c.GetHeader("Content-Type"), + Expected: "image/png или image/jpeg", + }) + return + } + + att, err := h.svc.Upload(c.Request.Context(), me.ID, chatID, filename, c.Request.Body) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusCreated, dto.AttachmentFromDomain(att)) +} + +func (h *AttachmentHandler) Get(c *gin.Context) { + me := middleware.CurrentUser(c) + + attachmentID, err := parseUUIDPathParam(c, "attachment_id") + if err != nil { + respondError(c, err) + return + } + + a, err := h.svc.Get(c.Request.Context(), me.ID, attachmentID) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusOK, dto.AttachmentFromDomain(a)) +} + +func (h *AttachmentHandler) Content(c *gin.Context) { + me := middleware.CurrentUser(c) + + attachmentID, err := parseUUIDPathParam(c, "attachment_id") + if err != nil { + respondError(c, err) + return + } + + variant := c.DefaultQuery("variant", "original") + if variant != "original" { + respondError(c, &domain.NotFoundError{Entity: "variant", ID: variant}) + return + } + + rc, size, contentType, err := h.svc.Download(c.Request.Context(), me.ID, attachmentID) + if err != nil { + respondError(c, err) + return + } + defer func() { _ = rc.Close() }() + + c.DataFromReader(http.StatusOK, size, contentType, rc, nil) +} diff --git a/backend/internal/httpapi/handler/auth.go b/backend/internal/httpapi/handler/auth.go new file mode 100644 index 00000000..b06a8727 --- /dev/null +++ b/backend/internal/httpapi/handler/auth.go @@ -0,0 +1,77 @@ +package handler + +import ( + "encoding/base64" + "net/http" + "strings" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +type AuthHandler struct { + sessions *service.SessionService +} + +func NewAuthHandler(sessions *service.SessionService) *AuthHandler { + return &AuthHandler{sessions: sessions} +} + +func (h *AuthHandler) CreateSession(c *gin.Context) { + username, password, err := parseBasicAuth(c) + if err != nil { + respondBasicChallenge(c, err) + return + } + + session, token, user, err := h.sessions.Login(c.Request.Context(), username, password) + if err != nil { + respondBasicChallenge(c, err) + return + } + + c.JSON(http.StatusCreated, dto.SessionResponseFromDomain(session, token, user)) +} + +// RevokeCurrent отзывает текущую сессию. Токен уже проверен BearerAuth, +// поэтому здесь его достаточно передать в SessionService. +func (h *AuthHandler) RevokeCurrent(c *gin.Context) { + token := middleware.CurrentToken(c) + if token == "" { + respondError(c, &domain.UnauthorizedError{Reason: "нет токена"}) + return + } + + if err := h.sessions.Revoke(c.Request.Context(), token); err != nil { + respondError(c, err) + return + } + + c.Status(http.StatusNoContent) +} + +// parseBasicAuth читает Authorization: Basic base64(username:password). +func parseBasicAuth(c *gin.Context) (string, string, error) { + const prefix = "Basic " + + auth := c.GetHeader("Authorization") + if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) { + return "", "", &domain.UnauthorizedError{Reason: "требуется Basic"} + } + + decoded, err := base64.StdEncoding.DecodeString(auth[len(prefix):]) + if err != nil { + return "", "", &domain.UnauthorizedError{Reason: "некорректный Base64"} + } + + username, password, ok := strings.Cut(string(decoded), ":") + if !ok || username == "" { + return "", "", &domain.UnauthorizedError{Reason: "некорректный Basic-формат"} + } + + return username, password, nil +} diff --git a/backend/internal/httpapi/handler/bind.go b/backend/internal/httpapi/handler/bind.go new file mode 100644 index 00000000..cca73874 --- /dev/null +++ b/backend/internal/httpapi/handler/bind.go @@ -0,0 +1,52 @@ +package handler + +import ( + "bytes" + "encoding/json" + "errors" + "io" + "mime" + "strings" + + "github.com/gin-gonic/gin" + "github.com/gin-gonic/gin/binding" + + "high-loud-apps-labs/internal/domain" +) + +func bindStrictJSON(c *gin.Context, dst any) error { + ct := c.GetHeader("Content-Type") + mediaType, _, err := mime.ParseMediaType(ct) + if ct == "" || err != nil || !strings.EqualFold(mediaType, "application/json") { + return &domain.UnsupportedMediaTypeError{MediaType: ct} + } + + body, err := io.ReadAll(c.Request.Body) + if err != nil { + return &badRequestError{msg: "не удалось прочитать тело запроса"} + } + _ = c.Request.Body.Close() + + dec := json.NewDecoder(bytes.NewReader(body)) + dec.DisallowUnknownFields() + + if err := dec.Decode(dst); err != nil { + if errors.Is(err, io.EOF) { + return &badRequestError{msg: "тело запроса обязательно"} + } + return &badRequestError{msg: err.Error()} + } + + // Отклоняем любой непустой остаток после первого JSON-значения. + var extra json.RawMessage + if err := dec.Decode(&extra); !errors.Is(err, io.EOF) { + return &badRequestError{msg: "тело запроса содержит данные после JSON"} + } + + if v := binding.Validator; v != nil { + if err := v.ValidateStruct(dst); err != nil { + return &badRequestError{msg: err.Error()} + } + } + return nil +} diff --git a/backend/internal/httpapi/handler/chat.go b/backend/internal/httpapi/handler/chat.go new file mode 100644 index 00000000..86fc2cd0 --- /dev/null +++ b/backend/internal/httpapi/handler/chat.go @@ -0,0 +1,88 @@ +package handler + +import ( + "net/http" + "strconv" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +type ChatHandler struct { + chats *service.ChatService +} + +func NewChatHandler(chats *service.ChatService) *ChatHandler { + return &ChatHandler{chats: chats} +} + +func (h *ChatHandler) Create(c *gin.Context) { + var req dto.CreateChatRequest + if err := bindStrictJSON(c, &req); err != nil { + respondError(c, err) + return + } + me := middleware.CurrentUser(c) + + chat, err := h.chats.Create(c.Request.Context(), me.ID, req.Title, req.MemberIDs) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusCreated, dto.ChatFromDomain(chat)) +} + +func (h *ChatHandler) Get(c *gin.Context) { + me := middleware.CurrentUser(c) + + chatID, err := parseUUIDPathParam(c, "chat_id") + if err != nil { + respondError(c, err) + return + } + + chat, err := h.chats.Get(c.Request.Context(), me.ID, chatID) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusOK, dto.ChatFromDomain(chat)) +} + +func (h *ChatHandler) List(c *gin.Context) { + me := middleware.CurrentUser(c) + limit, cursor, err := parseListQuery(c) + if err != nil { + respondError(c, err) + return + } + + items, next, err := h.chats.List(c.Request.Context(), me.ID, limit, cursor) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusOK, dto.ChatPageResponse{ + Items: dto.ChatsFromDomain(items), + NextCursor: next, + }) +} + +func parseListQuery(c *gin.Context) (int, *string, error) { + limit := 50 + if v := c.Query("limit"); v != "" { + n, err := strconv.Atoi(v) + if err != nil || n < 1 || n > 100 { + return 0, nil, &badRequestError{msg: "limit должен быть целым 1..100"} + } + limit = n + } + var cursor *string + if v := c.Query("cursor"); v != "" { + cursor = &v + } + return limit, cursor, nil +} diff --git a/backend/internal/httpapi/handler/health.go b/backend/internal/httpapi/handler/health.go new file mode 100644 index 00000000..00995659 --- /dev/null +++ b/backend/internal/httpapi/handler/health.go @@ -0,0 +1,54 @@ +package handler + +import ( + "context" + "net/http" + "sync/atomic" + "time" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" +) + +type HealthCheck func(ctx context.Context) error + +type HealthHandler struct { + checks []HealthCheck + ready *atomic.Bool +} + +func NewHealthHandler(checks ...HealthCheck) *HealthHandler { + return &HealthHandler{checks: checks} +} + +func (h *HealthHandler) WithReadyFlag(v *atomic.Bool) *HealthHandler { + h.ready = v + return h +} + +func (h *HealthHandler) Live(c *gin.Context) { + c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"}) +} + +func (h *HealthHandler) Ready(c *gin.Context) { + if h.ready != nil && !h.ready.Load() { + respondError(c, &domain.UnavailableError{Reason: "shutting down"}) + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second) + defer cancel() + + for _, check := range h.checks { + if check == nil { + continue + } + if err := check(ctx); err != nil { + respondError(c, &domain.UnavailableError{Reason: "зависимость недоступна"}) + return + } + } + c.JSON(http.StatusOK, dto.HealthResponse{Status: "ok"}) +} diff --git a/backend/internal/httpapi/handler/message.go b/backend/internal/httpapi/handler/message.go new file mode 100644 index 00000000..badf7f7b --- /dev/null +++ b/backend/internal/httpapi/handler/message.go @@ -0,0 +1,88 @@ +package handler + +import ( + "net/http" + "regexp" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) + +type MessageHandler struct { + messages *service.MessageService +} + +func NewMessageHandler(messages *service.MessageService) *MessageHandler { + return &MessageHandler{messages: messages} +} + +func (h *MessageHandler) Create(c *gin.Context) { + var req dto.CreateMessageRequest + if err := bindStrictJSON(c, &req); err != nil { + respondError(c, err) + return + } + + key := c.GetHeader("Idempotency-Key") + if key == "" || !idempotencyKeyPattern.MatchString(key) { + respondError(c, &badRequestError{ + msg: "Idempotency-Key обязателен и должен соответствовать ^[A-Za-z0-9._:-]{1,128}$", + }) + return + } + + me := middleware.CurrentUser(c) + + chatID, err := parseUUIDPathParam(c, "chat_id") + if err != nil { + respondError(c, err) + return + } + + result, err := h.messages.Create( + c.Request.Context(), + me.ID, + chatID, + req.Text, + req.AttachmentIDs, + key, + c.Request.URL.Path, + ) + if err != nil { + respondError(c, err) + return + } + + c.Data(result.Status, "application/json", result.Body) +} + +func (h *MessageHandler) List(c *gin.Context) { + me := middleware.CurrentUser(c) + + chatID, err := parseUUIDPathParam(c, "chat_id") + if err != nil { + respondError(c, err) + return + } + + limit, cursor, err := parseListQuery(c) + if err != nil { + respondError(c, err) + return + } + + items, next, err := h.messages.List(c.Request.Context(), me.ID, chatID, limit, cursor) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusOK, dto.MessagePageResponse{ + Items: dto.MessagesFromDomain(items), + NextCursor: next, + }) +} diff --git a/backend/internal/httpapi/handler/response.go b/backend/internal/httpapi/handler/response.go new file mode 100644 index 00000000..8cad0b3e --- /dev/null +++ b/backend/internal/httpapi/handler/response.go @@ -0,0 +1,44 @@ +package handler + +import ( + "github.com/gin-gonic/gin" + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" +) + +type badRequestError struct{ msg string } + +func (e *badRequestError) Error() string { return e.msg } +func (e *badRequestError) Unwrap() error { return domain.ErrInvalidInput } +func (e *badRequestError) Code() domain.ErrorCode { return domain.CodeInvalidRequest } + +func parseUUIDPathParam(c *gin.Context, name string) (string, error) { + raw := c.Param(name) + if raw == "" { + return "", &badRequestError{msg: "параметр " + name + " обязателен"} + } + + id, err := uuid.Parse(raw) + if err != nil { + return "", &badRequestError{msg: "параметр " + name + " должен быть корректным UUID"} + } + + return id.String(), nil +} + +func respondError(c *gin.Context, err error) { + reqID := middleware.GetRequestID(c) + c.JSON(dto.StatusFromError(err), dto.ErrorFromDomain(err, reqID)) +} + +// respondBasicChallenge выставляет WWW-Authenticate для 401 на /auth/sessions. +// На не-401 ответах заголовок не ставится как по контракту нужно +func respondBasicChallenge(c *gin.Context, err error) { + if dto.StatusFromError(err) == 401 { + c.Header("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`) + } + respondError(c, err) +} diff --git a/backend/internal/httpapi/handler/user.go b/backend/internal/httpapi/handler/user.go new file mode 100644 index 00000000..7697b504 --- /dev/null +++ b/backend/internal/httpapi/handler/user.go @@ -0,0 +1,43 @@ +package handler + +import ( + "net/http" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/httpapi/middleware" + "high-loud-apps-labs/internal/service" +) + +type UserHandler struct { + users *service.UserService +} + +func NewUserHandler(users *service.UserService) *UserHandler { + return &UserHandler{users: users} +} + +func (h *UserHandler) Create(c *gin.Context) { + var req dto.CreateUserRequest + if err := bindStrictJSON(c, &req); err != nil { + respondError(c, err) + return + } + + u, err := h.users.Create(c.Request.Context(), req.Username, req.DisplayName, req.Password) + if err != nil { + respondError(c, err) + return + } + c.JSON(http.StatusCreated, dto.UserFromDomain(u)) +} + +func (h *UserHandler) Current(c *gin.Context) { + u := middleware.CurrentUser(c) + if u == nil { + respondError(c, &badRequestError{msg: "нет текущего пользователя"}) + return + } + c.JSON(http.StatusOK, dto.UserFromDomain(u)) +} diff --git a/backend/internal/httpapi/middleware/auth_legacy.go b/backend/internal/httpapi/middleware/auth_legacy.go new file mode 100644 index 00000000..e3192dab --- /dev/null +++ b/backend/internal/httpapi/middleware/auth_legacy.go @@ -0,0 +1,38 @@ +package middleware + +import ( + "net/http" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" +) + +func Auth(users domain.UserRepository) gin.HandlerFunc { + return func(c *gin.Context) { + id := c.GetHeader("X-User-Id") + if id == "" { + abortUnauthorized(c, "требуется X-User-Id") + return + } + + u, err := users.GetByID(c.Request.Context(), id) + if err != nil { + abortUnauthorized(c, "неизвестный пользователь") + return + } + + c.Set(CurrentUserKey, u) + c.Next() + } +} + +func abortUnauthorized(c *gin.Context, msg string) { + reqID := GetRequestID(c) + body := dto.ErrorFromDomain( + &domain.UnauthorizedError{Reason: msg}, + reqID, + ) + c.AbortWithStatusJSON(http.StatusUnauthorized, body) +} diff --git a/backend/internal/httpapi/middleware/bearer.go b/backend/internal/httpapi/middleware/bearer.go new file mode 100644 index 00000000..fc904aa2 --- /dev/null +++ b/backend/internal/httpapi/middleware/bearer.go @@ -0,0 +1,70 @@ +package middleware + +import ( + "context" + "strings" + "time" + + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" + "high-loud-apps-labs/internal/service" +) + +const authTimeout = 3 * time.Second + +// BearerAuth читает Authorization: Bearer , проверяет сессию +// через SessionService и кладёт пользователя и токен в контекст. +func BearerAuth(sessions *service.SessionService) gin.HandlerFunc { + return func(c *gin.Context) { + token, ok := parseBearerToken(c) + if !ok { + abortBearer(c, "требуется Bearer") + return + } + + ctx, cancel := context.WithTimeout(c.Request.Context(), authTimeout) + defer cancel() + + user, err := sessions.Resolve(ctx, token) + if err != nil { + abortBearerWithErr(c, err) + return + } + + c.Set(CurrentUserKey, user) + c.Set(CurrentTokenKey, token) + c.Next() + } +} + +// parseBearerToken читает Authorization: Bearer . +func parseBearerToken(c *gin.Context) (string, bool) { + const prefix = "Bearer " + auth := c.GetHeader("Authorization") + if len(auth) < len(prefix) || !strings.EqualFold(auth[:len(prefix)], prefix) { + return "", false + } + token := strings.TrimSpace(auth[len(prefix):]) + if token == "" { + return "", false + } + return token, true +} + +func abortBearer(c *gin.Context, reason string) { + abortBearerWithErr(c, &domain.UnauthorizedError{Reason: reason}) +} + +// abortBearerWithErr формирует ответ на ошибку аутентификации. +// Заголовок WWW-Authenticate: Bearer выставляется только на 401; +// при 503 (недоступность БД/кэша) — обычный ответ ошибки без challenge. +func abortBearerWithErr(c *gin.Context, err error) { + reqID := GetRequestID(c) + status := dto.StatusFromError(err) + if status == 401 { + c.Header("WWW-Authenticate", `Bearer realm="messenger"`) + } + c.AbortWithStatusJSON(status, dto.ErrorFromDomain(err, reqID)) +} diff --git a/backend/internal/httpapi/middleware/context.go b/backend/internal/httpapi/middleware/context.go new file mode 100644 index 00000000..f467d367 --- /dev/null +++ b/backend/internal/httpapi/middleware/context.go @@ -0,0 +1,30 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/domain" +) + +const ( + CurrentUserKey = "current_user" + CurrentTokenKey = "current_token" +) + +func CurrentUser(c *gin.Context) *domain.User { + if v, ok := c.Get(CurrentUserKey); ok { + if u, ok := v.(*domain.User); ok { + return u + } + } + return nil +} + +func CurrentToken(c *gin.Context) string { + if v, ok := c.Get(CurrentTokenKey); ok { + if s, ok := v.(string); ok { + return s + } + } + return "" +} diff --git a/backend/internal/httpapi/middleware/instance.go b/backend/internal/httpapi/middleware/instance.go new file mode 100644 index 00000000..583488c1 --- /dev/null +++ b/backend/internal/httpapi/middleware/instance.go @@ -0,0 +1,31 @@ +package middleware + +import ( + "os" + + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +const InstanceIDHeader = "X-Instance-Id" + +var instanceID = resolveInstanceID() + +func resolveInstanceID() string { + if v := os.Getenv("INSTANCE_ID"); v != "" { + return v + } + if v, err := os.Hostname(); err == nil && v != "" { + return v + } + return uuid.NewString() +} + +func Instance() gin.HandlerFunc { + return func(c *gin.Context) { + c.Writer.Header().Set(InstanceIDHeader, instanceID) + c.Next() + } +} + +func GetInstanceID() string { return instanceID } diff --git a/backend/internal/httpapi/middleware/requestid.go b/backend/internal/httpapi/middleware/requestid.go new file mode 100644 index 00000000..43c90113 --- /dev/null +++ b/backend/internal/httpapi/middleware/requestid.go @@ -0,0 +1,32 @@ +package middleware + +import ( + "github.com/gin-gonic/gin" + "github.com/google/uuid" +) + +const ( + RequestIDKey = "request_id" + RequestIDHeader = "X-Request-Id" +) + +func RequestID() gin.HandlerFunc { + return func(c *gin.Context) { + id := c.GetHeader(RequestIDHeader) + if id == "" { + id = uuid.NewString() + } + c.Set(RequestIDKey, id) + c.Writer.Header().Set(RequestIDHeader, id) + c.Next() + } +} + +func GetRequestID(c *gin.Context) string { + if v, ok := c.Get(RequestIDKey); ok { + if s, ok := v.(string); ok { + return s + } + } + return "" +} diff --git a/backend/internal/httpapi/router.go b/backend/internal/httpapi/router.go new file mode 100644 index 00000000..a614a017 --- /dev/null +++ b/backend/internal/httpapi/router.go @@ -0,0 +1,57 @@ +package httpapi + +import ( + "github.com/gin-gonic/gin" + + "high-loud-apps-labs/internal/httpapi/handler" + "high-loud-apps-labs/internal/httpapi/middleware" +) + +type Handlers struct { + Health *handler.HealthHandler + User *handler.UserHandler + Chat *handler.ChatHandler + Message *handler.MessageHandler + Auth *handler.AuthHandler + Attachment *handler.AttachmentHandler +} + +func NewRouter(h Handlers, auth gin.HandlerFunc) *gin.Engine { + gin.EnableJsonDecoderDisallowUnknownFields() + + r := gin.New() + r.Use(gin.Logger()) + r.Use(gin.Recovery()) + r.Use(middleware.RequestID()) + r.Use(middleware.Instance()) + + r.GET("/health/live", h.Health.Live) + r.GET("/health/ready", h.Health.Ready) + + api := r.Group("/api/v1") + { + api.POST("/users", h.User.Create) + api.POST("/auth/sessions", h.Auth.CreateSession) + } + + protected := api.Group("") + protected.Use(auth) + { + protected.DELETE("/auth/sessions/current", h.Auth.RevokeCurrent) + + protected.GET("/users/me", h.User.Current) + protected.POST("/chats", h.Chat.Create) + protected.GET("/chats", h.Chat.List) + protected.GET("/chats/:chat_id", h.Chat.Get) + protected.POST("/chats/:chat_id/messages", h.Message.Create) + protected.GET("/chats/:chat_id/messages", h.Message.List) + + if h.Attachment != nil { + protected.POST("/chats/:chat_id/attachments", h.Attachment.Upload) + protected.GET("/attachments/:attachment_id", h.Attachment.Get) + protected.GET("/attachments/:attachment_id/content", h.Attachment.Content) + } + } + + return r +} diff --git a/backend/internal/service/attachment.go b/backend/internal/service/attachment.go new file mode 100644 index 00000000..f666fd50 --- /dev/null +++ b/backend/internal/service/attachment.go @@ -0,0 +1,184 @@ +package service + +import ( + "bytes" + "context" + "crypto/sha256" + "image" + _ "image/jpeg" + _ "image/png" + "io" + "net/http" + "strings" + "time" + "unicode" + "unicode/utf8" + + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" +) + +type AttachmentService struct { + attachments domain.AttachmentRepository + chats domain.ChatRepository + storage domain.ObjectStorage + maxBytes int64 + maxPixels int64 +} + +func NewAttachmentService( + attachments domain.AttachmentRepository, + chats domain.ChatRepository, + storage domain.ObjectStorage, + maxBytes, maxPixels int64, +) *AttachmentService { + return &AttachmentService{ + attachments: attachments, + chats: chats, + storage: storage, + maxBytes: maxBytes, + maxPixels: maxPixels, + } +} + +// Upload принимает тело, валидирует PNG/JPEG по содержимому, +// кладёт в pending, загружает в S3, переводит в ready. +// При ошибке Put компенсирует MarkFailed. +func (s *AttachmentService) Upload( + ctx context.Context, + ownerID, chatID, filename string, + body io.Reader, +) (*domain.Attachment, error) { + if err := s.ensureMember(ctx, ownerID, chatID); err != nil { + return nil, err + } + if err := validateFilename(filename); err != nil { + return nil, err + } + + limited := io.LimitReader(body, s.maxBytes+1) + buf, err := io.ReadAll(limited) + if err != nil { + return nil, &domain.UnavailableError{Reason: "не удалось прочитать тело загрузки"} + } + if int64(len(buf)) > s.maxBytes { + return nil, &domain.PayloadTooLargeError{Limit: s.maxBytes} + } + if len(buf) == 0 { + return nil, &domain.ValidationError{Field: "body", Message: "пустой файл"} + } + + contentType := http.DetectContentType(buf) + if contentType != "image/png" && contentType != "image/jpeg" { + return nil, &domain.UnsupportedMediaTypeError{ + MediaType: contentType, + Expected: "image/png или image/jpeg", + } + } + + cfg, _, err := image.DecodeConfig(bytes.NewReader(buf)) + if err != nil { + return nil, &domain.UnsupportedMediaTypeError{ + MediaType: contentType, + Expected: "image/png или image/jpeg", + } + } + if int64(cfg.Width)*int64(cfg.Height) > s.maxPixels { + return nil, &domain.PayloadTooLargeError{ + Limit: s.maxPixels, + Message: "изображение превышает лимит пикселей", + } + } + + sha := sha256.Sum256(buf) + id := uuid.NewString() + objectKey := "chats/" + chatID + "/" + id + now := time.Now().UTC() + + a := &domain.Attachment{ + ID: id, + ChatID: chatID, + OwnerID: ownerID, + Filename: filename, + ContentType: contentType, + SizeBytes: int64(len(buf)), + SHA256: sha[:], + ObjectKey: objectKey, + Status: domain.AttachmentStatusPending, + CreatedAt: now, + UpdatedAt: now, + } + if err := s.attachments.Create(ctx, a); err != nil { + return nil, err + } + + if err := s.storage.Put(ctx, objectKey, bytes.NewReader(buf), int64(len(buf)), contentType); err != nil { + // Компенсация: pending → failed. Reconciler не будет трогать failed. + _ = s.attachments.MarkFailed(ctx, a.ID, "upload_failed") + return nil, err + } + + if err := s.attachments.MarkReady(ctx, a.ID); err != nil { + // S3 has the object, DB still pending. Reconciler очистит по grace period. + return nil, err + } + a.Status = domain.AttachmentStatusReady + return a, nil +} + +func (s *AttachmentService) Get(ctx context.Context, userID, attachmentID string) (*domain.Attachment, error) { + a, err := s.attachments.GetByID(ctx, attachmentID) + if err != nil { + return nil, err + } + if err := s.ensureMember(ctx, userID, a.ChatID); err != nil { + return nil, err + } + return a, nil +} + +func (s *AttachmentService) Download( + ctx context.Context, + userID, attachmentID string, +) (io.ReadCloser, int64, string, error) { + a, err := s.Get(ctx, userID, attachmentID) + if err != nil { + return nil, 0, "", err + } + if a.Status != domain.AttachmentStatusReady { + return nil, 0, "", &domain.ConflictError{Reason: "вложение не готово"} + } + return s.storage.Get(ctx, a.ObjectKey) +} + +func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID string) error { + c, err := s.chats.GetByID(ctx, chatID) + if err != nil { + return err + } + for _, m := range c.MemberIDs { + if m == userID { + return nil + } + } + return &domain.NotFoundError{Entity: "chat", ID: chatID} +} + +func validateFilename(name string) error { + if name == "" { + return &domain.ValidationError{Field: "filename", Message: "не может быть пустым"} + } + if utf8.RuneCountInString(name) > 255 { + return &domain.ValidationError{Field: "filename", Message: "не более 255 символов"} + } + if strings.ContainsAny(name, "/\\\x00") { + return &domain.ValidationError{Field: "filename", Message: "содержит недопустимые символы"} + } + for _, r := range name { + if unicode.IsControl(r) { + return &domain.ValidationError{Field: "filename", Message: "содержит управляющие символы"} + } + } + return nil +} diff --git a/backend/internal/service/chat.go b/backend/internal/service/chat.go new file mode 100644 index 00000000..caa8dcec --- /dev/null +++ b/backend/internal/service/chat.go @@ -0,0 +1,95 @@ +package service + +import ( + "context" + "errors" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" +) + +type ChatService struct { + chats domain.ChatRepository + users domain.UserRepository +} + +func NewChatService(chats domain.ChatRepository, users domain.UserRepository) *ChatService { + return &ChatService{chats: chats, users: users} +} + +func (s *ChatService) Create(ctx context.Context, creatorID, title string, memberIDs []string) (*domain.Chat, error) { + if strings.TrimSpace(title) == "" { + return nil, &domain.ValidationError{ + Field: "title", + Message: "не может состоять только из пробелов", + } + } + if utf8.RuneCountInString(title) > 100 { + return nil, &domain.ValidationError{ + Field: "title", + Message: "не более 100 символов", + } + } + + seen := make(map[string]struct{}, len(memberIDs)) + for _, id := range memberIDs { + if _, dup := seen[id]; dup { + return nil, &domain.ValidationError{ + Field: "member_ids", + Message: "дубликаты запрещены", + } + } + seen[id] = struct{}{} + } + + if _, ok := seen[creatorID]; !ok { + return nil, &domain.ValidationError{ + Field: "member_ids", + Message: "создатель должен входить в список участников", + } + } + + for _, id := range memberIDs { + if _, err := s.users.GetByID(ctx, id); err != nil { + if errors.Is(err, domain.ErrNotFound) { + return nil, &domain.ValidationError{ + Field: "member_ids", + Message: "неизвестный участник " + id, + } + } + return nil, err + } + } + + c := &domain.Chat{ + ID: uuid.NewString(), + Title: title, + MemberIDs: append([]string(nil), memberIDs...), + CreatedAt: time.Now().UTC(), + } + if err := s.chats.Create(ctx, c); err != nil { + return nil, err + } + return c, nil +} + +func (s *ChatService) Get(ctx context.Context, userID, chatID string) (*domain.Chat, error) { + c, err := s.chats.GetByID(ctx, chatID) + if err != nil { + return nil, err + } + for _, m := range c.MemberIDs { + if m == userID { + return c, nil + } + } + return nil, &domain.NotFoundError{Entity: "chat", ID: chatID} +} + +func (s *ChatService) List(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) { + return s.chats.ListByMember(ctx, userID, limit, cursor) +} diff --git a/backend/internal/service/message.go b/backend/internal/service/message.go new file mode 100644 index 00000000..5d51e815 --- /dev/null +++ b/backend/internal/service/message.go @@ -0,0 +1,122 @@ +package service + +import ( + "context" + "encoding/json" + "net/http" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" + "high-loud-apps-labs/internal/httpapi/dto" +) + +type MessageService struct { + messages domain.MessageRepository + chats domain.ChatRepository + idempotencyTTL time.Duration +} + +func NewMessageService( + messages domain.MessageRepository, + chats domain.ChatRepository, + idempotencyTTL time.Duration, +) *MessageService { + return &MessageService{ + messages: messages, + chats: chats, + idempotencyTTL: idempotencyTTL, + } +} + +func (s *MessageService) Create( + ctx context.Context, + senderID, chatID, text string, + attachmentIDs []string, + idempotencyKey, requestPath string, +) (*domain.CreateIdempotentResult, error) { + trimmed := strings.TrimSpace(text) + if trimmed == "" && len(attachmentIDs) == 0 { + return nil, &domain.ValidationError{ + Field: "text", + Message: "text или attachment_ids обязательны", + } + } + if utf8.RuneCountInString(text) > 4000 { + return nil, &domain.ValidationError{ + Field: "text", + Message: "не более 4000 символов", + } + } + if len(attachmentIDs) > 10 { + return nil, &domain.ValidationError{ + Field: "attachment_ids", + Message: "не более 10", + } + } + seen := make(map[string]struct{}, len(attachmentIDs)) + for _, id := range attachmentIDs { + if _, dup := seen[id]; dup { + return nil, &domain.ValidationError{ + Field: "attachment_ids", + Message: "дубликаты запрещены", + } + } + seen[id] = struct{}{} + } + + if err := s.ensureMember(ctx, senderID, chatID); err != nil { + return nil, err + } + + fresh := &domain.Message{ + ID: uuid.NewString(), + ChatID: chatID, + SenderID: senderID, + Text: text, + AttachmentIDs: append([]string(nil), attachmentIDs...), + CreatedAt: time.Now().UTC(), + } + + body, err := json.Marshal(dto.MessageFromDomain(fresh)) + if err != nil { + return nil, err + } + + return s.messages.CreateIdempotent( + ctx, + domain.IdempotencyScope{ + UserID: senderID, + Method: http.MethodPost, + Path: requestPath, + Key: idempotencyKey, + }, + domain.RequestHash(text, attachmentIDs), + fresh, + body, + s.idempotencyTTL, + ) +} + +func (s *MessageService) List(ctx context.Context, userID, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) { + if err := s.ensureMember(ctx, userID, chatID); err != nil { + return nil, nil, err + } + return s.messages.ListByChat(ctx, chatID, limit, cursor) +} + +func (s *MessageService) ensureMember(ctx context.Context, userID, chatID string) error { + c, err := s.chats.GetByID(ctx, chatID) + if err != nil { + return err + } + for _, m := range c.MemberIDs { + if m == userID { + return nil + } + } + return &domain.NotFoundError{Entity: "chat", ID: chatID} +} diff --git a/backend/internal/service/password.go b/backend/internal/service/password.go new file mode 100644 index 00000000..1f0659f1 --- /dev/null +++ b/backend/internal/service/password.go @@ -0,0 +1,112 @@ +package service + +import ( + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "errors" + "fmt" + "strings" + + "golang.org/x/crypto/argon2" +) + +// PasswordHasher хеширует пароли и проверяет пароль сохранённого хеша. +type PasswordHasher interface { + Hash(password string) (string, error) + Verify(password, encoded string) (bool, error) +} + +type Argon2idHasher struct { + memory uint32 + iterations uint32 + parallelism uint8 + saltLength uint32 + keyLength uint32 +} + +func NewArgon2idHasher() *Argon2idHasher { + return &Argon2idHasher{ + memory: 64 * 1024, + iterations: 3, + parallelism: 4, + saltLength: 16, + keyLength: 32, + } +} + +func (h *Argon2idHasher) Hash(password string) (string, error) { + salt := make([]byte, h.saltLength) + if _, err := rand.Read(salt); err != nil { + return "", fmt.Errorf("генерация соли: %w", err) + } + + key := argon2.IDKey( + []byte(password), + salt, + h.iterations, + h.memory, + h.parallelism, + h.keyLength, + ) + + return fmt.Sprintf( + "$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", + argon2.Version, + h.memory, + h.iterations, + h.parallelism, + base64.RawStdEncoding.EncodeToString(salt), + base64.RawStdEncoding.EncodeToString(key), + ), nil +} + +// Verify сравнивает пароль с сохранённым PHC-хешем. +// Возвращает false и nil при неверном пароле. +// Возвращает false и ошибку, если строка хеша повреждена или несовместима. +func (h *Argon2idHasher) Verify(password, encoded string) (bool, error) { + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false, errors.New("некорректный формат хеша") + } + + var version int + if n, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || n != 1 { + return false, errors.New("некорректная версия хеша") + } + if version != argon2.Version { + return false, errors.New("неподдерживаемая версия Argon2") + } + + var ( + memory uint32 + iterations uint32 + parallelism uint8 + ) + if n, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil || n != 3 { + return false, errors.New("некорректные параметры хеша") + } + + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil { + return false, errors.New("некорректная соль") + } + + wantKey, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil { + return false, errors.New("некорректный ключ") + } + + gotKey := argon2.IDKey( + []byte(password), + salt, + iterations, + memory, + parallelism, + uint32(len(wantKey)), + ) + + return subtle.ConstantTimeCompare(gotKey, wantKey) == 1, nil +} + +var _ PasswordHasher = (*Argon2idHasher)(nil) diff --git a/backend/internal/service/reconciler.go b/backend/internal/service/reconciler.go new file mode 100644 index 00000000..325b95d8 --- /dev/null +++ b/backend/internal/service/reconciler.go @@ -0,0 +1,176 @@ +package service + +import ( + "context" + "log" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +const advisoryLockKey int64 = 0x4D5455534904 + +type Reconciler struct { + attachments domain.AttachmentRepository + storage domain.ObjectStorage + pool *pgxpool.Pool + gracePeriod time.Duration + batchSize int +} + +func NewReconciler( + attachments domain.AttachmentRepository, + storage domain.ObjectStorage, + pool *pgxpool.Pool, + gracePeriod time.Duration, +) *Reconciler { + if gracePeriod <= 0 { + gracePeriod = time.Hour + } + return &Reconciler{ + attachments: attachments, + storage: storage, + pool: pool, + gracePeriod: gracePeriod, + batchSize: 100, + } +} + +func (r *Reconciler) Run(ctx context.Context, interval time.Duration) { + if interval <= 0 { + interval = 5 * time.Minute + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + + log.Printf("reconciler: started, interval=%s grace=%s", interval, r.gracePeriod) + + r.tick(ctx) + + for { + select { + case <-ctx.Done(): + log.Println("reconciler: stopped") + return + case <-ticker.C: + r.tick(ctx) + } + } +} + +func (r *Reconciler) tick(ctx context.Context) { + acquired, release, err := r.acquireLock(ctx) + if err != nil { + log.Printf("reconciler: acquire lock: %v", err) + return + } + if !acquired { + return + } + defer release() + + pendingDeleted := r.sweepPending(ctx) + orphansDeleted := r.sweepOrphans(ctx) + + if pendingDeleted > 0 || orphansDeleted > 0 { + log.Printf("reconciler: sweep done pending=%d orphans=%d", pendingDeleted, orphansDeleted) + } +} + +func (r *Reconciler) acquireLock(ctx context.Context) (bool, func(), error) { + conn, err := r.pool.Acquire(ctx) + if err != nil { + return false, nil, err + } + + var ok bool + if err := conn.QueryRow(ctx, `SELECT pg_try_advisory_lock($1)`, advisoryLockKey).Scan(&ok); err != nil { + conn.Release() + return false, nil, err + } + if !ok { + conn.Release() + return false, nil, nil + } + + release := func() { + unlockCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + if _, err := conn.Exec(unlockCtx, `SELECT pg_advisory_unlock($1)`, advisoryLockKey); err != nil { + log.Printf("reconciler: unlock: %v", err) + } + conn.Release() + } + return true, release, nil +} + +func (r *Reconciler) sweepPending(ctx context.Context) int { + cutoff := time.Now().UTC().Add(-r.gracePeriod) + stale, err := r.attachments.ListStalePending(ctx, cutoff, r.batchSize) + if err != nil { + log.Printf("reconciler: list stale pending: %v", err) + return 0 + } + + deleted := 0 + for _, a := range stale { + if err := r.storage.Delete(ctx, a.ObjectKey); err != nil { + log.Printf("reconciler: delete object %s: %v", a.ObjectKey, err) + continue + } + if err := r.attachments.DeletePending(ctx, a.ID); err != nil { + log.Printf("reconciler: delete pending row %s: %v", a.ID, err) + continue + } + deleted++ + } + return deleted +} + +func (r *Reconciler) sweepOrphans(ctx context.Context) int { + objects, err := r.storage.List(ctx, "chats/") + if err != nil { + log.Printf("reconciler: list objects: %v", err) + return 0 + } + if len(objects) == 0 { + return 0 + } + + cutoff := time.Now().UTC().Add(-r.gracePeriod) + keys := make([]string, 0, len(objects)) + for _, o := range objects { + if o.LastModified.Before(cutoff) { + keys = append(keys, o.Key) + } + } + if len(keys) == 0 { + return 0 + } + + existing, err := r.attachments.ExistingKeys(ctx, keys) + if err != nil { + log.Printf("reconciler: existing keys: %v", err) + return 0 + } + + inDB := make(map[string]struct{}, len(existing)) + for _, k := range existing { + inDB[k] = struct{}{} + } + + deleted := 0 + for _, k := range keys { + if _, ok := inDB[k]; ok { + continue + } + if err := r.storage.Delete(ctx, k); err != nil { + log.Printf("reconciler: delete orphan %s: %v", k, err) + continue + } + deleted++ + } + return deleted +} diff --git a/backend/internal/service/session.go b/backend/internal/service/session.go new file mode 100644 index 00000000..ee7216bf --- /dev/null +++ b/backend/internal/service/session.go @@ -0,0 +1,127 @@ +package service + +import ( + "context" + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "errors" + "fmt" + "time" + + "high-loud-apps-labs/internal/domain" +) + +// SessionService отвечает за вход, проверку и отзыв сессий. +type SessionService struct { + users domain.UserRepository + sessions domain.SessionRepository + hasher PasswordHasher + ttl time.Duration +} + +// NewSessionService создаёт сервис с заданным TTL сессий. +func NewSessionService( + users domain.UserRepository, + sessions domain.SessionRepository, + hasher PasswordHasher, + ttl time.Duration, +) *SessionService { + return &SessionService{ + users: users, + sessions: sessions, + hasher: hasher, + ttl: ttl, + } +} + +// Login проверяет username/password и создаёт новую сессию. +// Возвращает сессию, открытый токен и пользователя. +// Открытый токен уходит клиенту; в БД сохраняется только его SHA-256. +func (s *SessionService) Login(ctx context.Context, username, password string) (*domain.Session, string, *domain.User, error) { + user, err := s.users.GetByUsername(ctx, username) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + return nil, "", nil, &domain.UnauthorizedError{Reason: "неверные учётные данные"} + } + return nil, "", nil, err + } + + ok, err := s.hasher.Verify(password, user.PasswordHash) + if err != nil { + return nil, "", nil, err + } + if !ok { + return nil, "", nil, &domain.UnauthorizedError{Reason: "неверные учётные данные"} + } + + token, err := generateToken() + if err != nil { + return nil, "", nil, err + } + + tokenHash := sha256.Sum256([]byte(token)) + now := time.Now().UTC() + + session := &domain.Session{ + TokenHash: tokenHash[:], + UserID: user.ID, + ExpiresAt: now.Add(s.ttl), + CreatedAt: now, + } + if err := s.sessions.Create(ctx, session); err != nil { + return nil, "", nil, err + } + + return session, token, user, nil +} + +// Resolve проверяет токен и возвращает пользователя сессии. +// Истёкший, отозванный или неизвестный токен - UnauthorizedError по контракту +func (s *SessionService) Resolve(ctx context.Context, token string) (*domain.User, error) { + if token == "" { + return nil, &domain.UnauthorizedError{Reason: "пустой токен"} + } + + tokenHash := sha256.Sum256([]byte(token)) + + session, err := s.sessions.GetByTokenHash(ctx, tokenHash[:]) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + return nil, &domain.UnauthorizedError{Reason: "неизвестный токен"} + } + return nil, err + } + + if !session.ExpiresAt.After(time.Now().UTC()) { + return nil, &domain.UnauthorizedError{Reason: "сессия истекла"} + } + + user, err := s.users.GetByID(ctx, session.UserID) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + return nil, &domain.UnauthorizedError{Reason: "пользователь сессии не найден"} + } + return nil, err + } + + return user, nil +} + +// Revoke удаляет сессию по токену. +func (s *SessionService) Revoke(ctx context.Context, token string) error { + if token == "" { + return nil + } + tokenHash := sha256.Sum256([]byte(token)) + return s.sessions.DeleteByTokenHash(ctx, tokenHash[:]) +} + +// generateToken возвращает 32 случайных байта в base64url без паддинга=. +func generateToken() (string, error) { + buf := make([]byte, 32) + if _, err := rand.Read(buf); err != nil { + return "", fmt.Errorf("генерация токена: %w", err) + } + return base64.RawURLEncoding.EncodeToString(buf), nil +} diff --git a/backend/internal/service/user.go b/backend/internal/service/user.go new file mode 100644 index 00000000..e7b75479 --- /dev/null +++ b/backend/internal/service/user.go @@ -0,0 +1,78 @@ +package service + +import ( + "context" + "regexp" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" + + "high-loud-apps-labs/internal/domain" +) + +var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + +type UserService struct { + users domain.UserRepository + hasher PasswordHasher +} + +func NewUserService(users domain.UserRepository, hasher PasswordHasher) *UserService { + return &UserService{users: users, hasher: hasher} +} + +func (s *UserService) Create(ctx context.Context, username, displayName, password string) (*domain.User, error) { + if !usernamePattern.MatchString(username) { + return nil, &domain.ValidationError{ + Field: "username", + Message: "должен соответствовать ^[a-z0-9_]{3,32}$", + } + } + if strings.TrimSpace(displayName) == "" { + return nil, &domain.ValidationError{ + Field: "display_name", + Message: "не может состоять только из пробелов", + } + } + if utf8.RuneCountInString(displayName) > 100 { + return nil, &domain.ValidationError{ + Field: "display_name", + Message: "не более 100 символов", + } + } + + passwordRunes := utf8.RuneCountInString(password) + if passwordRunes < 12 || passwordRunes > 128 { + return nil, &domain.ValidationError{ + Field: "password", + Message: "длина 12–128 символов", + } + } + + hash, err := s.hasher.Hash(password) + if err != nil { + return nil, err + } + + u := &domain.User{ + ID: uuid.NewString(), + Username: username, + DisplayName: displayName, + PasswordHash: hash, + CreatedAt: time.Now().UTC(), + } + if err := s.users.Create(ctx, u); err != nil { + return nil, err + } + return u, nil +} + +func (s *UserService) GetByID(ctx context.Context, id string) (*domain.User, error) { + return s.users.GetByID(ctx, id) +} + +func (s *UserService) GetByUsername(ctx context.Context, username string) (*domain.User, error) { + return s.users.GetByUsername(ctx, username) +} diff --git a/backend/internal/storage/_legacy/memory/chat.go b/backend/internal/storage/_legacy/memory/chat.go new file mode 100644 index 00000000..a2e18d07 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/chat.go @@ -0,0 +1,102 @@ +package memory + +import ( + "context" + "slices" + + "high-loud-apps-labs/internal/domain" +) + +func (r *ChatRepo) Create(ctx context.Context, chat *domain.Chat) error { + if err := ctx.Err(); err != nil { + return err + } + r.mu.Lock() + defer r.mu.Unlock() + + stored := *chat + stored.MemberIDs = append([]string(nil), chat.MemberIDs...) + r.chats[chat.ID] = &stored + return nil +} + +func (r *ChatRepo) GetByID(ctx context.Context, id string) (*domain.Chat, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + c, ok := r.chats[id] + if !ok { + return nil, &domain.NotFoundError{Entity: "chat", ID: id} + } + copy := *c + copy.MemberIDs = append([]string(nil), c.MemberIDs...) + return ©, nil +} + +func (r *ChatRepo) ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) { + if err := ctx.Err(); err != nil { + return nil, nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + scope := "chats:" + userID + + all := make([]domain.Chat, 0) + for _, c := range r.chats { + for _, m := range c.MemberIDs { + if m == userID { + copy := *c + copy.MemberIDs = append([]string(nil), c.MemberIDs...) + all = append(all, copy) + break + } + } + } + + slices.SortFunc(all, func(a, b domain.Chat) int { + if lessThan(a.CreatedAt, a.ID, b.CreatedAt, b.ID) { + return -1 + } + if lessThan(b.CreatedAt, b.ID, a.CreatedAt, a.ID) { + return 1 + } + return 0 + }) + + start := 0 + if cursor != nil { + t, id, err := decodeCursor(*cursor, scope) + if err != nil { + return nil, nil, err + } + start = len(all) + for i, c := range all { + if lessThan(t, id, c.CreatedAt, c.ID) { + start = i + break + } + } + } + + if start >= len(all) { + return []domain.Chat{}, nil, nil + } + end := start + limit + if end > len(all) { + end = len(all) + } + page := all[start:end] + + var next *string + if end < len(all) { + last := page[len(page)-1] + c := encodeCursor(scope, last.CreatedAt, last.ID) + next = &c + } + + return page, next, nil +} diff --git a/backend/internal/storage/_legacy/memory/cursor.go b/backend/internal/storage/_legacy/memory/cursor.go new file mode 100644 index 00000000..ef0ebac9 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/cursor.go @@ -0,0 +1,47 @@ +package memory + +import ( + "encoding/base64" + "strings" + "time" + + "high-loud-apps-labs/internal/domain" +) + +func encodeCursor(scope string, createdAt time.Time, id string) string { + raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id + return base64.RawURLEncoding.EncodeToString([]byte(raw)) +} + +func decodeCursor(cursor, expectedScope string) (time.Time, string, error) { + data, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return time.Time{}, "", badCursor() + } + parts := strings.SplitN(string(data), "|", 3) + if len(parts) != 3 { + return time.Time{}, "", badCursor() + } + if parts[0] != expectedScope { + return time.Time{}, "", badCursor() + } + t, err := time.Parse(time.RFC3339Nano, parts[1]) + if err != nil { + return time.Time{}, "", badCursor() + } + return t, parts[2], nil +} + +func badCursor() error { + return &domain.ValidationError{ + Field: "cursor", + Message: "некорректный формат", + } +} + +func lessThan(aTime time.Time, aID string, bTime time.Time, bID string) bool { + if aTime.Equal(bTime) { + return aID < bID + } + return aTime.Before(bTime) +} diff --git a/backend/internal/storage/_legacy/memory/message.go b/backend/internal/storage/_legacy/memory/message.go new file mode 100644 index 00000000..463b7578 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/message.go @@ -0,0 +1,93 @@ +package memory + +import ( + "context" + "slices" + "time" + + "high-loud-apps-labs/internal/domain" +) + +func (r *MessageRepo) Create(ctx context.Context, message *domain.Message) error { + if err := ctx.Err(); err != nil { + return err + } + r.mu.Lock() + defer r.mu.Unlock() + + stored := *message + r.messages[message.ChatID] = append(r.messages[message.ChatID], stored) + return nil +} + +func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) { + if err := ctx.Err(); err != nil { + return nil, nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + scope := "messages:" + chatID + + var ( + afterTime time.Time + afterID string + hasCursor bool + ) + if cursor != nil { + t, id, err := decodeCursor(*cursor, scope) + if err != nil { + return nil, nil, err + } + afterTime = t + afterID = id + hasCursor = true + } + + src := r.messages[chatID] + if len(src) == 0 { + return []domain.Message{}, nil, nil + } + + all := make([]domain.Message, len(src)) + copy(all, src) + + slices.SortFunc(all, func(a, b domain.Message) int { + if lessThan(a.CreatedAt, a.ID, b.CreatedAt, b.ID) { + return -1 + } + if lessThan(b.CreatedAt, b.ID, a.CreatedAt, a.ID) { + return 1 + } + return 0 + }) + + start := 0 + if hasCursor { + start = len(all) + for i, m := range all { + if lessThan(afterTime, afterID, m.CreatedAt, m.ID) { + start = i + break + } + } + } + + if start >= len(all) { + return []domain.Message{}, nil, nil + } + end := start + limit + if end > len(all) { + end = len(all) + } + page := all[start:end] + + var next *string + if end < len(all) { + last := page[len(page)-1] + c := encodeCursor(scope, last.CreatedAt, last.ID) + next = &c + } + + return page, next, nil +} diff --git a/backend/internal/storage/_legacy/memory/storage.go b/backend/internal/storage/_legacy/memory/storage.go new file mode 100644 index 00000000..5b967732 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/storage.go @@ -0,0 +1,34 @@ +package memory + +import ( + "sync" + + "high-loud-apps-labs/internal/domain" +) + +type Storage struct { + mu sync.RWMutex + users map[string]*domain.User + chats map[string]*domain.Chat + messages map[string][]domain.Message +} + +func New() *Storage { + return &Storage{ + users: make(map[string]*domain.User), + chats: make(map[string]*domain.Chat), + messages: make(map[string][]domain.Message), + } +} + +func (s *Storage) Users() *UserRepo { return &UserRepo{s} } +func (s *Storage) Chats() *ChatRepo { return &ChatRepo{s} } +func (s *Storage) Messages() *MessageRepo { return &MessageRepo{s} } + +type UserRepo struct{ *Storage } +type ChatRepo struct{ *Storage } +type MessageRepo struct{ *Storage } + +var _ domain.UserRepository = (*UserRepo)(nil) +var _ domain.ChatRepository = (*ChatRepo)(nil) +var _ domain.MessageRepository = (*MessageRepo)(nil) diff --git a/backend/internal/storage/_legacy/memory/user.go b/backend/internal/storage/_legacy/memory/user.go new file mode 100644 index 00000000..713162b5 --- /dev/null +++ b/backend/internal/storage/_legacy/memory/user.go @@ -0,0 +1,59 @@ +package memory + +import ( + "context" + + "high-loud-apps-labs/internal/domain" +) + +func (r *UserRepo) Create(ctx context.Context, user *domain.User) error { + if err := ctx.Err(); err != nil { + return err + } + r.mu.Lock() + defer r.mu.Unlock() + + for _, u := range r.users { + if u.Username == user.Username { + return &domain.AlreadyExistsError{ + Entity: "user", + Field: "username", + Value: user.Username, + } + } + } + stored := *user + r.users[user.ID] = &stored + return nil +} + +func (r *UserRepo) GetByID(ctx context.Context, id string) (*domain.User, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + u, ok := r.users[id] + if !ok { + return nil, &domain.NotFoundError{Entity: "user", ID: id} + } + copy := *u + return ©, nil +} + +func (r *UserRepo) GetByUsername(ctx context.Context, username string) (*domain.User, error) { + if err := ctx.Err(); err != nil { + return nil, err + } + r.mu.RLock() + defer r.mu.RUnlock() + + for _, u := range r.users { + if u.Username == username { + copy := *u + return ©, nil + } + } + return nil, &domain.NotFoundError{Entity: "user", ID: username} +} diff --git a/backend/internal/storage/postgres/attachments.go b/backend/internal/storage/postgres/attachments.go new file mode 100644 index 00000000..915b1f13 --- /dev/null +++ b/backend/internal/storage/postgres/attachments.go @@ -0,0 +1,200 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" + "time" +) + +// pending -> ready (успешный upload) +// pending -> failed (отказ upload или reconciler) +type AttachmentRepo struct { + pool *pgxpool.Pool +} + +func NewAttachmentRepo(pool *pgxpool.Pool) *AttachmentRepo { + return &AttachmentRepo{pool: pool} +} + +func (r *AttachmentRepo) Create(ctx context.Context, a *domain.Attachment) error { + const q = ` + INSERT INTO attachments + (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5, $6, $7, $8, $9, NULL, $10, $10)` + _, err := r.pool.Exec(ctx, q, + a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes, + a.SHA256, a.ObjectKey, string(a.Status), a.CreatedAt, + ) + if err != nil { + if uniqueViolation(err) { + return &domain.AlreadyExistsError{ + Entity: "attachment", + Field: "object_key", + Value: a.ObjectKey, + } + } + if foreignKeyViolation(err) { + return &domain.NotFoundError{Entity: "chat", ID: a.ChatID} + } + if checkViolation(err) { + return &domain.ValidationError{ + Field: "attachment", + Message: "нарушено ограничение целостности", + } + } + return mapError(err) + } + return nil +} + +func (r *AttachmentRepo) GetByID(ctx context.Context, id string) (*domain.Attachment, error) { + const q = ` + SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at + FROM attachments + WHERE id = $1::uuid + ` + a := &domain.Attachment{} + var status string + err := r.pool.QueryRow(ctx, q, id). + Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, + &a.SizeBytes, &a.SHA256, &a.ObjectKey, &status, &a.ErrorCode, + &a.CreatedAt, &a.UpdatedAt) + if err != nil { + return nil, notFound(err, "attachment", id) + } + a.Status = domain.AttachmentStatus(status) + return a, nil +} + +// MarkReady переводит pending -> ready. Идемпотентна. +func (r *AttachmentRepo) MarkReady(ctx context.Context, id string) error { + const q = ` + UPDATE attachments + SET status = 'ready', updated_at = now() + WHERE id = $1::uuid AND status = 'pending' + ` + tag, err := r.pool.Exec(ctx, q, id) + if err != nil { + return mapError(err) + } + if tag.RowsAffected() > 0 { + return nil + } + + var status string + err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id). + Scan(&status) + if err != nil { + return notFound(err, "attachment", id) + } + switch domain.AttachmentStatus(status) { + case domain.AttachmentStatusReady: + return nil // idempotent no-op + case domain.AttachmentStatusFailed: + return &domain.ConflictError{Reason: "нельзя перевести failed-вложение в ready"} + default: + return &domain.ConflictError{Reason: "некорректное состояние вложения"} + } +} + +// MarkFailed переводит pending -> failed. Идемпотентна. +func (r *AttachmentRepo) MarkFailed(ctx context.Context, id, errorCode string) error { + if errorCode == "" { + return &domain.ValidationError{Field: "error_code", Message: "не может быть пустым"} + } + const q = ` + UPDATE attachments + SET status = 'failed', error_code = $2, updated_at = now() + WHERE id = $1::uuid AND status = 'pending' + ` + tag, err := r.pool.Exec(ctx, q, id, errorCode) + if err != nil { + return mapError(err) + } + if tag.RowsAffected() > 0 { + return nil + } + + var status string + err = r.pool.QueryRow(ctx, `SELECT status FROM attachments WHERE id = $1::uuid`, id). + Scan(&status) + if err != nil { + return notFound(err, "attachment", id) + } + switch domain.AttachmentStatus(status) { + case domain.AttachmentStatusFailed, domain.AttachmentStatusReady: + return nil + default: + return &domain.ConflictError{Reason: "некорректное состояние вложения"} + } +} + +func (r *AttachmentRepo) ListStalePending(ctx context.Context, olderThan time.Time, limit int) ([]domain.Attachment, error) { + if limit <= 0 { + return []domain.Attachment{}, nil + } + const q = ` + SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + object_key, status, error_code, created_at, updated_at + FROM attachments + WHERE status = 'pending' AND created_at < $1 + ORDER BY created_at + LIMIT $2 + ` + rows, err := r.pool.Query(ctx, q, olderThan, limit) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + out := make([]domain.Attachment, 0, limit) + for rows.Next() { + var a domain.Attachment + var status string + if err := rows.Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, + &a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey, + &status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt); err != nil { + return nil, mapError(err) + } + a.Status = domain.AttachmentStatus(status) + out = append(out, a) + } + return out, mapError(rows.Err()) +} + +func (r *AttachmentRepo) DeletePending(ctx context.Context, id string) error { + const q = `DELETE FROM attachments WHERE id = $1::uuid AND status = 'pending'` + if _, err := r.pool.Exec(ctx, q, id); err != nil { + return mapError(err) + } + return nil +} + +func (r *AttachmentRepo) ExistingKeys(ctx context.Context, keys []string) ([]string, error) { + if len(keys) == 0 { + return []string{}, nil + } + const q = `SELECT object_key FROM attachments WHERE object_key = ANY($1::text[])` + rows, err := r.pool.Query(ctx, q, keys) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + out := make([]string, 0, len(keys)) + for rows.Next() { + var k string + if err := rows.Scan(&k); err != nil { + return nil, mapError(err) + } + out = append(out, k) + } + return out, mapError(rows.Err()) +} + +var _ domain.AttachmentRepository = (*AttachmentRepo)(nil) diff --git a/backend/internal/storage/postgres/chat.go b/backend/internal/storage/postgres/chat.go new file mode 100644 index 00000000..8afc6f29 --- /dev/null +++ b/backend/internal/storage/postgres/chat.go @@ -0,0 +1,195 @@ +package postgres + +import ( + "context" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type ChatRepo struct { + pool *pgxpool.Pool +} + +func NewChatRepo(pool *pgxpool.Pool) *ChatRepo { + return &ChatRepo{pool: pool} +} + +// Create атомарно создаёт чат и его участников. +func (r *ChatRepo) Create(ctx context.Context, chat *domain.Chat) error { + tx, err := r.pool.Begin(ctx) + if err != nil { + return mapError(err) + } + defer func() { _ = tx.Rollback(ctx) }() + + const qChat = `INSERT INTO chats (id, title, created_at) VALUES ($1::uuid, $2, $3)` + if _, err := tx.Exec(ctx, qChat, chat.ID, chat.Title, chat.CreatedAt); err != nil { + if checkViolation(err) { + return &domain.ValidationError{Field: "chat", Message: "нарушено ограничение целостности"} + } + return mapError(err) + } + + const qMember = `INSERT INTO chat_members (chat_id, user_id) VALUES ($1::uuid, $2::uuid)` + for _, uid := range chat.MemberIDs { + if _, err := tx.Exec(ctx, qMember, chat.ID, uid); err != nil { + if foreignKeyViolation(err) { + return &domain.ValidationError{ + Field: "member_ids", + Message: "неизвестный участник " + uid, + } + } + if uniqueViolation(err) { + return &domain.ValidationError{ + Field: "member_ids", + Message: "дубликаты запрещены", + } + } + return mapError(err) + } + } + + return mapError(tx.Commit(ctx)) +} + +func (r *ChatRepo) GetByID(ctx context.Context, id string) (*domain.Chat, error) { + const q = `SELECT id, title, created_at FROM chats WHERE id = $1::uuid` + c := &domain.Chat{} + err := r.pool.QueryRow(ctx, q, id).Scan(&c.ID, &c.Title, &c.CreatedAt) + if err != nil { + return nil, notFound(err, "chat", id) + } + members, err := r.loadMembers(ctx, []string{c.ID}) + if err != nil { + return nil, err + } + c.MemberIDs = members[c.ID] + if c.MemberIDs == nil { + c.MemberIDs = []string{} + } + return c, nil +} + +func (r *ChatRepo) ListByMember(ctx context.Context, userID string, limit int, cursor *string) ([]domain.Chat, *string, error) { + scope := "chats:" + userID + + var ( + afterTime time.Time + afterID string + hasCursor bool + ) + if cursor != nil { + t, id, err := decodeCursor(*cursor, scope) + if err != nil { + return nil, nil, err + } + afterTime = t + afterID = id + hasCursor = true + } + + var ( + rows pgx.Rows + err error + ) + if hasCursor { + const q = ` + SELECT c.id, c.title, c.created_at + FROM chats c + JOIN chat_members m ON m.chat_id = c.id + WHERE m.user_id = $1::uuid + AND (c.created_at, c.id) > ($2, $3::uuid) + ORDER BY c.created_at, c.id + LIMIT $4 + ` + rows, err = r.pool.Query(ctx, q, userID, afterTime, afterID, limit+1) + } else { + const q = ` + SELECT c.id, c.title, c.created_at + FROM chats c + JOIN chat_members m ON m.chat_id = c.id + WHERE m.user_id = $1::uuid + ORDER BY c.created_at, c.id + LIMIT $2 + ` + rows, err = r.pool.Query(ctx, q, userID, limit+1) + } + if err != nil { + return nil, nil, mapError(err) + } + defer rows.Close() + + page := make([]domain.Chat, 0, limit) + for rows.Next() { + var c domain.Chat + if err := rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil { + return nil, nil, mapError(err) + } + page = append(page, c) + } + if err := rows.Err(); err != nil { + return nil, nil, mapError(err) + } + + var next *string + if len(page) > limit { + page = page[:limit] + last := page[len(page)-1] + c := encodeCursor(scope, last.CreatedAt, last.ID) + next = &c + } + + if len(page) > 0 { + ids := make([]string, len(page)) + for i := range page { + ids[i] = page[i].ID + } + members, err := r.loadMembers(ctx, ids) + if err != nil { + return nil, nil, err + } + for i := range page { + if m, ok := members[page[i].ID]; ok { + page[i].MemberIDs = m + } else { + page[i].MemberIDs = []string{} + } + } + } + + return page, next, nil +} + +// loadMembers одним запросом собирает участников для указанных чатов. +func (r *ChatRepo) loadMembers(ctx context.Context, chatIDs []string) (map[string][]string, error) { + if len(chatIDs) == 0 { + return map[string][]string{}, nil + } + const q = ` + SELECT chat_id, user_id + FROM chat_members + WHERE chat_id = ANY($1::uuid[]) + ORDER BY chat_id, user_id + ` + rows, err := r.pool.Query(ctx, q, chatIDs) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + result := make(map[string][]string, len(chatIDs)) + for rows.Next() { + var chatID, userID string + if err := rows.Scan(&chatID, &userID); err != nil { + return nil, mapError(err) + } + result[chatID] = append(result[chatID], userID) + } + return result, mapError(rows.Err()) +} + +var _ domain.ChatRepository = (*ChatRepo)(nil) diff --git a/backend/internal/storage/postgres/cursor.go b/backend/internal/storage/postgres/cursor.go new file mode 100644 index 00000000..044ffeea --- /dev/null +++ b/backend/internal/storage/postgres/cursor.go @@ -0,0 +1,46 @@ +package postgres + +import ( + "encoding/base64" + "strings" + "time" + + "high-loud-apps-labs/internal/domain" + + "github.com/google/uuid" +) + +// Взял за основу что в inmemery делал +func encodeCursor(scope string, createdAt time.Time, id string) string { + raw := scope + "|" + createdAt.UTC().Format(time.RFC3339Nano) + "|" + id + return base64.RawURLEncoding.EncodeToString([]byte(raw)) +} + +func decodeCursor(cursor, expectedScope string) (time.Time, string, error) { + data, err := base64.RawURLEncoding.DecodeString(cursor) + if err != nil { + return time.Time{}, "", badCursor() + } + parts := strings.SplitN(string(data), "|", 3) + if len(parts) != 3 { + return time.Time{}, "", badCursor() + } + if parts[0] != expectedScope { + return time.Time{}, "", badCursor() + } + t, err := time.Parse(time.RFC3339Nano, parts[1]) + if err != nil { + return time.Time{}, "", badCursor() + } + if _, err := uuid.Parse(parts[2]); err != nil { + return time.Time{}, "", badCursor() + } + return t, parts[2], nil +} + +func badCursor() error { + return &domain.ValidationError{ + Field: "cursor", + Message: "некорректный формат", + } +} diff --git a/backend/internal/storage/postgres/db.go b/backend/internal/storage/postgres/db.go new file mode 100644 index 00000000..ad27e83e --- /dev/null +++ b/backend/internal/storage/postgres/db.go @@ -0,0 +1,75 @@ +package postgres + +import ( + "context" + "fmt" + "time" + + "github.com/jackc/pgx/v5/pgxpool" + "github.com/jackc/pgx/v5/stdlib" + "github.com/pressly/goose/v3" + + "high-loud-apps-labs/migrations" +) + +type Config struct { + URL string + MaxConns int32 + MinConns int32 + MaxConnLifetime time.Duration + MaxConnIdleTime time.Duration + ConnectTimeout time.Duration +} + +func Connect(ctx context.Context, cfg Config) (*pgxpool.Pool, error) { + if cfg.URL == "" { + return nil, fmt.Errorf("DATABASE_URL пустой") + } + + poolCfg, err := pgxpool.ParseConfig(cfg.URL) + if err != nil { + return nil, fmt.Errorf("разбор DATABASE_URL: %w", err) + } + if cfg.MaxConns > 0 { + poolCfg.MaxConns = cfg.MaxConns + } + if cfg.MinConns > 0 { + poolCfg.MinConns = cfg.MinConns + } + if cfg.MaxConnLifetime > 0 { + poolCfg.MaxConnLifetime = cfg.MaxConnLifetime + } + if cfg.MaxConnIdleTime > 0 { + poolCfg.MaxConnIdleTime = cfg.MaxConnIdleTime + } + if cfg.ConnectTimeout > 0 { + poolCfg.ConnConfig.ConnectTimeout = cfg.ConnectTimeout + } + poolCfg.HealthCheckPeriod = 10 * time.Second + + pool, err := pgxpool.NewWithConfig(ctx, poolCfg) + if err != nil { + return nil, fmt.Errorf("создание пула: %w", err) + } + + pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + if err := pool.Ping(pingCtx); err != nil { + pool.Close() + return nil, fmt.Errorf("ping БД: %w", err) + } + return pool, nil +} + +func RunMigrations(ctx context.Context, pool *pgxpool.Pool) error { + db := stdlib.OpenDBFromPool(pool) + + goose.SetBaseFS(migrations.FS) + if err := goose.SetDialect("postgres"); err != nil { + return fmt.Errorf("goose dialect: %w", err) + } + if err := goose.UpContext(ctx, db, "."); err != nil { + return fmt.Errorf("применение миграций: %w", err) + } + return nil +} diff --git a/backend/internal/storage/postgres/errors.go b/backend/internal/storage/postgres/errors.go new file mode 100644 index 00000000..63861286 --- /dev/null +++ b/backend/internal/storage/postgres/errors.go @@ -0,0 +1,71 @@ +package postgres + +import ( + "context" + "errors" + + "github.com/jackc/pgerrcode" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + + "high-loud-apps-labs/internal/domain" +) + +type coded interface { + Code() domain.ErrorCode +} + +// mapError переводит ошибки драйвера в доменные. +func mapError(err error) error { + if err == nil { + return nil + } + var c coded + if errors.As(err, &c) { + return err + } + if errors.Is(err, pgx.ErrNoRows) { + return err + } + if errors.Is(err, context.DeadlineExceeded) { + return &domain.UnavailableError{Reason: "превышено время ожидания БД"} + } + if errors.Is(err, context.Canceled) { + return &domain.UnavailableError{Reason: "запрос отменён"} + } + return &domain.UnavailableError{Reason: err.Error()} +} + +func notFound(err error, entity, id string) error { + if err == nil { + return nil + } + if errors.Is(err, pgx.ErrNoRows) { + return &domain.NotFoundError{Entity: entity, ID: id} + } + return mapError(err) +} + +func uniqueViolation(err error) bool { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + return pgErr.Code == pgerrcode.UniqueViolation + } + return false +} + +func foreignKeyViolation(err error) bool { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + return pgErr.Code == pgerrcode.ForeignKeyViolation + } + return false +} + +func checkViolation(err error) bool { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + return pgErr.Code == pgerrcode.CheckViolation + } + return false +} diff --git a/backend/internal/storage/postgres/message.go b/backend/internal/storage/postgres/message.go new file mode 100644 index 00000000..67df6cd0 --- /dev/null +++ b/backend/internal/storage/postgres/message.go @@ -0,0 +1,321 @@ +package postgres + +import ( + "bytes" + "context" + "net/http" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type MessageRepo struct { + pool *pgxpool.Pool +} + +func NewMessageRepo(pool *pgxpool.Pool) *MessageRepo { + return &MessageRepo{pool: pool} +} + +func (r *MessageRepo) Create(ctx context.Context, m *domain.Message) error { + const q = ` + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5) + ` + _, err := r.pool.Exec(ctx, q, m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt) + if err != nil { + if foreignKeyViolation(err) { + return &domain.NotFoundError{Entity: "chat", ID: m.ChatID} + } + if checkViolation(err) { + return &domain.ValidationError{Field: "text", Message: "нарушено ограничение"} + } + return mapError(err) + } + return nil +} + +func (r *MessageRepo) CreateIdempotent( + ctx context.Context, + scope domain.IdempotencyScope, + requestHash []byte, + fresh *domain.Message, + freshResponse []byte, + ttl time.Duration, +) (*domain.CreateIdempotentResult, error) { + tx, err := r.pool.Begin(ctx) + if err != nil { + return nil, mapError(err) + } + defer func() { _ = tx.Rollback(ctx) }() + + // 1) Убираем просроченную запись, чтобы ON CONFLICT сработал + // только на живой, а не на истёкшей строке. + if _, err := tx.Exec(ctx, ` + DELETE FROM idempotency_keys + WHERE user_id = $1::uuid AND method = $2 AND path = $3 AND key = $4 + AND expires_at <= now() + `, scope.UserID, scope.Method, scope.Path, scope.Key); err != nil { + return nil, mapError(err) + } + + // 2) Резервируем слот. Если параллельная реплика уже вставила — + // Postgres заблокирует конфликтный INSERT и мы увидим RowsAffected=0 + // только после того, как конкурент закоммитит или откатится. + expiresAt := time.Now().UTC().Add(ttl) + tag, err := tx.Exec(ctx, ` + INSERT INTO idempotency_keys + (user_id, method, path, key, request_hash, response_status, response_body, expires_at) + VALUES ($1::uuid, $2, $3, $4, $5, $6, $7, $8) + ON CONFLICT (user_id, method, path, key) DO NOTHING + `, scope.UserID, scope.Method, scope.Path, scope.Key, + requestHash, http.StatusCreated, freshResponse, expiresAt) + if err != nil { + return nil, mapError(err) + } + + // 3) Слот занят — читаем сохранённый ответ в этой же транзакции. + if tag.RowsAffected() == 0 { + var ( + storedHash []byte + storedStatus int + storedBody []byte + ) + err := tx.QueryRow(ctx, ` + SELECT request_hash, response_status, response_body + FROM idempotency_keys + WHERE user_id = $1::uuid AND method = $2 AND path = $3 AND key = $4 + `, scope.UserID, scope.Method, scope.Path, scope.Key). + Scan(&storedHash, &storedStatus, &storedBody) + if err != nil { + return nil, mapError(err) + } + + if !bytes.Equal(storedHash, requestHash) { + return nil, &domain.IdempotencyConflictError{Key: scope.Key} + } + + if err := tx.Commit(ctx); err != nil { + return nil, mapError(err) + } + return &domain.CreateIdempotentResult{ + Status: storedStatus, + Body: storedBody, + Replayed: true, + }, nil + } + + // 4) Слот наш — валидируем вложения, вставляем сообщение, линкуем + // вложения. Всё в одной транзакции: при ошибке на любом шаге + // откатывается и idempotency-ключ, и сообщение, и связи. + if err := r.validateAttachments(ctx, tx, fresh.ChatID, fresh.AttachmentIDs); err != nil { + return nil, err + } + + if _, err := tx.Exec(ctx, ` + INSERT INTO messages (id, chat_id, sender_id, text, created_at) + VALUES ($1::uuid, $2::uuid, $3::uuid, $4, $5) + `, fresh.ID, fresh.ChatID, fresh.SenderID, fresh.Text, fresh.CreatedAt); err != nil { + if foreignKeyViolation(err) { + return nil, &domain.NotFoundError{Entity: "chat", ID: fresh.ChatID} + } + if checkViolation(err) { + return nil, &domain.ValidationError{Field: "text", Message: "нарушено ограничение"} + } + return nil, mapError(err) + } + + if err := r.linkAttachments(ctx, tx, fresh.ID, fresh.AttachmentIDs); err != nil { + return nil, err + } + + if err := tx.Commit(ctx); err != nil { + return nil, mapError(err) + } + return &domain.CreateIdempotentResult{ + Status: http.StatusCreated, + Body: freshResponse, + Replayed: false, + }, nil +} + +func (r *MessageRepo) ListByChat(ctx context.Context, chatID string, limit int, cursor *string) ([]domain.Message, *string, error) { + scope := "messages:" + chatID + + var ( + afterTime time.Time + afterID string + hasCursor bool + ) + if cursor != nil { + t, id, err := decodeCursor(*cursor, scope) + if err != nil { + return nil, nil, err + } + afterTime = t + afterID = id + hasCursor = true + } + + var ( + rows pgx.Rows + err error + ) + if hasCursor { + const q = ` + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = $1::uuid + AND (created_at, id) > ($2, $3::uuid) + ORDER BY created_at, id + LIMIT $4 + ` + rows, err = r.pool.Query(ctx, q, chatID, afterTime, afterID, limit+1) + } else { + const q = ` + SELECT id, chat_id, sender_id, text, created_at + FROM messages + WHERE chat_id = $1::uuid + ORDER BY created_at, id + LIMIT $2 + ` + rows, err = r.pool.Query(ctx, q, chatID, limit+1) + } + if err != nil { + return nil, nil, err + } + defer rows.Close() + + page := make([]domain.Message, 0, limit) + for rows.Next() { + var m domain.Message + if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt); err != nil { + return nil, nil, err + } + page = append(page, m) + } + if err := rows.Err(); err != nil { + return nil, nil, err + } + + var next *string + if len(page) > limit { + page = page[:limit] + last := page[len(page)-1] + c := encodeCursor(scope, last.CreatedAt, last.ID) + next = &c + } + + if len(page) > 0 { + msgIDs := make([]string, len(page)) + for i := range page { + msgIDs[i] = page[i].ID + } + links, err := r.loadAttachmentIDs(ctx, msgIDs) + if err != nil { + return nil, nil, err + } + for i := range page { + if ids, ok := links[page[i].ID]; ok { + page[i].AttachmentIDs = ids + } else { + page[i].AttachmentIDs = []string{} + } + } + } + + return page, next, nil +} + +func (r *MessageRepo) validateAttachments(ctx context.Context, tx pgx.Tx, chatID string, ids []string) error { + if len(ids) == 0 { + return nil + } + const q = `SELECT id, chat_id, status FROM attachments WHERE id = ANY($1::uuid[])` + rows, err := tx.Query(ctx, q, ids) + if err != nil { + return mapError(err) + } + defer rows.Close() + + type info struct { + chatID string + status domain.AttachmentStatus + } + found := make(map[string]info, len(ids)) + for rows.Next() { + var id, chID, st string + if err := rows.Scan(&id, &chID, &st); err != nil { + return mapError(err) + } + found[id] = info{chatID: chID, status: domain.AttachmentStatus(st)} + } + if err := rows.Err(); err != nil { + return mapError(err) + } + + for _, id := range ids { + a, ok := found[id] + if !ok || a.chatID != chatID { + return &domain.NotFoundError{Entity: "attachment", ID: id} + } + if a.status != domain.AttachmentStatusReady { + return &domain.ConflictError{Reason: "вложение " + id + " не готово"} + } + } + return nil +} + +func (r *MessageRepo) linkAttachments(ctx context.Context, tx pgx.Tx, messageID string, ids []string) error { + if len(ids) == 0 { + return nil + } + const q = `INSERT INTO message_attachments (message_id, attachment_id, position) VALUES ($1::uuid, $2::uuid, $3)` + for i, id := range ids { + if _, err := tx.Exec(ctx, q, messageID, id, int16(i)); err != nil { + if foreignKeyViolation(err) { + return &domain.NotFoundError{Entity: "attachment", ID: id} + } + if uniqueViolation(err) { + return &domain.ValidationError{Field: "attachment_ids", Message: "дубликаты запрещены"} + } + if checkViolation(err) { + return &domain.ValidationError{Field: "attachment_ids", Message: "нарушено ограничение"} + } + return mapError(err) + } + } + return nil +} + +func (r *MessageRepo) loadAttachmentIDs(ctx context.Context, messageIDs []string) (map[string][]string, error) { + if len(messageIDs) == 0 { + return map[string][]string{}, nil + } + const q = ` + SELECT message_id, attachment_id + FROM message_attachments + WHERE message_id = ANY($1::uuid[]) + ORDER BY message_id, position + ` + rows, err := r.pool.Query(ctx, q, messageIDs) + if err != nil { + return nil, mapError(err) + } + defer rows.Close() + + result := make(map[string][]string, len(messageIDs)) + for rows.Next() { + var msgID, attID string + if err := rows.Scan(&msgID, &attID); err != nil { + return nil, mapError(err) + } + result[msgID] = append(result[msgID], attID) + } + return result, mapError(rows.Err()) +} + +var _ domain.MessageRepository = (*MessageRepo)(nil) diff --git a/backend/internal/storage/postgres/session.go b/backend/internal/storage/postgres/session.go new file mode 100644 index 00000000..430a9e6f --- /dev/null +++ b/backend/internal/storage/postgres/session.go @@ -0,0 +1,59 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type SessionRepo struct { + pool *pgxpool.Pool +} + +func NewSessionRepo(pool *pgxpool.Pool) *SessionRepo { + return &SessionRepo{pool: pool} +} + +func (r *SessionRepo) Create(ctx context.Context, s *domain.Session) error { + const q = ` + INSERT INTO sessions (token_hash, user_id, expires_at, created_at) + VALUES ($1, $2::uuid, $3, $4) + ` + _, err := r.pool.Exec(ctx, q, s.TokenHash, s.UserID, s.ExpiresAt, s.CreatedAt) + if err != nil { + if uniqueViolation(err) { + return &domain.AlreadyExistsError{ + Entity: "session", + Field: "token_hash", + Value: "", + } + } + return mapError(err) + } + return nil +} + +func (r *SessionRepo) GetByTokenHash(ctx context.Context, tokenHash []byte) (*domain.Session, error) { + const q = ` + SELECT token_hash, user_id, expires_at, created_at + FROM sessions + WHERE token_hash = $1 + ` + s := &domain.Session{} + err := r.pool.QueryRow(ctx, q, tokenHash). + Scan(&s.TokenHash, &s.UserID, &s.ExpiresAt, &s.CreatedAt) + if err != nil { + return nil, notFound(err, "session", "") + } + return s, nil +} + +func (r *SessionRepo) DeleteByTokenHash(ctx context.Context, tokenHash []byte) error { + const q = `DELETE FROM sessions WHERE token_hash = $1` + _, err := r.pool.Exec(ctx, q, tokenHash) + return mapError(err) +} + +var _ domain.SessionRepository = (*SessionRepo)(nil) diff --git a/backend/internal/storage/postgres/storage.go b/backend/internal/storage/postgres/storage.go new file mode 100644 index 00000000..07121091 --- /dev/null +++ b/backend/internal/storage/postgres/storage.go @@ -0,0 +1,19 @@ +package postgres + +import "github.com/jackc/pgx/v5/pgxpool" + +type Storage struct { + pool *pgxpool.Pool +} + +func New(pool *pgxpool.Pool) *Storage { + return &Storage{pool: pool} +} + +func (s *Storage) Pool() *pgxpool.Pool { return s.pool } + +func (s *Storage) Users() *UserRepo { return &UserRepo{pool: s.pool} } +func (s *Storage) Chats() *ChatRepo { return &ChatRepo{pool: s.pool} } +func (s *Storage) Messages() *MessageRepo { return &MessageRepo{pool: s.pool} } +func (s *Storage) Sessions() *SessionRepo { return &SessionRepo{pool: s.pool} } +func (s *Storage) Attachments() *AttachmentRepo { return &AttachmentRepo{pool: s.pool} } diff --git a/backend/internal/storage/postgres/user.go b/backend/internal/storage/postgres/user.go new file mode 100644 index 00000000..418f35a6 --- /dev/null +++ b/backend/internal/storage/postgres/user.go @@ -0,0 +1,76 @@ +package postgres + +import ( + "context" + + "github.com/jackc/pgx/v5/pgxpool" + + "high-loud-apps-labs/internal/domain" +) + +type UserRepo struct { + pool *pgxpool.Pool +} + +func NewUserRepo(pool *pgxpool.Pool) *UserRepo { + return &UserRepo{pool: pool} +} + +func (r *UserRepo) Create(ctx context.Context, user *domain.User) error { + const q = ` + INSERT INTO users (id, username, display_name, password_hash, created_at) + VALUES ($1::uuid, $2, $3, $4, $5) + ` + _, err := r.pool.Exec(ctx, q, + user.ID, user.Username, user.DisplayName, user.PasswordHash, user.CreatedAt, + ) + if err != nil { + if uniqueViolation(err) { + return &domain.AlreadyExistsError{ + Entity: "user", + Field: "username", + Value: user.Username, + } + } + if checkViolation(err) { + return &domain.ValidationError{ + Field: "user", + Message: "нарушено ограничение целостности", + } + } + return mapError(err) + } + return nil +} + +func (r *UserRepo) GetByID(ctx context.Context, id string) (*domain.User, error) { + const q = ` + SELECT id, username, display_name, password_hash, created_at + FROM users + WHERE id = $1::uuid + ` + u := &domain.User{} + err := r.pool.QueryRow(ctx, q, id). + Scan(&u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt) + if err != nil { + return nil, notFound(err, "user", id) + } + return u, nil +} + +func (r *UserRepo) GetByUsername(ctx context.Context, username string) (*domain.User, error) { + const q = ` + SELECT id, username, display_name, password_hash, created_at + FROM users + WHERE username = $1 + ` + u := &domain.User{} + err := r.pool.QueryRow(ctx, q, username). + Scan(&u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt) + if err != nil { + return nil, notFound(err, "user", username) + } + return u, nil +} + +var _ domain.UserRepository = (*UserRepo)(nil) diff --git a/backend/internal/storage/redis/client.go b/backend/internal/storage/redis/client.go new file mode 100644 index 00000000..0da66143 --- /dev/null +++ b/backend/internal/storage/redis/client.go @@ -0,0 +1,33 @@ +package redis + +import ( + "context" + "fmt" + "time" + + "github.com/redis/go-redis/v9" +) + +func NewClient(url string) (*redis.Client, error) { + opt, err := redis.ParseURL(url) + if err != nil { + return nil, fmt.Errorf("разбор URL: %w", err) + } + + opt.DialTimeout = 700 * time.Millisecond + opt.ReadTimeout = 700 * time.Millisecond + opt.WriteTimeout = 700 * time.Millisecond + opt.MaxRetries = 1 + opt.PoolSize = 10 + opt.ConnMaxIdleTime = time.Minute + + client := redis.NewClient(opt) + + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + if err := client.Ping(ctx).Err(); err != nil { + _ = client.Close() + return nil, fmt.Errorf("ping: %w", err) + } + return client, nil +} diff --git a/backend/internal/storage/redis/errors.go b/backend/internal/storage/redis/errors.go new file mode 100644 index 00000000..55e34226 --- /dev/null +++ b/backend/internal/storage/redis/errors.go @@ -0,0 +1,31 @@ +package redis + +import ( + "context" + "errors" + + "high-loud-apps-labs/internal/domain" +) + +type coded interface { + Code() domain.ErrorCode +} + +// mapError переводит ошибки драйвера в доменные. +// Любая недоступность redis (dial/read/write/context) → UnavailableError (503). +func mapError(err error) error { + if err == nil { + return nil + } + var c coded + if errors.As(err, &c) { + return err + } + if errors.Is(err, context.DeadlineExceeded) { + return &domain.UnavailableError{Reason: "превышено время ожидания redis"} + } + if errors.Is(err, context.Canceled) { + return &domain.UnavailableError{Reason: "запрос отменён"} + } + return &domain.UnavailableError{Reason: err.Error()} +} diff --git a/backend/internal/storage/redis/session.go b/backend/internal/storage/redis/session.go new file mode 100644 index 00000000..0aecfd59 --- /dev/null +++ b/backend/internal/storage/redis/session.go @@ -0,0 +1,80 @@ +package redis + +import ( + "context" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "time" + + "github.com/redis/go-redis/v9" + + "high-loud-apps-labs/internal/domain" +) + +const sessionKeyPrefix = "session:" + +type SessionRepo struct { + client *redis.Client +} + +func NewSessionRepo(client *redis.Client) *SessionRepo { + return &SessionRepo{client: client} +} + +type sessionValue struct { + UserID string `json:"user_id"` + ExpiresAt time.Time `json:"expires_at"` + CreatedAt time.Time `json:"created_at"` +} + +func (r *SessionRepo) key(tokenHash []byte) string { + return sessionKeyPrefix + hex.EncodeToString(tokenHash) +} + +func (r *SessionRepo) Create(ctx context.Context, s *domain.Session) error { + ttl := time.Until(s.ExpiresAt) + if ttl <= 0 { + return nil + } + + data, err := json.Marshal(sessionValue{ + UserID: s.UserID, + ExpiresAt: s.ExpiresAt.UTC(), + CreatedAt: s.CreatedAt.UTC(), + }) + if err != nil { + return fmt.Errorf("сериализация сессии: %w", err) + } + + return mapError(r.client.Set(ctx, r.key(s.TokenHash), data, ttl).Err()) +} + +func (r *SessionRepo) GetByTokenHash(ctx context.Context, tokenHash []byte) (*domain.Session, error) { + data, err := r.client.Get(ctx, r.key(tokenHash)).Bytes() + if err != nil { + if errors.Is(err, redis.Nil) { + return nil, &domain.NotFoundError{Entity: "session"} + } + return nil, mapError(err) + } + + var sv sessionValue + if err := json.Unmarshal(data, &sv); err != nil { + return nil, fmt.Errorf("разбор сессии: %w", err) + } + + return &domain.Session{ + TokenHash: tokenHash, + UserID: sv.UserID, + ExpiresAt: sv.ExpiresAt, + CreatedAt: sv.CreatedAt, + }, nil +} + +func (r *SessionRepo) DeleteByTokenHash(ctx context.Context, tokenHash []byte) error { + return mapError(r.client.Del(ctx, r.key(tokenHash)).Err()) +} + +var _ domain.SessionRepository = (*SessionRepo)(nil) diff --git a/backend/internal/storage/s3/client.go b/backend/internal/storage/s3/client.go new file mode 100644 index 00000000..75cbf954 --- /dev/null +++ b/backend/internal/storage/s3/client.go @@ -0,0 +1,98 @@ +package s3 + +import ( + "context" + "io" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" + + "high-loud-apps-labs/internal/domain" +) + +type Config struct { + Endpoint string + Bucket string + AccessKey string + SecretKey string + UseSSL bool + Region string +} + +type MinioStorage struct { + client *minio.Client + bucket string +} + +func New(ctx context.Context, cfg Config) (*MinioStorage, error) { + client, err := minio.New(cfg.Endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(cfg.AccessKey, cfg.SecretKey, ""), + Secure: cfg.UseSSL, + Region: cfg.Region, + }) + if err != nil { + return nil, err + } + + exists, err := client.BucketExists(ctx, cfg.Bucket) + if err != nil { + return nil, mapError(err) + } + if !exists { + return nil, &domain.UnavailableError{Reason: "s3 bucket отсутствует"} + } + + return &MinioStorage{client: client, bucket: cfg.Bucket}, nil +} + +func (s *MinioStorage) Ping(ctx context.Context) error { + _, err := s.client.BucketExists(ctx, s.bucket) + return mapError(err) +} + +func (s *MinioStorage) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error { + _, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ + ContentType: contentType, + }) + return mapError(err) +} + +func (s *MinioStorage) Get(ctx context.Context, key string) (io.ReadCloser, int64, string, error) { + obj, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{}) + if err != nil { + return nil, 0, "", mapError(err) + } + + stat, err := obj.Stat() + if err != nil { + _ = obj.Close() + return nil, 0, "", mapError(err) + } + + return obj, stat.Size, stat.ContentType, nil +} + +func (s *MinioStorage) Delete(ctx context.Context, key string) error { + err := s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}) + return mapError(err) +} + +func (s *MinioStorage) List(ctx context.Context, prefix string) ([]domain.ObjectInfo, error) { + out := make([]domain.ObjectInfo, 0) + for obj := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{ + Prefix: prefix, + Recursive: true, + }) { + if obj.Err != nil { + return nil, mapError(obj.Err) + } + out = append(out, domain.ObjectInfo{ + Key: obj.Key, + LastModified: obj.LastModified, + Size: obj.Size, + }) + } + return out, nil +} + +var _ domain.ObjectStorage = (*MinioStorage)(nil) diff --git a/backend/internal/storage/s3/errors.go b/backend/internal/storage/s3/errors.go new file mode 100644 index 00000000..8558a6d8 --- /dev/null +++ b/backend/internal/storage/s3/errors.go @@ -0,0 +1,42 @@ +package s3 + +import ( + "context" + "errors" + + "github.com/minio/minio-go/v7" + + "high-loud-apps-labs/internal/domain" +) + +type coded interface { + Code() domain.ErrorCode +} + +func mapError(err error) error { + if err == nil { + return nil + } + var c coded + if errors.As(err, &c) { + return err + } + var resp minio.ErrorResponse + if errors.As(err, &resp) { + switch resp.Code { + case "NoSuchKey", "NotFound": + return &domain.NotFoundError{Entity: "object"} + case "NoSuchBucket": + return &domain.UnavailableError{Reason: "s3 bucket отсутствует"} + case "AccessDenied", "InvalidAccessKeyId", "SignatureDoesNotMatch": + return &domain.UnavailableError{Reason: "s3 доступ запрещён"} + } + } + if errors.Is(err, context.DeadlineExceeded) { + return &domain.UnavailableError{Reason: "превышено время ожидания s3"} + } + if errors.Is(err, context.Canceled) { + return &domain.UnavailableError{Reason: "запрос отменён"} + } + return &domain.UnavailableError{Reason: err.Error()} +} diff --git a/backend/migrations/0001_init.sql b/backend/migrations/0001_init.sql new file mode 100644 index 00000000..d9fad408 --- /dev/null +++ b/backend/migrations/0001_init.sql @@ -0,0 +1,67 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT users_username_format + CHECK (username ~ '^[a-z0-9_]{3,32}$'), + CONSTRAINT users_display_name_not_blank + CHECK (display_name !~ '^[[:space:]]*$'), + CONSTRAINT users_display_name_len + CHECK (char_length(display_name) <= 100), + CONSTRAINT users_password_hash_not_blank + CHECK (length(password_hash) > 0) +); +CREATE UNIQUE INDEX users_username_uniq ON users (username); + +CREATE TABLE chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT chats_title_not_blank CHECK (title !~ '^[[:space:]]*$'), + CONSTRAINT chats_title_len CHECK (char_length(title) <= 100) +); +CREATE INDEX chats_created_id_idx ON chats (created_at, id); + +CREATE TABLE chat_members ( + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + joined_at TIMESTAMPTZ NOT NULL DEFAULT now(), + PRIMARY KEY (chat_id, user_id) +); +CREATE INDEX chat_members_user_idx ON chat_members (user_id, chat_id); + +CREATE TABLE messages ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + sender_id UUID NOT NULL REFERENCES users(id) ON DELETE RESTRICT, + text TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT messages_text_not_blank CHECK (text !~ '^[[:space:]]*$'), + CONSTRAINT messages_text_len CHECK (char_length(text) <= 4000) +); +CREATE INDEX messages_chat_created_id_idx + ON messages (chat_id, created_at, id); + +CREATE TABLE sessions ( + token_hash BYTEA PRIMARY KEY, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + expires_at TIMESTAMPTZ NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + CONSTRAINT sessions_token_hash_len CHECK (octet_length(token_hash) = 32) +); +CREATE INDEX sessions_user_idx ON sessions (user_id); +CREATE INDEX sessions_expires_idx ON sessions (expires_at); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS sessions; +DROP TABLE IF EXISTS messages; +DROP TABLE IF EXISTS chat_members; +DROP TABLE IF EXISTS chats; +DROP TABLE IF EXISTS users; +-- +goose StatementEnd \ No newline at end of file diff --git a/backend/migrations/0002_idempotency.sql b/backend/migrations/0002_idempotency.sql new file mode 100644 index 00000000..0ad719c9 --- /dev/null +++ b/backend/migrations/0002_idempotency.sql @@ -0,0 +1,35 @@ +-- +goose Up +-- +goose StatementBegin +CREATE TABLE idempotency_keys ( + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + method TEXT NOT NULL, + path TEXT NOT NULL, + key TEXT NOT NULL, + request_hash BYTEA NOT NULL, + response_status INT NOT NULL, + response_body BYTEA NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + expires_at TIMESTAMPTZ NOT NULL, + + CONSTRAINT idempotency_keys_pkey + PRIMARY KEY (user_id, method, path, key), + CONSTRAINT idempotency_keys_key_len + CHECK (char_length(key) BETWEEN 1 AND 128), + CONSTRAINT idempotency_keys_method_len + CHECK (char_length(method) BETWEEN 1 AND 16), + CONSTRAINT idempotency_keys_path_len + CHECK (char_length(path) BETWEEN 1 AND 2048), + CONSTRAINT idempotency_keys_hash_len + CHECK (octet_length(request_hash) = 32), + CONSTRAINT idempotency_keys_status_range + CHECK (response_status BETWEEN 100 AND 599) +); + +CREATE INDEX idempotency_keys_expires_idx + ON idempotency_keys (expires_at); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS idempotency_keys; +-- +goose StatementEnd \ No newline at end of file diff --git a/backend/migrations/0003_attachments.sql b/backend/migrations/0003_attachments.sql new file mode 100644 index 00000000..40268573 --- /dev/null +++ b/backend/migrations/0003_attachments.sql @@ -0,0 +1,73 @@ +-- +goose Up +-- +goose StatementBegin + +CREATE TABLE 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 RESTRICT, + filename TEXT NOT NULL, + content_type TEXT NOT NULL, + size_bytes BIGINT NOT NULL, + sha256 BYTEA NOT NULL, + object_key TEXT NOT NULL, + status TEXT NOT NULL, + error_code TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now(), + + CONSTRAINT attachments_filename_len + CHECK (char_length(filename) BETWEEN 1 AND 255), + CONSTRAINT attachments_filename_no_slash + CHECK (filename !~ '[/\\]'), + CONSTRAINT attachments_filename_no_ctrl + CHECK (filename !~ '[[:cntrl:]]'), + CONSTRAINT attachments_content_type_chk + CHECK (content_type IN ('image/png', 'image/jpeg')), + CONSTRAINT attachments_size_chk + CHECK (size_bytes > 0 AND size_bytes <= 10485760), + CONSTRAINT attachments_sha256_len + CHECK (octet_length(sha256) = 32), + CONSTRAINT attachments_object_key_len + CHECK (char_length(object_key) BETWEEN 1 AND 1024), + CONSTRAINT attachments_status_chk + CHECK (status IN ('ready', 'pending', 'failed')), + CONSTRAINT attachments_error_code_consistency + CHECK ( + (status IN ('ready', 'pending') AND error_code IS NULL) OR + (status = 'failed' AND error_code IS NOT NULL) + ) +); + +CREATE UNIQUE INDEX attachments_object_key_uniq + ON attachments (object_key); + +CREATE INDEX attachments_chat_idx + ON attachments (chat_id, created_at, id); + +CREATE INDEX attachments_pending_idx + ON attachments (created_at) + WHERE status = 'pending'; + +CREATE TABLE message_attachments ( + message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE, + attachment_id UUID NOT NULL REFERENCES attachments(id) ON DELETE RESTRICT, + position SMALLINT NOT NULL, + + CONSTRAINT message_attachments_pkey + PRIMARY KEY (message_id, attachment_id), + CONSTRAINT message_attachments_position_range + CHECK (position >= 0 AND position < 10), + CONSTRAINT message_attachments_position_uniq + UNIQUE (message_id, position) +); + +CREATE INDEX message_attachments_attachment_idx + ON message_attachments (attachment_id); + +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +DROP TABLE IF EXISTS message_attachments; +DROP TABLE IF EXISTS attachments; +-- +goose StatementEnd \ No newline at end of file diff --git a/backend/migrations/0004_messages_text_relaxed.sql b/backend/migrations/0004_messages_text_relaxed.sql new file mode 100644 index 00000000..f4e1798e --- /dev/null +++ b/backend/migrations/0004_messages_text_relaxed.sql @@ -0,0 +1,13 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_not_blank; +ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank + CHECK (text !~ '^[[:space:]]+$'); +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +ALTER TABLE messages DROP CONSTRAINT IF EXISTS messages_text_not_blank; +ALTER TABLE messages ADD CONSTRAINT messages_text_not_blank + CHECK (text !~ '^[[:space:]]*$'); +-- +goose StatementEnd \ No newline at end of file diff --git a/backend/migrations/embed.go b/backend/migrations/embed.go new file mode 100644 index 00000000..91cca1c3 --- /dev/null +++ b/backend/migrations/embed.go @@ -0,0 +1,6 @@ +package migrations + +import "embed" + +//go:embed *.sql +var FS embed.FS diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..7d6242c7 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,241 @@ +name: mtusi-messenger-lab3 + +x-api-resources: &api-resources + limits: + cpus: "${API_CPUS:-0.5}" + memory: "${API_MEMORY:-256M}" + reservations: + cpus: "${API_CPU_RESERVATION:-0.1}" + memory: "${API_MEMORY_RESERVATION:-64M}" + +services: + postgres: + image: postgres:16.4-alpine + restart: unless-stopped + environment: + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + POSTGRES_DB: messenger + secrets: + - postgres_password + volumes: + - postgres_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "${POSTGRES_CPUS:-1.0}" + memory: ${POSTGRES_MEMORY:-1G} + reservations: + cpus: "${POSTGRES_CPU_RESERVATION:-0.25}" + memory: ${POSTGRES_MEMORY_RESERVATION:-256M} + networks: + - internal + + redis: + image: redis:7.4-alpine + restart: unless-stopped + command: + - redis-server + - --appendonly + - "yes" + - --appendfsync + - everysec + - --save + - "60 1000" + volumes: + - redis_data:/data + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "${REDIS_CPUS:-0.5}" + memory: ${REDIS_MEMORY:-256M} + networks: + - internal + + minio: + image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z + restart: unless-stopped + command: ["server", "/data"] + secrets: + - minio_root_user + - minio_root_password + environment: + MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user + MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password + MINIO_BROWSER: "off" + volumes: + - minio_data:/data + healthcheck: + test: ["CMD", "mc", "ready", "local"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + deploy: + resources: + limits: + cpus: "${MINIO_CPUS:-0.5}" + memory: ${MINIO_MEMORY:-512M} + networks: + - internal + + mc-init: + image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z + restart: "no" + depends_on: + minio: + condition: service_healthy + entrypoint: ["/bin/sh", "/init.sh"] + environment: + MINIO_ENDPOINT: "http://minio:9000" + MINIO_BUCKET: "${S3_BUCKET:-messenger-attachments}" + ROOT_USER_FILE: /run/secrets/minio_root_user + ROOT_PASSWORD_FILE: /run/secrets/minio_root_password + API_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + API_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + volumes: + - ./infra/minio-init.sh:/init.sh:ro + secrets: + - minio_root_user + - minio_root_password + - s3_access_key_id + - s3_secret_access_key + networks: + - internal + + migrate: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger-api:lab3 + restart: "no" + command: ["migrate"] + environment: + DATABASE_URL_FILE: /run/secrets/database_url + secrets: + - database_url + depends_on: + postgres: + condition: service_healthy + healthcheck: + disable: true + deploy: + resources: + limits: + cpus: "0.5" + memory: 128M + networks: + - internal + + api: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger-api:lab3 + restart: unless-stopped + environment: + APP_HOST: "0.0.0.0" + APP_PORT: "8080" + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + SESSION_STORE: "redis" + SESSION_TTL_SECONDS: "${SESSION_TTL_SECONDS:-3600}" + IDEMPOTENCY_TTL_SECONDS: "${IDEMPOTENCY_TTL_SECONDS:-86400}" + SHUTDOWN_DRAIN_SECONDS: "${SHUTDOWN_DRAIN_SECONDS:-3}" + GIN_MODE: "release" + S3_ENDPOINT: "http://minio:9000" + S3_BUCKET: "${S3_BUCKET:-messenger-attachments}" + S3_REGION: "${S3_REGION:-us-east-1}" + S3_USE_SSL: "false" + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + MAX_UPLOAD_BYTES: "${MAX_UPLOAD_BYTES:-10485760}" + MAX_IMAGE_PIXELS: "${MAX_IMAGE_PIXELS:-40000000}" + RECONCILER_ENABLED: "${RECONCILER_ENABLED:-true}" + RECONCILER_INTERVAL_SECONDS: "${RECONCILER_INTERVAL_SECONDS:-300}" + RECONCILER_GRACE_SECONDS: "${RECONCILER_GRACE_SECONDS:-3600}" + secrets: + - database_url + - cache_url + - s3_access_key_id + - s3_secret_access_key + depends_on: + postgres: + condition: service_healthy + redis: + condition: service_healthy + migrate: + condition: service_completed_successfully + minio: + condition: service_healthy + mc-init: + condition: service_completed_successfully + expose: + - "8080" + stop_grace_period: 15s + healthcheck: + test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1:8080/health/ready >/dev/null || exit 1"] + interval: 5s + timeout: 3s + retries: 5 + start_period: 10s + deploy: + replicas: ${API_REPLICAS:-2} + resources: *api-resources + networks: + - internal + + lb: + image: nginx:1.27.4-alpine + restart: unless-stopped + ports: + - "${LB_PORT:-8080}:8080" + volumes: + - ./infra/nginx.conf:/etc/nginx/nginx.conf:ro + depends_on: + api: + condition: service_started + deploy: + resources: + limits: + cpus: "0.5" + memory: 128M + networks: + - internal + +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url + minio_root_user: + file: ./secrets/minio_root_user + minio_root_password: + file: ./secrets/minio_root_password + s3_access_key_id: + file: ./secrets/s3_access_key_id + s3_secret_access_key: + file: ./secrets/s3_secret_access_key + +volumes: + postgres_data: + redis_data: + minio_data: + +networks: + internal: + driver: bridge \ No newline at end of file diff --git a/evidence/attachments-negative.txt b/evidence/attachments-negative.txt new file mode 100644 index 00000000..7522096a --- /dev/null +++ b/evidence/attachments-negative.txt @@ -0,0 +1,14 @@ +=== 2026-10-23T18:17:57Z === +--- команда: python3 student-tests/test_attachments.py --base-url http://localhost:8080 --- +--- сценарии (по контракту contracts/README.md разд. Вложения) --- +1. 11 MiB -> 413 payload_too_large +2. PNG без IHDR -> 415 unsupported_media_type +3. PNG как image/jpeg -> 201, content_type image/png +4. Чужой attachment_id (metadata и bytes) -> 404 +5. attachment_id из другого чата -> 404 +6. 11 attachment_ids -> 400 invalid_request +7. Дубликаты attachment_ids -> 400 invalid_request +8. Пустой filename -> 400 invalid_request +9. Опасные filename (../, \, NUL) -> 400 invalid_request + +PASS: негативные сценарии вложений (413/415/404/400) прошли без 500 diff --git a/evidence/container-state.txt b/evidence/container-state.txt new file mode 100644 index 00000000..57dfb83b --- /dev/null +++ b/evidence/container-state.txt @@ -0,0 +1,25 @@ +=== 2026-10-23T17:17:08Z === +--- docker compose ps --- +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi-messenger-lab3-api-1 mtusi-messenger-api:lab3 "/server" api 24 minutes ago Up 2 minutes (healthy) 8080/tcp +mtusi-messenger-lab3-api-2 mtusi-messenger-api:lab3 "/server" api 24 minutes ago Up About a minute (healthy) 8080/tcp +mtusi-messenger-lab3-lb-1 nginx:1.27.4-alpine "/docker-entrypoint.…" lb 2 hours ago Up 2 hours 0.0.0.0:8080->8080/tcp, [::]:8080->8080/tcp +mtusi-messenger-lab3-minio-1 pgsty/silo:RELEASE.2026-09-16T00-00-00Z "/usr/bin/docker-ent…" minio 3 hours ago Up 28 seconds (healthy) 9000/tcp +mtusi-messenger-lab3-postgres-1 postgres:16.4-alpine "docker-entrypoint.s…" postgres 13 days ago Up 2 minutes (healthy) 5432/tcp +mtusi-messenger-lab3-redis-1 redis:7.4-alpine "docker-entrypoint.s…" redis 13 days ago Up 2 minutes (healthy) 6379/tcp + +--- используемые образы --- + image: mtusi-messenger-api:lab3 + image: nginx:1.27.4-alpine + image: pgsty/silo:RELEASE.2026-09-16T00-00-00Z + image: postgres:16.4-alpine + image: redis:7.4-alpine + +--- volumes --- +local mtusi-messenger-lab3_minio_data +local mtusi-messenger-lab3_postgres_data +local mtusi-messenger-lab3_redis_data +local mtusi_restore_test + +--- networks --- +4ae31a36cac6 mtusi-messenger-lab3_internal bridge local diff --git a/evidence/failover-1.txt b/evidence/failover-1.txt new file mode 100644 index 00000000..7f44f255 --- /dev/null +++ b/evidence/failover-1.txt @@ -0,0 +1,29 @@ +=== 2026-10-23T17:25:16Z === +--- состояние контейнеров ДО --- +SERVICE STATUS +api Up 10 minutes (healthy) +api Up 9 minutes (healthy) +lb Up 2 hours +minio Up 8 minutes (healthy) +postgres Up 10 minutes (healthy) +redis Up 10 minutes (healthy) +--- LB видит обе реплики --- +X-Instance-Id: 54992a38015c +X-Instance-Id: c0f8bc0ada76 + +--- запуск failover-хука через .sh --- +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": 63, "failed_attempts": 4, "first_recovery_seconds": 5.481} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. + +--- состояние контейнеров ПОСЛЕ (одна api должна быть Exited) --- +SERVICE STATUS +api Up 10 minutes (healthy) +api Exited (0) 11 seconds ago +lb Up 2 hours +mc-init Exited (0) About a minute ago +migrate Exited (0) About a minute ago +minio Up 8 minutes (healthy) +postgres Up 10 minutes (healthy) +redis Up 10 minutes (healthy) diff --git a/evidence/failover-2.txt b/evidence/failover-2.txt new file mode 100644 index 00000000..74865c94 --- /dev/null +++ b/evidence/failover-2.txt @@ -0,0 +1,29 @@ +=== 2026-10-23T17:26:38Z === +--- состояние контейнеров ДО --- +SERVICE STATUS +api Up 11 minutes (healthy) +api Up 59 seconds (healthy) +lb Up 2 hours +minio Up 9 minutes (healthy) +postgres Up 11 minutes (healthy) +redis Up 11 minutes (healthy) +--- LB видит обе реплики --- +X-Instance-Id: 54992a38015c +X-Instance-Id: c0f8bc0ada76 + +--- запуск failover-хука через .sh --- +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": 101, "failed_attempts": 2, "first_recovery_seconds": 5.344} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. + +--- состояние контейнеров ПОСЛЕ --- +SERVICE STATUS +api Up 12 minutes (healthy) +api Exited (0) 11 seconds ago +lb Up 2 hours +mc-init Exited (0) About a minute ago +migrate Exited (0) About a minute ago +minio Up 10 minutes (healthy) +postgres Up 12 minutes (healthy) +redis Up 12 minutes (healthy) diff --git a/evidence/failover-manual.txt b/evidence/failover-manual.txt new file mode 100644 index 00000000..33c8997f --- /dev/null +++ b/evidence/failover-manual.txt @@ -0,0 +1,53 @@ +=== 2026-10-23T18:00:53Z === +--- ДО --- +NAME STATUS +mtusi-messenger-lab3-api-1 Up 4 minutes (healthy) +mtusi-messenger-lab3-api-2 Up 4 minutes (healthy) +mtusi-messenger-lab3-lb-1 Up 11 minutes +mtusi-messenger-lab3-mc-init-1 Exited (0) 16 minutes ago +mtusi-messenger-lab3-migrate-1 Exited (0) 16 minutes ago +mtusi-messenger-lab3-minio-1 Up 32 minutes (healthy) +mtusi-messenger-lab3-postgres-1 Up 32 minutes (healthy) +mtusi-messenger-lab3-redis-1 Up 32 minutes (healthy) + +--- Останавливаем api-1 --- +--- api-1 остановлена; отвечает только api-2 --- +X-Instance-Id: 1bc206b485d7 + +--- состояние --- +NAME STATUS +mtusi-messenger-lab3-api-1 Exited (0) 19 seconds ago +mtusi-messenger-lab3-api-2 Up 4 minutes (healthy) +mtusi-messenger-lab3-lb-1 Up 11 minutes +mtusi-messenger-lab3-mc-init-1 Exited (0) 16 minutes ago +mtusi-messenger-lab3-migrate-1 Exited (0) 16 minutes ago +mtusi-messenger-lab3-minio-1 Up 33 minutes (healthy) +mtusi-messenger-lab3-postgres-1 Up 33 minutes (healthy) +mtusi-messenger-lab3-redis-1 Up 33 minutes (healthy) + +--- Восстанавливаем api-1, останавливаем api-2 --- +--- api-2 остановлена; отвечает только api-1 --- +X-Instance-Id: 27b584e2b0a9 + +--- состояние --- +NAME STATUS +mtusi-messenger-lab3-api-1 Up 37 seconds (healthy) +mtusi-messenger-lab3-api-2 Exited (0) 19 seconds ago +mtusi-messenger-lab3-lb-1 Up 12 minutes +mtusi-messenger-lab3-mc-init-1 Exited (0) 17 minutes ago +mtusi-messenger-lab3-migrate-1 Exited (0) 17 minutes ago +mtusi-messenger-lab3-minio-1 Up 34 minutes (healthy) +mtusi-messenger-lab3-postgres-1 Up 34 minutes (healthy) +mtusi-messenger-lab3-redis-1 Up 34 minutes (healthy) + +--- Восстанавливаем api-2 --- +--- итог --- +NAME STATUS +mtusi-messenger-lab3-api-1 Up 53 seconds (healthy) +mtusi-messenger-lab3-api-2 Up 15 seconds (healthy) +mtusi-messenger-lab3-lb-1 Up 12 minutes +mtusi-messenger-lab3-mc-init-1 Exited (0) 17 minutes ago +mtusi-messenger-lab3-migrate-1 Exited (0) 17 minutes ago +mtusi-messenger-lab3-minio-1 Up 34 minutes (healthy) +mtusi-messenger-lab3-postgres-1 Up 34 minutes (healthy) +mtusi-messenger-lab3-redis-1 Up 34 minutes (healthy) diff --git a/evidence/memory-10x10mb.txt b/evidence/memory-10x10mb.txt new file mode 100644 index 00000000..1d7a2157 --- /dev/null +++ b/evidence/memory-10x10mb.txt @@ -0,0 +1,39 @@ +=== 2026-10-23T17:56:47Z === +--- лимиты api --- +/mtusi-messenger-lab3-api-1 MemLimit=268435456 NanoCPUs=500000000 +/mtusi-messenger-lab3-api-2 MemLimit=268435456 NanoCPUs=500000000 + +api-реплики: ['27b584e2b0a9e5bfb16f2d3a386aab8d4dd261e10847c3364207cf107a87dbfa', '1bc206b485d75c7410c447ad816a88a5b5043bdd722313fe1e08b9691fb1fdf5'] +размер одного файла: 10485759 байт (~10.0 MiB) + +--- memory.peak ДО --- +27b584e2b0a9 peak=140772 KB +1bc206b485d7 peak=140960 KB + +--- docker stats ДО --- +mtusi-messenger-lab3-api-1 135.5MiB / 256MiB +mtusi-messenger-lab3-api-2 134.8MiB / 256MiB + +--- 10 uploads за 1.79 с --- +--- docker stats ПОСЛЕ --- +mtusi-messenger-lab3-api-1 107.8MiB / 256MiB +mtusi-messenger-lab3-api-2 109.8MiB / 256MiB + +--- memory.peak ПОСЛЕ --- +27b584e2b0a9 peak=141344 KB (138.0 MiB) +1bc206b485d7 peak=144868 KB (141.5 MiB) + +лимит MEM на реплику: 256 MiB + +PASS: 10 × 10 MiB uploads, пик RAM каждой реплики <= 256 MiB + + +--- интерпретация --- +После рестарта пин ~8 MiB. bootstrap() (3 register + 3 login = 6 argon2 +операций, каждая malloc 64 MiB) поднимает пик до ~140 MiB. 10×10 MiB +uploads добавили только +1..4 MiB к пик, потому что Go heap уже раздут +GC и io.LimitReader ограничивает каждый upload 10 MiB. + +Худ сценарий: 140 MiB (argon2) + 100 MiB (10×10 загрузок, если бы всё упало +на одну реплику) = 240 MiB < 256 MiB лимита. Фактически пик +141 MiB << 256 MiB. \ No newline at end of file diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..c15ebe6f --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,35 @@ +=== 2026-10-23T17:27:51Z === +--- состояние ДО --- +SERVICE STATUS +api Up 12 minutes (healthy) +api Up 49 seconds (healthy) +lb Up 2 hours +minio Up 11 minutes (healthy) +postgres Up 12 minutes (healthy) +redis Up 12 minutes (healthy) +--- время запуска ДО --- +/mtusi-messenger-lab3-api-1 2026-10-23T17:14:52.249193982Z +/mtusi-messenger-lab3-api-2 2026-10-23T17:27:01.87573057Z +/mtusi-messenger-lab3-minio-1 2026-10-23T17:16:40.444089494Z +/mtusi-messenger-lab3-postgres-1 2026-10-23T17:14:51.897985071Z +/mtusi-messenger-lab3-redis-1 2026-10-23T17:14:52.027046338Z + +--- persistence-хук через .sh --- +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. + +--- состояние ПОСЛЕ --- +SERVICE STATUS +api Up Less than a second (health: starting) +api Up Less than a second (health: starting) +lb Up 2 hours +minio Up 3 seconds (health: starting) +postgres Up 3 seconds (health: starting) +redis Up 3 seconds (health: starting) +--- время запуска ПОСЛЕ (должно быть свежее) --- +/mtusi-messenger-lab3-api-1 2026-10-23T17:27:57.091812745Z +/mtusi-messenger-lab3-api-2 2026-10-23T17:27:57.022336737Z +/mtusi-messenger-lab3-minio-1 2026-10-23T17:27:54.071925449Z +/mtusi-messenger-lab3-postgres-1 2026-10-23T17:27:54.153891511Z +/mtusi-messenger-lab3-redis-1 2026-10-23T17:27:54.213715075Z diff --git a/evidence/reconciler.txt b/evidence/reconciler.txt new file mode 100644 index 00000000..88172071 --- /dev/null +++ b/evidence/reconciler.txt @@ -0,0 +1,83 @@ +=== 2026-10-23T18:16:45Z === +--- ускоряем интервал (10s) и grace (30s) --- + Container mtusi-messenger-lab3-minio-1 Running + Container mtusi-messenger-lab3-postgres-1 Running + Container mtusi-messenger-lab3-redis-1 Running + Container mtusi-messenger-lab3-mc-init-1 Recreate + Container mtusi-messenger-lab3-migrate-1 Recreate + Container mtusi-messenger-lab3-migrate-1 Recreated + Container mtusi-messenger-lab3-mc-init-1 Recreated + Container mtusi-messenger-lab3-api-2 Recreate + Container mtusi-messenger-lab3-api-1 Recreate + Container mtusi-messenger-lab3-api-1 Recreated + Container mtusi-messenger-lab3-api-2 Recreated + Container mtusi-messenger-lab3-minio-1 Waiting + Container mtusi-messenger-lab3-postgres-1 Waiting + Container mtusi-messenger-lab3-postgres-1 Healthy + Container mtusi-messenger-lab3-migrate-1 Starting + Container mtusi-messenger-lab3-minio-1 Healthy + Container mtusi-messenger-lab3-mc-init-1 Starting + Container mtusi-messenger-lab3-migrate-1 Started + Container mtusi-messenger-lab3-mc-init-1 Started + Container mtusi-messenger-lab3-redis-1 Waiting + Container mtusi-messenger-lab3-migrate-1 Waiting + Container mtusi-messenger-lab3-minio-1 Waiting + Container mtusi-messenger-lab3-mc-init-1 Waiting + Container mtusi-messenger-lab3-postgres-1 Waiting + Container mtusi-messenger-lab3-migrate-1 Exited + Container mtusi-messenger-lab3-mc-init-1 Exited + Container mtusi-messenger-lab3-redis-1 Healthy + Container mtusi-messenger-lab3-postgres-1 Healthy + Container mtusi-messenger-lab3-minio-1 Healthy + Container mtusi-messenger-lab3-api-2 Starting + Container mtusi-messenger-lab3-api-2 Started + Container mtusi-messenger-lab3-api-1 Starting + Container mtusi-messenger-lab3-api-1 Started + +--- вставляем stale pending в БД --- +INSERT 0 1 + +--- ждём 25 секунд, смотрим лог reconciler --- +api-1 | 2026/10/23 18:16:51 reconciler: started, interval=10s grace=30s +api-1 | 2026/10/23 18:16:51 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-2 | 2026/10/23 18:16:50 reconciler: started, interval=10s grace=30s +api-2 | 2026/10/23 18:16:50 Запускаем на 0.0.0.0:8080 (SESSION_STORE=redis, RECONCILER_ENABLED=true) +api-2 | 2026/10/23 18:17:01 reconciler: sweep done pending=1 orphans=0 + +--- проверяем, что stale удалён --- + pending_left +-------------- + 0 +(1 row) + + +--- возвращаем дефолты --- + Container mtusi-messenger-lab3-postgres-1 Running + Container mtusi-messenger-lab3-redis-1 Running + Container mtusi-messenger-lab3-minio-1 Running + Container mtusi-messenger-lab3-api-1 Recreate + Container mtusi-messenger-lab3-api-2 Recreate + Container mtusi-messenger-lab3-api-2 Recreated + Container mtusi-messenger-lab3-api-1 Recreated + Container mtusi-messenger-lab3-minio-1 Waiting + Container mtusi-messenger-lab3-postgres-1 Waiting + Container mtusi-messenger-lab3-postgres-1 Healthy + Container mtusi-messenger-lab3-migrate-1 Starting + Container mtusi-messenger-lab3-minio-1 Healthy + Container mtusi-messenger-lab3-mc-init-1 Starting + Container mtusi-messenger-lab3-migrate-1 Started + Container mtusi-messenger-lab3-mc-init-1 Started + Container mtusi-messenger-lab3-postgres-1 Waiting + Container mtusi-messenger-lab3-redis-1 Waiting + Container mtusi-messenger-lab3-migrate-1 Waiting + Container mtusi-messenger-lab3-minio-1 Waiting + Container mtusi-messenger-lab3-mc-init-1 Waiting + Container mtusi-messenger-lab3-postgres-1 Healthy + Container mtusi-messenger-lab3-migrate-1 Exited + Container mtusi-messenger-lab3-redis-1 Healthy + Container mtusi-messenger-lab3-minio-1 Healthy + Container mtusi-messenger-lab3-mc-init-1 Exited + Container mtusi-messenger-lab3-api-2 Starting + Container mtusi-messenger-lab3-api-2 Started + Container mtusi-messenger-lab3-api-1 Starting + Container mtusi-messenger-lab3-api-1 Started diff --git a/evidence/s3-down.txt b/evidence/s3-down.txt new file mode 100644 index 00000000..35b16f99 --- /dev/null +++ b/evidence/s3-down.txt @@ -0,0 +1,23 @@ +=== 2026-10-23T17:16:17Z === +--- ДО: /health/ready --- +{"status":"ok"} +--- останавливаем MinIO --- + Container mtusi-messenger-lab3-minio-1 Stopping + Container mtusi-messenger-lab3-minio-1 Stopped +--- ждём 15 секунд, пока healthcheck реплик обновится --- +--- /health/ready через LB (ожидается 503 unavailable) --- +{"error":{"code":"unavailable","message":"сервис временно недоступен","request_id":"6a9bfae9-d751-46e7-868e-d5f55f0daaee"}} +--- прямой /health/ready у каждой api-реплики (обход LB) --- +wget: server returned error: HTTP/1.1 503 Service Unavailable + +wget: server returned error: HTTP/1.1 503 Service Unavailable + +--- запускаем MinIO обратно --- + Container mtusi-messenger-lab3-minio-1 Starting + Container mtusi-messenger-lab3-minio-1 Started +--- ждём 20 секунд до восстановления --- +--- ПОСЛЕ: /health/ready --- +{"status":"ok"} +--- X-Instance-Id после восстановления (должны быть оба) --- +X-Instance-Id: 54992a38015c +X-Instance-Id: c0f8bc0ada76 diff --git a/evidence/s3-private.txt b/evidence/s3-private.txt new file mode 100644 index 00000000..3ac09c69 --- /dev/null +++ b/evidence/s3-private.txt @@ -0,0 +1,18 @@ +=== 2026-10-23T17:59:59Z === +--- MinIO порт 9000 не опубликован наружу --- +invalid IP:0 +no port 9001/tcp for container mtusi-messenger-lab3-minio-1: 0/tcp +консоль не проброшена (ожидаемо) + +--- MINIO_BROWSER --- +off + +--- путь к объекту (первый в БД) --- +object_key=chats/975e679f-51da-4a2b-a67a-adb85e7c6e1c/b5ebe14a-faf7-4682-a8dd-487bf6b5eb25 + +--- GET без авторизации из api-контейнера (ожидается 403) --- + HTTP/1.1 403 Forbidden +wget: server returned error: HTTP/1.1 403 Forbidden + +--- API-пользователь (не root) --- +enabled messenger-api messenger-api diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..4ef4acca --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,3 @@ +=== 2026-10-23T17:13:20Z === +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. diff --git a/infra/minio-init.sh b/infra/minio-init.sh new file mode 100644 index 00000000..3defe34d --- /dev/null +++ b/infra/minio-init.sh @@ -0,0 +1,48 @@ +#!/bin/sh +set -eu + +: "${MINIO_ENDPOINT:?MINIO_ENDPOINT не задан}" +: "${MINIO_BUCKET:?MINIO_BUCKET не задан}" +: "${ROOT_USER_FILE:?ROOT_USER_FILE не задан}" +: "${ROOT_PASSWORD_FILE:?ROOT_PASSWORD_FILE не задан}" +: "${API_ACCESS_KEY_ID_FILE:?API_ACCESS_KEY_ID_FILE не задан}" +: "${API_SECRET_ACCESS_KEY_FILE:?API_SECRET_ACCESS_KEY_FILE не задан}" + +ROOT_USER="$(cat "$ROOT_USER_FILE")" +ROOT_PASS="$(cat "$ROOT_PASSWORD_FILE")" +API_ID="$(cat "$API_ACCESS_KEY_ID_FILE")" +API_SECRET="$(cat "$API_SECRET_ACCESS_KEY_FILE")" + +mc alias set bootstrap "$MINIO_ENDPOINT" "$ROOT_USER" "$ROOT_PASS" --api S3v4 >/dev/null + +mc mb --ignore-existing "bootstrap/$MINIO_BUCKET" + +if ! mc admin user info bootstrap "$API_ID" >/dev/null 2>&1; then + mc admin user add bootstrap "$API_ID" "$API_SECRET" +fi + +POLICY_FILE="/tmp/minio-policy.json" +trap 'rm -f "$POLICY_FILE"' EXIT +cat >"$POLICY_FILE" </dev/null || true + +mc admin policy attach bootstrap messenger-api --user "$API_ID" 2>/dev/null || true + +echo "minio bootstrap в норме: bucket=${MINIO_BUCKET} user=${API_ID}" \ No newline at end of file diff --git a/infra/nginx.conf b/infra/nginx.conf new file mode 100644 index 00000000..e8f196c7 --- /dev/null +++ b/infra/nginx.conf @@ -0,0 +1,48 @@ +worker_processes auto; + +events { + worker_connections 1024; +} + +http { + access_log /dev/stdout; + error_log /dev/stderr warn; + + client_max_body_size 12m; + + resolver 127.0.0.11 valid=10s ipv6=off; + + upstream backend { + zone backend 64k; + + server api:8080 resolve max_fails=2 fail_timeout=5s; + + keepalive 32; + keepalive_requests 1000; + keepalive_timeout 60s; + } + + server { + listen 8080; + server_tokens off; + + location / { + proxy_pass http://backend; + + proxy_http_version 1.1; + proxy_set_header Connection ""; + + 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_connect_timeout 2s; + proxy_send_timeout 30s; + proxy_read_timeout 30s; + + proxy_next_upstream error timeout; + proxy_next_upstream_tries 2; + } + } +} \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..e3f366d9 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,14 @@ +#!/bin/sh +set -eu + +# Перезапускает API и все хранилища состояния (PostgreSQL, Redis, MinIO), +# сохраняя тома и данные. Используется в `make test-persistence`. +# +# - `docker compose restart` останавливает и запускает существующие контейнеры; +# - именованные тома не затрагиваются (без `down`, без `-v`, без `rm`); +# - `migrate`/`mc-init` не перезапускаются: миграции уже применены, +# bucket уже создан, и то и другое сохраняется благодаря томам и хранилищам состояния. + +cd "$(dirname "$0")/.." + +docker compose restart postgres redis minio api \ No newline at end of file diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..e7943594 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,22 @@ +#!/bin/sh +set -eu + +: "${TARGET_INSTANCE_ID:?test provides TARGET_INSTANCE_ID (X-Instance-Id header)}" + +cd "$(dirname "$0")/.." + +cid="" +for c in $(docker compose ps -q api); do + host=$(docker inspect -f '{{.Config.Hostname}}' "$c" 2>/dev/null || true) + if [ "$host" = "$TARGET_INSTANCE_ID" ]; then + cid="$c" + break + fi +done + +if [ -z "$cid" ]; then + echo "no api replica with X-Instance-Id=$TARGET_INSTANCE_ID" >&2 + exit 1 +fi + +docker stop "$cid" >/dev/null \ No newline at end of file diff --git a/student-tests/README.md b/student-tests/README.md new file mode 100644 index 00000000..8deda0f7 --- /dev/null +++ b/student-tests/README.md @@ -0,0 +1,79 @@ +# Собственные тесты + +Проверки негативных сценариев, конкурентной работы, жизненного цикла +сессий и отказов хранилищ. Отдельно от публичных `tests/`. + +Сервер должен быть поднят на `http://localhost:8080`. + +## Быстрый прогон + + python student-tests/run_all.py --base-url http://localhost:8080 + +Запускает `test_cases.py`, `test_concurrency.py`, `test_auth.py`. +Это базовый набор, не требующий особой конфигурации стенда. + +## Отдельные сценарии + + python student-tests/test_expiry.py --base-url http://localhost:8080 + +Требует стенд с `SESSION_TTL_SECONDS=2`. Проверяет, что сессия +перестаёт работать после истечения TTL. Меняй TTL в `compose.yaml`, +перезапускай api и прогоняй. + + python student-tests/test_resilience.py --service redis + python student-tests/test_resilience.py --service postgres + +Требуют запущенного Docker и работающего стенда. Скрипт останавливает +указанный сервис через `docker compose stop`, проверяет, что защищённые +endpoint отдают 503 unavailable (не 401 и не 500), и запускает сервис +обратно. + +## Данные для EXPLAIN + + python student-tests/seed_messages.py --count 10000 + student-tests/explain_history.sh + +`seed_messages.py` печатает `chat_id`; передай его в `explain_history.sh`, +результат сохранится в `evidence/explain_history.txt`. +## Трек 2: план запроса и восстановление + +Исследование на ≥100 000 сообщениях с неравномерными размерами чатов. + + python student-tests/seed_large.py + # сохранить chat_id из вывода в evidence/chat_ids.txt + + student-tests/explain_history.sh + student-tests/compare_indexes.sh + student-tests/write_bench.sh + student-tests/backup_restore.sh + +Результаты попадают в `evidence/`: + +- `explain_history.txt` — EXPLAIN (ANALYZE, BUFFERS) ключевого запроса +- `index_comparison.txt` — baseline vs covering vs Seq Scan +- `write_cost.txt` — время 5000 INSERT под разными индексами +- `backup_restore.txt` — pg_dump → pg_restore → сверка счётчиков и md5 + +Восстановленный контейнер `pg_restore_target` оставлен для ручной проверки +и удаляется вручную после эксперимента. + +## Нагрузка (k6) + +Open-loop генератор: фиксированный arrival rate, устраняет coordinated +omission, отдельные метрики для POST /messages. Дополняет `tests/load.py` +(closed-loop), а не заменяет его. + + k6 run -e RATE=50 -e DURATION=30s student-tests/load-k6.js + k6 run -e RATE=50 -e DURATION=3m \ + --summary-export evidence/k6-rolling.json \ + student-tests/load-k6.js + +Setup регистрирует отдельного пользователя и создаёт чат. Чтобы нагружать +существующий чат (например, из `seed_large.py`), задай `CHAT_ID` и `TOKEN`: + + CHAT_ID=... TOKEN=... k6 run -e RATE=50 -e DURATION=1m student-tests/load-k6.js + +Summary (`--summary-export`) — JSON с p50/p95/p99 `write_latency_ms`, +`write_errors.rate`, `http_reqs.rate`. Порог `write_errors<1%` встроен в +options.thresholds: k6 вернёт non-zero exit, если под нагрузкой появились +ошибки сверх порога. \ No newline at end of file diff --git a/student-tests/load-k6.js b/student-tests/load-k6.js new file mode 100644 index 00000000..df8c3d12 --- /dev/null +++ b/student-tests/load-k6.js @@ -0,0 +1,125 @@ +// k6 — open-loop нагрузка на POST /messages. +// +// В отличие от tests/load.py (closed-loop) генерирует фиксированный +// arrival rate независимо от того, как быстро отвечает сервер. Это +// устраняет coordinated omission: если сервер тормозит, k6 не снижает +// интенсивность, а честно измеряет latency и долю ошибок. +// +// Два режима: +// 1. Default — setup регистрирует пользователя, логинится и создаёт чат. +// 2. External — если заданы CHAT_ID и TOKEN, используется существующий +// чат (например, из student-tests/seed_large.py). Регистрация не нужна. +// +// Запуск: +// k6 run -e RATE=50 -e DURATION=30s student-tests/load-k6.js +// k6 run -e RATE=50 -e DURATION=3m student-tests/load-k6.js \ +// --summary-export evidence/k6-rolling.json +// CHAT_ID=... TOKEN=... k6 run -e RATE=50 -e DURATION=1m student-tests/load-k6.js +// +// Метрики: +// write_latency_ms — latency POST /messages (p50/p95/p99 в summary) +// write_errors — доля не-201 +// write_total — счётчик запросов + +import http from 'k6/http'; +import encoding from 'k6/encoding'; +import { check, fail } from 'k6'; +import { Trend, Rate, Counter } from 'k6/metrics'; + +const RATE = parseInt(__ENV.RATE || '50', 10); +const DURATION = __ENV.DURATION || '1m'; +const BASE = __ENV.BASE_URL || 'http://localhost:8080'; +const EXTERNAL_CHAT = __ENV.CHAT_ID || ''; +const EXTERNAL_TOKEN = __ENV.TOKEN || ''; + +const writeLatency = new Trend('write_latency_ms', true); +const writeErrors = new Rate('write_errors'); +const writeTotal = new Counter('write_total'); + +export const options = { + scenarios: { + writes: { + executor: 'constant-arrival-rate', + rate: RATE, + timeUnit: '1s', + duration: DURATION, + preAllocatedVUs: Math.max(50, RATE * 2), + maxVUs: Math.max(200, RATE * 4), + }, + }, + thresholds: { + // Порог уровня 4: не более 1% ошибок. В треке 1 проверим, + // удерживается ли при разных профилях нагрузки. + 'write_errors': ['rate<0.01'], + }, +}; + +export function setup() { + if (EXTERNAL_CHAT && EXTERNAL_TOKEN) { + return { token: EXTERNAL_TOKEN, chatId: EXTERNAL_CHAT }; + } + if ((EXTERNAL_CHAT && !EXTERNAL_TOKEN) || (!EXTERNAL_CHAT && EXTERNAL_TOKEN)) { + fail('CHAT_ID и TOKEN должны задаваться вместе'); + } + + const suffix = Math.random().toString(36).slice(2, 10); + const username = `k6_${suffix}`; + const password = 'Example-pass-12345678'; + + const reg = http.post(`${BASE}/api/v1/users`, JSON.stringify({ + username, display_name: `K6 ${suffix}`, password, + }), { headers: { 'Content-Type': 'application/json' } }); + if (reg.status !== 201) fail(`register failed: ${reg.status} ${reg.body}`); + + const user = reg.json(); + const encoded = encoding.b64encode(`${username}:${password}`); + + const sess = http.post(`${BASE}/api/v1/auth/sessions`, null, { + headers: { 'Authorization': `Basic ${encoded}` }, + }); + if (sess.status !== 201) fail(`login failed: ${sess.status} ${sess.body}`); + + const token = sess.json().token; + + const chat = http.post(`${BASE}/api/v1/chats`, JSON.stringify({ + title: `k6 chat ${suffix}`, + member_ids: [user.id], + }), { + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${token}`, + }, + }); + if (chat.status !== 201) fail(`chat failed: ${chat.status} ${chat.body}`); + + return { token, chatId: chat.json().id }; +} + +export default function (data) { + const key = `k6-${__VU}-${__ITER}-${Date.now()}`; + const body = JSON.stringify({ text: `msg vu=${__VU} iter=${__ITER}` }); + + const res = http.post( + `${BASE}/api/v1/chats/${data.chatId}/messages`, + body, + { + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${data.token}`, + 'Idempotency-Key': key, + }, + tags: { name: 'POST /messages' }, + }, + ); + + writeTotal.add(1); + writeLatency.add(res.timings.duration); + writeErrors.add(res.status !== 201); + + check(res, { + 'status is 201': (r) => r.status === 201, + 'has message id': (r) => { + try { return !!r.json().id; } catch { return false; } + }, + }); +} \ No newline at end of file diff --git a/student-tests/run_all.py b/student-tests/run_all.py new file mode 100644 index 00000000..ea3983e3 --- /dev/null +++ b/student-tests/run_all.py @@ -0,0 +1,25 @@ +#!/usr/bin/env python3 +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parent + +QUICK = ('test_cases.py', 'test_concurrency.py', 'test_auth.py', 'test_attachments.py') + +def main(): + args = sys.argv[1:] + failed = [] + for script in QUICK: + print(f'=== {script} ===') + result = subprocess.run([sys.executable, str(ROOT / script), *args]) + if result.returncode != 0: + failed.append(script) + if failed: + print('FAILED:', ', '.join(failed)) + sys.exit(1) + print('=== Все тесты прошли ===') + + +if __name__ == '__main__': + main() \ No newline at end of file diff --git a/student-tests/seed_large.py b/student-tests/seed_large.py new file mode 100644 index 00000000..4aeabad5 --- /dev/null +++ b/student-tests/seed_large.py @@ -0,0 +1,82 @@ +#!/usr/bin/env python3 +"""Сидирует 100k сообщений в 24 чата с неравномерными размерами (трек 2).""" +import base64 +import sys +import time +import uuid +from concurrent.futures import ThreadPoolExecutor +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, arguments, check, run # noqa: E402 + + +def make_user(c, label): + username = f'{label}_{uuid.uuid4().hex[:12]}' + password = 'T3st-' + uuid.uuid4().hex + user = c.call('POST', '/api/v1/users', 201, body={ + 'username': username, 'display_name': label.capitalize(), + 'password': password}, schema='User')['json'] + cred = base64.b64encode(f'{username}:{password}'.encode()).decode() + s = c.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': 'Basic ' + cred}, schema='Session')['json'] + return {'id': user['id'], 'username': username, 'token': s['token']} + + +def seed_chat(base_url, ca_file, alice, bob, title, count, workers=8): + c = Client(base_url, ca_file) + chat = c.call('POST', '/api/v1/chats', 201, actor=alice, body={ + 'title': title, 'member_ids': [alice['id'], bob['id']]}, + schema='Chat')['json'] + chat_id = chat['id'] + + per_worker = count // workers + remainder = count - per_worker * workers + + def worker(wid): + local = Client(base_url, ca_file, timeout=20) + n = per_worker + (1 if wid < remainder else 0) + for i in range(n): + local.call('POST', f'/api/v1/chats/{chat_id}/messages', 201, + actor=alice, body={'text': f'{title} #{wid}-{i}'}, + schema='Message') + + started = time.monotonic() + with ThreadPoolExecutor(max_workers=workers) as pool: + list(pool.map(worker, range(workers))) + elapsed = time.monotonic() - started + print(f' {title}: {count} сообщений за {elapsed:.1f}s ({count/max(elapsed,0.001):.0f}/s)') + return chat_id + + +def main(): + args = arguments('Сидирует неравномерные чаты для трека 2') + c = Client(args.base_url, args.ca_file) + alice = make_user(c, 'track2_a') + bob = make_user(c, 'track2_b') + + plan = ( + [('hot', 55_000)] + + [('medium-%d' % i, 10_000) for i in range(1, 4)] + + [('small-%d' % i, 1_000) for i in range(1, 11)] + + [('tiny-%d' % i, 500) for i in range(1, 11)] + ) + total = sum(n for _, n in plan) + check(total == 100_000, f'сумма {total} != 100000') + + result = {} + started = time.monotonic() + for title, count in plan: + result[title] = seed_chat(args.base_url, args.ca_file, alice, bob, title, count) + elapsed = time.monotonic() - started + + print(f'\nВсего {total} сообщений за {elapsed:.1f}s ({total/elapsed:.0f}/s)') + print('chat_id для EXPLAIN (сохраните эти значения):') + for title in ('hot', 'medium-1', 'small-1', 'tiny-1'): + print(f' {title}: {result[title]}') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student-tests/seed_messages.py b/student-tests/seed_messages.py new file mode 100644 index 00000000..47bd6081 --- /dev/null +++ b/student-tests/seed_messages.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +"""Заполняет один чат N сообщениями. Надо EXPLAIN.""" +import base64 +import sys +import time +import uuid +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, arguments, check, run # noqa: E402 + + +def make_user(c, label): + username = f'{label}_{uuid.uuid4().hex[:12]}' + password = 'T3st-' + uuid.uuid4().hex + user = c.call('POST', '/api/v1/users', 201, body={ + 'username': username, 'display_name': label.capitalize(), + 'password': password}, schema='User')['json'] + cred = base64.b64encode(f'{username}:{password}'.encode()).decode() + s = c.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': 'Basic ' + cred}, schema='Session')['json'] + return {'id': user['id'], 'token': s['token']} + + +def main(): + def configure(p): + p.add_argument('--count', type=int, default=10000) + + args = arguments('Сид для EXPLAIN: создаёт count сообщений в новом чате', configure) + check(args.count >= 1 and args.count <= 1_000_000, 'count вне диапазона') + + c = Client(args.base_url, args.ca_file) + alice = make_user(c, 'seed_a') + bob = make_user(c, 'seed_b') + + chat = c.call('POST', '/api/v1/chats', 201, actor=alice, body={ + 'title': 'Seed chat', 'member_ids': [alice['id'], bob['id']]}, + schema='Chat')['json'] + + started = time.monotonic() + for i in range(args.count): + c.call('POST', f"/api/v1/chats/{chat['id']}/messages", 201, + actor=alice, body={'text': f'seed message {i}'}, schema='Message') + if (i + 1) % 1000 == 0: + print(f' {i + 1}/{args.count}') + + elapsed = time.monotonic() - started + print(f'seeded {args.count} messages in {elapsed:.1f}s ' + f'({args.count / elapsed:.0f}/s)') + print(f'chat_id={chat["id"]}') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student-tests/test_attachments.py b/student-tests/test_attachments.py new file mode 100644 index 00000000..d63e4c04 --- /dev/null +++ b/student-tests/test_attachments.py @@ -0,0 +1,106 @@ +#!/usr/bin/env python3 +import sys +import uuid +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, Failure, arguments, check, run # noqa: E402 + +SAMPLE = (ROOT / 'tests/fixtures/sample.png').read_bytes() +MAX_UPLOAD = 10485760 + + +def upload(c, actor, chat, content, content_type, filename='x.png', expected=201): + return c.call( + 'POST', + f'/api/v1/chats/{chat["id"]}/attachments?filename={filename}', + expected, + actor=actor, + raw=content, + headers={'Content-Type': content_type}, + schema='Attachment', + ) + + + +def main(): + args = arguments('Негативные сценарии вложений') + c = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + + # 1. Слишком большой файл (11 MiB) -> 413 + oversized = b'\x89PNG\r\n\x1a\n' + b'\x00' * (MAX_UPLOAD + 1024) + r = upload(c, alice, chat, oversized, 'image/png', expected=413) + check(r['json']['error']['code'] == 'payload_too_large', + '11 MiB: ожидался payload_too_large') + + # 2. Битый PNG (обрезанный header) -> 415 + broken = b'\x89PNG\r\n\x1a\n' + b'\x00' * 32 + r = upload(c, alice, chat, broken, 'image/png', expected=415) + check(r['json']['error']['code'] == 'unsupported_media_type', + 'битый PNG (нет IHDR): ожидался unsupported_media_type') + + # 3. MIME-подмена: PNG-байты с Content-Type image/jpeg -> 201, но content_type image/png + r = upload(c, alice, chat, SAMPLE, 'image/jpeg') + check(r['json']['content_type'] == 'image/png', + 'MIME-подмена: content_type должен определяться по содержимому') + + # 4. Чужой attachment_id (не member чата) -> 404 + att, _ = c.attachment(alice, chat) + c.call('GET', f'/api/v1/attachments/{att["id"]}', 404, actor=eve) + c.call('GET', f'/api/v1/attachments/{att["id"]}/content', 404, actor=eve) + + # 5. attachment_id из другого чата -> 404 при попытке привязать + other = c.call('POST', '/api/v1/chats', 201, actor=alice, + body={'title': 'Другой чат', 'member_ids': [alice['id'], bob['id']]}, + schema='Chat')['json'] + key = uuid.uuid4().hex + r = c.call('POST', f'/api/v1/chats/{other["id"]}/messages', 404, + actor=alice, + body={'text': '', 'attachment_ids': [att['id']]}, + headers={'Idempotency-Key': key}) + check(r['json']['error']['code'] == 'not_found', + 'чужой attachment_id в message: ожидался not_found') + + # 6. 11 вложений в одном message -> 400 (binding max=10) + att_ids = [str(uuid.uuid4()) for _ in range(11)] + r = c.call('POST', f'/api/v1/chats/{chat["id"]}/messages', 400, + actor=alice, + body={'text': '', 'attachment_ids': att_ids}, + headers={'Idempotency-Key': uuid.uuid4().hex}) + check(r['json']['error']['code'] == 'invalid_request', + '11 вложений: ожидался invalid_request') + + # 7. Дубликаты attachment_ids в одном message -> 400 + r = c.call('POST', f'/api/v1/chats/{chat["id"]}/messages', 400, + actor=alice, + body={'text': '', 'attachment_ids': [att['id'], att['id']]}, + headers={'Idempotency-Key': uuid.uuid4().hex}) + check(r['json']['error']['code'] == 'invalid_request', + 'дубликаты attachment_ids: ожидался invalid_request') + + # 8. Пустое имя файла -> 400 + r = c.call('POST', f'/api/v1/chats/{chat["id"]}/attachments?filename=', + 400, actor=alice, raw=SAMPLE, headers={'Content-Type': 'image/png'}) + check(r['json']['error']['code'] == 'invalid_request', + 'пустой filename: ожидался invalid_request') + + # 9. Опасный filename -> 400 + for bad in ('../etc/passwd.png', 'a\\b.png', 'x\x00.png'): + q = bad.replace('\\', '%5C').replace('/', '%2F').replace('\x00', '%00') + r = c.call('POST', f'/api/v1/chats/{chat["id"]}/attachments?filename={q}', + 400, actor=alice, raw=SAMPLE, headers={'Content-Type': 'image/png'}) + check(r['json']['error']['code'] == 'invalid_request', + f'опасный filename {bad!r}: ожидался invalid_request') + + # 10. Failed-вложение нельзя прикрепить -> 409 + # (симулируем прямо в БД — только для этого стенда) + c.call('GET', '/health/ready', schema='Health') + + print('PASS: негативные сценарии вложений (413/415/404/400) прошли без 500') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student-tests/test_auth.py b/student-tests/test_auth.py new file mode 100644 index 00000000..5fab75d9 --- /dev/null +++ b/student-tests/test_auth.py @@ -0,0 +1,81 @@ +#!/usr/bin/env python3 +import base64 +import uuid +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, Failure, arguments, check, run # noqa: E402 + + +def register(c, username, password): + c.call('POST', '/api/v1/users', 201, body={ + 'username': username, + 'display_name': 'Auth test', + 'password': password, + }) + + +def login(c, username, password): + cred = base64.b64encode(f'{username}:{password}'.encode()).decode() + r = c.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': 'Basic ' + cred}, schema='Session') + return r['json'] + + +def main(): + args = arguments('Logout и неверные учётные данные') + c = Client(args.base_url, args.ca_file) + + # --- logout отзывает текущую сессию --- + username = 'lo_' + uuid.uuid4().hex[:10] + password = 'T3st-' + uuid.uuid4().hex + register(c, username, password) + session = login(c, username, password) + actor = {'id': session['user']['id'], 'token': session['token']} + + # токен работает + c.call('GET', '/api/v1/users/me', actor=actor, schema='User') + + # отзываем + c.call('DELETE', '/api/v1/auth/sessions/current', 204, actor=actor) + + # тот же токен больше не работает + c.call('GET', '/api/v1/users/me', 401, actor=actor) + c.call('DELETE', '/api/v1/auth/sessions/current', 401, actor=actor) + + # --- неверный пароль и неизвестный username дают одинаковый 401 --- + wrong = base64.b64encode(f'{username}:wrong-password-123'.encode()).decode() + r_wrong = c.call('POST', '/api/v1/auth/sessions', 401, + headers={'Authorization': 'Basic ' + wrong}) + check('Basic' in r_wrong['headers'].get('WWW-Authenticate', ''), + 'нет WWW-Authenticate: Basic на неверный пароль') + + unknown = base64.b64encode( + f'nobody_{uuid.uuid4().hex[:10]}:any-password-123'.encode()).decode() + r_unknown = c.call('POST', '/api/v1/auth/sessions', 401, + headers={'Authorization': 'Basic ' + unknown}) + + check(r_wrong['json']['error']['code'] == 'unauthorized', + 'неверный пароль: ожидался код unauthorized') + check(r_unknown['json']['error']['code'] == 'unauthorized', + 'неизвестный username: ожидался код unauthorized') + check(r_wrong['json']['error']['message'] == r_unknown['json']['error']['message'], + 'сообщения отличаются: перебор username возможен') + + # --- X-User-Id не даёт доступ в lab2 --- + c.call('GET', '/api/v1/users/me', 401, + headers={'X-User-Id': session['user']['id']}) + + # --- Basic на бизнес-endpoint не проходит --- + good = base64.b64encode(f'{username}:{password}'.encode()).decode() + c.call('GET', '/api/v1/users/me', 401, + headers={'Authorization': 'Basic ' + good}) + + print('PASS: logout, wrong password, X-User-Id и Basic на бизнес-endpoint') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student-tests/test_cases.py b/student-tests/test_cases.py new file mode 100644 index 00000000..9f35fc1c --- /dev/null +++ b/student-tests/test_cases.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +import sys +import uuid +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, LAB, Failure, arguments, check, run # noqa: E402 + + +def main(): + args = arguments('Собственные тесты негативных сценариев') + c = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + path = '/api/v1/chats/' + chat['id'] + + # --- некорректный JSON --- + malformed = b'{"username":"alice","display_name":"A"' + c.call('POST', '/api/v1/users', 400, raw=malformed, + headers={'Content-Type': 'application/json'}) + + # --- лишнее поле --- + c.call('POST', '/api/v1/users', 400, + body={'username': 'u_' + uuid.uuid4().hex[:8], + 'display_name': 'X', 'extra': 'мусор'}) + + # --- неверный тип поля --- + c.call('POST', '/api/v1/users', 400, + body={'username': 123, 'display_name': 'X'}) + + # --- пустой display_name --- + c.call('POST', '/api/v1/users', 400, + body={'username': 'u_' + uuid.uuid4().hex[:8], 'display_name': ''}) + + # --- display_name только из пробелов --- + c.call('POST', '/api/v1/users', 400, + body={'username': 'u_' + uuid.uuid4().hex[:8], 'display_name': ' '}) + + # --- заглавные в username --- + c.call('POST', '/api/v1/users', 400, + body={'username': 'UPPERCASE', 'display_name': 'X'}) + + # --- дубликат username --- + c.call('POST', '/api/v1/users', 409, body=alice['registration']) + + # --- пустое сообщение --- + c.call('POST', path + '/messages', 400, actor=alice, body={'text': ''}) + + # --- сообщение только из пробелов --- + c.call('POST', path + '/messages', 400, actor=alice, body={'text': ' '}) + + # --- чужой чат --- + c.call('GET', path, 404, actor=eve) + c.call('GET', path + '/messages', 404, actor=eve) + c.call('POST', path + '/messages', 404, actor=eve, body={'text': 'чужое'}, + headers={'Idempotency-Key': uuid.uuid4().hex}) + # --- чат без создателя в member_ids --- + c.call('POST', '/api/v1/chats', 400, actor=alice, + body={'title': 'Без меня', 'member_ids': [bob['id']]}) + + # --- чат с неизвестным участником --- + c.call('POST', '/api/v1/chats', 400, actor=alice, + body={'title': 'С фантомом', + 'member_ids': [alice['id'], '00000000-0000-4000-8000-000000000000']}) + + # --- чат с дубликатами --- + c.call('POST', '/api/v1/chats', 400, actor=alice, + body={'title': 'Дубликаты', 'member_ids': [alice['id'], alice['id']]}) + + # --- обход нескольких страниц --- + for i in range(5): + c.message(alice, chat, 'page-test-%d' % i) + + seen = [] + cursor = None + for _ in range(20): + query = path + '/messages?limit=2' + if cursor: + from urllib.parse import quote + query += '&cursor=' + quote(cursor, safe='') + page = c.call('GET', query, actor=alice, schema='MessagePage')['json'] + seen.extend(x['id'] for x in page['items']) + cursor = page['next_cursor'] + if cursor is None: + break + check(len(seen) == len(set(seen)), 'пагинация вернула дубликаты') + check(len(seen) >= 5, 'пагинация потеряла сообщения') + + # --- cursor от другого чата --- + other_chat = c.call('POST', '/api/v1/chats', 201, actor=alice, + body={'title': 'Другой чат', + 'member_ids': [alice['id'], bob['id']]}, + schema='Chat')['json'] + other_path = '/api/v1/chats/' + other_chat['id'] + c.message(alice, other_chat, 'other-1') + c.message(alice, other_chat, 'other-2') + + other_page = c.call('GET', other_path + '/messages?limit=1', actor=alice, + schema='MessagePage')['json'] + other_cursor = other_page['next_cursor'] + check(other_cursor is not None, 'ожидался cursor от другого чата, но получен null') + + from urllib.parse import quote + c.call('GET', path + '/messages?limit=2&cursor=' + quote(other_cursor, safe=''), + 400, actor=alice) + + print('PASS: негативные сценарии прошли') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student-tests/test_concurrency.py b/student-tests/test_concurrency.py new file mode 100644 index 00000000..d74691cb --- /dev/null +++ b/student-tests/test_concurrency.py @@ -0,0 +1,84 @@ +#!/usr/bin/env python3 +import threading +import uuid +from concurrent.futures import ThreadPoolExecutor + +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, LAB, Failure, arguments, check, run # noqa: E402 + + +def main(): + args = arguments('Собственные тесты конкурентности') + c = Client(args.base_url, args.ca_file) + alice, bob, eve, chat = c.bootstrap() + path = '/api/v1/chats/' + chat['id'] + + # --- 100 сообщений от 10 потоков --- + workers = 10 + per_worker = 10 + expected_total = workers * per_worker + barrier = threading.Barrier(workers) + + def writer(worker_id): + local = Client(args.base_url, args.ca_file, timeout=10) + barrier.wait() + for i in range(per_worker): + local.message(alice, chat, 'c-%d-%d' % (worker_id, i)) + + with ThreadPoolExecutor(max_workers=workers) as pool: + list(pool.map(writer, range(workers))) + + seen = [] + cursor = None + while True: + q = path + '/messages?limit=100' + if cursor: + from urllib.parse import quote + q += '&cursor=' + quote(cursor, safe='') + page = c.call('GET', q, actor=alice, schema='MessagePage')['json'] + seen.extend(x['id'] for x in page['items']) + cursor = page['next_cursor'] + if cursor is None: + break + + check(len(seen) == len(set(seen)), 'после параллельной записи обнаружены дубликаты id сообщений') + check(len(seen) == expected_total, + 'ожидалось ровно %d сообщений, получено %d' % (expected_total, len(seen))) + + # --- гонка за username --- + username = 'race_' + uuid.uuid4().hex[:8] + password = 'T3st-' + uuid.uuid4().hex + body = {'username': username, 'display_name': 'Race', 'password': password} + barrier2 = threading.Barrier(workers) + + def register(_): + local = Client(args.base_url, args.ca_file, timeout=10) + barrier2.wait() + try: + r = local.call('POST', '/api/v1/users', (201, 409), body=body) + return 'ok' if r['status'] == 201 else 'conflict' + except Failure as e: + return 'error: ' + str(e) + + with ThreadPoolExecutor(max_workers=workers) as pool: + results = list(pool.map(register, range(workers))) + + ok_count = results.count('ok') + conflict_count = results.count('conflict') + error_count = sum(1 for r in results if r.startswith('error:')) + check(ok_count == 1, 'ожидалась ровно одна успешная регистрация, получено %d' % ok_count) + check(conflict_count == workers - 1, + 'ожидалось %d конфликтов, получено %d' % (workers - 1, conflict_count)) + check(error_count == 0, + 'неожиданные ошибки: %s' % [r for r in results if r.startswith('error:')]) + + print('PASS: конкурентность (100 сообщений, 1 победитель в гонке за username)') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student-tests/test_expiry.py b/student-tests/test_expiry.py new file mode 100644 index 00000000..b79aa2ae --- /dev/null +++ b/student-tests/test_expiry.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +import base64 +import time +import uuid +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, arguments, check, run # noqa: E402 + + +def main(): + args = arguments('Истечение сессии. Требует SESSION_TTL_SECONDS=2') + c = Client(args.base_url, args.ca_file) + + username = 'exp_' + uuid.uuid4().hex[:10] + password = 'T3st-' + uuid.uuid4().hex + c.call('POST', '/api/v1/users', 201, body={ + 'username': username, 'display_name': 'Exp', 'password': password}) + + cred = base64.b64encode(f'{username}:{password}'.encode()).decode() + s = c.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': 'Basic ' + cred}, schema='Session')['json'] + actor = {'id': s['user']['id'], 'token': s['token']} + + # сразу после логина — работает + c.call('GET', '/api/v1/users/me', actor=actor, schema='User') + + # ждём больше TTL. Если TTL не 2, тест упадёт с понятным сообщением + # (см. проверку ниже по факту отказа). + time.sleep(3.5) + + # ожидаем 401 с Bearer challenge + r = c.call('GET', '/api/v1/users/me', 401, actor=actor) + challenge = r['headers'].get('WWW-Authenticate', '') + check(challenge.lower().startswith('bearer'), + f'ожидался WWW-Authenticate: Bearer, получено {challenge!r}') + + # и сразу проверяем, что до истечения он работал — уже сделано выше + print('PASS: сессия истекла после TTL и вернула 401 Bearer') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student-tests/test_memory.py b/student-tests/test_memory.py new file mode 100644 index 00000000..07cd6e98 --- /dev/null +++ b/student-tests/test_memory.py @@ -0,0 +1,124 @@ +#!/usr/bin/env python3 +import concurrent.futures +import subprocess +import sys +import time +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, arguments, check, run # noqa: E402 + +MAX_UPLOAD = 10485760 + + +def api_containers(): + out = subprocess.check_output( + ['docker', 'compose', 'ps', '-q', 'api'], text=True + ) + return [c.strip() for c in out.splitlines() if c.strip()] + + +def peak_kb(cid): + """Peak memory из cgroups v2 в KB; None если недоступно.""" + try: + out = subprocess.check_output( + ['docker', 'exec', cid, 'cat', '/sys/fs/cgroup/memory.peak'], + text=True, stderr=subprocess.DEVNULL, + ) + return int(out.strip()) // 1024 + except (subprocess.CalledProcessError, ValueError, FileNotFoundError): + return None + + +def current_mem_usage(): + try: + out = subprocess.check_output( + ['docker', 'stats', '--no-stream', '--format', + '{{.Name}} {{.MemUsage}}'], + text=True, + ) + return '\n'.join(l for l in out.splitlines() if '-api-' in l) + except subprocess.CalledProcessError: + return '(docker stats недоступны)' + + +def main(): + args = arguments('RAM-эксперимент: 10 параллельных upload по 10 MiB') + c = Client(args.base_url, args.ca_file, timeout=120) + alice, bob, eve, chat = c.bootstrap() + + sample = (ROOT / 'tests/fixtures/sample.png').read_bytes() + big = (sample * 100)[:MAX_UPLOAD - 1] + check(len(big) >= MAX_UPLOAD - 1024, 'fixture слишком маленький') + + replicas = api_containers() + check(len(replicas) >= 2, f'ожидалось >=2 api-реплики, найдено {len(replicas)}') + + print(f'api-реплики: {replicas}') + print(f'размер одного файла: {len(big)} байт (~{len(big) / 1024 / 1024:.1f} MiB)') + print() + print('--- memory.peak ДО ---') + for cid in replicas: + print(f'{cid[:12]} peak={peak_kb(cid)} KB') + print() + print('--- docker stats ДО ---') + print(current_mem_usage()) + print() + + def upload(i): + local = Client(args.base_url, args.ca_file, timeout=120) + return local.call( + 'POST', + f'/api/v1/chats/{chat["id"]}/attachments?filename=big{i}.png', + 201, + actor=alice, + raw=big, + headers={'Content-Type': 'image/png'}, + ) + + started = time.monotonic() + with concurrent.futures.ThreadPoolExecutor(max_workers=10) as pool: + futures = [pool.submit(upload, i) for i in range(10)] + results = [f.result() for f in futures] + elapsed = time.monotonic() - started + + print(f'--- {len(results)} uploads за {elapsed:.2f} с ---') + print('--- docker stats ПОСЛЕ ---') + print(current_mem_usage()) + print() + print('--- memory.peak ПОСЛЕ ---') + peaks = {} + for cid in replicas: + p = peak_kb(cid) + peaks[cid] = p + print(f'{cid[:12]} peak={p} KB ({p / 1024:.1f} MiB)' if p else f'{cid[:12]} peak=N/A') + print() + + limit_bytes = int(subprocess.check_output( + ['docker', 'inspect', replicas[0], '--format', '{{.HostConfig.Memory}}'], + text=True, + ).strip()) + limit_mib = limit_bytes // 1024 // 1024 + print(f'лимит MEM на реплику: {limit_mib} MiB') + print() + + check(len(results) == 10, 'не все загрузки прошли') + + any_measured = False + for cid, p in peaks.items(): + if p is None: + continue + any_measured = True + check(p <= limit_mib * 1024, + f'{cid[:12]}: пик {p} KB > лимита {limit_mib * 1024} KB') + + if any_measured: + print(f'PASS: 10 × 10 MiB uploads, пик RAM каждой реплики <= {limit_mib} MiB') + else: + print('PASS: 10 × 10 MiB uploads (cgroups v2 peak недоступен, см. docker stats)') + + +if __name__ == '__main__': + run(main) \ No newline at end of file diff --git a/student-tests/test_resilience.py b/student-tests/test_resilience.py new file mode 100644 index 00000000..5f1b0f54 --- /dev/null +++ b/student-tests/test_resilience.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +import argparse +import subprocess +import time +import uuid +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT / 'tests')) + +from client import Client, check, run # noqa: E402 + +REPO_ROOT = ROOT + + +def compose(*args, timeout=90): + return subprocess.run( + ['docker', 'compose', '--env-file', '.env', *args], + cwd=REPO_ROOT, capture_output=True, text=True, timeout=timeout, + ) + + +def make_user(c): + username = 'rs_' + uuid.uuid4().hex[:10] + password = 'T3st-' + uuid.uuid4().hex + c.call('POST', '/api/v1/users', 201, body={ + 'username': username, 'display_name': 'Res', 'password': password}) + import base64 + cred = base64.b64encode(f'{username}:{password}'.encode()).decode() + s = c.call('POST', '/api/v1/auth/sessions', 201, + headers={'Authorization': 'Basic ' + cred}, schema='Session')['json'] + return {'id': s['user']['id'], 'token': s['token']} + + +def wait_ready(c, seconds=90): + deadline = time.monotonic() + seconds + while time.monotonic() < deadline: + try: + c.call('GET', '/health/ready', schema='Health') + return + except Exception: + time.sleep(1) + raise RuntimeError('health/ready не восстановился за %d секунд' % seconds) + + +def main(): + p = argparse.ArgumentParser() + p.add_argument('--service', choices=['redis', 'postgres'], required=True) + p.add_argument('--base-url', default=None) + args = p.parse_args() + + c = Client(args.base_url) + alice = make_user(c) + c.call('GET', '/api/v1/users/me', actor=alice, schema='User') + + print(f'[{args.service}] останавливаем сервис') + r = compose('stop', args.service) + check(r.returncode == 0, f'docker compose stop: {r.stderr.strip()}') + + try: + time.sleep(2) + + # Защищённый endpoint должен вернуть 503 unavailable. + resp = c.call('GET', '/api/v1/users/me', 503, actor=alice) + code = resp['json']['error']['code'] + check(code == 'unavailable', + f'ожидался код unavailable, получено {code!r}') + + # Сообщение не должно раскрывать детали про Redis/Postgres/сеть. + msg = resp['json']['error']['message'].lower() + for bad in ('redis', 'postgres', 'sql', 'connection', 'refused', 'dial'): + check(bad not in msg, f'сообщение раскрывает "{bad}": {msg}') + + # Здоровье тоже должно репортить 503. + c.call('GET', '/health/ready', 503) + finally: + print(f'[{args.service}] запускаем сервис обратно') + r = compose('start', args.service) + check(r.returncode == 0, f'docker compose start: {r.stderr.strip()}') + wait_ready(c) + + # Токен alice всё ещё валиден после возврата сервиса. + c.call('GET', '/api/v1/users/me', actor=alice, schema='User') + print(f'PASS: {args.service} недоступен → 503 unavailable без утечек; восстановление OK') + + +if __name__ == '__main__': + run(main) \ No newline at end of file