Solution/bpi2301 bakulin #76
@@ -18,3 +18,4 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
compose*.yaml
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
# Files read inside Linux containers or by sh must keep LF on Windows
|
||||
# checkouts with core.autocrlf=true.
|
||||
*.sh text eol=lf
|
||||
*.conf text eol=lf
|
||||
*.sql text eol=lf
|
||||
*.cmd text eol=crlf
|
||||
@@ -1,18 +1,18 @@
|
||||
## Лабораторная 2
|
||||
|
||||
Автор / группа: TODO
|
||||
Целевая оценка (3/4/5): TODO
|
||||
Трек 5, если заявлен: TODO
|
||||
Автор / группа: Бакулин, БПИ2301
|
||||
Целевая оценка (3/4/5): 4
|
||||
Трек 5, если заявлен: не заявлен
|
||||
|
||||
Что реализовано и что перенесено: TODO
|
||||
Команды запуска из чистого clone: TODO
|
||||
Результаты проверок и ссылка на REPORT.md: TODO
|
||||
Ограничения / известные ошибки: TODO
|
||||
Источники и использование ИИ: TODO
|
||||
Что реализовано и что перенесено: перенесён минимум lab1 (формат ошибок, keyset-пагинация, UUIDv7, graceful shutdown). Пользователи, чаты, участники и сообщения хранятся в PostgreSQL 18 (pgx, миграции goose при старте, атомарное создание чата). Регистрация с паролем (Argon2id), вход через Basic, непрозрачный Bearer-токен, сессии в Valkey с AOF `appendfsync always` и абсолютным сроком, logout. Недоступность PostgreSQL или Valkey даёт 503 с причиной в логе. Стенд в compose с secrets, хук перезапуска, EXPLAIN истории. Исправления базы lab1: `WithUser` перенесён из `auth` в `httpx` (иначе цикл импортов `auth ↔ user`), `server.Run` не зависит от сборки приложения, `member_ids` всегда в порядке id; in-memory хранилища удалены. Подробности — в REPORT.md.
|
||||
Команды запуска из чистого clone: `make secrets && make up`, затем `make test`, `make test-persistence ACTION_SCRIPT=scripts/restart.sh`, `make unit` (нужен Docker), `make explain`; удаление данных — `make destroy`.
|
||||
Результаты проверок и ссылка на REPORT.md: REPORT.md, раздел «Доказательства критериев»; файлы в `evidence/`.
|
||||
Ограничения / известные ошибки: один host без репликации; потеря Valkey разлогинивает всех; без TLS (lab7); описание семантики пагинации при конкурентных вставках — в REPORT.md.
|
||||
Источники и использование ИИ: перечислены в REPORT.md. ИИ-ассистент использовался как парный программист: генерация кода, тестов и черновиков документации по моим архитектурным решениям с последующим ревью каждого модуля и переработкой по замечаниям; выбор стека, структура проекта, модель данных, обработка ошибок, логирование и границы модулей определены мной; все проверки воспроизведены мной вручную.
|
||||
|
||||
- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [ ] Контракт и публичные тесты не ослаблены.
|
||||
- [ ] make check и make test выполнены; приложен реальный вывод.
|
||||
- [ ] Сценарии отказов/безопасности своего уровня воспроизведены.
|
||||
- [ ] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
||||
- [ ] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
||||
- [x] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
|
||||
- [x] Контракт и публичные тесты не ослаблены.
|
||||
- [x] make check и make test выполнены; приложен реальный вывод.
|
||||
- [x] Сценарии отказов/безопасности своего уровня воспроизведены.
|
||||
- [x] Все секреты и приватные ключи находятся вне Git; отчёт не содержит tokens.
|
||||
- [x] README задания сохранён, REPORT.md содержит инструкцию именно моего решения.
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
name: Application
|
||||
on: [push, pull_request]
|
||||
permissions:
|
||||
contents: read
|
||||
defaults:
|
||||
run:
|
||||
# Explicit bash turns on `-eo pipefail`, so a failing check behind `| tee` fails the job.
|
||||
shell: bash
|
||||
env:
|
||||
BASE_URL: http://localhost:8080
|
||||
jobs:
|
||||
unit:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.27.1'
|
||||
- run: go vet ./...
|
||||
- run: go test -race -count=1 ./...
|
||||
e2e:
|
||||
runs-on: ubuntu-latest
|
||||
needs: unit
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: '1.27.1'
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.12'
|
||||
- run: make secrets
|
||||
- run: docker compose up -d --build --wait
|
||||
- run: mkdir -p evidence-ci
|
||||
- name: Public smoke
|
||||
run: make test PYTHON=python BASE_URL="$BASE_URL" | tee evidence-ci/smoke.txt
|
||||
- name: Persistence across a restart of the API and both stores
|
||||
run: make test-persistence PYTHON=python BASE_URL="$BASE_URL" ACTION_SCRIPT=scripts/restart.sh | tee evidence-ci/persistence.txt
|
||||
- name: Concurrency check
|
||||
run: go run ./tools/concurrency --base-url "$BASE_URL" --output evidence-ci/concurrency.json
|
||||
- name: Container logs
|
||||
if: always()
|
||||
run: docker compose logs > evidence-ci/compose.log 2>&1 || true
|
||||
- name: Cleanup
|
||||
# The CI stand is disposable, so its volumes go too.
|
||||
if: always()
|
||||
run: docker compose down -v || true
|
||||
- uses: actions/upload-artifact@v4
|
||||
if: always()
|
||||
with:
|
||||
name: evidence
|
||||
path: evidence-ci
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
# Build stage: compile a static binary with the exact toolchain pinned in go.mod.
|
||||
FROM golang:1.27.1-alpine3.23@sha256:d9e2f2f07b10cc922da3e80e035c3058810b328d5aef82d2c63680967c5e2ec9 AS build
|
||||
WORKDIR /src
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download && go mod verify
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOFLAGS=-mod=readonly go build -trimpath -ldflags="-s -w" -o /out/server ./cmd/server
|
||||
|
||||
# Runtime stage: minimal image, unprivileged user, the server is PID 1.
|
||||
FROM alpine:3.23@sha256:fd791d74b68913cbb027c6546007b3f0d3bc45125f797758156952bc2d6daf40
|
||||
RUN adduser -D -H -u 10001 app
|
||||
COPY --from=build /out/server /usr/local/bin/server
|
||||
USER app
|
||||
ENV APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080 \
|
||||
APP_ENV=lab
|
||||
EXPOSE 8080
|
||||
HEALTHCHECK --interval=10s --timeout=2s --start-period=10s --retries=3 \
|
||||
CMD wget -qO- "http://127.0.0.1:${APP_PORT:-8080}/health/ready" >/dev/null || exit 1
|
||||
ENTRYPOINT ["/usr/local/bin/server"]
|
||||
@@ -24,3 +24,43 @@ load:
|
||||
test-persistence:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Application targets (Go implementation). The targets above are the skeleton.
|
||||
# ---------------------------------------------------------------------------
|
||||
EVIDENCE_DIR ?= evidence
|
||||
|
||||
.PHONY: build vet unit secrets up down destroy logs concurrency explain
|
||||
|
||||
build:
|
||||
go build -o bin/server ./cmd/server
|
||||
|
||||
vet:
|
||||
go vet ./...
|
||||
|
||||
# The tests start disposable PostgreSQL and Valkey containers: Docker must run.
|
||||
unit:
|
||||
go test -race -count=1 ./...
|
||||
|
||||
secrets:
|
||||
sh scripts/init-secrets.sh
|
||||
|
||||
up:
|
||||
docker compose up -d --build --wait
|
||||
|
||||
# Stops and removes the containers; volumes and therefore data are kept.
|
||||
down:
|
||||
docker compose down
|
||||
|
||||
# Deletes the volumes too: every user, chat, message and session of the stand.
|
||||
destroy:
|
||||
docker compose down -v
|
||||
|
||||
logs:
|
||||
docker compose logs
|
||||
|
||||
concurrency:
|
||||
go run ./tools/concurrency --base-url "$(BASE_URL)" --output "$(EVIDENCE_DIR)/concurrency.json"
|
||||
|
||||
explain:
|
||||
docker compose exec -T postgres psql -U messenger -d messenger -X -q -v ON_ERROR_STOP=1 -f - < scripts/explain-history.sql
|
||||
|
||||
@@ -1,37 +1,152 @@
|
||||
# Отчёт — лабораторная 2
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Бакулин, БПИ2301
|
||||
- Целевая оценка; выбранный трек 5, если нужен: 4. Уровни 3 и 4 выполнены полностью, трек 5 не заявлен.
|
||||
- Commit/PR: ветка `solution/bpi2301-bakulin`; ссылка на PR — после публикации.
|
||||
- Использованные источники, библиотеки и помощь ИИ: документация PostgreSQL 18 (транзакции, EXPLAIN, `uuidv7`, типы даты/времени), PostgreSQL wiki «Don't Do This», документация Valkey (persistence, ACL/requirepass, eviction), RFC 7617 (Basic), RFC 6750 и RFC 7235 (Bearer, `WWW-Authenticate`), RFC 9106 и OWASP Password Storage Cheat Sheet (Argon2id), документация Docker Compose (secrets, healthcheck, `depends_on`). Библиотеки: `github.com/gin-gonic/gin` v1.12.0, `github.com/google/uuid` v1.6.0, `github.com/jackc/pgx/v5` v5.11.0 (драйвер и пул), `github.com/pressly/goose/v3` v3.27.0 (миграции), `github.com/redis/go-redis/v9` v9.23.0 (клиент Valkey), `golang.org/x/crypto/argon2`, в тестах `github.com/testcontainers/testcontainers-go` v0.39.0. ИИ-ассистент использовался как парный программист: генерация кода, тестов и черновиков документации по моим архитектурным решениям с последующим ревью каждого модуля и переработкой по замечаниям; выбор стека, структура проекта, модель данных, обработка ошибок, логирование и границы модулей определены мной; все проверки воспроизведены мной вручную.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Версии, на которых выполнялись проверки: Go 1.27.1 (`go.mod`), Docker Desktop 28.5.1 с Compose v2 (нужен 2.24+ из-за `env_file … required: false`), Python 3.13 для публичных проверок (нужен 3.9+), GNU Make. Образы закреплены digest'ами: `postgres:18.6-alpine3.24`, `valkey/valkey:9.1.2-alpine3.24` в `compose.yaml`, `golang:1.27.1-alpine3.23` и `alpine:3.23` в `Dockerfile`.
|
||||
|
||||
```sh
|
||||
make secrets # ./secrets: пароли PostgreSQL и Valkey и URL для API; существующие файлы не перезаписываются
|
||||
make up # docker compose up -d --build --wait: postgres и valkey healthy → api применяет миграции → healthy
|
||||
make check PYTHON=python
|
||||
make validate PYTHON=python # полная проверка OpenAPI; нужен pip install -r requirements-dev.txt
|
||||
make test PYTHON=python
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh PYTHON=python # на Windows: ACTION_SCRIPT=scripts/restart.cmd
|
||||
make unit # собственные тесты; нужен запущенный Docker: тесты поднимают свои PostgreSQL и Valkey
|
||||
make explain > evidence/explain-history.txt
|
||||
make concurrency # 10 клиентов × 10 сообщений, отчёт в evidence/concurrency.json
|
||||
```
|
||||
|
||||
`.env` не обязателен: все настройки имеют значения по умолчанию, а пути к секретам задаёт `compose.yaml`. Если порт 8080 занят — `PUBLISH=127.0.0.1:18080 make up` и `BASE_URL=http://localhost:18080` для проверок. Короткий TTL для проверки истечения: `SESSION_TTL_SECONDS=2 make up` (Compose пересоздаст только api; вернуть — `make up`).
|
||||
|
||||
Секреты. `scripts/init-secrets.sh` создаёт в `./secrets` (каталог в `.gitignore` и `.dockerignore`) четыре файла: пароль PostgreSQL, DSN для API, `requirepass` для Valkey и URL Valkey для API. Compose монтирует их как secrets в `/run/secrets/*`; API читает их через `DATABASE_URL_FILE` и `SESSION_STORE_URL_FILE`, PostgreSQL — через `POSTGRES_PASSWORD_FILE`, Valkey — через `include` в `infra/valkey/valkey.conf`. Значения секретов не попадают ни в env контейнера (`docker inspect`), ни в образ, ни в логи. Скрипт не перезаписывает существующие файлы: PostgreSQL применяет пароль только при инициализации volume, новый пароль при живом volume заблокировал бы API.
|
||||
|
||||
Bootstrap и миграции. Схема — `migrations/00001_init.sql`, вшита в бинарник через `embed`. API при старте до открытия порта применяет миграции goose под session advisory lock (несколько реплик в lab3 сериализуются на нём), в логе `schema ready applied_migrations=[1]`, повторный старт — `applied_migrations=[]`. Отдельного шага оператора нет.
|
||||
|
||||
Остановка и удаление данных. `docker compose stop` — graceful остановка всех трёх процессов (API дорабатывает запросы, PostgreSQL пишет checkpoint, Valkey дописывает AOF); `make down` — удалить контейнеры с сохранением volumes; `make destroy` (`docker compose down -v`) — **намеренное удаление всех учебных данных**: пользователей, чатов, сообщений и сессий. Для полного сброса затем удалить `./secrets`.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
```
|
||||
клиент ──HTTP──> api (Go, gin) ──pgx pool (≤10)──> postgres [volume pgdata] users, chats, chat_members, messages
|
||||
└─go-redis (≤10)──> valkey [volume valkeydata] session:<sha256(token)> → user_id
|
||||
```
|
||||
|
||||
Источник истины: PostgreSQL — для пользователей, чатов, участников и сообщений; Valkey — единственное хранилище сессий и, значит, их источник истины (кэша в работе нет). Обе зависимости критичны для синхронного пути: без Valkey нельзя аутентифицировать ни один запрос, без PostgreSQL — прочитать или записать данные. `/health/ready` проверяет обе (общий бюджет 1 с) и флаг shutdown; `/health/live` — только процесс.
|
||||
|
||||
Модель данных (`migrations/00001_init.sql`). В схеме только модель данных — `NOT NULL`, PK, FK, `UNIQUE`; правила валидации (форматы, длины) живут в приложении и в БД не дублируются. Время — `timestamptz` (момент времени, в UTC по построению), `created_at` пишет приложение из UUIDv7 id, поэтому порядок `(created_at, id)` совпадает с порядком id.
|
||||
|
||||
| Таблица | Ключи и индексы | Зачем |
|
||||
| --- | --- | --- |
|
||||
| users | PK id, `users_username_key UNIQUE (username)` | уникальность username держит constraint, поэтому гонка регистраций безопасна |
|
||||
| chats | PK id | |
|
||||
| chat_members | PK (chat_id, user_id), индекс (user_id, chat_id), FK на chats и users | членство и состав чата; список чатов пользователя keyset-ом по второму индексу |
|
||||
| messages | PK (chat_id, id), FK на chats и users | ключ и есть индекс истории: `WHERE chat_id=$1 AND id>$2 ORDER BY id LIMIT $3` |
|
||||
|
||||
Ключ сообщений — `(chat_id, id)`, отдельного индекса по id нет. Сообщение по одному id API не ищет, а такой индекс стоил бы записи и, как показал EXPLAIN на 102 000 сообщений, уводил планировщик в обход всех сообщений по порядку id с фильтром по чату (15 303 отброшенные строки и 1092 буфера на первой странице). Цена: уникальность id гарантирована внутри чата; id генерирует сервер (UUIDv7, 74 случайных бита), коллизия практически исключена. Время диапазоном тоже не требует отдельного индекса: UUIDv7 содержит время, интервал `[t1, t2)` превращается в диапазон id по тому же ключу.
|
||||
|
||||
Транзакционные границы. Регистрация — один `INSERT`, `23505` по `users_username_key` даёт 409. Создание чата — одна транзакция: `INSERT chats` и `INSERT chat_members … unnest($2::uuid[])`; существование участников проверяется до транзакции одним batch-запросом (400 на неизвестного), FK остаётся страховкой. Отправка сообщения — проверка членства и один `INSERT`. Чтения — одиночные `SELECT` на `READ COMMITTED`. Ограничение (contracts/README:20): id выдаётся до `INSERT`, поэтому коммиты конкурентных отправок могут лечь не в порядке id; читатель, чей cursor уже прошёл больший id, не увидит «опоздавшее» сообщение при продолжении обхода, при чтении с начала оно стоит на своём месте, повторов нет.
|
||||
|
||||
Аутентификация (`internal/auth`). `POST /api/v1/auth/sessions` принимает только `Authorization: Basic` (`Request.BasicAuth` делит строку по первому двоеточию, так что двоеточие в пароле допустимо). Неизвестный пользователь, неверный пароль, username с NUL или не-UTF-8 дают одинаковый 401 с `WWW-Authenticate: Basic realm="messenger", charset="UTF-8"`; для неизвестного username всё равно выполняется KDF на фиктивном хеше, чтобы время ответа не выдавало существование пользователя. Успех — `201 {token, token_type: "Bearer", expires_at, user}`. Токен — 32 случайных байта в base64url (256 бит), непрозрачный. В Valkey хранится ключ `session:<sha256(token)>` со значением user_id и абсолютным сроком (`SET … PXAT/EXAT`): утечка AOF не даёт рабочих токенов, а рестарт не продлевает сессию. Все остальные endpoint'ы требуют `Authorization: Bearer <token>` (схема без учёта регистра); отсутствие, Basic, неизвестный, истёкший или отозванный токен — 401 с `WWW-Authenticate: Bearer realm="messenger"`. `X-User-Id` не читается вообще. `DELETE /api/v1/auth/sessions/current` удаляет ключ и отвечает 204, повтор — 401. Срок жизни задаёт `SESSION_TTL_SECONDS` и фиксируется при входе; скользящего продления нет, иначе `expires_at` в ответе был бы ложью. Пароли — Argon2id (`m=19 MiB, t=2, p=1`, соль 16 байт) в PHC-строке; параметры читаются из строки, поэтому их можно поднять без инвалидации старых хешей.
|
||||
|
||||
Valkey (`infra/valkey/valkey.conf`): `appendonly yes`, `appendfsync always` — запись сессии подтверждается после fsync, как COMMIT в PostgreSQL; пишут только вход и выход, чтение сессии fsync не трогает. `save ""` — RDB не нужен рядом с AOF. `maxmemory 256mb` с `noeviction` — сессии никогда не вытесняются: при заполнении новые входы получают 503, а не молча разлогинивают пользователей. Пароль Valkey задан: иначе любой процесс в docker-сети мог бы записать `session:<sha256(своего токена)>` и подделать сессию.
|
||||
|
||||
Отказы и таймауты. Пул PostgreSQL ограничен 10 соединениями, `statement_timeout=5s`, `idle_in_transaction_session_timeout=10s`, подключение и поиск имени хоста — 1 с. Клиент Valkey: пул 10, дозвон 1 с одной попыткой, чтение и запись 1 с, повторы команд выключены. Каждый запрос к `/api` ограничен дедлайном 5 с (меньше `WriteTimeout` сервера 10 с, чтобы 503 успел записаться). Недоступность зависимости (отказ соединения, таймаут, классы ошибок PostgreSQL 08/53/57) превращается в `503 unavailable`; клиенту уходит только безопасное сообщение (`database unavailable`, `session store unavailable`), причина пишется в лог строкой `request failed` с `request_id` и `cause`. Аноним при недоступном Valkey не пропускается: ответ 503, а не 401 и не 200. Регистрация от Valkey не зависит и продолжает работать.
|
||||
|
||||
Код и что изменилось относительно lab1:
|
||||
|
||||
```
|
||||
cmd/server конфигурация, пул PostgreSQL, миграции, клиент Valkey, listener, сигналы
|
||||
migrations/ SQL-миграции goose, вшиты в бинарник
|
||||
internal/app composition root (Deps: пул, клиент Valkey, TTL, таймаут запроса)
|
||||
internal/server http.Server и graceful shutdown; принимает http.Handler и флаг readiness
|
||||
internal/auth session.go, repository.go, service.go, handler.go, middleware.go; store/valkey.go
|
||||
internal/user + пароль и Authenticate; store/postgres.go (inmem удалён)
|
||||
internal/chat участники в каноническом порядке по id; store/postgres.go (inmem удалён)
|
||||
internal/health live/ready с проверками зависимостей
|
||||
internal/platform/ config (+ *_FILE, SESSION_TTL_SECONDS), postgres (пул, Migrate, Classify),
|
||||
valkey (клиент), password (Argon2id), httpx (+ WithUser, Timeout), apperr, ident, pagination
|
||||
internal/testenv PostgreSQL и Valkey в testcontainers для тестов; в бинарник не входит
|
||||
tools/concurrency чёрный ящик гонок, теперь с паролем и Bearer
|
||||
```
|
||||
|
||||
Перенесено из lab1 без изменений: формат ошибок и `X-Request-Id`, keyset-пагинация и cursor, UUIDv7 и вывод `created_at` из id, access log по уровням, graceful shutdown. Изменено по объявленному переходу 1 → 2: регистрация требует `password`, `X-User-Id` заменён Bearer-сессией, Basic только на входе. Исправления базы: `WithUser` переехал из `auth` в `httpx` (иначе цикл импортов `auth ↔ user`), `server.Run` больше не зависит от сборки приложения, `member_ids` всегда в порядке id (контракт считает их множеством). Удалены in-memory хранилища и сценарий `student-tests/lifecycle.sh` (он проверял потерю данных после рестарта — в lab2 нужно обратное, это проверяет `make test-persistence`).
|
||||
|
||||
Тесты. Сервисы, хранилища и HTTP тестируются против настоящих PostgreSQL и Valkey (testcontainers-go, один контейнер на пакет, `TRUNCATE` всех таблиц и `FLUSHDB` перед каждым тестом); чистая логика (валидация, cursor, конфигурация, Argon2id) — обычными юнит-тестами. testcontainers закреплён на v0.39.0: с v0.40 он запрашивает публикацию порта с `HostPort "0"`, которую Docker Desktop 28.5.1 на Windows не выполняет; goose — на v3.27.0 из-за совместимости транзитивной зависимости с этой версией.
|
||||
|
||||
Соответствие `.env.example` реальной конфигурации:
|
||||
|
||||
| Переменная | `.env.example` | Реализация | По умолчанию |
|
||||
| --- | --- | --- | --- |
|
||||
| APP_HOST, APP_PORT, APP_ENV | да | как в lab1 | 0.0.0.0, 8080, lab |
|
||||
| SESSION_TTL_SECONDS | да | срок сессии, целое > 0 | 3600 |
|
||||
| DATABASE_URL_FILE | да | файл с DSN PostgreSQL, обязателен | `/run/secrets/database_url` в compose |
|
||||
| SESSION_STORE | да (`postgres`) | не читается: сессии всегда в Valkey | — |
|
||||
| CACHE_URL_FILE | да | не используется: кэша нет; имя оставлено для кэша, если он появится | — |
|
||||
| SESSION_STORE_URL_FILE | нет | файл с URL Valkey, обязателен | `/run/secrets/session_store_url` в compose |
|
||||
| APP_LOG_LEVEL, APP_SHUTDOWN_TIMEOUT, APP_MAX_BODY_BYTES | нет | как в lab1 | info, 8s, 65536 |
|
||||
|
||||
Ошибка в любой переменной или нечитаемый файл секрета останавливают процесс на старте с кодом 2; сообщение называет переменную, но не значение.
|
||||
|
||||
Роли стенда:
|
||||
|
||||
| Роль | Сервис | Версия | Health/readiness | Volume | Сеть/порт | Credentials |
|
||||
| --- | --- | --- | --- | --- | --- | --- |
|
||||
| api | api (`mtusi-messenger-lab2`) | сборка из исходников | `/health/ready` (PG + Valkey) | — | `127.0.0.1:8080` | secrets `database_url`, `session_store_url` |
|
||||
| postgres | postgres | 18.6-alpine3.24 @digest | `pg_isready -h 127.0.0.1` | `pgdata:/var/lib/postgresql` | только внутренняя сеть | secret `postgres_password` |
|
||||
| redis_or_valkey | valkey | 9.1.2-alpine3.24 @digest | `valkey-cli ping` с паролем | `valkeydata:/data` (AOF) | только внутренняя сеть | secret `valkey_auth` |
|
||||
|
||||
CI. `.github/workflows/app.yml`: unit-тесты с race detector (на runner нужен Docker для testcontainers), затем `make secrets`, `docker compose up --build --wait`, smoke, persistence с `scripts/restart.sh`, concurrency, логи и `down -v` (стенд CI одноразовый). Skeleton-job `contracts.yml` не менялся.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
Все файлы сняты на одном стенде, поднятом с пустых volumes (дата, commit и параметры хоста в заголовке каждого файла).
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Публичный smoke | `make check`, `make validate`, `make test` | все PASS | evidence/smoke.txt |
|
||||
| Минимум lab1 с переходом на Bearer | `make test` (сценарий lab2), `TestProtectedRoutesAcceptOnlyBearer` и остальные HTTP-тесты `internal/app` | PASS | evidence/smoke.txt, evidence/tests.txt |
|
||||
| Данные и действующие сессии переживают рестарт API и хранилищ без удаления volumes | `make test-persistence ACTION_SCRIPT=scripts/restart.cmd` (`restart.sh`: `docker compose stop` + `up -d --wait`) | PASS тем же токеном; `StartedAt` всех трёх контейнеров изменился | evidence/persistence.txt |
|
||||
| PostgreSQL: пользователи, чаты, участники, сообщения; PK/FK, UNIQUE username; миграции с нуля | `make destroy && make up`, `\dt`, `\di` | `schema ready applied_migrations=[1]`, 4 таблицы и индексы | evidence/bootstrap.txt |
|
||||
| Повторный запуск миграций без дублирования | рестарт api; `TestMigrateIsRepeatable` | `applied_migrations=[]` | evidence/bootstrap.txt, evidence/tests.txt |
|
||||
| Атомарное создание чата и его rollback | `TestCreateRollsBackWhenMemberInsertFails` | после ошибки FK нет ни строки чата, ни участников | evidence/tests.txt |
|
||||
| Basic только на входе, Bearer на остальных, `X-User-Id` не даёт доступ | `TestProtectedRoutesAcceptOnlyBearer`, `TestLoginFailuresAreUniformBasicChallenges` | 401 с нужным `WWW-Authenticate`; `X-User-Id` игнорируется | evidence/tests.txt |
|
||||
| Срок жизни и отзыв сессии | `TestSessionExpiresAfterTTL`, `TestLogoutRevokesOnlyTheCurrentToken`; стенд с `SESSION_TTL_SECONDS=2` | через 3 с 401 Bearer; logout 204, повтор 401 | evidence/expiry.txt, evidence/tests.txt |
|
||||
| Пароли через password KDF | `TestRegisterStoresUserWithPasswordHash` | в БД `$argon2id$v=19$m=19456,t=2,p=1$…` | evidence/tests.txt |
|
||||
| Secrets/volumes не в Git | `git ls-files secrets .env` | пусто; `secrets/` в `.gitignore` и `.dockerignore` | — |
|
||||
| Valkey реально используется для сессий с TTL и persistence | `CONFIG GET`; остановка/запуск Valkey с тем же токеном | `appendonly yes`, `appendfsync always`, `noeviction`; после рестарта Valkey токен работает | evidence/bootstrap.txt, evidence/dependency-failures.txt |
|
||||
| Параметризованные запросы, ограниченный пул и таймауты | код `store/postgres.go` (только `$n`), `TestPoolAppliesLimitsAndTimeouts`, `TestTimeoutBoundsTheRequestContext` | `MaxConns=10`, `statement_timeout=5s`, connect 1 с, дедлайн запроса | evidence/tests.txt |
|
||||
| Гонка регистрации одного username | `TestConcurrentRegistrationOfOneUsernameHasOneWinner` | 10 параллельных попыток: ровно 1 успех, 9 × 409, одна строка | evidence/tests.txt |
|
||||
| Индекс истории обоснован EXPLAIN на ≥10 000 сообщений, без полного scan на страницу | `make explain` (102 000 сообщений, 12 000 в целевом чате, транзакция с ROLLBACK) | `Index Scan using messages_pkey`, `Index Cond (chat_id = … AND id > …)`, 51 строка; 51 буфер на первой странице и 6 на глубокой, 0.05–0.1 мс; OFFSET той же страницы — сортировка 10 052 строк, 495 буферов, 6.5 мс | evidence/explain-history.txt |
|
||||
| Автопроверки expiry, logout, неверного пароля, недоступности Redis/БД | `TestSessionExpiresAfterTTL`, `TestLogoutRevokesOnlyTheCurrentToken`, `TestLoginFailuresAreUniformBasicChallenges`, `TestValkeyOutageAnswers503`, `TestPostgresOutageAnswers503`, `TestHungPostgresAnswers503WithinTheRequestTimeout` | все PASS; при отказе — 503, никогда 200 или 401 | evidence/tests.txt, evidence/dependency-failures.txt |
|
||||
| Гонки отправки сообщений | `make concurrency` | 100/100 созданы, перечислены и уникальны, порядок по id | evidence/concurrency.json |
|
||||
| Выбранный трек 5 | не заявлен | — | — |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Стенд: Windows 11 Pro, Docker Desktop 28.5.1 (4 CPU, 3.8 GiB RAM для Linux-VM), без лимитов CPU/RAM на контейнеры, loopback-сеть. Нагрузочные измерения не проводились (трек 5 не заявлен); ниже проверки отказов уровня 4.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Полный рестарт: `docker compose stop` и `up -d --wait` (хук persistence) | те же данные и тот же токен | PASS, все три процесса перезапущены | весь тест с хуком ~15 с; потерь нет |
|
||||
| `docker compose stop valkey` | 503 на защищённые запросы и вход, ready 503, регистрация работает | 503 за 1.0 с (дозвон ограничен 1 с; без ограничения поиск имени остановленного контейнера в DNS Docker не укладывается в дедлайн запроса 5 с), `POST /users` 201, в логе `cause: dial tcp: lookup valkey: i/o timeout` | после `start` тот же токен 200: сессии восстановлены из AOF |
|
||||
| `docker compose stop postgres` | 503 на операции с данными, ready 503 | первый запрос — 503 за 0.02 с (PostgreSQL при остановке закрыл соединения пула, в логе `57P01`), следующие — за 1.0 с (новое соединение ждёт поиск имени, ограниченный 1 с, в логе `hostname resolving error … i/o timeout`) | после `start` 200 без перезапуска API: пул переподключается |
|
||||
| PostgreSQL на паузе (`docker pause`, тест) | запрос не висит дольше дедлайна | 503 при дедлайне 300 мс, ready 503 | — |
|
||||
| `SESSION_TTL_SECONDS=2` | сессия истекает | 200 сразу, 401 Bearer через 3 с | — |
|
||||
| Ввод, который PostgreSQL не может хранить: NUL в `display_name`/`title`/`text`, NUL или не-UTF-8 в Basic username | 400 / 401, не 500 | 400 и 401 (проверка в приложении) | — |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- Один Docker-host и по одному экземпляру PostgreSQL и Valkey: отказ host или диска теряет сервис; репликации и бэкапов нет. `appendfsync always` защищает подтверждённые сессии от падения процесса Valkey, но не от потери volume.
|
||||
- Потеря или `FLUSHALL` Valkey разлогинивает всех: сессии существуют только там. Данные пользователей при этом не теряются, нужен повторный вход.
|
||||
- Valkey — вторая критичная зависимость: при его отказе защищённый API отвечает 503, хотя PostgreSQL жив. Это осознанная цена хранения сессий вне БД.
|
||||
- Argon2id тратит 19 MiB на вход или регистрацию; ограничения частоты входа нет (429 в контракте факультативен), всплеск входов нагружает CPU и память API.
|
||||
- Семантика пагинации при конкурентных вставках описана выше: сообщение, закоммиченное позже сообщения с большим id, может быть пропущено продолжением уже идущего обхода.
|
||||
- Уникальность id сообщения гарантирована в пределах чата; поиск сообщения по одному id потребует отдельного индекса.
|
||||
- HTTP без TLS допустим только на локальном стенде (Basic передаёт пароль в открытом виде); TLS — в lab7. API ходит в PostgreSQL пользователем `messenger`, которого создаёт образ (он суперпользователь); отдельная роль без DDL и прав суперпользователя — задача lab7.
|
||||
- `scripts/restart.cmd` нужен только на Windows: `tests/resilience.py` запускает хук без shell, а Windows не исполняет `.sh` напрямую.
|
||||
- Дальше: трек 5 по согласованности кэша истории (отдельный экземпляр Valkey под кэш, имя переменной `CACHE_URL_FILE` зарезервировано) либо подготовка к lab3 (реплики API используют общие сессии в Valkey уже сейчас).
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
// Command server starts the messenger API over PostgreSQL and Valkey.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/platform/config"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/valkey"
|
||||
"messenger/internal/server"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load(os.LookupEnv)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "configuration error:", err)
|
||||
os.Exit(2)
|
||||
}
|
||||
log := newLogger(cfg)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
|
||||
defer stop()
|
||||
|
||||
if err := run(ctx, log, cfg); err != nil {
|
||||
log.Error("server stopped with error", "error", err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
// run connects the dependencies before it listens, so the port accepts
|
||||
// connections only once the schema is in place.
|
||||
func run(ctx context.Context, log *slog.Logger, cfg config.Config) error {
|
||||
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
applied, err := postgres.Migrate(ctx, pool)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
log.Info("schema ready", "applied_migrations", applied)
|
||||
sessions, err := valkey.NewClient(ctx, cfg.SessionStoreURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer sessions.Close()
|
||||
|
||||
ln, err := net.Listen("tcp", cfg.Addr())
|
||||
if err != nil {
|
||||
return fmt.Errorf("listen on %s: %w", cfg.Addr(), err)
|
||||
}
|
||||
log.Info("starting", "env", cfg.Env, "addr", cfg.Addr(),
|
||||
"shutdown_timeout", cfg.ShutdownTimeout.String(), "session_ttl", cfg.SessionTTL.String())
|
||||
a := app.New(app.Deps{
|
||||
Log: log,
|
||||
DB: pool,
|
||||
Sessions: sessions,
|
||||
SessionTTL: cfg.SessionTTL,
|
||||
MaxBodyBytes: cfg.MaxBodyBytes,
|
||||
})
|
||||
return server.Run(ctx, log, ln, a.Engine, a.Health, cfg.ShutdownTimeout)
|
||||
}
|
||||
|
||||
func newLogger(cfg config.Config) *slog.Logger {
|
||||
opts := &slog.HandlerOptions{Level: cfg.LogLevel}
|
||||
if cfg.Env == "dev" {
|
||||
return slog.New(slog.NewTextHandler(os.Stdout, opts))
|
||||
}
|
||||
return slog.New(slog.NewJSONHandler(os.Stdout, opts))
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
# Local stand: the API over PostgreSQL (users, chats, messages) and Valkey
|
||||
# (sessions). Only the API is published, on loopback. Credentials are files in
|
||||
# ./secrets created by `make secrets`; named volumes keep the data across
|
||||
# restarts, and only `make destroy` removes them.
|
||||
services:
|
||||
api:
|
||||
build: .
|
||||
image: mtusi-messenger:lab2
|
||||
# Fixed name for docker logs/inspect in the report.
|
||||
container_name: mtusi-messenger-lab2
|
||||
env_file:
|
||||
- path: .env
|
||||
required: false
|
||||
environment:
|
||||
DATABASE_URL_FILE: /run/secrets/database_url
|
||||
SESSION_STORE_URL_FILE: /run/secrets/session_store_url
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
secrets:
|
||||
- database_url
|
||||
- session_store_url
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
valkey:
|
||||
condition: service_healthy
|
||||
# Loopback only; override with PUBLISH=127.0.0.1:18080 when 8080 is busy.
|
||||
ports:
|
||||
- "${PUBLISH:-127.0.0.1:8080}:8080"
|
||||
stop_grace_period: 10s
|
||||
|
||||
postgres:
|
||||
image: postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873
|
||||
environment:
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets:
|
||||
- postgres_password
|
||||
# Since PostgreSQL 18 the image keeps PGDATA in a versioned directory
|
||||
# below /var/lib/postgresql, so the volume is mounted on the parent.
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql
|
||||
healthcheck:
|
||||
# Over TCP: during first initialisation the image runs a temporary
|
||||
# server on the Unix socket only, which must not count as ready.
|
||||
test: ["CMD", "pg_isready", "-h", "127.0.0.1", "-U", "messenger", "-d", "messenger"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 10s
|
||||
# A clean shutdown writes a checkpoint instead of leaving crash recovery
|
||||
# to the next start.
|
||||
stop_grace_period: 30s
|
||||
|
||||
valkey:
|
||||
image: valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11
|
||||
command: ["valkey-server", "/usr/local/etc/valkey/valkey.conf"]
|
||||
secrets:
|
||||
- valkey_auth
|
||||
volumes:
|
||||
- ./infra/valkey/valkey.conf:/usr/local/etc/valkey/valkey.conf:ro
|
||||
- valkeydata:/data
|
||||
healthcheck:
|
||||
# valkey-cli exits 0 even on NOAUTH, so the reply itself is checked.
|
||||
test: ["CMD-SHELL", "VALKEYCLI_AUTH=\"$$(sed -n 's/^requirepass //p' /run/secrets/valkey_auth)\" valkey-cli ping | grep -q PONG"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 5s
|
||||
stop_grace_period: 10s
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
valkeydata:
|
||||
|
||||
secrets:
|
||||
database_url:
|
||||
file: ./secrets/database_url
|
||||
session_store_url:
|
||||
file: ./secrets/session_store_url
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password
|
||||
valkey_auth:
|
||||
file: ./secrets/valkey_auth.conf
|
||||
@@ -0,0 +1,63 @@
|
||||
# Bootstrap from empty volumes: make destroy && make secrets up
|
||||
# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin)
|
||||
# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1
|
||||
|
||||
$ make up
|
||||
Network mtusi2026lab2_default Created
|
||||
Volume mtusi2026lab2_pgdata Created
|
||||
Volume mtusi2026lab2_valkeydata Created
|
||||
Container mtusi2026lab2-valkey-1 Created
|
||||
Container mtusi2026lab2-postgres-1 Created
|
||||
Container mtusi-messenger-lab2 Created
|
||||
Container mtusi2026lab2-valkey-1 Started
|
||||
Container mtusi2026lab2-postgres-1 Started
|
||||
Container mtusi2026lab2-valkey-1 Healthy
|
||||
Container mtusi2026lab2-postgres-1 Healthy
|
||||
Container mtusi-messenger-lab2 Started
|
||||
Container mtusi2026lab2-postgres-1 Healthy
|
||||
Container mtusi2026lab2-valkey-1 Healthy
|
||||
Container mtusi-messenger-lab2 Healthy
|
||||
|
||||
$ docker compose ps
|
||||
SERVICE IMAGE STATUS PORTS
|
||||
api mtusi-messenger:lab2 Up 6 seconds (healthy) 127.0.0.1:8080->8080/tcp
|
||||
postgres postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873 Up 12 seconds (healthy) 5432/tcp
|
||||
valkey valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11 Up 12 seconds (healthy) 6379/tcp
|
||||
|
||||
$ docker compose logs api | grep -E "schema ready|starting"
|
||||
{"time":"2026-10-21T12:10:07.644733332Z","level":"INFO","msg":"schema ready","applied_migrations":[1]}
|
||||
{"time":"2026-10-21T12:10:07.674799548Z","level":"INFO","msg":"starting","env":"lab","addr":"0.0.0.0:8080","shutdown_timeout":"8s","session_ttl":"1h0m0s"}
|
||||
|
||||
$ psql \dt and \di (schema created by the migration)
|
||||
List of tables
|
||||
Schema | Name | Type | Owner
|
||||
--------+------------------+-------+-----------
|
||||
public | chat_members | table | messenger
|
||||
public | chats | table | messenger
|
||||
public | goose_db_version | table | messenger
|
||||
public | messages | table | messenger
|
||||
public | users | table | messenger
|
||||
(5 rows)
|
||||
|
||||
List of indexes
|
||||
Schema | Name | Type | Owner | Table
|
||||
--------+----------------------------+-------+-----------+------------------
|
||||
public | chat_members_pkey | index | messenger | chat_members
|
||||
public | chat_members_user_chat_idx | index | messenger | chat_members
|
||||
public | chats_pkey | index | messenger | chats
|
||||
public | goose_db_version_pkey | index | messenger | goose_db_version
|
||||
public | messages_pkey | index | messenger | messages
|
||||
public | users_pkey | index | messenger | users
|
||||
public | users_username_key | index | messenger | users
|
||||
(7 rows)
|
||||
|
||||
|
||||
$ valkey-cli CONFIG GET (persistence of the session store)
|
||||
appendonly yes
|
||||
appendfsync always
|
||||
save
|
||||
maxmemory 268435456
|
||||
maxmemory-policy noeviction
|
||||
|
||||
$ second start: migrations are a no-op
|
||||
{"time":"2026-10-21T12:10:18.658799511Z","level":"INFO","msg":"schema ready","applied_migrations":[]}
|
||||
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"base_url": "http://localhost:8080",
|
||||
"started_at": "2026-10-21T12:11:30.6825994Z",
|
||||
"clients": 10,
|
||||
"per_client": 10,
|
||||
"expected_messages": 100,
|
||||
"created_201": 100,
|
||||
"failed_posts": 0,
|
||||
"listed_messages": 100,
|
||||
"unique_ids": 100,
|
||||
"created_but_not_listed": 0,
|
||||
"history_ordered_by_id": true,
|
||||
"pages_read": 1,
|
||||
"elapsed_seconds": 0.0793356,
|
||||
"passed": true
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
# Dependency outages on the stand (request deadline 5 s, dial/connect and host lookup 1 s, readiness budget 1 s)
|
||||
# date: 2026-10-21T12:14:15Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin)
|
||||
# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1
|
||||
|
||||
baseline GET /users/me: 200
|
||||
|
||||
$ docker compose stop valkey
|
||||
client sees only a safe message:
|
||||
{"error":{"code":"unavailable","message":"session store unavailable","request_id":"dcf58dc8b038da34fbffaa398be2ae19"}}
|
||||
|
||||
GET /users/me: 503 in 1.029945s, request_id 409bc8d095cd5c0aa8bff3a5a6103336
|
||||
POST /auth/sessions: 503 in 1.067333s, request_id b80c3cf8d77205be7d145d366e3eba7c
|
||||
POST /users (PostgreSQL only): 201
|
||||
GET /health/ready: 503
|
||||
api log, by request_id:
|
||||
409bc8d095cd5c0aa8bff3a5a6103336: session store unavailable <- dial tcp: lookup valkey: i/o timeout
|
||||
b80c3cf8d77205be7d145d366e3eba7c: session store unavailable <- dial tcp: lookup valkey: i/o timeout
|
||||
after docker compose start valkey, same token: 200 (session survived via AOF)
|
||||
|
||||
$ docker compose stop postgres
|
||||
first request: the pooled connection was closed by the server on shutdown
|
||||
GET /chats: 503 in 0.022848s, request_id 484577a2014613ccc2cc0af7a110147b
|
||||
next requests: a new connection waits for the host lookup, bounded by 1 s
|
||||
GET /chats: 503 in 1.018873s, request_id 68b40ce6917aac8cae5a3a06f53f0d3f
|
||||
GET /chats: 503 in 1.019858s, request_id d085caa2d09226ceebe5b9aad55cbc2b
|
||||
GET /chats: 503 in 1.016325s, request_id e646903eb0912bf1c2b75a3dee3c302e
|
||||
client sees only a safe message:
|
||||
{"error":{"code":"unavailable","message":"database unavailable","request_id":"0dc9f28ccba988caededb452faec06ac"}}
|
||||
|
||||
GET /health/ready: 503
|
||||
api log, by request_id:
|
||||
484577a2014613ccc2cc0af7a110147b: select chats: database unavailable <- FATAL: terminating connection due to administrator command (SQLSTATE 57P01)
|
||||
68b40ce6917aac8cae5a3a06f53f0d3f: select chats: database unavailable <- failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: dial udp 127.0.0.11:53: i/o timeout
|
||||
d085caa2d09226ceebe5b9aad55cbc2b: select chats: database unavailable <- failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: dial udp 127.0.0.11:53: i/o timeout
|
||||
e646903eb0912bf1c2b75a3dee3c302e: select chats: database unavailable <- failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: dial udp 127.0.0.11:53: i/o timeout
|
||||
after docker compose start postgres: 200
|
||||
GET /health/ready: 200
|
||||
@@ -0,0 +1,12 @@
|
||||
# Session expiry with SESSION_TTL_SECONDS=2
|
||||
# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin)
|
||||
# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1
|
||||
|
||||
api SESSION_TTL_SECONDS=2
|
||||
GET /users/me right after login: 200
|
||||
GET /users/me 3 s later:
|
||||
HTTP/1.1 401 Unauthorized
|
||||
Www-Authenticate: Bearer realm="messenger"
|
||||
{"error":{"code":"unauthorized","message":"a valid bearer token is required","request_id":"e191d8f08a51fced88868d6fac0d8674"}}
|
||||
|
||||
api restored with SESSION_TTL_SECONDS=3600
|
||||
@@ -0,0 +1,61 @@
|
||||
# EXPLAIN (ANALYZE, BUFFERS) of the history query: make explain
|
||||
# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin)
|
||||
# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1
|
||||
|
||||
big_chat_messages | all_messages
|
||||
-------------------+--------------
|
||||
12000 | 102003
|
||||
(1 row)
|
||||
|
||||
|
||||
== First page: no cursor, limit 50 (+1 row that tells whether a next page exists)
|
||||
QUERY PLAN
|
||||
------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=0.42..23.92 rows=51 width=67) (actual time=0.027..0.060 rows=51.00 loops=1)
|
||||
Buffers: shared hit=51
|
||||
-> Index Scan using messages_pkey on messages (cost=0.42..5526.02 rows=11988 width=67) (actual time=0.026..0.056 rows=51.00 loops=1)
|
||||
Index Cond: ((chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid) AND (id > '00000000-0000-0000-0000-000000000000'::uuid))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=51
|
||||
Planning:
|
||||
Buffers: shared hit=17
|
||||
Planning Time: 0.211 ms
|
||||
Execution Time: 0.097 ms
|
||||
(10 rows)
|
||||
|
||||
|
||||
== Deep page: cursor after the 10 001st message of the chat
|
||||
QUERY PLAN
|
||||
----------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=0.42..184.60 rows=51 width=67) (actual time=0.018..0.030 rows=51.00 loops=1)
|
||||
Buffers: shared hit=6
|
||||
-> Index Scan using messages_pkey on messages (cost=0.42..650.48 rows=180 width=67) (actual time=0.018..0.026 rows=51.00 loops=1)
|
||||
Index Cond: ((chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid) AND (id > '01a15e5f-9557-7527-8fb0-c493cb1229d4'::uuid))
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=6
|
||||
Planning Time: 0.071 ms
|
||||
Execution Time: 0.047 ms
|
||||
(8 rows)
|
||||
|
||||
|
||||
== For contrast, OFFSET pagination of the same page reads every skipped row
|
||||
QUERY PLAN
|
||||
-----------------------------------------------------------------------------------------------------------------------------------------------
|
||||
Limit (cost=2832.42..2832.54 rows=51 width=67) (actual time=6.369..6.376 rows=51.00 loops=1)
|
||||
Buffers: shared hit=495
|
||||
-> Sort (cost=2807.41..2837.39 rows=11989 width=67) (actual time=5.728..6.109 rows=10052.00 loops=1)
|
||||
Sort Key: id
|
||||
Sort Method: quicksort Memory: 1978kB
|
||||
Buffers: shared hit=495
|
||||
-> Bitmap Heap Scan on messages (cost=585.33..1995.19 rows=11989 width=67) (actual time=0.717..2.712 rows=12000.00 loops=1)
|
||||
Recheck Cond: (chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid)
|
||||
Heap Blocks: exact=363
|
||||
Buffers: shared hit=495
|
||||
-> Bitmap Index Scan on messages_pkey (cost=0.00..582.34 rows=11989 width=0) (actual time=0.672..0.673 rows=12000.00 loops=1)
|
||||
Index Cond: (chat_id = '01a15e5f-93aa-7f38-9ff3-684f0d6ded77'::uuid)
|
||||
Index Searches: 1
|
||||
Buffers: shared hit=132
|
||||
Planning Time: 0.041 ms
|
||||
Execution Time: 6.524 ms
|
||||
(16 rows)
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
# Persistence: make test-persistence ACTION_SCRIPT=scripts/restart.cmd (scripts/restart.sh on Linux)
|
||||
# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin)
|
||||
# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1
|
||||
|
||||
== containers before
|
||||
/mtusi-messenger-lab2 started=2026-10-21T12:10:18.43319842Z status=running
|
||||
/mtusi2026lab2-postgres-1 started=2026-10-21T12:10:01.058712538Z status=running
|
||||
/mtusi2026lab2-valkey-1 started=2026-10-21T12:10:01.069796526Z status=running
|
||||
|
||||
$ make test-persistence ACTION_SCRIPT=scripts/restart.cmd PYTHON=python
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
|
||||
== containers after (every StartedAt changed: all three processes were restarted, volumes kept)
|
||||
/mtusi-messenger-lab2 started=2026-10-21T12:10:34.891259448Z status=running
|
||||
/mtusi2026lab2-postgres-1 started=2026-10-21T12:10:28.977896441Z status=running
|
||||
/mtusi2026lab2-valkey-1 started=2026-10-21T12:10:28.995400471Z status=running
|
||||
@@ -0,0 +1,12 @@
|
||||
# Public checks: make check, make validate, make test
|
||||
# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin)
|
||||
# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1
|
||||
|
||||
$ make check PYTHON=python
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
|
||||
$ make validate PYTHON=python # after pip install -r requirements-dev.txt
|
||||
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
|
||||
|
||||
$ make test PYTHON=python
|
||||
PASS: lab2 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,50 @@
|
||||
# Automated tests against real PostgreSQL and Valkey (testcontainers): go test -race -v
|
||||
# date: 2026-10-21T12:09:10Z, commit: 7aa86d3 (branch solution/bpi2301-bakulin)
|
||||
# host: Windows 11, Docker Desktop 28.5.1 (4 CPU, 4111327232 bytes RAM), Go go1.27.1, Python 3.13.1
|
||||
# full suite: make unit (go test -race -count=1 ./...), 16 packages ok, 0 failed
|
||||
|
||||
--- PASS: TestRegisterUserShapeAndConflicts (0.18s)
|
||||
--- PASS: TestConcurrentSendsProduceExactlyUniqueMessages (0.97s)
|
||||
--- PASS: TestLoginReturnsBearerSession (0.14s)
|
||||
--- PASS: TestLoginFailuresAreUniformBasicChallenges (0.26s)
|
||||
--- PASS: TestLoginAcceptsColonAndUnicodePasswords (0.08s)
|
||||
--- PASS: TestProtectedRoutesAcceptOnlyBearer (0.17s)
|
||||
--- PASS: TestLogoutRevokesOnlyTheCurrentToken (0.16s)
|
||||
--- PASS: TestSessionExpiresAfterTTL (1.29s)
|
||||
--- PASS: TestValkeyOutageAnswers503 (3.75s)
|
||||
--- PASS: TestPostgresOutageAnswers503 (3.83s)
|
||||
--- PASS: TestHungPostgresAnswers503WithinTheRequestTimeout (5.34s)
|
||||
ok messenger/internal/app 30.321s
|
||||
--- PASS: TestLoginIssuesOpaqueTokenBackedByStoredSession (0.11s)
|
||||
--- PASS: TestLoginWithBadCredentialsCreatesNoSession (0.21s)
|
||||
--- PASS: TestAuthenticateResolvesTheOwner (0.15s)
|
||||
--- PASS: TestLogoutRevokesOnlyThatSession (0.14s)
|
||||
--- PASS: TestSessionExpiresAfterTTL (1.19s)
|
||||
ok messenger/internal/auth 15.854s
|
||||
ok messenger/internal/chat 12.655s [no tests to run]
|
||||
--- PASS: TestConcurrentAppendsKeepEveryMessage (0.17s)
|
||||
ok messenger/internal/chat/store 13.188s
|
||||
ok messenger/internal/platform/apperr 1.627s [no tests to run]
|
||||
ok messenger/internal/platform/config 1.627s [no tests to run]
|
||||
--- PASS: TestUnavailableCauseIsLoggedButNotReturned (0.03s)
|
||||
--- PASS: TestTimeoutBoundsTheRequestContext (0.05s)
|
||||
ok messenger/internal/platform/httpx 2.942s
|
||||
ok messenger/internal/platform/ident 1.656s [no tests to run]
|
||||
ok messenger/internal/platform/pagination 1.778s [no tests to run]
|
||||
ok messenger/internal/platform/password 1.641s [no tests to run]
|
||||
--- PASS: TestMigrateIsRepeatable (0.02s)
|
||||
--- PASS: TestPoolAppliesLimitsAndTimeouts (0.01s)
|
||||
--- PASS: TestClassifyMarksUnreachableDatabaseAsUnavailable (0.00s)
|
||||
--- PASS: TestClassifyRealConnectFailure (0.01s)
|
||||
ok messenger/internal/platform/postgres 9.771s
|
||||
ok messenger/internal/platform/valkey 2.782s [no tests to run]
|
||||
ok messenger/internal/server 3.156s [no tests to run]
|
||||
ok messenger/internal/testenv 2.767s [no tests to run]
|
||||
--- PASS: TestRegisterStoresUserWithPasswordHash (0.08s)
|
||||
--- PASS: TestRegisterRejectsInvalidInputAndStoresNothing (0.01s)
|
||||
--- PASS: TestRegisterReportsDuplicateAsConflict (0.14s)
|
||||
--- PASS: TestConcurrentRegistrationOfOneUsernameHasOneWinner (0.24s)
|
||||
--- PASS: TestExistingIDsReturnsOnlyRegistered (0.16s)
|
||||
--- PASS: TestAuthenticateChecksPassword (0.14s)
|
||||
--- PASS: TestAuthenticateFailuresLookTheSame (0.32s)
|
||||
ok messenger/internal/user 12.868s
|
||||
@@ -0,0 +1,107 @@
|
||||
module messenger
|
||||
|
||||
go 1.27.1
|
||||
|
||||
require (
|
||||
github.com/gin-gonic/gin v1.12.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/pressly/goose/v3 v3.27.0
|
||||
github.com/redis/go-redis/v9 v9.23.0
|
||||
github.com/testcontainers/testcontainers-go v0.39.0
|
||||
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0
|
||||
github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0
|
||||
golang.org/x/crypto v0.57.0
|
||||
)
|
||||
|
||||
require (
|
||||
dario.cat/mergo v1.0.2 // indirect
|
||||
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c // indirect
|
||||
github.com/Microsoft/go-winio v0.6.2 // indirect
|
||||
github.com/bytedance/gopkg v0.1.3 // indirect
|
||||
github.com/bytedance/sonic v1.15.0 // indirect
|
||||
github.com/bytedance/sonic/loader v0.5.0 // indirect
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/cloudwego/base64x v0.1.6 // indirect
|
||||
github.com/containerd/errdefs v1.0.0 // indirect
|
||||
github.com/containerd/errdefs/pkg v0.3.0 // indirect
|
||||
github.com/containerd/log v0.1.0 // indirect
|
||||
github.com/containerd/platforms v0.2.1 // indirect
|
||||
github.com/cpuguy83/dockercfg v0.3.2 // indirect
|
||||
github.com/creack/pty v1.1.24 // indirect
|
||||
github.com/distribution/reference v0.6.0 // indirect
|
||||
github.com/docker/docker v28.3.3+incompatible // indirect
|
||||
github.com/docker/go-connections v0.6.0 // indirect
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
github.com/ebitengine/purego v0.10.1 // indirect
|
||||
github.com/felixge/httpsnoop v1.1.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
|
||||
github.com/gin-contrib/sse v1.1.0 // indirect
|
||||
github.com/go-logr/logr v1.4.4 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-playground/validator/v10 v10.30.1 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/goccy/go-yaml v1.19.2 // indirect
|
||||
github.com/gogo/protobuf v1.3.2 // indirect
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/compress v1.19.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e // indirect
|
||||
github.com/magiconair/properties v1.8.10 // indirect
|
||||
github.com/mattn/go-isatty v0.0.24 // indirect
|
||||
github.com/mdelapenya/tlscert v0.2.0 // indirect
|
||||
github.com/mfridman/interpolate v0.0.2 // indirect
|
||||
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||
github.com/moby/go-archive v0.2.0 // indirect
|
||||
github.com/moby/patternmatcher v0.6.1 // indirect
|
||||
github.com/moby/sys/sequential v0.7.0 // indirect
|
||||
github.com/moby/sys/user v0.4.0 // indirect
|
||||
github.com/moby/sys/userns v0.1.0 // indirect
|
||||
github.com/moby/term v0.5.2 // indirect
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
|
||||
github.com/modern-go/reflect2 v1.0.2 // indirect
|
||||
github.com/morikuni/aec v1.0.0 // indirect
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/opencontainers/image-spec v1.1.1 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
|
||||
github.com/quic-go/qpack v0.6.0 // indirect
|
||||
github.com/quic-go/quic-go v0.59.0 // indirect
|
||||
github.com/sethvargo/go-retry v0.4.0 // indirect
|
||||
github.com/shirou/gopsutil/v4 v4.26.6 // indirect
|
||||
github.com/sirupsen/logrus v1.9.4 // indirect
|
||||
github.com/stretchr/testify v1.12.1 // indirect
|
||||
github.com/tklauser/go-sysconf v0.4.0 // indirect
|
||||
github.com/tklauser/numcpus v0.12.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.3.1 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 // indirect
|
||||
go.opentelemetry.io/otel v1.46.0 // indirect
|
||||
go.opentelemetry.io/otel/metric v1.46.0 // indirect
|
||||
go.opentelemetry.io/otel/trace v1.46.0 // indirect
|
||||
go.uber.org/atomic v1.12.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/arch v0.22.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba // indirect
|
||||
golang.org/x/net v0.59.0 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
golang.org/x/time v0.11.0 // indirect
|
||||
google.golang.org/grpc v1.84.0 // indirect
|
||||
google.golang.org/protobuf v1.36.12 // indirect
|
||||
)
|
||||
@@ -0,0 +1,304 @@
|
||||
dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
|
||||
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
|
||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk=
|
||||
github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c h1:udKWzYgxTojEKWjV8V+WSxDXJ4NFATAsZjh8iIbsQIg=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20250102033503-faa5f7b0171c/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
|
||||
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/bytedance/gopkg v0.1.3 h1:TPBSwH8RsouGCBcMBktLt1AymVo2TVsBVCY4b6TnZ/M=
|
||||
github.com/bytedance/gopkg v0.1.3/go.mod h1:576VvJ+eJgyCzdjS+c4+77QF3p7ubbtiKARP3TxducM=
|
||||
github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uSE=
|
||||
github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k=
|
||||
github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE=
|
||||
github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
|
||||
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
|
||||
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
|
||||
github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI=
|
||||
github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M=
|
||||
github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE=
|
||||
github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk=
|
||||
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
|
||||
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
|
||||
github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A=
|
||||
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
|
||||
github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA=
|
||||
github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
|
||||
github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s=
|
||||
github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE=
|
||||
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/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
|
||||
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||
github.com/docker/docker v28.3.3+incompatible h1:Dypm25kh4rmk49v1eiVbsAtpAsYURjYkaKubwuBdxEI=
|
||||
github.com/docker/docker v28.3.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/go-connections v0.6.0 h1:LlMG9azAe1TqfR7sO+NJttz1gy6KO7VJBh+pMmjSD94=
|
||||
github.com/docker/go-connections v0.6.0/go.mod h1:AahvXYshr6JgfUJGdDCs2b5EZG/vmaMAntpSFH5BFKE=
|
||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
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/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY=
|
||||
github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||
github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc=
|
||||
github.com/felixge/httpsnoop v1.1.0/go.mod h1:Zqxgdd+1Rkcz8euOqdr7lqgCRJztwr5hp9vDSi5UZCE=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw=
|
||||
github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s=
|
||||
github.com/gin-contrib/sse v1.1.0 h1:n0w2GMuUpWDVp7qSpvze6fAu9iRxJY4Hmj6AmBOU05w=
|
||||
github.com/gin-contrib/sse v1.1.0/go.mod h1:hxRZ5gVpWMT7Z0B0gSNYqqsSCNIJMjzvm6fqCz9vjwM=
|
||||
github.com/gin-gonic/gin v1.12.0 h1:b3YAbrZtnf8N//yjKeU2+MQsh2mY5htkZidOM7O0wG8=
|
||||
github.com/gin-gonic/gin v1.12.0/go.mod h1:VxccKfsSllpKshkBWgVgRniFFAzFb9csfngsqANjnLc=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
|
||||
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
|
||||
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy06ntQJp0BBvFG0w=
|
||||
github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-yaml v1.19.2 h1:PmFC1S6h8ljIz6gMRBopkjP1TVT7xuwrButHID66PoM=
|
||||
github.com/goccy/go-yaml v1.19.2/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0 h1:Bd7KaOxzULLxtZ/K5s1aLbWhR0+5RToO65TXHsf3bqQ=
|
||||
github.com/grpc-ecosystem/grpc-gateway/v2 v2.31.0/go.mod h1:nN7ts3dFXKtCZWc//yfkpcQNKJABg16/uDVAZpLDalo=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
|
||||
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
|
||||
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
|
||||
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
|
||||
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
||||
github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ=
|
||||
github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e h1:Q6MvJtQK/iRcRtzAscm/zF23XxJlbECiGPyRicsX+Ak=
|
||||
github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
|
||||
github.com/magiconair/properties v1.8.10 h1:s31yESBquKXCV9a/ScB3ESkOjUYYv+X0rg8SYxI99mE=
|
||||
github.com/magiconair/properties v1.8.10/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
|
||||
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
||||
github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A=
|
||||
github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI=
|
||||
github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o=
|
||||
github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY=
|
||||
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
|
||||
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||
github.com/moby/go-archive v0.2.0 h1:zg5QDUM2mi0JIM9fdQZWC7U8+2ZfixfTYoHL7rWUcP8=
|
||||
github.com/moby/go-archive v0.2.0/go.mod h1:mNeivT14o8xU+5q1YnNrkQVpK+dnNe/K6fHqnTg4qPU=
|
||||
github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U=
|
||||
github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
|
||||
github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w3A14Sw=
|
||||
github.com/moby/sys/atomicwriter v0.1.0/go.mod h1:Ul8oqv2ZMNHOceF643P6FKPXeCmYtlQMvpizfsSoaWs=
|
||||
github.com/moby/sys/sequential v0.7.0 h1:ASQNGNROJSuOO6LL6bPHbKvuZu6NU8P4ldPWk31zj/8=
|
||||
github.com/moby/sys/sequential v0.7.0/go.mod h1:NfSTAp6V3fw4tmkD62PEcOKeZKquXT8VKCkf7aVR79o=
|
||||
github.com/moby/sys/user v0.4.0 h1:jhcMKit7SA80hivmFJcbB1vqmw//wU61Zdui2eQXuMs=
|
||||
github.com/moby/sys/user v0.4.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs=
|
||||
github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g=
|
||||
github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
|
||||
github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ=
|
||||
github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
|
||||
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
|
||||
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
|
||||
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
|
||||
github.com/ncruces/go-strftime v1.0.0 h1:HMFp8mLCTPp341M/ZnA4qaf7ZlsbTc+miZjCLOFAw7w=
|
||||
github.com/ncruces/go-strftime v1.0.0/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||
github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040=
|
||||
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||
github.com/pressly/goose/v3 v3.27.0 h1:/D30gVTuQhu0WsNZYbJi4DMOsx1lNq+6SkLe+Wp59BM=
|
||||
github.com/pressly/goose/v3 v3.27.0/go.mod h1:3ZBeCXqzkgIRvrEMDkYh1guvtoJTU5oMMuDdkutoM78=
|
||||
github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
|
||||
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
|
||||
github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
|
||||
github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
|
||||
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
|
||||
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/sethvargo/go-retry v0.4.0 h1:9qy1OoIAxBL+gBYnkTnTnWle5wlfsXQlwRzIbbpdqPw=
|
||||
github.com/sethvargo/go-retry v0.4.0/go.mod h1:tvsjdKG6xfiCx4LSiUZ06kcv38xvdVQwv8R6/VnnVWg=
|
||||
github.com/shirou/gopsutil/v4 v4.26.6 h1:Mzr/npDtQC/xpeEuQKHZt8Zo9CmPvhTj8nkR8w5TLDs=
|
||||
github.com/shirou/gopsutil/v4 v4.26.6/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ=
|
||||
github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w=
|
||||
github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/objx v0.5.3 h1:jmXUvGomnU1o3W/V5h2VEradbpJDwGrzugQQvL0POH4=
|
||||
github.com/stretchr/objx v0.5.3/go.mod h1:rDQraq+vQZU7Fde9LOZLr8Tax6zZvy4kuNKF+QYS+U0=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||
github.com/testcontainers/testcontainers-go v0.39.0 h1:uCUJ5tA+fcxbFAB0uP3pIK3EJ2IjjDUHFSZ1H1UxAts=
|
||||
github.com/testcontainers/testcontainers-go v0.39.0/go.mod h1:qmHpkG7H5uPf/EvOORKvS6EuDkBUPE3zpVGaH9NL7f8=
|
||||
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0 h1:REJz+XwNpGC/dCgTfYvM4SKqobNqDBfvhq74s2oHTUM=
|
||||
github.com/testcontainers/testcontainers-go/modules/postgres v0.39.0/go.mod h1:4K2OhtHEeT+JSIFX4V8DkGKsyLa96Y2vLdd3xsxD5HE=
|
||||
github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0 h1:b5BwugyCMHrFfZYCSFKY9IHMhoWtR+REcN2ddcCQv6g=
|
||||
github.com/testcontainers/testcontainers-go/modules/valkey v0.39.0/go.mod h1:lgNiE/W5RPjOu1S8fZ9Z9e63JU8wyaxNQ43Umv6kroQ=
|
||||
github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU=
|
||||
github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI=
|
||||
github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4=
|
||||
github.com/tklauser/numcpus v0.12.0/go.mod h1:ABHeXzJnr/qqwguhClkZKT1/8VABcYrsyUiUGobwWJg=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY=
|
||||
github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
|
||||
github.com/valkey-io/valkey-go v1.0.59 h1:W67Z0UY+Qqk3k8NKkFCFlM3X4yQUniixl7dSJAch2Qo=
|
||||
github.com/valkey-io/valkey-go v1.0.59/go.mod h1:bHmwjIEOrGq/ubOJfh5uMRs7Xj6mV3mQ/ZXUbmqpjqY=
|
||||
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
|
||||
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
|
||||
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
|
||||
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0 h1:3g7B90UzBltIDKq1/5mrTGxTnOFDV0ICOhLoxiZ8jlg=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.71.0/go.mod h1:Ef8SuTh59BT7+ofpDxN9z+yOlc4t2GjLmKDgYNJL/NU=
|
||||
go.opentelemetry.io/otel v1.46.0 h1:FHt5/CDyVxi/8IM1CH7VE/rRgq3kLHa2mSTVMO8AWyc=
|
||||
go.opentelemetry.io/otel v1.46.0/go.mod h1:Gj3SEScelsNC45tp4nSxRYlS+f5iez7W8XPMCt905kE=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 h1:Mne5On7VWdx7omSrSSZvM4Kw7cS7NQkOOmLcgscI51U=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0/go.mod h1:IPtUMKL4O3tH5y+iXVyAXqpAwMuzC1IrxVS81rummfE=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0 h1:IeMeyr1aBvBiPVYihXIaeIZba6b8E1bYp7lbdxK8CQg=
|
||||
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.19.0/go.mod h1:oVdCUtjq9MK9BlS7TtucsQwUcXcymNiEDjgDD2jMtZU=
|
||||
go.opentelemetry.io/otel/metric v1.46.0 h1:yBnkXvgV7AXFILZc5K6IZe/CBFF3OS7BJ8ov6/lj0K8=
|
||||
go.opentelemetry.io/otel/metric v1.46.0/go.mod h1:iPmdWqifKUdzziPkvvzIJXITl56fQx2mGM/DHLB3/2o=
|
||||
go.opentelemetry.io/otel/sdk v1.46.0 h1:h5CNQQjEbuQXY/JfZtgt3i7HVFV3aHPO2OAwO2eTYPI=
|
||||
go.opentelemetry.io/otel/sdk v1.46.0/go.mod h1:GAERFXFt5SYCEB+YiKUbMBeza6UaDH7GmGOZEfh2gSM=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.46.0 h1:0piZ26EG4RBfebb2jhDH6ERCYHoVWduc3kLgPCwSnSE=
|
||||
go.opentelemetry.io/otel/sdk/metric v1.46.0/go.mod h1:I1PbKrdVc8Qu8HYVDNtqVIwLwjNrhsV/uFuxfwg8mO4=
|
||||
go.opentelemetry.io/otel/trace v1.46.0 h1:OULy7ccdJnZtJ0UDYFOIGaCmiWzJ8Vi2G/Rsu60qs1c=
|
||||
go.opentelemetry.io/otel/trace v1.46.0/go.mod h1:J7GAXweO77XSFkB/rmAqk9D6ihszhFjLU+d9WuUxDLI=
|
||||
go.opentelemetry.io/proto/otlp v1.0.0 h1:T0TX0tmXU8a3CbNXzEKGeU5mIVOdf0oykP+u2lIVU/I=
|
||||
go.opentelemetry.io/proto/otlp v1.0.0/go.mod h1:Sy6pihPLfYHkr3NkUbEhGHFhINUSI/v80hjKIs5JXpM=
|
||||
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
|
||||
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
|
||||
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
|
||||
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
|
||||
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/arch v0.22.0 h1:c/Zle32i5ttqRXjdLyyHZESLD/bB90DCU1g9l/0YBDI=
|
||||
golang.org/x/arch v0.22.0/go.mod h1:dNHoOeKiyja7GTvF9NJS1l3Z2yntpQNzgrjh1cU103A=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
|
||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba h1:Ck8QetSgk912qxWLMCKxd0in+aiyBQyDSMae6e/xmpU=
|
||||
golang.org/x/exp v0.0.0-20260908205506-85c1c2202aba/go.mod h1:50RgIsmK7OwqzTTeqcSXQW8SswW0o8fRcDxmqGluJ8E=
|
||||
golang.org/x/mod v0.2.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
|
||||
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
|
||||
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
|
||||
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
||||
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
|
||||
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
|
||||
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459 h1:GS9OIt/j7c8bvBjYNgnKQysVfmV7e4jM0H8ZK95G4t8=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260921155816-b14227669459/go.mod h1:PX5/4vemwVoXtwEcRDWwcR1/r0qrosfx3qoVADMwnVE=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260918162117-cecb64721679 h1:KmqdJU4vrNcxy/6qdg3JduZtalEXrJLspVltnR1cE+8=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260918162117-cecb64721679/go.mod h1:OaIUM3+LpYcK2GXM4FTmhWoIq371Owdr+Cc7/BsYHHc=
|
||||
google.golang.org/grpc v1.84.0 h1:soMyaPJ8pAak5PIQ0DGBUir0XRo2fRoMqhNWMLlLxO0=
|
||||
google.golang.org/grpc v1.84.0/go.mod h1:ljCht0DrxQrXBDRTZp52Qxh3Ffk8CdYm2sj4O2QN2C0=
|
||||
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
||||
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q=
|
||||
gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA=
|
||||
modernc.org/libc v1.68.0 h1:PJ5ikFOV5pwpW+VqCK1hKJuEWsonkIJhhIXyuF/91pQ=
|
||||
modernc.org/libc v1.68.0/go.mod h1:NnKCYeoYgsEqnY3PgvNgAeaJnso968ygU8Z0DxjoEc0=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/sqlite v1.46.1 h1:eFJ2ShBLIEnUWlLy12raN0Z1plqmFX9Qe3rjQTKt6sU=
|
||||
modernc.org/sqlite v1.46.1/go.mod h1:CzbrU2lSB1DKUusvwGz7rqEKIq+NUd8GWuBBZDs9/nA=
|
||||
@@ -0,0 +1,19 @@
|
||||
# Valkey is the only store of user sessions, so it is their source of truth.
|
||||
# Every write is fsynced before the reply: an acknowledged login survives a
|
||||
# crash or a restart. Only logins and logouts write; session lookups are reads
|
||||
# and pay nothing for this.
|
||||
appendonly yes
|
||||
appendfsync always
|
||||
dir /data
|
||||
|
||||
# The append-only file already persists everything; RDB snapshots are off.
|
||||
save ""
|
||||
|
||||
# Sessions are never evicted to free memory: once maxmemory is reached the
|
||||
# store refuses new sessions (login answers 503) instead of silently logging
|
||||
# users out. A session key takes about a hundred bytes.
|
||||
maxmemory 256mb
|
||||
maxmemory-policy noeviction
|
||||
|
||||
# requirepass comes from the compose secret, never from the repository.
|
||||
include /run/secrets/valkey_auth
|
||||
@@ -0,0 +1,70 @@
|
||||
// Package app is the composition root: it builds every module with its
|
||||
// stores and mounts them on one HTTP engine. Both the server binary and the
|
||||
// integration tests start from here.
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"messenger/internal/auth"
|
||||
authstore "messenger/internal/auth/store"
|
||||
"messenger/internal/chat"
|
||||
chatstore "messenger/internal/chat/store"
|
||||
"messenger/internal/health"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/user"
|
||||
userstore "messenger/internal/user/store"
|
||||
)
|
||||
|
||||
type App struct {
|
||||
Engine *gin.Engine
|
||||
Health *health.Handler
|
||||
}
|
||||
|
||||
// Deps are the shared resources the modules are built on; their lifetime is
|
||||
// owned by the caller.
|
||||
type Deps struct {
|
||||
Log *slog.Logger
|
||||
DB *pgxpool.Pool
|
||||
Sessions *redis.Client
|
||||
SessionTTL time.Duration
|
||||
MaxBodyBytes int64
|
||||
// RequestTimeout bounds every API request; zero means the default.
|
||||
RequestTimeout time.Duration
|
||||
}
|
||||
|
||||
// defaultRequestTimeout stays below the server's 10s write timeout, so a
|
||||
// request that gives up on a dependency can still write its 503.
|
||||
const defaultRequestTimeout = 5 * time.Second
|
||||
|
||||
func New(d Deps) *App {
|
||||
users := user.NewService(userstore.NewUsers(d.DB))
|
||||
chats := chat.NewService(chatstore.NewChats(d.DB), chatstore.NewMessages(d.DB), users)
|
||||
|
||||
engine := httpx.NewEngine(d.Log, d.MaxBodyBytes)
|
||||
probes := health.New(
|
||||
health.Check{Name: "postgres", Probe: d.DB.Ping},
|
||||
health.Check{Name: "valkey", Probe: func(ctx context.Context) error { return d.Sessions.Ping(ctx).Err() }},
|
||||
)
|
||||
probes.Register(engine)
|
||||
|
||||
sessions := auth.NewService(users, authstore.NewSessions(d.Sessions), d.SessionTTL)
|
||||
|
||||
timeout := d.RequestTimeout
|
||||
if timeout == 0 {
|
||||
timeout = defaultRequestTimeout
|
||||
}
|
||||
public := engine.Group("/api/v1", httpx.Timeout(timeout))
|
||||
protected := public.Group("", auth.RequireSession(sessions))
|
||||
auth.NewHandler(sessions).Register(public, protected)
|
||||
user.NewHandler(users).Register(public, protected)
|
||||
chat.NewHandler(chats).Register(protected)
|
||||
|
||||
return &App{Engine: engine, Health: probes}
|
||||
}
|
||||
@@ -0,0 +1,510 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/pagination"
|
||||
"messenger/internal/testenv"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
var (
|
||||
db *testenv.DB
|
||||
kv *testenv.KV
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if db, err = testenv.StartPostgres(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if kv, err = testenv.StartValkey(ctx); err != nil {
|
||||
db.Close()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
kv.Close()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
const testPassword = "correct-horse-battery"
|
||||
|
||||
// errorBody mirrors the contract error envelope.
|
||||
type errorBody struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
type response struct {
|
||||
status int
|
||||
requestID string
|
||||
challenge string
|
||||
raw []byte
|
||||
}
|
||||
|
||||
// tester drives the assembled application in-process.
|
||||
type tester struct {
|
||||
t *testing.T
|
||||
a *app.App
|
||||
}
|
||||
|
||||
// account is a registered user with a live session.
|
||||
type account struct {
|
||||
user.Response
|
||||
token string
|
||||
}
|
||||
|
||||
// sessionBody mirrors the contract Session schema.
|
||||
type sessionBody struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User user.Response `json:"user"`
|
||||
}
|
||||
|
||||
func newTester(t *testing.T) *tester {
|
||||
t.Helper()
|
||||
return newTesterWithTTL(t, time.Hour)
|
||||
}
|
||||
|
||||
// newTesterWithTTL assembles the application over empty stores.
|
||||
func newTesterWithTTL(t *testing.T, ttl time.Duration) *tester {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
kv.Reset(t)
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
return &tester{t: t, a: app.New(app.Deps{
|
||||
Log: log,
|
||||
DB: db.Pool,
|
||||
Sessions: kv.Client,
|
||||
SessionTTL: ttl,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
})}
|
||||
}
|
||||
|
||||
// do sends one request, authenticated with the bearer token when it is set.
|
||||
// A string body is sent verbatim, anything else is JSON-encoded; both get a
|
||||
// JSON content type. Every response is checked for X-Request-Id, error
|
||||
// bodies for the contract envelope, and every 401 for the challenge of the
|
||||
// scheme the route expects.
|
||||
func (ts *tester) do(method, path, token string, body any, headers map[string]string) response {
|
||||
ts.t.Helper()
|
||||
req := httptest.NewRequest(method, path, encode(ts.t, body))
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
ts.a.Engine.ServeHTTP(rec, req)
|
||||
|
||||
res := response{
|
||||
status: rec.Code,
|
||||
requestID: rec.Header().Get("X-Request-Id"),
|
||||
challenge: rec.Header().Get("WWW-Authenticate"),
|
||||
raw: rec.Body.Bytes(),
|
||||
}
|
||||
if res.requestID == "" {
|
||||
ts.t.Fatalf("%s %s: missing X-Request-Id", method, path)
|
||||
}
|
||||
if res.status >= 400 {
|
||||
var eb errorBody
|
||||
if err := json.Unmarshal(res.raw, &eb); err != nil {
|
||||
ts.t.Fatalf("%s %s: error body is not the contract envelope: %v: %s", method, path, err, res.raw)
|
||||
}
|
||||
if eb.Error.RequestID != res.requestID || eb.Error.Code == "" || eb.Error.Message == "" {
|
||||
ts.t.Fatalf("%s %s: bad error envelope: %s", method, path, res.raw)
|
||||
}
|
||||
}
|
||||
if res.status == http.StatusUnauthorized {
|
||||
scheme := "Bearer "
|
||||
if path == "/api/v1/auth/sessions" {
|
||||
scheme = "Basic "
|
||||
}
|
||||
if !strings.HasPrefix(res.challenge, scheme) {
|
||||
ts.t.Fatalf("%s %s: 401 challenge = %q, want scheme %q", method, path, res.challenge, scheme)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
func encode(t *testing.T, body any) io.Reader {
|
||||
t.Helper()
|
||||
switch b := body.(type) {
|
||||
case nil:
|
||||
return nil
|
||||
case string:
|
||||
return strings.NewReader(b)
|
||||
default:
|
||||
raw, err := json.Marshal(b)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return bytes.NewReader(raw)
|
||||
}
|
||||
}
|
||||
|
||||
// expect asserts the status and, for errors, the contract code.
|
||||
func (ts *tester) expect(res response, status int, code string) {
|
||||
ts.t.Helper()
|
||||
if res.status != status {
|
||||
ts.t.Fatalf("status = %d, want %d; body %s", res.status, status, res.raw)
|
||||
}
|
||||
if code != "" {
|
||||
var eb errorBody
|
||||
_ = json.Unmarshal(res.raw, &eb)
|
||||
if eb.Error.Code != code {
|
||||
ts.t.Fatalf("code = %q, want %q; body %s", eb.Error.Code, code, res.raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// decode parses a success body into a typed DTO and rejects unknown fields,
|
||||
// which enforces the additionalProperties=false rule of the contract.
|
||||
func decode[T any](t *testing.T, res response) T {
|
||||
t.Helper()
|
||||
var out T
|
||||
dec := json.NewDecoder(bytes.NewReader(res.raw))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(&out); err != nil {
|
||||
t.Fatalf("body does not match %T: %v: %s", out, err, res.raw)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// user registers a user and logs it in.
|
||||
func (ts *tester) user(username, display string) account {
|
||||
ts.t.Helper()
|
||||
body := map[string]string{"username": username, "display_name": display, "password": testPassword}
|
||||
res := ts.do(http.MethodPost, "/api/v1/users", "", body, nil)
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
registered := decode[user.Response](ts.t, res)
|
||||
return account{Response: registered, token: ts.login(username, testPassword).Token}
|
||||
}
|
||||
|
||||
func basic(username, password string) map[string]string {
|
||||
raw := base64.StdEncoding.EncodeToString([]byte(username + ":" + password))
|
||||
return map[string]string{"Authorization": "Basic " + raw}
|
||||
}
|
||||
|
||||
func (ts *tester) login(username, password string) sessionBody {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic(username, password))
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
return decode[sessionBody](ts.t, res)
|
||||
}
|
||||
|
||||
func (ts *tester) chat(token, title string, members ...string) chat.ChatResponse {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats", token, map[string]any{"title": title, "member_ids": members}, nil)
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
return decode[chat.ChatResponse](ts.t, res)
|
||||
}
|
||||
|
||||
func (ts *tester) message(token, chatID, text string) chat.MessageResponse {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats/"+chatID+"/messages", token, map[string]string{"text": text}, nil)
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
return decode[chat.MessageResponse](ts.t, res)
|
||||
}
|
||||
|
||||
func (ts *tester) chats(token, query string) pagination.PageResponse[chat.ChatResponse] {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodGet, "/api/v1/chats"+query, token, nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
return decode[pagination.PageResponse[chat.ChatResponse]](ts.t, res)
|
||||
}
|
||||
|
||||
func (ts *tester) history(token, chatID, query string) pagination.PageResponse[chat.MessageResponse] {
|
||||
ts.t.Helper()
|
||||
res := ts.do(http.MethodGet, "/api/v1/chats/"+chatID+"/messages"+query, token, nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
return decode[pagination.PageResponse[chat.MessageResponse]](ts.t, res)
|
||||
}
|
||||
|
||||
func cursorQuery(limit int, cursor *string) string {
|
||||
q := fmt.Sprintf("?limit=%d", limit)
|
||||
if cursor != nil {
|
||||
q += "&cursor=" + url.QueryEscape(*cursor)
|
||||
}
|
||||
return q
|
||||
}
|
||||
|
||||
func TestHealthEndpoints(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
for _, path := range []string{"/health/live", "/health/ready"} {
|
||||
res := ts.do(http.MethodGet, path, "", nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
if strings.TrimSpace(string(res.raw)) != `{"status":"ok"}` {
|
||||
t.Fatalf("%s body = %s", path, res.raw)
|
||||
}
|
||||
}
|
||||
ts.a.Health.SetReady(false)
|
||||
ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/health/live", "", nil, nil), http.StatusOK, "")
|
||||
}
|
||||
|
||||
func TestRegisterUserShapeAndConflicts(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
if alice.ID == "" || alice.Username != "alice" || alice.DisplayName != "Алиса" {
|
||||
t.Fatalf("user = %+v", alice)
|
||||
}
|
||||
if !strings.HasSuffix(alice.CreatedAt, "Z") || len(alice.CreatedAt) != len("2026-09-01T12:00:00.000Z") {
|
||||
t.Fatalf("created_at = %q", alice.CreatedAt)
|
||||
}
|
||||
post := func(body any) response { return ts.do(http.MethodPost, "/api/v1/users", "", body, nil) }
|
||||
ts.expect(post(map[string]string{"username": "alice", "display_name": "Other", "password": testPassword}), http.StatusConflict, "conflict")
|
||||
ts.expect(post(map[string]string{"username": "bob", "display_name": "Bob"}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(map[string]string{"username": "Alice", "display_name": "x"}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(map[string]string{"username": "bob", "display_name": " "}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(map[string]any{"username": "bob", "display_name": "x", "password": "no"}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(`{"username":`), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(map[string]any{"username": 5, "display_name": "x"}), http.StatusBadRequest, "invalid_request")
|
||||
}
|
||||
|
||||
func TestCurrentUserIsTheTokenOwner(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
ts.user("bob", "Боб")
|
||||
res := ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
if got := decode[user.Response](t, res); got != alice.Response {
|
||||
t.Fatalf("me = %+v, want %+v", got, alice.Response)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCreationRules(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, bob := ts.user("alice", "A"), ts.user("bob", "B")
|
||||
created := ts.chat(alice.token, "Проект по ВНП", alice.ID, bob.ID)
|
||||
if created.Title != "Проект по ВНП" || len(created.MemberIDs) != 2 || created.MemberIDs[0] != alice.ID || created.MemberIDs[1] != bob.ID {
|
||||
t.Fatalf("chat = %+v", created)
|
||||
}
|
||||
unknown := "22222222-2222-4222-8222-222222222222"
|
||||
invalid := map[string]any{
|
||||
"creator missing": map[string]any{"title": "t", "member_ids": []string{bob.ID}},
|
||||
"duplicate": map[string]any{"title": "t", "member_ids": []string{alice.ID, alice.ID}},
|
||||
"unknown member": map[string]any{"title": "t", "member_ids": []string{alice.ID, unknown}},
|
||||
"malformed uuid": map[string]any{"title": "t", "member_ids": []string{alice.ID, "not-a-uuid"}},
|
||||
"empty members": map[string]any{"title": "t", "member_ids": []string{}},
|
||||
"blank title": map[string]any{"title": "", "member_ids": []string{alice.ID}},
|
||||
"long title": map[string]any{"title": strings.Repeat("я", 101), "member_ids": []string{alice.ID}},
|
||||
"missing members": map[string]any{"title": "t"},
|
||||
"extra field": map[string]any{"title": "t", "member_ids": []string{alice.ID}, "owner": alice.ID},
|
||||
}
|
||||
for name, body := range invalid {
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats", alice.token, body, nil)
|
||||
if res.status != http.StatusBadRequest {
|
||||
t.Errorf("%s: status = %d, want 400; body %s", name, res.status, res.raw)
|
||||
}
|
||||
}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/chats", "", map[string]any{"title": "t", "member_ids": []string{alice.ID}}, nil), http.StatusUnauthorized, "unauthorized")
|
||||
}
|
||||
|
||||
func TestChatVisibilityAndListing(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E")
|
||||
created := ts.chat(alice.token, "Контрактный тест", alice.ID, bob.ID)
|
||||
path := "/api/v1/chats/" + created.ID
|
||||
|
||||
res := ts.do(http.MethodGet, path, bob.token, nil, nil)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
if got := decode[chat.ChatResponse](t, res); got.ID != created.ID || len(got.MemberIDs) != 2 {
|
||||
t.Fatalf("GET chat = %+v", got)
|
||||
}
|
||||
ts.expect(ts.do(http.MethodGet, path, eve.token, nil, nil), http.StatusNotFound, "not_found")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats/33333333-3333-4333-8333-333333333333", alice.token, nil, nil), http.StatusNotFound, "not_found")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats/not-a-uuid", alice.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(ts.do(http.MethodGet, path, "", nil, nil), http.StatusUnauthorized, "unauthorized")
|
||||
|
||||
listed := ts.chats(bob.token, "?limit=100")
|
||||
if len(listed.Items) != 1 || listed.Items[0].ID != created.ID || listed.NextCursor != nil {
|
||||
t.Fatalf("bob list = %+v", listed)
|
||||
}
|
||||
hidden := ts.do(http.MethodGet, "/api/v1/chats?limit=100", eve.token, nil, nil)
|
||||
ts.expect(hidden, http.StatusOK, "")
|
||||
if !strings.Contains(string(hidden.raw), `"items":[]`) || !strings.Contains(string(hidden.raw), `"next_cursor":null`) {
|
||||
t.Fatalf("empty list must serialize items as [] and next_cursor as null: %s", hidden.raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatListPagination(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
var expected []string
|
||||
for i := 0; i < 5; i++ {
|
||||
expected = append(expected, ts.chat(alice.token, fmt.Sprintf("chat %d", i), alice.ID).ID)
|
||||
}
|
||||
var walked []string
|
||||
var cursor *string
|
||||
for pages := 0; pages < 10; pages++ {
|
||||
page := ts.chats(alice.token, cursorQuery(2, cursor))
|
||||
for _, c := range page.Items {
|
||||
walked = append(walked, c.ID)
|
||||
}
|
||||
if page.NextCursor == nil {
|
||||
break
|
||||
}
|
||||
cursor = page.NextCursor
|
||||
}
|
||||
if strings.Join(walked, ",") != strings.Join(expected, ",") {
|
||||
t.Fatalf("walked %v, want %v", walked, expected)
|
||||
}
|
||||
|
||||
bob := ts.user("bob", "B")
|
||||
foreign := ts.chats(alice.token, "?limit=1").NextCursor
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats"+cursorQuery(50, foreign), bob.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats?cursor=", alice.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats?limit=abc", alice.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
}
|
||||
|
||||
func TestMessagesEndToEnd(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, bob, eve := ts.user("alice", "A"), ts.user("bob", "B"), ts.user("eve", "E")
|
||||
room := ts.chat(alice.token, "Контрактный тест", alice.ID, bob.ID)
|
||||
path := "/api/v1/chats/" + room.ID + "/messages"
|
||||
|
||||
first := ts.message(alice.token, room.ID, "Привет, мир 👋")
|
||||
second := ts.message(bob.token, room.ID, "Сообщение Боба")
|
||||
if first.SenderID != alice.ID || second.SenderID != bob.ID {
|
||||
t.Fatalf("sender must come from the bearer token: %+v %+v", first, second)
|
||||
}
|
||||
if first.ChatID != room.ID || first.Text != "Привет, мир 👋" {
|
||||
t.Fatalf("message = %+v", first)
|
||||
}
|
||||
|
||||
page1 := ts.history(bob.token, room.ID, "?limit=1")
|
||||
if len(page1.Items) != 1 || page1.NextCursor == nil || page1.Items[0] != first {
|
||||
t.Fatalf("page1 = %+v", page1)
|
||||
}
|
||||
page2 := ts.history(alice.token, room.ID, cursorQuery(1, page1.NextCursor))
|
||||
if len(page2.Items) != 1 || page2.NextCursor != nil || page2.Items[0] != second {
|
||||
t.Fatalf("page2 = %+v", page2)
|
||||
}
|
||||
|
||||
post := func(actor string, body any) response { return ts.do(http.MethodPost, path, actor, body, nil) }
|
||||
ts.expect(post(alice.token, map[string]string{"text": ""}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, map[string]string{"text": " "}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, map[string]string{"text": strings.Repeat("я", 4001)}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, map[string]string{"text": "x", "sender_id": bob.ID}), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(alice.token, `{"text":`), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(post(eve.token, map[string]string{"text": "чужое"}), http.StatusNotFound, "not_found")
|
||||
ts.expect(post(eve.token, map[string]string{"text": ""}), http.StatusNotFound, "not_found")
|
||||
ts.expect(post("", map[string]string{"text": "x"}), http.StatusUnauthorized, "unauthorized")
|
||||
ts.expect(post(alice.token, map[string]string{"text": strings.Repeat("я", 4000)}), http.StatusCreated, "")
|
||||
|
||||
get := func(actor, query string) response { return ts.do(http.MethodGet, path+query, actor, nil, nil) }
|
||||
ts.expect(get(eve.token, ""), http.StatusNotFound, "not_found")
|
||||
ts.expect(get(alice.token, "?limit=0"), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(get(alice.token, "?limit=101"), http.StatusBadRequest, "invalid_request")
|
||||
ts.expect(get(alice.token, "?cursor=garbage"), http.StatusBadRequest, "invalid_request")
|
||||
|
||||
other := ts.chat(alice.token, "другой", alice.ID)
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+other.ID+"/messages"+cursorQuery(50, page1.NextCursor), alice.token, nil, nil), http.StatusBadRequest, "invalid_request")
|
||||
}
|
||||
|
||||
func TestMessageHistoryWalkAcrossPages(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
room := ts.chat(alice.token, "t", alice.ID)
|
||||
var expected []string
|
||||
for i := 0; i < 23; i++ {
|
||||
expected = append(expected, ts.message(alice.token, room.ID, fmt.Sprintf("m%d", i)).ID)
|
||||
}
|
||||
var walked []string
|
||||
var cursor *string
|
||||
for {
|
||||
page := ts.history(alice.token, room.ID, cursorQuery(5, cursor))
|
||||
for _, m := range page.Items {
|
||||
walked = append(walked, m.ID)
|
||||
}
|
||||
if page.NextCursor == nil {
|
||||
break
|
||||
}
|
||||
cursor = page.NextCursor
|
||||
}
|
||||
if strings.Join(walked, ",") != strings.Join(expected, ",") {
|
||||
t.Fatalf("walk mismatch:\n%v\n%v", walked, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentSendsProduceExactlyUniqueMessages(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
const clients, perClient = 10, 10
|
||||
clientsList := make([]account, 0, clients)
|
||||
members := make([]string, 0, clients)
|
||||
for i := 0; i < clients; i++ {
|
||||
c := ts.user(fmt.Sprintf("client_%d", i), "C")
|
||||
clientsList = append(clientsList, c)
|
||||
members = append(members, c.ID)
|
||||
}
|
||||
room := ts.chat(clientsList[0].token, "нагрузка", members...)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
for n, c := range clientsList {
|
||||
wg.Go(func() {
|
||||
for i := 0; i < perClient; i++ {
|
||||
res := ts.do(http.MethodPost, "/api/v1/chats/"+room.ID+"/messages", c.token, map[string]string{"text": fmt.Sprintf("c%d-m%d", n, i)}, nil)
|
||||
if res.status != http.StatusCreated {
|
||||
t.Errorf("client %d: status %d body %s", n, res.status, res.raw)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
page := ts.history(clientsList[0].token, room.ID, "?limit=100")
|
||||
if len(page.Items) != clients*perClient || page.NextCursor != nil {
|
||||
t.Fatalf("got %d messages, next=%v; want %d and null", len(page.Items), page.NextCursor, clients*perClient)
|
||||
}
|
||||
ids := make([]string, 0, len(page.Items))
|
||||
unique := make(map[string]struct{}, len(page.Items))
|
||||
for _, m := range page.Items {
|
||||
ids = append(ids, m.ID)
|
||||
unique[m.ID] = struct{}{}
|
||||
}
|
||||
if !sort.StringsAreSorted(ids) {
|
||||
t.Fatal("history is not ordered by id")
|
||||
}
|
||||
if len(unique) != clients*perClient {
|
||||
t.Fatalf("%d unique ids, want %d", len(unique), clients*perClient)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownRouteAndMethodAreContractErrors(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/nothing", "", nil, nil), http.StatusNotFound, "not_found")
|
||||
ts.expect(ts.do(http.MethodDelete, "/api/v1/users", "", nil, nil), http.StatusMethodNotAllowed, "invalid_request")
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", "{}", map[string]string{"Content-Type": "text/plain"}), http.StatusUnsupportedMediaType, "unsupported_media_type")
|
||||
big := map[string]string{"username": "big", "display_name": strings.Repeat("x", 70*1024)}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", big, nil), http.StatusRequestEntityTooLarge, "payload_too_large")
|
||||
}
|
||||
@@ -0,0 +1,162 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
const (
|
||||
basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
bearerChallenge = `Bearer realm="messenger"`
|
||||
)
|
||||
|
||||
func TestLoginReturnsBearerSession(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
before := time.Now()
|
||||
|
||||
res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword))
|
||||
ts.expect(res, http.StatusCreated, "")
|
||||
s := decode[sessionBody](t, res)
|
||||
|
||||
if s.TokenType != "Bearer" || len(s.Token) != 43 || s.Token == alice.token {
|
||||
t.Fatalf("session = %+v; want a fresh 43-character Bearer token", s)
|
||||
}
|
||||
if s.User != alice.Response {
|
||||
t.Fatalf("user = %+v, want %+v", s.User, alice.Response)
|
||||
}
|
||||
expires, err := time.Parse(time.RFC3339, s.ExpiresAt)
|
||||
if err != nil || !strings.HasSuffix(s.ExpiresAt, "Z") {
|
||||
t.Fatalf("expires_at = %q: %v", s.ExpiresAt, err)
|
||||
}
|
||||
if ttl := expires.Sub(before); ttl < 59*time.Minute || ttl > 61*time.Minute {
|
||||
t.Fatalf("expires_at is %v after login, want the 1h TTL", ttl)
|
||||
}
|
||||
if strings.Contains(string(res.raw), "password") || strings.Contains(string(res.raw), "argon2") {
|
||||
t.Fatalf("session response leaks password data: %s", res.raw)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginFailuresAreUniformBasicChallenges(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
ts.user("alice", "Алиса")
|
||||
encoded := func(s string) string { return base64.StdEncoding.EncodeToString([]byte(s)) }
|
||||
cases := map[string]map[string]string{
|
||||
"wrong password": basic("alice", testPassword+"!"),
|
||||
"unknown user": basic("mallory", testPassword),
|
||||
"no header": nil,
|
||||
"bearer scheme": {"Authorization": "Bearer " + strings.Repeat("A", 43)},
|
||||
"not base64": {"Authorization": "Basic !!!"},
|
||||
"no colon": {"Authorization": "Basic " + encoded("alice")},
|
||||
"nul username": basic("a\x00b", testPassword),
|
||||
"non-utf-8 name": basic("\xff\xfe", testPassword),
|
||||
}
|
||||
messages := map[string]string{}
|
||||
for name, headers := range cases {
|
||||
res := ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, headers)
|
||||
ts.expect(res, http.StatusUnauthorized, "unauthorized")
|
||||
if res.challenge != basicChallenge {
|
||||
t.Errorf("%s: WWW-Authenticate = %q, want %q", name, res.challenge, basicChallenge)
|
||||
}
|
||||
var eb errorBody
|
||||
_ = json.Unmarshal(res.raw, &eb)
|
||||
messages[name] = eb.Error.Message
|
||||
}
|
||||
if messages["wrong password"] != messages["unknown user"] {
|
||||
t.Fatalf("wrong password and unknown user must look the same: %q vs %q", messages["wrong password"], messages["unknown user"])
|
||||
}
|
||||
}
|
||||
|
||||
// The password is UTF-8 and may contain colons; only the first colon of the
|
||||
// Basic credentials separates the username (contracts/README.md).
|
||||
func TestLoginAcceptsColonAndUnicodePasswords(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
const password = "пароль:с:двоеточием 👋"
|
||||
body := map[string]string{"username": "alice", "display_name": "Алиса", "password": password}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusCreated, "")
|
||||
s := ts.login("alice", password)
|
||||
if s.User.Username != "alice" {
|
||||
t.Fatalf("logged in as %+v", s.User)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProtectedRoutesAcceptOnlyBearer(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice, eve := ts.user("alice", "A"), ts.user("eve", "E")
|
||||
me := func(headers map[string]string) response {
|
||||
return ts.do(http.MethodGet, "/api/v1/users/me", "", nil, headers)
|
||||
}
|
||||
rejected := map[string]map[string]string{
|
||||
"no header": nil,
|
||||
"basic with valid credentials": basic("alice", testPassword),
|
||||
"x-user-id alone": {"X-User-Id": alice.ID},
|
||||
"unknown token": {"Authorization": "Bearer " + strings.Repeat("A", 43)},
|
||||
"empty token": {"Authorization": "Bearer "},
|
||||
"scheme only": {"Authorization": "Bearer"},
|
||||
"other scheme": {"Authorization": "Token " + alice.token},
|
||||
"two tokens": {"Authorization": "Bearer " + alice.token + " " + eve.token},
|
||||
}
|
||||
for name, headers := range rejected {
|
||||
res := me(headers)
|
||||
ts.expect(res, http.StatusUnauthorized, "unauthorized")
|
||||
if res.challenge != bearerChallenge {
|
||||
t.Errorf("%s: WWW-Authenticate = %q, want %q", name, res.challenge, bearerChallenge)
|
||||
}
|
||||
}
|
||||
accepted := map[string]map[string]string{
|
||||
"lowercase scheme": {"Authorization": "bearer " + alice.token},
|
||||
"several spaces": {"Authorization": "Bearer " + alice.token},
|
||||
"x-user-id is ignored": {"Authorization": "Bearer " + alice.token, "X-User-Id": eve.ID},
|
||||
}
|
||||
for name, headers := range accepted {
|
||||
res := me(headers)
|
||||
ts.expect(res, http.StatusOK, "")
|
||||
if got := decode[user.Response](t, res); got.ID != alice.ID {
|
||||
t.Errorf("%s: caller = %s, want alice", name, got.ID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogoutRevokesOnlyTheCurrentToken(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
alice := ts.user("alice", "A")
|
||||
other := ts.login("alice", testPassword)
|
||||
|
||||
res := ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil)
|
||||
ts.expect(res, http.StatusNoContent, "")
|
||||
if len(res.raw) != 0 {
|
||||
t.Fatalf("204 must have no body, got %q", res.raw)
|
||||
}
|
||||
|
||||
for _, again := range []response{
|
||||
ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil),
|
||||
ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil),
|
||||
} {
|
||||
ts.expect(again, http.StatusUnauthorized, "unauthorized")
|
||||
if again.challenge != bearerChallenge {
|
||||
t.Fatalf("revoked token: WWW-Authenticate = %q, want %q", again.challenge, bearerChallenge)
|
||||
}
|
||||
}
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", other.Token, nil, nil), http.StatusOK, "")
|
||||
}
|
||||
|
||||
func TestSessionExpiresAfterTTL(t *testing.T) {
|
||||
ts := newTesterWithTTL(t, time.Second)
|
||||
alice := ts.user("alice", "A")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusOK, "")
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusUnauthorized, "unauthorized")
|
||||
}
|
||||
|
||||
func TestRegistrationIsPublicAndIgnoresAuthorization(t *testing.T) {
|
||||
ts := newTester(t)
|
||||
body := map[string]string{"username": "alice", "display_name": "A", "password": testPassword}
|
||||
headers := map[string]string{"Authorization": "Bearer garbage"}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, headers), http.StatusCreated, "")
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package app_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
// newIsolatedTester assembles the application over containers of its own,
|
||||
// so a test can stop or pause them without disturbing the shared ones.
|
||||
func newIsolatedTester(t *testing.T, requestTimeout time.Duration) (*tester, *testenv.DB, *testenv.KV) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
pg, err := testenv.StartPostgres(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(pg.Close)
|
||||
vk, err := testenv.StartValkey(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(vk.Close)
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(app.Deps{
|
||||
Log: log,
|
||||
DB: pg.Pool,
|
||||
Sessions: vk.Client,
|
||||
SessionTTL: time.Hour,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
RequestTimeout: requestTimeout,
|
||||
})
|
||||
return &tester{t: t, a: a}, pg, vk
|
||||
}
|
||||
|
||||
// Without the session store no caller can be authenticated: the API answers
|
||||
// 503, never 401 (the token is not at fault) and never as an anonymous user.
|
||||
// Registration does not need the store and keeps working.
|
||||
func TestValkeyOutageAnswers503(t *testing.T) {
|
||||
ts, _, vk := newIsolatedTester(t, 5*time.Second)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
room := ts.chat(alice.token, "t", alice.ID)
|
||||
|
||||
if err := vk.Stop(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats/"+room.ID, alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodDelete, "/api/v1/auth/sessions/current", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/health/live", "", nil, nil), http.StatusOK, "")
|
||||
|
||||
body := map[string]string{"username": "bob", "display_name": "Bob", "password": testPassword}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusCreated, "")
|
||||
}
|
||||
|
||||
func TestPostgresOutageAnswers503(t *testing.T) {
|
||||
ts, pg, _ := newIsolatedTester(t, 5*time.Second)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
|
||||
if err := pg.Stop(context.Background()); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/chats", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/api/v1/users/me", alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/auth/sessions", "", nil, basic("alice", testPassword)), http.StatusServiceUnavailable, "unavailable")
|
||||
body := map[string]string{"username": "bob", "display_name": "Bob", "password": testPassword}
|
||||
ts.expect(ts.do(http.MethodPost, "/api/v1/users", "", body, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
ts.expect(ts.do(http.MethodGet, "/health/ready", "", nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
}
|
||||
|
||||
// A database that accepts connections but never answers must not hold the
|
||||
// request: the request deadline turns it into a 503.
|
||||
func TestHungPostgresAnswers503WithinTheRequestTimeout(t *testing.T) {
|
||||
ts, pg, _ := newIsolatedTester(t, 300*time.Millisecond)
|
||||
alice := ts.user("alice", "Алиса")
|
||||
|
||||
ctx := context.Background()
|
||||
if err := pg.Pause(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = pg.Unpause(ctx) })
|
||||
|
||||
for _, path := range []string{"/api/v1/chats", "/health/ready"} {
|
||||
start := time.Now()
|
||||
ts.expect(ts.do(http.MethodGet, path, alice.token, nil, nil), http.StatusServiceUnavailable, "unavailable")
|
||||
if elapsed := time.Since(start); elapsed > 2*time.Second {
|
||||
t.Fatalf("%s took %v with a hung database, want it bounded", path, elapsed)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
// Challenges sent with every 401: Basic on the login endpoint, Bearer on
|
||||
// every protected one.
|
||||
const (
|
||||
basicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
bearerChallenge = `Bearer realm="messenger"`
|
||||
)
|
||||
|
||||
var errCredentialsRequired = apperr.Unauthorized("basic credentials are required")
|
||||
|
||||
type Handler struct {
|
||||
sessions *Service
|
||||
}
|
||||
|
||||
func NewHandler(sessions *Service) *Handler {
|
||||
return &Handler{sessions: sessions}
|
||||
}
|
||||
|
||||
// Register mounts login on the public router and logout on the router
|
||||
// guarded by RequireSession.
|
||||
func (h *Handler) Register(public, protected gin.IRouter) {
|
||||
public.POST("/auth/sessions", h.login)
|
||||
protected.DELETE("/auth/sessions/current", h.logout)
|
||||
}
|
||||
|
||||
type sessionResponse struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt string `json:"expires_at"`
|
||||
User user.Response `json:"user"`
|
||||
}
|
||||
|
||||
// login accepts only the Authorization header; the body is not read.
|
||||
// BasicAuth splits at the first colon, so a password may contain colons.
|
||||
func (h *Handler) login(c *gin.Context) {
|
||||
username, password, ok := c.Request.BasicAuth()
|
||||
if !ok {
|
||||
unauthorized(c, basicChallenge, errCredentialsRequired)
|
||||
return
|
||||
}
|
||||
issued, err := h.sessions.Login(c.Request.Context(), username, password)
|
||||
if err != nil {
|
||||
unauthorized(c, basicChallenge, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, sessionResponse{
|
||||
Token: issued.Token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: httpx.Timestamp(issued.ExpiresAt),
|
||||
User: user.ToResponse(issued.User),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *Handler) logout(c *gin.Context) {
|
||||
token, ok := c.Get(keyToken)
|
||||
if !ok {
|
||||
httpx.Error(c, errors.New("logout route is missing the RequireSession middleware"))
|
||||
return
|
||||
}
|
||||
if err := h.sessions.Logout(c.Request.Context(), token.(string)); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
// Status alone is buffered by gin; writing the header now sends the 204
|
||||
// with an empty body.
|
||||
c.Status(http.StatusNoContent)
|
||||
c.Writer.WriteHeaderNow()
|
||||
}
|
||||
|
||||
// unauthorized adds the challenge only to a 401: an unavailable store is a
|
||||
// 503 and must not invite the client to retry its credentials.
|
||||
func unauthorized(c *gin.Context, challenge string, err error) {
|
||||
if apperr.KindOf(err) == apperr.KindUnauthorized {
|
||||
c.Header("WWW-Authenticate", challenge)
|
||||
}
|
||||
httpx.Error(c, err)
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
var (
|
||||
db *testenv.DB
|
||||
kv *testenv.KV
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if db, err = testenv.StartPostgres(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if kv, err = testenv.StartValkey(ctx); err != nil {
|
||||
db.Close()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
kv.Close()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/httpx"
|
||||
)
|
||||
|
||||
const keyToken = "messenger.session_token"
|
||||
|
||||
// RequireSession admits a request only with Authorization: Bearer <token> of
|
||||
// a live session and records its owner for the handlers behind it. Any other
|
||||
// identification, including Basic credentials and X-User-Id, is ignored, so
|
||||
// it can neither grant access nor change the caller.
|
||||
func RequireSession(sessions *Service) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
token, ok := bearerToken(c.GetHeader("Authorization"))
|
||||
if !ok {
|
||||
unauthorized(c, bearerChallenge, ErrUnauthenticated)
|
||||
return
|
||||
}
|
||||
userID, err := sessions.Authenticate(c.Request.Context(), token)
|
||||
if err != nil {
|
||||
unauthorized(c, bearerChallenge, err)
|
||||
return
|
||||
}
|
||||
httpx.SetUserID(c, userID)
|
||||
c.Set(keyToken, token)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// bearerToken parses "Bearer <token>": the scheme is case-insensitive and
|
||||
// may be followed by several spaces (RFC 7235); the token itself never
|
||||
// contains whitespace.
|
||||
func bearerToken(header string) (string, bool) {
|
||||
scheme, rest, found := strings.Cut(header, " ")
|
||||
if !found || !strings.EqualFold(scheme, "Bearer") {
|
||||
return "", false
|
||||
}
|
||||
token := strings.TrimLeft(rest, " ")
|
||||
if token == "" || strings.ContainsAny(token, " \t") {
|
||||
return "", false
|
||||
}
|
||||
return token, true
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
// Create stores the session until its absolute ExpiresAt.
|
||||
Create(ctx context.Context, s Session) error
|
||||
// UserID returns the owner of a live session, or ErrUnauthenticated when
|
||||
// the token is unknown, expired or revoked: the store cannot tell these
|
||||
// apart, and the client must not either.
|
||||
UserID(ctx context.Context, tokenHash string) (uuid.UUID, error)
|
||||
// Delete revokes the session; deleting a missing one is not an error.
|
||||
Delete(ctx context.Context, tokenHash string) error
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
var ErrUnauthenticated = apperr.Unauthorized("a valid bearer token is required")
|
||||
|
||||
// Credentials is what this module needs from the users module: the user
|
||||
// whose password matches, or user.ErrInvalidCredentials.
|
||||
type Credentials interface {
|
||||
Authenticate(ctx context.Context, username, password string) (user.User, error)
|
||||
}
|
||||
|
||||
// Issued is a new session as the client sees it; Token is shown only here.
|
||||
type Issued struct {
|
||||
Token string
|
||||
ExpiresAt time.Time
|
||||
User user.User
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
users Credentials
|
||||
sessions Repository
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func NewService(users Credentials, sessions Repository, ttl time.Duration) *Service {
|
||||
return &Service{users: users, sessions: sessions, ttl: ttl}
|
||||
}
|
||||
|
||||
// Login checks the credentials first, so a store outage never hands out a
|
||||
// token without a stored session. The expiry is fixed here and never
|
||||
// extended; it is truncated to milliseconds, the precision of both the API
|
||||
// timestamps and the store.
|
||||
func (s *Service) Login(ctx context.Context, username, password string) (Issued, error) {
|
||||
u, err := s.users.Authenticate(ctx, username, password)
|
||||
if err != nil {
|
||||
return Issued{}, err
|
||||
}
|
||||
token := newToken()
|
||||
expiresAt := time.Now().Add(s.ttl).Truncate(time.Millisecond).UTC()
|
||||
if err := s.sessions.Create(ctx, Session{TokenHash: HashToken(token), UserID: u.ID, ExpiresAt: expiresAt}); err != nil {
|
||||
return Issued{}, err
|
||||
}
|
||||
return Issued{Token: token, ExpiresAt: expiresAt, User: u}, nil
|
||||
}
|
||||
|
||||
// Authenticate returns the owner of the token's live session or
|
||||
// ErrUnauthenticated. A store failure stays an unavailability, never
|
||||
// ErrUnauthenticated: an outage must not look like a bad token to the client.
|
||||
func (s *Service) Authenticate(ctx context.Context, token string) (uuid.UUID, error) {
|
||||
return s.sessions.UserID(ctx, HashToken(token))
|
||||
}
|
||||
|
||||
func (s *Service) Logout(ctx context.Context, token string) error {
|
||||
return s.sessions.Delete(ctx, HashToken(token))
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package auth_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/auth"
|
||||
authstore "messenger/internal/auth/store"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/user"
|
||||
userstore "messenger/internal/user/store"
|
||||
)
|
||||
|
||||
const pw = "correct-horse-battery"
|
||||
|
||||
type fixture struct {
|
||||
ctx context.Context
|
||||
svc *auth.Service
|
||||
alice user.User
|
||||
}
|
||||
|
||||
// newFixture wires the real users service and session store over empty
|
||||
// databases and registers alice.
|
||||
func newFixture(t *testing.T, ttl time.Duration) fixture {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
kv.Reset(t)
|
||||
ctx := context.Background()
|
||||
users := user.NewService(userstore.NewUsers(db.Pool))
|
||||
alice, err := users.Register(ctx, "alice", "Алиса", pw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return fixture{ctx: ctx, svc: auth.NewService(users, authstore.NewSessions(kv.Client), ttl), alice: alice}
|
||||
}
|
||||
|
||||
func TestLoginIssuesOpaqueTokenBackedByStoredSession(t *testing.T) {
|
||||
f := newFixture(t, time.Hour)
|
||||
before := time.Now()
|
||||
issued, err := f.svc.Login(f.ctx, "alice", pw)
|
||||
after := time.Now()
|
||||
if err != nil {
|
||||
t.Fatalf("Login: %v", err)
|
||||
}
|
||||
raw, err := base64.RawURLEncoding.DecodeString(issued.Token)
|
||||
if err != nil || len(raw) != 32 {
|
||||
t.Fatalf("token %q must be 32 random bytes in base64url, got %d bytes, %v", issued.Token, len(raw), err)
|
||||
}
|
||||
if strings.Contains(issued.Token, f.alice.ID.String()) || strings.Contains(issued.Token, "alice") {
|
||||
t.Fatal("token must not carry the identity")
|
||||
}
|
||||
if issued.User != f.alice {
|
||||
t.Fatalf("User = %+v, want alice", issued.User)
|
||||
}
|
||||
if issued.ExpiresAt.Before(before.Add(time.Hour).Add(-time.Millisecond)) || issued.ExpiresAt.After(after.Add(time.Hour)) {
|
||||
t.Fatalf("ExpiresAt = %v, want login time + 1h", issued.ExpiresAt)
|
||||
}
|
||||
|
||||
key := "session:" + auth.HashToken(issued.Token)
|
||||
owner, err := kv.Client.Get(f.ctx, key).Result()
|
||||
if err != nil || owner != f.alice.ID.String() {
|
||||
t.Fatalf("stored owner = %q, %v; want alice", owner, err)
|
||||
}
|
||||
expireAt, err := kv.Client.PExpireTime(f.ctx, key).Result()
|
||||
if err != nil || expireAt != time.Duration(issued.ExpiresAt.UnixMilli())*time.Millisecond {
|
||||
t.Fatalf("store expiry = %v, %v; want the absolute ExpiresAt %d ms", expireAt, err, issued.ExpiresAt.UnixMilli())
|
||||
}
|
||||
keys, err := kv.Client.Keys(f.ctx, "*").Result()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, k := range keys {
|
||||
if strings.Contains(k, issued.Token) {
|
||||
t.Fatal("the raw token must never be stored")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginWithBadCredentialsCreatesNoSession(t *testing.T) {
|
||||
f := newFixture(t, time.Hour)
|
||||
for name, in := range map[string][2]string{
|
||||
"wrong password": {"alice", pw + "!"},
|
||||
"unknown user": {"mallory", pw},
|
||||
} {
|
||||
if _, err := f.svc.Login(f.ctx, in[0], in[1]); !errors.Is(err, user.ErrInvalidCredentials) {
|
||||
t.Errorf("%s: err = %v, want ErrInvalidCredentials", name, err)
|
||||
}
|
||||
}
|
||||
if n, err := kv.Client.DBSize(f.ctx).Result(); err != nil || n != 0 {
|
||||
t.Fatalf("sessions = %d, %v; want none", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticateResolvesTheOwner(t *testing.T) {
|
||||
f := newFixture(t, time.Hour)
|
||||
first, _ := f.svc.Login(f.ctx, "alice", pw)
|
||||
second, _ := f.svc.Login(f.ctx, "alice", pw)
|
||||
if first.Token == second.Token {
|
||||
t.Fatal("every login must get its own token")
|
||||
}
|
||||
for _, token := range []string{first.Token, second.Token} {
|
||||
id, err := f.svc.Authenticate(f.ctx, token)
|
||||
if err != nil || id != f.alice.ID {
|
||||
t.Fatalf("Authenticate = %v, %v; want alice", id, err)
|
||||
}
|
||||
}
|
||||
_, err := f.svc.Authenticate(f.ctx, "unknown-token")
|
||||
if !errors.Is(err, auth.ErrUnauthenticated) || apperr.KindOf(err) != apperr.KindUnauthorized {
|
||||
t.Fatalf("unknown token: err = %v, want ErrUnauthenticated", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogoutRevokesOnlyThatSession(t *testing.T) {
|
||||
f := newFixture(t, time.Hour)
|
||||
first, _ := f.svc.Login(f.ctx, "alice", pw)
|
||||
second, _ := f.svc.Login(f.ctx, "alice", pw)
|
||||
if err := f.svc.Logout(f.ctx, first.Token); err != nil {
|
||||
t.Fatalf("Logout: %v", err)
|
||||
}
|
||||
if _, err := f.svc.Authenticate(f.ctx, first.Token); !errors.Is(err, auth.ErrUnauthenticated) {
|
||||
t.Fatalf("revoked token: err = %v, want ErrUnauthenticated", err)
|
||||
}
|
||||
if id, err := f.svc.Authenticate(f.ctx, second.Token); err != nil || id != f.alice.ID {
|
||||
t.Fatalf("other session: %v, %v; want still valid", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSessionExpiresAfterTTL(t *testing.T) {
|
||||
f := newFixture(t, time.Second)
|
||||
issued, err := f.svc.Login(f.ctx, "alice", pw)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.svc.Authenticate(f.ctx, issued.Token); err != nil {
|
||||
t.Fatalf("fresh session: %v", err)
|
||||
}
|
||||
time.Sleep(time.Until(issued.ExpiresAt) + 100*time.Millisecond)
|
||||
if _, err := f.svc.Authenticate(f.ctx, issued.Token); !errors.Is(err, auth.ErrUnauthenticated) {
|
||||
t.Fatalf("expired session: err = %v, want ErrUnauthenticated", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
// Package auth authenticates callers.
|
||||
//
|
||||
// A login with Basic credentials creates a server-side session and returns an
|
||||
// opaque bearer token; every protected request presents the token, and logout
|
||||
// deletes the session, which revokes the token at once. The store keeps only
|
||||
// the SHA-256 of a token: a token is a random 256-bit value, so a fast digest
|
||||
// cannot be reversed by guessing, and a leaked store yields no usable tokens.
|
||||
// Passwords need the opposite treatment (a slow KDF), see platform/password.
|
||||
package auth
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Session struct {
|
||||
TokenHash string
|
||||
UserID uuid.UUID
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
// newToken returns 32 random bytes in base64url: 43 characters, 256 bits.
|
||||
func newToken() string {
|
||||
raw := make([]byte, 32)
|
||||
// Since Go 1.24 crypto/rand.Read never returns an error.
|
||||
_, _ = rand.Read(raw)
|
||||
return base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
|
||||
// HashToken is the key under which a token's session is stored.
|
||||
func HashToken(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
// Package store holds the session store implementations.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"messenger/internal/auth"
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
const keyPrefix = "session:"
|
||||
|
||||
// Sessions keeps one key per session, session:<sha256 of token>, holding the
|
||||
// owner's id. Valkey expires the key itself, so no cleanup job is needed.
|
||||
type Sessions struct {
|
||||
client *redis.Client
|
||||
}
|
||||
|
||||
var _ auth.Repository = (*Sessions)(nil)
|
||||
|
||||
func NewSessions(client *redis.Client) *Sessions {
|
||||
return &Sessions{client: client}
|
||||
}
|
||||
|
||||
// Create sets an absolute expiry (EXAT/PXAT): a restart of the store replays
|
||||
// it from the append-only file and does not extend the session.
|
||||
func (r *Sessions) Create(ctx context.Context, s auth.Session) error {
|
||||
err := r.client.SetArgs(ctx, keyPrefix+s.TokenHash, s.UserID.String(), redis.SetArgs{ExpireAt: s.ExpiresAt}).Err()
|
||||
if err != nil {
|
||||
return unavailable(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Sessions) UserID(ctx context.Context, tokenHash string) (uuid.UUID, error) {
|
||||
raw, err := r.client.Get(ctx, keyPrefix+tokenHash).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return uuid.Nil, auth.ErrUnauthenticated
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.Nil, unavailable(err)
|
||||
}
|
||||
id, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
return uuid.Nil, fmt.Errorf("session holds a malformed user id: %w", err)
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (r *Sessions) Delete(ctx context.Context, tokenHash string) error {
|
||||
if err := r.client.Del(ctx, keyPrefix+tokenHash).Err(); err != nil {
|
||||
return unavailable(err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func unavailable(err error) error {
|
||||
return apperr.Wrap(apperr.KindUnavailable, "session store unavailable", err)
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
// Package chat is the chats module. Entities mirror table rows and
|
||||
// carry no behaviour; membership is a query rather than a field, and
|
||||
// ChatWithMembers is the projection the API renders. Both entities derive
|
||||
// CreatedAt from their UUIDv7 id, so the history order (created_at, id) is
|
||||
// plain id order.
|
||||
package chat
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"slices"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxTitleCodePoints = 100
|
||||
MaxMembers = 100
|
||||
)
|
||||
|
||||
// ErrNotFound covers both a missing chat and a chat the caller is not a
|
||||
// member of, so outsiders cannot probe for existence.
|
||||
var ErrNotFound = apperr.NotFound("chat not found")
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type ChatWithMembers struct {
|
||||
Chat
|
||||
MemberIDs []uuid.UUID
|
||||
}
|
||||
|
||||
func NewChat(id uuid.UUID, title string) (Chat, error) {
|
||||
if strings.TrimSpace(title) == "" {
|
||||
return Chat{}, apperr.Invalid("title must not be blank")
|
||||
}
|
||||
if utf8.RuneCountInString(title) > MaxTitleCodePoints {
|
||||
return Chat{}, apperr.Invalid(fmt.Sprintf("title must be at most %d characters", MaxTitleCodePoints))
|
||||
}
|
||||
// PostgreSQL text cannot hold U+0000.
|
||||
if strings.ContainsRune(title, 0) {
|
||||
return Chat{}, apperr.Invalid("title must not contain the NUL character")
|
||||
}
|
||||
return Chat{ID: id, Title: title, CreatedAt: ident.TimeOf(id)}, nil
|
||||
}
|
||||
|
||||
// ValidateMembers checks the member list of a new chat and returns its own
|
||||
// copy ordered by id: members are a set, and this is the one order in which
|
||||
// every read returns them.
|
||||
func ValidateMembers(memberIDs []uuid.UUID, creator uuid.UUID) ([]uuid.UUID, error) {
|
||||
if len(memberIDs) == 0 {
|
||||
return nil, apperr.Invalid("member_ids must not be empty")
|
||||
}
|
||||
if len(memberIDs) > MaxMembers {
|
||||
return nil, apperr.Invalid(fmt.Sprintf("member_ids must have at most %d entries", MaxMembers))
|
||||
}
|
||||
seen := make(map[uuid.UUID]struct{}, len(memberIDs))
|
||||
for _, m := range memberIDs {
|
||||
if m == uuid.Nil {
|
||||
return nil, apperr.Invalid("member_ids must not contain the nil uuid")
|
||||
}
|
||||
if _, dup := seen[m]; dup {
|
||||
return nil, apperr.Invalid("member_ids must be unique")
|
||||
}
|
||||
seen[m] = struct{}{}
|
||||
}
|
||||
if _, ok := seen[creator]; !ok {
|
||||
return nil, apperr.Invalid("member_ids must include the creator")
|
||||
}
|
||||
members := slices.Clone(memberIDs)
|
||||
slices.SortFunc(members, func(a, b uuid.UUID) int { return bytes.Compare(a[:], b[:]) })
|
||||
return members, nil
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
func TestNewChatDerivesCreatedAtFromID(t *testing.T) {
|
||||
id := ident.New()
|
||||
c, err := chat.NewChat(id, "Проект")
|
||||
if err != nil {
|
||||
t.Fatalf("NewChat error: %v", err)
|
||||
}
|
||||
if c.ID != id || c.Title != "Проект" || !c.CreatedAt.Equal(ident.TimeOf(id)) {
|
||||
t.Fatalf("unexpected chat %+v", c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewChatValidatesTitle(t *testing.T) {
|
||||
// PostgreSQL text cannot hold U+0000, so a title with it is invalid input,
|
||||
// not a storage failure.
|
||||
for _, title := range []string{"", " ", strings.Repeat("я", 101), "nul\x00inside"} {
|
||||
_, err := chat.NewChat(ident.New(), title)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("title %q must be rejected as invalid, got %v", title, err)
|
||||
}
|
||||
}
|
||||
if _, err := chat.NewChat(ident.New(), strings.Repeat("я", 100)); err != nil {
|
||||
t.Errorf("100 code points must be accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Members come back in one canonical order (by id) whatever order the client
|
||||
// sent, so the created chat and every later read of it agree.
|
||||
func TestValidateMembersCopiesInCanonicalOrder(t *testing.T) {
|
||||
alice, bob := ident.New(), ident.New()
|
||||
input := []uuid.UUID{bob, alice}
|
||||
members, err := chat.ValidateMembers(input, alice)
|
||||
if err != nil {
|
||||
t.Fatalf("ValidateMembers error: %v", err)
|
||||
}
|
||||
if len(members) != 2 || members[0] != alice || members[1] != bob {
|
||||
t.Fatalf("members must be ordered by id, got %v", members)
|
||||
}
|
||||
input[1] = ident.New()
|
||||
if members[0] != alice {
|
||||
t.Fatal("result must not alias the input slice")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMembersRejectsInvalidLists(t *testing.T) {
|
||||
alice, bob := ident.New(), ident.New()
|
||||
cases := map[string][]uuid.UUID{
|
||||
"empty": {},
|
||||
"nil": nil,
|
||||
"missing creator": {bob},
|
||||
"duplicate": {alice, bob, bob},
|
||||
"duplicate self": {alice, alice},
|
||||
"too many": append([]uuid.UUID{alice}, many(100)...),
|
||||
"nil uuid member": {alice, uuid.Nil},
|
||||
}
|
||||
for name, members := range cases {
|
||||
_, err := chat.ValidateMembers(members, alice)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("%s: want invalid, got %v", name, err)
|
||||
}
|
||||
if err != nil && !strings.Contains(err.Error(), "member_ids") {
|
||||
t.Errorf("%s: message must name the field: %v", name, err)
|
||||
}
|
||||
}
|
||||
if _, err := chat.ValidateMembers(append([]uuid.UUID{alice}, many(99)...), alice); err != nil {
|
||||
t.Errorf("100 members must be accepted: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func many(n int) []uuid.UUID {
|
||||
out := make([]uuid.UUID, n)
|
||||
for i := range out {
|
||||
out[i] = ident.New()
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
chats *Service
|
||||
}
|
||||
|
||||
func NewHandler(chats *Service) *Handler {
|
||||
return &Handler{chats: chats}
|
||||
}
|
||||
|
||||
// Register mounts the chat endpoints on a router guarded by the authentication middleware.
|
||||
func (h *Handler) Register(protected gin.IRouter) {
|
||||
protected.POST("/chats", httpx.WithUser(h.create))
|
||||
protected.GET("/chats", httpx.WithUser(h.list))
|
||||
protected.GET("/chats/:chat_id", httpx.WithUser(h.get))
|
||||
protected.POST("/chats/:chat_id/messages", httpx.WithUser(h.send))
|
||||
protected.GET("/chats/:chat_id/messages", httpx.WithUser(h.history))
|
||||
}
|
||||
|
||||
type createChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
|
||||
type createMessageRequest struct {
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
type ChatResponse struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
func toChatResponse(c ChatWithMembers) ChatResponse {
|
||||
members := make([]string, 0, len(c.MemberIDs))
|
||||
for _, m := range c.MemberIDs {
|
||||
members = append(members, m.String())
|
||||
}
|
||||
return ChatResponse{
|
||||
ID: c.ID.String(),
|
||||
Title: c.Title,
|
||||
MemberIDs: members,
|
||||
CreatedAt: httpx.Timestamp(c.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func toMessageResponse(m Message) MessageResponse {
|
||||
return MessageResponse{
|
||||
ID: m.ID.String(),
|
||||
ChatID: m.ChatID.String(),
|
||||
SenderID: m.SenderID.String(),
|
||||
Text: m.Text,
|
||||
CreatedAt: httpx.Timestamp(m.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) create(c *gin.Context, actor uuid.UUID) {
|
||||
var req createChatRequest
|
||||
if err := httpx.DecodeJSON(c, &req); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
members := make([]uuid.UUID, 0, len(req.MemberIDs))
|
||||
for _, raw := range req.MemberIDs {
|
||||
id, err := ident.Parse(raw)
|
||||
if err != nil {
|
||||
httpx.Error(c, apperr.Invalid("member_ids must contain valid UUIDs"))
|
||||
return
|
||||
}
|
||||
members = append(members, id)
|
||||
}
|
||||
chat, err := h.chats.Create(c.Request.Context(), actor, req.Title, members)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, toChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *Handler) list(c *gin.Context, actor uuid.UUID) {
|
||||
limit, after, err := listing(c, actor)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
page, err := h.chats.List(c.Request.Context(), actor, after, limit)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, pagination.ToResponse(page, actor, toChatResponse))
|
||||
}
|
||||
|
||||
func (h *Handler) get(c *gin.Context, actor uuid.UUID) {
|
||||
chatID, err := chatParam(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
chat, err := h.chats.Get(c.Request.Context(), actor, chatID)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, toChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *Handler) send(c *gin.Context, actor uuid.UUID) {
|
||||
chatID, err := chatParam(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
var req createMessageRequest
|
||||
if err := httpx.DecodeJSON(c, &req); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
message, err := h.chats.Send(c.Request.Context(), actor, chatID, req.Text)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, toMessageResponse(message))
|
||||
}
|
||||
|
||||
func (h *Handler) history(c *gin.Context, actor uuid.UUID) {
|
||||
chatID, err := chatParam(c)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
limit, after, err := listing(c, chatID)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
page, err := h.chats.History(c.Request.Context(), actor, chatID, after, limit)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, pagination.ToResponse(page, chatID, toMessageResponse))
|
||||
}
|
||||
|
||||
func chatParam(c *gin.Context) (uuid.UUID, error) {
|
||||
id, err := ident.Parse(c.Param("chat_id"))
|
||||
if err != nil {
|
||||
return uuid.Nil, apperr.Invalid("chat_id must be a UUID")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// listing parses limit and cursor; the cursor must belong to scope, and a
|
||||
// present but empty cursor is rejected like a malformed one.
|
||||
func listing(c *gin.Context, scope uuid.UUID) (int, *uuid.UUID, error) {
|
||||
limit, err := pagination.ParseLimit(c.Query("limit"))
|
||||
if err != nil {
|
||||
return 0, nil, apperr.Wrap(apperr.KindInvalid, err.Error(), err)
|
||||
}
|
||||
token, present := c.GetQuery("cursor")
|
||||
if !present {
|
||||
return limit, nil, nil
|
||||
}
|
||||
last, err := pagination.DecodeCursor(token, scope)
|
||||
if err != nil {
|
||||
return 0, nil, apperr.Wrap(apperr.KindInvalid, "cursor is invalid for this resource", err)
|
||||
}
|
||||
return limit, &last, nil
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
const MaxTextCodePoints = 4000
|
||||
|
||||
type Message struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
// NewMessage keeps the text verbatim; only blank or oversized text is rejected.
|
||||
func NewMessage(id, chatID, senderID uuid.UUID, text string) (Message, error) {
|
||||
if strings.TrimSpace(text) == "" {
|
||||
return Message{}, apperr.Invalid("text must not be blank")
|
||||
}
|
||||
if utf8.RuneCountInString(text) > MaxTextCodePoints {
|
||||
return Message{}, apperr.Invalid(fmt.Sprintf("text must be at most %d characters", MaxTextCodePoints))
|
||||
}
|
||||
// PostgreSQL text cannot hold U+0000.
|
||||
if strings.ContainsRune(text, 0) {
|
||||
return Message{}, apperr.Invalid("text must not contain the NUL character")
|
||||
}
|
||||
return Message{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: ident.TimeOf(id),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
func TestNewMessageDerivesCreatedAt(t *testing.T) {
|
||||
id, chatID, alice := ident.New(), ident.New(), ident.New()
|
||||
m, err := chat.NewMessage(id, chatID, alice, "Привет, мир 👋")
|
||||
if err != nil {
|
||||
t.Fatalf("NewMessage error: %v", err)
|
||||
}
|
||||
if m.ID != id || m.ChatID != chatID || m.SenderID != alice || m.Text != "Привет, мир 👋" {
|
||||
t.Fatalf("unexpected message %+v", m)
|
||||
}
|
||||
if !m.CreatedAt.Equal(ident.TimeOf(id)) {
|
||||
t.Fatalf("CreatedAt = %v, want %v", m.CreatedAt, ident.TimeOf(id))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMessageValidatesTextByCodePoints(t *testing.T) {
|
||||
chatID, alice := ident.New(), ident.New()
|
||||
if _, err := chat.NewMessage(ident.New(), chatID, alice, strings.Repeat("я", 4000)); err != nil {
|
||||
t.Errorf("4000 code points must be accepted: %v", err)
|
||||
}
|
||||
for _, text := range []string{"", " ", "\n\t ", strings.Repeat("я", 4001), "nul\x00inside"} {
|
||||
_, err := chat.NewMessage(ident.New(), chatID, alice, text)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("text %q must be rejected as invalid, got %v", text, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewMessageKeepsSurroundingWhitespace(t *testing.T) {
|
||||
m, err := chat.NewMessage(ident.New(), ident.New(), ident.New(), " hi ")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if m.Text != " hi " {
|
||||
t.Fatalf("text must be stored verbatim, got %q", m.Text)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// ChatRepository stores chats and their membership. Membership is answered
|
||||
// by queries; the projection with members is assembled by the store in one
|
||||
// query, never by loading a member list into the entity. Members are always
|
||||
// returned ordered by id.
|
||||
type ChatRepository interface {
|
||||
// Create stores the chat row and its members atomically.
|
||||
Create(ctx context.Context, chat Chat, memberIDs []uuid.UUID) error
|
||||
// GetForMember returns the chat with members, or ErrNotFound when the
|
||||
// chat does not exist or the user is not a member.
|
||||
GetForMember(ctx context.Context, chatID, userID uuid.UUID) (ChatWithMembers, error)
|
||||
IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error)
|
||||
// ListByMember returns up to limit chats of the user ordered by
|
||||
// (created_at, id), strictly after the chat identified by after when set.
|
||||
ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]ChatWithMembers, error)
|
||||
}
|
||||
|
||||
type MessageRepository interface {
|
||||
Append(ctx context.Context, message Message) error
|
||||
// ListByChat returns up to limit messages ordered by (created_at, id),
|
||||
// strictly after the message identified by after when set. An unknown
|
||||
// chat yields an empty history.
|
||||
ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]Message, error)
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package chat
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
// UserLookup is what this module needs from the users module: one batched
|
||||
// existence check. It is declared here, on the consumer side, so this
|
||||
// package never imports the users package and tests substitute a map.
|
||||
type UserLookup interface {
|
||||
ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error)
|
||||
}
|
||||
|
||||
type Service struct {
|
||||
chats ChatRepository
|
||||
messages MessageRepository
|
||||
users UserLookup
|
||||
}
|
||||
|
||||
func NewService(chats ChatRepository, messages MessageRepository, users UserLookup) *Service {
|
||||
return &Service{chats: chats, messages: messages, users: users}
|
||||
}
|
||||
|
||||
func (s *Service) Create(ctx context.Context, creator uuid.UUID, title string, memberIDs []uuid.UUID) (ChatWithMembers, error) {
|
||||
chat, err := NewChat(ident.New(), title)
|
||||
if err != nil {
|
||||
return ChatWithMembers{}, err
|
||||
}
|
||||
members, err := ValidateMembers(memberIDs, creator)
|
||||
if err != nil {
|
||||
return ChatWithMembers{}, err
|
||||
}
|
||||
found, err := s.users.ExistingIDs(ctx, members)
|
||||
if err != nil {
|
||||
return ChatWithMembers{}, fmt.Errorf("check members: %w", err)
|
||||
}
|
||||
if len(found) != len(members) {
|
||||
return ChatWithMembers{}, apperr.Invalid("member_ids must reference registered users")
|
||||
}
|
||||
if err := s.chats.Create(ctx, chat, members); err != nil {
|
||||
return ChatWithMembers{}, err
|
||||
}
|
||||
return ChatWithMembers{Chat: chat, MemberIDs: members}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Get(ctx context.Context, actor, chatID uuid.UUID) (ChatWithMembers, error) {
|
||||
return s.chats.GetForMember(ctx, chatID, actor)
|
||||
}
|
||||
|
||||
func (s *Service) List(ctx context.Context, actor uuid.UUID, after *uuid.UUID, limit int) (pagination.Page[ChatWithMembers], error) {
|
||||
items, err := s.chats.ListByMember(ctx, actor, after, limit+1)
|
||||
if err != nil {
|
||||
return pagination.Page[ChatWithMembers]{}, err
|
||||
}
|
||||
return pagination.Cut(items, limit, func(c ChatWithMembers) uuid.UUID { return c.ID }), nil
|
||||
}
|
||||
|
||||
func (s *Service) Send(ctx context.Context, actor, chatID uuid.UUID, text string) (Message, error) {
|
||||
if err := s.requireMember(ctx, actor, chatID); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
message, err := NewMessage(ident.New(), chatID, actor, text)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if err := s.messages.Append(ctx, message); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (s *Service) History(ctx context.Context, actor, chatID uuid.UUID, after *uuid.UUID, limit int) (pagination.Page[Message], error) {
|
||||
if err := s.requireMember(ctx, actor, chatID); err != nil {
|
||||
return pagination.Page[Message]{}, err
|
||||
}
|
||||
items, err := s.messages.ListByChat(ctx, chatID, after, limit+1)
|
||||
if err != nil {
|
||||
return pagination.Page[Message]{}, err
|
||||
}
|
||||
return pagination.Cut(items, limit, func(m Message) uuid.UUID { return m.ID }), nil
|
||||
}
|
||||
|
||||
// requireMember runs before any field validation, so an outsider gets the
|
||||
// same answer for a foreign chat as for a missing one.
|
||||
func (s *Service) requireMember(ctx context.Context, actor, chatID uuid.UUID) error {
|
||||
ok, err := s.chats.IsMember(ctx, chatID, actor)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check membership: %w", err)
|
||||
}
|
||||
if !ok {
|
||||
return ErrNotFound
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,234 @@
|
||||
package chat_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/chat/store"
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/testenv"
|
||||
"messenger/internal/user"
|
||||
userstore "messenger/internal/user/store"
|
||||
)
|
||||
|
||||
var db *testenv.DB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
db, err = testenv.StartPostgres(context.Background())
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
// countingUsers passes through to the real users service and counts the
|
||||
// lookups, which proves that member checks are batched.
|
||||
type countingUsers struct {
|
||||
*user.Service
|
||||
calls atomic.Int32
|
||||
}
|
||||
|
||||
func (c *countingUsers) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
|
||||
c.calls.Add(1)
|
||||
return c.Service.ExistingIDs(ctx, ids)
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
svc *chat.Service
|
||||
users *countingUsers
|
||||
alice, bob uuid.UUID
|
||||
eve uuid.UUID
|
||||
ctx context.Context
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) fixture {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
ctx := context.Background()
|
||||
users := &countingUsers{Service: user.NewService(userstore.NewUsers(db.Pool))}
|
||||
register := func(name string) uuid.UUID {
|
||||
u, err := users.Register(ctx, name, name, "correct-horse-battery")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return u.ID
|
||||
}
|
||||
return fixture{
|
||||
svc: chat.NewService(store.NewChats(db.Pool), store.NewMessages(db.Pool), users),
|
||||
users: users,
|
||||
alice: register("alice"),
|
||||
bob: register("bob"),
|
||||
eve: register("eve"),
|
||||
ctx: ctx,
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatChecksMembersInOneCall(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "Проект", []uuid.UUID{f.eve, f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatalf("Create error: %v", err)
|
||||
}
|
||||
if c.Title != "Проект" || len(c.MemberIDs) != 3 {
|
||||
t.Fatalf("unexpected chat %+v", c)
|
||||
}
|
||||
if got := f.users.calls.Load(); got != 1 {
|
||||
t.Fatalf("member existence must be checked with one batch call, got %d", got)
|
||||
}
|
||||
got, err := f.svc.Get(f.ctx, f.bob, c.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("member Get error: %v", err)
|
||||
}
|
||||
if fmt.Sprint(got.MemberIDs) != fmt.Sprint(c.MemberIDs) || !got.CreatedAt.Equal(c.CreatedAt) || got.Title != c.Title {
|
||||
t.Fatalf("Get = %+v, want what Create returned %+v", got, c)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatRejectsUnknownMember(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
_, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, ident.New()})
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Fatalf("kind = %v, want KindInvalid (%v)", apperr.KindOf(err), err)
|
||||
}
|
||||
if !strings.Contains(err.Error(), "member_ids") {
|
||||
t.Fatalf("message must name the field: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateChatRejectsInvalidInputBeforeAnyLookup(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
cases := map[string]struct {
|
||||
title string
|
||||
members []uuid.UUID
|
||||
}{
|
||||
"blank title": {" ", []uuid.UUID{f.alice}},
|
||||
"missing creator": {"t", []uuid.UUID{f.bob}},
|
||||
"duplicate": {"t", []uuid.UUID{f.alice, f.alice}},
|
||||
}
|
||||
for name, tc := range cases {
|
||||
_, err := f.svc.Create(f.ctx, f.alice, tc.title, tc.members)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Errorf("%s: kind = %v, want KindInvalid (%v)", name, apperr.KindOf(err), err)
|
||||
}
|
||||
}
|
||||
if got := f.users.calls.Load(); got != 0 {
|
||||
t.Fatalf("invalid input must be rejected before any user lookup, got %d calls", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetHidesForeignAndUnknownChatsAlike(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, foreign := f.svc.Get(f.ctx, f.eve, c.ID)
|
||||
_, unknown := f.svc.Get(f.ctx, f.eve, ident.New())
|
||||
if !errors.Is(foreign, chat.ErrNotFound) || !errors.Is(unknown, chat.ErrNotFound) {
|
||||
t.Fatalf("foreign = %v, unknown = %v; want both ErrNotFound", foreign, unknown)
|
||||
}
|
||||
if apperr.KindOf(foreign) != apperr.KindNotFound {
|
||||
t.Fatalf("ErrNotFound must carry KindNotFound, got %v", apperr.KindOf(foreign))
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPagesOnlyOwnChats(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
var mine []uuid.UUID
|
||||
for range 3 {
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mine = append(mine, c.ID)
|
||||
if _, err := f.svc.Create(f.ctx, f.eve, "t", []uuid.UUID{f.eve}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
page, err := f.svc.List(f.ctx, f.alice, nil, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page.Items) != 2 || page.Next == nil || *page.Next != mine[1] {
|
||||
t.Fatalf("first page = %+v", page)
|
||||
}
|
||||
if len(page.Items[0].MemberIDs) != 2 {
|
||||
t.Fatalf("listed chats must carry their members: %+v", page.Items[0])
|
||||
}
|
||||
rest, err := f.svc.List(f.ctx, f.alice, page.Next, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rest.Items) != 1 || rest.Items[0].ID != mine[2] || rest.Next != nil {
|
||||
t.Fatalf("second page = %+v", rest)
|
||||
}
|
||||
all, err := f.svc.List(f.ctx, f.bob, nil, 100)
|
||||
if err != nil || len(all.Items) != 3 || all.Next != nil {
|
||||
t.Fatalf("bob page = %+v, %v", all, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSendChecksAccessBeforeValidation(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = f.svc.Send(f.ctx, f.eve, c.ID, " ")
|
||||
if !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("outsider with blank text: err = %v, want ErrNotFound", err)
|
||||
}
|
||||
_, err = f.svc.Send(f.ctx, f.bob, c.ID, " ")
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Fatalf("member with blank text: kind = %v, want KindInvalid", apperr.KindOf(err))
|
||||
}
|
||||
msg, err := f.svc.Send(f.ctx, f.bob, c.ID, "Привет 👋")
|
||||
if err != nil || msg.SenderID != f.bob || msg.ChatID != c.ID || msg.Text != "Привет 👋" {
|
||||
t.Fatalf("Send = %+v, %v", msg, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHistoryPagesAndHidesForeignChat(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
c, err := f.svc.Create(f.ctx, f.alice, "t", []uuid.UUID{f.alice, f.bob})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, _ := f.svc.Send(f.ctx, f.alice, c.ID, "1")
|
||||
second, _ := f.svc.Send(f.ctx, f.bob, c.ID, "2")
|
||||
|
||||
page, err := f.svc.History(f.ctx, f.bob, c.ID, nil, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page.Items) != 1 || page.Items[0] != first || page.Next == nil || *page.Next != first.ID {
|
||||
t.Fatalf("first page = %+v", page)
|
||||
}
|
||||
rest, err := f.svc.History(f.ctx, f.alice, c.ID, page.Next, 1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rest.Items) != 1 || rest.Items[0] != second || rest.Next != nil {
|
||||
t.Fatalf("second page = %+v", rest)
|
||||
}
|
||||
if _, err := f.svc.History(f.ctx, f.eve, c.ID, nil, 10); !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("outsider history: err = %v, want ErrNotFound", err)
|
||||
}
|
||||
empty, err := f.svc.History(f.ctx, f.alice, c.ID, &second.ID, 10)
|
||||
if err != nil || empty.Items == nil || len(empty.Items) != 0 || empty.Next != nil {
|
||||
t.Fatalf("page after last = %+v, %v; want empty non-nil items", empty, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
// Package store holds the repository implementations of the chats module.
|
||||
//
|
||||
// Keyset pages use `id > COALESCE($after, nil uuid)`: every UUIDv7 is
|
||||
// greater than the nil UUID, so one query serves the first page and the
|
||||
// following ones, and the condition stays a range the index can scan. Ids
|
||||
// are UUIDv7 and created_at derives from them, so ordering by id is the
|
||||
// contract order (created_at, id). Errors pass through postgres.Classify, so
|
||||
// an unreachable database surfaces as unavailability.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/postgres"
|
||||
)
|
||||
|
||||
type Chats struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
var _ chat.ChatRepository = (*Chats)(nil)
|
||||
|
||||
func NewChats(pool *pgxpool.Pool) *Chats {
|
||||
return &Chats{pool: pool}
|
||||
}
|
||||
|
||||
func (r *Chats) Create(ctx context.Context, c chat.Chat, memberIDs []uuid.UUID) error {
|
||||
return postgres.Classify(pgx.BeginFunc(ctx, r.pool, func(tx pgx.Tx) error {
|
||||
if _, err := tx.Exec(ctx,
|
||||
`INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`,
|
||||
c.ID, c.Title, c.CreatedAt); err != nil {
|
||||
return fmt.Errorf("insert chat: %w", err)
|
||||
}
|
||||
if _, err := tx.Exec(ctx,
|
||||
`INSERT INTO chat_members (chat_id, user_id) SELECT $1, unnest($2::uuid[])`,
|
||||
c.ID, memberIDs); err != nil {
|
||||
return fmt.Errorf("insert chat members: %w", err)
|
||||
}
|
||||
return nil
|
||||
}))
|
||||
}
|
||||
|
||||
func (r *Chats) GetForMember(ctx context.Context, chatID, userID uuid.UUID) (chat.ChatWithMembers, error) {
|
||||
row := r.pool.QueryRow(ctx, `
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)
|
||||
FROM chats c
|
||||
WHERE c.id = $1
|
||||
AND EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`,
|
||||
chatID, userID)
|
||||
c, err := scanChat(row)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return chat.ChatWithMembers{}, chat.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return chat.ChatWithMembers{}, fmt.Errorf("select chat: %w", postgres.Classify(err))
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func (r *Chats) IsMember(ctx context.Context, chatID, userID uuid.UUID) (bool, error) {
|
||||
var ok bool
|
||||
err := r.pool.QueryRow(ctx,
|
||||
`SELECT EXISTS (SELECT 1 FROM chat_members WHERE chat_id = $1 AND user_id = $2)`,
|
||||
chatID, userID).Scan(&ok)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("check membership: %w", postgres.Classify(err))
|
||||
}
|
||||
return ok, nil
|
||||
}
|
||||
|
||||
func (r *Chats) ListByMember(ctx context.Context, userID uuid.UUID, after *uuid.UUID, limit int) ([]chat.ChatWithMembers, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)
|
||||
FROM chat_members own
|
||||
JOIN chats c ON c.id = own.chat_id
|
||||
WHERE own.user_id = $1
|
||||
AND own.chat_id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
|
||||
ORDER BY own.chat_id
|
||||
LIMIT $3`,
|
||||
userID, after, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select chats: %w", postgres.Classify(err))
|
||||
}
|
||||
chats, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.ChatWithMembers, error) {
|
||||
return scanChat(row)
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read chats: %w", postgres.Classify(err))
|
||||
}
|
||||
return chats, nil
|
||||
}
|
||||
|
||||
func scanChat(row pgx.Row) (chat.ChatWithMembers, error) {
|
||||
var c chat.ChatWithMembers
|
||||
if err := row.Scan(&c.ID, &c.Title, &c.CreatedAt, &c.MemberIDs); err != nil {
|
||||
return chat.ChatWithMembers{}, err
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
type Messages struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
var _ chat.MessageRepository = (*Messages)(nil)
|
||||
|
||||
func NewMessages(pool *pgxpool.Pool) *Messages {
|
||||
return &Messages{pool: pool}
|
||||
}
|
||||
|
||||
func (r *Messages) Append(ctx context.Context, m chat.Message) error {
|
||||
_, err := r.pool.Exec(ctx,
|
||||
`INSERT INTO messages (id, chat_id, sender_id, text, created_at) VALUES ($1, $2, $3, $4, $5)`,
|
||||
m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert message: %w", postgres.Classify(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ListByChat is the history query; scripts/explain-history.sql runs the same
|
||||
// statement for the EXPLAIN evidence.
|
||||
func (r *Messages) ListByChat(ctx context.Context, chatID uuid.UUID, after *uuid.UUID, limit int) ([]chat.Message, error) {
|
||||
rows, err := r.pool.Query(ctx, `
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
AND id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
|
||||
ORDER BY id
|
||||
LIMIT $3`,
|
||||
chatID, after, limit)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select messages: %w", postgres.Classify(err))
|
||||
}
|
||||
messages, err := pgx.CollectRows(rows, func(row pgx.CollectableRow) (chat.Message, error) {
|
||||
var m chat.Message
|
||||
err := row.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt)
|
||||
return m, err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read messages: %w", postgres.Classify(err))
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
@@ -0,0 +1,313 @@
|
||||
package store_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/chat/store"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/testenv"
|
||||
"messenger/internal/user"
|
||||
userstore "messenger/internal/user/store"
|
||||
)
|
||||
|
||||
var db *testenv.DB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
db, err = testenv.StartPostgres(context.Background())
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
ctx context.Context
|
||||
chats *store.Chats
|
||||
messages *store.Messages
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) fixture {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
return fixture{ctx: context.Background(), chats: store.NewChats(db.Pool), messages: store.NewMessages(db.Pool)}
|
||||
}
|
||||
|
||||
// mustUser stores a user row: chats and messages reference users by FK.
|
||||
func (f fixture) mustUser(t *testing.T) uuid.UUID {
|
||||
t.Helper()
|
||||
id := ident.New()
|
||||
u, err := user.New(id, "u"+id.String()[24:], "User")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
u.PasswordHash = "x"
|
||||
if err := userstore.NewUsers(db.Pool).Create(f.ctx, u); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func (f fixture) mustChat(t *testing.T, members ...uuid.UUID) chat.Chat {
|
||||
t.Helper()
|
||||
c, err := chat.NewChat(ident.New(), "title")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.chats.Create(f.ctx, c, members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func (f fixture) newMessage(t *testing.T, chatID, sender uuid.UUID) chat.Message {
|
||||
t.Helper()
|
||||
m, err := chat.NewMessage(ident.New(), chatID, sender, "text")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func TestChatCreateThenGetForMember(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice, bob := f.mustUser(t), f.mustUser(t)
|
||||
c := f.mustChat(t, alice, bob)
|
||||
|
||||
got, err := f.chats.GetForMember(f.ctx, c.ID, bob)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Chat != c || len(got.MemberIDs) != 2 || got.MemberIDs[0] != alice || got.MemberIDs[1] != bob {
|
||||
t.Fatalf("GetForMember = %+v, want %+v with members [alice bob]", got, c)
|
||||
}
|
||||
if _, err := f.chats.GetForMember(f.ctx, c.ID, f.mustUser(t)); !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("outsider must get ErrNotFound, got %v", err)
|
||||
}
|
||||
for who, want := range map[uuid.UUID]bool{alice: true, bob: true, ident.New(): false} {
|
||||
ok, err := f.chats.IsMember(f.ctx, c.ID, who)
|
||||
if err != nil || ok != want {
|
||||
t.Fatalf("IsMember(%s) = %v, %v; want %v", who, ok, err, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The chat row and its members are written in one transaction: a failing
|
||||
// member insert must leave no chat behind.
|
||||
func TestCreateRollsBackWhenMemberInsertFails(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c, err := chat.NewChat(ident.New(), "half-made")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.chats.Create(f.ctx, c, []uuid.UUID{alice, ident.New()}); err == nil {
|
||||
t.Fatal("Create with an unknown member must fail")
|
||||
}
|
||||
var chats, members int
|
||||
if err := db.Pool.QueryRow(f.ctx, `SELECT (SELECT count(*) FROM chats), (SELECT count(*) FROM chat_members)`).Scan(&chats, &members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 0 || members != 0 {
|
||||
t.Fatalf("chats = %d, members = %d after a failed create; want 0 and 0", chats, members)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatUnknownID(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
if _, err := f.chats.GetForMember(f.ctx, ident.New(), ident.New()); !errors.Is(err, chat.ErrNotFound) {
|
||||
t.Fatalf("GetForMember err = %v, want ErrNotFound", err)
|
||||
}
|
||||
if ok, err := f.chats.IsMember(f.ctx, ident.New(), ident.New()); err != nil || ok {
|
||||
t.Fatalf("IsMember on unknown chat = %v, %v; want false, nil", ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListByMemberFiltersOrdersAndPages(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice, bob := f.mustUser(t), f.mustUser(t)
|
||||
var mine []chat.Chat
|
||||
for range 5 {
|
||||
c, err := chat.NewChat(ident.New(), "title")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mine = append(mine, c)
|
||||
// A chat without alice must never show up in her listing.
|
||||
f.mustChat(t, bob)
|
||||
}
|
||||
// Insert the oldest chat last to prove the store sorts, not appends.
|
||||
for _, c := range append(mine[1:], mine[0]) {
|
||||
if err := f.chats.Create(f.ctx, c, []uuid.UUID{alice, bob}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
page, err := f.chats.ListByMember(f.ctx, alice, nil, 3)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page) != 3 || page[0].ID != mine[0].ID || page[2].ID != mine[2].ID {
|
||||
t.Fatalf("first page mismatch: %v", page)
|
||||
}
|
||||
if len(page[0].MemberIDs) != 2 || page[0].MemberIDs[0] != alice {
|
||||
t.Fatalf("listing must carry members in id order: %+v", page[0])
|
||||
}
|
||||
last := page[2].ID
|
||||
rest, err := f.chats.ListByMember(f.ctx, alice, &last, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(rest) != 2 || rest[0].ID != mine[3].ID || rest[1].ID != mine[4].ID {
|
||||
t.Fatalf("second page mismatch: %v", rest)
|
||||
}
|
||||
none, err := f.chats.ListByMember(f.ctx, ident.New(), nil, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if none == nil || len(none) != 0 {
|
||||
t.Fatalf("unknown member must get an empty non-nil slice, got %#v", none)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListByChatUnknownChatIsEmpty(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
got, err := f.messages.ListByChat(f.ctx, ident.New(), nil, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got == nil || len(got) != 0 {
|
||||
t.Fatalf("want empty non-nil slice, got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendOutOfOrderIsListedByID(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
first, second, third := f.newMessage(t, c.ID, alice), f.newMessage(t, c.ID, alice), f.newMessage(t, c.ID, alice)
|
||||
for _, m := range []chat.Message{third, first, second} {
|
||||
if err := f.messages.Append(f.ctx, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
got, err := f.messages.ListByChat(f.ctx, c.ID, nil, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 3 || got[0] != first || got[1] != second || got[2] != third {
|
||||
t.Fatalf("history must be ordered by id and round-trip every field, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPagingWalksWholeHistoryWithoutGapsOrDuplicates(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
const total = 250
|
||||
var expected []uuid.UUID
|
||||
for range total {
|
||||
m := f.newMessage(t, c.ID, alice)
|
||||
expected = append(expected, m.ID)
|
||||
if err := f.messages.Append(f.ctx, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
// Another chat's history must not leak into this one.
|
||||
other := f.mustChat(t, alice)
|
||||
if err := f.messages.Append(f.ctx, f.newMessage(t, other.ID, alice)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, limit := range []int{1, 7, 100} {
|
||||
var seen []uuid.UUID
|
||||
var after *uuid.UUID
|
||||
for {
|
||||
page, err := f.messages.ListByChat(f.ctx, c.ID, after, limit)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(page) == 0 {
|
||||
break
|
||||
}
|
||||
if len(page) > limit {
|
||||
t.Fatalf("limit %d: page has %d items", limit, len(page))
|
||||
}
|
||||
for _, m := range page {
|
||||
seen = append(seen, m.ID)
|
||||
}
|
||||
last := page[len(page)-1].ID
|
||||
after = &last
|
||||
}
|
||||
if len(seen) != total {
|
||||
t.Fatalf("limit %d: walked %d messages, want %d", limit, len(seen), total)
|
||||
}
|
||||
for i := range seen {
|
||||
if seen[i] != expected[i] {
|
||||
t.Fatalf("limit %d: position %d has %s, want %s", limit, i, seen[i], expected[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAfterUnknownIDStartsAtNextGreater(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
a := f.newMessage(t, c.ID, alice)
|
||||
gap := ident.New() // never stored, but sorts between a and b
|
||||
b := f.newMessage(t, c.ID, alice)
|
||||
for _, m := range []chat.Message{a, b} {
|
||||
if err := f.messages.Append(f.ctx, m); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
got, err := f.messages.ListByChat(f.ctx, c.ID, &gap, 10)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != 1 || got[0].ID != b.ID {
|
||||
t.Fatalf("want only b after the gap id, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConcurrentAppendsKeepEveryMessage(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
alice := f.mustUser(t)
|
||||
c := f.mustChat(t, alice)
|
||||
const clients, perClient = 10, 10
|
||||
var wg sync.WaitGroup
|
||||
for range clients {
|
||||
wg.Go(func() {
|
||||
for range perClient {
|
||||
if err := f.messages.Append(f.ctx, f.newMessage(t, c.ID, alice)); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
wg.Wait()
|
||||
got, err := f.messages.ListByChat(f.ctx, c.ID, nil, clients*perClient+1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(got) != clients*perClient {
|
||||
t.Fatalf("stored %d messages, want %d", len(got), clients*perClient)
|
||||
}
|
||||
for i := 1; i < len(got); i++ {
|
||||
if bytes.Compare(got[i-1].ID[:], got[i].ID[:]) >= 0 {
|
||||
t.Fatalf("history out of order at %d", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,77 @@
|
||||
// Package health serves the probes. Liveness only proves the process
|
||||
// answers. Readiness means the process can serve the synchronous API: it is
|
||||
// switched off during shutdown, so an orchestrator stops routing new traffic
|
||||
// before the listener closes, and it fails while a critical dependency is
|
||||
// unreachable.
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
)
|
||||
|
||||
// checkTimeout is shared by all checks of one probe, so the probe answers
|
||||
// within it even when every dependency hangs; the Docker healthcheck allows
|
||||
// two seconds.
|
||||
const checkTimeout = time.Second
|
||||
|
||||
// Check is a dependency the API cannot serve requests without.
|
||||
type Check struct {
|
||||
Name string
|
||||
Probe func(ctx context.Context) error
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
ready atomic.Bool
|
||||
checks []Check
|
||||
}
|
||||
|
||||
type status struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
func New(checks ...Check) *Handler {
|
||||
h := &Handler{checks: checks}
|
||||
h.ready.Store(true)
|
||||
return h
|
||||
}
|
||||
|
||||
func (h *Handler) SetReady(ready bool) {
|
||||
h.ready.Store(ready)
|
||||
}
|
||||
|
||||
func (h *Handler) IsReady() bool {
|
||||
return h.ready.Load()
|
||||
}
|
||||
|
||||
func (h *Handler) Register(r gin.IRouter) {
|
||||
r.GET("/health/live", h.live)
|
||||
r.GET("/health/ready", h.readyz)
|
||||
}
|
||||
|
||||
func (h *Handler) live(c *gin.Context) {
|
||||
httpx.JSON(c, http.StatusOK, status{Status: "ok"})
|
||||
}
|
||||
|
||||
func (h *Handler) readyz(c *gin.Context) {
|
||||
if !h.ready.Load() {
|
||||
httpx.Error(c, apperr.Unavailable("service is shutting down"))
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), checkTimeout)
|
||||
defer cancel()
|
||||
for _, check := range h.checks {
|
||||
if err := check.Probe(ctx); err != nil {
|
||||
httpx.Error(c, apperr.Wrap(apperr.KindUnavailable, check.Name+" is unavailable", err))
|
||||
return
|
||||
}
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, status{Status: "ok"})
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// Package apperr is the application's error vocabulary: every module reports
|
||||
// failures as a Kind plus a message that is safe to show to clients, and the
|
||||
// transport layer owns the only mapping from Kind to HTTP status. Causes are
|
||||
// kept for logs but never rendered.
|
||||
package apperr
|
||||
|
||||
import "errors"
|
||||
|
||||
type Kind int
|
||||
|
||||
const (
|
||||
KindInternal Kind = iota
|
||||
KindInvalid
|
||||
KindUnauthorized
|
||||
KindNotFound
|
||||
KindConflict
|
||||
KindPayloadTooLarge
|
||||
KindUnsupportedMedia
|
||||
KindUnavailable
|
||||
)
|
||||
|
||||
type Error struct {
|
||||
Kind Kind
|
||||
Message string
|
||||
Cause error
|
||||
}
|
||||
|
||||
func (e *Error) Error() string { return e.Message }
|
||||
|
||||
func (e *Error) Unwrap() error { return e.Cause }
|
||||
|
||||
func New(kind Kind, message string) *Error {
|
||||
return &Error{Kind: kind, Message: message}
|
||||
}
|
||||
|
||||
func Wrap(kind Kind, message string, cause error) *Error {
|
||||
return &Error{Kind: kind, Message: message, Cause: cause}
|
||||
}
|
||||
|
||||
// KindOf classifies any error; foreign errors are internal.
|
||||
func KindOf(err error) Kind {
|
||||
if e, ok := errors.AsType[*Error](err); ok {
|
||||
return e.Kind
|
||||
}
|
||||
return KindInternal
|
||||
}
|
||||
|
||||
func Invalid(message string) *Error { return New(KindInvalid, message) }
|
||||
func Unauthorized(message string) *Error { return New(KindUnauthorized, message) }
|
||||
func NotFound(message string) *Error { return New(KindNotFound, message) }
|
||||
func Conflict(message string) *Error { return New(KindConflict, message) }
|
||||
func PayloadTooLarge(message string) *Error { return New(KindPayloadTooLarge, message) }
|
||||
func UnsupportedMedia(message string) *Error { return New(KindUnsupportedMedia, message) }
|
||||
func Unavailable(message string) *Error { return New(KindUnavailable, message) }
|
||||
@@ -0,0 +1,49 @@
|
||||
package apperr_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
func TestKindOfReturnsKindForApplicationError(t *testing.T) {
|
||||
err := apperr.NotFound("chat not found")
|
||||
if got := apperr.KindOf(err); got != apperr.KindNotFound {
|
||||
t.Fatalf("KindOf = %v, want KindNotFound", got)
|
||||
}
|
||||
if err.Error() != "chat not found" {
|
||||
t.Fatalf("Error() = %q", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
func TestKindOfSeesThroughWrapping(t *testing.T) {
|
||||
err := fmt.Errorf("service: %w", apperr.Conflict("username taken"))
|
||||
if got := apperr.KindOf(err); got != apperr.KindConflict {
|
||||
t.Fatalf("KindOf = %v, want KindConflict", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKindOfDefaultsToInternal(t *testing.T) {
|
||||
if got := apperr.KindOf(errors.New("boom")); got != apperr.KindInternal {
|
||||
t.Fatalf("KindOf = %v, want KindInternal", got)
|
||||
}
|
||||
if got := apperr.KindOf(nil); got != apperr.KindInternal {
|
||||
t.Fatalf("KindOf(nil) = %v, want KindInternal", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestWrapKeepsCause(t *testing.T) {
|
||||
cause := errors.New("cause")
|
||||
err := apperr.Wrap(apperr.KindInvalid, "bad input", cause)
|
||||
if !errors.Is(err, cause) {
|
||||
t.Fatal("wrapped error must unwrap to cause")
|
||||
}
|
||||
if apperr.KindOf(err) != apperr.KindInvalid {
|
||||
t.Fatal("wrapped error must keep its kind")
|
||||
}
|
||||
if err.Error() != "bad input" {
|
||||
t.Fatalf("Error() = %q, want public message only", err.Error())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
// Package config reads the process configuration from the environment.
|
||||
// Settings have defaults suited to the lab stand, except credentials, which
|
||||
// are required and read from files named by *_FILE variables so that their
|
||||
// values never appear in the environment. An invalid value stops the process
|
||||
// at start-up with a message naming the variable, never the secret.
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Host string
|
||||
Port int
|
||||
Env string
|
||||
LogLevel slog.Level
|
||||
ShutdownTimeout time.Duration
|
||||
MaxBodyBytes int64
|
||||
DatabaseURL string
|
||||
SessionStoreURL string
|
||||
SessionTTL time.Duration
|
||||
}
|
||||
|
||||
// Lookup reads one variable, reporting whether it was set; os.LookupEnv
|
||||
// in production, a map in tests.
|
||||
type Lookup func(key string) (string, bool)
|
||||
|
||||
func Load(lookup Lookup) (Config, error) {
|
||||
cfg := Config{
|
||||
Host: "0.0.0.0",
|
||||
Port: 8080,
|
||||
Env: "lab",
|
||||
LogLevel: slog.LevelInfo,
|
||||
ShutdownTimeout: 8 * time.Second,
|
||||
MaxBodyBytes: 64 * 1024,
|
||||
SessionTTL: time.Hour,
|
||||
}
|
||||
if v, ok := lookup("APP_HOST"); ok {
|
||||
if strings.TrimSpace(v) == "" {
|
||||
return Config{}, fmt.Errorf("APP_HOST must not be empty")
|
||||
}
|
||||
cfg.Host = v
|
||||
}
|
||||
if v, ok := lookup("APP_PORT"); ok {
|
||||
port, err := strconv.Atoi(v)
|
||||
if err != nil || port < 1 || port > 65535 {
|
||||
return Config{}, fmt.Errorf("APP_PORT must be an integer between 1 and 65535, got %q", v)
|
||||
}
|
||||
cfg.Port = port
|
||||
}
|
||||
if v, ok := lookup("APP_ENV"); ok && strings.TrimSpace(v) != "" {
|
||||
cfg.Env = v
|
||||
}
|
||||
if v, ok := lookup("APP_LOG_LEVEL"); ok {
|
||||
var level slog.Level
|
||||
if err := level.UnmarshalText([]byte(v)); err != nil {
|
||||
return Config{}, fmt.Errorf("APP_LOG_LEVEL must be debug, info, warn or error, got %q", v)
|
||||
}
|
||||
cfg.LogLevel = level
|
||||
}
|
||||
if v, ok := lookup("APP_SHUTDOWN_TIMEOUT"); ok {
|
||||
d, err := time.ParseDuration(v)
|
||||
if err != nil || d <= 0 {
|
||||
return Config{}, fmt.Errorf("APP_SHUTDOWN_TIMEOUT must be a positive duration such as 8s, got %q", v)
|
||||
}
|
||||
cfg.ShutdownTimeout = d
|
||||
}
|
||||
if v, ok := lookup("APP_MAX_BODY_BYTES"); ok {
|
||||
n, err := strconv.ParseInt(v, 10, 64)
|
||||
if err != nil || n <= 0 {
|
||||
return Config{}, fmt.Errorf("APP_MAX_BODY_BYTES must be a positive integer, got %q", v)
|
||||
}
|
||||
cfg.MaxBodyBytes = n
|
||||
}
|
||||
if v, ok := lookup("SESSION_TTL_SECONDS"); ok {
|
||||
n, err := strconv.ParseUint(v, 10, 32)
|
||||
if err != nil || n == 0 {
|
||||
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive whole number of seconds, got %q", v)
|
||||
}
|
||||
cfg.SessionTTL = time.Duration(n) * time.Second
|
||||
}
|
||||
var err error
|
||||
if cfg.DatabaseURL, err = secret(lookup, "DATABASE_URL_FILE"); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
if cfg.SessionStoreURL, err = secret(lookup, "SESSION_STORE_URL_FILE"); err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
// secret reads the file named by the variable; surrounding whitespace,
|
||||
// including the trailing newline editors add, is dropped.
|
||||
func secret(lookup Lookup, key string) (string, error) {
|
||||
path, ok := lookup(key)
|
||||
if !ok || strings.TrimSpace(path) == "" {
|
||||
return "", fmt.Errorf("%s must name a file with the value", key)
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("%s: cannot read %s: %w", key, path, err)
|
||||
}
|
||||
value := strings.TrimSpace(string(raw))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s: file %s is empty", key, path)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func (c Config) Addr() string {
|
||||
return net.JoinHostPort(c.Host, strconv.Itoa(c.Port))
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package config_test
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/config"
|
||||
)
|
||||
|
||||
func env(values map[string]string) func(string) (string, bool) {
|
||||
return func(key string) (string, bool) {
|
||||
v, ok := values[key]
|
||||
return v, ok
|
||||
}
|
||||
}
|
||||
|
||||
func secretFile(t *testing.T, content string) string {
|
||||
t.Helper()
|
||||
path := filepath.Join(t.TempDir(), "secret")
|
||||
if err := os.WriteFile(path, []byte(content), 0o600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
const (
|
||||
databaseURL = "postgres://app:s3cret@db:5432/messenger"
|
||||
sessionStoreURL = "redis://:s3cret@valkey:6379/0"
|
||||
)
|
||||
|
||||
// required holds the variables without defaults, merged with extra.
|
||||
func required(t *testing.T, extra map[string]string) map[string]string {
|
||||
t.Helper()
|
||||
values := map[string]string{
|
||||
"DATABASE_URL_FILE": secretFile(t, databaseURL+"\n"),
|
||||
"SESSION_STORE_URL_FILE": secretFile(t, sessionStoreURL+"\r\n"),
|
||||
}
|
||||
for k, v := range extra {
|
||||
values[k] = v
|
||||
}
|
||||
return values
|
||||
}
|
||||
|
||||
func TestDefaults(t *testing.T) {
|
||||
cfg, err := config.Load(env(required(t, nil)))
|
||||
if err != nil {
|
||||
t.Fatalf("Load error: %v", err)
|
||||
}
|
||||
if cfg.Addr() != "0.0.0.0:8080" {
|
||||
t.Fatalf("Addr = %q", cfg.Addr())
|
||||
}
|
||||
if cfg.Env != "lab" || cfg.LogLevel != slog.LevelInfo {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.ShutdownTimeout != 8*time.Second || cfg.MaxBodyBytes != 65536 {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.DatabaseURL != databaseURL || cfg.SessionStoreURL != sessionStoreURL {
|
||||
t.Fatalf("urls = %q, %q; want the file contents without line endings", cfg.DatabaseURL, cfg.SessionStoreURL)
|
||||
}
|
||||
if cfg.SessionTTL != time.Hour {
|
||||
t.Fatalf("SessionTTL = %v, want 1h", cfg.SessionTTL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSecretFilesAreRequiredAndNeverEchoed(t *testing.T) {
|
||||
for _, key := range []string{"DATABASE_URL_FILE", "SESSION_STORE_URL_FILE"} {
|
||||
cases := map[string]func(map[string]string){
|
||||
"unset": func(v map[string]string) { delete(v, key) },
|
||||
"empty path": func(v map[string]string) { v[key] = "" },
|
||||
"no file": func(v map[string]string) { v[key] = filepath.Join(t.TempDir(), "missing") },
|
||||
"blank file": func(v map[string]string) { v[key] = secretFile(t, " \r\n") },
|
||||
}
|
||||
for name, breakIt := range cases {
|
||||
values := required(t, nil)
|
||||
breakIt(values)
|
||||
_, err := config.Load(env(values))
|
||||
if err == nil {
|
||||
t.Errorf("%s %s: want error", key, name)
|
||||
continue
|
||||
}
|
||||
if !strings.Contains(err.Error(), key) || strings.Contains(err.Error(), "s3cret") {
|
||||
t.Errorf("%s %s: error must name the variable and hide the secret: %v", key, name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestOverrides(t *testing.T) {
|
||||
cfg, err := config.Load(env(required(t, map[string]string{
|
||||
"APP_HOST": "127.0.0.1",
|
||||
"APP_PORT": "9090",
|
||||
"APP_ENV": "dev",
|
||||
"APP_LOG_LEVEL": "debug",
|
||||
"APP_SHUTDOWN_TIMEOUT": "3s",
|
||||
"APP_MAX_BODY_BYTES": "1024",
|
||||
"SESSION_TTL_SECONDS": "300",
|
||||
})))
|
||||
if err != nil {
|
||||
t.Fatalf("Load error: %v", err)
|
||||
}
|
||||
if cfg.Addr() != "127.0.0.1:9090" || cfg.Env != "dev" || cfg.LogLevel != slog.LevelDebug {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
if cfg.ShutdownTimeout != 3*time.Second || cfg.MaxBodyBytes != 1024 || cfg.SessionTTL != 5*time.Minute {
|
||||
t.Fatalf("cfg = %+v", cfg)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidValuesAreReported(t *testing.T) {
|
||||
cases := map[string]map[string]string{
|
||||
"port text": {"APP_PORT": "abc"},
|
||||
"port zero": {"APP_PORT": "0"},
|
||||
"port too big": {"APP_PORT": "70000"},
|
||||
"level": {"APP_LOG_LEVEL": "loud"},
|
||||
"timeout": {"APP_SHUTDOWN_TIMEOUT": "soon"},
|
||||
"timeout negative": {"APP_SHUTDOWN_TIMEOUT": "-1s"},
|
||||
"body zero": {"APP_MAX_BODY_BYTES": "0"},
|
||||
"body text": {"APP_MAX_BODY_BYTES": "big"},
|
||||
"empty host": {"APP_HOST": ""},
|
||||
"ttl zero": {"SESSION_TTL_SECONDS": "0"},
|
||||
"ttl negative": {"SESSION_TTL_SECONDS": "-5"},
|
||||
"ttl text": {"SESSION_TTL_SECONDS": "hour"},
|
||||
"ttl fraction": {"SESSION_TTL_SECONDS": "1.5"},
|
||||
}
|
||||
for name, values := range cases {
|
||||
if _, err := config.Load(env(required(t, values))); err == nil {
|
||||
t.Errorf("%s: want error", name)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
// DecodeJSON reads exactly one JSON object into dst and enforces what gin's
|
||||
// binding does not: the media type must be application/json (415), invalid
|
||||
// UTF-8, unknown fields, wrong types, malformed syntax and trailing data are
|
||||
// 400, an oversized body is 413, and messages never expose Go type names.
|
||||
func DecodeJSON(c *gin.Context, dst any) error {
|
||||
mediaType, _, err := mime.ParseMediaType(c.GetHeader("Content-Type"))
|
||||
if err != nil || mediaType != "application/json" {
|
||||
return apperr.UnsupportedMedia("Content-Type must be application/json")
|
||||
}
|
||||
// The body is already capped by the bodyLimit middleware, so reading it
|
||||
// whole is bounded.
|
||||
body, err := io.ReadAll(c.Request.Body)
|
||||
if err != nil {
|
||||
return decodeError(err)
|
||||
}
|
||||
// encoding/json silently replaces invalid UTF-8 with U+FFFD; the contract
|
||||
// and RFC 8259 require UTF-8, so such a body is rejected, not repaired.
|
||||
if !utf8.Valid(body) {
|
||||
return apperr.Invalid("request body must be valid UTF-8")
|
||||
}
|
||||
dec := json.NewDecoder(bytes.NewReader(body))
|
||||
dec.DisallowUnknownFields()
|
||||
if err := dec.Decode(dst); err != nil {
|
||||
return decodeError(err)
|
||||
}
|
||||
if err := dec.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
||||
return apperr.Invalid("unexpected data after JSON body")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func decodeError(err error) error {
|
||||
if tooLarge, ok := errors.AsType[*http.MaxBytesError](err); ok {
|
||||
return apperr.Wrap(apperr.KindPayloadTooLarge, fmt.Sprintf("request body must be at most %d bytes", tooLarge.Limit), err)
|
||||
}
|
||||
if errors.Is(err, io.EOF) {
|
||||
return apperr.Wrap(apperr.KindInvalid, "request body is required", err)
|
||||
}
|
||||
if wrongType, ok := errors.AsType[*json.UnmarshalTypeError](err); ok {
|
||||
if wrongType.Field == "" {
|
||||
return apperr.Wrap(apperr.KindInvalid, "request body must be a JSON object", err)
|
||||
}
|
||||
return apperr.Wrap(apperr.KindInvalid, fmt.Sprintf("field %q has wrong type", wrongType.Field), err)
|
||||
}
|
||||
// encoding/json reports unknown fields as a plain error without a type.
|
||||
const unknownPrefix = "json: unknown field "
|
||||
if msg := err.Error(); strings.HasPrefix(msg, unknownPrefix) {
|
||||
field := strings.Trim(strings.TrimPrefix(msg, unknownPrefix), `"`)
|
||||
return apperr.Wrap(apperr.KindInvalid, fmt.Sprintf("unknown field %q", field), err)
|
||||
}
|
||||
return apperr.Wrap(apperr.KindInvalid, "malformed JSON body", err)
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Package httpx is the transport layer over gin: request ids, access
|
||||
// logging, panic recovery, body limits, strict JSON decoding and the single
|
||||
// error format of the API contract. gin is used for routing and the
|
||||
// middleware chain; everything here exists because the contract demands a
|
||||
// specific behaviour that gin's own helpers do not provide.
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
const (
|
||||
keyRequestID = "messenger.request_id"
|
||||
keyLogger = "messenger.logger"
|
||||
)
|
||||
|
||||
// NewEngine builds an engine whose every response, including routing
|
||||
// failures and panics, carries X-Request-Id and the contract error body.
|
||||
func NewEngine(log *slog.Logger, maxBodyBytes int64) *gin.Engine {
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
e := gin.New()
|
||||
// Redirects would answer with text/plain and without a request id.
|
||||
e.RedirectTrailingSlash = false
|
||||
e.RedirectFixedPath = false
|
||||
e.HandleMethodNotAllowed = true
|
||||
|
||||
e.Use(requestID(log), accessLog(), recovery(), bodyLimit(maxBodyBytes))
|
||||
e.NoRoute(func(c *gin.Context) { Error(c, apperr.NotFound("route not found")) })
|
||||
e.NoMethod(func(c *gin.Context) { Fail(c, http.StatusMethodNotAllowed, "invalid_request", "method not allowed") })
|
||||
return e
|
||||
}
|
||||
|
||||
func RequestID(c *gin.Context) string {
|
||||
return c.GetString(keyRequestID)
|
||||
}
|
||||
|
||||
func logger(c *gin.Context) *slog.Logger {
|
||||
if l, ok := c.Get(keyLogger); ok {
|
||||
if log, ok := l.(*slog.Logger); ok {
|
||||
return log
|
||||
}
|
||||
}
|
||||
return slog.Default()
|
||||
}
|
||||
@@ -0,0 +1,376 @@
|
||||
package httpx_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/httpx"
|
||||
)
|
||||
|
||||
type errorBody struct {
|
||||
Error struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
type payload struct {
|
||||
Text string `json:"text"`
|
||||
Count int `json:"count"`
|
||||
}
|
||||
|
||||
func newEngine(t *testing.T, logs *bytes.Buffer) *gin.Engine {
|
||||
t.Helper()
|
||||
return newEngineAt(t, logs, slog.LevelDebug)
|
||||
}
|
||||
|
||||
func newEngineAt(t *testing.T, logs *bytes.Buffer, level slog.Level) *gin.Engine {
|
||||
t.Helper()
|
||||
if logs == nil {
|
||||
logs = &bytes.Buffer{}
|
||||
}
|
||||
log := slog.New(slog.NewJSONHandler(logs, &slog.HandlerOptions{Level: level}))
|
||||
e := httpx.NewEngine(log, 64)
|
||||
e.GET("/ok", func(c *gin.Context) { httpx.JSON(c, http.StatusOK, gin.H{"text": "<a&b>"}) })
|
||||
e.GET("/panic", func(c *gin.Context) { panic("secret detail") })
|
||||
e.GET("/fail", func(c *gin.Context) { httpx.Error(c, errors.New("db password leaked")) })
|
||||
e.GET("/notfound", func(c *gin.Context) { httpx.Error(c, apperr.NotFound("chat not found")) })
|
||||
e.GET("/who", func(c *gin.Context) {
|
||||
httpx.SetUserID(c, uuid.MustParse("11111111-1111-4111-8111-111111111111"))
|
||||
id, ok := httpx.UserID(c)
|
||||
httpx.JSON(c, http.StatusOK, gin.H{"id": id.String(), "ok": ok})
|
||||
})
|
||||
e.POST("/decode", func(c *gin.Context) {
|
||||
var p payload
|
||||
if err := httpx.DecodeJSON(c, &p); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, p)
|
||||
})
|
||||
return e
|
||||
}
|
||||
|
||||
func do(t *testing.T, e *gin.Engine, method, path string, body string, headers map[string]string) (*httptest.ResponseRecorder, errorBody) {
|
||||
t.Helper()
|
||||
var reader io.Reader
|
||||
if body != "" {
|
||||
reader = strings.NewReader(body)
|
||||
}
|
||||
req := httptest.NewRequest(method, path, reader)
|
||||
for k, v := range headers {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
rec := httptest.NewRecorder()
|
||||
e.ServeHTTP(rec, req)
|
||||
var eb errorBody
|
||||
if rec.Code >= 400 {
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &eb); err != nil {
|
||||
t.Fatalf("%s %s: error body is not JSON: %v: %s", method, path, err, rec.Body.String())
|
||||
}
|
||||
}
|
||||
return rec, eb
|
||||
}
|
||||
|
||||
func assertError(t *testing.T, rec *httptest.ResponseRecorder, eb errorBody, status int, code string) {
|
||||
t.Helper()
|
||||
if rec.Code != status {
|
||||
t.Fatalf("status = %d, want %d (body %s)", rec.Code, status, rec.Body.String())
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
|
||||
t.Fatalf("Content-Type = %q", ct)
|
||||
}
|
||||
rid := rec.Header().Get("X-Request-Id")
|
||||
if rid == "" {
|
||||
t.Fatal("missing X-Request-Id header")
|
||||
}
|
||||
if eb.Error.RequestID != rid {
|
||||
t.Fatalf("body request_id %q != header %q", eb.Error.RequestID, rid)
|
||||
}
|
||||
if eb.Error.Code != code || eb.Error.Message == "" {
|
||||
t.Fatalf("error = %+v, want code %q with a message", eb.Error, code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUnknownRouteIsJSON404(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/nope", "", nil)
|
||||
assertError(t, rec, eb, http.StatusNotFound, "not_found")
|
||||
}
|
||||
|
||||
func TestTrailingSlashIsNotRedirected(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/ok/", "", nil)
|
||||
assertError(t, rec, eb, http.StatusNotFound, "not_found")
|
||||
}
|
||||
|
||||
func TestWrongMethodIsJSON405WithAllow(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodDelete, "/ok", "", nil)
|
||||
assertError(t, rec, eb, http.StatusMethodNotAllowed, "invalid_request")
|
||||
if !strings.Contains(rec.Header().Get("Allow"), "GET") {
|
||||
t.Fatalf("Allow = %q, want GET", rec.Header().Get("Allow"))
|
||||
}
|
||||
}
|
||||
|
||||
func TestPanicBecomesJSON500WithoutDetails(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
rec, eb := do(t, newEngine(t, logs), http.MethodGet, "/panic", "", nil)
|
||||
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
||||
if strings.Contains(rec.Body.String(), "secret detail") {
|
||||
t.Fatal("panic value leaked to the client")
|
||||
}
|
||||
if !strings.Contains(logs.String(), "secret detail") {
|
||||
t.Fatal("panic must be logged for operators")
|
||||
}
|
||||
}
|
||||
|
||||
func TestForeignErrorBecomesJSON500WithoutDetails(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
rec, eb := do(t, newEngine(t, logs), http.MethodGet, "/fail", "", nil)
|
||||
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
||||
if strings.Contains(rec.Body.String(), "password") {
|
||||
t.Fatal("internal error text leaked to the client")
|
||||
}
|
||||
if !strings.Contains(logs.String(), "db password leaked") {
|
||||
t.Fatal("cause must be logged for operators")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplicationErrorMapping(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodGet, "/notfound", "", nil)
|
||||
assertError(t, rec, eb, http.StatusNotFound, "not_found")
|
||||
if eb.Error.Message != "chat not found" {
|
||||
t.Fatalf("message = %q", eb.Error.Message)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSuccessResponseHeadersAndEscaping(t *testing.T) {
|
||||
rec, _ := do(t, newEngine(t, nil), http.MethodGet, "/ok", "", nil)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d", rec.Code)
|
||||
}
|
||||
if ct := rec.Header().Get("Content-Type"); ct != "application/json; charset=utf-8" {
|
||||
t.Fatalf("Content-Type = %q", ct)
|
||||
}
|
||||
if rec.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("missing X-Request-Id on success")
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), `"<a&b>"`) {
|
||||
t.Fatalf("HTML escaping must be off, body %s", rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestRequestIDIsGeneratedNotEchoed(t *testing.T) {
|
||||
rec, _ := do(t, newEngine(t, nil), http.MethodGet, "/ok", "", map[string]string{"X-Request-Id": "client-chosen"})
|
||||
if got := rec.Header().Get("X-Request-Id"); got == "" || got == "client-chosen" {
|
||||
t.Fatalf("X-Request-Id = %q, want a server generated value", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeJSONHappyPath(t *testing.T) {
|
||||
rec, _ := do(t, newEngine(t, nil), http.MethodPost, "/decode", `{"text":"Привет","count":2}`, map[string]string{"Content-Type": "application/json; charset=utf-8"})
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"Привет"`) {
|
||||
t.Fatalf("status %d body %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeJSONFailures(t *testing.T) {
|
||||
jsonCT := map[string]string{"Content-Type": "application/json"}
|
||||
cases := []struct {
|
||||
name string
|
||||
body string
|
||||
headers map[string]string
|
||||
status int
|
||||
code string
|
||||
contain string
|
||||
}{
|
||||
{"no content type", `{"text":"x"}`, nil, http.StatusUnsupportedMediaType, "unsupported_media_type", ""},
|
||||
{"wrong content type", `{"text":"x"}`, map[string]string{"Content-Type": "text/plain"}, http.StatusUnsupportedMediaType, "unsupported_media_type", ""},
|
||||
{"empty body", "", jsonCT, http.StatusBadRequest, "invalid_request", "body"},
|
||||
{"malformed", `{"text":`, jsonCT, http.StatusBadRequest, "invalid_request", "malformed"},
|
||||
{"invalid utf-8", "{\"text\":\"bad \xff byte\"}", jsonCT, http.StatusBadRequest, "invalid_request", "UTF-8"},
|
||||
{"unknown field", `{"text":"x","extra":1}`, jsonCT, http.StatusBadRequest, "invalid_request", "extra"},
|
||||
{"wrong type", `{"text":123}`, jsonCT, http.StatusBadRequest, "invalid_request", "text"},
|
||||
{"trailing data", `{"text":"x"} {"text":"y"}`, jsonCT, http.StatusBadRequest, "invalid_request", "after"},
|
||||
{"array instead of object", `[1,2]`, jsonCT, http.StatusBadRequest, "invalid_request", ""},
|
||||
{"too large", `{"text":"` + strings.Repeat("x", 100) + `"}`, jsonCT, http.StatusRequestEntityTooLarge, "payload_too_large", ""},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
rec, eb := do(t, newEngine(t, nil), http.MethodPost, "/decode", tc.body, tc.headers)
|
||||
assertError(t, rec, eb, tc.status, tc.code)
|
||||
if tc.contain != "" && !strings.Contains(eb.Error.Message, tc.contain) {
|
||||
t.Fatalf("message %q must mention %q", eb.Error.Message, tc.contain)
|
||||
}
|
||||
if strings.Contains(eb.Error.Message, "httpx_test") || strings.Contains(eb.Error.Message, "payload") {
|
||||
t.Fatalf("message must not leak Go type names: %q", eb.Error.Message)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccessLogHasRequestFieldsButNoBody(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
e := newEngine(t, logs)
|
||||
rec, _ := do(t, e, http.MethodPost, "/decode", `{"text":"confidential"}`, map[string]string{"Content-Type": "application/json"})
|
||||
var entry map[string]any
|
||||
line := lastLine(logs.String())
|
||||
if err := json.Unmarshal([]byte(line), &entry); err != nil {
|
||||
t.Fatalf("log line is not JSON: %v: %s", err, line)
|
||||
}
|
||||
for _, key := range []string{"request_id", "method", "path", "status", "duration_ms"} {
|
||||
if _, ok := entry[key]; !ok {
|
||||
t.Errorf("log entry missing %q: %s", key, line)
|
||||
}
|
||||
}
|
||||
if entry["request_id"] != rec.Header().Get("X-Request-Id") {
|
||||
t.Errorf("log request_id %v != header %q", entry["request_id"], rec.Header().Get("X-Request-Id"))
|
||||
}
|
||||
if strings.Contains(logs.String(), "confidential") {
|
||||
t.Fatal("request body must never be logged")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAccessLogLevelFollowsStatus(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
e := newEngineAt(t, logs, slog.LevelInfo)
|
||||
|
||||
do(t, e, http.MethodGet, "/ok", "", nil)
|
||||
if strings.Contains(logs.String(), `"msg":"request"`) {
|
||||
t.Fatalf("successful requests must be logged at debug level only, got %s", logs.String())
|
||||
}
|
||||
do(t, e, http.MethodGet, "/notfound", "", nil)
|
||||
if entry := logEntry(t, logs); entry["level"] != "INFO" || entry["status"] != float64(404) {
|
||||
t.Fatalf("client error must be logged at info level with its status, got %v", entry)
|
||||
}
|
||||
do(t, e, http.MethodGet, "/fail", "", nil)
|
||||
if entry := logEntry(t, logs); entry["level"] != "ERROR" || entry["status"] != float64(500) {
|
||||
t.Fatalf("server error must be logged at error level with its status, got %v", entry)
|
||||
}
|
||||
|
||||
verbose := &bytes.Buffer{}
|
||||
do(t, newEngineAt(t, verbose, slog.LevelDebug), http.MethodGet, "/ok", "", nil)
|
||||
if entry := logEntry(t, verbose); entry["level"] != "DEBUG" || entry["status"] != float64(200) {
|
||||
t.Fatalf("at debug level successful requests must be logged, got %v", entry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserIDRoundTripAndAccessLogAttribution(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
rec, _ := do(t, newEngine(t, logs), http.MethodGet, "/who", "", nil)
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"ok":true`) {
|
||||
t.Fatalf("UserID must return what SetUserID stored: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
if entry := logEntry(t, logs); entry["user_id"] != "11111111-1111-4111-8111-111111111111" {
|
||||
t.Fatalf("access log must attribute the request to the user, got %v", entry)
|
||||
}
|
||||
anonymous := &bytes.Buffer{}
|
||||
do(t, newEngine(t, anonymous), http.MethodGet, "/ok", "", nil)
|
||||
if _, present := logEntry(t, anonymous)["user_id"]; present {
|
||||
t.Fatal("anonymous requests must not carry user_id")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWithUserPassesTheIdentifiedCaller(t *testing.T) {
|
||||
actor := uuid.MustParse("22222222-2222-4222-8222-222222222222")
|
||||
e := newEngine(t, nil)
|
||||
e.GET("/actor",
|
||||
func(c *gin.Context) { httpx.SetUserID(c, actor) },
|
||||
httpx.WithUser(func(c *gin.Context, got uuid.UUID) {
|
||||
httpx.JSON(c, http.StatusOK, gin.H{"id": got.String()})
|
||||
}))
|
||||
rec, _ := do(t, e, http.MethodGet, "/actor", "", nil)
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), actor.String()) {
|
||||
t.Fatalf("handler did not receive the caller: %d %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Reaching WithUser without an authentication middleware in front is a
|
||||
// wiring mistake, so it must not be blamed on the client with a 401.
|
||||
func TestWithUserWithoutIdentityIsAWiringError(t *testing.T) {
|
||||
e := newEngine(t, nil)
|
||||
e.GET("/actor", httpx.WithUser(func(c *gin.Context, _ uuid.UUID) {
|
||||
httpx.JSON(c, http.StatusOK, gin.H{})
|
||||
}))
|
||||
rec, eb := do(t, e, http.MethodGet, "/actor", "", nil)
|
||||
assertError(t, rec, eb, http.StatusInternalServerError, "internal_error")
|
||||
}
|
||||
|
||||
// A 5xx tells the client only the safe message; the operator must still see
|
||||
// why the dependency failed, so the cause goes to the log.
|
||||
func TestUnavailableCauseIsLoggedButNotReturned(t *testing.T) {
|
||||
logs := &bytes.Buffer{}
|
||||
e := newEngine(t, logs)
|
||||
e.GET("/down", func(c *gin.Context) {
|
||||
cause := errors.New("dial tcp 10.0.0.7:5432: connect: connection refused")
|
||||
httpx.Error(c, fmt.Errorf("select user: %w", apperr.Wrap(apperr.KindUnavailable, "database unavailable", cause)))
|
||||
})
|
||||
rec, eb := do(t, e, http.MethodGet, "/down", "", nil)
|
||||
assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable")
|
||||
// Neither the cause nor the wrapping context of internal operations
|
||||
// ("select user: ...") may reach the client.
|
||||
if eb.Error.Message != "database unavailable" {
|
||||
t.Fatalf("client message = %q, want only the application message", eb.Error.Message)
|
||||
}
|
||||
if !strings.Contains(logs.String(), "connection refused") || !strings.Contains(logs.String(), eb.Error.RequestID) {
|
||||
t.Fatalf("log must carry the cause and the request id, got:\n%s", logs.String())
|
||||
}
|
||||
}
|
||||
|
||||
// Everything below Timeout sees one deadline, so waiting on a dependency
|
||||
// ends with the request instead of holding the client.
|
||||
func TestTimeoutBoundsTheRequestContext(t *testing.T) {
|
||||
e := newEngine(t, nil)
|
||||
e.GET("/slow", httpx.Timeout(50*time.Millisecond), func(c *gin.Context) {
|
||||
select {
|
||||
case <-c.Request.Context().Done():
|
||||
httpx.Error(c, apperr.Wrap(apperr.KindUnavailable, "gave up", c.Request.Context().Err()))
|
||||
case <-time.After(5 * time.Second):
|
||||
httpx.JSON(c, http.StatusOK, gin.H{})
|
||||
}
|
||||
})
|
||||
start := time.Now()
|
||||
rec, eb := do(t, e, http.MethodGet, "/slow", "", nil)
|
||||
assertError(t, rec, eb, http.StatusServiceUnavailable, "unavailable")
|
||||
if elapsed := time.Since(start); elapsed > time.Second {
|
||||
t.Fatalf("request took %v, want it cut at the 50ms deadline", elapsed)
|
||||
}
|
||||
}
|
||||
|
||||
func logEntry(t *testing.T, logs *bytes.Buffer) map[string]any {
|
||||
t.Helper()
|
||||
var entry map[string]any
|
||||
line := lastLine(logs.String())
|
||||
if err := json.Unmarshal([]byte(line), &entry); err != nil {
|
||||
t.Fatalf("log line is not JSON: %v: %s", err, line)
|
||||
}
|
||||
return entry
|
||||
}
|
||||
|
||||
func TestTimestampFormat(t *testing.T) {
|
||||
ts := time.Date(2026, 9, 1, 12, 0, 0, 0, time.FixedZone("MSK", 3*3600))
|
||||
if got := httpx.Timestamp(ts); got != "2026-09-01T09:00:00.000Z" {
|
||||
t.Fatalf("Timestamp = %q", got)
|
||||
}
|
||||
ts = time.Date(2026, 9, 1, 12, 0, 0, 7_000_000, time.UTC)
|
||||
if got := httpx.Timestamp(ts); got != "2026-09-01T12:00:00.007Z" {
|
||||
t.Fatalf("Timestamp = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func lastLine(s string) string {
|
||||
lines := strings.Split(strings.TrimSpace(s), "\n")
|
||||
return lines[len(lines)-1]
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const keyUserID = "messenger.user_id"
|
||||
|
||||
// SetUserID records the authenticated caller for the handlers behind the
|
||||
// authentication middleware and for the access log.
|
||||
func SetUserID(c *gin.Context, id uuid.UUID) {
|
||||
c.Set(keyUserID, id)
|
||||
}
|
||||
|
||||
func UserID(c *gin.Context) (uuid.UUID, bool) {
|
||||
v, ok := c.Get(keyUserID)
|
||||
if !ok {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
id, ok := v.(uuid.UUID)
|
||||
return id, ok
|
||||
}
|
||||
|
||||
// UserHandler is a handler that needs the authenticated caller.
|
||||
type UserHandler func(c *gin.Context, actor uuid.UUID)
|
||||
|
||||
// WithUser adapts a UserHandler to gin. Reaching it without the
|
||||
// authentication middleware in front is a wiring mistake, so it is reported
|
||||
// as an internal error rather than blamed on the client with a 401.
|
||||
func WithUser(h UserHandler) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
actor, ok := UserID(c)
|
||||
if !ok {
|
||||
Error(c, errors.New("route is missing the authentication middleware"))
|
||||
return
|
||||
}
|
||||
h(c, actor)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"runtime/debug"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// requestID assigns a fresh identifier to every request.
|
||||
//
|
||||
// An incoming X-Request-Id is deliberately not reused. This service is
|
||||
// client-facing: there is no trusted upstream that could have minted the
|
||||
// value, so honouring it would let any caller pick an arbitrary string that
|
||||
// ends up in logs (log injection, unbounded length) or collides with another
|
||||
// request and corrupts log correlation. Reusing the header is the right
|
||||
// choice only when the caller is a trusted proxy or a sibling service that
|
||||
// propagates one id across a chain of calls.
|
||||
func requestID(log *slog.Logger) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
var raw [16]byte
|
||||
if _, err := rand.Read(raw[:]); err != nil {
|
||||
panic("httpx: random source unavailable: " + err.Error())
|
||||
}
|
||||
id := hex.EncodeToString(raw[:])
|
||||
c.Set(keyRequestID, id)
|
||||
c.Set(keyLogger, log)
|
||||
c.Writer.Header().Set("X-Request-Id", id)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// accessLog writes one line per request at a level that follows the
|
||||
// outcome (2xx debug, 4xx info, 5xx error), so the ordinary log level decides
|
||||
// how much traffic is recorded. Headers, query strings and bodies are never
|
||||
// recorded. time.Since uses the monotonic clock, so wall clock adjustments
|
||||
// cannot distort the duration.
|
||||
func accessLog() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
c.Next()
|
||||
|
||||
status := c.Writer.Status()
|
||||
level := slog.LevelDebug
|
||||
switch {
|
||||
case status >= http.StatusInternalServerError:
|
||||
level = slog.LevelError
|
||||
case status >= http.StatusBadRequest:
|
||||
level = slog.LevelInfo
|
||||
}
|
||||
log := logger(c)
|
||||
ctx := c.Request.Context()
|
||||
if !log.Enabled(ctx, level) {
|
||||
return
|
||||
}
|
||||
attrs := []any{
|
||||
"request_id", RequestID(c),
|
||||
"method", c.Request.Method,
|
||||
"path", c.Request.URL.Path,
|
||||
"status", status,
|
||||
"duration_ms", float64(time.Since(start).Microseconds()) / 1000,
|
||||
"bytes", c.Writer.Size(),
|
||||
}
|
||||
if id, ok := UserID(c); ok {
|
||||
attrs = append(attrs, "user_id", id.String())
|
||||
}
|
||||
log.Log(ctx, level, "request", attrs...)
|
||||
}
|
||||
}
|
||||
|
||||
// recovery turns a panic into a contract 500; the stack goes to the log only.
|
||||
func recovery() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
logger(c).Error("panic recovered",
|
||||
"request_id", RequestID(c),
|
||||
"panic", fmt.Sprint(r),
|
||||
"stack", string(debug.Stack()),
|
||||
)
|
||||
if !c.Writer.Written() {
|
||||
Fail(c, http.StatusInternalServerError, "internal_error", "internal error")
|
||||
}
|
||||
c.Abort()
|
||||
}
|
||||
}()
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// Timeout bounds the whole request with one deadline: waiting for a pooled
|
||||
// connection, SQL statements and session lookups all observe it, so a hung
|
||||
// dependency ends in a 503 within d instead of holding the client.
|
||||
func Timeout(d time.Duration) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), d)
|
||||
defer cancel()
|
||||
c.Request = c.Request.WithContext(ctx)
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// bodyLimit caps request bodies; exceeding it surfaces from the JSON
|
||||
// decoder as *http.MaxBytesError.
|
||||
func bodyLimit(maxBytes int64) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
if c.Request.Body != nil {
|
||||
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, maxBytes)
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
)
|
||||
|
||||
type errorEnvelope struct {
|
||||
Error errorDetail `json:"error"`
|
||||
}
|
||||
|
||||
type errorDetail struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
|
||||
type statusCode struct {
|
||||
status int
|
||||
code string
|
||||
}
|
||||
|
||||
var kinds = map[apperr.Kind]statusCode{
|
||||
apperr.KindInvalid: {http.StatusBadRequest, "invalid_request"},
|
||||
apperr.KindUnauthorized: {http.StatusUnauthorized, "unauthorized"},
|
||||
apperr.KindNotFound: {http.StatusNotFound, "not_found"},
|
||||
apperr.KindConflict: {http.StatusConflict, "conflict"},
|
||||
apperr.KindPayloadTooLarge: {http.StatusRequestEntityTooLarge, "payload_too_large"},
|
||||
apperr.KindUnsupportedMedia: {http.StatusUnsupportedMediaType, "unsupported_media_type"},
|
||||
apperr.KindUnavailable: {http.StatusServiceUnavailable, "unavailable"},
|
||||
}
|
||||
|
||||
// JSON is the single place that fixes the response media type; PureJSON
|
||||
// keeps non-ASCII and HTML characters verbatim.
|
||||
func JSON(c *gin.Context, status int, body any) {
|
||||
c.PureJSON(status, body)
|
||||
}
|
||||
|
||||
// Fail writes the contract error body and stops the handler chain.
|
||||
func Fail(c *gin.Context, status int, code, message string) {
|
||||
JSON(c, status, errorEnvelope{Error: errorDetail{
|
||||
Code: code,
|
||||
Message: message,
|
||||
RequestID: RequestID(c),
|
||||
}})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
// Error maps an application error to its status. Anything that is not an
|
||||
// application error is logged with its text and reported as an opaque 500,
|
||||
// so internal details never reach the client. An application error shows the
|
||||
// client only its own message, never the context it was wrapped in on the
|
||||
// way up; when it is answered with a 5xx (a dependency outage) the full
|
||||
// chain and the cause go to the log for the operator.
|
||||
func Error(c *gin.Context, err error) {
|
||||
appErr, isApp := errors.AsType[*apperr.Error](err)
|
||||
var mapped statusCode
|
||||
known := false
|
||||
if isApp {
|
||||
mapped, known = kinds[appErr.Kind]
|
||||
}
|
||||
if !known {
|
||||
logger(c).Error("request failed", "request_id", RequestID(c), "error", err.Error())
|
||||
Fail(c, http.StatusInternalServerError, "internal_error", "internal error")
|
||||
return
|
||||
}
|
||||
if mapped.status >= http.StatusInternalServerError {
|
||||
attrs := []any{"request_id", RequestID(c), "error", err.Error()}
|
||||
if appErr.Cause != nil {
|
||||
attrs = append(attrs, "cause", appErr.Cause.Error())
|
||||
}
|
||||
logger(c).Error("request failed", attrs...)
|
||||
}
|
||||
Fail(c, mapped.status, mapped.code, appErr.Message)
|
||||
}
|
||||
|
||||
// Timestamp renders RFC 3339 UTC with a fixed millisecond width: Python 3.9
|
||||
// (the oldest version the public tests support) only parses 3 or 6 fractional
|
||||
// digits, and RFC3339Nano would trim trailing zeros to a variable width.
|
||||
func Timestamp(t time.Time) string {
|
||||
return t.UTC().Format("2006-01-02T15:04:05.000Z")
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
// Package ident generates and parses entity identifiers.
|
||||
//
|
||||
// Identifiers are UUIDv7 (RFC 9562): the first 48 bits carry a Unix
|
||||
// millisecond timestamp and google/uuid keeps them strictly increasing within
|
||||
// one process even when the system clock steps backwards, so lexicographic
|
||||
// order of ids equals creation order.
|
||||
package ident
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var ErrInvalid = errors.New("invalid uuid")
|
||||
|
||||
// New panics only if the platform random source is unavailable, which is a
|
||||
// fatal environment fault rather than a request error.
|
||||
func New() uuid.UUID {
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
panic("ident: random source unavailable: " + err.Error())
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
// TimeOf extracts the creation time carried by a UUIDv7 as UTC with
|
||||
// millisecond precision.
|
||||
func TimeOf(id uuid.UUID) time.Time {
|
||||
sec, nsec := id.Time().UnixTime()
|
||||
return time.Unix(sec, nsec).UTC()
|
||||
}
|
||||
|
||||
// Parse accepts only the canonical 36-character form; uuid.Parse alone would
|
||||
// also take braces, urn prefixes and bare hex.
|
||||
func Parse(raw string) (uuid.UUID, error) {
|
||||
if len(raw) != 36 {
|
||||
return uuid.Nil, ErrInvalid
|
||||
}
|
||||
id, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
return uuid.Nil, ErrInvalid
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ident_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
func TestNewProducesVersion7(t *testing.T) {
|
||||
id := ident.New()
|
||||
if got := id.Version(); got != 7 {
|
||||
t.Fatalf("version = %d, want 7", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewIsStrictlyIncreasingWithinProcess(t *testing.T) {
|
||||
prev := ident.New()
|
||||
for i := 0; i < 20000; i++ {
|
||||
next := ident.New()
|
||||
if bytes.Compare(next[:], prev[:]) <= 0 {
|
||||
t.Fatalf("id %s is not greater than previous %s at iteration %d", next, prev, i)
|
||||
}
|
||||
prev = next
|
||||
}
|
||||
}
|
||||
|
||||
func TestTimeOfMatchesWallClockAtMillisecondPrecision(t *testing.T) {
|
||||
before := time.Now().Truncate(time.Millisecond)
|
||||
id := ident.New()
|
||||
after := time.Now().Add(time.Millisecond)
|
||||
|
||||
got := ident.TimeOf(id)
|
||||
if got.Location() != time.UTC {
|
||||
t.Fatalf("TimeOf must return UTC, got %v", got.Location())
|
||||
}
|
||||
if got.Before(before) || got.After(after) {
|
||||
t.Fatalf("TimeOf = %v, want within [%v, %v]", got, before, after)
|
||||
}
|
||||
if got.Nanosecond()%int(time.Millisecond) != 0 {
|
||||
t.Fatalf("TimeOf must be truncated to milliseconds, got %v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseAcceptsCanonicalForm(t *testing.T) {
|
||||
id := ident.New()
|
||||
parsed, err := ident.Parse(id.String())
|
||||
if err != nil {
|
||||
t.Fatalf("Parse(%q) error: %v", id, err)
|
||||
}
|
||||
if parsed != id {
|
||||
t.Fatalf("Parse round trip mismatch: %s != %s", parsed, id)
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseRejectsNonCanonicalForms(t *testing.T) {
|
||||
id := ident.New().String()
|
||||
for _, raw := range []string{
|
||||
"",
|
||||
"not-a-uuid",
|
||||
"{" + id + "}",
|
||||
"urn:uuid:" + id,
|
||||
id[:32] + id[33:], // hex without one dash
|
||||
id + "0",
|
||||
" " + id,
|
||||
} {
|
||||
if _, err := ident.Parse(raw); err == nil {
|
||||
t.Errorf("Parse(%q) must fail", raw)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Package pagination implements keyset paging. A cursor is opaque to the
|
||||
// client and bound to the resource it pages (the chat for history, the user
|
||||
// for the chat list), so a cursor from one resource is rejected on another.
|
||||
// Because ids are UUIDv7 and creation time derives from the id, the keyset
|
||||
// (created_at, id) collapses to the id of the last returned item.
|
||||
package pagination
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
DefaultLimit = 50
|
||||
MaxLimit = 100
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidCursor = errors.New("invalid cursor")
|
||||
ErrInvalidLimit = errors.New("limit must be an integer between 1 and 100")
|
||||
)
|
||||
|
||||
const cursorVersion = 1
|
||||
|
||||
func EncodeCursor(scope, last uuid.UUID) string {
|
||||
raw := make([]byte, 0, 1+16+16)
|
||||
raw = append(raw, cursorVersion)
|
||||
raw = append(raw, scope[:]...)
|
||||
raw = append(raw, last[:]...)
|
||||
return base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
|
||||
func DecodeCursor(token string, scope uuid.UUID) (uuid.UUID, error) {
|
||||
raw, err := base64.RawURLEncoding.DecodeString(token)
|
||||
if err != nil || len(raw) != 1+16+16 || raw[0] != cursorVersion {
|
||||
return uuid.Nil, ErrInvalidCursor
|
||||
}
|
||||
if got, err := uuid.FromBytes(raw[1:17]); err != nil || got != scope {
|
||||
return uuid.Nil, ErrInvalidCursor
|
||||
}
|
||||
last, err := uuid.FromBytes(raw[17:])
|
||||
if err != nil {
|
||||
return uuid.Nil, ErrInvalidCursor
|
||||
}
|
||||
return last, nil
|
||||
}
|
||||
|
||||
// ParseLimit accepts an unsigned decimal between 1 and MaxLimit; an empty
|
||||
// value means DefaultLimit. ParseUint rejects signs, whitespace and
|
||||
// fractions and reports overflow, so no length guard is needed.
|
||||
func ParseLimit(raw string) (int, error) {
|
||||
if raw == "" {
|
||||
return DefaultLimit, nil
|
||||
}
|
||||
n, err := strconv.ParseUint(raw, 10, 32)
|
||||
if err != nil || n < 1 || n > MaxLimit {
|
||||
return 0, ErrInvalidLimit
|
||||
}
|
||||
return int(n), nil
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package pagination_test
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
func TestCursorRoundTrip(t *testing.T) {
|
||||
scope, last := ident.New(), ident.New()
|
||||
encoded := pagination.EncodeCursor(scope, last)
|
||||
if encoded == "" || strings.ContainsAny(encoded, "+/=") {
|
||||
t.Fatalf("cursor must be non-empty URL-safe base64 without padding, got %q", encoded)
|
||||
}
|
||||
got, err := pagination.DecodeCursor(encoded, scope)
|
||||
if err != nil {
|
||||
t.Fatalf("DecodeCursor error: %v", err)
|
||||
}
|
||||
if got != last {
|
||||
t.Fatalf("decoded %s, want %s", got, last)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeCursorRejectsForeignScope(t *testing.T) {
|
||||
encoded := pagination.EncodeCursor(ident.New(), ident.New())
|
||||
_, err := pagination.DecodeCursor(encoded, ident.New())
|
||||
if !errors.Is(err, pagination.ErrInvalidCursor) {
|
||||
t.Fatalf("err = %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDecodeCursorRejectsGarbage(t *testing.T) {
|
||||
scope := ident.New()
|
||||
valid := pagination.EncodeCursor(scope, ident.New())
|
||||
for _, raw := range []string{"", "***", "abc", valid[:len(valid)-2], valid + "AA", strings.ToUpper(valid)} {
|
||||
if _, err := pagination.DecodeCursor(raw, scope); !errors.Is(err, pagination.ErrInvalidCursor) {
|
||||
t.Errorf("DecodeCursor(%q) err = %v, want ErrInvalidCursor", raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestParseLimit(t *testing.T) {
|
||||
cases := []struct {
|
||||
raw string
|
||||
want int
|
||||
ok bool
|
||||
}{
|
||||
{"", 50, true},
|
||||
{"1", 1, true},
|
||||
{"100", 100, true},
|
||||
{"0", 0, false},
|
||||
{"101", 0, false},
|
||||
{"-1", 0, false},
|
||||
{"+5", 0, false},
|
||||
{"5.0", 0, false},
|
||||
{"abc", 0, false},
|
||||
{" 5", 0, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got, err := pagination.ParseLimit(tc.raw)
|
||||
if tc.ok && (err != nil || got != tc.want) {
|
||||
t.Errorf("ParseLimit(%q) = %d, %v; want %d", tc.raw, got, err, tc.want)
|
||||
}
|
||||
if !tc.ok && !errors.Is(err, pagination.ErrInvalidLimit) {
|
||||
t.Errorf("ParseLimit(%q) err = %v, want ErrInvalidLimit", tc.raw, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package pagination
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
// Page is the service-level page: the items and the id to continue from,
|
||||
// nil when the listing is exhausted. It carries the raw keyset id so that any
|
||||
// transport can encode the continuation its own way.
|
||||
type Page[T any] struct {
|
||||
Items []T
|
||||
Next *uuid.UUID
|
||||
}
|
||||
|
||||
// PageResponse is the wire form of a page with the continuation already
|
||||
// encoded as an opaque cursor.
|
||||
type PageResponse[T any] struct {
|
||||
Items []T `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// Cut turns a slice over-fetched by one item into a page of at most limit
|
||||
// items; the extra item only proves that a next page exists.
|
||||
func Cut[T any](items []T, limit int, id func(T) uuid.UUID) Page[T] {
|
||||
if len(items) <= limit {
|
||||
return Page[T]{Items: items}
|
||||
}
|
||||
items = items[:limit]
|
||||
next := id(items[limit-1])
|
||||
return Page[T]{Items: items, Next: &next}
|
||||
}
|
||||
|
||||
func ToResponse[D, R any](p Page[D], scope uuid.UUID, convert func(D) R) PageResponse[R] {
|
||||
out := PageResponse[R]{Items: make([]R, 0, len(p.Items))}
|
||||
for _, item := range p.Items {
|
||||
out.Items = append(out.Items, convert(item))
|
||||
}
|
||||
if p.Next != nil {
|
||||
cursor := EncodeCursor(scope, *p.Next)
|
||||
out.NextCursor = &cursor
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package pagination_test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/pagination"
|
||||
)
|
||||
|
||||
func self(id uuid.UUID) uuid.UUID { return id }
|
||||
|
||||
func TestCutTrimsOverfetchAndDerivesNext(t *testing.T) {
|
||||
ids := []uuid.UUID{ident.New(), ident.New(), ident.New()}
|
||||
|
||||
fewer := pagination.Cut(ids[:2], 5, self)
|
||||
if len(fewer.Items) != 2 || fewer.Next != nil {
|
||||
t.Fatalf("fewer than limit: %+v", fewer)
|
||||
}
|
||||
exact := pagination.Cut(ids[:2], 2, self)
|
||||
if len(exact.Items) != 2 || exact.Next != nil {
|
||||
t.Fatalf("exactly limit must not continue: %+v", exact)
|
||||
}
|
||||
over := pagination.Cut(ids, 2, self)
|
||||
if len(over.Items) != 2 || over.Next == nil || *over.Next != ids[1] {
|
||||
t.Fatalf("over-fetched page must trim and continue from the last kept id: %+v", over)
|
||||
}
|
||||
if over.Items[0] != ids[0] || over.Items[1] != ids[1] {
|
||||
t.Fatalf("order changed: %v", over.Items)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToResponseEncodesCursorWithinScope(t *testing.T) {
|
||||
scope, last := ident.New(), ident.New()
|
||||
page := pagination.Page[uuid.UUID]{Items: []uuid.UUID{last}, Next: &last}
|
||||
res := pagination.ToResponse(page, scope, func(id uuid.UUID) string { return id.String() })
|
||||
if len(res.Items) != 1 || res.Items[0] != last.String() {
|
||||
t.Fatalf("items = %v", res.Items)
|
||||
}
|
||||
if res.NextCursor == nil {
|
||||
t.Fatal("next cursor missing")
|
||||
}
|
||||
got, err := pagination.DecodeCursor(*res.NextCursor, scope)
|
||||
if err != nil || got != last {
|
||||
t.Fatalf("cursor decodes to %s, %v; want %s", got, err, last)
|
||||
}
|
||||
}
|
||||
|
||||
func TestToResponseSerializesEmptyPageAsContractRequires(t *testing.T) {
|
||||
res := pagination.ToResponse(pagination.Page[uuid.UUID]{}, ident.New(), func(id uuid.UUID) string { return id.String() })
|
||||
raw, err := json.Marshal(res)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if string(raw) != `{"items":[],"next_cursor":null}` {
|
||||
t.Fatalf("empty page = %s", raw)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// Package password hashes user passwords with Argon2id.
|
||||
//
|
||||
// A password has little entropy, so its hash must be slow and memory-hard to
|
||||
// make offline guessing expensive. Session tokens are the opposite case: they
|
||||
// are random 256-bit values, and a fast digest is enough for them.
|
||||
package password
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
// OWASP baseline for Argon2id. The parameters are written into every hash,
|
||||
// so raising them later does not invalidate stored hashes.
|
||||
const (
|
||||
memoryKiB = 19 * 1024
|
||||
iterations = 2
|
||||
threads = 1
|
||||
saltLen = 16
|
||||
keyLen = 32
|
||||
)
|
||||
|
||||
var ErrMalformed = errors.New("malformed password hash")
|
||||
|
||||
var b64 = base64.RawStdEncoding
|
||||
|
||||
// Hash returns the PHC string $argon2id$v=19$m=…,t=…,p=…$<salt>$<key>.
|
||||
func Hash(plain string) string {
|
||||
salt := make([]byte, saltLen)
|
||||
// Since Go 1.24 crypto/rand.Read never returns an error.
|
||||
_, _ = rand.Read(salt)
|
||||
key := argon2.IDKey([]byte(plain), salt, iterations, memoryKiB, threads, keyLen)
|
||||
return fmt.Sprintf("$argon2id$v=%d$%s$%s$%s",
|
||||
argon2.Version, params(memoryKiB, iterations, threads), b64.EncodeToString(salt), b64.EncodeToString(key))
|
||||
}
|
||||
|
||||
// Verify reports whether plain matches encoded. An error means the stored
|
||||
// hash itself is unreadable, not that the password is wrong.
|
||||
func Verify(encoded, plain string) (bool, error) {
|
||||
parts := strings.Split(encoded, "$")
|
||||
if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" {
|
||||
return false, ErrMalformed
|
||||
}
|
||||
var version int
|
||||
if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil || version != argon2.Version {
|
||||
return false, ErrMalformed
|
||||
}
|
||||
var memory, time uint32
|
||||
var parallelism uint8
|
||||
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &time, ¶llelism); err != nil ||
|
||||
memory == 0 || time == 0 || parallelism == 0 {
|
||||
return false, ErrMalformed
|
||||
}
|
||||
salt, err := b64.DecodeString(parts[4])
|
||||
if err != nil || len(salt) == 0 {
|
||||
return false, ErrMalformed
|
||||
}
|
||||
key, err := b64.DecodeString(parts[5])
|
||||
if err != nil || len(key) == 0 {
|
||||
return false, ErrMalformed
|
||||
}
|
||||
got := argon2.IDKey([]byte(plain), salt, time, memory, parallelism, uint32(len(key)))
|
||||
return subtle.ConstantTimeCompare(got, key) == 1, nil
|
||||
}
|
||||
|
||||
func params(memory, time uint32, parallelism uint8) string {
|
||||
return fmt.Sprintf("m=%d,t=%d,p=%d", memory, time, parallelism)
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package password_test
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
|
||||
"messenger/internal/platform/password"
|
||||
)
|
||||
|
||||
func TestHashVerifiesOnlyTheOriginalPassword(t *testing.T) {
|
||||
for _, plain := range []string{
|
||||
"correct horse battery staple",
|
||||
"с:двоеточием:и кириллицей",
|
||||
"emoji 👋👋👋 and : colon",
|
||||
} {
|
||||
encoded := password.Hash(plain)
|
||||
ok, err := password.Verify(encoded, plain)
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("Verify(%q) = %v, %v; want true", plain, ok, err)
|
||||
}
|
||||
ok, err = password.Verify(encoded, plain+"x")
|
||||
if err != nil || ok {
|
||||
t.Fatalf("Verify(wrong) = %v, %v; want false", ok, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashIsSaltedPHCString(t *testing.T) {
|
||||
a, b := password.Hash("same password"), password.Hash("same password")
|
||||
if a == b {
|
||||
t.Fatal("two hashes of one password are equal: salt is missing")
|
||||
}
|
||||
if !strings.HasPrefix(a, "$argon2id$v=19$m=19456,t=2,p=1$") {
|
||||
t.Fatalf("unexpected encoding %q", a)
|
||||
}
|
||||
if strings.Contains(a, "same password") {
|
||||
t.Fatal("hash contains the plaintext")
|
||||
}
|
||||
}
|
||||
|
||||
// A hash made with other parameters must still verify: parameters travel in
|
||||
// the encoded string, so they can be raised without invalidating old hashes.
|
||||
func TestVerifyUsesParametersFromTheEncoding(t *testing.T) {
|
||||
salt := make([]byte, 16)
|
||||
_, _ = rand.Read(salt)
|
||||
key := argon2.IDKey([]byte("legacy password"), salt, 1, 8*1024, 2, 32)
|
||||
b64 := base64.RawStdEncoding
|
||||
encoded := fmt.Sprintf("$argon2id$v=19$m=8192,t=1,p=2$%s$%s", b64.EncodeToString(salt), b64.EncodeToString(key))
|
||||
|
||||
ok, err := password.Verify(encoded, "legacy password")
|
||||
if err != nil || !ok {
|
||||
t.Fatalf("Verify = %v, %v; want true", ok, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVerifyRejectsMalformedEncoding(t *testing.T) {
|
||||
valid := password.Hash("whatever password")
|
||||
parts := strings.Split(valid, "$")
|
||||
for name, encoded := range map[string]string{
|
||||
"empty": "",
|
||||
"wrong algo": strings.Replace(valid, "argon2id", "argon2i", 1),
|
||||
"wrong version": strings.Replace(valid, "v=19", "v=16", 1),
|
||||
"bad params": strings.Replace(valid, "m=19456,t=2,p=1", "m=x,t=2,p=1", 1),
|
||||
"zero params": strings.Replace(valid, "m=19456,t=2,p=1", "m=19456,t=0,p=1", 1),
|
||||
"bad salt": strings.Join([]string{"", parts[1], parts[2], parts[3], "!!!", parts[5]}, "$"),
|
||||
"bad key": strings.Join([]string{"", parts[1], parts[2], parts[3], parts[4], "!!!"}, "$"),
|
||||
"missing fields": strings.Join(parts[:5], "$"),
|
||||
} {
|
||||
if _, err := password.Verify(encoded, "whatever password"); err == nil {
|
||||
t.Errorf("%s: Verify accepted %q", name, encoded)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// Package postgres opens the connection pool and applies the schema.
|
||||
//
|
||||
// Pool size and server-side timeouts are constants rather than settings: they
|
||||
// bound how long one request can hold a connection, and the request deadline
|
||||
// of the API is chosen against them.
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/jackc/pgx/v5/stdlib"
|
||||
"github.com/pressly/goose/v3"
|
||||
"github.com/pressly/goose/v3/lock"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/migrations"
|
||||
)
|
||||
|
||||
const (
|
||||
maxConns = 10
|
||||
connectTimeout = time.Second
|
||||
statementTimeout = 5 * time.Second
|
||||
idleInTxTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
const codeUniqueViolation = "23505"
|
||||
|
||||
func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse database url: %w", err)
|
||||
}
|
||||
cfg.MaxConns = maxConns
|
||||
cfg.ConnConfig.ConnectTimeout = connectTimeout
|
||||
// Unlike net.Dialer, pgx resolves the host before ConnectTimeout starts.
|
||||
// Docker DNS takes seconds to give up on a stopped container: unbounded,
|
||||
// every request would wait out its deadline and log only "context
|
||||
// deadline exceeded" instead of the failed lookup.
|
||||
cfg.ConnConfig.LookupFunc = func(ctx context.Context, host string) ([]string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, connectTimeout)
|
||||
defer cancel()
|
||||
return net.DefaultResolver.LookupHost(ctx, host)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["statement_timeout"] = strconv.FormatInt(statementTimeout.Milliseconds(), 10)
|
||||
cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = strconv.FormatInt(idleInTxTimeout.Milliseconds(), 10)
|
||||
// The application keeps every time in UTC; pgx would otherwise scan
|
||||
// timestamptz into time.Local.
|
||||
cfg.AfterConnect = func(_ context.Context, conn *pgx.Conn) error {
|
||||
conn.TypeMap().RegisterType(&pgtype.Type{
|
||||
Name: "timestamptz",
|
||||
OID: pgtype.TimestamptzOID,
|
||||
Codec: &pgtype.TimestamptzCodec{ScanLocation: time.UTC},
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create pool: %w", err)
|
||||
}
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping database: %w", err)
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
// Migrate applies pending migrations and returns the versions it applied. A
|
||||
// session advisory lock serialises concurrent starts, so several API
|
||||
// replicas may call it at once; applied versions are recorded, which makes a
|
||||
// repeated run a no-op.
|
||||
func Migrate(ctx context.Context, pool *pgxpool.Pool) ([]int64, error) {
|
||||
db := stdlib.OpenDBFromPool(pool)
|
||||
defer db.Close()
|
||||
locker, err := lock.NewPostgresSessionLocker()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("migration lock: %w", err)
|
||||
}
|
||||
provider, err := goose.NewProvider(goose.DialectPostgres, db, migrations.FS, goose.WithSessionLocker(locker))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load migrations: %w", err)
|
||||
}
|
||||
results, err := provider.Up(ctx)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("apply migrations: %w", err)
|
||||
}
|
||||
applied := make([]int64, 0, len(results))
|
||||
for _, r := range results {
|
||||
applied = append(applied, r.Source.Version)
|
||||
}
|
||||
return applied, nil
|
||||
}
|
||||
|
||||
// Classify marks a failure to reach the database as apperr.KindUnavailable,
|
||||
// which the API answers with 503 instead of 500; any other error is returned
|
||||
// unchanged. Unreachable means: the connection could not be made or broke,
|
||||
// the request deadline passed, or the server refused work (PostgreSQL error
|
||||
// classes 08 connection exception, 53 insufficient resources, 57 operator
|
||||
// intervention such as shutdown or statement timeout).
|
||||
func Classify(err error) error {
|
||||
if err == nil || !unreachable(err) {
|
||||
return err
|
||||
}
|
||||
return apperr.Wrap(apperr.KindUnavailable, "database unavailable", err)
|
||||
}
|
||||
|
||||
func unreachable(err error) bool {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
switch class := pgErr.Code[:min(2, len(pgErr.Code))]; class {
|
||||
case "08", "53", "57":
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
if _, ok := errors.AsType[*pgconn.ConnectError](err); ok {
|
||||
return true
|
||||
}
|
||||
if _, ok := errors.AsType[net.Error](err); ok {
|
||||
return true
|
||||
}
|
||||
return errors.Is(err, context.DeadlineExceeded) ||
|
||||
errors.Is(err, context.Canceled) ||
|
||||
errors.Is(err, io.EOF) ||
|
||||
errors.Is(err, io.ErrUnexpectedEOF) ||
|
||||
errors.Is(err, net.ErrClosed) ||
|
||||
pgconn.Timeout(err) ||
|
||||
pgconn.SafeToRetry(err)
|
||||
}
|
||||
|
||||
// IsUniqueViolation reports whether err is a unique violation of the named
|
||||
// constraint.
|
||||
func IsUniqueViolation(err error, constraint string) bool {
|
||||
pgErr, ok := errors.AsType[*pgconn.PgError](err)
|
||||
return ok && pgErr.Code == codeUniqueViolation && pgErr.ConstraintName == constraint
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
package postgres_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
var db *testenv.DB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
db, err = testenv.StartPostgres(context.Background())
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func TestPoolScansTimestamptzInUTC(t *testing.T) {
|
||||
var got time.Time
|
||||
if err := db.Pool.QueryRow(context.Background(), `SELECT now()`).Scan(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got.Location() != time.UTC {
|
||||
t.Fatalf("timestamptz scanned in %v, want UTC", got.Location())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMigrateIsRepeatable(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
applied, err := postgres.Migrate(ctx, db.Pool)
|
||||
if err != nil {
|
||||
t.Fatalf("second migrate: %v", err)
|
||||
}
|
||||
if len(applied) != 0 {
|
||||
t.Fatalf("second migrate applied %v, want nothing", applied)
|
||||
}
|
||||
var version int64
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT max(version_id) FROM goose_db_version`).Scan(&version); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if version != 1 {
|
||||
t.Fatalf("schema version = %d, want 1", version)
|
||||
}
|
||||
for _, table := range []string{"users", "chats", "chat_members", "messages"} {
|
||||
var exists bool
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT to_regclass('public.' || $1) IS NOT NULL`, table).Scan(&exists); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !exists {
|
||||
t.Fatalf("table %s is missing", table)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestPoolAppliesLimitsAndTimeouts(t *testing.T) {
|
||||
if got := db.Pool.Config().MaxConns; got != 10 {
|
||||
t.Fatalf("MaxConns = %d, want 10", got)
|
||||
}
|
||||
if got := db.Pool.Config().ConnConfig.ConnectTimeout; got != time.Second {
|
||||
t.Fatalf("ConnectTimeout = %v, want 1s", got)
|
||||
}
|
||||
ctx := context.Background()
|
||||
for setting, want := range map[string]string{
|
||||
"statement_timeout": "5s",
|
||||
"idle_in_transaction_session_timeout": "10s",
|
||||
} {
|
||||
var got string
|
||||
if err := db.Pool.QueryRow(ctx, "SHOW "+setting).Scan(&got); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if got != want {
|
||||
t.Fatalf("%s = %q, want %q", setting, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestResetEmptiesEveryTableButKeepsSchemaVersion(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
_, err := db.Pool.Exec(ctx, `
|
||||
WITH u AS (
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES ('0190f1d2-0000-7000-8000-000000000001', 'reset_check', 'Reset', 'x', now())
|
||||
RETURNING id
|
||||
), c AS (
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
VALUES ('0190f1d2-0000-7000-8000-000000000002', 'Reset', now())
|
||||
RETURNING id
|
||||
), m AS (
|
||||
INSERT INTO chat_members (chat_id, user_id) SELECT c.id, u.id FROM c, u
|
||||
)
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT '0190f1d2-0000-7000-8000-000000000003', c.id, u.id, 'hi', now() FROM c, u`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.Reset(t)
|
||||
|
||||
for _, table := range []string{"users", "chats", "chat_members", "messages"} {
|
||||
var n int
|
||||
if err := db.Pool.QueryRow(ctx, "SELECT count(*) FROM "+table).Scan(&n); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("%s has %d rows after Reset", table, n)
|
||||
}
|
||||
}
|
||||
var versions int
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM goose_db_version`).Scan(&versions); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if versions == 0 {
|
||||
t.Fatal("Reset wiped the schema version table")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsUniqueViolation(t *testing.T) {
|
||||
db.Reset(t)
|
||||
ctx := context.Background()
|
||||
insert := `INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES ($1, 'taken', 'Taken', 'x', now())`
|
||||
if _, err := db.Pool.Exec(ctx, insert, "0190f1d2-0000-7000-8000-000000000011"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, dup := db.Pool.Exec(ctx, insert, "0190f1d2-0000-7000-8000-000000000012")
|
||||
if dup == nil {
|
||||
t.Fatal("duplicate username was accepted")
|
||||
}
|
||||
|
||||
if !postgres.IsUniqueViolation(dup, "users_username_key") {
|
||||
t.Fatalf("not recognised as a username violation: %v", dup)
|
||||
}
|
||||
if postgres.IsUniqueViolation(dup, "some_other_key") {
|
||||
t.Fatal("matched a different constraint")
|
||||
}
|
||||
if postgres.IsUniqueViolation(fmt.Errorf("wrapped: %w", dup), "users_username_key") == false {
|
||||
t.Fatal("wrapped error not recognised")
|
||||
}
|
||||
if postgres.IsUniqueViolation(errors.New("plain"), "users_username_key") || postgres.IsUniqueViolation(nil, "users_username_key") {
|
||||
t.Fatal("non-database error recognised as a violation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyMarksUnreachableDatabaseAsUnavailable(t *testing.T) {
|
||||
unavailable := map[string]error{
|
||||
"admin shutdown": &pgconn.PgError{Code: "57P01"},
|
||||
"statement timeout": &pgconn.PgError{Code: "57014"},
|
||||
"connection failure": &pgconn.PgError{Code: "08006"},
|
||||
"too many connections": &pgconn.PgError{Code: "53300"},
|
||||
"deadline": fmt.Errorf("query: %w", context.DeadlineExceeded),
|
||||
"network": &net.OpError{Op: "read", Net: "tcp", Err: errors.New("connection reset")},
|
||||
"unexpected eof": fmt.Errorf("read: %w", io.ErrUnexpectedEOF),
|
||||
}
|
||||
for name, err := range unavailable {
|
||||
got := postgres.Classify(err)
|
||||
if apperr.KindOf(got) != apperr.KindUnavailable || !errors.Is(got, err) {
|
||||
t.Errorf("%s: Classify = %v (kind %v), want KindUnavailable keeping the cause", name, got, apperr.KindOf(got))
|
||||
}
|
||||
}
|
||||
for name, err := range map[string]error{
|
||||
"unique violation": &pgconn.PgError{Code: "23505"},
|
||||
"plain": errors.New("boom"),
|
||||
"nil": nil,
|
||||
} {
|
||||
if got := postgres.Classify(err); got != err {
|
||||
t.Errorf("%s: Classify = %v, want the error unchanged", name, got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestClassifyRealConnectFailure(t *testing.T) {
|
||||
_, err := postgres.NewPool(context.Background(), "postgres://u:p@127.0.0.1:1/db?connect_timeout=1")
|
||||
if err == nil {
|
||||
t.Fatal("NewPool to a closed port must fail")
|
||||
}
|
||||
if apperr.KindOf(postgres.Classify(err)) != apperr.KindUnavailable {
|
||||
t.Fatalf("connect failure classified as %v: %v", apperr.KindOf(postgres.Classify(err)), err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// Package valkey connects to the Valkey session store.
|
||||
//
|
||||
// Timeouts are constants: every authenticated request reads a session, so
|
||||
// they bound how long a request can wait when the store is slow. Command
|
||||
// retries are disabled and a connection is dialled once for the same reason:
|
||||
// a request that cannot reach the store fails fast with 503 instead of
|
||||
// waiting through a backoff. On the compose stand Docker DNS takes seconds
|
||||
// to give up on a stopped container, so one dial attempt is already bounded
|
||||
// only by dialTimeout.
|
||||
package valkey
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const (
|
||||
dialTimeout = time.Second
|
||||
ioTimeout = time.Second
|
||||
poolSize = 10
|
||||
noRetries = -1
|
||||
singleAttempt = 1
|
||||
)
|
||||
|
||||
// NewClient connects to url (redis:// scheme) and pings the server. Errors
|
||||
// never include the URL, which carries the password.
|
||||
func NewClient(ctx context.Context, url string) (*redis.Client, error) {
|
||||
opts, err := redis.ParseURL(url)
|
||||
if err != nil {
|
||||
return nil, errors.New("session store url is invalid")
|
||||
}
|
||||
opts.DialTimeout = dialTimeout
|
||||
opts.ReadTimeout = ioTimeout
|
||||
opts.WriteTimeout = ioTimeout
|
||||
opts.PoolSize = poolSize
|
||||
opts.MaxRetries = noRetries
|
||||
opts.DialerRetries = singleAttempt
|
||||
|
||||
client := redis.NewClient(opts)
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
_ = client.Close()
|
||||
return nil, fmt.Errorf("ping session store at %s: %w", opts.Addr, err)
|
||||
}
|
||||
return client, nil
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package valkey_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/platform/valkey"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
func TestNewClientPingsAndBoundsWaiting(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
kv, err := testenv.StartValkey(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer kv.Close()
|
||||
|
||||
client, err := valkey.NewClient(ctx, kv.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("NewClient: %v", err)
|
||||
}
|
||||
defer client.Close()
|
||||
opts := client.Options()
|
||||
if opts.DialTimeout != time.Second || opts.ReadTimeout != time.Second || opts.WriteTimeout != time.Second {
|
||||
t.Fatalf("timeouts = %v/%v/%v, want 1s each", opts.DialTimeout, opts.ReadTimeout, opts.WriteTimeout)
|
||||
}
|
||||
// go-redis dials up to five times by default; with Docker DNS taking
|
||||
// seconds to give up on a stopped container that alone would exceed the
|
||||
// request deadline.
|
||||
if opts.DialerRetries != 1 {
|
||||
t.Fatalf("DialerRetries = %d, want a single dial attempt", opts.DialerRetries)
|
||||
}
|
||||
// go-redis stores the requested -1 ("no retries") as 0 after init; its
|
||||
// default would be 3.
|
||||
if opts.PoolSize != 10 || opts.MaxRetries != 0 {
|
||||
t.Fatalf("PoolSize = %d, MaxRetries = %d; want 10 and 0 (fail fast)", opts.PoolSize, opts.MaxRetries)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewClientErrorsNeverEchoTheURL(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
for name, url := range map[string]string{
|
||||
"malformed": "redis://:s3cret@host:port/x",
|
||||
"unreachable": "redis://:s3cret@127.0.0.1:1/0",
|
||||
} {
|
||||
_, err := valkey.NewClient(ctx, url)
|
||||
if err == nil {
|
||||
t.Fatalf("%s: want error", name)
|
||||
}
|
||||
if strings.Contains(err.Error(), "s3cret") {
|
||||
t.Fatalf("%s: error leaks the password: %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Package server runs the HTTP listener with the lifecycle the lab
|
||||
// requires: readiness drops first, the listener stops accepting, in-flight
|
||||
// requests drain within a bounded timeout, then the process exits.
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Readiness is switched off when shutdown starts.
|
||||
type Readiness interface {
|
||||
SetReady(ready bool)
|
||||
}
|
||||
|
||||
// Run serves until ctx is cancelled, then shuts down within shutdownTimeout.
|
||||
func Run(ctx context.Context, log *slog.Logger, ln net.Listener, h http.Handler, ready Readiness, shutdownTimeout time.Duration) error {
|
||||
srv := &http.Server{
|
||||
Handler: h,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
MaxHeaderBytes: 64 * 1024,
|
||||
ErrorLog: slog.NewLogLogger(log.Handler(), slog.LevelWarn),
|
||||
}
|
||||
|
||||
served := make(chan error, 1)
|
||||
go func() {
|
||||
err := srv.Serve(ln)
|
||||
if errors.Is(err, http.ErrServerClosed) {
|
||||
err = nil
|
||||
}
|
||||
served <- err
|
||||
}()
|
||||
log.Info("listening", "addr", ln.Addr().String())
|
||||
|
||||
select {
|
||||
case err := <-served:
|
||||
return err
|
||||
case <-ctx.Done():
|
||||
}
|
||||
|
||||
log.Info("shutdown started", "timeout", shutdownTimeout.String())
|
||||
ready.SetReady(false)
|
||||
drain, cancel := context.WithTimeout(context.Background(), shutdownTimeout)
|
||||
defer cancel()
|
||||
if err := srv.Shutdown(drain); err != nil {
|
||||
log.Error("shutdown forced", "error", err.Error())
|
||||
_ = srv.Close()
|
||||
return err
|
||||
}
|
||||
<-served
|
||||
log.Info("shutdown completed")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package server_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"messenger/internal/health"
|
||||
"messenger/internal/platform/httpx"
|
||||
"messenger/internal/server"
|
||||
)
|
||||
|
||||
// newHandler builds the smallest handler with probes; the lifecycle does not
|
||||
// depend on the business modules or their stores.
|
||||
func newHandler(log *slog.Logger) (*gin.Engine, *health.Handler) {
|
||||
e := httpx.NewEngine(log, 1024)
|
||||
probes := health.New()
|
||||
probes.Register(e)
|
||||
return e, probes
|
||||
}
|
||||
|
||||
func TestServesUntilContextCancelledThenStopsCleanly(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
h, probes := newHandler(log)
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }()
|
||||
|
||||
base := "http://" + ln.Addr().String()
|
||||
waitFor(t, base+"/health/ready", http.StatusOK)
|
||||
|
||||
cancel()
|
||||
select {
|
||||
case err := <-done:
|
||||
if err != nil {
|
||||
t.Fatalf("Run returned error: %v", err)
|
||||
}
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("Run did not stop within 10s after cancel")
|
||||
}
|
||||
if _, err := http.Get(base + "/health/live"); err == nil {
|
||||
t.Fatal("listener must be closed after shutdown")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadinessDropsBeforeListenerCloses(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
h, probes := newHandler(log)
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
done := make(chan error, 1)
|
||||
go func() { done <- server.Run(ctx, log, ln, h, probes, 5*time.Second) }()
|
||||
base := "http://" + ln.Addr().String()
|
||||
waitFor(t, base+"/health/ready", http.StatusOK)
|
||||
|
||||
// Hold one request open so shutdown has something to drain.
|
||||
slow := make(chan struct{})
|
||||
go func() {
|
||||
resp, err := http.Get(base + "/health/live")
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
}
|
||||
close(slow)
|
||||
}()
|
||||
|
||||
cancel()
|
||||
<-done
|
||||
<-slow
|
||||
if !probes.IsReady() {
|
||||
return
|
||||
}
|
||||
t.Fatal("readiness must be switched off during shutdown")
|
||||
}
|
||||
|
||||
func waitFor(t *testing.T, url string, status int) {
|
||||
t.Helper()
|
||||
deadline := time.Now().Add(5 * time.Second)
|
||||
for time.Now().Before(deadline) {
|
||||
resp, err := http.Get(url)
|
||||
if err == nil {
|
||||
resp.Body.Close()
|
||||
if resp.StatusCode == status {
|
||||
return
|
||||
}
|
||||
}
|
||||
time.Sleep(20 * time.Millisecond)
|
||||
}
|
||||
t.Fatalf("%s did not return %d in time", url, status)
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
// Package testenv starts disposable PostgreSQL and Valkey containers for tests
|
||||
// that exercise the real stores. Only _test files import it, so it never
|
||||
// reaches the server binary. The images match compose.yaml.
|
||||
package testenv
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/testcontainers/testcontainers-go"
|
||||
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||||
tcvalkey "github.com/testcontainers/testcontainers-go/modules/valkey"
|
||||
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/platform/valkey"
|
||||
)
|
||||
|
||||
const (
|
||||
PostgresImage = "postgres:18.6-alpine3.24@sha256:77f585114c32fbca283dc835b0596f4e52b51b4c6662d7810b2f4084f60a1873"
|
||||
ValkeyImage = "valkey/valkey:9.1.2-alpine3.24@sha256:48332870af354a799964c0012ae1194a0bf2bf894eb508f945810596dc2d8d11"
|
||||
)
|
||||
|
||||
// useDockerDesktopPipe names the Docker Desktop engine pipe explicitly on
|
||||
// Windows. Without DOCKER_HOST, testcontainers probes the pipe with a file
|
||||
// stat, which fails while the pipe is busy; with several test packages
|
||||
// starting containers at once that probe fails spuriously and the run panics.
|
||||
// Dialling the named host instead waits for a free pipe instance.
|
||||
var useDockerDesktopPipe = sync.OnceFunc(func() {
|
||||
if runtime.GOOS == "windows" && os.Getenv("DOCKER_HOST") == "" {
|
||||
_ = os.Setenv("DOCKER_HOST", "npipe:////./pipe/docker_engine")
|
||||
}
|
||||
})
|
||||
|
||||
// DB is a migrated PostgreSQL instance.
|
||||
type DB struct {
|
||||
Pool *pgxpool.Pool
|
||||
DSN string
|
||||
container *tcpostgres.PostgresContainer
|
||||
}
|
||||
|
||||
func StartPostgres(ctx context.Context) (*DB, error) {
|
||||
useDockerDesktopPipe()
|
||||
c, err := tcpostgres.Run(ctx, PostgresImage,
|
||||
tcpostgres.WithDatabase("messenger"),
|
||||
tcpostgres.WithUsername("messenger"),
|
||||
tcpostgres.WithPassword("messenger"),
|
||||
tcpostgres.BasicWaitStrategies(),
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("start postgres: %w", err)
|
||||
}
|
||||
db := &DB{container: c}
|
||||
db.DSN, err = c.ConnectionString(ctx, "sslmode=disable")
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, fmt.Errorf("postgres address: %w", err)
|
||||
}
|
||||
db.Pool, err = postgres.NewPool(ctx, db.DSN)
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
if _, err := postgres.Migrate(ctx, db.Pool); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// Reset empties every application table, so each test starts from an empty
|
||||
// schema. The table list comes from the catalog: a table added by a later
|
||||
// migration cannot be forgotten here.
|
||||
func (d *DB) Reset(t testing.TB) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
var tables string
|
||||
err := d.Pool.QueryRow(ctx, `
|
||||
SELECT coalesce(string_agg(format('%I.%I', schemaname, tablename), ', '), '')
|
||||
FROM pg_tables
|
||||
WHERE schemaname = 'public' AND tablename <> 'goose_db_version'`).Scan(&tables)
|
||||
if err != nil {
|
||||
t.Fatalf("list tables: %v", err)
|
||||
}
|
||||
if tables == "" {
|
||||
return
|
||||
}
|
||||
if _, err := d.Pool.Exec(ctx, "TRUNCATE "+tables+" CASCADE"); err != nil {
|
||||
t.Fatalf("truncate: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop stops the container, as when the database goes down.
|
||||
func (d *DB) Stop(ctx context.Context) error {
|
||||
timeout := 5 * time.Second
|
||||
return d.container.Stop(ctx, &timeout)
|
||||
}
|
||||
|
||||
// Pause freezes the server: connections stay open but nothing answers, as
|
||||
// with a hung database.
|
||||
func (d *DB) Pause(ctx context.Context) error {
|
||||
return withDocker(ctx, func(cli *testcontainers.DockerClient) error {
|
||||
return cli.ContainerPause(ctx, d.container.GetContainerID())
|
||||
})
|
||||
}
|
||||
|
||||
func (d *DB) Unpause(ctx context.Context) error {
|
||||
return withDocker(ctx, func(cli *testcontainers.DockerClient) error {
|
||||
return cli.ContainerUnpause(ctx, d.container.GetContainerID())
|
||||
})
|
||||
}
|
||||
|
||||
func withDocker(ctx context.Context, fn func(cli *testcontainers.DockerClient) error) error {
|
||||
cli, err := testcontainers.NewDockerClientWithOpts(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cli.Close()
|
||||
return fn(cli)
|
||||
}
|
||||
|
||||
func (d *DB) Close() {
|
||||
if d.Pool != nil {
|
||||
d.Pool.Close()
|
||||
}
|
||||
_ = testcontainers.TerminateContainer(d.container)
|
||||
}
|
||||
|
||||
// KV is a Valkey instance without persistence or password.
|
||||
type KV struct {
|
||||
Client *redis.Client
|
||||
URL string
|
||||
container *tcvalkey.ValkeyContainer
|
||||
}
|
||||
|
||||
func StartValkey(ctx context.Context) (*KV, error) {
|
||||
useDockerDesktopPipe()
|
||||
c, err := tcvalkey.Run(ctx, ValkeyImage)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("start valkey: %w", err)
|
||||
}
|
||||
kv := &KV{container: c}
|
||||
kv.URL, err = c.ConnectionString(ctx)
|
||||
if err != nil {
|
||||
kv.Close()
|
||||
return nil, fmt.Errorf("valkey address: %w", err)
|
||||
}
|
||||
// The production client, so outage tests see its fail-fast settings.
|
||||
kv.Client, err = valkey.NewClient(ctx, kv.URL)
|
||||
if err != nil {
|
||||
kv.Close()
|
||||
return nil, err
|
||||
}
|
||||
return kv, nil
|
||||
}
|
||||
|
||||
func (k *KV) Reset(t testing.TB) {
|
||||
t.Helper()
|
||||
if err := k.Client.FlushDB(context.Background()).Err(); err != nil {
|
||||
t.Fatalf("flushdb: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Stop stops the container, as when the session store goes down.
|
||||
func (k *KV) Stop(ctx context.Context) error {
|
||||
timeout := 5 * time.Second
|
||||
return k.container.Stop(ctx, &timeout)
|
||||
}
|
||||
|
||||
func (k *KV) Close() {
|
||||
if k.Client != nil {
|
||||
_ = k.Client.Close()
|
||||
}
|
||||
_ = testcontainers.TerminateContainer(k.container)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package testenv_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
func TestValkeyStartsAndResetFlushesKeys(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
kv, err := testenv.StartValkey(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer kv.Close()
|
||||
|
||||
if err := kv.Client.Set(ctx, "probe", "1", 0).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
kv.Reset(t)
|
||||
n, err := kv.Client.DBSize(ctx).Result()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 0 {
|
||||
t.Fatalf("%d keys left after Reset", n)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/httpx"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
users *Service
|
||||
}
|
||||
|
||||
func NewHandler(users *Service) *Handler {
|
||||
return &Handler{users: users}
|
||||
}
|
||||
|
||||
// Register mounts registration on the public router and the current-user
|
||||
// endpoint on the router guarded by the authentication middleware.
|
||||
func (h *Handler) Register(public, protected gin.IRouter) {
|
||||
public.POST("/users", h.register)
|
||||
protected.GET("/users/me", httpx.WithUser(h.me))
|
||||
}
|
||||
|
||||
type createRequest struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
func ToResponse(u User) Response {
|
||||
return Response{
|
||||
ID: u.ID.String(),
|
||||
Username: u.Username,
|
||||
DisplayName: u.DisplayName,
|
||||
CreatedAt: httpx.Timestamp(u.CreatedAt),
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) register(c *gin.Context) {
|
||||
var req createRequest
|
||||
if err := httpx.DecodeJSON(c, &req); err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
u, err := h.users.Register(c.Request.Context(), req.Username, req.DisplayName, req.Password)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusCreated, ToResponse(u))
|
||||
}
|
||||
|
||||
func (h *Handler) me(c *gin.Context, actor uuid.UUID) {
|
||||
u, err := h.users.GetByID(c.Request.Context(), actor)
|
||||
if err != nil {
|
||||
httpx.Error(c, err)
|
||||
return
|
||||
}
|
||||
httpx.JSON(c, http.StatusOK, ToResponse(u))
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
var db *testenv.DB
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
var err error
|
||||
db, err = testenv.StartPostgres(context.Background())
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
// Create stores a new user or returns ErrUsernameTaken; the uniqueness
|
||||
// check and the insert must be atomic.
|
||||
Create(ctx context.Context, user User) error
|
||||
GetByID(ctx context.Context, id uuid.UUID) (User, error)
|
||||
GetByUsername(ctx context.Context, username string) (User, error)
|
||||
// ExistingIDs returns the subset of ids that belong to registered users,
|
||||
// in no particular order, in one round trip.
|
||||
ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error)
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/platform/password"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
repo Repository
|
||||
}
|
||||
|
||||
func NewService(repo Repository) *Service {
|
||||
return &Service{repo: repo}
|
||||
}
|
||||
|
||||
// Register validates every field before hashing, so invalid input never pays
|
||||
// for the KDF.
|
||||
func (s *Service) Register(ctx context.Context, username, displayName, plain string) (User, error) {
|
||||
u, err := New(ident.New(), username, displayName)
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
if err := ValidatePassword(plain); err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
u.PasswordHash = password.Hash(plain)
|
||||
if err := s.repo.Create(ctx, u); err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
// dummyHash is verified for unknown usernames, so a failed login takes the
|
||||
// same time whether or not the user exists.
|
||||
var dummyHash = sync.OnceValue(func() string {
|
||||
return password.Hash("timing-equaliser-password")
|
||||
})
|
||||
|
||||
// Authenticate returns the user whose password matches, or
|
||||
// ErrInvalidCredentials for an unknown username and a wrong password alike.
|
||||
// The username comes straight from the Basic header, so one that cannot
|
||||
// exist (including NUL or invalid UTF-8, which the database would reject)
|
||||
// is answered without a query, but after the same KDF work.
|
||||
func (s *Service) Authenticate(ctx context.Context, username, plain string) (User, error) {
|
||||
if !usernamePattern.MatchString(username) {
|
||||
_, _ = password.Verify(dummyHash(), plain)
|
||||
return User{}, ErrInvalidCredentials
|
||||
}
|
||||
u, err := s.repo.GetByUsername(ctx, username)
|
||||
if errors.Is(err, ErrNotFound) {
|
||||
_, _ = password.Verify(dummyHash(), plain)
|
||||
return User{}, ErrInvalidCredentials
|
||||
}
|
||||
if err != nil {
|
||||
return User{}, fmt.Errorf("find user: %w", err)
|
||||
}
|
||||
ok, err := password.Verify(u.PasswordHash, plain)
|
||||
if err != nil {
|
||||
return User{}, fmt.Errorf("verify password of user %s: %w", u.ID, err)
|
||||
}
|
||||
if !ok {
|
||||
return User{}, ErrInvalidCredentials
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
|
||||
func (s *Service) GetByID(ctx context.Context, id uuid.UUID) (User, error) {
|
||||
return s.repo.GetByID(ctx, id)
|
||||
}
|
||||
|
||||
// ExistingIDs serves the chats module when it validates member lists.
|
||||
func (s *Service) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
|
||||
if len(ids) == 0 {
|
||||
return []uuid.UUID{}, nil
|
||||
}
|
||||
found, err := s.repo.ExistingIDs(ctx, ids)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("user lookup: %w", err)
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
@@ -0,0 +1,189 @@
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/user"
|
||||
"messenger/internal/user/store"
|
||||
)
|
||||
|
||||
const pw = "correct-horse-battery"
|
||||
|
||||
// newService wires the service to the real store over an empty database.
|
||||
func newService(t *testing.T) *user.Service {
|
||||
t.Helper()
|
||||
db.Reset(t)
|
||||
return user.NewService(store.NewUsers(db.Pool))
|
||||
}
|
||||
|
||||
func TestRegisterStoresUserWithPasswordHash(t *testing.T) {
|
||||
svc := newService(t)
|
||||
ctx := context.Background()
|
||||
u, err := svc.Register(ctx, "alice", "Алиса", pw)
|
||||
if err != nil {
|
||||
t.Fatalf("Register error: %v", err)
|
||||
}
|
||||
if u.ID.Version() != 7 {
|
||||
t.Fatalf("id must be UUIDv7, got version %d", u.ID.Version())
|
||||
}
|
||||
if !strings.HasPrefix(u.PasswordHash, "$argon2id$") || strings.Contains(u.PasswordHash, pw) {
|
||||
t.Fatalf("password must be stored as an Argon2id hash, got %q", u.PasswordHash)
|
||||
}
|
||||
got, err := svc.GetByID(ctx, u.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("GetByID error: %v", err)
|
||||
}
|
||||
if got != u {
|
||||
t.Fatalf("GetByID = %+v, want %+v", got, u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterRejectsInvalidInputAndStoresNothing(t *testing.T) {
|
||||
svc := newService(t)
|
||||
ctx := context.Background()
|
||||
cases := map[string][3]string{
|
||||
"username": {"Bad Name", "x", pw},
|
||||
"display_name": {"alice", " ", pw},
|
||||
"password": {"alice", "Алиса", "short"},
|
||||
}
|
||||
for field, in := range cases {
|
||||
_, err := svc.Register(ctx, in[0], in[1], in[2])
|
||||
if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), field) {
|
||||
t.Errorf("%s: err = %v, want KindInvalid naming the field", field, err)
|
||||
}
|
||||
}
|
||||
var n int
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM users`).Scan(&n); err != nil || n != 0 {
|
||||
t.Fatalf("users = %d, %v; invalid input must not be stored", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRegisterReportsDuplicateAsConflict(t *testing.T) {
|
||||
svc := newService(t)
|
||||
ctx := context.Background()
|
||||
if _, err := svc.Register(ctx, "alice", "A", pw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err := svc.Register(ctx, "alice", "B", pw)
|
||||
if !errors.Is(err, user.ErrUsernameTaken) || apperr.KindOf(err) != apperr.KindConflict {
|
||||
t.Fatalf("err = %v, want ErrUsernameTaken with KindConflict", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Concurrent registrations of one username race on the unique constraint:
|
||||
// exactly one wins and every other gets the conflict, never a 500.
|
||||
func TestConcurrentRegistrationOfOneUsernameHasOneWinner(t *testing.T) {
|
||||
svc := newService(t)
|
||||
ctx := context.Background()
|
||||
const attempts = 10
|
||||
errs := make(chan error, attempts)
|
||||
var start, done sync.WaitGroup
|
||||
start.Add(1)
|
||||
for range attempts {
|
||||
done.Add(1)
|
||||
go func() {
|
||||
defer done.Done()
|
||||
start.Wait()
|
||||
_, err := svc.Register(ctx, "racer", "Racer", pw)
|
||||
errs <- err
|
||||
}()
|
||||
}
|
||||
start.Done()
|
||||
done.Wait()
|
||||
close(errs)
|
||||
|
||||
won, lost := 0, 0
|
||||
for err := range errs {
|
||||
switch {
|
||||
case err == nil:
|
||||
won++
|
||||
case errors.Is(err, user.ErrUsernameTaken):
|
||||
lost++
|
||||
default:
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
if won != 1 || lost != attempts-1 {
|
||||
t.Fatalf("won = %d, lost = %d; want 1 and %d", won, lost, attempts-1)
|
||||
}
|
||||
var n int
|
||||
if err := db.Pool.QueryRow(ctx, `SELECT count(*) FROM users WHERE username = 'racer'`).Scan(&n); err != nil || n != 1 {
|
||||
t.Fatalf("rows = %d, %v; want exactly one", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetByIDUnknownIsNotFound(t *testing.T) {
|
||||
svc := newService(t)
|
||||
_, err := svc.GetByID(context.Background(), ident.New())
|
||||
if !errors.Is(err, user.ErrNotFound) || apperr.KindOf(err) != apperr.KindNotFound {
|
||||
t.Fatalf("err = %v, want ErrNotFound with KindNotFound", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExistingIDsReturnsOnlyRegistered(t *testing.T) {
|
||||
svc := newService(t)
|
||||
ctx := context.Background()
|
||||
alice, _ := svc.Register(ctx, "alice", "A", pw)
|
||||
bob, _ := svc.Register(ctx, "bob", "B", pw)
|
||||
got, err := svc.ExistingIDs(ctx, []uuid.UUID{ident.New(), bob.ID, alice.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
set := map[uuid.UUID]bool{}
|
||||
for _, id := range got {
|
||||
set[id] = true
|
||||
}
|
||||
if len(got) != 2 || !set[alice.ID] || !set[bob.ID] {
|
||||
t.Fatalf("ExistingIDs = %v, want alice and bob", got)
|
||||
}
|
||||
if empty, err := svc.ExistingIDs(ctx, nil); err != nil || len(empty) != 0 {
|
||||
t.Fatalf("ExistingIDs(nil) = %v, %v", empty, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthenticateChecksPassword(t *testing.T) {
|
||||
svc := newService(t)
|
||||
ctx := context.Background()
|
||||
const colonPassword = "пароль:с:двоеточием 👋"
|
||||
alice, err := svc.Register(ctx, "alice", "Алиса", colonPassword)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, err := svc.Authenticate(ctx, "alice", colonPassword)
|
||||
if err != nil || got != alice {
|
||||
t.Fatalf("Authenticate = %+v, %v; want alice", got, err)
|
||||
}
|
||||
}
|
||||
|
||||
// A wrong password and an unknown username must be indistinguishable.
|
||||
func TestAuthenticateFailuresLookTheSame(t *testing.T) {
|
||||
svc := newService(t)
|
||||
ctx := context.Background()
|
||||
if _, err := svc.Register(ctx, "alice", "Алиса", pw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, wrong := svc.Authenticate(ctx, "alice", pw+"!")
|
||||
_, unknown := svc.Authenticate(ctx, "nobody", pw)
|
||||
// Basic credentials are client bytes: a username the database cannot
|
||||
// even compare must still be a plain credential failure.
|
||||
_, nul := svc.Authenticate(ctx, "a\x00b", pw)
|
||||
_, notUTF8 := svc.Authenticate(ctx, "\xff\xfe", pw)
|
||||
for name, err := range map[string]error{
|
||||
"wrong password": wrong, "unknown user": unknown, "nul username": nul, "invalid utf-8 username": notUTF8,
|
||||
} {
|
||||
if !errors.Is(err, user.ErrInvalidCredentials) || apperr.KindOf(err) != apperr.KindUnauthorized {
|
||||
t.Errorf("%s: err = %v, want ErrInvalidCredentials", name, err)
|
||||
}
|
||||
}
|
||||
if wrong.Error() != unknown.Error() {
|
||||
t.Fatalf("messages differ: %q vs %q", wrong, unknown)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
// Package store holds the repository implementations of the users module.
|
||||
// Errors pass through postgres.Classify, so an unreachable database surfaces
|
||||
// as unavailability rather than as an internal error.
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"messenger/internal/platform/postgres"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
type Users struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
var _ user.Repository = (*Users)(nil)
|
||||
|
||||
func NewUsers(pool *pgxpool.Pool) *Users {
|
||||
return &Users{pool: pool}
|
||||
}
|
||||
|
||||
// Create relies on the unique constraint rather than a prior lookup, so two
|
||||
// concurrent registrations of one username cannot both succeed.
|
||||
func (r *Users) Create(ctx context.Context, u user.User) error {
|
||||
_, err := r.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, u.PasswordHash, u.CreatedAt)
|
||||
if postgres.IsUniqueViolation(err, "users_username_key") {
|
||||
return user.ErrUsernameTaken
|
||||
}
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert user: %w", postgres.Classify(err))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *Users) GetByID(ctx context.Context, id uuid.UUID) (user.User, error) {
|
||||
return r.one(ctx, `SELECT id, username, display_name, password_hash, created_at FROM users WHERE id = $1`, id)
|
||||
}
|
||||
|
||||
func (r *Users) GetByUsername(ctx context.Context, username string) (user.User, error) {
|
||||
return r.one(ctx, `SELECT id, username, display_name, password_hash, created_at FROM users WHERE username = $1`, username)
|
||||
}
|
||||
|
||||
func (r *Users) ExistingIDs(ctx context.Context, ids []uuid.UUID) ([]uuid.UUID, error) {
|
||||
rows, err := r.pool.Query(ctx, `SELECT id FROM users WHERE id = ANY($1)`, ids)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("select user ids: %w", postgres.Classify(err))
|
||||
}
|
||||
found, err := pgx.CollectRows(rows, pgx.RowTo[uuid.UUID])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read user ids: %w", postgres.Classify(err))
|
||||
}
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (r *Users) one(ctx context.Context, query string, arg any) (user.User, error) {
|
||||
var u user.User
|
||||
err := r.pool.QueryRow(ctx, query, arg).Scan(&u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return user.User{}, user.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return user.User{}, fmt.Errorf("select user: %w", postgres.Classify(err))
|
||||
}
|
||||
return u, nil
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
// Package user is the users module. The entity mirrors the table row and
|
||||
// carries no behaviour; New only enforces input invariants and CreatedAt
|
||||
// derives from the UUIDv7 id. The password itself is never kept: the service
|
||||
// validates it and stores only its hash.
|
||||
package user
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxDisplayNameCodePoints = 100
|
||||
MinPasswordCodePoints = 12
|
||||
MaxPasswordCodePoints = 128
|
||||
)
|
||||
|
||||
var (
|
||||
ErrNotFound = apperr.NotFound("user not found")
|
||||
ErrUsernameTaken = apperr.Conflict("username already taken")
|
||||
// ErrInvalidCredentials covers an unknown username and a wrong password
|
||||
// alike, so a login attempt cannot probe which usernames exist.
|
||||
ErrInvalidCredentials = apperr.Unauthorized("invalid username or password")
|
||||
)
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func New(id uuid.UUID, username, displayName string) (User, error) {
|
||||
if !usernamePattern.MatchString(username) {
|
||||
return User{}, apperr.Invalid("username must match [a-z0-9_]{3,32}")
|
||||
}
|
||||
if strings.TrimSpace(displayName) == "" {
|
||||
return User{}, apperr.Invalid("display_name must not be blank")
|
||||
}
|
||||
if utf8.RuneCountInString(displayName) > MaxDisplayNameCodePoints {
|
||||
return User{}, apperr.Invalid(fmt.Sprintf("display_name must be at most %d characters", MaxDisplayNameCodePoints))
|
||||
}
|
||||
// PostgreSQL text cannot hold U+0000.
|
||||
if strings.ContainsRune(displayName, 0) {
|
||||
return User{}, apperr.Invalid("display_name must not contain the NUL character")
|
||||
}
|
||||
return User{
|
||||
ID: id,
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: ident.TimeOf(id),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ValidatePassword checks the contract length in Unicode code points.
|
||||
func ValidatePassword(plain string) error {
|
||||
if strings.TrimSpace(plain) == "" {
|
||||
return apperr.Invalid("password must not be blank")
|
||||
}
|
||||
if n := utf8.RuneCountInString(plain); n < MinPasswordCodePoints || n > MaxPasswordCodePoints {
|
||||
return apperr.Invalid(fmt.Sprintf("password must be %d to %d characters", MinPasswordCodePoints, MaxPasswordCodePoints))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package user_test
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"messenger/internal/platform/apperr"
|
||||
"messenger/internal/platform/ident"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
func TestNewDerivesCreatedAtFromID(t *testing.T) {
|
||||
id := ident.New()
|
||||
u, err := user.New(id, "alice", "Алиса")
|
||||
if err != nil {
|
||||
t.Fatalf("New error: %v", err)
|
||||
}
|
||||
if u.ID != id || u.Username != "alice" || u.DisplayName != "Алиса" {
|
||||
t.Fatalf("unexpected user %+v", u)
|
||||
}
|
||||
if !u.CreatedAt.Equal(ident.TimeOf(id)) {
|
||||
t.Fatalf("CreatedAt = %v, want %v", u.CreatedAt, ident.TimeOf(id))
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewValidatesUsername(t *testing.T) {
|
||||
valid := []string{"abc", "a_1", strings.Repeat("a", 32), "user_2026"}
|
||||
for _, name := range valid {
|
||||
if _, err := user.New(ident.New(), name, "ok"); err != nil {
|
||||
t.Errorf("username %q must be accepted: %v", name, err)
|
||||
}
|
||||
}
|
||||
invalid := []string{"", "ab", strings.Repeat("a", 33), "Alice", "al ice", "алиса", "a-b", "a.b", " abc", "abc\n"}
|
||||
for _, name := range invalid {
|
||||
_, err := user.New(ident.New(), name, "ok")
|
||||
if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), "username") {
|
||||
t.Errorf("username %q must be rejected as invalid naming the field, got %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The contract counts the password length in Unicode code points, not bytes.
|
||||
func TestValidatePasswordCountsCodePoints(t *testing.T) {
|
||||
valid := []string{
|
||||
strings.Repeat("я", 12),
|
||||
strings.Repeat("👋", 128),
|
||||
"pass:word:with:colons",
|
||||
" spaced but long enough ",
|
||||
}
|
||||
for _, p := range valid {
|
||||
if err := user.ValidatePassword(p); err != nil {
|
||||
t.Errorf("password of %d code points must be accepted: %v", len([]rune(p)), err)
|
||||
}
|
||||
}
|
||||
invalid := []string{"", strings.Repeat(" ", 12), strings.Repeat("\t", 20), strings.Repeat("я", 11), strings.Repeat("👋", 129)}
|
||||
for _, p := range invalid {
|
||||
err := user.ValidatePassword(p)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), "password") {
|
||||
t.Errorf("password %q must be rejected as invalid naming the field, got %v", p, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewValidatesDisplayNameByCodePoints(t *testing.T) {
|
||||
valid := []string{"A", "Алиса", strings.Repeat("я", 100), "👋 hi"}
|
||||
for _, name := range valid {
|
||||
if _, err := user.New(ident.New(), "alice", name); err != nil {
|
||||
t.Errorf("display_name %q must be accepted: %v", name, err)
|
||||
}
|
||||
}
|
||||
invalid := []string{"", " ", "\t\n", strings.Repeat("я", 101), "nul\x00inside"}
|
||||
for _, name := range invalid {
|
||||
_, err := user.New(ident.New(), "alice", name)
|
||||
if apperr.KindOf(err) != apperr.KindInvalid || !strings.Contains(err.Error(), "display_name") {
|
||||
t.Errorf("display_name %q must be rejected as invalid naming the field, got %v", name, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL CONSTRAINT users_username_key UNIQUE,
|
||||
display_name text NOT NULL,
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id),
|
||||
user_id uuid NOT NULL REFERENCES users (id),
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
-- Lists the chats of one user in (created_at, id) order: chat ids are UUIDv7.
|
||||
CREATE INDEX chat_members_user_chat_idx ON chat_members (user_id, chat_id);
|
||||
|
||||
-- The key is (chat_id, id) and serves the history query directly:
|
||||
-- WHERE chat_id = $1 AND id > $2 ORDER BY id LIMIT $3. Nothing looks a
|
||||
-- message up by id alone, and a separate index on id would only cost writes
|
||||
-- and tempt the planner into scanning every chat's messages in id order.
|
||||
CREATE TABLE messages (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id),
|
||||
id uuid NOT NULL,
|
||||
sender_id uuid NOT NULL REFERENCES users (id),
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (chat_id, id)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE messages;
|
||||
DROP TABLE chat_members;
|
||||
DROP TABLE chats;
|
||||
DROP TABLE users;
|
||||
@@ -0,0 +1,8 @@
|
||||
// Package migrations embeds the versioned SQL schema, so the server binary
|
||||
// always carries the schema it was built against.
|
||||
package migrations
|
||||
|
||||
import "embed"
|
||||
|
||||
//go:embed *.sql
|
||||
var FS embed.FS
|
||||
@@ -0,0 +1,75 @@
|
||||
-- EXPLAIN evidence for the message history query (make explain).
|
||||
--
|
||||
-- Everything runs in one transaction that is rolled back: it seeds 12 000
|
||||
-- messages in one chat and 90 000 in 300 other chats, so the plan is measured
|
||||
-- on a realistic volume without leaving data on the stand. The PREPARE text
|
||||
-- is the statement of Messages.ListByChat in internal/chat/store/postgres.go;
|
||||
-- keep them identical.
|
||||
\set ON_ERROR_STOP on
|
||||
\pset pager off
|
||||
BEGIN;
|
||||
|
||||
SELECT uuidv7() AS author \gset
|
||||
SELECT uuidv7() AS big_chat \gset
|
||||
|
||||
INSERT INTO users (id, username, display_name, password_hash, created_at)
|
||||
VALUES (:'author', 'explain_' || substr(md5(random()::text), 1, 12), 'EXPLAIN', 'not-a-password-hash',
|
||||
uuid_extract_timestamp(:'author'));
|
||||
|
||||
CREATE TEMP TABLE noise_chats ON COMMIT DROP AS
|
||||
SELECT uuidv7() AS id FROM generate_series(1, 300);
|
||||
|
||||
INSERT INTO chats (id, title, created_at)
|
||||
SELECT :'big_chat'::uuid, 'explain: big chat', uuid_extract_timestamp(:'big_chat')
|
||||
UNION ALL
|
||||
SELECT id, 'explain: other chat', uuid_extract_timestamp(id) FROM noise_chats;
|
||||
|
||||
INSERT INTO chat_members (chat_id, user_id)
|
||||
SELECT id, :'author' FROM chats WHERE title LIKE 'explain:%';
|
||||
|
||||
-- Rows are inserted ordered by n, so the messages of all chats interleave in
|
||||
-- the heap as they would under real traffic; ids stay increasing per chat.
|
||||
INSERT INTO messages (id, chat_id, sender_id, text, created_at)
|
||||
SELECT id, chat_id, :'author', 'message ' || n, uuid_extract_timestamp(id)
|
||||
FROM (
|
||||
SELECT uuidv7() AS id, chat_id, n
|
||||
FROM (
|
||||
SELECT :'big_chat'::uuid AS chat_id, n FROM generate_series(1, 12000) AS n
|
||||
UNION ALL
|
||||
SELECT c.id, n FROM noise_chats AS c, generate_series(1, 300) AS n
|
||||
) AS planned
|
||||
ORDER BY n
|
||||
) AS generated;
|
||||
|
||||
ANALYZE users, chats, chat_members, messages;
|
||||
|
||||
SELECT (SELECT count(*) FROM messages WHERE chat_id = :'big_chat') AS big_chat_messages,
|
||||
(SELECT count(*) FROM messages) AS all_messages;
|
||||
|
||||
PREPARE history(uuid, uuid, int) AS
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
AND id > COALESCE($2, '00000000-0000-0000-0000-000000000000'::uuid)
|
||||
ORDER BY id
|
||||
LIMIT $3;
|
||||
|
||||
\echo
|
||||
\echo == First page: no cursor, limit 50 (+1 row that tells whether a next page exists)
|
||||
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', NULL, 51);
|
||||
|
||||
SELECT id AS deep_cursor FROM messages WHERE chat_id = :'big_chat' ORDER BY id OFFSET 10000 LIMIT 1 \gset
|
||||
\echo
|
||||
\echo == Deep page: cursor after the 10 001st message of the chat
|
||||
EXPLAIN (ANALYZE, BUFFERS) EXECUTE history(:'big_chat', :'deep_cursor', 51);
|
||||
|
||||
\echo
|
||||
\echo == For contrast, OFFSET pagination of the same page reads every skipped row
|
||||
EXPLAIN (ANALYZE, BUFFERS)
|
||||
SELECT id, chat_id, sender_id, text, created_at
|
||||
FROM messages
|
||||
WHERE chat_id = :'big_chat'
|
||||
ORDER BY id
|
||||
OFFSET 10001 LIMIT 51;
|
||||
|
||||
ROLLBACK;
|
||||
Executable
+42
@@ -0,0 +1,42 @@
|
||||
#!/bin/sh
|
||||
# Creates the local credentials of the compose stand in ./secrets, which git
|
||||
# ignores. Existing secrets are kept: PostgreSQL applies its password only
|
||||
# when the volume is first initialised, so a new password would lock the API
|
||||
# out of an existing database. To start over: make destroy, then delete
|
||||
# ./secrets and run this again.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
files="postgres_password database_url valkey_auth.conf session_store_url"
|
||||
present=0
|
||||
for f in $files; do
|
||||
if [ -e "secrets/$f" ]; then
|
||||
present=$((present + 1))
|
||||
fi
|
||||
done
|
||||
if [ "$present" -eq 4 ]; then
|
||||
echo "secrets already exist in ./secrets, kept"
|
||||
exit 0
|
||||
fi
|
||||
if [ "$present" -ne 0 ]; then
|
||||
echo "./secrets is incomplete: delete it (and run make destroy if the stand holds data), then run again" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# 24 random bytes as hex: URL-safe without escaping.
|
||||
random() {
|
||||
od -An -tx1 -N24 /dev/urandom | tr -d ' \n'
|
||||
}
|
||||
pg=$(random)
|
||||
kv=$(random)
|
||||
|
||||
# The directory is private; the files stay readable for the unprivileged
|
||||
# users inside the containers, which see them through bind mounts.
|
||||
umask 022
|
||||
mkdir -p secrets
|
||||
chmod 700 secrets
|
||||
printf '%s\n' "$pg" > secrets/postgres_password
|
||||
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$pg" > secrets/database_url
|
||||
printf 'requirepass %s\n' "$kv" > secrets/valkey_auth.conf
|
||||
printf 'redis://:%s@valkey:6379/0\n' "$kv" > secrets/session_store_url
|
||||
echo "created ./secrets"
|
||||
@@ -0,0 +1,5 @@
|
||||
@echo off
|
||||
rem Windows entry point of scripts/restart.sh: tests/resilience.py starts the
|
||||
rem hook without a shell, and Windows cannot execute a .sh file directly.
|
||||
sh "%~dp0restart.sh"
|
||||
exit /b %errorlevel%
|
||||
Executable
+9
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
# Restart hook of `make test-persistence` (tests/resilience.py): stops the
|
||||
# API, PostgreSQL and Valkey gracefully and starts the same containers again.
|
||||
# Volumes and data are kept and nothing is re-seeded. The test hides the
|
||||
# output, so failures surface through the exit code only.
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose stop
|
||||
docker compose up -d --wait
|
||||
@@ -0,0 +1,83 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"time"
|
||||
)
|
||||
|
||||
// client is a minimal typed HTTP client for the messenger API.
|
||||
type client struct {
|
||||
base string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func newClient(baseURL string) *client {
|
||||
return &client{base: baseURL, http: &http.Client{Timeout: 10 * time.Second}}
|
||||
}
|
||||
|
||||
func (c *client) post(path, token string, body, out any) error {
|
||||
raw, err := json.Marshal(body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, c.base+path, bytes.NewReader(raw))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
return c.do(req, token, http.StatusCreated, out)
|
||||
}
|
||||
|
||||
// login opens a session with Basic credentials and returns its bearer token.
|
||||
func (c *client) login(username, password string) (string, error) {
|
||||
req, err := http.NewRequest(http.MethodPost, c.base+"/api/v1/auth/sessions", nil)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
req.SetBasicAuth(username, password)
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := c.do(req, "", http.StatusCreated, &session); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return session.Token, nil
|
||||
}
|
||||
|
||||
// get requests the largest page the contract allows, continuing from cursor
|
||||
// when it is set.
|
||||
func (c *client) get(path, token, cursor string, out any) error {
|
||||
query := url.Values{"limit": {"100"}}
|
||||
if cursor != "" {
|
||||
query.Set("cursor", cursor)
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, c.base+path+"?"+query.Encode(), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return c.do(req, token, http.StatusOK, out)
|
||||
}
|
||||
|
||||
func (c *client) do(req *http.Request, token string, want int, out any) error {
|
||||
if token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
resp, err := c.http.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
payload, err := io.ReadAll(io.LimitReader(resp.Body, 4<<20))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if resp.StatusCode != want {
|
||||
return fmt.Errorf("%s %s: status %d, want %d: %s", req.Method, req.URL.Path, resp.StatusCode, want, payload)
|
||||
}
|
||||
return json.Unmarshal(payload, out)
|
||||
}
|
||||
@@ -0,0 +1,227 @@
|
||||
// Command concurrency is the black-box race check from the grading rubric:
|
||||
// N clients send M messages each into one chat at the same time, then the
|
||||
// history is paged and must contain exactly N*M unique messages in id order.
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"messenger/internal/chat"
|
||||
"messenger/internal/platform/pagination"
|
||||
"messenger/internal/user"
|
||||
)
|
||||
|
||||
// Report is the machine readable outcome saved as evidence.
|
||||
type Report struct {
|
||||
BaseURL string `json:"base_url"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
Clients int `json:"clients"`
|
||||
PerClient int `json:"per_client"`
|
||||
Expected int `json:"expected_messages"`
|
||||
Created int `json:"created_201"`
|
||||
Failed int `json:"failed_posts"`
|
||||
Listed int `json:"listed_messages"`
|
||||
Unique int `json:"unique_ids"`
|
||||
Missing int `json:"created_but_not_listed"`
|
||||
Ordered bool `json:"history_ordered_by_id"`
|
||||
Pages int `json:"pages_read"`
|
||||
ElapsedSeconds float64 `json:"elapsed_seconds"`
|
||||
Passed bool `json:"passed"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
baseURL := flag.String("base-url", "http://localhost:8080", "API base URL")
|
||||
clients := flag.Int("clients", 10, "number of concurrent clients")
|
||||
perClient := flag.Int("per-client", 10, "messages sent by each client")
|
||||
output := flag.String("output", "", "optional path for the JSON report")
|
||||
flag.Parse()
|
||||
|
||||
report, err := run(*baseURL, *clients, *perClient)
|
||||
if err != nil {
|
||||
fail(err)
|
||||
}
|
||||
text, _ := json.MarshalIndent(report, "", " ")
|
||||
fmt.Println(string(text))
|
||||
if *output != "" {
|
||||
if err := writeReport(*output, text); err != nil {
|
||||
fail(err)
|
||||
}
|
||||
}
|
||||
if !report.Passed {
|
||||
fail(fmt.Errorf("concurrency invariants violated"))
|
||||
}
|
||||
fmt.Printf("PASS: %d clients x %d messages produced exactly %d unique ordered messages\n",
|
||||
report.Clients, report.PerClient, report.Unique)
|
||||
}
|
||||
|
||||
func fail(err error) {
|
||||
fmt.Fprintln(os.Stderr, "FAIL:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
func writeReport(path string, text []byte) error {
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(path, append(text, '\n'), 0o644)
|
||||
}
|
||||
|
||||
func run(baseURL string, clients, perClient int) (Report, error) {
|
||||
if clients < 1 || perClient < 1 {
|
||||
return Report{}, fmt.Errorf("clients and per-client must be positive")
|
||||
}
|
||||
report := Report{
|
||||
BaseURL: baseURL,
|
||||
StartedAt: time.Now().UTC(),
|
||||
Clients: clients,
|
||||
PerClient: perClient,
|
||||
Expected: clients * perClient,
|
||||
}
|
||||
api := newClient(baseURL)
|
||||
runID := uuid.NewString()[:8]
|
||||
|
||||
members, tokens, err := registerClients(api, runID, clients)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
room, err := createChat(api, runID, members, tokens[0])
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
started := time.Now()
|
||||
created, failed := sendConcurrently(baseURL, room.ID, tokens, perClient)
|
||||
report.ElapsedSeconds = time.Since(started).Seconds()
|
||||
report.Created = len(created)
|
||||
report.Failed = failed
|
||||
|
||||
listed, pages, err := readHistory(api, room.ID, tokens[0])
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
report.Pages = pages
|
||||
report.Listed = len(listed)
|
||||
report.Unique, report.Missing = compare(created, listed)
|
||||
report.Ordered = sort.StringsAreSorted(listed)
|
||||
report.Passed = report.Failed == 0 &&
|
||||
report.Created == report.Expected &&
|
||||
report.Listed == report.Expected &&
|
||||
report.Unique == report.Expected &&
|
||||
report.Missing == 0 &&
|
||||
report.Ordered
|
||||
return report, nil
|
||||
}
|
||||
|
||||
// clientPassword is shared by the synthetic clients of one run.
|
||||
const clientPassword = "concurrency-check-password"
|
||||
|
||||
// registerClients registers and logs in n clients, returning their ids and
|
||||
// bearer tokens in the same order.
|
||||
func registerClients(api *client, runID string, n int) (ids, tokens []string, err error) {
|
||||
for i := range n {
|
||||
var created user.Response
|
||||
username := fmt.Sprintf("load_%s_%d", runID, i)
|
||||
body := map[string]string{
|
||||
"username": username,
|
||||
"display_name": fmt.Sprintf("Load client %d", i),
|
||||
"password": clientPassword,
|
||||
}
|
||||
if err := api.post("/api/v1/users", "", body, &created); err != nil {
|
||||
return nil, nil, fmt.Errorf("register client %d: %w", i, err)
|
||||
}
|
||||
token, err := api.login(username, clientPassword)
|
||||
if err != nil {
|
||||
return nil, nil, fmt.Errorf("log in client %d: %w", i, err)
|
||||
}
|
||||
ids = append(ids, created.ID)
|
||||
tokens = append(tokens, token)
|
||||
}
|
||||
return ids, tokens, nil
|
||||
}
|
||||
|
||||
func createChat(api *client, runID string, members []string, creatorToken string) (chat.ChatResponse, error) {
|
||||
var created chat.ChatResponse
|
||||
body := map[string]any{"title": "Concurrency check " + runID, "member_ids": members}
|
||||
if err := api.post("/api/v1/chats", creatorToken, body, &created); err != nil {
|
||||
return chat.ChatResponse{}, fmt.Errorf("create chat: %w", err)
|
||||
}
|
||||
return created, nil
|
||||
}
|
||||
|
||||
// sendConcurrently gives every member its own HTTP client and releases them
|
||||
// together, so the server sees genuinely parallel writes to one chat.
|
||||
func sendConcurrently(baseURL, chatID string, tokens []string, perClient int) (map[string]struct{}, int) {
|
||||
var mu sync.Mutex
|
||||
created := make(map[string]struct{}, len(tokens)*perClient)
|
||||
failed := 0
|
||||
|
||||
var wg sync.WaitGroup
|
||||
start := make(chan struct{})
|
||||
for i, token := range tokens {
|
||||
wg.Add(1)
|
||||
go func(n int, token string) {
|
||||
defer wg.Done()
|
||||
own := newClient(baseURL)
|
||||
<-start
|
||||
for m := range perClient {
|
||||
var msg chat.MessageResponse
|
||||
body := map[string]string{"text": fmt.Sprintf("client %d message %d", n, m)}
|
||||
err := own.post("/api/v1/chats/"+chatID+"/messages", token, body, &msg)
|
||||
mu.Lock()
|
||||
if err != nil {
|
||||
failed++
|
||||
} else {
|
||||
created[msg.ID] = struct{}{}
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}(i, token)
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
return created, failed
|
||||
}
|
||||
|
||||
func readHistory(api *client, chatID, token string) ([]string, int, error) {
|
||||
var ids []string
|
||||
pages := 0
|
||||
cursor := ""
|
||||
for {
|
||||
var page pagination.PageResponse[chat.MessageResponse]
|
||||
if err := api.get("/api/v1/chats/"+chatID+"/messages", token, cursor, &page); err != nil {
|
||||
return nil, pages, fmt.Errorf("read history: %w", err)
|
||||
}
|
||||
pages++
|
||||
for _, m := range page.Items {
|
||||
ids = append(ids, m.ID)
|
||||
}
|
||||
if page.NextCursor == nil {
|
||||
return ids, pages, nil
|
||||
}
|
||||
cursor = *page.NextCursor
|
||||
}
|
||||
}
|
||||
|
||||
// compare returns how many distinct ids the history holds and how many
|
||||
// acknowledged ids never showed up in it.
|
||||
func compare(created map[string]struct{}, listed []string) (unique, missing int) {
|
||||
seen := make(map[string]struct{}, len(listed))
|
||||
for _, id := range listed {
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
for id := range created {
|
||||
if _, ok := seen[id]; !ok {
|
||||
missing++
|
||||
}
|
||||
}
|
||||
return len(seen), missing
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/internal/app"
|
||||
"messenger/internal/testenv"
|
||||
)
|
||||
|
||||
var (
|
||||
db *testenv.DB
|
||||
kv *testenv.KV
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
ctx := context.Background()
|
||||
var err error
|
||||
if db, err = testenv.StartPostgres(ctx); err != nil {
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
if kv, err = testenv.StartValkey(ctx); err != nil {
|
||||
db.Close()
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
os.Exit(1)
|
||||
}
|
||||
code := m.Run()
|
||||
kv.Close()
|
||||
db.Close()
|
||||
os.Exit(code)
|
||||
}
|
||||
|
||||
func TestRunAgainstLiveServer(t *testing.T) {
|
||||
log := slog.New(slog.NewTextHandler(io.Discard, nil))
|
||||
a := app.New(app.Deps{Log: log, DB: db.Pool, Sessions: kv.Client, SessionTTL: time.Hour, MaxBodyBytes: 64 * 1024})
|
||||
srv := httptest.NewServer(a.Engine)
|
||||
defer srv.Close()
|
||||
|
||||
report, err := run(srv.URL, 10, 10)
|
||||
if err != nil {
|
||||
t.Fatalf("run error: %v", err)
|
||||
}
|
||||
if report.Clients != 10 || report.PerClient != 10 || report.Expected != 100 {
|
||||
t.Fatalf("report = %+v", report)
|
||||
}
|
||||
if report.Created != 100 || report.Listed != 100 || report.Unique != 100 || report.Missing != 0 {
|
||||
t.Fatalf("report = %+v", report)
|
||||
}
|
||||
if !report.Ordered || !report.Passed {
|
||||
t.Fatalf("report = %+v", report)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunFailsOnUnreachableServer(t *testing.T) {
|
||||
if _, err := run("http://127.0.0.1:1", 2, 2); err == nil {
|
||||
t.Fatal("expected an error for an unreachable server")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user