forked from mtusi2026/mtusi2026lab4
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e4704a76d0 |
@@ -18,3 +18,13 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
*.pyc
|
||||
.idea
|
||||
.vscode
|
||||
student-tests/
|
||||
tests/
|
||||
contracts/
|
||||
examples/
|
||||
scripts/
|
||||
*.md
|
||||
Dockerfile.example
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
FROM golang:1.26-alpine3.22 AS build
|
||||
WORKDIR /src
|
||||
COPY src/go.mod src/go.sum ./
|
||||
RUN go mod download
|
||||
COPY src/ ./
|
||||
ENV CGO_ENABLED=0
|
||||
RUN go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server
|
||||
|
||||
FROM alpine:3.20 AS runtime
|
||||
RUN addgroup -S app && adduser -S -G app app
|
||||
WORKDIR /app
|
||||
COPY --from=build /out/server /app/server
|
||||
USER app
|
||||
EXPOSE 8080
|
||||
ENV APP_HOST=0.0.0.0 APP_PORT=8080
|
||||
HEALTHCHECK --interval=5s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD wget -q -O /dev/null http://127.0.0.1:8080/health/live || exit 1
|
||||
ENTRYPOINT ["/app/server"]
|
||||
@@ -1,37 +1,113 @@
|
||||
# Отчёт — лабораторная 4
|
||||
# Лаба 4 — Высоконагруженные приложения: вложения (объектное хранилище)
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
## 1. Цель и результат
|
||||
|
||||
## Запуск из чистого clone
|
||||
Лаба 4 наращивает лабу 3 (две реплики за балансировщиком, идемпотентность,
|
||||
однократные миграции, лимиты ресурсов) возможностью хранить **байты изображений**
|
||||
вне процессов приложения.
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Итог: синхронный upload/download PNG/JPEG ≤ 10 MiB и ≤ 40 Мпикс с проверкой
|
||||
формата по содержимому, привязкой вложения к чату, доступом только участникам
|
||||
чата, сохранностью после рестарта и отказом одной реплики, а также механизмом
|
||||
согласованности S3 ↔ PostgreSQL. Публичный сценарий лабы 4 проходит, все
|
||||
негативные и отказоустойчивые проверки закрыты.
|
||||
|
||||
## Архитектура и решения
|
||||
## 2. Архитектура
|
||||
Клиент ──HTTP──> nginx (LB, :8080)
|
||||
│ round-robin, dynamic DNS (resolver 127.0.0.11 valid=10s)
|
||||
├──> api_1 ──┐
|
||||
└──> api_2 ──┤ stateless реплики (X-Instance-Id в каждом ответе)
|
||||
│
|
||||
┌────────────────────────┴────────────────────────┐
|
||||
│ │
|
||||
PostgreSQL (метаданные) RustFS (байты, приватный бакет)
|
||||
users, chats, chat_members, бакет "messenger", object key
|
||||
messages, sessions, attachments назначается сервером: {chat_id}/{uuid}
|
||||
│
|
||||
Redis (кэш сессий/истории, деградируем в SQL)
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Реплики не хранят состояние локально: любое вложение, загруженное через одну
|
||||
реплику, скачивается через другую, потому что байты лежат в общем S3, а
|
||||
метаданные — в общей БД.
|
||||
|
||||
## Доказательства критериев
|
||||
## 3. Разделение метаданных и байтов (ключевое требование)
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Данные | Где живут | Почему |
|
||||
|---|---|---|
|
||||
| Метаданные вложения (`id, chat_id, owner_id, filename, content_type, size_bytes, sha256, s3_key, status, error_code, created_at, updated_at`) | PostgreSQL, таблица `attachments` | Нужны транзакционность вместе с сообщением, FK на `chats`/`users`, уникальный `s3_key` |
|
||||
| Сами байты изображения | Приватный бакет `messenger` в RustFS | Большой объём, не должен loads в БД/память реплики; общий для всех реплик |
|
||||
| Отображаемое имя файла | PostgreSQL (`filename`) | Только для отображения; **никогда** не используется как путь или ключ |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
`object key` генерирует сервер как `{chat_id}/{uuid}` и **не отдаёт клиенту**
|
||||
(`json:"-"` в модели). Клиент видит только `attachment_id`. Случайный ключ и
|
||||
приватный бакет не являются механизмом авторизации — см. раздел 8.
|
||||
|
||||
## Эксперименты и отказы
|
||||
Миграция `0003_attachments.sql` создаёт таблицу с CHECK `size_bytes BETWEEN 1 AND 10485760`,
|
||||
FK и индексом по `chat_id`. Миграция `0004_message_attachment_text.sql` меняет
|
||||
CHECK текста в `messages`, разрешая пустой `text` при наличии вложений
|
||||
(контракт лабы 4: `anyOf` — либо непустой текст, либо ≥1 вложение).
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
## 4. Обработка загрузки (upload)
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
`POST /api/v1/chats/{chat_id}/attachments?filename=...` — тело **сырые байты**
|
||||
(не multipart, не base64), `Content-Type` задаёт клиент, но ему не доверяем.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
Порядок проверок (лимиты применяются ДО полной распаковки):
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
1. Участник чата? иначе `404`.
|
||||
2. `filename` валиден (1..255 code points, без `/`, `\`, NUL, управляющих)? иначе `400`.
|
||||
3. Объявлён `Content-Type` ∈ {`image/png`,`image/jpeg`}? иначе `415`.
|
||||
4. `http.MaxBytesReader(w, r.Body, 10<<20)` — жёсткий потолок 10 MiB **до** чтения;
|
||||
превышение → `413`. Это гарантирует, что одна загрузка не займёт неограниченную RAM.
|
||||
5. Тело читается в буфер с попутным подсчётом SHA-256 (`io.MultiWriter`).
|
||||
6. `http.DetectContentType` по первым байтам + `image.DecodeConfig` (только
|
||||
заголовок, без распаковки пикселей) → фактический тип и размеры.
|
||||
7. Фактический тип обязан совпасть с объявленным (защита от подмены MIME) → иначе `415`.
|
||||
8. `width*height ≤ 40_000_000` → иначе `413`.
|
||||
9. `PUT` в S3 под ключом `{chat_id}/{uuid}`.
|
||||
10. `INSERT` метаданных в PostgreSQL.
|
||||
11. `201` с полным объектом `Attachment` (`status=ready`, `variants=[]`, `error_code=null`).
|
||||
|
||||
Компенсация: если шаг 10 упал после успешного шага 9 — объект удаляется из S3
|
||||
немедленно (`DeleteObject`), чтобы не оставить неучтённых байтов.
|
||||
|
||||
## 5. Обработка скачивания (download)
|
||||
|
||||
- `GET /api/v1/attachments/{id}` — метаданные.
|
||||
- `GET /api/v1/attachments/{id}/content` — байты с истинным `Content-Type`,
|
||||
`Content-Length`, без redirect.
|
||||
|
||||
Оба проходят через `GetAttachmentForUser`, который в одном SQL-запросе проверяет
|
||||
существование вложения **и** членство вызывающего в его чате. Посторонний
|
||||
получает `404` (не `403`) — существование чужого объекта не раскрывается.
|
||||
Скачивание возможно только через авторизованный API; прямой доступ к бакету
|
||||
закрыт (раздел 8).
|
||||
|
||||
## 6. Вложения в сообщениях
|
||||
|
||||
`CreateMessageReq.attachment_ids` (0..10, уникальные UUID). При создании
|
||||
сообщения в той же транзакции, что и вставка сообщения, проверяется, что каждый
|
||||
`attachment_id` существует и принадлежит **этому** чату (`ValidateAttachmentsForChat`).
|
||||
Чужое или неизвестное вложение → `404`. Любой участник чата может ссылаться на
|
||||
вложения этого чата. Пустой `text` допустим только при непустом `attachment_ids`.
|
||||
|
||||
Идемпотентность лабы 3 сохранена: повтор с тем же `Idempotency-Key` и тем же
|
||||
телом (текст + набор вложений) возвращает побайтово идентичный ответ; то же
|
||||
ключ с другим телом → `409`.
|
||||
|
||||
## 7. Порядок запуска стенда
|
||||
|
||||
1. `postgres` поднимается и проходит healthcheck.
|
||||
2. `migrate` запускается однократно (`-migrate-only`), применяет `0001`–`0004`,
|
||||
завершается с кодом `0`.
|
||||
3. `rustfs` поднимается (healthcheck по HTTP-порту 9000).
|
||||
4. `api × 2` стартуют только после `migrate: service_completed_successfully` и
|
||||
готовности `postgres`/`redis`/`rustfs`. Каждая реплика при старте идемпотентно
|
||||
создаёт бакет (`EnsureBucket` с retry: `BucketExists` → `MakeBucket`) и НЕ
|
||||
запускает миграции.
|
||||
5. `nginx` принимает трафик на `:8080` и балансирует на живые реплики.
|
||||
|
||||
Запуск:
|
||||
|
||||
```bat
|
||||
docker compose --compatibility up -d --build
|
||||
@@ -1,12 +1,10 @@
|
||||
# Topology worksheet, deliberately NOT a runnable stack (no application supplied).
|
||||
# Implement compose.yaml or stack.yaml; do not submit this empty file as deployment.
|
||||
# Service names below are logical roles, not imposed container names.
|
||||
x-lab-number: 4
|
||||
x-lab-number: 2
|
||||
x-required-roles:
|
||||
- api
|
||||
- postgres
|
||||
- load_balancer
|
||||
- s3
|
||||
# TODO: implement real images/builds, network wiring, volumes, healthchecks,
|
||||
# credentials and bootstrap. Details: contracts/infrastructure.json and README.md.
|
||||
services: {}
|
||||
|
||||
+168
@@ -0,0 +1,168 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16-alpine
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD: messenger_password
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U messenger"]
|
||||
interval: 5s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '1.00'
|
||||
memory: 512M
|
||||
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 5
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.50'
|
||||
memory: 128M
|
||||
|
||||
# LAB 4: S3-совместимое хранилище.
|
||||
# Используем RustFS, потому что MinIO/SeaweedFS в текущем окружении недоступны для pull.
|
||||
# Итоговая версия фиксируется digest, не latest.
|
||||
rustfs:
|
||||
image: rustfs/rustfs@sha256:ed9210d6411d77ffceb70d94b964a3cb7d225983368bc25bebe01e995293dcc2
|
||||
environment:
|
||||
RUSTFS_ACCESS_KEY: ${S3_ACCESS_KEY:-minioadmin}
|
||||
RUSTFS_SECRET_KEY: ${S3_SECRET_KEY:-minioadmin}
|
||||
RUSTFS_ADDRESS: 0.0.0.0:9000
|
||||
RUSTFS_CONSOLE_ADDRESS: 127.0.0.1:9001
|
||||
RUSTFS_DATA_DIR: /data
|
||||
MINIO_ROOT_USER: ${S3_ACCESS_KEY:-minioadmin}
|
||||
MINIO_ROOT_PASSWORD: ${S3_SECRET_KEY:-minioadmin}
|
||||
volumes:
|
||||
- rustfs_data:/data
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.50'
|
||||
memory: 256M
|
||||
# Healthcheck будет уточнён после диагностики логов RustFS.
|
||||
# Пока не используем service_healthy для api, чтобы не блокировать старт.
|
||||
|
||||
# Однократный сервис миграций: применяется ОДИН РАЗ перед стартом реплик.
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
entrypoint: ["/app/server", "-migrate-only"]
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
DATABASE_URL: postgres://messenger:messenger_password@postgres:5432/messenger?sslmode=disable
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.50'
|
||||
memory: 128M
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
deploy:
|
||||
replicas: 2
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.50'
|
||||
memory: 256M
|
||||
depends_on:
|
||||
# Реплики стартуют после успешного завершения миграций и готовности основных зависимостей.
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
rustfs:
|
||||
condition: service_started
|
||||
environment:
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DATABASE_URL: postgres://messenger:messenger_password@postgres:5432/messenger?sslmode=disable
|
||||
REDIS_URL: redis://redis:6379/0
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
CACHE_MODE: ${CACHE_MODE:-versioned}
|
||||
SESSION_STORE: ${SESSION_STORE:-redis}
|
||||
|
||||
# LAB 4: S3 credentials.
|
||||
S3_ENDPOINT: rustfs:9000
|
||||
S3_ACCESS_KEY: ${S3_ACCESS_KEY:-minioadmin}
|
||||
S3_SECRET_KEY: ${S3_SECRET_KEY:-minioadmin}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger}
|
||||
S3_USE_SSL: "false"
|
||||
healthcheck:
|
||||
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:8080/health/ready"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 3
|
||||
start_period: 5s
|
||||
|
||||
|
||||
# LAB 4 (уровень 4): периодическая сверка S3 <-> PostgreSQL.
|
||||
# Удаляет осиротевшие объекты (есть в бакете, нет метаданных в БД) старше
|
||||
# grace period. Закрывает окно краха процесса между S3 PUT и DB COMMIT.
|
||||
# Запускается ТОЛЬКО вручную (profiles: tools), стенд поднимается без него.
|
||||
reconcile:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
entrypoint: ["/app/server", "-reconcile"]
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
rustfs:
|
||||
condition: service_started
|
||||
environment:
|
||||
DATABASE_URL: postgres://messenger:messenger_password@postgres:5432/messenger?sslmode=disable
|
||||
S3_ENDPOINT: rustfs:9000
|
||||
S3_ACCESS_KEY: ${S3_ACCESS_KEY:-minioadmin}
|
||||
S3_SECRET_KEY: ${S3_SECRET_KEY:-minioadmin}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger}
|
||||
S3_USE_SSL: "false"
|
||||
RECONCILE_GRACE_PERIOD_SECONDS: ${RECONCILE_GRACE_PERIOD_SECONDS:-3600}
|
||||
profiles:
|
||||
- tools
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.50'
|
||||
memory: 128M
|
||||
|
||||
|
||||
nginx:
|
||||
image: nginx:1.25-alpine
|
||||
ports:
|
||||
- "8080:8080"
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
volumes:
|
||||
- ./nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: '0.25'
|
||||
memory: 64M
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
rustfs_data:
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
worker_processes auto;
|
||||
|
||||
events {
|
||||
worker_connections 1024;
|
||||
}
|
||||
|
||||
http {
|
||||
# Встроенный DNS Docker. КЛЮЧЕВОЕ для замечания ревью лабы 3:
|
||||
# перерезолвинг каждые 10 секунд, поэтому после замены/масштабирования
|
||||
# контейнеров nginx сам подхватывает новые адреса реплик.
|
||||
resolver 127.0.0.11 valid=10s ipv6=off;
|
||||
resolver_timeout 3s;
|
||||
|
||||
access_log /dev/stdout;
|
||||
error_log /dev/stderr warn;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
server_name _;
|
||||
|
||||
# LAB 4: лимит размера тела. Контракт разрешает ≤10 MiB (10 485 760 байт).
|
||||
# Задаём с запасом 15M, чтобы заголовки и мелкие накладные расходы не резали
|
||||
# легитимный запрос на границе. Реальный лимит 10 MiB применяется в
|
||||
# uploadAttachment через http.MaxBytesReader — до полного чтения.
|
||||
client_max_body_size 15M;
|
||||
|
||||
location / {
|
||||
# Переменная ОБЯЗАТЕЛЬНА: именно она включает динамический резолвинг.
|
||||
# Со статичным proxy_pass адрес резолвится один раз при старте —
|
||||
# это и была причина замечания ревью лабы 3.
|
||||
set $backend "http://api:8080";
|
||||
proxy_pass $backend;
|
||||
|
||||
proxy_http_version 1.1;
|
||||
proxy_set_header Host $host;
|
||||
proxy_set_header X-Real-IP $remote_addr;
|
||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||
proxy_set_header X-Forwarded-Proto $scheme;
|
||||
proxy_set_header Connection "";
|
||||
|
||||
# LAB 4: стриминг тела вложения на upstream без буферизации.
|
||||
# Без этого nginx читает всё тело в RAM перед отправкой в api —
|
||||
# нарушает требование "потоковая обработка не позволяет одной
|
||||
# загрузке занять неограниченную RAM" (уровень 4).
|
||||
proxy_request_buffering off;
|
||||
|
||||
# Увеличенные таймауты для загрузки/скачивания вложений.
|
||||
proxy_connect_timeout 2s;
|
||||
proxy_send_timeout 60s;
|
||||
proxy_read_timeout 60s;
|
||||
|
||||
# Failover: при ошибке на одной реплике пробуем следующую
|
||||
proxy_next_upstream error timeout http_502 http_503 http_504;
|
||||
proxy_next_upstream_tries 2;
|
||||
proxy_next_upstream_timeout 5s;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; $Base='http://localhost:8080'; $Pass='Example-only-not-a-real-secret-123'; $Fixture='tests\fixtures\sample.png'; $u='load'+[guid]::NewGuid().ToString('N').Substring(0,8); $userBody=@{username=$u;display_name='Load';password=$Pass}|ConvertTo-Json -Compress; Invoke-RestMethod -Method Post -Uri ($Base+'/api/v1/users') -ContentType 'application/json' -Body $userBody|Out-Null; $basic=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($u+':'+$Pass)); $sess=Invoke-RestMethod -Method Post -Uri ($Base+'/api/v1/auth/sessions') -Headers @{Authorization='Basic '+$basic}; $tok=$sess.token; $hdr=@{Authorization='Bearer '+$tok}; $chatBody=@{title='Concurrent';member_ids=@($sess.user.id)}|ConvertTo-Json -Compress; $chat=Invoke-RestMethod -Method Post -Uri ($Base+'/api/v1/chats') -Headers $hdr -ContentType 'application/json' -Body $chatBody; $jobs=1..10 | ForEach-Object { $i=$_; Start-Job -ScriptBlock { param($B,$C,$T,$F,$I) $h=@{Authorization='Bearer '+$T}; try { $r=Invoke-WebRequest -Method Post -Uri ($B+'/api/v1/chats/'+$C+'/attachments?filename=c'+$I+'.png') -Headers $h -ContentType 'image/png' -Body ([IO.File]::ReadAllBytes($F)) -UseBasicParsing; return [pscustomobject]@{I=$I;Status=[int]$r.StatusCode;Ok=($r.StatusCode -eq 201)} } catch { return [pscustomobject]@{I=$I;Status=$_.Exception.Response.StatusCode.value__;Ok=$false} } } -ArgumentList $Base,$chat.id,$tok,$Fixture,$i }; $jobs | Wait-Job | Out-Null; $res=$jobs | Receive-Job; $jobs | Remove-Job; $res | Format-Table -AutoSize; $ok=($res | Where-Object Ok).Count; if ($ok -ne 10) { throw ('Only '+$ok+' uploads succeeded') }; Write-Host 'PASS: 10 concurrent uploads succeeded' -ForegroundColor Green"
|
||||
|
||||
set RC=%ERRORLEVEL%
|
||||
if not "%RC%"=="0" exit /b %RC%
|
||||
|
||||
echo.
|
||||
echo Memory stats after concurrent uploads:
|
||||
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}"
|
||||
|
||||
exit /b 0
|
||||
@@ -0,0 +1,162 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set BASE=http://localhost:8080
|
||||
set PASS=Example-only-not-a-real-secret-123
|
||||
set FIXTURE=tests\fixtures\sample.png
|
||||
set TMP=%TEMP%\lab4-failover-attachment
|
||||
if not exist "%TMP%" mkdir "%TMP%"
|
||||
|
||||
call :guid
|
||||
set RND=%GUIDVAL%
|
||||
set FUSER=fo%RND%
|
||||
|
||||
echo === Register failover user ===
|
||||
call :register "%FUSER%" "Failover" fo
|
||||
if errorlevel 1 exit /b 1
|
||||
set FUSER_ID=!fo_ID!
|
||||
|
||||
call :login "%FUSER%" fo
|
||||
if errorlevel 1 exit /b 1
|
||||
set TOKEN=!fo_TOKEN!
|
||||
|
||||
echo === Create chat ===
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"title\":\"Failover attachment\",\"member_ids\":[\"%FUSER_ID%\"]}" ^
|
||||
-o "%TMP%\chat.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\chat.code"
|
||||
|
||||
call :check_code chat.code 201 "create chat"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
|
||||
|
||||
echo === Upload attachment ===
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%FIXTURE%').Hash.ToLower()"') do set ORIG_SHA=%%a
|
||||
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=failover.png" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-H "Content-Type: image/png" ^
|
||||
--data-binary "@%FIXTURE%" ^
|
||||
-D "%TMP%\upload.headers" ^
|
||||
-o "%TMP%\att.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\att.code"
|
||||
|
||||
call :check_code att.code 201 "upload attachment"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
|
||||
|
||||
for /f "tokens=1,* delims=:" %%a in ('findstr /i "X-Instance-Id" "%TMP%\upload.headers"') do set UPLOAD_INSTANCE=%%b
|
||||
echo Uploaded by:%UPLOAD_INSTANCE%
|
||||
echo attachment=%ATT_ID%
|
||||
|
||||
echo === Stop one API replica ===
|
||||
set STOP_ID=
|
||||
for /f "delims=" %%i in ('docker compose ps -q api') do if not defined STOP_ID set STOP_ID=%%i
|
||||
|
||||
if "%STOP_ID%"=="" (
|
||||
echo FAIL: no api containers found
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo Stopping container %STOP_ID%
|
||||
docker stop --time 10 %STOP_ID% >nul
|
||||
timeout /t 5 /nobreak >nul
|
||||
|
||||
echo === Download attachment after replica stop ===
|
||||
set TRY=0
|
||||
|
||||
:download_retry
|
||||
set /a TRY+=1
|
||||
if %TRY% GTR 10 (
|
||||
echo FAIL: download did not recover
|
||||
docker compose --compatibility up -d api >nul
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%/content" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-D "%TMP%\download.headers" ^
|
||||
-o "%TMP%\download.png" ^
|
||||
-w "%%{http_code}" > "%TMP%\download.code"
|
||||
|
||||
set /p RC=<"%TMP%\download.code"
|
||||
if "%RC%"=="200" goto download_ok
|
||||
|
||||
echo Download attempt %TRY% got HTTP %RC%, retrying...
|
||||
timeout /t 1 /nobreak >nul
|
||||
goto download_retry
|
||||
|
||||
:download_ok
|
||||
for /f "tokens=1,* delims=:" %%a in ('findstr /i "X-Instance-Id" "%TMP%\download.headers"') do set DOWNLOAD_INSTANCE=%%b
|
||||
echo Downloaded by:%DOWNLOAD_INSTANCE%
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%TMP%\download.png').Hash.ToLower()"') do set DL_SHA=%%a
|
||||
|
||||
if not "%DL_SHA%"=="%ORIG_SHA%" (
|
||||
echo FAIL: downloaded sha256 mismatch after failover
|
||||
docker compose --compatibility up -d api >nul
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo PASS: attachment bytes available after stopping one replica
|
||||
|
||||
echo === Restore API replicas ===
|
||||
docker compose --compatibility up -d api
|
||||
timeout /t 15 /nobreak >nul
|
||||
docker compose ps
|
||||
|
||||
exit /b 0
|
||||
|
||||
:guid
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
|
||||
exit /b 0
|
||||
|
||||
:register
|
||||
rem %1 username, %2 display_name, %3 prefix
|
||||
curl.exe -s -X POST "%BASE%/api/v1/users" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
|
||||
-o "%TMP%\%~3.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\%~3.code"
|
||||
|
||||
set /p RC=<"%TMP%\%~3.code"
|
||||
if not "%RC%"=="201" (
|
||||
echo FAIL: register %~1, HTTP %RC%
|
||||
type "%TMP%\%~3.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:login
|
||||
rem %1 username, %2 prefix
|
||||
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
|
||||
-u "%~1:%PASS%" ^
|
||||
-o "%TMP%\%~2.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\%~2.code"
|
||||
|
||||
set /p RC=<"%TMP%\%~2.code"
|
||||
if not "%RC%"=="201" (
|
||||
echo FAIL: login %~1, HTTP %RC%
|
||||
type "%TMP%\%~2.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).user.id"') do set "%~2_UID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:check_code
|
||||
rem %1 code file, %2 expected status, %3 label
|
||||
set /p RC=<"%TMP%\%~1"
|
||||
if not "%RC%"=="%~2" (
|
||||
echo FAIL: %~3 expected %~2, got %RC%
|
||||
exit /b 1
|
||||
)
|
||||
exit /b 0
|
||||
@@ -0,0 +1,184 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set BASE=http://localhost:8080
|
||||
set PASS=Example-only-not-a-real-secret-123
|
||||
set FIXTURE=tests\fixtures\sample.png
|
||||
set TMP=%TEMP%\lab4-manual
|
||||
if not exist "%TMP%" mkdir "%TMP%"
|
||||
|
||||
call :guid
|
||||
set RND=%GUIDVAL%
|
||||
|
||||
set ALICE=alice%RND%
|
||||
set BOB=bob%RND%
|
||||
set EVE=eve%RND%
|
||||
|
||||
echo === Register users ===
|
||||
call :register "%ALICE%" "Alice" alice
|
||||
if errorlevel 1 exit /b 1
|
||||
set ALICE_ID=!alice_ID!
|
||||
|
||||
call :register "%BOB%" "Bob" bob
|
||||
if errorlevel 1 exit /b 1
|
||||
set BOB_ID=!bob_ID!
|
||||
|
||||
call :register "%EVE%" "Eve" eve
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo === Login users ===
|
||||
call :login "%ALICE%" alice
|
||||
if errorlevel 1 exit /b 1
|
||||
set ALICE_TOKEN=!alice_TOKEN!
|
||||
|
||||
call :login "%BOB%" bob
|
||||
if errorlevel 1 exit /b 1
|
||||
set BOB_TOKEN=!bob_TOKEN!
|
||||
|
||||
call :login "%EVE%" eve
|
||||
if errorlevel 1 exit /b 1
|
||||
set EVE_TOKEN=!eve_TOKEN!
|
||||
|
||||
echo === Create chat alice+bob ===
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"title\":\"Lab4 manual bat\",\"member_ids\":[\"%ALICE_ID%\",\"%BOB_ID%\"]}" ^
|
||||
-o "%TMP%\chat.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\chat.code"
|
||||
|
||||
call :check_code chat.code 201 "create chat"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
|
||||
echo chat=%CHAT_ID%
|
||||
|
||||
echo === Upload attachment as alice ===
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%FIXTURE%').Hash.ToLower()"') do set ORIG_SHA=%%a
|
||||
echo original sha256=%ORIG_SHA%
|
||||
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=sample.png" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: image/png" ^
|
||||
--data-binary "@%FIXTURE%" ^
|
||||
-o "%TMP%\att.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\att.code"
|
||||
|
||||
call :check_code att.code 201 "upload attachment"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).sha256"') do set ATT_SHA=%%a
|
||||
|
||||
echo attachment=%ATT_ID%
|
||||
echo attachment sha256=%ATT_SHA%
|
||||
|
||||
if not "%ATT_SHA%"=="%ORIG_SHA%" (
|
||||
echo FAIL: attachment sha256 mismatch
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo === Create message with empty text and attachment_ids ===
|
||||
call :guid
|
||||
set KEY=%GUIDVAL%
|
||||
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/messages" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Idempotency-Key: %KEY%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"text\":\"\",\"attachment_ids\":[\"%ATT_ID%\"]}" ^
|
||||
-o "%TMP%\msg.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\msg.code"
|
||||
|
||||
call :check_code msg.code 201 "create message"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\msg.json' -Raw | ConvertFrom-Json).id"') do set MSG_ID=%%a
|
||||
echo message=%MSG_ID%
|
||||
|
||||
echo === Bob downloads attachment content ===
|
||||
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%/content" ^
|
||||
-H "Authorization: Bearer %BOB_TOKEN%" ^
|
||||
-o "%TMP%\download.png" ^
|
||||
-w "%%{http_code}" > "%TMP%\dl.code"
|
||||
|
||||
call :check_code dl.code 200 "download content"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%TMP%\download.png').Hash.ToLower()"') do set DL_SHA=%%a
|
||||
echo downloaded sha256=%DL_SHA%
|
||||
|
||||
if not "%DL_SHA%"=="%ORIG_SHA%" (
|
||||
echo FAIL: downloaded sha256 mismatch
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo === Eve must get 404 on metadata ===
|
||||
curl.exe -s -o nul ^
|
||||
-H "Authorization: Bearer %EVE_TOKEN%" ^
|
||||
-w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%" > "%TMP%\eve_meta.code"
|
||||
|
||||
call :check_code eve_meta.code 404 "eve metadata"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo === Eve must get 404 on content ===
|
||||
curl.exe -s -o nul ^
|
||||
-H "Authorization: Bearer %EVE_TOKEN%" ^
|
||||
-w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\eve_content.code"
|
||||
|
||||
call :check_code eve_content.code 404 "eve content"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo.
|
||||
echo ALL MANUAL ATTACHMENT CHECKS PASSED
|
||||
exit /b 0
|
||||
|
||||
:guid
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
|
||||
exit /b 0
|
||||
|
||||
:register
|
||||
rem %1 username, %2 display_name, %3 prefix
|
||||
curl.exe -s -X POST "%BASE%/api/v1/users" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
|
||||
-o "%TMP%\%~3.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\%~3.code"
|
||||
|
||||
set /p RC=<"%TMP%\%~3.code"
|
||||
if not "%RC%"=="201" (
|
||||
echo FAIL: register %~1, HTTP %RC%
|
||||
type "%TMP%\%~3.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:login
|
||||
rem %1 username, %2 prefix
|
||||
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
|
||||
-u "%~1:%PASS%" ^
|
||||
-o "%TMP%\%~2.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\%~2.code"
|
||||
|
||||
set /p RC=<"%TMP%\%~2.code"
|
||||
if not "%RC%"=="201" (
|
||||
echo FAIL: login %~1, HTTP %RC%
|
||||
type "%TMP%\%~2.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).user.id"') do set "%~2_UID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:check_code
|
||||
rem %1 code file, %2 expected status, %3 label
|
||||
set /p RC=<"%TMP%\%~1"
|
||||
if not "%RC%"=="%~2" (
|
||||
echo FAIL: %~3 expected %~2, got %RC%
|
||||
exit /b 1
|
||||
)
|
||||
exit /b 0
|
||||
@@ -0,0 +1,10 @@
|
||||
@echo off
|
||||
rem Пик памяти API ВО ВРЕМЯ 10 одновременных загрузок (уровень 4).
|
||||
rem Монитор и загрузки — фоновые PowerShell-джобы; пик пишется в файл,
|
||||
rem главный поток читает его после остановки монитора.
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
|
||||
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; $Base='http://localhost:8080'; $Pass='Example-only-not-a-real-secret-123'; $Fixture='tests\fixtures\sample.png'; $Tmp=Join-Path $env:TEMP 'lab4-mempeak'; New-Item -ItemType Directory -Force $Tmp | Out-Null; $u='mp'+[guid]::NewGuid().ToString('N').Substring(0,8); $ub=@{username=$u;display_name='MemPeak';password=$Pass}|ConvertTo-Json -Compress; Invoke-RestMethod -Method Post -Uri ($Base+'/api/v1/users') -ContentType 'application/json' -Body $ub | Out-Null; $basic=[Convert]::ToBase64String([Text.Encoding]::UTF8.GetBytes($u+':'+$Pass)); $sess=Invoke-RestMethod -Method Post -Uri ($Base+'/api/v1/auth/sessions') -Headers @{Authorization='Basic '+$basic}; $tok=$sess.token; $hdr=@{Authorization='Bearer '+$tok}; $cb=@{title='MemPeak';member_ids=@($sess.user.id)}|ConvertTo-Json -Compress; $chat=Invoke-RestMethod -Method Post -Uri ($Base+'/api/v1/chats') -Headers $hdr -ContentType 'application/json' -Body $cb; Write-Host ('chat='+$chat.id); $peakFile=Join-Path $Tmp 'peak.txt'; $stopFile=Join-Path $Tmp 'stop.flag'; Remove-Item $peakFile,$stopFile -ErrorAction SilentlyContinue; $mon=Start-Job -ScriptBlock { param($pf,$sf); $max=0.0; while(-not (Test-Path $sf)){ $s = docker stats --no-stream --format '{{.Name}} {{.MemUsage}}' mtusi2026lab4_api_1 mtusi2026lab4_api_2 2>$null; foreach($l in $s){ if($l -match '(\d+(?:\.\d+)?)MiB /'){ $v=[double]$Matches[1]; if($v -gt $max){$max=$v} } }; Start-Sleep -Milliseconds 200 }; Set-Content -Path $pf -Value $max } -ArgumentList $peakFile,$stopFile; $bytes=[IO.File]::ReadAllBytes($Fixture); $jobs=1..10 | ForEach-Object { $i=$_; Start-Job -ScriptBlock { param($B,$C,$T,$By,$I) $h=@{Authorization='Bearer '+$T}; try { $r=Invoke-WebRequest -Method Post -Uri ($B+'/api/v1/chats/'+$C+'/attachments?filename=mp'+$I+'.png') -Headers $h -ContentType 'image/png' -Body $By -UseBasicParsing; return [pscustomobject]@{I=$I;Status=[int]$r.StatusCode;Ok=($r.StatusCode -eq 201)} } catch { return [pscustomobject]@{I=$I;Status=$_.Exception.Response.StatusCode.value__;Ok=$false} } } -ArgumentList $Base,$chat.id,$tok,$bytes,$i }; $jobs | Wait-Job | Out-Null; $res=$jobs | Receive-Job; $jobs | Remove-Job; $res | Format-Table -AutoSize; $ok=($res | Where-Object Ok).Count; Write-Host ('Successful uploads: '+$ok+' / 10'); if($ok -ne 10){ throw 'not all uploads succeeded' }; New-Item -ItemType File -Path $stopFile | Out-Null; $mon | Wait-Job | Out-Null; $peak=Get-Content $peakFile; $mon | Remove-Job; Write-Host ''; Write-Host ('Peak API memory during 10 concurrent uploads: '+$peak+' MiB (limit 256 MiB per replica)') -ForegroundColor Green; docker stats --no-stream --format 'table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.MemPerc}}'"
|
||||
|
||||
exit /b %ERRORLEVEL%
|
||||
@@ -0,0 +1,223 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set BASE=http://localhost:8080
|
||||
set PASS=Example-only-not-a-real-secret-123
|
||||
set FIXTURE=tests\fixtures\sample.png
|
||||
set TMP=%TEMP%\lab4-negative
|
||||
if not exist "%TMP%" mkdir "%TMP%"
|
||||
|
||||
call :guid
|
||||
set RND=%GUIDVAL%
|
||||
|
||||
set ALICE=alice%RND%
|
||||
set BOB=bob%RND%
|
||||
set EVE=eve%RND%
|
||||
|
||||
echo === Setup users ===
|
||||
call :register "%ALICE%" "Alice" alice
|
||||
if errorlevel 1 exit /b 1
|
||||
set ALICE_ID=!alice_ID!
|
||||
|
||||
call :register "%BOB%" "Bob" bob
|
||||
if errorlevel 1 exit /b 1
|
||||
set BOB_ID=!bob_ID!
|
||||
|
||||
call :register "%EVE%" "Eve" eve
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
call :login "%ALICE%" alice
|
||||
if errorlevel 1 exit /b 1
|
||||
set ALICE_TOKEN=!alice_TOKEN!
|
||||
|
||||
call :login "%BOB%" bob
|
||||
if errorlevel 1 exit /b 1
|
||||
set BOB_TOKEN=!bob_TOKEN!
|
||||
|
||||
call :login "%EVE%" eve
|
||||
if errorlevel 1 exit /b 1
|
||||
set EVE_TOKEN=!eve_TOKEN!
|
||||
|
||||
echo === Create chat1 alice+bob ===
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"title\":\"Negative chat1\",\"member_ids\":[\"%ALICE_ID%\",\"%BOB_ID%\"]}" ^
|
||||
-o "%TMP%\chat1.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\chat1.code"
|
||||
|
||||
call :check_code chat1.code 201 "create chat1"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat1.json' -Raw | ConvertFrom-Json).id"') do set CHAT1_ID=%%a
|
||||
|
||||
echo === Create chat2 alice+eve ===
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"title\":\"Negative chat2\",\"member_ids\":[\"%ALICE_ID%\",\"%EVE_ID%\"]}" ^
|
||||
-o "%TMP%\chat2.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\chat2.code"
|
||||
|
||||
call :check_code chat2.code 201 "create chat2"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat2.json' -Raw | ConvertFrom-Json).id"') do set CHAT2_ID=%%a
|
||||
|
||||
echo === 1. Too large file must return 413 ===
|
||||
powershell -NoProfile -Command "$path='%TMP%\large.png'; $sig=[byte[]](0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A); $pad=New-Object byte[] (11534336-8); [IO.File]::WriteAllBytes($path, $sig + $pad)"
|
||||
|
||||
curl.exe -s -o nul ^
|
||||
-X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=large.png" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: image/png" ^
|
||||
--data-binary "@%TMP%\large.png" ^
|
||||
-w "%%{http_code}" > "%TMP%\large.code"
|
||||
|
||||
call :check_code large.code 413 "too large upload"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo === 2. Broken image must return 415 ===
|
||||
powershell -NoProfile -Command "[IO.File]::WriteAllBytes('%TMP%\broken.png',[byte[]](0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A,0xFF,0xFF,0xFF,0xFF))"
|
||||
|
||||
curl.exe -s -o nul ^
|
||||
-X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=broken.png" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: image/png" ^
|
||||
--data-binary "@%TMP%\broken.png" ^
|
||||
-w "%%{http_code}" > "%TMP%\broken.code"
|
||||
|
||||
call :check_code broken.code 415 "broken png"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo === 3. MIME mismatch must return 415 ===
|
||||
curl.exe -s -o nul ^
|
||||
-X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=fake.png" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: image/jpeg" ^
|
||||
--data-binary "@%FIXTURE%" ^
|
||||
-w "%%{http_code}" > "%TMP%\mime.code"
|
||||
|
||||
call :check_code mime.code 415 "jpeg disguised as png"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo === 4. Dangerous filename must return 400 ===
|
||||
curl.exe -s -o nul ^
|
||||
-X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=..%%2Fevil.png" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: image/png" ^
|
||||
--data-binary "@%FIXTURE%" ^
|
||||
-w "%%{http_code}" > "%TMP%\filename.code"
|
||||
|
||||
call :check_code filename.code 400 "path traversal filename"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo === 5. Valid upload into chat1 ===
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT1_ID%/attachments?filename=sample.png" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Content-Type: image/png" ^
|
||||
--data-binary "@%FIXTURE%" ^
|
||||
-o "%TMP%\att.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\att.code"
|
||||
|
||||
call :check_code att.code 201 "valid upload"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
|
||||
echo attachment=%ATT_ID% in chat1=%CHAT1_ID%
|
||||
|
||||
echo === 6. Cross-chat attachment reference must return 404 ===
|
||||
call :guid
|
||||
set KEY1=%GUIDVAL%
|
||||
|
||||
curl.exe -s -o nul ^
|
||||
-X POST "%BASE%/api/v1/chats/%CHAT2_ID%/messages" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Idempotency-Key: %KEY1%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"text\":\"\",\"attachment_ids\":[\"%ATT_ID%\"]}" ^
|
||||
-w "%%{http_code}" > "%TMP%\crosschat.code"
|
||||
|
||||
call :check_code crosschat.code 404 "cross-chat attachment reference"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo === 7. Unknown attachment id must return 404 ===
|
||||
call :guid
|
||||
set UNKNOWN=%%a
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString()"') do set UNKNOWN=%%a
|
||||
|
||||
call :guid
|
||||
set KEY2=%GUIDVAL%
|
||||
|
||||
curl.exe -s -o nul ^
|
||||
-X POST "%BASE%/api/v1/chats/%CHAT1_ID%/messages" ^
|
||||
-H "Authorization: Bearer %ALICE_TOKEN%" ^
|
||||
-H "Idempotency-Key: %KEY2%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"text\":\"\",\"attachment_ids\":[\"%UNKNOWN%\"]}" ^
|
||||
-w "%%{http_code}" > "%TMP%\unknown.code"
|
||||
|
||||
call :check_code unknown.code 404 "unknown attachment id"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo === 8. Eve cannot download chat1 attachment ===
|
||||
curl.exe -s -o nul ^
|
||||
-H "Authorization: Bearer %EVE_TOKEN%" ^
|
||||
-w "%%{http_code}" "%BASE%/api/v1/attachments/%ATT_ID%/content" > "%TMP%\eve_dl.code"
|
||||
|
||||
call :check_code eve_dl.code 404 "eve download"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo.
|
||||
echo ALL NEGATIVE CHECKS PASSED
|
||||
exit /b 0
|
||||
|
||||
:guid
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
|
||||
exit /b 0
|
||||
|
||||
:register
|
||||
rem %1 username, %2 display_name, %3 prefix
|
||||
curl.exe -s -X POST "%BASE%/api/v1/users" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
|
||||
-o "%TMP%\%~3.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\%~3.code"
|
||||
|
||||
set /p RC=<"%TMP%\%~3.code"
|
||||
if not "%RC%"=="201" (
|
||||
echo FAIL: register %~1, HTTP %RC%
|
||||
type "%TMP%\%~3.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:login
|
||||
rem %1 username, %2 prefix
|
||||
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
|
||||
-u "%~1:%PASS%" ^
|
||||
-o "%TMP%\%~2.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\%~2.code"
|
||||
|
||||
set /p RC=<"%TMP%\%~2.code"
|
||||
if not "%RC%"=="201" (
|
||||
echo FAIL: login %~1, HTTP %RC%
|
||||
type "%TMP%\%~2.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).user.id"') do set "%~2_UID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:check_code
|
||||
rem %1 code file, %2 expected status, %3 label
|
||||
set /p RC=<"%TMP%\%~1"
|
||||
if not "%RC%"=="%~2" (
|
||||
echo FAIL: %~3 expected %~2, got %RC%
|
||||
exit /b 1
|
||||
)
|
||||
exit /b 0
|
||||
@@ -0,0 +1,133 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set BASE=http://localhost:8080
|
||||
set PASS=Example-only-not-a-real-secret-123
|
||||
set FIXTURE=tests\fixtures\sample.png
|
||||
set TMP=%TEMP%\lab4-persistence
|
||||
if not exist "%TMP%" mkdir "%TMP%"
|
||||
|
||||
call :guid
|
||||
set RND=%GUIDVAL%
|
||||
set PUSER=pers%RND%
|
||||
|
||||
echo === Register persistence user ===
|
||||
call :register "%PUSER%" "Persistence" pers
|
||||
if errorlevel 1 exit /b 1
|
||||
set PUSER_ID=!pers_ID!
|
||||
|
||||
call :login "%PUSER%" pers
|
||||
if errorlevel 1 exit /b 1
|
||||
set TOKEN=!pers_TOKEN!
|
||||
|
||||
echo === Create chat ===
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"title\":\"Persistence attachment\",\"member_ids\":[\"%PUSER_ID%\"]}" ^
|
||||
-o "%TMP%\chat.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\chat.code"
|
||||
|
||||
call :check_code chat.code 201 "create chat"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
|
||||
|
||||
echo === Upload attachment ===
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%FIXTURE%').Hash.ToLower()"') do set ORIG_SHA=%%a
|
||||
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=persistence.png" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-H "Content-Type: image/png" ^
|
||||
--data-binary "@%FIXTURE%" ^
|
||||
-o "%TMP%\att.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\att.code"
|
||||
|
||||
call :check_code att.code 201 "upload attachment"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
|
||||
|
||||
echo === Create message with attachment ===
|
||||
call :guid
|
||||
set KEY=%GUIDVAL%
|
||||
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/messages" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-H "Idempotency-Key: %KEY%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"text\":\"\",\"attachment_ids\":[\"%ATT_ID%\"]}" ^
|
||||
-o "%TMP%\msg.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\msg.code"
|
||||
|
||||
call :check_code msg.code 201 "create message"
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\msg.json' -Raw | ConvertFrom-Json).id"') do set MSG_ID=%%a
|
||||
|
||||
echo === Save state ===
|
||||
>"%TMP%\username.txt" echo %PUSER%
|
||||
>"%TMP%\token.txt" echo %TOKEN%
|
||||
>"%TMP%\chat_id.txt" echo %CHAT_ID%
|
||||
>"%TMP%\att_id.txt" echo %ATT_ID%
|
||||
>"%TMP%\msg_id.txt" echo %MSG_ID%
|
||||
>"%TMP%\sha256.txt" echo %ORIG_SHA%
|
||||
|
||||
echo State saved to %TMP%
|
||||
echo attachment=%ATT_ID%
|
||||
echo message=%MSG_ID%
|
||||
echo.
|
||||
echo Now run:
|
||||
echo scripts\restart-stack.bat
|
||||
echo scripts\persistence-verify.bat
|
||||
exit /b 0
|
||||
|
||||
:guid
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
|
||||
exit /b 0
|
||||
|
||||
:register
|
||||
rem %1 username, %2 display_name, %3 prefix
|
||||
curl.exe -s -X POST "%BASE%/api/v1/users" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
|
||||
-o "%TMP%\%~3.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\%~3.code"
|
||||
|
||||
set /p RC=<"%TMP%\%~3.code"
|
||||
if not "%RC%"=="201" (
|
||||
echo FAIL: register %~1, HTTP %RC%
|
||||
type "%TMP%\%~3.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:login
|
||||
rem %1 username, %2 prefix
|
||||
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
|
||||
-u "%~1:%PASS%" ^
|
||||
-o "%TMP%\%~2.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\%~2.code"
|
||||
|
||||
set /p RC=<"%TMP%\%~2.code"
|
||||
if not "%RC%"=="201" (
|
||||
echo FAIL: login %~1, HTTP %RC%
|
||||
type "%TMP%\%~2.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).user.id"') do set "%~2_UID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:check_code
|
||||
rem %1 code file, %2 expected status, %3 label
|
||||
set /p RC=<"%TMP%\%~1"
|
||||
if not "%RC%"=="%~2" (
|
||||
echo FAIL: %~3 expected %~2, got %RC%
|
||||
exit /b 1
|
||||
)
|
||||
exit /b 0
|
||||
@@ -0,0 +1,92 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set BASE=http://localhost:8080
|
||||
set TMP=%TEMP%\lab4-persistence
|
||||
|
||||
if not exist "%TMP%\token.txt" (
|
||||
echo FAIL: state not found. Run scripts\persistence-attachment.bat first.
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
set /p TOKEN=<"%TMP%\token.txt"
|
||||
set /p CHAT_ID=<"%TMP%\chat_id.txt"
|
||||
set /p ATT_ID=<"%TMP%\att_id.txt"
|
||||
set /p MSG_ID=<"%TMP%\msg_id.txt"
|
||||
set /p EXPECTED_SHA=<"%TMP%\sha256.txt"
|
||||
|
||||
echo === Persistence verify after restart ===
|
||||
echo chat=%CHAT_ID%
|
||||
echo attachment=%ATT_ID%
|
||||
echo message=%MSG_ID%
|
||||
echo expected sha256=%EXPECTED_SHA%
|
||||
|
||||
echo === Check metadata ===
|
||||
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-o "%TMP%\meta.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\meta.code"
|
||||
|
||||
set /p RC=<"%TMP%\meta.code"
|
||||
if not "%RC%"=="200" (
|
||||
echo FAIL: metadata HTTP %RC%
|
||||
type "%TMP%\meta.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\meta.json' -Raw | ConvertFrom-Json).sha256"') do set META_SHA=%%a
|
||||
|
||||
if not "%META_SHA%"=="%EXPECTED_SHA%" (
|
||||
echo FAIL: metadata sha256 mismatch
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo PASS: attachment metadata survived restart
|
||||
|
||||
echo === Check content ===
|
||||
curl.exe -s -X GET "%BASE%/api/v1/attachments/%ATT_ID%/content" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-o "%TMP%\download.png" ^
|
||||
-w "%%{http_code}" > "%TMP%\content.code"
|
||||
|
||||
set /p RC=<"%TMP%\content.code"
|
||||
if not "%RC%"=="200" (
|
||||
echo FAIL: content HTTP %RC%
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-FileHash -Algorithm SHA256 -Path '%TMP%\download.png').Hash.ToLower()"') do set DL_SHA=%%a
|
||||
|
||||
if not "%DL_SHA%"=="%EXPECTED_SHA%" (
|
||||
echo FAIL: downloaded sha256 mismatch
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo PASS: attachment bytes survived restart
|
||||
|
||||
echo === Check message history ===
|
||||
curl.exe -s -X GET "%BASE%/api/v1/chats/%CHAT_ID%/messages?limit=100" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-o "%TMP%\history.json" ^
|
||||
-w "%%{http_code}" > "%TMP%\history.code"
|
||||
|
||||
set /p RC=<"%TMP%\history.code"
|
||||
if not "%RC%"=="200" (
|
||||
echo FAIL: history HTTP %RC%
|
||||
type "%TMP%\history.json"
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
powershell -NoProfile -Command "$h=Get-Content '%TMP%\history.json' -Raw | ConvertFrom-Json; if (-not ($h.items | Where-Object { $_.id -eq '%MSG_ID%' -and $_.attachment_ids -contains '%ATT_ID%' })) { exit 1 }"
|
||||
|
||||
if errorlevel 1 (
|
||||
echo FAIL: message with attachment not found after restart
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo PASS: message with attachment survived restart
|
||||
echo PASS: old session token still works
|
||||
echo.
|
||||
echo ALL PERSISTENCE CHECKS PASSED
|
||||
exit /b 0
|
||||
@@ -0,0 +1,39 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set TMP=%TEMP%\lab4-private
|
||||
if not exist "%TMP%" mkdir "%TMP%"
|
||||
|
||||
echo === Private bucket check ===
|
||||
|
||||
for /f "delims=" %%a in ('docker compose exec -T postgres psql -U messenger -d messenger -tAc "SELECT s3_key FROM attachments ORDER BY created_at DESC LIMIT 1"') do set KEY=%%a
|
||||
|
||||
if "%KEY%"=="" (
|
||||
echo FAIL: no attachments found. Run scripts\manual-attachments.bat first.
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo Latest s3_key: %KEY%
|
||||
|
||||
docker compose exec -T api sh -lc "wget -S -O /dev/null http://rustfs:9000/messenger/%KEY% 2>&1" > "%TMP%\out.txt" 2>&1
|
||||
|
||||
echo.
|
||||
echo Direct unauthenticated response:
|
||||
type "%TMP%\out.txt"
|
||||
echo.
|
||||
|
||||
findstr /c:"HTTP/1.1 200" "%TMP%\out.txt" >nul
|
||||
if not errorlevel 1 (
|
||||
echo FAIL: bucket appears public, direct GET returned 200
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
findstr /c:"HTTP/" "%TMP%\out.txt" >nul
|
||||
if errorlevel 1 (
|
||||
echo WARN: could not parse HTTP status from wget output
|
||||
exit /b 2
|
||||
)
|
||||
|
||||
echo PASS: direct unauthenticated access is not 200
|
||||
exit /b 0
|
||||
@@ -0,0 +1,88 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set BASE=http://localhost:8080
|
||||
set PASS=Example-only-not-a-real-secret-123
|
||||
set FIXTURE=tests\fixtures\sample.png
|
||||
set TMP=%TEMP%\lab4-reconcile
|
||||
if not exist "%TMP%" mkdir "%TMP%"
|
||||
|
||||
call :guid
|
||||
set RND=%GUIDVAL%
|
||||
set RUSER=rec%RND%
|
||||
|
||||
echo === Register + login ===
|
||||
call :register "%RUSER%" "Reconcile" rec
|
||||
if errorlevel 1 exit /b 1
|
||||
set RID=!rec_ID!
|
||||
call :login "%RUSER%" rec
|
||||
if errorlevel 1 exit /b 1
|
||||
set TOKEN=!rec_TOKEN!
|
||||
|
||||
echo === Create chat ===
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"title\":\"Reconcile demo\",\"member_ids\":[\"%RID%\"]}" ^
|
||||
-o "%TMP%\chat.json" -w "%%{http_code}" > "%TMP%\chat.code"
|
||||
call :check_code chat.code 201 "create chat"
|
||||
if errorlevel 1 exit /b 1
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\chat.json' -Raw | ConvertFrom-Json).id"') do set CHAT_ID=%%a
|
||||
|
||||
echo === Upload attachment (creates S3 object + DB metadata) ===
|
||||
curl.exe -s -X POST "%BASE%/api/v1/chats/%CHAT_ID%/attachments?filename=orphan.png" ^
|
||||
-H "Authorization: Bearer %TOKEN%" ^
|
||||
-H "Content-Type: image/png" ^
|
||||
--data-binary "@%FIXTURE%" ^
|
||||
-o "%TMP%\att.json" -w "%%{http_code}" > "%TMP%\att.code"
|
||||
call :check_code att.code 201 "upload attachment"
|
||||
if errorlevel 1 exit /b 1
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\att.json' -Raw | ConvertFrom-Json).id"') do set ATT_ID=%%a
|
||||
echo attachment=%ATT_ID%
|
||||
|
||||
echo === Simulate crash: delete DB metadata, leave S3 object orphaned ===
|
||||
docker compose exec -T postgres psql -U messenger -d messenger -c "DELETE FROM attachments WHERE id='%ATT_ID%';"
|
||||
|
||||
echo === Run reconcile with grace=0 (must delete the orphan) ===
|
||||
docker compose --compatibility --profile tools run -e RECONCILE_GRACE_PERIOD_SECONDS=0 --rm reconcile
|
||||
if errorlevel 1 (
|
||||
echo FAIL: reconcile exited non-zero
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
echo === Run reconcile again (idempotent: 0 orphans deleted) ===
|
||||
docker compose --compatibility --profile tools run -e RECONCILE_GRACE_PERIOD_SECONDS=0 --rm reconcile
|
||||
if errorlevel 1 exit /b 1
|
||||
|
||||
echo.
|
||||
echo RECONCILE DEMO COMPLETE: inspect logs above for "reconcile deleted orphan" then "orphans_deleted 0".
|
||||
exit /b 0
|
||||
|
||||
:guid
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "[guid]::NewGuid().ToString('N').Substring(0,8)"') do set GUIDVAL=%%a
|
||||
exit /b 0
|
||||
|
||||
:register
|
||||
curl.exe -s -X POST "%BASE%/api/v1/users" ^
|
||||
-H "Content-Type: application/json" ^
|
||||
-d "{\"username\":\"%~1\",\"display_name\":\"%~2\",\"password\":\"%PASS%\"}" ^
|
||||
-o "%TMP%\%~3.json" -w "%%{http_code}" > "%TMP%\%~3.code"
|
||||
set /p RC=<"%TMP%\%~3.code"
|
||||
if not "%RC%"=="201" ( echo FAIL: register %~1, HTTP %RC% & type "%TMP%\%~3.json" & exit /b 1 )
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~3.json' -Raw | ConvertFrom-Json).id"') do set "%~3_ID=%%a"
|
||||
exit /b 0
|
||||
|
||||
:login
|
||||
curl.exe -s -X POST "%BASE%/api/v1/auth/sessions" ^
|
||||
-u "%~1:%PASS%" ^
|
||||
-o "%TMP%\%~2.json" -w "%%{http_code}" > "%TMP%\%~2.code"
|
||||
set /p RC=<"%TMP%\%~2.code"
|
||||
if not "%RC%"=="201" ( echo FAIL: login %~1, HTTP %RC% & type "%TMP%\%~2.json" & exit /b 1 )
|
||||
for /f "delims=" %%a in ('powershell -NoProfile -Command "(Get-Content '%TMP%\%~2.json' -Raw | ConvertFrom-Json).token"') do set "%~2_TOKEN=%%a"
|
||||
exit /b 0
|
||||
|
||||
:check_code
|
||||
set /p RC=<"%TMP%\%~1"
|
||||
if not "%RC%"=="%~2" ( echo FAIL: %~3 expected %~2, got %RC% & exit /b 1 )
|
||||
exit /b 0
|
||||
@@ -0,0 +1,5 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
docker compose --compatibility --profile tools run --rm reconcile
|
||||
exit /b %ERRORLEVEL%
|
||||
@@ -0,0 +1,29 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set TMP=%TEMP%\lab4-restart
|
||||
if not exist "%TMP%" mkdir "%TMP%"
|
||||
|
||||
echo Restarting stack without removing volumes...
|
||||
docker compose --compatibility restart
|
||||
|
||||
set COUNT=0
|
||||
|
||||
:wait
|
||||
set /a COUNT+=1
|
||||
if %COUNT% GTR 90 (
|
||||
echo FAIL: stack did not become ready in time
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
curl.exe -s -o nul -w "%%{http_code}" http://localhost:8080/health/ready > "%TMP%\code.txt"
|
||||
set /p C=<"%TMP%\code.txt"
|
||||
|
||||
if "%C%"=="200" (
|
||||
echo Stack is ready.
|
||||
exit /b 0
|
||||
)
|
||||
|
||||
timeout /t 1 /nobreak >nul
|
||||
goto wait
|
||||
@@ -0,0 +1,8 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set PYTHONUTF8=1
|
||||
|
||||
python -X utf8 tests\smoke.py --base-url http://localhost:8080
|
||||
exit /b %ERRORLEVEL%
|
||||
@@ -0,0 +1,28 @@
|
||||
@echo off
|
||||
setlocal EnableDelayedExpansion
|
||||
cd /d "%~dp0.."
|
||||
|
||||
if "%TARGET_INSTANCE_ID%"=="" (
|
||||
echo FAIL: TARGET_INSTANCE_ID is not set
|
||||
exit /b 1
|
||||
)
|
||||
|
||||
for /f "tokens=1 delims=-" %%a in ("%TARGET_INSTANCE_ID%") do set PREFIX=%%a
|
||||
|
||||
echo Target instance: %TARGET_INSTANCE_ID%
|
||||
echo Hostname prefix: %PREFIX%
|
||||
|
||||
for /f "delims=" %%i in ('docker ps --filter "label=com.docker.compose.service=api" --format "{{.ID}}"') do (
|
||||
for /f "delims=" %%h in ('docker inspect --format "{{.Config.Hostname}}" %%i') do (
|
||||
set "H=%%h"
|
||||
echo !H! | findstr /b /c:"%PREFIX%" >nul
|
||||
if not errorlevel 1 (
|
||||
echo Stopping container %%i with hostname !H!
|
||||
docker stop --time 10 %%i >nul
|
||||
exit /b 0
|
||||
)
|
||||
)
|
||||
)
|
||||
|
||||
echo FAIL: target replica not found
|
||||
exit /b 1
|
||||
@@ -0,0 +1,12 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set PYTHONUTF8=1
|
||||
|
||||
python -X utf8 tests\resilience.py ^
|
||||
--base-url http://localhost:8080 ^
|
||||
--mode failover ^
|
||||
--action-script scripts\stop-one.bat
|
||||
|
||||
exit /b %ERRORLEVEL%
|
||||
@@ -0,0 +1,12 @@
|
||||
@echo off
|
||||
setlocal
|
||||
cd /d "%~dp0.."
|
||||
|
||||
set PYTHONUTF8=1
|
||||
|
||||
python -X utf8 tests\resilience.py ^
|
||||
--base-url http://localhost:8080 ^
|
||||
--mode persistence ^
|
||||
--action-script scripts\restart-stack.bat
|
||||
|
||||
exit /b %ERRORLEVEL%
|
||||
@@ -0,0 +1,261 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"sync/atomic"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"mtusi-messenger/internal/api"
|
||||
"mtusi-messenger/internal/store"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func getenv(key, def string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
func getenvDurationSeconds(key string, def time.Duration) time.Duration {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil && n >= 0 {
|
||||
return time.Duration(n) * time.Second
|
||||
}
|
||||
}
|
||||
return def
|
||||
}
|
||||
|
||||
// ensureBucketWithRetry — идемпотентный bootstrap бакета с ожиданием готовности S3.
|
||||
func ensureBucketWithRetry(ctx context.Context, c *store.S3Client, log *slog.Logger) error {
|
||||
const attempts = 30
|
||||
const delay = 2 * time.Second
|
||||
|
||||
var lastErr error
|
||||
for i := 1; i <= attempts; i++ {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
lastErr = c.EnsureBucket(ctx)
|
||||
if lastErr == nil {
|
||||
return nil
|
||||
}
|
||||
log.Warn("s3 bootstrap retry",
|
||||
"attempt", i,
|
||||
"max_attempts", attempts,
|
||||
"error", lastErr.Error(),
|
||||
)
|
||||
timer := time.NewTimer(delay)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
timer.Stop()
|
||||
return ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
return lastErr
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Однократные режимы, запускаемые отдельными контейнерами.
|
||||
migrateOnly := flag.Bool("migrate-only", false, "Run migrations and exit")
|
||||
reconcile := flag.Bool("reconcile", false, "Reconcile orphaned S3 objects and exit")
|
||||
flag.Parse()
|
||||
|
||||
log := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
host := getenv("APP_HOST", "0.0.0.0")
|
||||
port := getenv("APP_PORT", "8080")
|
||||
addr := host + ":" + port
|
||||
|
||||
databaseURL := getenv("DATABASE_URL", "postgres://messenger:messenger_password@localhost:5432/messenger?sslmode=disable")
|
||||
redisURL := getenv("REDIS_URL", "redis://localhost:6379/0")
|
||||
sessionStore := getenv("SESSION_STORE", "postgres")
|
||||
sessionTTL := getenv("SESSION_TTL_SECONDS", "3600")
|
||||
cacheMode := getenv("CACHE_MODE", "versioned")
|
||||
|
||||
// LAB 4: S3 / RustFS. Отдельные credentials с ограниченными правами (уровень 4).
|
||||
s3Endpoint := getenv("S3_ENDPOINT", "localhost:9000")
|
||||
s3AccessKey := getenv("S3_ACCESS_KEY", "")
|
||||
s3SecretKey := getenv("S3_SECRET_KEY", "")
|
||||
s3Bucket := getenv("S3_BUCKET", "messenger")
|
||||
s3UseSSL := getenv("S3_USE_SSL", "false") == "true"
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// Подключение к PostgreSQL.
|
||||
poolConfig, err := pgxpool.ParseConfig(databaseURL)
|
||||
if err != nil {
|
||||
log.Error("parse database URL", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
poolConfig.MaxConns = 10
|
||||
poolConfig.MinConns = 2
|
||||
poolConfig.ConnConfig.ConnectTimeout = 5 * time.Second
|
||||
poolConfig.MaxConnLifetime = 30 * time.Minute
|
||||
poolConfig.MaxConnIdleTime = 5 * time.Minute
|
||||
poolConfig.HealthCheckPeriod = 10 * time.Second
|
||||
poolConfig.ConnConfig.RuntimeParams["statement_timeout"] = "15000"
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
||||
if err != nil {
|
||||
log.Error("connect to postgres", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
log.Error("ping postgres", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("postgres connected")
|
||||
|
||||
// --- ОДНОКРАТНЫЙ РЕЖИМ МИГРАЦИЙ ---
|
||||
if *migrateOnly {
|
||||
log.Info("migrate-only mode: applying migrations")
|
||||
if err := store.RunMigrations(ctx, pool, log); err != nil {
|
||||
log.Error("migrations failed", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("migrations applied successfully, exiting")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// Инициализация S3-клиента (без bootstrap бакета — он нужен только репликам).
|
||||
var s3client *store.S3Client
|
||||
if s3Endpoint != "" && s3AccessKey != "" && s3SecretKey != "" {
|
||||
s3client, err = store.NewS3Client(s3Endpoint, s3AccessKey, s3SecretKey, s3Bucket, s3UseSSL, log)
|
||||
if err != nil {
|
||||
log.Error("init s3 client", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// --- ОДНОКРАТНЫЙ РЕЖИМ RECONCILE (уровень 4) ---
|
||||
// Удаляет осиротевшие объекты S3 (без метаданных в БД) старше grace period.
|
||||
// Не требует Redis и не создаёт бакет.
|
||||
if *reconcile {
|
||||
if s3client == nil {
|
||||
log.Error("reconcile requires S3 credentials")
|
||||
os.Exit(1)
|
||||
}
|
||||
grace := getenvDurationSeconds("RECONCILE_GRACE_PERIOD_SECONDS", 3600*time.Second)
|
||||
log.Info("reconcile mode: sweeping orphaned objects", "grace_seconds", int(grace.Seconds()))
|
||||
recCtx, cancel := context.WithTimeout(ctx, 120*time.Second)
|
||||
defer cancel()
|
||||
if err := store.RunReconcile(recCtx, pool, s3client, grace, log); err != nil {
|
||||
log.Error("reconcile failed", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("reconcile completed successfully, exiting")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
// --- ОБЫЧНЫЙ СТАРТ РЕПЛИКИ ---
|
||||
log.Info("migrations already applied by migrate service, starting server")
|
||||
|
||||
// Подключение к Redis.
|
||||
var rdb *redis.Client
|
||||
if sessionStore == "redis" {
|
||||
opts, err := redis.ParseURL(redisURL)
|
||||
if err != nil {
|
||||
log.Error("parse redis URL", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
rdb = redis.NewClient(opts)
|
||||
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||
log.Warn("redis ping failed, falling back to postgres-only mode", "error", err.Error())
|
||||
rdb = nil
|
||||
} else {
|
||||
log.Info("redis connected")
|
||||
defer rdb.Close()
|
||||
}
|
||||
}
|
||||
|
||||
// Идемпотентный bootstrap приватного бакета (только для реплик).
|
||||
if s3client != nil {
|
||||
bootstrapCtx, cancel := context.WithTimeout(ctx, 90*time.Second)
|
||||
err = ensureBucketWithRetry(bootstrapCtx, s3client, log)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Error("ensure s3 bucket", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
log.Info("s3 ready", "endpoint", s3Endpoint, "bucket", s3Bucket)
|
||||
} else {
|
||||
log.Warn("S3 credentials not set; attachment upload/download will return 503")
|
||||
}
|
||||
|
||||
ready := &atomic.Bool{}
|
||||
ready.Store(true)
|
||||
|
||||
st := store.New(pool, rdb, s3client, sessionTTL, cacheMode, log)
|
||||
log.Info("store configured",
|
||||
"session_store", sessionStore,
|
||||
"cache_mode", cacheMode,
|
||||
"s3_configured", s3client != nil,
|
||||
)
|
||||
|
||||
// --- LAB 3: Идентификация реплики ---
|
||||
var instanceID string
|
||||
if v := getenv("INSTANCE_ID", ""); v != "" {
|
||||
instanceID = v
|
||||
} else {
|
||||
hostname, _ := os.Hostname()
|
||||
prefix := hostname
|
||||
if len(prefix) > 12 {
|
||||
prefix = prefix[:12]
|
||||
}
|
||||
if prefix == "" {
|
||||
prefix = "local"
|
||||
}
|
||||
instanceID = prefix + "-" + uuid.NewString()[:8]
|
||||
}
|
||||
log.Info("instance identity", "instance_id", instanceID)
|
||||
|
||||
srv := api.NewServer(st, log, ready, instanceID)
|
||||
|
||||
httpSrv := &http.Server{
|
||||
Addr: addr,
|
||||
Handler: srv.Handler(),
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 60 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
go func() { errCh <- httpSrv.ListenAndServe() }()
|
||||
log.Info("server started", "addr", addr, "pid", os.Getpid())
|
||||
|
||||
stop := make(chan os.Signal, 1)
|
||||
signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT)
|
||||
|
||||
select {
|
||||
case err := <-errCh:
|
||||
if err != nil && !errors.Is(err, http.ErrServerClosed) {
|
||||
log.Error("server failed", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
case sig := <-stop:
|
||||
log.Info("shutdown signal received", "signal", sig.String())
|
||||
ready.Store(false)
|
||||
sctx, cancel := context.WithTimeout(context.Background(), 8*time.Second)
|
||||
defer cancel()
|
||||
if err := httpSrv.Shutdown(sctx); err != nil {
|
||||
log.Warn("graceful shutdown exceeded budget, closing", "error", err.Error())
|
||||
_ = httpSrv.Close()
|
||||
}
|
||||
log.Info("server stopped cleanly")
|
||||
}
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
module mtusi-messenger
|
||||
|
||||
go 1.22
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.5.5
|
||||
github.com/minio/minio-go/v7 v7.0.76
|
||||
github.com/redis/go-redis/v9 v9.5.1
|
||||
golang.org/x/crypto v0.26.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.2.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/goccy/go-json v0.10.3 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.1 // indirect
|
||||
github.com/klauspost/compress v1.17.9 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
golang.org/x/net v0.28.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.24.0 // indirect
|
||||
golang.org/x/text v0.17.0 // indirect
|
||||
)
|
||||
+62
@@ -0,0 +1,62 @@
|
||||
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/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
|
||||
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
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/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
|
||||
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/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
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/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-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
|
||||
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
|
||||
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
|
||||
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
|
||||
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
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.0.76 h1:9nxHH2XDai61cT/EFhyIw/wW4vJfpPNvl7lSFpRt+Ng=
|
||||
github.com/minio/minio-go/v7 v7.0.76/go.mod h1:AVM3IUN6WwKzmwBxVdjzhH8xq+f57JSbbvzqvUzR6eg=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
|
||||
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
|
||||
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
|
||||
golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE=
|
||||
golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.24.0 h1:Twjiwq9dn6R1fQcyiK+wQyHWfaz/BJB+YIpzU/Cv3Xg=
|
||||
golang.org/x/sys v0.24.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.17.0 h1:XtiM5bkSOt+ewxlOE/aE/AKEHibwj/6gvWMl9Rsh0Qc=
|
||||
golang.org/x/text v0.17.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,530 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/apperr"
|
||||
"mtusi-messenger/internal/model"
|
||||
"mtusi-messenger/internal/store"
|
||||
)
|
||||
|
||||
// ───────────────────────────── health ─────────────────────────────
|
||||
func (s *Server) healthLive(w http.ResponseWriter, r *http.Request) {
|
||||
writeJSON(w, r, http.StatusOK, model.Health{Status: "ok"})
|
||||
}
|
||||
|
||||
func (s *Server) healthReady(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
if !s.ready.Load() {
|
||||
apperr.Write(w, id, apperr.Unavailable("service is shutting down"))
|
||||
return
|
||||
}
|
||||
if err := s.store.Ping(r.Context()); err != nil {
|
||||
apperr.Write(w, id, apperr.Unavailable("database unavailable"))
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusOK, model.Health{Status: "ok"})
|
||||
}
|
||||
|
||||
// ───────────────────────────── users ─────────────────────────────
|
||||
func (s *Server) usersRoute(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w, r)
|
||||
return
|
||||
}
|
||||
id := requestID(r)
|
||||
var req model.CreateUserReq
|
||||
if aerr := decodeJSON(w, r, &req); aerr != nil {
|
||||
apperr.Write(w, id, aerr)
|
||||
return
|
||||
}
|
||||
if !usernameRe.MatchString(req.Username) {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("username must match ^[a-z0-9_]{3,32}$"))
|
||||
return
|
||||
}
|
||||
if !validText(req.DisplayName, 100) {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("display_name must be 1..100 non-space code points"))
|
||||
return
|
||||
}
|
||||
if !validPassword(req.Password) {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("password must be 12..128 non-space code points"))
|
||||
return
|
||||
}
|
||||
u, err := s.store.CreateUser(r.Context(), req.Username, req.DisplayName, req.Password)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusCreated, u)
|
||||
}
|
||||
|
||||
func (s *Server) me(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
methodNotAllowed(w, r)
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusOK, currentUser(r))
|
||||
}
|
||||
|
||||
// ───────────────────────────── sessions ─────────────────────────────
|
||||
func (s *Server) createSession(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
methodNotAllowed(w, r)
|
||||
return
|
||||
}
|
||||
id := requestID(r)
|
||||
creds := currentCreds(r)
|
||||
user, err := s.store.UserByCredentials(r.Context(), creds.Username, creds.Password)
|
||||
if err != nil {
|
||||
w.Header().Set("WWW-Authenticate", wwwBasic)
|
||||
apperr.Write(w, id, apperr.Unauthorized("invalid credentials"))
|
||||
return
|
||||
}
|
||||
token, expiresAt, err := s.store.CreateSession(r.Context(), user.ID)
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.Internal("internal error"))
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusCreated, model.SessionResponse{
|
||||
Token: token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: model.Timestamp{Time: expiresAt},
|
||||
User: *user,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Server) revokeSession(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodDelete {
|
||||
methodNotAllowed(w, r)
|
||||
return
|
||||
}
|
||||
if err := s.store.RevokeSession(r.Context(), currentToken(r)); err != nil {
|
||||
apperr.Write(w, requestID(r), apperr.Unavailable("logout not confirmed, please retry"))
|
||||
return
|
||||
}
|
||||
writeNoContent(w, r)
|
||||
}
|
||||
|
||||
// ───────────────────────────── chats ─────────────────────────────
|
||||
func (s *Server) chatsRoute(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
s.createChat(w, r)
|
||||
case http.MethodGet:
|
||||
s.listChats(w, r)
|
||||
default:
|
||||
methodNotAllowed(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) createChat(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
var req model.CreateChatReq
|
||||
if aerr := decodeJSON(w, r, &req); aerr != nil {
|
||||
apperr.Write(w, id, aerr)
|
||||
return
|
||||
}
|
||||
if !validText(req.Title, 100) {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("title must be 1..100 non-space code points"))
|
||||
return
|
||||
}
|
||||
if len(req.MemberIDs) < 1 || len(req.MemberIDs) > 100 {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("member_ids must contain 1..100 items"))
|
||||
return
|
||||
}
|
||||
seen := make(map[string]bool, len(req.MemberIDs))
|
||||
for _, raw := range req.MemberIDs {
|
||||
uid, err := parseUUID(raw)
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("member_ids must be UUID strings"))
|
||||
return
|
||||
}
|
||||
if seen[uid] {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("duplicate member_id"))
|
||||
return
|
||||
}
|
||||
seen[uid] = true
|
||||
}
|
||||
caller := currentUser(r)
|
||||
if !seen[caller.ID] {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("creator must be included in member_ids"))
|
||||
return
|
||||
}
|
||||
chat, err := s.store.CreateChat(r.Context(), req.Title, req.MemberIDs, caller.ID)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusCreated, chat)
|
||||
}
|
||||
|
||||
func (s *Server) listChats(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
caller := currentUser(r)
|
||||
limit, cur, aerr := pagingParams(r, caller.ID)
|
||||
if aerr != nil {
|
||||
apperr.Write(w, id, aerr)
|
||||
return
|
||||
}
|
||||
items, more, err := s.store.ListChats(r.Context(), caller.ID, limit, cur)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
page := model.Page[*model.Chat]{Items: items}
|
||||
if more && len(items) > 0 {
|
||||
last := items[len(items)-1]
|
||||
encoded := store.EncodeCursor(store.Cursor{Scope: caller.ID, TS: last.CreatedAt.Time, ID: last.ID})
|
||||
page.NextCursor = &encoded
|
||||
}
|
||||
writeJSON(w, r, http.StatusOK, page)
|
||||
}
|
||||
|
||||
func (s *Server) chatRoute(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet {
|
||||
methodNotAllowed(w, r)
|
||||
return
|
||||
}
|
||||
id := requestID(r)
|
||||
chatID, err := parseUUID(r.PathValue("chat_id"))
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("chat_id must be a UUID"))
|
||||
return
|
||||
}
|
||||
chat, err := s.store.ChatForUser(r.Context(), chatID, currentUser(r).ID)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusOK, chat)
|
||||
}
|
||||
|
||||
// ───────────────────────────── messages ─────────────────────────────
|
||||
func (s *Server) messagesRoute(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.Method {
|
||||
case http.MethodPost:
|
||||
s.createMessage(w, r)
|
||||
case http.MethodGet:
|
||||
s.listMessages(w, r)
|
||||
default:
|
||||
methodNotAllowed(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) createMessage(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
chatID, err := parseUUID(r.PathValue("chat_id"))
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("chat_id must be a UUID"))
|
||||
return
|
||||
}
|
||||
chat, err := s.store.ChatForUser(r.Context(), chatID, currentUser(r).ID)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Idempotency-Key обязателен с lab3.
|
||||
idempotencyKey := r.Header.Get("Idempotency-Key")
|
||||
if idempotencyKey == "" {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("Idempotency-Key header is required"))
|
||||
return
|
||||
}
|
||||
|
||||
var req model.CreateMessageReq
|
||||
if aerr := decodeJSON(w, r, &req); aerr != nil {
|
||||
apperr.Write(w, id, aerr)
|
||||
return
|
||||
}
|
||||
if req.AttachmentIDs == nil {
|
||||
req.AttachmentIDs = []string{} // отсутствующий attachment_ids == [] (контракт)
|
||||
}
|
||||
|
||||
// ─── Правило anyOf контракта: либо непустой текст, либо ≥1 вложение. ───
|
||||
if len(req.AttachmentIDs) == 0 {
|
||||
if !validText(req.Text, 4000) {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("text must be 1..4000 non-space code points or provide attachment_ids"))
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if len(req.AttachmentIDs) > 10 {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("attachment_ids must contain at most 10 items"))
|
||||
return
|
||||
}
|
||||
seen := make(map[string]bool, len(req.AttachmentIDs))
|
||||
for _, raw := range req.AttachmentIDs {
|
||||
aid, err := parseUUID(raw)
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("attachment_ids must be UUID strings"))
|
||||
return
|
||||
}
|
||||
if seen[aid] {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("duplicate attachment_id"))
|
||||
return
|
||||
}
|
||||
seen[aid] = true
|
||||
}
|
||||
// Непустой текст рядом с вложениями тоже должен быть валидным.
|
||||
if req.Text != "" && !validText(req.Text, 4000) {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("text must be 1..4000 non-space code points"))
|
||||
return
|
||||
}
|
||||
// Все вложения должны существовать и принадлежать ЭТОМУ чату; чужое/неизвестное — 404.
|
||||
if err := s.store.ValidateAttachmentsForChat(r.Context(), chat.ID, req.AttachmentIDs); err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
msg, err := s.store.CreateMessage(r.Context(), chat.ID, currentUser(r).ID, req.Text, req.AttachmentIDs, idempotencyKey)
|
||||
if err != nil {
|
||||
if errors.Is(err, store.ErrIdempotencyConflict) {
|
||||
apperr.Write(w, id, apperr.Conflict("idempotency key mismatch"))
|
||||
return
|
||||
}
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusCreated, msg)
|
||||
}
|
||||
|
||||
func (s *Server) listMessages(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
chatID, err := parseUUID(r.PathValue("chat_id"))
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("chat_id must be a UUID"))
|
||||
return
|
||||
}
|
||||
chat, err := s.store.ChatForUser(r.Context(), chatID, currentUser(r).ID)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
limit, cur, aerr := pagingParams(r, chat.ID)
|
||||
if aerr != nil {
|
||||
apperr.Write(w, id, aerr)
|
||||
return
|
||||
}
|
||||
items, more, err := s.store.ListMessages(r.Context(), chat.ID, limit, cur)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
page := model.Page[*model.Message]{Items: items}
|
||||
if more && len(items) > 0 {
|
||||
last := items[len(items)-1]
|
||||
encoded := store.EncodeCursor(store.Cursor{Scope: chat.ID, TS: last.CreatedAt.Time, ID: last.ID})
|
||||
page.NextCursor = &encoded
|
||||
}
|
||||
writeJSON(w, r, http.StatusOK, page)
|
||||
}
|
||||
|
||||
// ───────────────────────────── attachments (lab4) ─────────────────────────────
|
||||
func (s *Server) attachmentsRoute(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodPost {
|
||||
s.uploadAttachment(w, r)
|
||||
return
|
||||
}
|
||||
methodNotAllowed(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) attachmentRoute(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
s.getAttachment(w, r)
|
||||
return
|
||||
}
|
||||
methodNotAllowed(w, r)
|
||||
}
|
||||
|
||||
func (s *Server) attachmentContentRoute(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
s.downloadAttachment(w, r)
|
||||
return
|
||||
}
|
||||
methodNotAllowed(w, r)
|
||||
}
|
||||
|
||||
// uploadAttachment — POST /chats/{chat_id}/attachments?filename=...
|
||||
// Сырые байты одного PNG/JPEG (НЕ multipart). Лимиты применяются ДО полного
|
||||
// чтения/декодирования; MIME проверяется по содержимому.
|
||||
// Память ограничена жёстким потолком 10 MiB (MaxBytesReader): одна загрузка
|
||||
// не может занять неограниченную RAM.
|
||||
// 201 возвращается ТОЛЬКО после сохранения оригинала в S3 И метаданных в БД.
|
||||
// Если БД-запись не удалась после успешного S3-PUT — объект компенсируется (удаляется).
|
||||
func (s *Server) uploadAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
|
||||
chatID, err := parseUUID(r.PathValue("chat_id"))
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("chat_id must be a UUID"))
|
||||
return
|
||||
}
|
||||
filename := r.URL.Query().Get("filename")
|
||||
if !validFilename(filename) {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("filename must be 1..255 code points without '/', '\\', NUL or control chars"))
|
||||
return
|
||||
}
|
||||
|
||||
// Доступ к чату: участник или 404.
|
||||
chat, err := s.store.ChatForUser(r.Context(), chatID, currentUser(r).ID)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
|
||||
// Content-Type запроса должен быть одним из двух поддерживаемых.
|
||||
media, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if media != "image/png" && media != "image/jpeg" {
|
||||
apperr.Write(w, id, apperr.UnsupportedMedia("Content-Type must be image/png or image/jpeg"))
|
||||
return
|
||||
}
|
||||
|
||||
// Лимит размера ДО полного чтения: не более 10 MiB.
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxUploadBytes)
|
||||
|
||||
// Читаем тело, попутно считая SHA-256. Объём ограничен лимитом выше.
|
||||
buf := &bytes.Buffer{}
|
||||
hasher := sha256.New()
|
||||
n, err := io.Copy(io.MultiWriter(buf, hasher), r.Body)
|
||||
if err != nil {
|
||||
var maxErr *http.MaxBytesError
|
||||
if errors.As(err, &maxErr) {
|
||||
apperr.Write(w, id, apperr.PayloadTooLarge("file exceeds 10 MiB limit"))
|
||||
return
|
||||
}
|
||||
apperr.Write(w, id, apperr.InvalidRequest("unable to read request body"))
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("empty file"))
|
||||
return
|
||||
}
|
||||
data := buf.Bytes()
|
||||
|
||||
// MIME по содержимому + размеры без полного декодирования пикселей.
|
||||
contentType, width, height, serr := sniffImage(data)
|
||||
if serr != nil {
|
||||
apperr.Write(w, id, apperr.UnsupportedMedia("file is not a valid PNG or JPEG image"))
|
||||
return
|
||||
}
|
||||
// Подмена MIME: объявленный тип обязан совпадать с фактическим.
|
||||
if contentType != media {
|
||||
apperr.Write(w, id, apperr.UnsupportedMedia("declared Content-Type does not match file content"))
|
||||
return
|
||||
}
|
||||
if int64(width)*int64(height) > maxDecodedPixels {
|
||||
apperr.Write(w, id, apperr.PayloadTooLarge("image exceeds 40 megapixels"))
|
||||
return
|
||||
}
|
||||
|
||||
objectKey := chat.ID + "/" + uuid.NewString()
|
||||
|
||||
// Кладём байты в приватный S3-бакет.
|
||||
if err := s.store.PutObject(r.Context(), objectKey, data, contentType); err != nil {
|
||||
apperr.Write(w, id, apperr.Unavailable("object storage unavailable"))
|
||||
return
|
||||
}
|
||||
|
||||
now := model.NowTS()
|
||||
att := &model.Attachment{
|
||||
ID: uuid.NewString(),
|
||||
ChatID: chat.ID,
|
||||
OwnerID: currentUser(r).ID,
|
||||
Filename: filename,
|
||||
ContentType: contentType,
|
||||
SizeBytes: n,
|
||||
SHA256: hex.EncodeToString(hasher.Sum(nil)),
|
||||
Status: "ready",
|
||||
Variants: []model.Variant{}, // в lab4 всегда пусто
|
||||
ErrorCode: nil, // в lab4 всегда null
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
S3Key: objectKey,
|
||||
}
|
||||
if err := s.store.CreateAttachment(r.Context(), att); err != nil {
|
||||
// Компенсация: объект в S3 уже есть, а метаданных нет — удаляем объект,
|
||||
// чтобы не оставить вечный неучтённый файл (механизм согласованности).
|
||||
_ = s.store.DeleteObject(r.Context(), objectKey)
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusCreated, att)
|
||||
}
|
||||
|
||||
// getAttachment — GET /attachments/{id}: метаданные, только участникам чата.
|
||||
// Посторонний получает 404 (не раскрываем существование чужого объекта).
|
||||
func (s *Server) getAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
attID, err := parseUUID(r.PathValue("attachment_id"))
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("attachment_id must be a UUID"))
|
||||
return
|
||||
}
|
||||
att, err := s.store.GetAttachmentForUser(r.Context(), attID, currentUser(r).ID)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
writeJSON(w, r, http.StatusOK, att)
|
||||
}
|
||||
|
||||
// downloadAttachment — GET /attachments/{id}/content: байты через авторизованный
|
||||
// API, с истинным Content-Type, без redirect. variant по умолчанию original;
|
||||
// отсутствующий вариант — 404 (в lab4 есть только original).
|
||||
func (s *Server) downloadAttachment(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
if variant := r.URL.Query().Get("variant"); variant != "" && variant != "original" {
|
||||
apperr.Write(w, id, apperr.NotFound("variant not found"))
|
||||
return
|
||||
}
|
||||
attID, err := parseUUID(r.PathValue("attachment_id"))
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.InvalidRequest("attachment_id must be a UUID"))
|
||||
return
|
||||
}
|
||||
att, err := s.store.GetAttachmentForUser(r.Context(), attID, currentUser(r).ID)
|
||||
if err != nil {
|
||||
s.writeStoreErr(w, r, err)
|
||||
return
|
||||
}
|
||||
data, err := s.store.GetObject(r.Context(), att.S3Key)
|
||||
if err != nil {
|
||||
apperr.Write(w, id, apperr.Unavailable("object storage unavailable"))
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", att.ContentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(att.SizeBytes, 10))
|
||||
w.Header().Set("X-Request-Id", id)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(data)
|
||||
}
|
||||
|
||||
func (s *Server) notFound(w http.ResponseWriter, r *http.Request) {
|
||||
apperr.Write(w, requestID(r), apperr.NotFound("route not found"))
|
||||
}
|
||||
|
||||
func (s *Server) writeStoreErr(w http.ResponseWriter, r *http.Request, err error) {
|
||||
id := requestID(r)
|
||||
switch {
|
||||
case errors.Is(err, store.ErrNotFound):
|
||||
// Общее сообщение: не раскрываем, чат это или чужое вложение.
|
||||
apperr.Write(w, id, apperr.NotFound("object not found or not accessible"))
|
||||
case errors.Is(err, store.ErrBadMembers):
|
||||
apperr.Write(w, id, apperr.InvalidRequest("unknown member_id"))
|
||||
case errors.Is(err, store.ErrUsernameTaken):
|
||||
apperr.Write(w, id, apperr.Conflict("username is already taken"))
|
||||
case store.IsUnavailable(err):
|
||||
apperr.Write(w, id, apperr.Unavailable("database unavailable"))
|
||||
default:
|
||||
apperr.Write(w, id, apperr.Internal("internal error"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,208 @@
|
||||
// package api, helpers.go — пограничный контроль входа (перенос из lab1 + разбор Authorization + валидация вложений lab4).
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg" // регистрация декодеров для image.DecodeConfig
|
||||
_ "image/png"
|
||||
"mime"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi-messenger/internal/apperr"
|
||||
"mtusi-messenger/internal/store"
|
||||
)
|
||||
|
||||
const maxBodyBytes = 1 << 20
|
||||
|
||||
// ─────────────── Лимиты вложений (lab4) ───────────────
|
||||
const (
|
||||
// Ровно 10 MiB, как в контракте: 10 * 1024 * 1024 = 10 485 760 байт.
|
||||
// Это ЖЁСТКИЙ потолок: одна загрузка не может занять больше, даже если
|
||||
// клиент шлёт бесконечный поток (см. http.MaxBytesReader в uploadAttachment).
|
||||
maxUploadBytes = 10 << 20
|
||||
// Не более 40 миллионов декодированных пикселей (ширина * высота).
|
||||
maxDecodedPixels = 40_000_000
|
||||
)
|
||||
|
||||
var usernameRe = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
// parseUUID — канонический UUID или ошибка (из lab1).
|
||||
func parseUUID(s string) (string, error) {
|
||||
u, err := uuid.Parse(s)
|
||||
if err != nil || u.String() != s {
|
||||
return "", errors.New("invalid uuid")
|
||||
}
|
||||
return u.String(), nil
|
||||
}
|
||||
|
||||
func runeLen(s string) int { return utf8.RuneCountInString(s) }
|
||||
|
||||
// validText — обязательная строка 1..max code points, не только пробелы (из lab1).
|
||||
func validText(s string, max int) bool {
|
||||
n := runeLen(s)
|
||||
return n >= 1 && n <= max && strings.TrimSpace(s) != ""
|
||||
}
|
||||
|
||||
// validPassword — контракт lab2: 12–128 code points, UTF-8, двоеточие допустимо.
|
||||
func validPassword(s string) bool {
|
||||
n := runeLen(s)
|
||||
return n >= 12 && n <= 128 && strings.TrimSpace(s) != ""
|
||||
}
|
||||
|
||||
// validFilename — отображаемое имя файла (1..255 code points), lab4.
|
||||
// Отклоняем '/', '\', NUL и управляющие символы. Имя используется ТОЛЬКО для
|
||||
// отображения и никогда как путь или ключ S3 — ключ генерирует сервер.
|
||||
func validFilename(s string) bool {
|
||||
n := runeLen(s)
|
||||
if n < 1 || n > 255 {
|
||||
return false
|
||||
}
|
||||
if strings.ContainsAny(s, "/\\\x00") {
|
||||
return false
|
||||
}
|
||||
for _, r := range s {
|
||||
if r < 0x20 || r == 0x7f { // управляющие символы
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// sniffImage — определяет фактический тип изображения по СОДЕРЖИМОМУ и его
|
||||
// геометрические размеры БЕЗ полного декодирования пикселей.
|
||||
// Возвращает (contentType, width, height, error).
|
||||
// Контракт: MIME проверяется по содержимому; лимит пикселей применяется до
|
||||
// полной распаковки. Декодеры берут только заголовок, поэтому память ограничена.
|
||||
func sniffImage(data []byte) (string, int, int, error) {
|
||||
ct := http.DetectContentType(data)
|
||||
if ct != "image/png" && ct != "image/jpeg" {
|
||||
return "", 0, 0, errors.New("unsupported image type")
|
||||
}
|
||||
cfg, format, err := image.DecodeConfig(bytes.NewReader(data))
|
||||
if err != nil {
|
||||
return "", 0, 0, err // повреждённое/неполное изображение
|
||||
}
|
||||
// Проверяем, что формат соответствует MIME (подмена типа).
|
||||
expectedFormat := ""
|
||||
if ct == "image/png" {
|
||||
expectedFormat = "png"
|
||||
} else if ct == "image/jpeg" {
|
||||
expectedFormat = "jpeg"
|
||||
}
|
||||
if format != expectedFormat {
|
||||
return "", 0, 0, errors.New("declared Content-Type does not match file content")
|
||||
}
|
||||
return ct, cfg.Width, cfg.Height, nil
|
||||
}
|
||||
|
||||
// ─────────────── Разбор Authorization (из lab2, без изменений) ───────────────
|
||||
type loginCreds struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
func parseBasicHeader(r *http.Request) (loginCreds, bool) {
|
||||
h := r.Header.Get("Authorization")
|
||||
const prefix = "Basic "
|
||||
if !strings.HasPrefix(h, prefix) {
|
||||
return loginCreds{}, false
|
||||
}
|
||||
raw, err := base64.StdEncoding.DecodeString(strings.TrimPrefix(h, prefix))
|
||||
if err != nil {
|
||||
return loginCreds{}, false
|
||||
}
|
||||
u, p, found := strings.Cut(string(raw), ":")
|
||||
if !found {
|
||||
return loginCreds{}, false
|
||||
}
|
||||
return loginCreds{Username: u, Password: p}, true
|
||||
}
|
||||
|
||||
func parseBearerHeader(r *http.Request) (string, bool) {
|
||||
h := r.Header.Get("Authorization")
|
||||
const prefix = "Bearer "
|
||||
if !strings.HasPrefix(h, prefix) {
|
||||
return "", false
|
||||
}
|
||||
t := strings.TrimPrefix(h, prefix)
|
||||
if t == "" {
|
||||
return "", false
|
||||
}
|
||||
return t, true
|
||||
}
|
||||
|
||||
// decodeJSON — конвейер 415 -> 413 -> 400 (из lab1, без изменений).
|
||||
// ВНИМАНИЕ: используется только для JSON-эндпоинтов. Загрузка вложений идёт
|
||||
// сырыми байтами мимо этой функции (см. uploadAttachment).
|
||||
func decodeJSON(w http.ResponseWriter, r *http.Request, dst any) *apperr.AppError {
|
||||
media, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
||||
if media != "application/json" {
|
||||
return apperr.UnsupportedMedia("Content-Type must be application/json")
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, maxBodyBytes)
|
||||
dec := json.NewDecoder(r.Body)
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(dst); err != nil {
|
||||
var maxErr *http.MaxBytesError
|
||||
if errors.As(err, &maxErr) {
|
||||
return apperr.PayloadTooLarge("request body too large")
|
||||
}
|
||||
return apperr.InvalidRequest("malformed JSON body")
|
||||
}
|
||||
if dec.More() {
|
||||
return apperr.InvalidRequest("multiple JSON documents in body")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// pagingParams — limit 1..100 (default 50) + курсор с проверкой scope (из lab1).
|
||||
func pagingParams(r *http.Request, scope string) (int, *store.Cursor, *apperr.AppError) {
|
||||
limit := 50
|
||||
if v := r.URL.Query().Get("limit"); v != "" {
|
||||
n, err := strconv.Atoi(v)
|
||||
if err != nil || n < 1 || n > 100 {
|
||||
return 0, nil, apperr.InvalidRequest("limit must be an integer in 1..100")
|
||||
}
|
||||
limit = n
|
||||
}
|
||||
var cur *store.Cursor
|
||||
if v := r.URL.Query().Get("cursor"); v != "" {
|
||||
c, err := store.DecodeCursor(v)
|
||||
if err != nil {
|
||||
return 0, nil, apperr.InvalidRequest("invalid cursor")
|
||||
}
|
||||
if c.Scope != scope {
|
||||
return 0, nil, apperr.InvalidRequest("cursor does not belong to this resource")
|
||||
}
|
||||
cur = &c
|
||||
}
|
||||
return limit, cur, nil
|
||||
}
|
||||
|
||||
// writeJSON — единая точка успешного ответа (из lab1).
|
||||
func writeJSON(w http.ResponseWriter, r *http.Request, status int, payload any) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("X-Request-Id", requestID(r))
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(payload)
|
||||
}
|
||||
|
||||
// writeNoContent — 204 без тела (logout). Тела нет, но заголовки контракта остаются.
|
||||
func writeNoContent(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Request-Id", requestID(r))
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
|
||||
func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
|
||||
apperr.Write(w, requestID(r), apperr.New(http.StatusMethodNotAllowed, apperr.CodeInvalidRequest, "method not allowed"))
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi-messenger/internal/apperr"
|
||||
"mtusi-messenger/internal/model"
|
||||
"mtusi-messenger/internal/store"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ctxKey int
|
||||
|
||||
const (
|
||||
ctxRequestID ctxKey = iota
|
||||
ctxUser
|
||||
ctxToken
|
||||
ctxCreds
|
||||
)
|
||||
|
||||
const (
|
||||
wwwBearer = `Bearer realm="messenger"`
|
||||
wwwBasic = `Basic realm="messenger", charset="UTF-8"`
|
||||
)
|
||||
|
||||
// ─────────── Охранник 1: номерок (из lab1) ───────────
|
||||
func requestIDMiddleware(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
id := r.Header.Get("X-Request-Id")
|
||||
if id == "" {
|
||||
id = uuid.NewString()
|
||||
}
|
||||
w.Header().Set("X-Request-Id", id)
|
||||
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), ctxRequestID, id)))
|
||||
})
|
||||
}
|
||||
|
||||
func requestID(r *http.Request) string {
|
||||
if id, ok := r.Context().Value(ctxRequestID).(string); ok && id != "" {
|
||||
return id
|
||||
}
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// ─────────── Охранник 2: журнал без тел (из lab1) ───────────
|
||||
type statusWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (s *statusWriter) WriteHeader(code int) {
|
||||
s.status = code
|
||||
s.ResponseWriter.WriteHeader(code)
|
||||
}
|
||||
|
||||
func loggingMiddleware(log *slog.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
sw := &statusWriter{ResponseWriter: w, status: http.StatusOK}
|
||||
next.ServeHTTP(sw, r)
|
||||
log.Info("http",
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
"status", sw.status,
|
||||
"dur_ms", time.Since(start).Milliseconds(),
|
||||
"request_id", requestID(r),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
// ─────────── Охранник 3: выключатель паник (из lab1) ───────────
|
||||
func recoverMiddleware(log *slog.Logger, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
if rec := recover(); rec != nil {
|
||||
log.Error("panic recovered", "path", r.URL.Path, "request_id", requestID(r))
|
||||
apperr.Write(w, requestID(r), apperr.Internal("internal error"))
|
||||
}
|
||||
}()
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// ─────────── Охранник 4: Идентификатор реплики (Lab 3) ───────────
|
||||
func instanceIDMiddleware(instanceID string, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Instance-Id", instanceID)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// ─────────── Пропускной пункт lab2: Bearer ───────────
|
||||
func (s *Server) auth(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
h := r.Header.Get("Authorization")
|
||||
if strings.HasPrefix(h, "Basic ") {
|
||||
w.Header().Set("WWW-Authenticate", wwwBearer)
|
||||
apperr.Write(w, id, apperr.Unauthorized("Bearer token required"))
|
||||
return
|
||||
}
|
||||
token, ok := parseBearerHeader(r)
|
||||
if !ok {
|
||||
w.Header().Set("WWW-Authenticate", wwwBearer)
|
||||
apperr.Write(w, id, apperr.Unauthorized("Bearer token required"))
|
||||
return
|
||||
}
|
||||
user, err := s.store.SessionUser(r.Context(), token)
|
||||
if err != nil {
|
||||
if store.IsUnavailable(err) {
|
||||
apperr.Write(w, id, apperr.Unavailable("database unavailable"))
|
||||
return
|
||||
}
|
||||
w.Header().Set("WWW-Authenticate", wwwBearer)
|
||||
apperr.Write(w, id, apperr.Unauthorized("valid Bearer token required"))
|
||||
return
|
||||
}
|
||||
ctx := context.WithValue(r.Context(), ctxUser, user)
|
||||
ctx = context.WithValue(ctx, ctxToken, token)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
}
|
||||
}
|
||||
|
||||
// ─────────── Входная дверь: только Basic ───────────
|
||||
func (s *Server) basicOnly(next http.HandlerFunc) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
id := requestID(r)
|
||||
creds, ok := parseBasicHeader(r)
|
||||
if !ok {
|
||||
w.Header().Set("WWW-Authenticate", wwwBasic)
|
||||
apperr.Write(w, id, apperr.Unauthorized("Basic credentials required"))
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), ctxCreds, creds)))
|
||||
}
|
||||
}
|
||||
|
||||
func currentUser(r *http.Request) *model.User { return r.Context().Value(ctxUser).(*model.User) }
|
||||
func currentToken(r *http.Request) string { return r.Context().Value(ctxToken).(string) }
|
||||
func currentCreds(r *http.Request) loginCreds { return r.Context().Value(ctxCreds).(loginCreds) }
|
||||
@@ -0,0 +1,55 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
|
||||
"mtusi-messenger/internal/store"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
store *store.Store
|
||||
log *slog.Logger
|
||||
ready *atomic.Bool
|
||||
instanceID string // LAB 3: ID реплики
|
||||
}
|
||||
|
||||
func NewServer(st *store.Store, log *slog.Logger, ready *atomic.Bool, instanceID string) *Server {
|
||||
return &Server{store: st, log: log, ready: ready, instanceID: instanceID}
|
||||
}
|
||||
|
||||
func (s *Server) Handler() http.Handler {
|
||||
mux := http.NewServeMux()
|
||||
|
||||
// Публичные (security: [] в OpenAPI):
|
||||
mux.HandleFunc("/health/live", s.healthLive)
|
||||
mux.HandleFunc("/health/ready", s.healthReady)
|
||||
mux.HandleFunc("/api/v1/users", s.usersRoute)
|
||||
// Вход — ТОЛЬКО Basic:
|
||||
mux.HandleFunc("/api/v1/auth/sessions", s.basicOnly(s.createSession))
|
||||
|
||||
// Всё остальное — только Bearer:
|
||||
mux.HandleFunc("/api/v1/auth/sessions/current", s.auth(s.revokeSession))
|
||||
mux.HandleFunc("/api/v1/users/me", s.auth(s.me))
|
||||
mux.HandleFunc("/api/v1/chats", s.auth(s.chatsRoute))
|
||||
mux.HandleFunc("/api/v1/chats/{chat_id}", s.auth(s.chatRoute))
|
||||
mux.HandleFunc("/api/v1/chats/{chat_id}/messages", s.auth(s.messagesRoute))
|
||||
|
||||
// ─────────── LAB 4: вложения ───────────
|
||||
// Загрузка одного PNG/JPEG (сырые байты, НЕ multipart).
|
||||
mux.HandleFunc("/api/v1/chats/{chat_id}/attachments", s.auth(s.attachmentsRoute))
|
||||
// Метаданные вложения (доступ только участникам чата).
|
||||
mux.HandleFunc("/api/v1/attachments/{attachment_id}", s.auth(s.attachmentRoute))
|
||||
// Скачивание байтов через авторизованный API.
|
||||
mux.HandleFunc("/api/v1/attachments/{attachment_id}/content", s.auth(s.attachmentContentRoute))
|
||||
|
||||
mux.HandleFunc("/", s.notFound)
|
||||
|
||||
var h http.Handler = mux
|
||||
h = recoverMiddleware(s.log, h)
|
||||
h = loggingMiddleware(s.log, h)
|
||||
h = requestIDMiddleware(h)
|
||||
h = instanceIDMiddleware(s.instanceID, h)
|
||||
return h
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package apperr
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type Code string
|
||||
|
||||
const (
|
||||
CodeInvalidRequest Code = "invalid_request"
|
||||
CodeUnauthorized Code = "unauthorized"
|
||||
CodeNotFound Code = "not_found"
|
||||
CodeConflict Code = "conflict"
|
||||
CodePayloadTooLarge Code = "payload_too_large"
|
||||
CodeUnsupportedMedia Code = "unsupported_media_type"
|
||||
CodeRateLimited Code = "rate_limited"
|
||||
CodeUnavailable Code = "unavailable"
|
||||
CodeInternal Code = "internal_error"
|
||||
)
|
||||
|
||||
type Body struct {
|
||||
Code Code `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
|
||||
type Envelope struct {
|
||||
Error Body `json:"error"`
|
||||
}
|
||||
|
||||
type AppError struct {
|
||||
Status int
|
||||
Code Code
|
||||
Msg string
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string { return e.Msg }
|
||||
|
||||
func New(status int, code Code, msg string) *AppError {
|
||||
return &AppError{Status: status, Code: code, Msg: msg}
|
||||
}
|
||||
|
||||
func InvalidRequest(msg string) *AppError { return New(http.StatusBadRequest, CodeInvalidRequest, msg) }
|
||||
func Unauthorized(msg string) *AppError { return New(http.StatusUnauthorized, CodeUnauthorized, msg) }
|
||||
func NotFound(msg string) *AppError { return New(http.StatusNotFound, CodeNotFound, msg) }
|
||||
func Conflict(msg string) *AppError { return New(http.StatusConflict, CodeConflict, msg) }
|
||||
func PayloadTooLarge(msg string) *AppError {
|
||||
return New(http.StatusRequestEntityTooLarge, CodePayloadTooLarge, msg)
|
||||
}
|
||||
func UnsupportedMedia(msg string) *AppError {
|
||||
return New(http.StatusUnsupportedMediaType, CodeUnsupportedMedia, msg)
|
||||
}
|
||||
func Unavailable(msg string) *AppError {
|
||||
return New(http.StatusServiceUnavailable, CodeUnavailable, msg)
|
||||
}
|
||||
func Internal(msg string) *AppError { return New(http.StatusInternalServerError, CodeInternal, msg) }
|
||||
|
||||
// Write отдаёт ошибку в формате контракта: {"error":{"code","message","request_id"}}.
|
||||
func Write(w http.ResponseWriter, requestID string, err *AppError) {
|
||||
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
w.WriteHeader(err.Status)
|
||||
_ = json.NewEncoder(w).Encode(Envelope{Error: Body{Code: err.Code, Message: err.Msg, RequestID: requestID}})
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Timestamp — детерминированная сериализация времени (ровно 3 знака миллисекунд).
|
||||
// Из lab1, без изменений.
|
||||
type Timestamp struct{ time.Time }
|
||||
|
||||
func (t Timestamp) MarshalJSON() ([]byte, error) {
|
||||
s := t.UTC().Format("2006-01-02T15:04:05.000Z")
|
||||
return strconv.AppendQuote(nil, s), nil
|
||||
}
|
||||
|
||||
func NowTS() Timestamp { return Timestamp{Time: time.Now().UTC().Truncate(time.Millisecond)} }
|
||||
|
||||
// User — ответ API: пароль и его hash НЕ включены (контракт).
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt Timestamp `json:"created_at"`
|
||||
}
|
||||
|
||||
// CreateUserReq — вход регистрации: username + display_name + password.
|
||||
// Password только на входе, в User не попадает.
|
||||
type CreateUserReq struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
// LoginRequest — вход в систему через Basic Auth (парсится из заголовка Authorization).
|
||||
type LoginRequest struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
// Session — внутренняя модель сессии (не отдаётся клиенту).
|
||||
type Session struct {
|
||||
TokenHash []byte // SHA-256 от opaque-токена
|
||||
UserID string
|
||||
ExpiresAt time.Time
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// SessionResponse — ответ POST /auth/sessions (то, что отдаём клиенту).
|
||||
type SessionResponse struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"` // всегда "Bearer"
|
||||
ExpiresAt Timestamp `json:"expires_at"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt Timestamp `json:"created_at"`
|
||||
}
|
||||
|
||||
type CreateChatReq struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
|
||||
// Message — ответ API. Поле AttachmentIDs обязательно по контракту lab4,
|
||||
// для старых текстовых сообщений возвращаем [].
|
||||
type Message struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt Timestamp `json:"created_at"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
// CreateMessageReq — вход для отправки сообщения.
|
||||
// AttachmentIDs опционален (default: []); по контракту anyOf:
|
||||
// либо text minLength:1, либо attachment_ids minItems:1.
|
||||
type CreateMessageReq struct {
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
// Page — дженерик-страница (из lab1).
|
||||
type Page[T any] struct {
|
||||
Items []T `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type Health struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// ─────────────────────── LAB 4: Вложения ───────────────────────
|
||||
|
||||
// Attachment — метаданные вложения (ответ API).
|
||||
// В лабе 4 всегда: status="ready", variants=[], error_code=null.
|
||||
// S3Key хранится в БД, но НЕ отдаётся клиенту (json:"-").
|
||||
type Attachment struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"` // "image/png" | "image/jpeg"
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status string `json:"status"` // "ready" в лабе 4
|
||||
Variants []Variant `json:"variants"` // [] в лабе 4
|
||||
ErrorCode *string `json:"error_code"` // null в лабе 4
|
||||
CreatedAt Timestamp `json:"created_at"`
|
||||
UpdatedAt Timestamp `json:"updated_at"`
|
||||
|
||||
// Внутренние поля, не сериализуются в JSON
|
||||
S3Key string `json:"-"`
|
||||
}
|
||||
|
||||
// Variant — производная версия оригинала (thumbnail, crop, optimized).
|
||||
// В лабе 4 не используется (variants всегда []), структура нужна для схемы контракта.
|
||||
type Variant struct {
|
||||
Name string `json:"name"` // "thumbnail" | "crop" | "optimized"
|
||||
ContentType string `json:"content_type"` // image/jpeg | image/png | image/webp
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
@@ -0,0 +1,223 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"mtusi-messenger/internal/model"
|
||||
)
|
||||
|
||||
// redisTimeout — максимальное время на одну Redis-операцию.
|
||||
// При недоступном Redis клиент быстро возвращает ошибку, и мы деградируем
|
||||
// в SQL, а не блокируем запрос (требование уровня 4 про потерю сервиса).
|
||||
const redisTimeout = 300 * time.Millisecond
|
||||
|
||||
// ─────────────── Кэш сессий (уровень 4) ───────────────
|
||||
|
||||
func sessKey(hash [sha256.Size]byte) string { return "sess:" + hex.EncodeToString(hash[:]) }
|
||||
|
||||
// cacheSessionPut: TTL кэша = остаток срока сессии (политика expiry согласована
|
||||
// с источником истины); при ошибке Redis — только warn, запрос живёт через SQL.
|
||||
func (s *Store) cacheSessionPut(ctx context.Context, hash [sha256.Size]byte, userID string, expiresAt time.Time) {
|
||||
if s.rdb == nil {
|
||||
return
|
||||
}
|
||||
ttl := time.Until(expiresAt)
|
||||
if ttl <= 0 {
|
||||
return
|
||||
}
|
||||
val := userID + "|" + expiresAt.Format(time.RFC3339Nano)
|
||||
rctx, cancel := context.WithTimeout(ctx, redisTimeout)
|
||||
defer cancel()
|
||||
if err := s.rdb.Set(rctx, sessKey(hash), val, ttl).Err(); err != nil {
|
||||
s.log.Warn("redis set failed, degrading to postgres", "error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// cacheSessionGet: miss (redis.Nil) — норма; ошибка/таймаут Redis — деградация;
|
||||
// истёкшее значение в кэше не доверяем (двойная страховка помимо TTL).
|
||||
func (s *Store) cacheSessionGet(ctx context.Context, hash [sha256.Size]byte) (string, bool) {
|
||||
if s.rdb == nil {
|
||||
return "", false
|
||||
}
|
||||
rctx, cancel := context.WithTimeout(ctx, redisTimeout)
|
||||
defer cancel()
|
||||
val, err := s.rdb.Get(rctx, sessKey(hash)).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return "", false
|
||||
}
|
||||
if err != nil {
|
||||
s.log.Warn("redis get failed, degrading to postgres", "error", err.Error())
|
||||
return "", false
|
||||
}
|
||||
parts := strings.SplitN(val, "|", 2)
|
||||
if len(parts) != 2 {
|
||||
return "", false
|
||||
}
|
||||
exp, err := time.Parse(time.RFC3339Nano, parts[1])
|
||||
if err != nil || time.Now().UTC().After(exp) {
|
||||
return "", false
|
||||
}
|
||||
return parts[0], true
|
||||
}
|
||||
|
||||
func (s *Store) cacheSessionDel(ctx context.Context, hash [sha256.Size]byte) error {
|
||||
if s.rdb == nil {
|
||||
return nil
|
||||
}
|
||||
rctx, cancel := context.WithTimeout(ctx, redisTimeout)
|
||||
defer cancel()
|
||||
return s.rdb.Del(rctx, sessKey(hash)).Err()
|
||||
}
|
||||
|
||||
// ─────────────── Версия чата (инвалидация кэша истории, трек 5) ───────────────
|
||||
|
||||
func chatVerKey(chatID string) string { return "chatver:" + chatID }
|
||||
|
||||
func (s *Store) bumpChatVersion(ctx context.Context, chatID string) {
|
||||
if s.rdb == nil {
|
||||
return
|
||||
}
|
||||
var err error
|
||||
for attempt := 1; attempt <= 3; attempt++ {
|
||||
rctx, cancel := context.WithTimeout(ctx, redisTimeout)
|
||||
err = s.rdb.Incr(rctx, chatVerKey(chatID)).Err()
|
||||
cancel()
|
||||
if err == nil {
|
||||
return
|
||||
}
|
||||
s.log.Warn("redis incr failed, retrying", "attempt", attempt, "error", err.Error())
|
||||
time.Sleep(50 * time.Millisecond)
|
||||
}
|
||||
s.log.Warn("redis incr failed after retries; history cache bounded stale until TTL", "error", err.Error())
|
||||
}
|
||||
|
||||
// ChatVersion: 0 при отсутствии Redis/ключа — кэш истории тогда не используется,
|
||||
// корректность не страдает.
|
||||
func (s *Store) ChatVersion(ctx context.Context, chatID string) int64 {
|
||||
if s.rdb == nil {
|
||||
return 0
|
||||
}
|
||||
rctx, cancel := context.WithTimeout(ctx, redisTimeout)
|
||||
defer cancel()
|
||||
v, err := s.rdb.Get(rctx, chatVerKey(chatID)).Int64()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return v
|
||||
}
|
||||
|
||||
// ─────────────── Кэш истории (трек 5) ───────────────
|
||||
|
||||
// HistoryCacheKey — ключ страницы истории: версия чата + параметры запроса.
|
||||
// Версия в ключе — это и есть защита от stale read.
|
||||
func HistoryCacheKey(chatID string, version int64, cursorJSON string, limit int) string {
|
||||
return fmt.Sprintf("hist:v%d:%s:%s:%d", version, chatID, cursorJSON, limit)
|
||||
}
|
||||
|
||||
// CacheHistoryPut — положить страницу в кэш. TTL 60 c: кэш для горячего чтения,
|
||||
// не долгосрочное хранилище; источник истины остаётся Postgres.
|
||||
func (s *Store) CacheHistoryPut(ctx context.Context, key string, messages []*model.Message, hasMore bool) {
|
||||
if s.rdb == nil {
|
||||
return
|
||||
}
|
||||
data := map[string]any{
|
||||
"items": messages,
|
||||
"has_more": hasMore,
|
||||
"cached_at": time.Now().UTC().Format(time.RFC3339Nano),
|
||||
}
|
||||
b, err := json.Marshal(data)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
rctx, cancel := context.WithTimeout(ctx, redisTimeout)
|
||||
defer cancel()
|
||||
if err := s.rdb.Set(rctx, key, b, 60*time.Second).Err(); err != nil {
|
||||
s.log.Warn("redis history set failed", "error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// CacheHistoryGet — достать страницу из кэша: (items, hasMore, ok).
|
||||
// ok=false означает miss или ошибку Redis — тогда идём в SQL.
|
||||
func (s *Store) CacheHistoryGet(ctx context.Context, key string) ([]*model.Message, bool, bool) {
|
||||
if s.rdb == nil {
|
||||
return nil, false, false
|
||||
}
|
||||
rctx, cancel := context.WithTimeout(ctx, redisTimeout)
|
||||
defer cancel()
|
||||
val, err := s.rdb.Get(rctx, key).Result()
|
||||
if err != nil {
|
||||
return nil, false, false
|
||||
}
|
||||
var data struct {
|
||||
Items []*model.Message `json:"items"`
|
||||
HasMore bool `json:"has_more"`
|
||||
}
|
||||
if err := json.Unmarshal([]byte(val), &data); err != nil {
|
||||
return nil, false, false
|
||||
}
|
||||
return data.Items, data.HasMore, true
|
||||
}
|
||||
|
||||
// ─────────────── Single-flight (защита от cache stampede) ───────────────
|
||||
// При конкурирующих холодных чтениях одного ключа только один поток выполняет
|
||||
// fn; остальные ждут его результат. Без этого N конкурентных miss дали бы N
|
||||
// SQL-запросов («лавиной») — это и есть cache stampede.
|
||||
|
||||
type SingleFlight struct {
|
||||
mu sync.Mutex
|
||||
m map[string]*call
|
||||
}
|
||||
|
||||
type call struct {
|
||||
wg sync.WaitGroup
|
||||
val any
|
||||
err error
|
||||
}
|
||||
|
||||
func NewSingleFlight() *SingleFlight {
|
||||
return &SingleFlight{m: make(map[string]*call)}
|
||||
}
|
||||
|
||||
// Do выполняет fn только если нет активного вызова с тем же key; иначе ждёт
|
||||
// готовый результат того вызова.
|
||||
func (sf *SingleFlight) Do(key string, fn func() (any, error)) (any, error) {
|
||||
sf.mu.Lock()
|
||||
if c, ok := sf.m[key]; ok {
|
||||
sf.mu.Unlock()
|
||||
c.wg.Wait()
|
||||
return c.val, c.err
|
||||
}
|
||||
c := new(call)
|
||||
c.wg.Add(1)
|
||||
sf.m[key] = c
|
||||
sf.mu.Unlock()
|
||||
|
||||
c.val, c.err = fn()
|
||||
c.wg.Done()
|
||||
|
||||
sf.mu.Lock()
|
||||
delete(sf.m, key)
|
||||
sf.mu.Unlock()
|
||||
|
||||
return c.val, c.err
|
||||
}
|
||||
|
||||
// EncodeCursorJSON — компактное представление курсора для ключа кэша.
|
||||
func EncodeCursorJSON(c *Cursor) string {
|
||||
if c == nil {
|
||||
return "null"
|
||||
}
|
||||
b, _ := json.Marshal(c)
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"embed"
|
||||
"fmt"
|
||||
"io/fs"
|
||||
"log/slog"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationsFS embed.FS
|
||||
|
||||
// RunMigrations запускает все миграции из папки migrations/ по алфавиту.
|
||||
// Идемпотентно: IF NOT EXISTS в SQL защищает от дублей.
|
||||
func RunMigrations(ctx context.Context, pool *pgxpool.Pool, log *slog.Logger) error {
|
||||
entries, err := fs.ReadDir(migrationsFS, "migrations")
|
||||
if err != nil {
|
||||
return fmt.Errorf("read migrations dir: %w", err)
|
||||
}
|
||||
|
||||
// Сортируем по имени файла (0001_init.sql, 0002_..., ...)
|
||||
var files []string
|
||||
for _, e := range entries {
|
||||
if !e.IsDir() && strings.HasSuffix(e.Name(), ".sql") {
|
||||
files = append(files, e.Name())
|
||||
}
|
||||
}
|
||||
sort.Strings(files)
|
||||
|
||||
for _, name := range files {
|
||||
sql, err := fs.ReadFile(migrationsFS, filepath.Join("migrations", name))
|
||||
if err != nil {
|
||||
return fmt.Errorf("read migration %s: %w", name, err)
|
||||
}
|
||||
log.Info("running migration", "file", name)
|
||||
if _, err := pool.Exec(ctx, string(sql)); err != nil {
|
||||
return fmt.Errorf("exec migration %s: %w", name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
-- Все операции идемпотентны (IF NOT EXISTS)
|
||||
|
||||
-- Пользователи
|
||||
CREATE TABLE IF NOT EXISTS 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_re CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
CONSTRAINT users_name_len CHECK (char_length(display_name) BETWEEN 1 AND 100)
|
||||
);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS users_username_key ON users (username);
|
||||
|
||||
-- Чаты
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
-- Участники чатов
|
||||
CREATE TABLE IF NOT EXISTS chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
-- Сообщения
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id uuid NOT NULL,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
|
||||
sender_id uuid NOT NULL REFERENCES users(id),
|
||||
text text NOT NULL CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (chat_id, id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS messages_history_idx ON messages (chat_id, created_at, id);
|
||||
|
||||
-- Сессии (источник истины)
|
||||
CREATE TABLE IF NOT EXISTS 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()
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS sessions_expires_idx ON sessions (expires_at);
|
||||
@@ -0,0 +1,14 @@
|
||||
-- Lab 3: идемпотентность отправки сообщений.
|
||||
-- Все операции идемпотентны (IF NOT EXISTS).
|
||||
|
||||
-- Область ключа: (chat_id, sender_id, idempotency_key).
|
||||
-- Это пользователь + путь + ключ для фиксированного метода POST.
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD COLUMN IF NOT EXISTS idempotency_key text;
|
||||
|
||||
-- У старых сообщений ключа нет (NULL).
|
||||
-- В Postgres NULL'ы в уникальном индексе считаются различающимися,
|
||||
-- поэтому старый контент не нарушит уникальность.
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS messages_idempotency_uq
|
||||
ON messages (chat_id, sender_id, idempotency_key);
|
||||
@@ -0,0 +1,27 @@
|
||||
CREATE TABLE IF NOT EXISTS attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
filename text NOT NULL,
|
||||
content_type text NOT NULL, -- 'image/png' | 'image/jpeg' (MIME по содержимому)
|
||||
size_bytes bigint NOT NULL CHECK (size_bytes >= 1 AND size_bytes <= 10485760),
|
||||
sha256 text NOT NULL, -- lowercase hex, 64 символа
|
||||
s3_key text NOT NULL UNIQUE, -- внутренний ключ бакета; НЕ отдаётся клиенту
|
||||
status text NOT NULL DEFAULT 'ready', -- в лабе 4 всегда 'ready'
|
||||
error_code text, -- в лабе 4 всегда NULL
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_attachments_chat ON attachments(chat_id);
|
||||
|
||||
-- ============================================================
|
||||
-- LAB 4: сообщения ссылаются на вложения этого чата.
|
||||
-- ============================================================
|
||||
-- Массив хранит 0..10 уникальных UUID. Ограничения по количеству и
|
||||
-- уникальности, а также принадлежность вложений ИМЕННО этому чату
|
||||
-- проверяются в приложении при создании сообщения (см. handlers.go).
|
||||
--
|
||||
-- Старые текстовые сообщения получают пустой массив '{}' — это сохраняет
|
||||
-- обратную совместимость и обязательное поле контракта "attachment_ids".
|
||||
ALTER TABLE messages ADD COLUMN IF NOT EXISTS attachment_ids uuid[] NOT NULL DEFAULT '{}';
|
||||
@@ -0,0 +1,18 @@
|
||||
-- LAB 4: разрешаем пустой текст, если сообщение содержит хотя бы одно вложение.
|
||||
--
|
||||
-- Контракт lab4:
|
||||
-- либо text непустой (1..4000),
|
||||
-- либо attachment_ids содержит хотя бы один элемент.
|
||||
--
|
||||
-- Старое ограничение lab3 требовало char_length(text) >= 1 всегда,
|
||||
-- поэтому оно ломает сообщения вида {"text":"","attachment_ids":[...]}.
|
||||
|
||||
ALTER TABLE messages
|
||||
DROP CONSTRAINT IF EXISTS messages_text_check;
|
||||
|
||||
ALTER TABLE messages
|
||||
ADD CONSTRAINT messages_text_check CHECK (
|
||||
(char_length(text) >= 1 AND char_length(text) <= 4000)
|
||||
OR
|
||||
(char_length(text) = 0 AND cardinality(attachment_ids) > 0)
|
||||
);
|
||||
@@ -0,0 +1,738 @@
|
||||
// Package store — слой доступа к данным lab2+lab4.
|
||||
// PostgreSQL = источник истины (пользователи, чаты, участники, сообщения, сессии,
|
||||
// метаданные вложений).
|
||||
// Redis = опциональный кэш (сессии + история в треке 5): при его потере
|
||||
// деградируем в SQL, а не отказываем.
|
||||
// S3-совместимое хранилище = источник истины для БАЙТОВ вложений (лаба 4).
|
||||
//
|
||||
// ВСЕ запросы параметризованы ($1, $2, ...): значения никогда не конкатенируются
|
||||
// в текст SQL — это защита от SQL-инъекций.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi-messenger/internal/model"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"golang.org/x/crypto/scrypt"
|
||||
)
|
||||
|
||||
// Sentinel-ошибки: store НЕ ЗНАЕТ про HTTP; хендлеры маппят флажки в статусы.
|
||||
var (
|
||||
ErrNotFound = errors.New("not found") // объекта нет ИЛИ он чужой
|
||||
ErrUsernameTaken = errors.New("username taken") // unique-ограничение
|
||||
ErrInvalidCursor = errors.New("invalid cursor") // битая/чужая закладка
|
||||
ErrInvalidLogin = errors.New("invalid credentials") // неверный пароль ИЛИ неизвестный user (намеренно одно!)
|
||||
ErrBadMembers = errors.New("invalid member_ids") // неизвестный участник / вызывающий не в списке
|
||||
ErrIdempotencyConflict = errors.New("idempotency conflict") // тот же ключ, но другое тело
|
||||
)
|
||||
|
||||
// Store — доступ к PostgreSQL (+ опциональный Redis) + S3-хранилище (лаба 4).
|
||||
type Store struct {
|
||||
pool *pgxpool.Pool // ограниченный пул соединений (уровень 4)
|
||||
rdb *redis.Client // nil = режим деградации без кэша
|
||||
s3 *S3Client // приватное S3-совместимое хранилище (лаба 4)
|
||||
sessionTTL time.Duration // из SESSION_TTL_SECONDS
|
||||
cacheMode string // "off" | "naive" | "versioned"; дефолт versioned (трек 5)
|
||||
log *slog.Logger
|
||||
sf *SingleFlight // stampede-защита кэша истории (трек 5)
|
||||
}
|
||||
|
||||
// New собирает Store. s3 может быть nil в случае недоступности — тогда все
|
||||
// операции с вложениями вернут ErrNotFound/503 (хендлеры маппят IsS3Unavailable).
|
||||
func New(pool *pgxpool.Pool, rdb *redis.Client, s3 *S3Client, sessionTTLSeconds, cacheMode string, log *slog.Logger) *Store {
|
||||
ttl := 3600
|
||||
if v, err := strconv.Atoi(sessionTTLSeconds); err == nil && v > 0 {
|
||||
ttl = v
|
||||
}
|
||||
if cacheMode == "" {
|
||||
cacheMode = "versioned"
|
||||
}
|
||||
return &Store{
|
||||
pool: pool,
|
||||
rdb: rdb,
|
||||
s3: s3,
|
||||
sessionTTL: time.Duration(ttl) * time.Second,
|
||||
cacheMode: cacheMode,
|
||||
log: log,
|
||||
sf: NewSingleFlight(),
|
||||
}
|
||||
}
|
||||
|
||||
// Ping — живость БД; используется в /health/ready: БД мертва => 503.
|
||||
func (s *Store) Ping(ctx context.Context) error { return s.pool.Ping(ctx) }
|
||||
|
||||
// ─────────────── Курсор и пагинация (перенесено из lab1 без изменений) ───────────────
|
||||
|
||||
type Cursor struct {
|
||||
Scope string `json:"s"` // чей ресурс: id чата или id пользователя
|
||||
TS time.Time `json:"t"` // время записи-закладки
|
||||
ID string `json:"i"` // id записи-закладки
|
||||
}
|
||||
|
||||
func EncodeCursor(c Cursor) string {
|
||||
b, _ := json.Marshal(c)
|
||||
return base64.RawURLEncoding.EncodeToString(b)
|
||||
}
|
||||
|
||||
func DecodeCursor(s string) (Cursor, error) {
|
||||
var c Cursor
|
||||
b, err := base64.RawURLEncoding.DecodeString(s)
|
||||
if err != nil {
|
||||
return c, ErrInvalidCursor
|
||||
}
|
||||
if err := json.Unmarshal(b, &c); err != nil {
|
||||
return c, ErrInvalidCursor
|
||||
}
|
||||
if c.Scope == "" || c.ID == "" || c.TS.IsZero() {
|
||||
return c, ErrInvalidCursor
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func lessPair(ai time.Time, aid string, bi time.Time, bid string) bool {
|
||||
if !ai.Equal(bi) {
|
||||
return ai.Before(bi)
|
||||
}
|
||||
return aid < bid
|
||||
}
|
||||
|
||||
// paginate — дженерик листания для списков, собранных в памяти (список чатов).
|
||||
// История сообщений листается НА СТОРОНЕ SQL (см. ListMessages).
|
||||
func paginate[T any](all []T, limit int, cur *Cursor, key func(T) (time.Time, string)) ([]T, bool) {
|
||||
start := 0
|
||||
if cur != nil {
|
||||
start = sort.Search(len(all), func(i int) bool {
|
||||
t, id := key(all[i])
|
||||
return lessPair(cur.TS, cur.ID, t, id)
|
||||
})
|
||||
}
|
||||
rest := all[start:]
|
||||
if len(rest) > limit {
|
||||
return rest[:limit], true
|
||||
}
|
||||
return rest, false
|
||||
}
|
||||
|
||||
// ─────────────── Пароли: KDF scrypt ───────────────
|
||||
|
||||
const (
|
||||
scryptN = 32768 // 2^15
|
||||
scryptR = 8
|
||||
scryptP = 1
|
||||
scryptKeyLen = 32
|
||||
saltLen = 16
|
||||
)
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
salt := make([]byte, saltLen)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", err
|
||||
}
|
||||
h, err := scrypt.Key([]byte(password), salt, scryptN, scryptR, scryptP, scryptKeyLen)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
enc := base64.RawStdEncoding.EncodeToString
|
||||
return fmt.Sprintf("scrypt$%d$%d$%d$%s$%s", scryptN, scryptR, scryptP, enc(salt), enc(h)), nil
|
||||
}
|
||||
|
||||
func VerifyPassword(password, encoded string) bool {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[0] != "scrypt" {
|
||||
return false
|
||||
}
|
||||
N, e1 := strconv.Atoi(parts[1])
|
||||
r, e2 := strconv.Atoi(parts[2])
|
||||
p, e3 := strconv.Atoi(parts[3])
|
||||
if e1 != nil || e2 != nil || e3 != nil {
|
||||
return false
|
||||
}
|
||||
salt, e4 := base64.RawStdEncoding.DecodeString(parts[4])
|
||||
want, e5 := base64.RawStdEncoding.DecodeString(parts[5])
|
||||
if e4 != nil || e5 != nil {
|
||||
return false
|
||||
}
|
||||
got, err := scrypt.Key([]byte(password), salt, N, r, p, len(want))
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return subtle.ConstantTimeCompare(want, got) == 1
|
||||
}
|
||||
|
||||
// isUniqueViolation — SQLSTATE 23505: нарушение UNIQUE (гонка регистрации).
|
||||
func isUniqueViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
return errors.As(err, &pgErr) && pgErr.Code == "23505"
|
||||
}
|
||||
|
||||
// ─────────────── Пользователи ───────────────
|
||||
|
||||
func (s *Store) CreateUser(ctx context.Context, username, displayName, password string) (*model.User, error) {
|
||||
hash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u := &model.User{ID: uuid.NewString(), Username: username, DisplayName: displayName, CreatedAt: model.NowTS()}
|
||||
_, err = s.pool.Exec(ctx,
|
||||
`INSERT INTO users (id, username, display_name, password_hash, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
u.ID, u.Username, u.DisplayName, hash, u.CreatedAt.Time)
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
return nil, ErrUsernameTaken
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
const userCols = `id, username, display_name, created_at`
|
||||
|
||||
func (s *Store) User(ctx context.Context, id string) (*model.User, error) {
|
||||
var u model.User
|
||||
var createdAt time.Time
|
||||
err := s.pool.QueryRow(ctx, `SELECT `+userCols+` FROM users WHERE id = $1`, id).
|
||||
Scan(&u.ID, &u.Username, &u.DisplayName, &createdAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
u.CreatedAt = model.Timestamp{Time: createdAt.UTC()}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (s *Store) UserByCredentials(ctx context.Context, username, password string) (*model.User, error) {
|
||||
var u model.User
|
||||
var createdAt time.Time
|
||||
var hash string
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT `+userCols+`, password_hash FROM users WHERE username = $1`, username).
|
||||
Scan(&u.ID, &u.Username, &u.DisplayName, &createdAt, &hash)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrInvalidLogin
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if !VerifyPassword(password, hash) {
|
||||
return nil, ErrInvalidLogin
|
||||
}
|
||||
u.CreatedAt = model.Timestamp{Time: createdAt.UTC()}
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
// ─────────────── Сессии ───────────────
|
||||
|
||||
func (s *Store) CreateSession(ctx context.Context, userID string) (string, time.Time, error) {
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
token := base64.RawURLEncoding.EncodeToString(raw)
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
expiresAt := time.Now().UTC().Add(s.sessionTTL)
|
||||
_, err := s.pool.Exec(ctx,
|
||||
`INSERT INTO sessions (token_hash, user_id, expires_at) VALUES ($1, $2, $3)`,
|
||||
hash[:], userID, expiresAt)
|
||||
if err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
s.cacheSessionPut(ctx, hash, userID, expiresAt)
|
||||
return token, expiresAt, nil
|
||||
}
|
||||
|
||||
func (s *Store) SessionUser(ctx context.Context, token string) (*model.User, error) {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
if userID, ok := s.cacheSessionGet(ctx, hash); ok {
|
||||
return s.User(ctx, userID)
|
||||
}
|
||||
var userID string
|
||||
var expiresAt time.Time
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT user_id, expires_at FROM sessions WHERE token_hash = $1`, hash[:]).
|
||||
Scan(&userID, &expiresAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if time.Now().UTC().After(expiresAt) {
|
||||
_, _ = s.pool.Exec(ctx, `DELETE FROM sessions WHERE token_hash = $1`, hash[:])
|
||||
s.cacheSessionDel(ctx, hash)
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
s.cacheSessionPut(ctx, hash, userID, expiresAt)
|
||||
return s.User(ctx, userID)
|
||||
}
|
||||
|
||||
func (s *Store) RevokeSession(ctx context.Context, token string) error {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
if _, err := s.pool.Exec(ctx, `DELETE FROM sessions WHERE token_hash = $1`, hash[:]); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.rdb == nil {
|
||||
return nil
|
||||
}
|
||||
var err error
|
||||
for attempt := 1; attempt <= 3; attempt++ {
|
||||
if err = s.cacheSessionDel(ctx, hash); err == nil {
|
||||
return nil
|
||||
}
|
||||
s.log.Warn("redis DEL failed on logout, retrying", "attempt", attempt, "error", err.Error())
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
return fmt.Errorf("session cache invalidation failed after retries: %w", err)
|
||||
}
|
||||
|
||||
// ─────────────── Чаты ───────────────
|
||||
|
||||
func (s *Store) CreateChat(ctx context.Context, title string, memberIDs []string, callerID string) (*model.Chat, error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
rows, err := tx.Query(ctx, `SELECT id FROM users WHERE id = ANY($1)`, memberIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
found := make(map[string]bool, len(memberIDs))
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
found[id] = true
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(found) != len(memberIDs) {
|
||||
return nil, ErrBadMembers
|
||||
}
|
||||
if !found[callerID] {
|
||||
return nil, ErrBadMembers
|
||||
}
|
||||
chat := &model.Chat{ID: uuid.NewString(), Title: title, MemberIDs: memberIDs, CreatedAt: model.NowTS()}
|
||||
if _, err := tx.Exec(ctx,
|
||||
`INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`,
|
||||
chat.ID, chat.Title, chat.CreatedAt.Time); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := tx.Exec(ctx,
|
||||
`INSERT INTO chat_members (chat_id, user_id) SELECT $1, unnest($2::uuid[])`,
|
||||
chat.ID, memberIDs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *Store) ChatForUser(ctx context.Context, chatID, userID string) (*model.Chat, error) {
|
||||
var c model.Chat
|
||||
var createdAt time.Time
|
||||
var members []string
|
||||
err := s.pool.QueryRow(ctx,
|
||||
`SELECT c.id, c.title, c.created_at, (SELECT array_agg(cm.user_id) FROM chat_members cm WHERE cm.chat_id = c.id) FROM chats c WHERE c.id = $1 AND EXISTS (SELECT 1 FROM chat_members cm WHERE cm.chat_id = c.id AND cm.user_id = $2)`,
|
||||
chatID, userID).Scan(&c.ID, &c.Title, &createdAt, &members)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
c.CreatedAt = model.Timestamp{Time: createdAt.UTC()}
|
||||
c.MemberIDs = members
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func (s *Store) ListChats(ctx context.Context, userID string, limit int, cur *Cursor) ([]*model.Chat, bool, error) {
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`SELECT c.id, c.title, c.created_at, array_agg(cm.user_id) FROM chats c JOIN chat_members cm ON cm.chat_id = c.id WHERE c.id IN (SELECT chat_id FROM chat_members WHERE user_id = $1) GROUP BY c.id ORDER BY c.created_at, c.id`, userID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
all := make([]*model.Chat, 0, 8)
|
||||
for rows.Next() {
|
||||
var c model.Chat
|
||||
var createdAt time.Time
|
||||
var members []string
|
||||
if err := rows.Scan(&c.ID, &c.Title, &createdAt, &members); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
c.CreatedAt = model.Timestamp{Time: createdAt.UTC()}
|
||||
c.MemberIDs = members
|
||||
all = append(all, &c)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
items, more := paginate(all, limit, cur, func(c *model.Chat) (time.Time, string) {
|
||||
return c.CreatedAt.Time, c.ID
|
||||
})
|
||||
return items, more, nil
|
||||
}
|
||||
|
||||
// ─────────────── Сообщения ───────────────
|
||||
// CreateMessage — идемпотентная отправка сообщения (Lab 3) + привязка вложений (Lab 4).
|
||||
// Область идемпотентности: (chat_id, sender_id, idempotency_key).
|
||||
// attachmentIDs проверяются на принадлежность чату в той же транзакции.
|
||||
func (s *Store) CreateMessage(
|
||||
ctx context.Context,
|
||||
chatID, senderID, text string,
|
||||
attachmentIDs []string,
|
||||
idempotencyKey string,
|
||||
) (*model.Message, error) {
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
// 1. Проверка членства в чате
|
||||
var one int
|
||||
err = tx.QueryRow(ctx,
|
||||
`SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2`,
|
||||
chatID, senderID).Scan(&one)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 2. Проверка принадлежности вложений ЭТОМУ чату.
|
||||
// Все вложения должны существовать и принадлежать chatID; чужое/неизвестное — 404.
|
||||
if len(attachmentIDs) > 0 {
|
||||
rows, err := tx.Query(ctx,
|
||||
`SELECT id FROM attachments WHERE id = ANY($1) AND chat_id = $2`,
|
||||
attachmentIDs, chatID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
found := make(map[string]bool, len(attachmentIDs))
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return nil, err
|
||||
}
|
||||
found[id] = true
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(found) != len(attachmentIDs) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
}
|
||||
|
||||
// 3. Пытаемся вставить сообщение. Если ключ уже есть — игнорируем (DO NOTHING)
|
||||
m := &model.Message{
|
||||
ID: uuid.NewString(),
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: model.NowTS(),
|
||||
AttachmentIDs: attachmentIDs,
|
||||
}
|
||||
var insertedID string
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at, idempotency_key, attachment_ids)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
||||
ON CONFLICT (chat_id, sender_id, idempotency_key) DO NOTHING
|
||||
RETURNING id
|
||||
`, m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt.Time, idempotencyKey, attachmentIDs).Scan(&insertedID)
|
||||
if err != nil && !errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 4. Если вернулось ErrNoRows, значит сработал ON CONFLICT DO NOTHING
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
var existing model.Message
|
||||
var createdAt time.Time
|
||||
var existingAttIDs []string
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT id, chat_id, sender_id, text, created_at, attachment_ids
|
||||
FROM messages
|
||||
WHERE chat_id = $1 AND sender_id = $2 AND idempotency_key = $3
|
||||
`, chatID, senderID, idempotencyKey).Scan(
|
||||
&existing.ID, &existing.ChatID, &existing.SenderID, &existing.Text, &createdAt, &existingAttIDs)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// Сравниваем тело (текст + вложения). Любое отличие — конфликт 409.
|
||||
if existing.Text != text || !sameStringSet(existingAttIDs, attachmentIDs) {
|
||||
return nil, ErrIdempotencyConflict
|
||||
}
|
||||
existing.CreatedAt = model.Timestamp{Time: createdAt.UTC()}
|
||||
existing.AttachmentIDs = existingAttIDs
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &existing, nil
|
||||
}
|
||||
|
||||
// 5. Успешная вставка
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if s.cacheMode == "versioned" {
|
||||
s.bumpChatVersion(ctx, chatID)
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
// sameStringSet — сравнение двух срезов строк как множеств.
|
||||
func sameStringSet(a, b []string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
m := make(map[string]struct{}, len(a))
|
||||
for _, v := range a {
|
||||
m[v] = struct{}{}
|
||||
}
|
||||
for _, v := range b {
|
||||
if _, ok := m[v]; !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (s *Store) ListMessages(ctx context.Context, chatID string, limit int, cur *Cursor) ([]*model.Message, bool, error) {
|
||||
cursorJSON := EncodeCursorJSON(cur)
|
||||
useCache := s.rdb != nil && s.cacheMode != "off"
|
||||
version := int64(0)
|
||||
if useCache && s.cacheMode == "versioned" {
|
||||
version = s.ChatVersion(ctx, chatID)
|
||||
}
|
||||
key := HistoryCacheKey(chatID, version, cursorJSON, limit)
|
||||
if useCache {
|
||||
if items, hasMore, ok := s.CacheHistoryGet(ctx, key); ok {
|
||||
s.log.Info("cache_hit", "chat_id", chatID, "mode", s.cacheMode, "cursor", cursorJSON)
|
||||
return items, hasMore, nil
|
||||
}
|
||||
}
|
||||
type pageResult struct {
|
||||
items []*model.Message
|
||||
hasMore bool
|
||||
}
|
||||
fetch := func() (any, error) {
|
||||
s.log.Info("sql_list_messages", "chat_id", chatID, "cursor", cursorJSON, "limit", limit)
|
||||
q := `SELECT id, chat_id, sender_id, text, created_at, attachment_ids FROM messages WHERE chat_id = $1`
|
||||
args := []any{chatID}
|
||||
if cur != nil {
|
||||
q += ` AND (created_at, id) > ($2, $3)`
|
||||
args = append(args, cur.TS, cur.ID)
|
||||
}
|
||||
q += ` ORDER BY created_at, id LIMIT $` + strconv.Itoa(len(args)+1)
|
||||
args = append(args, limit+1)
|
||||
rows, err := s.pool.Query(ctx, q, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := make([]*model.Message, 0, limit)
|
||||
for rows.Next() {
|
||||
var m model.Message
|
||||
var createdAt time.Time
|
||||
var attIDs []string
|
||||
if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &createdAt, &attIDs); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
m.CreatedAt = model.Timestamp{Time: createdAt.UTC()}
|
||||
if attIDs == nil {
|
||||
attIDs = []string{}
|
||||
}
|
||||
m.AttachmentIDs = attIDs
|
||||
items = append(items, &m)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
more := len(items) > limit
|
||||
if more {
|
||||
items = items[:limit]
|
||||
}
|
||||
return pageResult{items: items, hasMore: more}, nil
|
||||
}
|
||||
var res any
|
||||
var err error
|
||||
if useCache && s.cacheMode == "versioned" {
|
||||
res, err = s.sf.Do(key, fetch)
|
||||
} else {
|
||||
res, err = fetch()
|
||||
}
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
pr := res.(pageResult)
|
||||
if useCache {
|
||||
s.CacheHistoryPut(ctx, key, pr.items, pr.hasMore)
|
||||
}
|
||||
return pr.items, pr.hasMore, nil
|
||||
}
|
||||
|
||||
// ─────────────── Вложения (лаба 4) ───────────────
|
||||
|
||||
// CreateAttachment — вставка метаданных в БД. Вызывается после успешного
|
||||
// PUT в S3. При ошибке хендлер компенсирует — удаляет объект из S3.
|
||||
func (s *Store) CreateAttachment(ctx context.Context, att *model.Attachment) error {
|
||||
_, err := s.pool.Exec(ctx, `
|
||||
INSERT INTO attachments
|
||||
(id, chat_id, owner_id, filename, content_type, size_bytes, sha256, s3_key, status, error_code, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
|
||||
`,
|
||||
att.ID, att.ChatID, att.OwnerID, att.Filename, att.ContentType,
|
||||
att.SizeBytes, att.SHA256, att.S3Key, att.Status, att.ErrorCode,
|
||||
att.CreatedAt.Time, att.UpdatedAt.Time)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetAttachmentForUser — метаданные вложения с проверкой членства в чате.
|
||||
// Посторонний получает ErrNotFound (404) — не раскрываем существование объекта.
|
||||
func (s *Store) GetAttachmentForUser(ctx context.Context, attachmentID, userID string) (*model.Attachment, error) {
|
||||
var a model.Attachment
|
||||
var createdAt, updatedAt time.Time
|
||||
var s3Key string
|
||||
var errCode *string
|
||||
err := s.pool.QueryRow(ctx, `
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type,
|
||||
a.size_bytes, a.sha256, a.s3_key, a.status, a.error_code,
|
||||
a.created_at, a.updated_at
|
||||
FROM attachments a
|
||||
WHERE a.id = $1
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM chat_members cm
|
||||
WHERE cm.chat_id = a.chat_id AND cm.user_id = $2
|
||||
)
|
||||
`, attachmentID, userID).Scan(
|
||||
&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType,
|
||||
&a.SizeBytes, &a.SHA256, &s3Key, &a.Status, &errCode,
|
||||
&createdAt, &updatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
a.CreatedAt = model.Timestamp{Time: createdAt.UTC()}
|
||||
a.UpdatedAt = model.Timestamp{Time: updatedAt.UTC()}
|
||||
a.S3Key = s3Key
|
||||
a.ErrorCode = errCode
|
||||
if a.Variants == nil {
|
||||
a.Variants = []model.Variant{}
|
||||
}
|
||||
return &a, nil
|
||||
}
|
||||
|
||||
// ValidateAttachmentsForChat — проверка, что все attachmentIDs существуют и
|
||||
// принадлежат именно этому чату. Вызывается перед сохранением сообщения.
|
||||
// Если хоть один ID не прошёл — ErrNotFound (чужой объект = как будто нет).
|
||||
func (s *Store) ValidateAttachmentsForChat(ctx context.Context, chatID string, ids []string) error {
|
||||
if len(ids) == 0 {
|
||||
return nil
|
||||
}
|
||||
rows, err := s.pool.Query(ctx,
|
||||
`SELECT id FROM attachments WHERE id = ANY($1) AND chat_id = $2`,
|
||||
ids, chatID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rows.Close()
|
||||
found := make(map[string]bool, len(ids))
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
return err
|
||||
}
|
||||
found[id] = true
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if len(found) != len(ids) {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ─────────────── Делегаты S3 (лаба 4) ───────────────
|
||||
|
||||
// PutObject — кладёт байты в приватный S3-бакет.
|
||||
// Ошибки маппятся хендлером в 503 через IsS3Unavailable.
|
||||
func (s *Store) PutObject(ctx context.Context, key string, data []byte, contentType string) error {
|
||||
if s.s3 == nil {
|
||||
return errors.New("s3 client not configured")
|
||||
}
|
||||
return s.s3.PutObject(ctx, key, data, contentType)
|
||||
}
|
||||
|
||||
// GetObject — скачивает байты из S3.
|
||||
func (s *Store) GetObject(ctx context.Context, key string) ([]byte, error) {
|
||||
if s.s3 == nil {
|
||||
return nil, errors.New("s3 client not configured")
|
||||
}
|
||||
return s.s3.GetObject(ctx, key)
|
||||
}
|
||||
|
||||
// DeleteObject — удаляет объект. Используется для компенсации, если INSERT
|
||||
// метаданных в БД упал после успешного S3 PUT.
|
||||
func (s *Store) DeleteObject(ctx context.Context, key string) error {
|
||||
if s.s3 == nil {
|
||||
return nil
|
||||
}
|
||||
return s.s3.DeleteObject(ctx, key)
|
||||
}
|
||||
|
||||
// IsUnavailable — «БД недоступна» (пул закрыт, таймаут, ошибка подключения).
|
||||
func IsUnavailable(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
var connErr *pgconn.ConnectError
|
||||
var netErr net.Error
|
||||
if errors.As(err, &connErr) {
|
||||
return true
|
||||
}
|
||||
if errors.As(err, &netErr) && netErr.Timeout() {
|
||||
return true
|
||||
}
|
||||
errStr := err.Error()
|
||||
return strings.Contains(errStr, "connection refused") ||
|
||||
strings.Contains(errStr, "connection reset") ||
|
||||
strings.Contains(errStr, "pool closed") ||
|
||||
strings.Contains(errStr, "acquire timeout") ||
|
||||
strings.Contains(errStr, "no connection")
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
// RunReconcile — сверка S3 ↔ PostgreSQL (уровень 4).
|
||||
//
|
||||
// Какую проблему закрывает: если процесс реплики упадёт между успешным PUT
|
||||
// объекта в S3 и COMMIT метаданных в PostgreSQL, в бакете останется
|
||||
// осиротевший объект без записи в БД. Немедленная компенсация в хендлере
|
||||
// закрывает только штатный путь (ошибка БД после PUT в том же запросе), но не
|
||||
// крах процесса. Reconcile периодически находит такие объекты и удаляет их,
|
||||
// соблюдая grace period.
|
||||
//
|
||||
// Grace period защищает от гонки: объект создан, метаданные ещё не закоммичены
|
||||
// другим процессом — reconcile не трогает объекты моложе порога.
|
||||
func RunReconcile(ctx context.Context, pool *pgxpool.Pool, s3 *S3Client, grace time.Duration, log *slog.Logger) error {
|
||||
if s3 == nil {
|
||||
return fmt.Errorf("reconcile: s3 client not configured")
|
||||
}
|
||||
|
||||
// 1. Известные ключи из БД (источник истины для метаданных).
|
||||
rows, err := pool.Query(ctx, `SELECT s3_key FROM attachments`)
|
||||
if err != nil {
|
||||
return fmt.Errorf("reconcile: query known keys: %w", err)
|
||||
}
|
||||
known := make(map[string]struct{})
|
||||
for rows.Next() {
|
||||
var k string
|
||||
if err := rows.Scan(&k); err != nil {
|
||||
rows.Close()
|
||||
return fmt.Errorf("reconcile: scan key: %w", err)
|
||||
}
|
||||
known[k] = struct{}{}
|
||||
}
|
||||
rows.Close()
|
||||
if err := rows.Err(); err != nil {
|
||||
return fmt.Errorf("reconcile: rows err: %w", err)
|
||||
}
|
||||
|
||||
// 2. Объекты в бакете.
|
||||
objs, err := s3.ListObjectsInfo(ctx, "")
|
||||
if err != nil {
|
||||
return fmt.Errorf("reconcile: list objects: %w", err)
|
||||
}
|
||||
|
||||
// 3. Осиротевшие объекты старше grace period — удалить.
|
||||
now := time.Now().UTC()
|
||||
var deleted, skippedYoung int
|
||||
for _, o := range objs {
|
||||
if _, ok := known[o.Key]; ok {
|
||||
continue // учтён в БД
|
||||
}
|
||||
age := now.Sub(o.LastModified.UTC())
|
||||
if age < grace {
|
||||
skippedYoung++
|
||||
log.Info("reconcile skip young orphan",
|
||||
"key", o.Key,
|
||||
"age_seconds", int(age.Seconds()),
|
||||
"grace_seconds", int(grace.Seconds()))
|
||||
continue
|
||||
}
|
||||
if err := s3.DeleteObject(ctx, o.Key); err != nil {
|
||||
log.Warn("reconcile delete failed", "key", o.Key, "error", err.Error())
|
||||
continue
|
||||
}
|
||||
deleted++
|
||||
log.Info("reconcile deleted orphan",
|
||||
"key", o.Key,
|
||||
"age_seconds", int(age.Seconds()))
|
||||
}
|
||||
|
||||
log.Info("reconcile complete",
|
||||
"objects_in_bucket", len(objs),
|
||||
"known_in_db", len(known),
|
||||
"orphans_deleted", deleted,
|
||||
"orphans_skipped_young", skippedYoung,
|
||||
"grace_seconds", int(grace.Seconds()))
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,184 @@
|
||||
// Package store, s3.go — клиент приватного S3-совместимого хранилища (лаба 4).
|
||||
//
|
||||
// Разделение данных:
|
||||
// - Источник истины для БАЙТОВ — это хранилище.
|
||||
// - Источник истины для МЕТАДАННЫХ — PostgreSQL (таблица attachments).
|
||||
//
|
||||
// Ключ объекта (s3_key) назначает сервер и НЕ раскрывает клиенту.
|
||||
// Бакет приватный: анонимного доступа нет, скачивание только через
|
||||
// авторизованный API с проверкой членства в чате. Приватный бакет сам по себе
|
||||
// НЕ заменяет эту проверку (см. handlers.go).
|
||||
package store
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
// s3Timeout — максимальное время на одну операцию с хранилищем.
|
||||
// При недоступном хранилище клиент быстро получает ошибку, и загрузка/скачивание
|
||||
// честно возвращают 503, а не висят до таймаута клиента.
|
||||
const s3Timeout = 30 * time.Second
|
||||
|
||||
// S3Client — обёртка над MinIO/S3 с фиксированным приватным бакетом.
|
||||
type S3Client struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
// NewS3Client создаёт клиент.
|
||||
// - endpoint: "host:port" без схемы, например "minio:9000";
|
||||
// - useSSL: false для локального стенда по HTTP.
|
||||
//
|
||||
// Уровень 4: "API использует отдельные S3 credentials с ограниченными правами".
|
||||
// Эти креды читаются из переменных окружения (см. main.go, compose.yaml) и не
|
||||
// коммитятся в репозиторий.
|
||||
func NewS3Client(endpoint, accessKey, secretKey, bucket string, useSSL bool, log *slog.Logger) (*S3Client, error) {
|
||||
c, err := minio.New(endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKey, secretKey, ""),
|
||||
Secure: useSSL,
|
||||
BucketLookup: minio.BucketLookupPath,
|
||||
Region: "us-east-1",
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("init s3 client: %w", err)
|
||||
}
|
||||
return &S3Client{client: c, bucket: bucket, log: log}, nil
|
||||
}
|
||||
|
||||
// EnsureBucket — идемпотентный бутстрап приватного бакета.
|
||||
// Вызывается при старте; повторный вызов безопасен (существующий бакет не
|
||||
// пересоздаётся). Уровень 4: "Bucket bootstrap идемпотентен".
|
||||
func (s *S3Client) EnsureBucket(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s3Timeout)
|
||||
defer cancel()
|
||||
ok, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check bucket: %w", err)
|
||||
}
|
||||
if ok {
|
||||
s.log.Info("s3 bucket already exists", "bucket", s.bucket)
|
||||
return nil
|
||||
}
|
||||
if err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{}); err != nil {
|
||||
return fmt.Errorf("create bucket: %w", err)
|
||||
}
|
||||
s.log.Info("s3 bucket created", "bucket", s.bucket)
|
||||
return nil
|
||||
}
|
||||
|
||||
// PutObject — положить байты в бакет. Используется в загрузке вложения.
|
||||
// Ошибка маппится хендлером в 503 (хранилище недоступно).
|
||||
func (s *S3Client) PutObject(ctx context.Context, key string, data []byte, contentType string) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s3Timeout)
|
||||
defer cancel()
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("s3 put object: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetObject — скачать байты. Используется в скачивании вложения.
|
||||
// Сначала проверяем существование (чтобы отличить "объект не найден" от сетевой
|
||||
// ошибки), затем читаем. Ошибка маппится хендлером в 503 или 404.
|
||||
func (s *S3Client) GetObject(ctx context.Context, key string) ([]byte, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s3Timeout)
|
||||
defer cancel()
|
||||
if _, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{}); err != nil {
|
||||
return nil, fmt.Errorf("s3 stat object: %w", err)
|
||||
}
|
||||
obj, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 get object: %w", err)
|
||||
}
|
||||
defer obj.Close()
|
||||
data, err := io.ReadAll(obj)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("s3 read object: %w", err)
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// DeleteObject — удалить объект. Используется для компенсации, если запись
|
||||
// метаданных в БД не удалась после успешного S3 PUT (механизм согласованности
|
||||
// уровня 4), а также в reconciler для очистки осиротевших объектов.
|
||||
func (s *S3Client) DeleteObject(ctx context.Context, key string) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, s3Timeout)
|
||||
defer cancel()
|
||||
if err := s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}); err != nil {
|
||||
return fmt.Errorf("s3 delete object: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListKeys — перечислить все ключи объектов с префиксом ("" = все).
|
||||
// Используется в reconciler (уровень 4) для поиска осиротевших объектов:
|
||||
// объектов в хранилище, у которых нет метаданных в БД.
|
||||
// Результат ограничен по времени (см. context.WithTimeout), но НЕ по количеству —
|
||||
// для учебной лабы этого достаточно; в отчёте зафиксируешь ограничение.
|
||||
func (s *S3Client) ListKeys(ctx context.Context, prefix string) ([]string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s3Timeout)
|
||||
defer cancel()
|
||||
var keys []string
|
||||
for obj := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
|
||||
if obj.Err != nil {
|
||||
return nil, fmt.Errorf("s3 list objects: %w", obj.Err)
|
||||
}
|
||||
keys = append(keys, obj.Key)
|
||||
}
|
||||
return keys, nil
|
||||
}
|
||||
|
||||
// IsS3Unavailable — эвристика "хранилище недоступно" для маппинга в 503.
|
||||
// Аналог IsUnavailable для PostgreSQL: сетевые ошибки и таймауты считаем
|
||||
// недоступностью, а не ошибкой клиента.
|
||||
func IsS3Unavailable(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return true
|
||||
}
|
||||
msg := err.Error()
|
||||
return strings.Contains(msg, "connection refused") ||
|
||||
strings.Contains(msg, "connection reset") ||
|
||||
strings.Contains(msg, "no such host") ||
|
||||
strings.Contains(msg, "timeout") ||
|
||||
strings.Contains(msg, "i/o timeout") ||
|
||||
strings.Contains(msg, "dial tcp")
|
||||
}
|
||||
|
||||
// ObjectInfo — ключ и время изменения объекта. Нужно reconcile, чтобы
|
||||
// отличать «осиротевший давно» от «создан секунду назад, метаданные дописываются».
|
||||
type ObjectInfo struct {
|
||||
Key string
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
// ListObjectsInfo — перечислить объекты с ключом и временем изменения.
|
||||
// Используется RunReconcile (уровень 4) для поиска осиротевших объектов.
|
||||
func (s *S3Client) ListObjectsInfo(ctx context.Context, prefix string) ([]ObjectInfo, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, s3Timeout)
|
||||
defer cancel()
|
||||
var out []ObjectInfo
|
||||
for obj := range s.client.ListObjects(ctx, s.bucket, minio.ListObjectsOptions{Prefix: prefix, Recursive: true}) {
|
||||
if obj.Err != nil {
|
||||
return nil, fmt.Errorf("s3 list objects: %w", obj.Err)
|
||||
}
|
||||
out = append(out, ObjectInfo{Key: obj.Key, LastModified: obj.LastModified})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Track 1 bench: матрица нагрузка x размер истории для in-memory store.
|
||||
Только stdlib. Пример:
|
||||
python student-tests/bench.py --label before --container mtusi-messenger-lab1
|
||||
"""
|
||||
import argparse
|
||||
import csv
|
||||
import http.client
|
||||
import json
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
|
||||
|
||||
class Client:
|
||||
def __init__(self, host, port, user_id=None):
|
||||
self.host, self.port, self.user_id = host, port, user_id
|
||||
self.conn = None
|
||||
|
||||
def _ensure(self):
|
||||
if self.conn is None:
|
||||
self.conn = http.client.HTTPConnection(self.host, self.port, timeout=30)
|
||||
|
||||
def request(self, method, path, body=None):
|
||||
headers = {}
|
||||
data = None
|
||||
if body is not None:
|
||||
headers["Content-Type"] = "application/json"
|
||||
data = json.dumps(body).encode()
|
||||
if self.user_id:
|
||||
headers["X-User-Id"] = self.user_id
|
||||
for attempt in (0, 1):
|
||||
try:
|
||||
self._ensure()
|
||||
self.conn.request(method, path, body=data, headers=headers)
|
||||
resp = self.conn.getresponse()
|
||||
payload = resp.read()
|
||||
return resp.status, (json.loads(payload) if payload else None)
|
||||
except Exception:
|
||||
self.conn = None
|
||||
if attempt:
|
||||
raise
|
||||
time.sleep(0.05)
|
||||
|
||||
|
||||
class Phase:
|
||||
def __init__(self):
|
||||
self.lock = threading.Lock()
|
||||
self.lat = []
|
||||
self.errors = 0
|
||||
self.requests = 0
|
||||
|
||||
def add(self, ms, ok):
|
||||
with self.lock:
|
||||
self.lat.append(ms)
|
||||
self.requests += 1
|
||||
if not ok:
|
||||
self.errors += 1
|
||||
|
||||
def row(self):
|
||||
with self.lock:
|
||||
lat = sorted(self.lat)
|
||||
n = len(lat)
|
||||
if n == 0:
|
||||
return 0, 0, 0, 0
|
||||
def p(q):
|
||||
return lat[min(n - 1, int((q * n) // 100))]
|
||||
return n, p(50), p(95), p(99)
|
||||
|
||||
|
||||
class RssSampler:
|
||||
def __init__(self, container, interval=2.0):
|
||||
self.container, self.interval = container, interval
|
||||
self.samples = []
|
||||
self.stop = threading.Event()
|
||||
self.th = threading.Thread(target=self._run, daemon=True)
|
||||
|
||||
def _run(self):
|
||||
while not self.stop.is_set():
|
||||
try:
|
||||
out = subprocess.run(
|
||||
["docker", "stats", "--no-stream", "--format", "{{.MemUsage}}", self.container],
|
||||
capture_output=True, text=True, timeout=10).stdout.strip().splitlines()
|
||||
if out:
|
||||
self.samples.append(parse_mib(out[0].split("/")[0].strip()))
|
||||
except Exception:
|
||||
pass
|
||||
self.stop.wait(self.interval)
|
||||
|
||||
def start(self):
|
||||
self.th.start()
|
||||
return self
|
||||
|
||||
def finish(self):
|
||||
self.stop.set()
|
||||
self.th.join(timeout=5)
|
||||
if not self.samples:
|
||||
return 0.0, 0.0
|
||||
return sum(self.samples) / len(self.samples), max(self.samples)
|
||||
|
||||
|
||||
def parse_mib(s):
|
||||
s = s.strip()
|
||||
if s.endswith("GiB"):
|
||||
return float(s[:-3]) * 1024
|
||||
if s.endswith("MiB"):
|
||||
return float(s[:-3])
|
||||
if s.endswith("KiB"):
|
||||
return float(s[:-4]) / 1024
|
||||
return 0.0
|
||||
|
||||
|
||||
def run_phase(host, port, clients, duration, op):
|
||||
ph = Phase()
|
||||
deadline = time.perf_counter() + duration
|
||||
|
||||
def worker(idx):
|
||||
c = Client(host, port, op_user(idx))
|
||||
while time.perf_counter() < deadline:
|
||||
t0 = time.perf_counter()
|
||||
ok = op(c, idx)
|
||||
ph.add((time.perf_counter() - t0) * 1000.0, ok)
|
||||
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(clients)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
return ph
|
||||
|
||||
|
||||
USERS = []
|
||||
|
||||
|
||||
def op_user(idx):
|
||||
return USERS[idx % len(USERS)]
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--base-url", default="http://localhost:8080")
|
||||
ap.add_argument("--label", required=True)
|
||||
ap.add_argument("--container", default="mtusi-messenger-lab1")
|
||||
ap.add_argument("--sizes", default="10000,50000,150000")
|
||||
ap.add_argument("--clients", default="8,32,96")
|
||||
ap.add_argument("--duration", type=float, default=10.0)
|
||||
ap.add_argument("--out", default="student-tests/results")
|
||||
args = ap.parse_args()
|
||||
|
||||
hostport = args.base_url.split("://")[-1].split("/")
|
||||
host, _, port = hostport[0].partition(":")
|
||||
port = int(port or 80)
|
||||
sizes = [int(x) for x in args.sizes.split(",")]
|
||||
clients = [int(x) for x in args.clients.split(",")]
|
||||
os_makedirs(args.out)
|
||||
|
||||
admin = Client(host, port)
|
||||
global USERS
|
||||
n_users = max(clients)
|
||||
for i in range(n_users):
|
||||
st, body = admin.request("POST", "/api/v1/users",
|
||||
{"username": "b" + uuid.uuid4().hex[:12], "display_name": "Bench"})
|
||||
assert st == 201, (st, body)
|
||||
USERS.append(body["id"])
|
||||
|
||||
admin.user_id = USERS[0] # <-- ДОБАВИТЬ ЭТУ СТРОКУ: чаты создаёт участник
|
||||
|
||||
st, chat = admin.request("POST", "/api/v1/chats",
|
||||
{"title": "bench", "member_ids": USERS})
|
||||
assert st == 201, (st, chat)
|
||||
chat_id = chat["id"]
|
||||
st, chat2 = admin.request("POST", "/api/v1/chats",
|
||||
{"title": "bench-writes", "member_ids": USERS})
|
||||
assert st == 201
|
||||
chat2_id = chat2["id"]
|
||||
|
||||
rows = []
|
||||
have = 0
|
||||
for H in sizes:
|
||||
# досеиваем историю до H (кумулятивно, экономим время)
|
||||
if H > have:
|
||||
seed(host, port, chat_id, H - have)
|
||||
have = H
|
||||
for C in clients:
|
||||
print(f"[{args.label}] read H={H} C={C} ...", flush=True)
|
||||
rss = RssSampler(args.container).start()
|
||||
ph = run_phase(host, port, C, args.duration, make_read_op(chat_id))
|
||||
avg, mx = rss.finish()
|
||||
n, p50, p95, p99 = ph.row()
|
||||
rows.append([args.label, "read", H, C, args.duration, n, ph.errors,
|
||||
round(n / args.duration, 1), p50, p95, p99, round(avg, 1), round(mx, 1)])
|
||||
for C in clients:
|
||||
print(f"[{args.label}] write C={C} ...", flush=True)
|
||||
rss = RssSampler(args.container).start()
|
||||
ph = run_phase(host, port, C, args.duration, make_write_op(chat2_id))
|
||||
avg, mx = rss.finish()
|
||||
n, p50, p95, p99 = ph.row()
|
||||
rows.append([args.label, "write", 0, C, args.duration, n, ph.errors,
|
||||
round(n / args.duration, 1), p50, p95, p99, round(avg, 1), round(mx, 1)])
|
||||
|
||||
path = f"{args.out}/{args.label}.csv"
|
||||
with open(path, "w", newline="", encoding="utf-8") as f:
|
||||
w = csv.writer(f)
|
||||
w.writerow(["label", "phase", "history", "clients", "duration_s", "requests",
|
||||
"errors", "rps", "p50_ms", "p95_ms", "p99_ms", "rss_avg_mib", "rss_max_mib"])
|
||||
w.writerows(rows)
|
||||
print(f"saved {path}")
|
||||
|
||||
|
||||
def os_makedirs(p):
|
||||
import os
|
||||
os.makedirs(p, exist_ok=True)
|
||||
|
||||
|
||||
def seed(host, port, chat_id, count):
|
||||
threads_n = 16
|
||||
per = count // threads_n
|
||||
lock = threading.Lock()
|
||||
state = {"done": 0, "errors": 0}
|
||||
t0 = time.perf_counter()
|
||||
|
||||
def worker(idx):
|
||||
c = Client(host, port, USERS[0])
|
||||
for j in range(per):
|
||||
st, _ = c.request("POST", f"/api/v1/chats/{chat_id}/messages",
|
||||
{"text": f"seed-{idx}-{j}"})
|
||||
with lock:
|
||||
state["done"] += 1
|
||||
if st != 201:
|
||||
state["errors"] += 1
|
||||
|
||||
threads = [threading.Thread(target=worker, args=(i,)) for i in range(threads_n)]
|
||||
for t in threads:
|
||||
t.start()
|
||||
for t in threads:
|
||||
t.join()
|
||||
dt = time.perf_counter() - t0
|
||||
print(f" seeded {state['done']} msgs in {dt:.1f}s "
|
||||
f"({state['done']/dt:.0f} rps, errors={state['errors']})", flush=True)
|
||||
|
||||
|
||||
def make_read_op(chat_id):
|
||||
cursors = {}
|
||||
|
||||
def op(c, idx):
|
||||
cur = cursors.get(idx)
|
||||
path = f"/api/v1/chats/{chat_id}/messages?limit=50"
|
||||
if cur:
|
||||
path += "&cursor=" + cur
|
||||
st, body = c.request("GET", path)
|
||||
if st != 200:
|
||||
cursors[idx] = None
|
||||
return False
|
||||
cursors[idx] = body.get("next_cursor")
|
||||
return True
|
||||
|
||||
return op
|
||||
|
||||
|
||||
def make_write_op(chat_id):
|
||||
counter = {"i": 0}
|
||||
lock = threading.Lock()
|
||||
|
||||
def op(c, idx):
|
||||
with lock:
|
||||
counter["i"] += 1
|
||||
i = counter["i"]
|
||||
st, _ = c.request("POST", f"/api/v1/chats/{chat_id}/messages", {"text": f"w{i}"})
|
||||
return st == 201
|
||||
|
||||
return op
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,229 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Трек 5: согласованность кэша истории. Фазы (переключаются bat-ом через CACHE_MODE):
|
||||
off — baseline: кэша нет. 32 конкурентных холодных чтения = ~32 SQL
|
||||
(демонстрация cache stampede «до»), 100 последовательных чтений = 100 SQL,
|
||||
latency p50/p95/p99 «до».
|
||||
naive — кэш БЕЗ инвалидации версией: ожидаем НАБЛЮДАТЬ stale read
|
||||
(запись не видна из тёплого кэша). Это демонстрация проблемы согласованности.
|
||||
versioned — защищённый кэш: read-after-write OK; 32 конкурентных холодных чтения
|
||||
схлопываются single-flight'ом в 1 SQL (защита от stampede «после»);
|
||||
100 тёплых чтений обслуживает кэш (sql<=2), latency «после».
|
||||
recovery — restart redis: сессия и read-after-write корректны, кэш перестраивается.
|
||||
Запуск: scripts/cache_demo.bat (сам переключает CACHE_MODE и перезапускает api).
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import subprocess
|
||||
import threading
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
FAILURES = []
|
||||
BASE = "http://localhost:8080"
|
||||
PASSWORD = "Example-only-not-a-real-secret-123"
|
||||
READS = 100 # последовательных чтений для SQL-счётчика и latency
|
||||
STAMPEDE = 32 # конкурентных холодных чтений
|
||||
|
||||
|
||||
def check(name, cond, detail=""):
|
||||
if not cond:
|
||||
FAILURES.append(name)
|
||||
print(f"[{'PASS' if cond else 'FAIL'}] {name}" + (f" -- {detail}" if not cond and detail else ""))
|
||||
|
||||
|
||||
def sh(cmd):
|
||||
subprocess.run(cmd, shell=True, check=True, capture_output=True)
|
||||
|
||||
|
||||
def req(method, path, body=None, token=None, basic=None):
|
||||
headers, data = {}, None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||||
headers["Content-Type"] = "application/json"
|
||||
if token:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
if basic:
|
||||
headers["Authorization"] = "Basic " + base64.b64encode(basic.encode()).decode()
|
||||
r = urllib.request.Request(BASE + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(r, timeout=30) as resp:
|
||||
payload = resp.read().decode()
|
||||
return resp.status, (json.loads(payload) if payload else None)
|
||||
except urllib.error.HTTPError as e:
|
||||
payload = e.read().decode()
|
||||
try:
|
||||
return e.code, json.loads(payload)
|
||||
except ValueError:
|
||||
return e.code, None
|
||||
|
||||
|
||||
def wait_ready(timeout=90, want=200):
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
st, _ = req("GET", "/health/ready")
|
||||
if st == want:
|
||||
return True
|
||||
time.sleep(1)
|
||||
return False
|
||||
|
||||
|
||||
def register_login():
|
||||
uname = "t5" + uuid.uuid4().hex[:10]
|
||||
st, _ = req("POST", "/api/v1/users", {"username": uname, "display_name": "T5", "password": PASSWORD})
|
||||
assert st == 201, st
|
||||
st, sess = req("POST", "/api/v1/auth/sessions", basic=f"{uname}:{PASSWORD}")
|
||||
assert st == 201, st
|
||||
return sess["token"], sess["user"]["id"]
|
||||
|
||||
|
||||
def setup_chat(token, uid, n):
|
||||
st, chat = req("POST", "/api/v1/chats",
|
||||
{"title": "track5-" + uuid.uuid4().hex[:8], "member_ids": [uid]}, token=token)
|
||||
assert st == 201, st
|
||||
for i in range(n):
|
||||
st, _ = req("POST", f"/api/v1/chats/{chat['id']}/messages",
|
||||
{"text": f"t5-{i:03d}"}, token=token)
|
||||
assert st == 201, st
|
||||
return chat
|
||||
|
||||
|
||||
def read_page(token, chat_id):
|
||||
st, body = req("GET", f"/api/v1/chats/{chat_id}/messages?limit=50", token=token)
|
||||
assert st == 200, (st, body)
|
||||
return body
|
||||
|
||||
|
||||
def sql_count(since_unix, chat_id):
|
||||
out = subprocess.run(f"docker compose logs api --since {since_unix}",
|
||||
shell=True, capture_output=True, text=True).stdout
|
||||
return sum(1 for line in out.splitlines()
|
||||
if "sql_list_messages" in line and chat_id in line)
|
||||
|
||||
|
||||
def concurrent_burst(token, chat_id):
|
||||
"""32 конкурентных холодных чтения через barrier; возвращает число SQL-запросов."""
|
||||
since = str(int(time.time()))
|
||||
barrier = threading.Barrier(STAMPEDE)
|
||||
|
||||
def reader():
|
||||
barrier.wait()
|
||||
read_page(token, chat_id)
|
||||
|
||||
ts = [threading.Thread(target=reader) for _ in range(STAMPEDE)]
|
||||
for t in ts:
|
||||
t.start()
|
||||
for t in ts:
|
||||
t.join()
|
||||
return sql_count(since, chat_id)
|
||||
|
||||
|
||||
def pctl(xs):
|
||||
xs = sorted(xs)
|
||||
n = len(xs)
|
||||
|
||||
def p(q):
|
||||
return xs[min(n - 1, int(q * n // 100))]
|
||||
|
||||
return round(p(50), 2), round(p(95), 2), round(p(99), 2)
|
||||
|
||||
|
||||
def phase_off():
|
||||
token, uid = register_login()
|
||||
chat = setup_chat(token, uid, 5)
|
||||
# Stampede БЕЗ кэша: каждый из 32 конкурентных читателей идёт в SQL.
|
||||
n_burst = concurrent_burst(token, chat["id"])
|
||||
check("off: concurrent cold reads -> each hits SQL (stampede without cache)",
|
||||
n_burst >= STAMPEDE // 2, f"sql={n_burst} clients={STAMPEDE}")
|
||||
# Baseline: каждое последовательное чтение = SQL.
|
||||
since = str(int(time.time()))
|
||||
lats = []
|
||||
for _ in range(READS):
|
||||
t0 = time.perf_counter()
|
||||
read_page(token, chat["id"])
|
||||
lats.append((time.perf_counter() - t0) * 1000)
|
||||
n = sql_count(since, chat["id"])
|
||||
check("off: every read hits SQL", n >= READS * 0.9, f"sql={n} reads={READS}")
|
||||
p50, p95, p99 = pctl(lats)
|
||||
print(f"RESULT mode=off reads={READS} sql={n} stampede_sql={n_burst} p50={p50} p95={p95} p99={p99}")
|
||||
|
||||
|
||||
def phase_naive():
|
||||
token, uid = register_login()
|
||||
# Stale read: тёплый кэш + запись + повторное чтение.
|
||||
chat = setup_chat(token, uid, 2)
|
||||
read_page(token, chat["id"]) # прогрев кэша
|
||||
req("POST", f"/api/v1/chats/{chat['id']}/messages", {"text": "stale-probe"}, token=token)
|
||||
page = read_page(token, chat["id"])
|
||||
visible = any(m["text"] == "stale-probe" for m in page["items"])
|
||||
check("naive: stale read OBSERVED (cache returned old page after write)", not visible,
|
||||
f"visible={visible}")
|
||||
print(f"RESULT mode=naive stale_observed={not visible}")
|
||||
|
||||
|
||||
def phase_versioned():
|
||||
token, uid = register_login()
|
||||
# Read-after-write: запись видна сразу даже при тёплом кэше.
|
||||
chat = setup_chat(token, uid, 2)
|
||||
read_page(token, chat["id"])
|
||||
req("POST", f"/api/v1/chats/{chat['id']}/messages", {"text": "stale-probe"}, token=token)
|
||||
page = read_page(token, chat["id"])
|
||||
visible = any(m["text"] == "stale-probe" for m in page["items"])
|
||||
check("versioned: read-after-write OK (no stale)", visible)
|
||||
# Stampede ПОД защитой: single-flight схлопывает 32 холодных чтения в 1 SQL.
|
||||
chat2 = setup_chat(token, uid, 5)
|
||||
n_burst = concurrent_burst(token, chat2["id"])
|
||||
check("versioned: single-flight -> exactly 1 SQL on cold key", n_burst == 1,
|
||||
f"sql={n_burst} clients={STAMPEDE}")
|
||||
# Тёплые чтения обслуживает кэш.
|
||||
since = str(int(time.time()))
|
||||
lats = []
|
||||
for _ in range(READS):
|
||||
t0 = time.perf_counter()
|
||||
read_page(token, chat["id"])
|
||||
lats.append((time.perf_counter() - t0) * 1000)
|
||||
n = sql_count(since, chat["id"])
|
||||
check("versioned: warm reads served from cache", n <= 2, f"sql={n} reads={READS}")
|
||||
p50, p95, p99 = pctl(lats)
|
||||
print(f"RESULT mode=versioned reads={READS} sql={n} stampede_sql={n_burst} p50={p50} p95={p95} p99={p99}")
|
||||
|
||||
|
||||
def phase_recovery():
|
||||
token, uid = register_login()
|
||||
chat = setup_chat(token, uid, 3)
|
||||
read_page(token, chat["id"])
|
||||
sh("docker compose restart redis")
|
||||
check("recovery: api ready after redis restart", wait_ready(90, 200))
|
||||
st, _ = req("GET", "/api/v1/users/me", token=token)
|
||||
check("recovery: session still valid (source of truth = postgres)", st == 200, str(st))
|
||||
since = str(int(time.time()))
|
||||
page = read_page(token, chat["id"])
|
||||
check("recovery: history readable after cache loss", len(page["items"]) == 3)
|
||||
req("POST", f"/api/v1/chats/{chat['id']}/messages", {"text": "after-restart"}, token=token)
|
||||
page = read_page(token, chat["id"])
|
||||
check("recovery: read-after-write OK with rebuilt cache",
|
||||
any(m["text"] == "after-restart" for m in page["items"]))
|
||||
n = sql_count(since, chat["id"])
|
||||
check("recovery: cache rebuilds (1 sql for 2 reads)", n == 1, f"sql={n}")
|
||||
print(f"RESULT mode=recovery sql_after_restart={n}")
|
||||
|
||||
|
||||
def main():
|
||||
global BASE
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--base-url", default=BASE)
|
||||
ap.add_argument("--phase", required=True, choices=["off", "naive", "versioned", "recovery"])
|
||||
a = ap.parse_args()
|
||||
BASE = a.base_url
|
||||
{"off": phase_off, "naive": phase_naive,
|
||||
"versioned": phase_versioned, "recovery": phase_recovery}[a.phase]()
|
||||
if FAILURES:
|
||||
print(f"PHASE {a.phase} FAILED: {FAILURES}")
|
||||
raise SystemExit(1)
|
||||
print(f"PHASE {a.phase} DONE")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,50 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Сравнение двух label'ов: python student-tests/compare.py --before before --after after"""
|
||||
import argparse
|
||||
import csv
|
||||
|
||||
|
||||
def load(path):
|
||||
d = {}
|
||||
with open(path, newline="", encoding="utf-8") as f:
|
||||
for r in csv.DictReader(f):
|
||||
d[(r["phase"], r["history"], r["clients"])] = r
|
||||
return d
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--before", required=True)
|
||||
ap.add_argument("--after", required=True)
|
||||
ap.add_argument("--results", default="student-tests/results")
|
||||
ap.add_argument("--out", default="student-tests/results/summary.md")
|
||||
args = ap.parse_args()
|
||||
|
||||
b = load(f"{args.results}/{args.before}.csv")
|
||||
a = load(f"{args.results}/{args.after}.csv")
|
||||
lines = [f"# Трек 1: сравнение `{args.before}` → `{args.after}`", ""]
|
||||
|
||||
for phase in ("read", "write"):
|
||||
keys = sorted(k for k in b if k[0] == phase and k in a)
|
||||
if not keys:
|
||||
continue
|
||||
lines.append(f"## Фаза `{phase}`")
|
||||
lines.append("")
|
||||
lines.append("| H | C | rps до | rps после | Δ rps | p95 до | p95 после | p99 до | p99 после | RSS max до | RSS max после |")
|
||||
lines.append("|---|---|---|---|---|---|---|---|---|---|---|")
|
||||
for k in keys:
|
||||
rb, ra = b[k], a[k]
|
||||
dr = (float(ra["rps"]) - float(rb["rps"])) / float(rb["rps"]) * 100 if float(rb["rps"]) else 0
|
||||
lines.append(f"| {k[1]} | {k[2]} | {rb['rps']} | {ra['rps']} | {dr:+.0f}% | "
|
||||
f"{rb['p95_ms']} | {ra['p95_ms']} | {rb['p99_ms']} | {ra['p99_ms']} | "
|
||||
f"{rb['rss_max_mib']} | {ra['rss_max_mib']} |")
|
||||
lines.append("")
|
||||
|
||||
with open(args.out, "w", encoding="utf-8") as f:
|
||||
f.write("\n".join(lines) + "\n")
|
||||
print("\n".join(lines))
|
||||
print(f"\nsaved {args.out}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,117 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Проверка expiry при коротком SESSION_TTL_SECONDS (стенд поднят скриптом).
|
||||
|
||||
Устойчивость к медленному старту после перезапуска api:
|
||||
- заголовки ищем в lower-case (Go шлёт "Www-Authenticate", а не "WWW-Authenticate");
|
||||
- логин повторяется, пока на сессии не останется достаточный запас времени,
|
||||
иначе «свежий» запрос ловил 401 из-за гонки времени при очень коротком TTL.
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
from datetime import datetime, timezone
|
||||
|
||||
BASE = "http://localhost:8080"
|
||||
PASSWORD = "Example-only-not-a-real-secret-123"
|
||||
FAILURES = []
|
||||
|
||||
|
||||
def check(name, cond, detail=""):
|
||||
if not cond:
|
||||
FAILURES.append(name)
|
||||
print(f"[{'PASS' if cond else 'FAIL'}] {name}" + (f" -- {detail}" if not cond and detail else ""))
|
||||
|
||||
|
||||
def _lower_headers(headers):
|
||||
"""Ключи заголовков в нижнем регистре: регистр присылки не важен."""
|
||||
return {k.lower(): v for k, v in headers.items()}
|
||||
|
||||
|
||||
def www_auth(hdr):
|
||||
return hdr.get("www-authenticate", "")
|
||||
|
||||
|
||||
def req(method, path, body=None, token=None, basic=None):
|
||||
headers, data = {}, None
|
||||
if body is not None:
|
||||
data = json.dumps(body).encode()
|
||||
headers["Content-Type"] = "application/json"
|
||||
if token:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
if basic:
|
||||
headers["Authorization"] = "Basic " + base64.b64encode(basic.encode()).decode()
|
||||
r = urllib.request.Request(BASE + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(r, timeout=10) as resp:
|
||||
p = resp.read().decode()
|
||||
return resp.status, _lower_headers(resp.headers), (json.loads(p) if p else None)
|
||||
except urllib.error.HTTPError as e:
|
||||
p = e.read().decode()
|
||||
try:
|
||||
return e.code, _lower_headers(e.headers), json.loads(p)
|
||||
except ValueError:
|
||||
return e.code, _lower_headers(e.headers), None
|
||||
|
||||
|
||||
def seconds_left(sess):
|
||||
"""Сколько секунд осталось до истечения — по expires_at из ответа сервера."""
|
||||
exp = datetime.fromisoformat(sess["expires_at"].replace("Z", "+00:00"))
|
||||
return (exp - datetime.now(timezone.utc)).total_seconds()
|
||||
|
||||
|
||||
def login_fresh(uname, min_left=1.5, attempts=5):
|
||||
"""Логин с повторами, пока на сессии не останется >= min_left секунд.
|
||||
Защита от медленных первых запросов сразу после перезапуска контейнера."""
|
||||
sess = None
|
||||
for i in range(attempts):
|
||||
st, _, sess = req("POST", "/api/v1/auth/sessions", basic=f"{uname}:{PASSWORD}")
|
||||
assert st == 201, f"login failed: {st} {sess}"
|
||||
if seconds_left(sess) >= min_left:
|
||||
return sess
|
||||
time.sleep(0.3)
|
||||
return sess # последний вариант, даже если запас мал
|
||||
|
||||
|
||||
def main():
|
||||
global BASE
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--base-url", default=BASE)
|
||||
BASE = ap.parse_args().base_url
|
||||
|
||||
uname = "exp" + uuid.uuid4().hex[:10]
|
||||
st, _, body = req("POST", "/api/v1/users",
|
||||
{"username": uname, "display_name": "EX", "password": PASSWORD})
|
||||
assert st == 201, f"register failed: {st} {body}"
|
||||
|
||||
sess = login_fresh(uname)
|
||||
check("login 201 with short TTL", True)
|
||||
token = sess["token"]
|
||||
|
||||
# Свежий токен обязан работать сразу после логина.
|
||||
st, _, _ = req("GET", "/api/v1/users/me", token=token)
|
||||
check("fresh token 200", st == 200, str(st))
|
||||
|
||||
# Ждём гарантированного истечения: реальный остаток + 1 с запаса.
|
||||
wait = seconds_left(sess) + 1.0
|
||||
time.sleep(max(wait, 0.5))
|
||||
|
||||
st, hdr, _ = req("GET", "/api/v1/users/me", token=token)
|
||||
check("expired token -> 401", st == 401, str(st))
|
||||
check("expired WWW-Authenticate Bearer", www_auth(hdr).startswith("Bearer"),
|
||||
f"got {www_auth(hdr)!r}")
|
||||
|
||||
st, _, _ = req("POST", "/api/v1/auth/sessions", basic=f"{uname}:{PASSWORD}")
|
||||
check("re-login after expiry 201", st == 201)
|
||||
|
||||
if FAILURES:
|
||||
print(f"FAILED {len(FAILURES)}: {FAILURES}")
|
||||
raise SystemExit(1)
|
||||
print("EXPIRY CHECKS PASSED")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,10 @@
|
||||
-- План запроса keyset-пагинации истории: первая страница (курсор = начало времён).
|
||||
-- Ожидаем WITH index: Limit -> Index Scan using messages_history_idx
|
||||
-- Ожидаем WITHOUT index: Limit -> Sort -> Seq Scan on messages
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'chat'
|
||||
AND (created_at, id) > ('2020-01-01T00:00:00+00', '00000000-0000-0000-0000-000000000000')
|
||||
ORDER BY created_at, id
|
||||
LIMIT 51;
|
||||
@@ -0,0 +1,143 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Уровень 4: деградация при потере сервисов.
|
||||
Redis down -> API обслуживает запросы через SQL (200), логи warn.
|
||||
Postgres down -> /health/ready 503 и защищённые запросы 503 (не 200, не «аноним доверен»).
|
||||
Запуск из корня репозитория:
|
||||
python student-tests/failover_tests.py --base-url http://localhost:8080
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import subprocess
|
||||
import time
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
FAILURES = []
|
||||
BASE = "http://localhost:8080"
|
||||
PASSWORD = "Example-only-not-a-real-secret-123"
|
||||
|
||||
|
||||
def check(name, cond, detail=""):
|
||||
if not cond:
|
||||
FAILURES.append(name)
|
||||
print(f"[{'PASS' if cond else 'FAIL'}] {name}" + (f" -- {detail}" if not cond and detail else ""))
|
||||
|
||||
|
||||
def sh(cmd):
|
||||
subprocess.run(cmd, shell=True, check=True, capture_output=True)
|
||||
|
||||
|
||||
def req(method, path, body=None, token=None, basic=None):
|
||||
headers, data = {}, None
|
||||
if body is not None:
|
||||
data = json.dumps(body, ensure_ascii=False).encode("utf-8")
|
||||
headers["Content-Type"] = "application/json"
|
||||
if token:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
if basic:
|
||||
headers["Authorization"] = "Basic " + base64.b64encode(basic.encode()).decode()
|
||||
r = urllib.request.Request(BASE + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(r, timeout=10) as resp:
|
||||
payload = resp.read().decode()
|
||||
return resp.status, (json.loads(payload) if payload else None)
|
||||
except urllib.error.HTTPError as e:
|
||||
payload = e.read().decode()
|
||||
try:
|
||||
return e.code, json.loads(payload)
|
||||
except ValueError:
|
||||
return e.code, None
|
||||
|
||||
|
||||
def wait_ready(timeout=60, want=200):
|
||||
deadline = time.time() + timeout
|
||||
while time.time() < deadline:
|
||||
st, _ = req("GET", "/health/ready")
|
||||
if st == want:
|
||||
return True
|
||||
time.sleep(1)
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
global BASE
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--base-url", default=BASE)
|
||||
BASE = ap.parse_args().base_url
|
||||
|
||||
# Создаём пользователя
|
||||
uname = "fo" + uuid.uuid4().hex[:10] # без дефиса
|
||||
st, body = req("POST", "/api/v1/users", {"username": uname, "display_name": "FO", "password": PASSWORD})
|
||||
print(f"DEBUG register: {st} {body}")
|
||||
assert st == 201, f"register failed: {st} {body}"
|
||||
|
||||
st, sess = req("POST", "/api/v1/auth/sessions", basic=f"{uname}:{PASSWORD}")
|
||||
print(f"DEBUG login: {st} {sess}")
|
||||
assert st == 201, f"login failed: {st} {sess}"
|
||||
token = sess["token"]
|
||||
|
||||
# Узнаём свой id через /me
|
||||
me_st, me = req("GET", "/api/v1/users/me", token=token)
|
||||
print(f"DEBUG /me: {me_st} {me}")
|
||||
assert me_st == 200, f"/me failed: {me_st} {me}"
|
||||
|
||||
# Создаём чат с самим собой
|
||||
st, chat = req("POST", "/api/v1/chats", {"title": "failover", "member_ids": [me["id"]]}, token=token)
|
||||
print(f"DEBUG create chat: {st} {chat}")
|
||||
assert st == 201, f"create chat failed: {st} {chat}"
|
||||
|
||||
# ── Redis down: деградация в SQL, не отказ ──
|
||||
sh("docker compose stop redis")
|
||||
time.sleep(2)
|
||||
st, _ = req("GET", "/health/ready")
|
||||
check("redis down: ready stays 200 (cache optional)", st == 200, str(st))
|
||||
st, _ = req("GET", "/api/v1/users/me", token=token)
|
||||
check("redis down: auth via postgres 200", st == 200, str(st))
|
||||
st, _ = req("POST", f"/api/v1/chats/{chat['id']}/messages", {"text": "during redis outage"}, token=token)
|
||||
check("redis down: write 201", st == 201, str(st))
|
||||
st, _ = req("GET", f"/api/v1/chats/{chat['id']}/messages?limit=10", token=token)
|
||||
check("redis down: read 200", st == 200, str(st))
|
||||
sh("docker compose start redis")
|
||||
check("redis back: ready 200", wait_ready(60, 200))
|
||||
|
||||
# ── Postgres down: ограниченные ошибки, аноним не доверен ──
|
||||
sh("docker compose stop postgres")
|
||||
check("postgres down: ready becomes 503", wait_ready(60, 503))
|
||||
st, _ = req("GET", "/api/v1/users/me", token=token)
|
||||
check("postgres down: protected request 503 (not 200/401)", st == 503, str(st))
|
||||
st, _ = req("GET", "/api/v1/users/me")
|
||||
check("postgres down: anonymous still 401", st == 401, str(st))
|
||||
sh("docker compose start postgres")
|
||||
check("postgres back: ready 200", wait_ready(90, 200))
|
||||
st, _ = req("GET", "/api/v1/users/me", token=token)
|
||||
check("postgres back: same session valid 200", st == 200, str(st))
|
||||
|
||||
# ── Logout при сбое Redis: никаких «тихих» 204, оставляющих токен живым ──
|
||||
st, sess2 = req("POST", "/api/v1/auth/sessions", basic=f"{uname}:{PASSWORD}")
|
||||
assert st == 201, f"second login failed: {st} {sess2}"
|
||||
token2 = sess2["token"]
|
||||
|
||||
sh("docker compose stop redis")
|
||||
time.sleep(2)
|
||||
st, _ = req("DELETE", "/api/v1/auth/sessions/current", token=token2)
|
||||
check("redis down: logout returns 503, not silent 204", st == 503, str(st))
|
||||
st, _ = req("GET", "/api/v1/users/me", token=token2)
|
||||
check("redis down: token already revoked in postgres -> 401", st == 401, str(st))
|
||||
|
||||
sh("docker compose start redis")
|
||||
check("redis back (2): ready 200", wait_ready(60, 200))
|
||||
st, _ = req("DELETE", "/api/v1/auth/sessions/current", token=token2)
|
||||
check("redis back: logout retry 204 (DEL confirmed)", st == 204, str(st))
|
||||
st, _ = req("GET", "/api/v1/users/me", token=token2)
|
||||
check("after retry logout: token still 401", st == 401, str(st))
|
||||
|
||||
if FAILURES:
|
||||
print(f"FAILED {len(FAILURES)}: {FAILURES}")
|
||||
raise SystemExit(1)
|
||||
print("ALL FAILOVER TESTS PASSED")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,13 @@
|
||||
label,phase,history,clients,duration_s,requests,errors,rps,p50_ms,p95_ms,p99_ms,rss_avg_mib,rss_max_mib
|
||||
after,read,10000,8,10.0,30742,0,3074.2,2.49499999335967,3.886999998940155,4.761800009873696,13.1,13.8
|
||||
after,read,10000,32,10.0,30201,0,3020.1,9.78890000260435,20.00600000610575,25.65840000170283,14.4,15.5
|
||||
after,read,10000,96,10.0,29649,0,2964.9,28.709499994874932,63.01610000082292,82.43429999856744,16.8,17.1
|
||||
after,read,50000,8,10.0,28185,0,2818.5,2.7227999962633476,4.248600002028979,5.172299992409535,28.3,29.1
|
||||
after,read,50000,32,10.0,29028,0,2902.8,10.134899988770485,20.84120000654366,26.748899996164255,29.1,29.5
|
||||
after,read,50000,96,10.0,28423,0,2842.3,29.948700001114048,65.7708999933675,86.68810001108795,31.4,31.8
|
||||
after,read,150000,8,10.0,27419,0,2741.9,2.7879000117536634,4.394600007799454,5.48490000073798,63.2,64.1
|
||||
after,read,150000,32,10.0,28074,0,2807.4,10.497099996428005,21.50559998699464,27.85779999976512,64.4,64.6
|
||||
after,read,150000,96,10.0,27398,0,2739.8,30.90340000926517,68.98559999535792,89.6325999929104,65.4,66.1
|
||||
after,write,0,8,10.0,29707,0,2970.7,2.5760000135051087,4.090300004463643,4.8944000009214506,67.2,68.5
|
||||
after,write,0,32,10.0,31299,0,3129.9,9.55940000130795,18.42429999669548,23.47979998739902,76.9,79.8
|
||||
after,write,0,96,10.0,30573,0,3057.3,28.819400002248585,57.44370000320487,74.52429999830201,88.3,91.9
|
||||
|
@@ -0,0 +1,13 @@
|
||||
label,phase,history,clients,duration_s,requests,errors,rps,p50_ms,p95_ms,p99_ms,rss_avg_mib,rss_max_mib
|
||||
before,read,10000,8,10.0,18018,0,1801.8,4.309399999328889,6.358900005579926,7.281500002136454,14.7,15.2
|
||||
before,read,10000,32,10.0,23178,0,2317.8,13.206499992520548,21.274199985782616,26.18879999499768,21.0,23.4
|
||||
before,read,10000,96,10.0,21816,0,2181.6,38.834299994050525,82.42629999585915,111.98879999574274,24.3,26.8
|
||||
before,read,50000,8,10.0,4367,0,436.7,17.476300010457635,26.245999993989244,34.15639999730047,36.1,36.5
|
||||
before,read,50000,32,10.0,6347,0,634.7,48.60369999369141,79.3132999970112,96.15100000519305,66.5,68.8
|
||||
before,read,50000,96,10.0,6858,0,685.8,112.8383000032045,350.77499999897555,623.8148999982513,106.7,120.9
|
||||
before,read,150000,8,10.0,1325,0,132.5,58.98650000744965,79.64959999662824,89.75159999681637,84.9,86.7
|
||||
before,read,150000,32,10.0,1937,0,193.7,162.91949999867938,228.64050000498537,269.6471999952337,156.8,158.0
|
||||
before,read,150000,96,10.0,1989,0,198.9,465.26430000085384,717.6964999962365,1624.148500006413,301.4,338.8
|
||||
before,write,0,8,10.0,30212,0,3021.2,2.5287000025855377,4.0160999924410135,4.905399997369386,108.2,225.0
|
||||
before,write,0,32,10.0,32543,0,3254.3,9.214399993652478,18.110699995304458,22.875099995872006,81.1,85.5
|
||||
before,write,0,96,10.0,31735,0,3173.5,27.65499999804888,56.61030000192113,73.07939999736845,92.8,98.3
|
||||
|
@@ -0,0 +1,265 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Студенческие тесты lab2 (Bearer-аутентификация). Одна команда:
|
||||
python student-tests/run_tests.py --base-url http://localhost:8080
|
||||
"""
|
||||
import argparse
|
||||
import base64
|
||||
import json
|
||||
import threading
|
||||
import urllib.error
|
||||
import urllib.request
|
||||
import uuid
|
||||
|
||||
FAILURES = []
|
||||
BASE = "http://localhost:8080"
|
||||
PASSWORD = "Example-only-not-a-real-secret-123" # 38 code points: в диапазоне 12..128
|
||||
|
||||
|
||||
def check(name, cond, detail=""):
|
||||
if not cond:
|
||||
FAILURES.append(name)
|
||||
print(f"[{'PASS' if cond else 'FAIL'}] {name}" + (f" -- {detail}" if not cond and detail else ""))
|
||||
|
||||
|
||||
def _lower_headers(headers):
|
||||
"""Ключи заголовков в нижнем регистре.
|
||||
Go шлёт WWW-Authenticate как "Www-Authenticate" (каноническая MIME-форма),
|
||||
а dict(headers) сохраняет регистр как пришёл. Чтобы тест не зависел от
|
||||
регистра, всегда ищем заголовки в lower-case."""
|
||||
return {k.lower(): v for k, v in headers.items()}
|
||||
|
||||
|
||||
def www_auth(hdr):
|
||||
"""Значение WWW-Authenticate независимо от регистра ключа."""
|
||||
return hdr.get("www-authenticate", "")
|
||||
|
||||
|
||||
def req(method, path, body=None, raw=None, token=None, basic=None,
|
||||
ctype="application/json", extra_headers=None):
|
||||
headers, data = {}, None
|
||||
if raw is not None:
|
||||
data, headers["Content-Type"] = raw.encode("utf-8"), ctype
|
||||
elif body is not None:
|
||||
data, headers["Content-Type"] = json.dumps(body, ensure_ascii=False).encode("utf-8"), ctype
|
||||
if token:
|
||||
headers["Authorization"] = "Bearer " + token
|
||||
if basic:
|
||||
headers["Authorization"] = "Basic " + base64.b64encode(basic.encode("utf-8")).decode("ascii")
|
||||
if extra_headers:
|
||||
headers.update(extra_headers)
|
||||
r = urllib.request.Request(BASE + path, data=data, headers=headers, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(r, timeout=30) as resp:
|
||||
payload = resp.read().decode("utf-8")
|
||||
return resp.status, _lower_headers(resp.headers), (json.loads(payload) if payload else None)
|
||||
except urllib.error.HTTPError as e:
|
||||
payload = e.read().decode("utf-8")
|
||||
try:
|
||||
return e.code, _lower_headers(e.headers), json.loads(payload)
|
||||
except ValueError:
|
||||
return e.code, _lower_headers(e.headers), None
|
||||
|
||||
|
||||
def register(username=None, password=PASSWORD, display="Тест Юникод ✓"):
|
||||
username = username or "u" + uuid.uuid4().hex[:12]
|
||||
return req("POST", "/api/v1/users",
|
||||
{"username": username, "display_name": display, "password": password})
|
||||
|
||||
|
||||
def login(username, password=PASSWORD):
|
||||
return req("POST", "/api/v1/auth/sessions", basic=f"{username}:{password}")
|
||||
|
||||
|
||||
def main():
|
||||
global BASE
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--base-url", default=BASE)
|
||||
BASE = ap.parse_args().base_url
|
||||
|
||||
# ── health и номерки ──
|
||||
st, hdr, body = req("GET", "/health/live")
|
||||
check("health live 200", st == 200 and body == {"status": "ok"})
|
||||
check("X-Request-Id present", bool(hdr.get("x-request-id")))
|
||||
st, _, body = req("GET", "/health/ready")
|
||||
check("health ready 200", st == 200 and body == {"status": "ok"})
|
||||
|
||||
# ── валидация регистрации ──
|
||||
st, _, _ = req("POST", "/api/v1/users", raw='{"username": "abc",')
|
||||
check("malformed JSON -> 400", st == 400)
|
||||
st, _, _ = req("POST", "/api/v1/users",
|
||||
{"username": "extrafield1", "display_name": "x", "password": PASSWORD, "junk": 1})
|
||||
check("extra field -> 400", st == 400)
|
||||
st, _, _ = req("POST", "/api/v1/users", {"username": "BadName", "display_name": "x", "password": PASSWORD})
|
||||
check("username pattern -> 400", st == 400)
|
||||
st, _, _ = req("POST", "/api/v1/users", {"username": "wsname1", "display_name": " ", "password": PASSWORD})
|
||||
check("whitespace display_name -> 400", st == 400)
|
||||
st, _, _ = req("POST", "/api/v1/users", {"username": "shortpw1", "display_name": "x", "password": "short"})
|
||||
check("password < 12 -> 400", st == 400)
|
||||
st, _, _ = req("POST", "/api/v1/users", {"username": "longpw1", "display_name": "x", "password": "x" * 129})
|
||||
check("password > 128 -> 400", st == 400)
|
||||
st, _, _ = req("POST", "/api/v1/users", {"username": "wspw1", "display_name": "x", "password": " "})
|
||||
check("whitespace password -> 400", st == 400)
|
||||
|
||||
st, _, alice = register()
|
||||
check("register 201", st == 201)
|
||||
check("no password/hash in User response",
|
||||
isinstance(alice, dict) and "password" not in alice and "password_hash" not in alice)
|
||||
st2, _, _ = register(username=alice["username"])
|
||||
check("duplicate username -> 409", st2 == 409)
|
||||
|
||||
# ── вход: неверные/неизвестные/успех ──
|
||||
st, hdr, _ = login(alice["username"], "Wrong-" + PASSWORD)
|
||||
check("wrong password -> 401", st == 401)
|
||||
check("wrong password WWW-Authenticate Basic", www_auth(hdr).startswith("Basic"),
|
||||
f"got {www_auth(hdr)!r}")
|
||||
st, hdr, _ = login("nosuchuser" + uuid.uuid4().hex[:6])
|
||||
check("unknown user -> 401 same shape",
|
||||
st == 401 and www_auth(hdr).startswith("Basic"), f"got {www_auth(hdr)!r}")
|
||||
st, _, sess = login(alice["username"])
|
||||
check("login 201 session", st == 201 and sess["token_type"] == "Bearer" and len(sess["token"]) >= 32)
|
||||
check("session has expires_at and user", "expires_at" in sess and "user" in sess)
|
||||
token_a = sess["token"]
|
||||
|
||||
_, _, bob = register(); _, _, sess_b = login(bob["username"]); token_b = sess_b["token"]
|
||||
_, _, carol = register(); _, _, sess_c = login(carol["username"]); token_c = sess_c["token"]
|
||||
|
||||
# ── правила аутентификации lab2 ──
|
||||
st, hdr, _ = req("GET", "/api/v1/users/me")
|
||||
check("no auth -> 401 + Bearer header",
|
||||
st == 401 and www_auth(hdr).startswith("Bearer"), f"got {www_auth(hdr)!r}")
|
||||
st, _, _ = req("GET", "/api/v1/users/me", extra_headers={"X-User-Id": alice["id"]})
|
||||
check("X-User-Id ignored -> 401", st == 401)
|
||||
st, hdr, _ = req("GET", "/api/v1/users/me", basic=f"{alice['username']}:{PASSWORD}")
|
||||
check("Basic on business endpoint -> 401 Bearer",
|
||||
st == 401 and www_auth(hdr).startswith("Bearer"), f"got {www_auth(hdr)!r}")
|
||||
st, _, _ = req("GET", "/api/v1/users/me", token="not-a-valid-token")
|
||||
check("garbage Bearer -> 401", st == 401)
|
||||
st, _, me = req("GET", "/api/v1/users/me", token=token_a)
|
||||
check("me with Bearer 200", st == 200 and me["id"] == alice["id"])
|
||||
|
||||
# ── чаты и доступ ──
|
||||
st, _, chat = req("POST", "/api/v1/chats",
|
||||
{"title": "Проект по ВНП", "member_ids": [alice["id"], bob["id"]]}, token=token_a)
|
||||
check("create chat 201", st == 201)
|
||||
cid = chat["id"]
|
||||
st, _, _ = req("GET", f"/api/v1/chats/{cid}", token=token_c)
|
||||
check("outsider GET chat -> 404", st == 404)
|
||||
st, _, _ = req("POST", f"/api/v1/chats/{cid}/messages", {"text": "hi"}, token=token_c)
|
||||
check("outsider POST message -> 404", st == 404)
|
||||
st, _, body = req("GET", "/api/v1/chats", token=token_c)
|
||||
check("outsider list hides foreign chat", st == 200 and all(c["id"] != cid for c in body["items"]))
|
||||
|
||||
# ── валидация сообщений ──
|
||||
st, _, _ = req("POST", f"/api/v1/chats/{cid}/messages", {"text": ""}, token=token_a)
|
||||
check("empty text -> 400", st == 400)
|
||||
st, _, _ = req("POST", f"/api/v1/chats/{cid}/messages", {"text": " \n "}, token=token_a)
|
||||
check("whitespace text -> 400", st == 400)
|
||||
st, _, _ = req("POST", f"/api/v1/chats/{cid}/messages", {"text": "x" * 4001}, token=token_a)
|
||||
check("text > 4000 -> 400", st == 400)
|
||||
text = "Привет, Боб! 🚀 Ünïcödé ✓"
|
||||
st, _, msg = req("POST", f"/api/v1/chats/{cid}/messages", {"text": text}, token=token_a)
|
||||
check("unicode round-trip exact", st == 201 and msg["text"] == text)
|
||||
|
||||
# ── пагинация ──
|
||||
for i in range(24):
|
||||
req("POST", f"/api/v1/chats/{cid}/messages", {"text": f"msg-{i:02d}"}, token=token_b)
|
||||
hist, cursor, pages = [], None, 0
|
||||
while True:
|
||||
q = f"/api/v1/chats/{cid}/messages?limit=10" + (f"&cursor={cursor}" if cursor else "")
|
||||
st, _, page = req("GET", q, token=token_b)
|
||||
pages += 1
|
||||
hist.extend(page["items"])
|
||||
cursor = page["next_cursor"]
|
||||
if not cursor:
|
||||
break
|
||||
ids = [m["id"] for m in hist]
|
||||
check("25 msgs -> 3 pages, no repeats", pages == 3 and len(ids) == 25 and len(set(ids)) == 25)
|
||||
pairs = [(m["created_at"], m["id"]) for m in hist]
|
||||
bad = [i for i in range(len(pairs) - 1) if pairs[i] > pairs[i + 1]]
|
||||
check("history ordered by (created_at, id)", not bad, f"{len(bad)} inversions")
|
||||
|
||||
st, _, chat2 = req("POST", "/api/v1/chats",
|
||||
{"title": "other", "member_ids": [alice["id"], bob["id"]]}, token=token_a)
|
||||
req("POST", f"/api/v1/chats/{chat2['id']}/messages", {"text": "o1"}, token=token_a)
|
||||
req("POST", f"/api/v1/chats/{chat2['id']}/messages", {"text": "o2"}, token=token_a)
|
||||
_, _, p2 = req("GET", f"/api/v1/chats/{chat2['id']}/messages?limit=1", token=token_a)
|
||||
st, _, _ = req("GET", f"/api/v1/chats/{cid}/messages?limit=10&cursor={p2['next_cursor']}", token=token_a)
|
||||
check("cursor from another chat -> 400", st == 400)
|
||||
ok = True
|
||||
for v in ("0", "101", "abc"):
|
||||
st, _, _ = req("GET", f"/api/v1/chats/{cid}/messages?limit={v}", token=token_a)
|
||||
ok = ok and st == 400
|
||||
check("limit 0/101/abc -> 400", ok)
|
||||
|
||||
# ── logout и отзыв ──
|
||||
_, _, sess_b2 = login(bob["username"])
|
||||
token_b2 = sess_b2["token"]
|
||||
st, _, _ = req("DELETE", "/api/v1/auth/sessions/current", token=token_b2)
|
||||
check("logout -> 204", st == 204)
|
||||
st, _, _ = req("GET", "/api/v1/users/me", token=token_b2)
|
||||
check("revoked token -> 401", st == 401)
|
||||
st, _, _ = req("GET", "/api/v1/users/me", token=token_b)
|
||||
check("other session of same user still valid", st == 200)
|
||||
|
||||
# ── гонка регистрации: UNIQUE-ограничение (имя БЕЗ дефиса: маска username) ──
|
||||
race_name = "race" + uuid.uuid4().hex[:10]
|
||||
results, lock = [], threading.Lock()
|
||||
def racer():
|
||||
s, _, _ = register(username=race_name)
|
||||
with lock:
|
||||
results.append(s)
|
||||
ts = [threading.Thread(target=racer) for _ in range(10)]
|
||||
for t in ts: t.start()
|
||||
for t in ts: t.join()
|
||||
check("race registration: one 201, rest 409",
|
||||
results.count(201) == 1 and results.count(409) == 9, str(results))
|
||||
|
||||
# ── rollback составной операции ──
|
||||
_, _, before = req("GET", "/api/v1/chats", token=token_a)
|
||||
n_before = len(before["items"])
|
||||
st, _, _ = req("POST", "/api/v1/chats",
|
||||
{"title": "rollback-probe", "member_ids": [alice["id"], str(uuid.uuid4())]}, token=token_a)
|
||||
check("chat with unknown member -> 400", st == 400)
|
||||
_, _, after = req("GET", "/api/v1/chats", token=token_a)
|
||||
check("rollback: no partial chat",
|
||||
len(after["items"]) == n_before and all(c["title"] != "rollback-probe" for c in after["items"]))
|
||||
|
||||
# ── конкурентные записи ──
|
||||
errs = []
|
||||
def writer(idx):
|
||||
for j in range(10):
|
||||
s, _, _ = req("POST", f"/api/v1/chats/{cid}/messages", {"text": f"c{idx}-{j}"}, token=token_b)
|
||||
if s != 201:
|
||||
errs.append((idx, j, s))
|
||||
ts = [threading.Thread(target=writer, args=(i,)) for i in range(10)]
|
||||
for t in ts: t.start()
|
||||
for t in ts: t.join()
|
||||
check("100 concurrent sends all 201", not errs, str(errs[:5]))
|
||||
items, cur = [], None
|
||||
while True:
|
||||
q = f"/api/v1/chats/{cid}/messages?limit=100" + (f"&cursor={cur}" if cur else "")
|
||||
st, _, page = req("GET", q, token=token_b)
|
||||
items.extend(page["items"])
|
||||
cur = page["next_cursor"]
|
||||
if not cur:
|
||||
break
|
||||
expected = {f"c{i}-{j}" for i in range(10) for j in range(10)}
|
||||
got = {m["text"] for m in items}
|
||||
check("exactly 100 unique concurrent messages", expected <= got and len(got & expected) == 100)
|
||||
|
||||
# ── прочее контрактное ──
|
||||
st, _, _ = req("POST", "/api/v1/users",
|
||||
raw='{"username":"cttest1","display_name":"x","password":"' + PASSWORD + '"}',
|
||||
ctype="text/plain")
|
||||
check("wrong Content-Type -> 415", st == 415)
|
||||
st, _, body = req("GET", "/nope")
|
||||
check("unknown route -> 404 envelope", st == 404 and body and body["error"]["code"] == "not_found")
|
||||
|
||||
if FAILURES:
|
||||
print(f"FAILED {len(FAILURES)}: {FAILURES}")
|
||||
raise SystemExit(1)
|
||||
print("ALL STUDENT TESTS PASSED")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,7 @@
|
||||
-- Сев 10 000 сообщений в существующий чат.
|
||||
-- Переменные psql: :chat и :sender (передаются через -v chat=... -v sender=...).
|
||||
-- created_at разносим в прошлое по секунде на сообщение, чтобы ключ
|
||||
-- (created_at, id) был плотным и реалистичным для keyset-пагинации.
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT gen_random_uuid(), :'chat', :'sender', 'seed-' || g, now() - make_interval(secs => g)
|
||||
FROM generate_series(1, 10000) AS g;
|
||||
Reference in New Issue
Block a user