diff --git a/.dockerignore b/.dockerignore index bc16e556..d89d8b24 100644 --- a/.dockerignore +++ b/.dockerignore @@ -18,3 +18,4 @@ __pycache__ node_modules evidence .DS_Store +.agent diff --git a/.env.example b/.env.example index d99d26a2..654d7678 100644 --- a/.env.example +++ b/.env.example @@ -1,20 +1,29 @@ -# Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. APP_HOST=0.0.0.0 APP_PORT=8080 -APP_ENV=lab +MAX_INFLIGHT_REQUESTS=0 SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. + +LOG_LEVEL=INFO + DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: -SESSION_STORE=postgres + +# Session store: postgres | redis (redis requires CACHE_URL_FILE) +SESSION_STORE=redis CACHE_URL_FILE=/run/secrets/cache_url + API_REPLICAS=2 IDEMPOTENCY_TTL_SECONDS=86400 + +API_CPUS=0.5 +API_MEM=512M +LB_CPUS=0.5 + S3_ENDPOINT=http://s3:9000 S3_REGION=us-east-1 S3_BUCKET=messenger-attachments S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key +S3_BOOTSTRAP_ACCESS_KEY_ID_FILE=/run/secrets/s3_bootstrap_access_key_id +S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_bootstrap_secret_access_key MAX_UPLOAD_BYTES=10485760 MAX_IMAGE_PIXELS=40000000 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 00000000..af248579 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,11 @@ +# Normalize text files to LF in the repository. +* text=auto eol=lf + +# Binary fixtures and assets +*.png binary +*.jpg binary +*.jpeg binary +*.gif binary +*.webp binary +*.ico binary +*.pdf binary diff --git a/.gitignore b/.gitignore index b5a71896..1a3c10b5 100644 --- a/.gitignore +++ b/.gitignore @@ -28,5 +28,6 @@ coverage/ .DS_Store .idea/ .vscode/ +.agent/ # Keep small, sanitized evidence summaries in evidence/ under version control. # Inspect git diff --cached: ignore rules are not a secret scanner. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..ed559f5f --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +FROM golang:1.27.1 AS build +WORKDIR /src + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN CGO_ENABLED=0 GOOS=linux go build -o /out/server ./cmd/server + +FROM gcr.io/distroless/static-debian12:nonroot +WORKDIR /app +COPY --from=build /out/server . +COPY contracts ./contracts +COPY web/swagger ./web/swagger + +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \ + CMD ["/app/server", "healthcheck"] + +ENTRYPOINT ["/app/server"] diff --git a/Dockerfile.migrate b/Dockerfile.migrate new file mode 100644 index 00000000..4b82ab32 --- /dev/null +++ b/Dockerfile.migrate @@ -0,0 +1,8 @@ +FROM golang:1.27.1 AS build +ENV CGO_ENABLED=0 +RUN go install github.com/pressly/goose/v3/cmd/goose@v3.28.0 + +FROM alpine:3.24 +COPY --from=build /go/bin/goose /usr/local/bin/goose +COPY db/migrations /migrations +ENTRYPOINT ["sh", "-c", "goose -dir /migrations postgres \"$(cat /run/secrets/database_url)\" up"] diff --git a/Dockerfile.reconcile b/Dockerfile.reconcile new file mode 100644 index 00000000..a8042a53 --- /dev/null +++ b/Dockerfile.reconcile @@ -0,0 +1,14 @@ +FROM golang:1.27.1 AS build +WORKDIR /src + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN CGO_ENABLED=0 GOOS=linux go build -o /out/reconcile ./cmd/reconcile + +FROM gcr.io/distroless/static-debian12:nonroot +WORKDIR /app +COPY --from=build /out/reconcile . + +ENTRYPOINT ["/app/reconcile"] diff --git a/Dockerfile.s3config b/Dockerfile.s3config new file mode 100644 index 00000000..87053443 --- /dev/null +++ b/Dockerfile.s3config @@ -0,0 +1,13 @@ +FROM golang:1.27.1 AS build +ENV CGO_ENABLED=0 +WORKDIR /src + +COPY go.mod go.sum ./ +RUN go mod download + +COPY . . +RUN go build -o /out/s3config ./cmd/s3config + +FROM gcr.io/distroless/static-debian12:nonroot +COPY --from=build /out/s3config /usr/local/bin/s3config +ENTRYPOINT ["/usr/local/bin/s3config"] diff --git a/Makefile b/Makefile index 5d86d171..861ad0c4 100644 --- a/Makefile +++ b/Makefile @@ -28,3 +28,5 @@ test-persistence: test-failover: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2) $(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +-include student.mk diff --git a/REPORT.md b/REPORT.md index 9ad2b84f..0f523de8 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,123 @@ # Отчёт — лабораторная 4 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO -- Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Автор, группа: Каменски Лин, БПИ2301 +- Целевая оценка; выбранный трек 5, если нужен: 5; трек 2 +- Commit/PR: [Pull Request 58](https://gitea.privcloud.tech/mtusi2026/mtusi2026lab4/pulls/58) +- Использованные источники, библиотеки и помощь ИИ: материалы лабораторной; Go 1.27.1, chi v5, pgx v5, sqlc, goose, PostgreSQL 18.6, Redis 8.8.3, Nginx 1.31.7, Docker Compose, SeaweedFS 4.51; OpenCode - для ускорения работы и оформления отчёта ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Требуются Go 1.26+ и Docker с Compose. Образы: `golang:1.27.1`, `postgres:18.6`, `redis:8.8.3-alpine3.23`, `nginx:1.31.7-alpine3.24`, `chrislusf/seaweedfs:4.51`. + +Создать локальные секреты - файлы в `secrets/`, в каждый записать значение без лишних символов: +- `database_url.txt` - URL PostgreSQL для API; +- `cache_url.txt` - URL Redis; +- `postgres_password.txt` - пароль пользователя `messenger`; +- `test_database_url.txt` - URL БД для тестов; +- `s3_access_key_id.txt` - S3 access key ID для API; +- `s3_secret_access_key.txt` - S3 secret access key для API; +- `s3_bootstrap_access_key_id.txt` - bootstrap access key ID; +- `s3_bootstrap_secret_access_key.txt` - bootstrap secret access key. + +Пример (значения заменить своими): +```bash +mkdir -p secrets +printf '%s' 'CHANGE_ME' > secrets/postgres_password.txt +printf '%s' 'postgres://messenger:CHANGE_ME@postgres:5432/messenger?sslmode=disable' > secrets/database_url.txt +printf '%s' 'redis://redis:6379/0' > secrets/cache_url.txt +``` + +Скопировать шаблон переменных окружения: +```bash +cp .env.example .env +``` + +Сборка и запуск: +```bash +docker compose up --build -d +``` + +Готовность API проверяется запросом к `/health/ready`; после этого API доступен на http://localhost:8080. + +Сваггер доступен на http://localhost:8080/swagger/. + +Безопасная остановка (данные в volume `pgdata` сохраняются): +```bash +docker compose down +``` + +Намеренное удаление учебных данных: +```bash +docker compose down -v +``` ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +**Процессы/хранилища:** `api` (Go, chi, pgxpool, sqlc, goose, s3-client) × `API_REPLICAS` (≥2) → PostgreSQL (источник истины метаданных) + Redis (кэш сессий с fallback) → SeaweedFS S3 (байты вложений, приватный bucket `messenger-attachments`). Единственная публичная точка входа - nginx на `127.0.0.1:8080`. + +**Источник истины** - PostgreSQL. API не хранит локального состояния: сессии доступны всем репликам через общее хранилище, режим выбирается переменной `SESSION_STORE` (`postgres` | `redis`). При недоступности Redis API обходит кэш и читает сессию из PostgreSQL. + +**Что перенесено из lab3:** Go-приложение и PostgreSQL, аутентификация, сессии, чаты, сообщения, идемпотентность и `X-Instance-Id`, Redis-кэш сессий, `compose.yaml`, `deploy/nginx.conf`, `Dockerfile`, `Dockerfile.migrate`, `scripts/restart.sh`, `scripts/stop-one.sh`, `student-tests`, `student.mk`, `Makefile`, `web/swagger`, `contracts`. + +**Что изменено в lab4:** Добавлен S3-совместимое объектное хранилище (SeaweedFS) в compose.yaml с приватным bucket `messenger-attachments`, политика доступа и bootstrap-конфигурация; таблица/миграция `attachments` + типы `attachment_content_type` и `attachment_status`; sqlc-запросы и DTO для вложений; `attachment_ids` в сообщениях (0-10 уникальных ID, по умолчанию `[]`); `POST /api/v1/chats/{chat_id}/attachments` - загрузка raw PNG/JPEG с ограничениями (10 MiB, 40 млн пикселей, MIME по содержимому); эндпоинты `GET /api/v1/attachments/{id}` и `GET /api/v1/attachments/{id}/content`; поле `attachment_ids` в ответах сообщений; механизм `queued → ready` (upload пишет `queued`, после успешного S3 Put переводит в `ready`); Nginx `client_max_body_size 11m`; очистка orphan-объектов через reconciler с индексом `idx_attachments_queued_created_at`; отдельные S3 credentials: bootstrap (Admin) и API (Read/Write/List/Tagging, без Admin/Delete). + +**Загрузка и память.** Тело запроса ограничено 10 MiB и потоково записывается во временный файл с одновременным подсчётом SHA-256. По заголовку изображения проверяются формат, MIME и лимит 40 млн пикселей до распаковки. Затем выполняется полное декодирование для проверки целостности; на одной API-реплике одновременно работает не более одного декодера. Тело сохраняется потоково, но декодер держит распакованные пиксели в RAM. Затем файл передаётся в S3; временный файл удаляется при завершении запроса. Контекст запроса и чтение тела на стороне API ограничены 5 с; таймаут чтения возвращает `408 invalid_request`. Локальный файл нужен только на время загрузки, оригинал хранится в S3. + +**Очистка незавершённых загрузок.** До записи в S3 создаётся строка `queued`, после успеха - `ready`. Фоновая очистка каждые 10 минут обрабатывает брошенные `queued` старше 1 часа. Полную сверку объектов и метаданных выполняет отдельный reconciler; часовой grace period защищает свежие загрузки. + +**Готовность и недоступность S3.** `GET /health/ready` теперь помимо PostgreSQL проверяет доступ к S3 bucket через `HeadBucket` с общим таймаутом 2 с. При недоступности S3 загрузка и скачивание возвращают `503 unavailable`. Повторы Nginx при `503` отключены: PostgreSQL и S3 общие для всех API-реплик, поэтому переход на другую реплику при их недоступности не помогает. После рестарта S3 доступ к bucket может восстановиться раньше загрузки и скачивания - тест дополнительно ждёт их восстановления. + +**Bootstrap и доступ к S3.** Bucket создаётся только при отсутствии; повторный запуск и одновременное создание несколькими репликами допустимы. API и bootstrap используют разные credentials. Порты S3 и административных интерфейсов не публикуются на хост, HTTP-интерфейс filer отключён. Прямой анонимный доступ к объекту возвращает `403 Forbidden`. + +**Переменные `.env.example`:** `APP_HOST`, `APP_PORT` - адрес и порт API; `SESSION_TTL_SECONDS` - срок жизни сессии; `LOG_LEVEL` - уровень логов; `DATABASE_URL_FILE`, `CACHE_URL_FILE` - пути к secrets с URL PostgreSQL и Redis; `SESSION_STORE` - режим хранения сессий, `postgres` или `redis`; `IDEMPOTENCY_TTL_SECONDS` - срок хранения ключей идемпотентности; `API_REPLICAS` - число реплик `api`; `API_CPUS`, `API_MEM` - лимиты CPU и памяти каждой реплики; `LB_CPUS` - лимит CPU Nginx; `S3_ENDPOINT`, `S3_REGION`, `S3_BUCKET` - настройки S3; `MAX_UPLOAD_BYTES`, `MAX_IMAGE_PIXELS` - лимиты загрузок; `S3_ACCESS_KEY_ID_FILE`, `S3_SECRET_ACCESS_KEY_FILE`, `S3_BOOTSTRAP_ACCESS_KEY_ID_FILE`, `S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE` - пути к secrets. ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make test` | `PASS` | `evidence/make-test.txt` | +| Уровень 3 | +| Загрузка, ready, checksum и сообщение с вложением | `make test` | `PASS` | `evidence/make-test.txt` | +| Запрет доступа постороннему | `make test` | `404` для метаданных и содержимого | `evidence/make-test.txt` | +| PNG/JPEG, MIME, лимиты размера и пикселей, безопасный filename | `make test`; `make contract-go`: `TestAttachmentJPEG`, `TestAttachmentTooLarge`, `TestAttachmentTooManyPixels`, `TestAttachmentCorruptedImage`, `TestAttachmentMimeSubstitution`, `TestAttachmentUnsafeFilename` | `PASS` | `evidence/make-test.txt`, `evidence/contract-go.txt` | +| Запрет прямого доступа к объекту S3 | `docker compose exec -T s3 wget -S -O /dev/null "http://127.0.0.1:9000/messenger-attachments/${OBJECT_KEY}"` | `403 Forbidden` | `evidence/s3-private.txt` | +| Скачивание после остановки загрузившей реплики | `make failure-go`: `TestAttachmentUploaderFailover` | `PASS` | `evidence/failure-go.txt` | +| Сохранность метаданных и оригинала после restart | `make test-persistence ACTION_SCRIPT=scripts/restart.sh`; `docker compose ps -a` до и после | `PASS` | `evidence/test-persistence.txt` | +| Отказ S3 и восстановление | `make s3-failure-go`: `TestS3Down` | `PASS` | `evidence/s3-failure-go.txt` | +| Уровень 4 | +| Негативные тесты вложений без 500 | `make contract-go`: `TestAttachmentTooLarge`, `TestAttachmentCorruptedImage`, `TestAttachmentMimeSubstitution`, `TestAttachmentForeignID`, `TestAttachmentFromAnotherChat` | `PASS` | `evidence/contract-go.txt` | +| ≥10 одновременных загрузок и пик памяти | `make load-go LOAD_WORKERS=10 LOAD_DURATION=30s` | `PASS`: 10 загрузчиков, 30,4 с, 632 ответа 201, ошибок 0; 20,82 загрузки/с; память API-1: 12,2 → 50,9 MiB, API-2: 13,3 → 56,7 MiB | `evidence/load-go.txt` | +| Уровень 5, трек 2 | +| Обнаружение и исправление orphan/missing при моделировании рассогласования S3/БД | `make reconcile-test-go`: `TestReconcileRepair` | `PASS`: dry-run нашёл 1 orphan и 1 missing без изменений; исправление удалило объект и пометило вложение failed; повторный запуск без изменений | `evidence/reconcile-test-go.txt` | +| Свежие и используемые вложения сохраняются | `make reconcile-test-go`: `TestReconcileSafety` | `PASS`: пропущены 1 свежее и 1 используемое вложение; строки и оригиналы сохранены | `evidence/reconcile-test-go.txt` | +| Стоимость обхода объектов и метаданных | `make reconcile-test-go`: `TestReconcileCost` (3 обхода, объекты, строки, страницы, время) | `PASS`: 14 607 объектов, 14 614 строк, 15 страниц S3; время обходов: 1609 / 1721 / 1587 мс | `evidence/reconcile-test-go.txt` | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +### Трек 2: согласованность двух хранилищ -| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | -| --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +**Гипотеза:** сверка S3 и PostgreSQL обнаруживает объекты без метаданных и метаданные без оригинала; исправление повторяемо, а свежие и используемые вложения сохраняются. + +Методика: `make reconcile-test-go` создаёт вложения через API и подменяет S3-ключ одного из них в БД. Исходный объект становится orphan, строка указывает на отсутствующий объект (missing). Затем выполняются dry-run, исправление и повторное исправление. В этом сценарии grace period сокращён до 1 с; в проверке безопасности используется штатный 1 час. Команда запускается на стенде без параллельных загрузок. + +| Проверка | Что ожидаем | Что наблюдали | +| --- | --- | --- | +| Dry-run после рассогласования | Найти orphan/missing без изменения данных | Обнаружены 1 orphan и 1 missing; изменений нет | +| Исправление | Удалить orphan и отметить missing как ошибку | Orphan удалён; missing переведён в `failed` с `object_missing` | +| Повторное исправление | Не менять уже исправленные данные | Расхождения устранены, новых изменений нет | +| Защита вложений | Сохранить свежие и используемые вложения | Свежий `queued` и привязанный к сообщению `queued` старше 2 часов пропущены; строки сохранены, скачанные оригиналы совпадают побайтно | + +Стоимость полного обхода измерена тремя последовательными dry-run: 14 607 объектов S3, 14 614 строк PostgreSQL, 15 страниц списка S3. Время: 1609 / 1721 / 1587 мс, медиана 1609 мс. Доказательство: `evidence/reconcile-test-go.txt`. + +Ограничения: моделируются последствия рассогласования через SQL, а не реальные сетевые отказы или падение процесса между операциями. Сверка не восстанавливает утраченные байты: missing помечается ошибкой. Списки объектов и строк загружаются в память целиком; стоимость растёт с объёмом данных. Измерен один объём, поэтому масштабируемость не доказана. Время трёх обходов составило 1,59-1,72 с. + +**Вывод**: Гипотеза подтверждена на подготовленных сценариях: reconciler обнаружил и исправил orphan/missing, повторный запуск не внёс изменений, свежие и используемые вложения сохранились. Обход 14,6 тыс. объектов занял 1,59-1,72 с. Утраченный оригинал не восстанавливается, а отмечается ошибкой. ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +- Nginx, PostgreSQL и SeaweedFS работают в одном экземпляре на одном хосте. Реплики API не защищают от отказа этих компонентов или самого хоста. +- Если удалить сессию из Redis не удалось, logout возвращает `503 unavailable`: сессия уже удалена из PostgreSQL, но старый токен может приниматься из кэша до 60 с. Несмотря на `503`, может потребоваться повторный вход. Пока Redis недоступен, получить `204` нельзя, хотя сессия в БД уже удалена; после восстановления повторный logout вернёт `401`. +- Полное декодирование требует памяти под распакованное изображение. Для ограничения расхода RAM на каждой API-реплике работает один декодер, поэтому параллельные загрузки ожидают своей очереди. +- Общей транзакции PostgreSQL и S3 нет: расхождения исправляются последующей сверкой. Reconciler отмечает отсутствие оригинала, но не восстанавливает байты. Сохранность volumes не заменяет резервные копии обоих хранилищ. +- Фоновая очистка сначала удаляет строку БД, затем объект S3. Если удаление из S3 не удалось, объект останется без метаданных до запуска reconciler. +- Reconciler загружает списки объектов и метаданных целиком в память, поэтому расход памяти растёт с объёмом хранилища. diff --git a/cmd/reconcile/main.go b/cmd/reconcile/main.go new file mode 100644 index 00000000..a71f7e76 --- /dev/null +++ b/cmd/reconcile/main.go @@ -0,0 +1,133 @@ +package main + +import ( + "context" + "encoding/json" + "flag" + "fmt" + "log/slog" + "os" + "os/signal" + "syscall" + + "github.com/joho/godotenv" + + "mtusi2026-messenger/internal/config" + "mtusi2026-messenger/internal/db" + "mtusi2026-messenger/internal/postgres" + "mtusi2026-messenger/internal/reconcile" + s3store "mtusi2026-messenger/internal/s3" +) + +func main() { + if err := run(); err != nil { + fmt.Fprintln(os.Stderr, "reconcile:", err) + os.Exit(1) + } +} + +func run() error { + if err := godotenv.Load(); err != nil && !os.IsNotExist(err) { + return err + } + + dryRun := flag.Bool("dry-run", true, "report findings without changing PostgreSQL or S3") + grace := flag.Duration("grace", reconcile.DefaultGrace, "minimum age before an object or row may be repaired") + maxDeletes := flag.Int("max-deletes", reconcile.DefaultMaxDeletes, "maximum destructive repairs per run") + asJSON := flag.Bool("json", false, "print the report as JSON") + flag.Parse() + + cfg := config.Load() + log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: cfg.Logging.Level})) + + databaseURL, err := cfg.Database.URL() + if err != nil { + return err + } + + ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM) + defer stop() + + pool, err := postgres.New(ctx, databaseURL) + if err != nil { + return err + } + defer pool.Close() + + s3Client, err := s3store.New(ctx, cfg.S3, log) + if err != nil { + return err + } + + reconciler := reconcile.New(s3Client, db.New(pool), log) + + report, err := reconciler.Run(ctx, reconcile.Options{ + DryRun: *dryRun, + Grace: *grace, + MaxDeletes: *maxDeletes, + }) + if err != nil { + return err + } + + if *asJSON { + encoder := json.NewEncoder(os.Stdout) + encoder.SetIndent("", " ") + if err := encoder.Encode(report); err != nil { + return err + } + } else { + printReport(report) + } + + if len(report.Errors) > 0 { + return fmt.Errorf("completed with %d errors", len(report.Errors)) + } + + return nil +} + +func printReport(report reconcile.Report) { + fmt.Printf("dry_run=%t duration_ms=%d\n", report.DryRun, report.DurationMS) + fmt.Printf( + "scanned objects=%d rows=%d list_pages=%d\n", + report.ScannedObjects, + report.ScannedRows, + report.ListPages, + ) + fmt.Printf( + "found orphan_objects=%d missing_attachments=%d stale_uploads=%d corrupt_objects=%d\n", + len(report.OrphanObjects), + len(report.MissingAttachments), + len(report.StaleUploads), + len(report.CorruptObjects), + ) + fmt.Printf( + "skipped fresh=%d referenced=%d inactive=%d\n", + report.SkippedFresh, + report.SkippedReferenced, + report.SkippedInactive, + ) + fmt.Printf( + "changes deleted_objects=%d deleted_rows=%d marked_failed=%d\n", + report.DeletedObjects, + report.DeletedRows, + report.MarkedFailed, + ) + + for _, key := range report.OrphanObjects { + fmt.Printf("orphan_object %s\n", key) + } + for _, key := range report.CorruptObjects { + fmt.Printf("corrupt_object %s\n", key) + } + for _, id := range report.MissingAttachments { + fmt.Printf("missing_attachment %s\n", id) + } + for _, id := range report.StaleUploads { + fmt.Printf("stale_upload %s\n", id) + } + for _, message := range report.Errors { + fmt.Printf("error %s\n", message) + } +} diff --git a/cmd/s3config/main.go b/cmd/s3config/main.go new file mode 100644 index 00000000..c619e27a --- /dev/null +++ b/cmd/s3config/main.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "os" + + "mtusi2026-messenger/internal/config" + "mtusi2026-messenger/internal/s3" +) + +func main() { + cfg := config.LoadS3() + + err := s3.WriteConfig("/config/s3.json", cfg) + if err != nil { + fmt.Fprintln(os.Stderr, "s3config:", err) + os.Exit(1) + } +} diff --git a/cmd/server/main.go b/cmd/server/main.go new file mode 100644 index 00000000..e4032d40 --- /dev/null +++ b/cmd/server/main.go @@ -0,0 +1,18 @@ +package main + +import ( + "log" + "os" + + "mtusi2026-messenger/internal/app" +) + +func main() { + if len(os.Args) > 1 && os.Args[1] == "healthcheck" { + os.Exit(app.Healthcheck()) + } + + if err := app.Run(); err != nil { + log.Fatal(err) + } +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..1c52a328 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,196 @@ +services: + api: + build: + context: . + dockerfile: Dockerfile + image: mtusi-messenger:lab4 + env_file: + - .env + secrets: + - database_url + - cache_url + - s3_access_key_id + - s3_secret_access_key + - s3_bootstrap_access_key_id + - s3_bootstrap_secret_access_key + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: "${API_CPUS:-0.5}" + memory: ${API_MEM:-512M} + depends_on: + migrate: + condition: service_completed_successfully + s3: + condition: service_healthy + restart: unless-stopped + + postgres: + image: postgres:18.6 + ports: + - "127.0.0.1:5432:5432" + environment: + POSTGRES_USER: messenger + POSTGRES_DB: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + secrets: + - postgres_password + volumes: + - pgdata:/var/lib/postgresql + healthcheck: + test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U $${POSTGRES_USER} -d $${POSTGRES_DB}"] + interval: 5s + timeout: 3s + retries: 10 + deploy: + resources: + limits: + cpus: "1.0" + memory: 512M + restart: unless-stopped + + migrate: + build: + context: . + dockerfile: Dockerfile.migrate + secrets: + - database_url + deploy: + resources: + limits: + cpus: "0.5" + memory: 128M + depends_on: + postgres: + condition: service_healthy + restart: "no" + + redis: + image: redis:8.8.3-alpine3.23 + command: ["redis-server", "--save", "", "--appendonly", "no"] + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + deploy: + resources: + limits: + cpus: "0.25" + memory: 128M + restart: unless-stopped + + s3-config-init: + build: + context: . + dockerfile: Dockerfile.s3config + image: mtusi-messenger-s3config:lab4 + entrypoint: ["/usr/local/bin/s3config"] + user: "0:0" + env_file: + - .env + secrets: + - s3_access_key_id + - s3_secret_access_key + - s3_bootstrap_access_key_id + - s3_bootstrap_secret_access_key + volumes: + - s3config:/config + deploy: + resources: + limits: + cpus: "0.25" + memory: 64M + restart: "no" + + s3: + image: chrislusf/seaweedfs:4.51 + command: + - "server" + - "-dir=/data" + - "-s3" + - "-s3.port=9000" + - "-s3.iam=false" + - "-filer.disableHttp" + - "-s3.port.iceberg=0" + - "-s3.port.lance=0" + - "-s3.config=/config/s3.json" + depends_on: + s3-config-init: + condition: service_completed_successfully + volumes: + - s3config:/config:ro + - seaweedfs:/data + healthcheck: + test: + - CMD-SHELL + - wget -qO- http://127.0.0.1:9000/healthz >/dev/null && wget -qO- http://127.0.0.1:9333/dir/status | grep -q '"Url":' + interval: 10s + timeout: 5s + retries: 5 + start_period: 20s + deploy: + resources: + limits: + cpus: "0.5" + memory: 512M + restart: unless-stopped + + load_balancer: + image: nginx:1.31.7-alpine3.24 + ports: + - "127.0.0.1:8080:8080" + volumes: + - ./deploy/nginx.conf:/etc/nginx/nginx.conf:ro + deploy: + resources: + limits: + cpus: ${LB_CPUS:-0.5} + memory: 128M + depends_on: + api: + condition: service_healthy + restart: unless-stopped + + reconcile: + build: + context: . + dockerfile: Dockerfile.reconcile + image: mtusi-messenger-reconcile:lab4 + profiles: ["tools"] + env_file: + - .env + secrets: + - database_url + - s3_access_key_id + - s3_secret_access_key + - s3_bootstrap_access_key_id + - s3_bootstrap_secret_access_key + deploy: + resources: + limits: + cpus: "0.25" + memory: 64M + restart: "no" + +secrets: + postgres_password: + file: ./secrets/postgres_password.txt + database_url: + file: ./secrets/database_url.txt + cache_url: + file: ./secrets/cache_url.txt + s3_access_key_id: + file: ./secrets/s3_access_key_id.txt + s3_secret_access_key: + file: ./secrets/s3_secret_access_key.txt + s3_bootstrap_access_key_id: + file: ./secrets/s3_bootstrap_access_key_id.txt + s3_bootstrap_secret_access_key: + file: ./secrets/s3_bootstrap_secret_access_key.txt + +volumes: + pgdata: + seaweedfs: + s3config: diff --git a/db/migrations/00001_init.sql b/db/migrations/00001_init.sql new file mode 100644 index 00000000..ae4b6415 --- /dev/null +++ b/db/migrations/00001_init.sql @@ -0,0 +1,51 @@ +-- +goose Up +CREATE TABLE users ( + id uuid PRIMARY KEY, + username text NOT NULL UNIQUE + CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'), + display_name text NOT NULL + CONSTRAINT users_display_name_not_blank CHECK (display_name ~ '\S'), + password_hash text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE sessions ( + token_hash text PRIMARY KEY, + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + created_at timestamptz NOT NULL DEFAULT now(), + expires_at timestamptz NOT NULL +); + +CREATE INDEX idx_sessions_expires_at ON sessions (expires_at); + +CREATE TABLE chats ( + id uuid PRIMARY KEY, + title text NOT NULL + CONSTRAINT chats_title_not_blank CHECK (title ~ '\S'), + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE TABLE chat_members ( + chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + PRIMARY KEY (chat_id, user_id) +); + +CREATE INDEX idx_chat_members_user_id ON chat_members (user_id); + +CREATE TABLE messages ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, + sender_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT, + text text NOT NULL, + created_at timestamptz NOT NULL DEFAULT now() +); + +CREATE INDEX idx_messages_chat_history ON messages (chat_id, created_at, id); + +-- +goose Down +DROP TABLE messages; +DROP TABLE chat_members; +DROP TABLE chats; +DROP TABLE sessions; +DROP TABLE users; diff --git a/db/migrations/00002_add_idempotency_keys.sql b/db/migrations/00002_add_idempotency_keys.sql new file mode 100644 index 00000000..3820d3ca --- /dev/null +++ b/db/migrations/00002_add_idempotency_keys.sql @@ -0,0 +1,17 @@ +-- +goose Up +CREATE TABLE idempotency_keys ( + user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE, + method text NOT NULL, + path text NOT NULL, + key text NOT NULL, + request_hash text NOT NULL, + message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE + DEFERRABLE INITIALLY DEFERRED, + created_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (user_id, method, path, key) +); + +CREATE INDEX idx_idempotency_keys_created_at ON idempotency_keys (created_at); + +-- +goose Down +DROP TABLE idempotency_keys; diff --git a/db/migrations/00003_attachments.sql b/db/migrations/00003_attachments.sql new file mode 100644 index 00000000..febdea50 --- /dev/null +++ b/db/migrations/00003_attachments.sql @@ -0,0 +1,38 @@ +-- +goose Up +CREATE TYPE attachment_content_type AS ENUM ('image/png', 'image/jpeg'); +CREATE TYPE attachment_status AS ENUM ('queued', 'processing', 'ready', 'failed'); + +CREATE TABLE attachments ( + id uuid PRIMARY KEY, + chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE, + owner_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT, + s3_key text NOT NULL UNIQUE, + filename text NOT NULL, + content_type attachment_content_type NOT NULL, + size_bytes int NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760), + sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'), + status attachment_status NOT NULL DEFAULT 'queued', + error_code text, + created_at timestamptz NOT NULL DEFAULT now(), + updated_at timestamptz NOT NULL DEFAULT now() +); + +CREATE INDEX idx_attachments_chat_id ON attachments (chat_id, created_at); +CREATE INDEX idx_attachments_owner_id ON attachments (owner_id); +CREATE INDEX idx_attachments_queued_created_at ON attachments (created_at) WHERE status = 'queued'; + +CREATE TABLE message_attachments ( + message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE, + attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE RESTRICT, + position int NOT NULL CHECK (position >= 0 AND position < 10), + PRIMARY KEY (message_id, attachment_id), + UNIQUE (message_id, position) +); + +CREATE INDEX idx_message_attachments_attachment_id ON message_attachments (attachment_id); + +-- +goose Down +DROP TABLE message_attachments; +DROP TABLE attachments; +DROP TYPE attachment_status; +DROP TYPE attachment_content_type; diff --git a/db/queries/attachments.sql b/db/queries/attachments.sql new file mode 100644 index 00000000..9f6ce774 --- /dev/null +++ b/db/queries/attachments.sql @@ -0,0 +1,95 @@ +-- name: CreateAttachment :one +INSERT INTO attachments ( + id, + chat_id, + owner_id, + s3_key, + filename, + content_type, + size_bytes, + sha256, + error_code +) +SELECT + sqlc.arg(id)::uuid, + sqlc.arg(chat_id)::uuid, + sqlc.arg(owner_id)::uuid, + sqlc.arg(s3_key)::text, + sqlc.arg(filename)::text, + sqlc.arg(content_type)::attachment_content_type, + sqlc.arg(size_bytes)::int, + sqlc.arg(sha256)::text, + NULLIF(sqlc.arg(error_code)::text, '') +WHERE EXISTS ( + SELECT 1 FROM chat_members + WHERE chat_id = sqlc.arg(chat_id) + AND user_id = sqlc.arg(owner_id) +) +RETURNING *; + +-- name: CreateMessageAttachment :exec +INSERT INTO message_attachments ( + message_id, + attachment_id, + position +) VALUES ( + sqlc.arg(message_id), + sqlc.arg(attachment_id), + sqlc.arg(position) +); + +-- name: GetAttachmentByID :one +SELECT * FROM attachments +WHERE id = $1::uuid; + +-- name: ListAttachmentsByIDs :many +SELECT status +FROM attachments +WHERE chat_id = sqlc.arg(chat_id)::uuid + AND id = ANY(sqlc.arg(ids)::uuid[]); + +-- name: ListMessageAttachmentIDs :many +SELECT attachment_id +FROM message_attachments +WHERE message_id = sqlc.arg(message_id)::uuid +ORDER BY position; + +-- name: ListMessageAttachmentsByMessageIDs :many +SELECT message_id, attachment_id +FROM message_attachments +WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[]) +ORDER BY message_id, position; + +-- name: ListStaleAttachmentUploads :many +SELECT id, s3_key FROM attachments +WHERE status = 'queued' AND created_at < sqlc.arg(cutoff)::timestamptz; + +-- name: ListAttachmentsForReconcile :many +SELECT + a.id, + a.s3_key, + a.status, + a.size_bytes, + a.created_at, + EXISTS ( + SELECT 1 FROM message_attachments ma + WHERE ma.attachment_id = a.id + ) AS referenced +FROM attachments a; + +-- name: MarkAttachmentFailedIfReady :execrows +UPDATE attachments +SET status = 'failed', + error_code = sqlc.arg(error_code)::text, + updated_at = now() +WHERE id = sqlc.arg(id)::uuid AND status = 'ready'; + +-- name: MarkAttachmentReady :one +UPDATE attachments +SET status = 'ready', updated_at = now() +WHERE id = $1::uuid +RETURNING *; + +-- name: DeleteQueuedAttachment :execrows +DELETE FROM attachments +WHERE id = sqlc.arg(id)::uuid AND status = 'queued'; diff --git a/db/queries/chats.sql b/db/queries/chats.sql new file mode 100644 index 00000000..80d2ba3e --- /dev/null +++ b/db/queries/chats.sql @@ -0,0 +1,73 @@ +-- name: CreateChat :one +INSERT INTO chats ( + id, + title +) VALUES ( + sqlc.arg(id), + sqlc.arg(title) +) +RETURNING *; + +-- name: AddChatMembers :exec +INSERT INTO chat_members ( + chat_id, + user_id +) +SELECT + sqlc.arg(chat_id)::uuid, + unnest(sqlc.arg(user_ids)::uuid[]); + +-- name: IsChatMember :one +SELECT EXISTS ( + SELECT 1 FROM chat_members + WHERE chat_id = sqlc.arg(chat_id) + AND user_id = sqlc.arg(user_id) +); + +-- name: GetChat :one +SELECT + c.*, + ARRAY( + SELECT cm.user_id FROM chat_members cm + WHERE cm.chat_id = c.id + ORDER BY cm.user_id + )::uuid[] AS member_ids +FROM chats c +WHERE c.id = sqlc.arg(id) + AND EXISTS ( + SELECT 1 FROM chat_members m + WHERE m.chat_id = c.id + AND m.user_id = sqlc.arg(user_id) + ); + +-- name: ListChatsFirstPage :many +SELECT + c.*, + ARRAY( + SELECT cm.user_id FROM chat_members cm + WHERE cm.chat_id = c.id + ORDER BY cm.user_id + )::uuid[] AS member_ids +FROM chats c +JOIN chat_members m ON m.chat_id = c.id +WHERE m.user_id = sqlc.arg(user_id) +ORDER BY c.created_at, c.id +LIMIT sqlc.arg(lim); + +-- name: ListChatsAfter :many +SELECT + c.*, + ARRAY( + SELECT cm.user_id FROM chat_members cm + WHERE cm.chat_id = c.id + ORDER BY cm.user_id + )::uuid[] AS member_ids +FROM chats c +JOIN chat_members m ON m.chat_id = c.id +WHERE m.user_id = sqlc.arg(user_id) + AND (c.created_at, c.id) > ( + sqlc.arg(after_created_at)::timestamptz, + sqlc.arg(after_id)::uuid + ) +ORDER BY c.created_at, c.id +LIMIT sqlc.arg(lim); diff --git a/db/queries/idempotency_keys.sql b/db/queries/idempotency_keys.sql new file mode 100644 index 00000000..95239270 --- /dev/null +++ b/db/queries/idempotency_keys.sql @@ -0,0 +1,36 @@ +-- name: CreateIdempotencyKey :execrows +INSERT INTO idempotency_keys ( + user_id, + method, + path, + key, + request_hash, + message_id +) VALUES ( + sqlc.arg(user_id), + sqlc.arg(method), + sqlc.arg(path), + sqlc.arg(key), + sqlc.arg(request_hash), + sqlc.arg(message_id) +) +ON CONFLICT DO NOTHING; + +-- name: GetIdempotentMessage :one +SELECT + k.request_hash, + m.id, + m.chat_id, + m.sender_id, + m.text, + m.created_at +FROM idempotency_keys AS k +JOIN messages AS m ON m.id = k.message_id +WHERE k.user_id = sqlc.arg(user_id) + AND k.method = sqlc.arg(method) + AND k.path = sqlc.arg(path) + AND k.key = sqlc.arg(key); + +-- name: DeleteExpiredIdempotencyKeys :execrows +DELETE FROM idempotency_keys +WHERE created_at < sqlc.arg(cutoff); diff --git a/db/queries/messages.sql b/db/queries/messages.sql new file mode 100644 index 00000000..69635c4f --- /dev/null +++ b/db/queries/messages.sql @@ -0,0 +1,34 @@ +-- name: CreateMessage :one +INSERT INTO messages ( + id, + chat_id, + sender_id, + text +) +SELECT + sqlc.arg(id)::uuid, + sqlc.arg(chat_id)::uuid, + sqlc.arg(sender_id)::uuid, + sqlc.arg(text)::text +WHERE EXISTS ( + SELECT 1 FROM chat_members + WHERE chat_id = sqlc.arg(chat_id) + AND user_id = sqlc.arg(sender_id) +) +RETURNING *; + +-- name: ListMessagesFirstPage :many +SELECT * FROM messages +WHERE chat_id = sqlc.arg(chat_id) +ORDER BY created_at, id +LIMIT sqlc.arg(lim); + +-- name: ListMessagesAfter :many +SELECT * FROM messages +WHERE chat_id = sqlc.arg(chat_id) + AND (created_at, id) > ( + sqlc.arg(after_created_at)::timestamptz, + sqlc.arg(after_id)::uuid + ) +ORDER BY created_at, id +LIMIT sqlc.arg(lim); diff --git a/db/queries/sessions.sql b/db/queries/sessions.sql new file mode 100644 index 00000000..065907f2 --- /dev/null +++ b/db/queries/sessions.sql @@ -0,0 +1,24 @@ +-- name: CreateSession :exec +INSERT INTO sessions ( + token_hash, + user_id, + expires_at +) VALUES ( + sqlc.arg(token_hash), + sqlc.arg(user_id), + sqlc.arg(expires_at) +); + +-- name: GetActiveSession :one +SELECT user_id, expires_at FROM sessions +WHERE token_hash = sqlc.arg(token_hash) + AND expires_at > now(); + +-- name: DeleteSession :execrows +DELETE FROM sessions +WHERE token_hash = sqlc.arg(token_hash) + AND expires_at > now(); + +-- name: DeleteExpiredSessions :execrows +DELETE FROM sessions +WHERE expires_at <= now(); diff --git a/db/queries/users.sql b/db/queries/users.sql new file mode 100644 index 00000000..d6e9c030 --- /dev/null +++ b/db/queries/users.sql @@ -0,0 +1,21 @@ +-- name: CreateUser :one +INSERT INTO users ( + id, + username, + display_name, + password_hash +) VALUES ( + sqlc.arg(id), + sqlc.arg(username), + sqlc.arg(display_name), + sqlc.arg(password_hash) +) +RETURNING id, username, display_name, created_at; + +-- name: GetUser :one +SELECT id, username, display_name, created_at FROM users +WHERE id = sqlc.arg(id); + +-- name: GetUserByUsername :one +SELECT * FROM users +WHERE username = sqlc.arg(username); diff --git a/deploy/nginx.conf b/deploy/nginx.conf new file mode 100644 index 00000000..8c2a5941 --- /dev/null +++ b/deploy/nginx.conf @@ -0,0 +1,33 @@ +events { + worker_connections 4096; +} + +http { + resolver 127.0.0.11 valid=2s ipv6=off; + + server { + listen 8080; + + client_max_body_size 11m; + + location ~ ^/api/v1/chats/[^/]+/messages$ { + set $backend http://api:8080; + proxy_pass $backend; + + proxy_connect_timeout 1s; + proxy_read_timeout 6s; + proxy_next_upstream error timeout http_500 http_502 non_idempotent; + proxy_next_upstream_tries 2; + } + + location / { + set $backend http://api:8080; + proxy_pass $backend; + + proxy_connect_timeout 1s; + proxy_read_timeout 6s; + proxy_next_upstream error timeout http_500 http_502; + proxy_next_upstream_tries 2; + } + } +} diff --git a/evidence/contract-go.txt b/evidence/contract-go.txt new file mode 100644 index 00000000..95916369 --- /dev/null +++ b/evidence/contract-go.txt @@ -0,0 +1,36 @@ +> make contract-go +go test -tags=contract ./student-tests/... -v -args -base-url=http://localhost:8080 +=== RUN TestAttachmentJPEG +--- PASS: TestAttachmentJPEG (0.23s) +=== RUN TestAttachmentTooManyPixels +--- PASS: TestAttachmentTooManyPixels (1.83s) +=== RUN TestAttachmentUnsafeFilename +--- PASS: TestAttachmentUnsafeFilename (0.12s) +=== RUN TestAttachmentTooLarge +--- PASS: TestAttachmentTooLarge (0.22s) +=== RUN TestAttachmentCorruptedImage +--- PASS: TestAttachmentCorruptedImage (0.14s) +=== RUN TestAttachmentMimeSubstitution +--- PASS: TestAttachmentMimeSubstitution (0.12s) +=== RUN TestAttachmentForeignID +--- PASS: TestAttachmentForeignID (0.26s) +=== RUN TestAttachmentFromAnotherChat +--- PASS: TestAttachmentFromAnotherChat (0.41s) +=== RUN TestCreateChatRollback +--- PASS: TestCreateChatRollback (0.16s) +=== RUN TestInvalidJSON +--- PASS: TestInvalidJSON (0.00s) +=== RUN TestForeignChat +--- PASS: TestForeignChat (0.57s) +=== RUN TestEmptyMessage +--- PASS: TestEmptyMessage (0.21s) +=== RUN TestDuplicateUsername +--- PASS: TestDuplicateUsername (0.28s) +=== RUN TestPagination +--- PASS: TestPagination (0.36s) +=== RUN TestLoginWrongPassword +--- PASS: TestLoginWrongPassword (0.34s) +=== RUN TestLogout +--- PASS: TestLogout (0.17s) +PASS +ok mtusi2026-messenger/student-tests 5.442s \ No newline at end of file diff --git a/evidence/failure-go.txt b/evidence/failure-go.txt new file mode 100644 index 00000000..a64fc650 --- /dev/null +++ b/evidence/failure-go.txt @@ -0,0 +1,14 @@ +> make failure-go +go test -tags=failure ./student-tests/... -v -args -base-url=http://localhost:8080 +=== RUN TestAttachmentUploaderFailover +--- PASS: TestAttachmentUploaderFailover (1.53s) +=== RUN TestRedisDown +--- PASS: TestRedisDown (2.81s) +=== RUN TestDatabaseDown +--- PASS: TestDatabaseDown (7.67s) +=== RUN TestS3Down +--- PASS: TestS3Down (47.30s) +=== RUN TestSessionExpiry +--- PASS: TestSessionExpiry (6.05s) +PASS +ok mtusi2026-messenger/student-tests 65.376s \ No newline at end of file diff --git a/evidence/load-go.txt b/evidence/load-go.txt new file mode 100644 index 00000000..8c4004d9 --- /dev/null +++ b/evidence/load-go.txt @@ -0,0 +1,27 @@ +> make load-go LOAD_WORKERS=10 LOAD_DURATION=30s +go test -count=1 -tags=load ./student-tests/... -v -args -base-url=http://localhost:8080 -workers=10 -duration=30s +=== RUN TestConcurrentUploads + attachment_load_test.go:141: workers=10 duration=30.0s elapsed=30.4s + attachment_load_test.go:142: total uploads=632 transport_errors=0 + attachment_load_test.go:144: HTTP 201: 632 + attachment_load_test.go:148: successful RPS: 20.82 + attachment_load_test.go:152: latency p50=449.961ms p95=691.7505ms p99=807.8037ms + attachment_load_test.go:176: memory per container (idle -> peak): + attachment_load_test.go:178: mtusi2026lab4-api-1: 12.2MiB -> 50.9MiB + attachment_load_test.go:178: mtusi2026lab4-api-2: 13.3MiB -> 56.7MiB +--- PASS: TestConcurrentUploads (32.69s) +=== RUN TestLoad + load_test.go:109: duration: 30.000s + load_test.go:110: workers: 10 + load_test.go:111: rate per worker: 20 req/s + load_test.go:112: target rate: 200 req/s + load_test.go:113: total requests: 5990 + load_test.go:116: HTTP 201: 5990 + load_test.go:119: request errors: 0 + load_test.go:123: successful RPS: 199.66 + load_test.go:132: successful latency p50: 8.1287ms + load_test.go:133: successful latency p95: 9.4585ms + load_test.go:134: successful latency p99: 16.4092ms +--- PASS: TestLoad (30.58s) +PASS +ok mtusi2026-messenger/student-tests 63.302s \ No newline at end of file diff --git a/evidence/make-test.txt b/evidence/make-test.txt new file mode 100644 index 00000000..c0cec4bc --- /dev/null +++ b/evidence/make-test.txt @@ -0,0 +1,3 @@ +> make test +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab4 public API scenario; infrastructure and grading evidence remain separate. \ No newline at end of file diff --git a/evidence/reconcile-test-go.txt b/evidence/reconcile-test-go.txt new file mode 100644 index 00000000..dfcb37a4 --- /dev/null +++ b/evidence/reconcile-test-go.txt @@ -0,0 +1,52 @@ +> make reconcile-test-go +docker compose --env-file .env build reconcile +[+] Building 13.8s (18/18) FINISHED + => [internal] load local bake definitions 0.0s + => => reading from stdin 600B 0.0s + => [internal] load build definition from Dockerfile.reconcile 0.0s + => => transferring dockerfile: 342B 0.0s + => [internal] load metadata for gcr.io/distroless/static-debian12:nonroot 1.4s + => [internal] load metadata for docker.io/library/golang:1.27.1 1.3s + => [auth] library/golang:pull token for registry-1.docker.io 0.0s + => [internal] load .dockerignore 0.0s + => => transferring context: 203B 0.0s + => [build 1/6] FROM docker.io/library/golang:1.27.1@sha256:162be5298a40ed317005c8339c6de4d10d3eef336d66dc8e9259b03ab9d3a6d2 0.0s + => [internal] load build context 0.0s + => => transferring context: 11.25kB 0.0s + => [stage-1 1/3] FROM gcr.io/distroless/static-debian12:nonroot@sha256:afa5c872c891853ca7fcf1f12c3edb23f7eeef36189728842dd51042ff57f7ab 0.0s + => CACHED [build 2/6] WORKDIR /src 0.0s + => CACHED [build 3/6] COPY go.mod go.sum ./ 0.0s + => CACHED [build 4/6] RUN go mod download 0.0s + => [build 5/6] COPY . . 0.1s + => [build 6/6] RUN CGO_ENABLED=0 GOOS=linux go build -o /out/reconcile ./cmd/reconcile 11.9s + => CACHED [stage-1 2/3] WORKDIR /app 0.0s + => CACHED [stage-1 3/3] COPY --from=build /out/reconcile . 0.0s + => exporting to image 0.0s + => => exporting layers 0.0s + => => writing image sha256:0a06bc3c0229755a92090161a0913af4d053556c4934087af6539b95c1d9b1cd 0.0s + => => naming to docker.io/library/mtusi-messenger-reconcile:lab4 0.0s + => resolving provenance for metadata file 0.0s +[+] build 1/1 + ✔ Image mtusi-messenger-reconcile:lab4 Built 13.9s +go test -count=1 -tags=reconcile ./student-tests/... -v -args -base-url=http://localhost:8080 +=== RUN TestReconcileRepair + reconcile_test.go:85: dry_run=true objects=14606 rows=14612 list_pages=15 duration_ms=1813 orphan=1 missing=1 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[] + reconcile_test.go:99: dry_run=false objects=14606 rows=14612 list_pages=15 duration_ms=1557 orphan=1 missing=1 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=1 deleted_rows=0 marked_failed=1 errors=[] + reconcile_test.go:109: dry_run=false objects=14605 rows=14612 list_pages=15 duration_ms=1836 orphan=0 missing=0 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[] + reconcile_test.go:116: PASS: dry-run preserved data; orphan deleted; missing marked failed; repeat made no changes +--- PASS: TestReconcileRepair (8.81s) +=== RUN TestReconcileSafety + reconcile_test.go:137: dry_run=true objects=14607 rows=14614 list_pages=15 duration_ms=1630 orphan=0 missing=0 stale=0 skipped_fresh=1 skipped_referenced=1 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[] + reconcile_test.go:137: dry_run=false objects=14607 rows=14614 list_pages=15 duration_ms=1560 orphan=0 missing=0 stale=0 skipped_fresh=1 skipped_referenced=1 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[] + reconcile_test.go:161: PASS: fresh and old referenced uploads preserved, originals unchanged +--- PASS: TestReconcileSafety (5.03s) +=== RUN TestReconcileCost + reconcile_test.go:166: scan 1/3 + reconcile_test.go:167: dry_run=true objects=14607 rows=14614 list_pages=15 duration_ms=1609 orphan=0 missing=0 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[] + reconcile_test.go:166: scan 2/3 + reconcile_test.go:167: dry_run=true objects=14607 rows=14614 list_pages=15 duration_ms=1721 orphan=0 missing=0 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[] + reconcile_test.go:166: scan 3/3 + reconcile_test.go:167: dry_run=true objects=14607 rows=14614 list_pages=15 duration_ms=1587 orphan=0 missing=0 stale=0 skipped_fresh=0 skipped_referenced=0 deleted_objects=0 deleted_rows=0 marked_failed=0 errors=[] +--- PASS: TestReconcileCost (6.92s) +PASS +ok mtusi2026-messenger/student-tests 20.784s \ No newline at end of file diff --git a/evidence/s3-private.txt b/evidence/s3-private.txt new file mode 100644 index 00000000..4008dd42 --- /dev/null +++ b/evidence/s3-private.txt @@ -0,0 +1,7 @@ +> OBJECT_KEY=$(docker compose exec -T postgres psql -U messenger -d messenger -Atc "SELECT s3_key FROM attachments WHERE status = 'ready' ORDER BY created_at DESC LIMIT 1;") +> echo "$OBJECT_KEY" +chats/01a1a6df-35b7-786f-aa90-f7155218d5b0/attachments/01a1a6df-35ca-7a29-9fa7-fb30a52fbed0 +> docker compose exec -T s3 wget -S -O /dev/null "http://127.0.0.1:9000/messenger-attachments/${OBJECT_KEY}" +Connecting to 127.0.0.1:9000 (127.0.0.1:9000) + HTTP/1.1 403 Forbidden +wget: server returned error: HTTP/1.1 403 Forbidden \ No newline at end of file diff --git a/evidence/test-failover.txt b/evidence/test-failover.txt new file mode 100644 index 00000000..8a1835e8 --- /dev/null +++ b/evidence/test-failover.txt @@ -0,0 +1,5 @@ +> make test-failover ACTION_SCRIPT=scripts/stop-one.sh +python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080" +Executing explicit failover hook; hook output suppressed to avoid leaking secrets. +{"observed_instances_before": 2, "successful_writes": 122, "failed_attempts": 0, "first_recovery_seconds": 2.441} +PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence. \ No newline at end of file diff --git a/evidence/test-persistence.txt b/evidence/test-persistence.txt new file mode 100644 index 00000000..284f1cc1 --- /dev/null +++ b/evidence/test-persistence.txt @@ -0,0 +1,24 @@ +> docker compose ps -a +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab4-api-1 mtusi-messenger:lab4 "/app/server" api 2 hours ago Up 2 hours (healthy) 8080/tcp +mtusi2026lab4-api-2 mtusi-messenger:lab4 "/app/server" api 2 hours ago Up 21 seconds (healthy) 8080/tcp +mtusi2026lab4-load_balancer-1 nginx:1.31.7-alpine3.24 "/docker-entrypoint.…" load_balancer 2 hours ago Up 2 hours 127.0.0.1:8080->8080/tcp +mtusi2026lab4-migrate-1 mtusi2026lab4-migrate "sh -c 'goose -dir /…" migrate 26 hours ago Exited (0) 2 hours ago +mtusi2026lab4-postgres-1 postgres:18.6 "docker-entrypoint.s…" postgres 26 hours ago Up 2 hours (healthy) 127.0.0.1:5432->5432/tcp +mtusi2026lab4-redis-1 redis:8.8.3-alpine3.23 "docker-entrypoint.s…" redis 26 hours ago Up 2 hours (healthy) 6379/tcp +mtusi2026lab4-s3-1 chrislusf/seaweedfs:4.51 "/entrypoint.sh serv…" s3 26 hours ago Up 2 hours (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp +mtusi2026lab4-s3-config-init-1 mtusi-messenger-s3config:lab4 "/usr/local/bin/s3co…" s3-config-init 2 hours ago Exited (0) 2 hours ago +> make test-persistence ACTION_SCRIPT=scripts/restart.sh +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +> docker compose ps -a +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab4-api-1 mtusi-messenger:lab4 "/app/server" api 31 seconds ago Up 9 seconds (healthy) 8080/tcp +mtusi2026lab4-api-2 mtusi-messenger:lab4 "/app/server" api 31 seconds ago Up 9 seconds (healthy) 8080/tcp +mtusi2026lab4-load_balancer-1 nginx:1.31.7-alpine3.24 "/docker-entrypoint.…" load_balancer 31 seconds ago Up 3 seconds 127.0.0.1:8080->8080/tcp +mtusi2026lab4-migrate-1 mtusi2026lab4-migrate "sh -c 'goose -dir /…" migrate 31 seconds ago Exited (0) 24 seconds ago +mtusi2026lab4-postgres-1 postgres:18.6 "docker-entrypoint.s…" postgres 31 seconds ago Up 30 seconds (healthy) 127.0.0.1:5432->5432/tcp +mtusi2026lab4-redis-1 redis:8.8.3-alpine3.23 "docker-entrypoint.s…" redis 31 seconds ago Up 29 seconds (healthy) 6379/tcp +mtusi2026lab4-s3-1 chrislusf/seaweedfs:4.51 "/entrypoint.sh serv…" s3 31 seconds ago Up 29 seconds (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp +mtusi2026lab4-s3-config-init-1 mtusi-messenger-s3config:lab4 "/usr/local/bin/s3co…" s3-config-init 31 seconds ago Exited (0) 30 seconds ago \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..dbbd6734 --- /dev/null +++ b/go.mod @@ -0,0 +1,41 @@ +module mtusi2026-messenger + +go 1.26.0 + +require ( + github.com/aws/aws-sdk-go-v2 v1.47.5 + github.com/aws/aws-sdk-go-v2/config v1.33.13 + github.com/aws/aws-sdk-go-v2/credentials v1.20.13 + github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6 + github.com/aws/smithy-go v1.28.7 + github.com/go-chi/chi/v5 v5.5.0 + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.11.0 + github.com/joho/godotenv v1.5.1 + github.com/redis/go-redis/v9 v9.23.0 + golang.org/x/crypto v0.58.0 +) + +require ( + github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5 // indirect + github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8 // indirect + github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8 // indirect + github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8 // indirect + github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8 // indirect + github.com/aws/aws-sdk-go-v2/service/signin v1.12.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sso v1.39.1 // indirect + github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1 // indirect + github.com/aws/aws-sdk-go-v2/service/sts v1.51.6 // indirect + github.com/cespare/xxhash/v2 v2.3.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 + go.uber.org/atomic v1.12.0 // indirect + golang.org/x/sync v0.24.0 // indirect + golang.org/x/sys v0.49.0 // indirect + golang.org/x/text v0.43.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..4a47d012 --- /dev/null +++ b/go.sum @@ -0,0 +1,83 @@ +github.com/aws/aws-sdk-go-v2 v1.47.5 h1:5yA+A+BiXbDq1ZLbYwPViwhEGDMdR0MEhL8Jh2LnLS4= +github.com/aws/aws-sdk-go-v2 v1.47.5/go.mod h1:iNIkogrRvFwm+1OQIUOP2lPBtlXZboOIUWiCa+pHEMw= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25 h1:MW0Iql1SOTgQPj8mCxrU/eH/K/AIDWi08ItOsXaWRLU= +github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25/go.mod h1:+ucVYmHYB3JqVueY0E1RHahdXAnKQbIHqybSbvOUbOM= +github.com/aws/aws-sdk-go-v2/config v1.33.13 h1:u52x6ISQOx172tpwM0VRxhJzEsTJYpMcEYUGM2A1wrw= +github.com/aws/aws-sdk-go-v2/config v1.33.13/go.mod h1:/ixtz6XPHb42zekjQ0LqD9XjTpTysNWhIGwufY1OzI4= +github.com/aws/aws-sdk-go-v2/credentials v1.20.13 h1:j4z41OtspmQ3ftQycaDunJ26b69a9SHfaSVUPqgRv8k= +github.com/aws/aws-sdk-go-v2/credentials v1.20.13/go.mod h1:SgPA1oS3b+dlMxZb618jDmGNscpXSCaxz15igS52OlE= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5 h1:iXjy2SFhGYNB1jWZBhQqq8HbhsbPF23w+/zbMvPXsTg= +github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5/go.mod h1:k0RUiBnyXU0AXRJ9tJmY2jQke6XUIX2PNVnWtvgaPtc= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8 h1:gzIWc3DTPfPbWKrQnmL6BV6WD7NPD1htxzgdk/bQ/Z4= +github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8/go.mod h1:vvUA5cDAZWGo4R14FZPZfXfJCmgsafOWEq78Kg0D+ug= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8 h1:PWv+vG8aCLS8qd+HPdYhK7Vees90aKHWFFQ1AGYD/8E= +github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8/go.mod h1:xxPZEpflYHJA12366ZGyq66z9dHPCoeCIu99Jck+MPw= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8 h1:WOp5De9iWdEHDPh2Kb0tBP3vGBm/dVAuRGh4a4tqduo= +github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8/go.mod h1:z8W8YgfhbWl62XqcTtK+CxPrE+iHGRVUOtrrbIOp7ZY= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23 h1:MCIR6iZaCKO4UE0hUvoWWLI6JVjppopV64JfAJaMjng= +github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23/go.mod h1:pphWFS+z0MmIPivj8c/6bATd1Z+Av7Ust75DPdEFsPA= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9 h1:GACgECI422Xt5n4Pojr3O0aocPv8lfH4PJe2Aon5Wv8= +github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9/go.mod h1:lPfnRkHpn1qWZJLbVEw10rIqSuCee1pNQTyGkUjAYh8= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8 h1:+PMpWlJYOn2tm7sm6OZ/ZLd4jStl17T7wjNapd1Rczs= +github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8/go.mod h1:cnG3xZLGQL4c/bt8pjdzlFtZsW7epBBuNS04a5xMjdo= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8 h1:RS1Wt+K0r4X0ci9Vf4JYTUfHzsQihYA2LgGJeEneKpk= +github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8/go.mod h1:adMnTy5kyoUW86jhXnGSVc/mvMQI4GZd1qYWMeaS8M8= +github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6 h1:to7+Vr3rLkpYRwVKJ5QRriI0eE306vrVN/6TnVHgxvY= +github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6/go.mod h1:Jc89ASa2k7HMAskj3bSnt8bf+k+p2IApzkoXnEjkgUw= +github.com/aws/aws-sdk-go-v2/service/signin v1.12.1 h1:WZLC+k/CZx11kZGQDqu9mpA8yhiXGX2I//jhuJE1FDo= +github.com/aws/aws-sdk-go-v2/service/signin v1.12.1/go.mod h1:wxAk0D2oVBSjuXEIzvcKxTZfM90zticdAbh/VD42L0Y= +github.com/aws/aws-sdk-go-v2/service/sso v1.39.1 h1:4VB/dnDo7pmXSLQMszSoBzWndENuc7mzwTpD6lQn77A= +github.com/aws/aws-sdk-go-v2/service/sso v1.39.1/go.mod h1:UGsEYgWz4Gi+46vDQ5zkFLqUgJziyOE2SXjZjgmfmHc= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1 h1:w8yui2ZqdHmGwVBDGvazoIvKw/DnLENBLkYnN8q8iqo= +github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1/go.mod h1:gJSdIY6YUzDvqQLcMtPVZgBRHrFwzHryvpE8Igzbxps= +github.com/aws/aws-sdk-go-v2/service/sts v1.51.6 h1:BVjKrtRUtSYKztBCCfE1oAB+d3ViKcW2cKZfserOqMQ= +github.com/aws/aws-sdk-go-v2/service/sts v1.51.6/go.mod h1:eSukFLeLkPMm+1OxDR+F0joauZdyBkF47Ow7MqjZiJw= +github.com/aws/smithy-go v1.28.7 h1:h/n1SHrwVtaDYLz8mR1/Kq4Xh4RaFbFW7bOYckg9mfE= +github.com/aws/smithy-go v1.28.7/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc= +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-chi/chi/v5 v5.5.0 h1:xdKQ1dJmVJsmgahrXSKhUx8NyVPzpwdlyrrQFpMPJgg= +github.com/go-chi/chi/v5 v5.5.0/go.mod h1:d1IG7GtKPSjPyBP365PH8sAGVHk3qfB/IhjFNRH1wWM= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-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/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= +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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +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.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE= +go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk= +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/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= +golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= +golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= +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= diff --git a/internal/app/app.go b/internal/app/app.go new file mode 100644 index 00000000..80a3c9f2 --- /dev/null +++ b/internal/app/app.go @@ -0,0 +1,165 @@ +package app + +import ( + "context" + "net/http" + "os" + "os/signal" + "sync" + "syscall" + "time" + + "github.com/joho/godotenv" + + goredis "github.com/redis/go-redis/v9" + + "mtusi2026-messenger/internal/config" + "mtusi2026-messenger/internal/db" + "mtusi2026-messenger/internal/logger" + "mtusi2026-messenger/internal/postgres" + "mtusi2026-messenger/internal/redis" + s3store "mtusi2026-messenger/internal/s3" + "mtusi2026-messenger/internal/service" +) + +func Run() error { + if err := godotenv.Load(); err != nil && !os.IsNotExist(err) { + return err + } + + cfg := config.Load() + + log := logger.New(cfg.Logging.Level).With("instance_id", cfg.Server.InstanceID) + + dbURL, err := cfg.Database.URL() + if err != nil { + log.Error("read database url", "error", err) + return err + } + + pool, err := postgres.New(context.Background(), dbURL) + if err != nil { + log.Error("connect to database", "error", err) + return err + } + defer pool.Close() + + queries := db.New(pool) + + var rdb *goredis.Client + if cfg.UseCache() { + cacheURL, err := cfg.Redis.URL() + if err != nil { + log.Error("read redis url", "error", err) + return err + } + + rdb, err = redis.New(context.Background(), log, cacheURL) + if err != nil { + log.Error("create redis client", "error", err) + return err + } + defer rdb.Close() + } + + log.Info("session store configured", "store", cfg.SessionStore) + + s3Client, err := s3store.New(context.Background(), cfg.S3, log) + if err != nil { + log.Error("create s3 client", "error", err) + return err + } + + ensureCtx, cancelEnsure := context.WithTimeout(context.Background(), 10*time.Second) + err = s3Client.EnsureBucket(ensureCtx) + cancelEnsure() + if err != nil { + log.Error("ensure s3 bucket", "error", err) + return err + } + + log.Info("s3 storage ready", "endpoint", cfg.S3.Endpoint, "bucket", s3Client.Bucket()) + + cleanupCtx, stopCleanup := context.WithCancel(context.Background()) + var cleanupWG sync.WaitGroup + cleanupWG.Go(func() { + runSessionCleanup(cleanupCtx, log, queries, sessionCleanupInterval) + }) + cleanupWG.Go(func() { + runIdempotencyCleanup(cleanupCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval) + }) + cleanupWG.Go(func() { + runAttachmentCleanup( + cleanupCtx, + log, + queries, + s3Client, + attachmentOrphanGracePeriod, + attachmentCleanupInterval, + ) + }) + defer func() { + stopCleanup() + cleanupWG.Wait() + }() + + authService := service.NewAuthService(queries, rdb, cfg.Server.SessionTTL, log) + userService := service.NewUserService(queries) + chatService := service.NewChatService(pool, queries) + messageService := service.NewMessageService(pool, queries) + attachmentService := service.NewAttachmentService(queries, s3Client, cfg.S3.MaxUploadBytes, cfg.S3.MaxImagePixels) + + router := NewRouter( + log, + cfg.Server.InstanceID, + cfg.Server.MaxInflightRequests, + cfg.S3.MaxUploadBytes, + pool, + s3Client, + authService, + userService, + chatService, + messageService, + attachmentService, + ) + + server := &http.Server{ + Addr: cfg.Server.Host + ":" + cfg.Server.Port, + Handler: router, + } + + serverErrors := make(chan error, 1) + + go func() { + log.Info("server starting", "addr", server.Addr) + serverErrors <- server.ListenAndServe() + }() + + signalCh := make(chan os.Signal, 1) + signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM) + defer signal.Stop(signalCh) + + select { + case err := <-serverErrors: + if err == http.ErrServerClosed { + return nil + } + + log.Error("server failed", "error", err) + return err + + case sig := <-signalCh: + log.Info("shutdown signal received", "signal", sig.String()) + + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) + defer cancel() + + if err := server.Shutdown(ctx); err != nil { + log.Error("server shutdown failed", "error", err) + return err + } + + log.Info("server stopped") + return nil + } +} diff --git a/internal/app/cleanup.go b/internal/app/cleanup.go new file mode 100644 index 00000000..c27528de --- /dev/null +++ b/internal/app/cleanup.go @@ -0,0 +1,149 @@ +package app + +import ( + "context" + "log/slog" + "time" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/db" +) + +const ( + sessionCleanupInterval = time.Hour + idempotencyCleanupInterval = time.Hour + + attachmentCleanupInterval = 10 * time.Minute + attachmentOrphanGracePeriod = time.Hour +) + +type sessionCleaner interface { + DeleteExpiredSessions(ctx context.Context) (int64, error) +} + +type idempotencyCleaner interface { + DeleteExpiredIdempotencyKeys(ctx context.Context, cutoff time.Time) (int64, error) +} + +type staleUploadLister interface { + ListStaleAttachmentUploads(ctx context.Context, cutoff time.Time) ([]db.ListStaleAttachmentUploadsRow, error) + DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int64, error) +} + +type objectDeleter interface { + DeleteAdmin(ctx context.Context, key string) error +} + +func runSessionCleanup(ctx context.Context, log *slog.Logger, q sessionCleaner, interval time.Duration) { + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + n, err := q.DeleteExpiredSessions(ctx) + if err != nil { + if ctx.Err() == nil { + log.Error("delete expired sessions", "error", err) + } + continue + } + if n > 0 { + log.Info("expired sessions deleted", "count", n) + } + } + } +} + +func runIdempotencyCleanup( + ctx context.Context, + log *slog.Logger, + q idempotencyCleaner, + ttl time.Duration, + interval time.Duration, +) { + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + n, err := q.DeleteExpiredIdempotencyKeys(ctx, time.Now().Add(-ttl)) + if err != nil { + if ctx.Err() == nil { + log.Error("delete expired idempotency keys", "error", err) + } + continue + } + if n > 0 { + log.Info("expired idempotency keys deleted", "count", n) + } + } + } +} + +func runAttachmentCleanup( + ctx context.Context, + log *slog.Logger, + q staleUploadLister, + store objectDeleter, + grace time.Duration, + interval time.Duration, +) { + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + cleanupStaleUploads(ctx, log, q, store, grace) + } + } +} + +func cleanupStaleUploads( + ctx context.Context, + log *slog.Logger, + q staleUploadLister, + store objectDeleter, + grace time.Duration, +) { + cutoff := time.Now().Add(-grace) + + rows, err := q.ListStaleAttachmentUploads(ctx, cutoff) + if err != nil { + if ctx.Err() == nil { + log.Error("list stale attachment uploads", "error", err) + } + return + } + + deleted := 0 + + for _, row := range rows { + removed, err := q.DeleteQueuedAttachment(ctx, row.ID) + if err != nil { + log.Error("delete stale upload row", "id", row.ID, "error", err) + continue + } + if removed == 0 { + continue + } + if err := store.DeleteAdmin(ctx, row.S3Key); err != nil { + log.Error("delete stale upload object", "key", row.S3Key, "error", err) + continue + } + deleted++ + } + + if deleted > 0 { + log.Info("stale attachment uploads deleted", "count", deleted) + } +} diff --git a/internal/app/healthcheck.go b/internal/app/healthcheck.go new file mode 100644 index 00000000..6cab9274 --- /dev/null +++ b/internal/app/healthcheck.go @@ -0,0 +1,46 @@ +package app + +import ( + "context" + "fmt" + "net/http" + "os" + "time" + + "mtusi2026-messenger/internal/config" + + "github.com/joho/godotenv" +) + +func Healthcheck() int { + if err := godotenv.Load(); err != nil && !os.IsNotExist(err) { + fmt.Fprintln(os.Stderr, "healthcheck: load env:", err) + return 1 + } + + cfg := config.Load() + url := fmt.Sprintf("http://localhost:%s/health/ready", cfg.Server.Port) + + ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) + defer cancel() + + req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil) + if err != nil { + fmt.Fprintln(os.Stderr, "healthcheck: build request:", err) + return 1 + } + + resp, err := http.DefaultClient.Do(req) + if err != nil { + fmt.Fprintln(os.Stderr, "healthcheck: request failed:", err) + return 1 + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + fmt.Fprintln(os.Stderr, "healthcheck: unexpected status", resp.StatusCode) + return 1 + } + + return 0 +} diff --git a/internal/app/router.go b/internal/app/router.go new file mode 100644 index 00000000..86bed0e6 --- /dev/null +++ b/internal/app/router.go @@ -0,0 +1,72 @@ +package app + +import ( + "log/slog" + "net/http" + "time" + + "github.com/go-chi/chi/v5" + + "mtusi2026-messenger/internal/handler" + "mtusi2026-messenger/internal/middleware" + "mtusi2026-messenger/internal/service" +) + +func NewRouter( + log *slog.Logger, + instanceID string, + maxInflightRequests int, + maxUploadBytes int, + db handler.Pinger, + storage handler.Pinger, + authService *service.AuthService, + userService *service.UserService, + chatService *service.ChatService, + messageService *service.MessageService, + attachmentService *service.AttachmentService, +) http.Handler { + router := chi.NewRouter() + + healthHandler := handler.NewHealthHandler(log, db, storage) + authHandler := handler.NewAuthHandler(authService) + userHandler := handler.NewUserHandler(userService) + chatHandler := handler.NewChatHandler(chatService) + messageHandler := handler.NewMessageHandler(messageService) + attachmentHandler := handler.NewAttachmentHandler(attachmentService, maxUploadBytes) + + router.Use(middleware.InstanceID(instanceID)) + router.Use(middleware.RequestTimeout(5 * time.Second)) + router.Use(middleware.RequestID) + router.Use(middleware.Logging(log)) + router.Use(middleware.ConcurrencyLimit(maxInflightRequests)) + + router.Get("/health/live", healthHandler.Live) + router.Get("/health/ready", healthHandler.Ready) + + router.Post("/api/v1/users", userHandler.Create) + + router.Post("/api/v1/auth/sessions", authHandler.CreateSession) + router.Delete("/api/v1/auth/sessions/current", authHandler.RevokeSession) + + router.Group(func(r chi.Router) { + r.Use(middleware.Auth(authService)) + + r.Get("/api/v1/users/me", userHandler.Me) + + r.Post("/api/v1/chats", chatHandler.Create) + r.Get("/api/v1/chats", chatHandler.List) + r.Get("/api/v1/chats/{chat_id}", chatHandler.Get) + + r.Post("/api/v1/chats/{chat_id}/messages", messageHandler.Create) + r.Get("/api/v1/chats/{chat_id}/messages", messageHandler.List) + + r.Post("/api/v1/chats/{chat_id}/attachments", attachmentHandler.Upload) + r.Get("/api/v1/attachments/{attachment_id}", attachmentHandler.Get) + r.Get("/api/v1/attachments/{attachment_id}/content", attachmentHandler.GetContent) + }) + + router.Handle("/swagger/*", http.StripPrefix("/swagger/", http.FileServer(http.Dir("web/swagger")))) + router.Handle("/contracts/*", http.StripPrefix("/contracts/", http.FileServer(http.Dir("contracts")))) + + return router +} diff --git a/internal/config/config.go b/internal/config/config.go new file mode 100644 index 00000000..9271fe13 --- /dev/null +++ b/internal/config/config.go @@ -0,0 +1,230 @@ +package config + +import ( + "fmt" + "log" + "log/slog" + "os" + "strconv" + "strings" + "time" +) + +const ( + SessionStorePostgres = "postgres" + SessionStoreRedis = "redis" +) + +type Config struct { + Server ServerConfig + Logging LoggingConfig + Database DatabaseConfig + Redis RedisConfig + S3 S3Config + SessionStore string +} + +func (c Config) UseCache() bool { + return c.SessionStore == SessionStoreRedis +} + +type ServerConfig struct { + Host string + Port string + MaxInflightRequests int + SessionTTL time.Duration + IdempotencyTTL time.Duration + InstanceID string +} + +type LoggingConfig struct { + Level slog.Level +} + +type DatabaseConfig struct { + URLFile string +} + +func (c DatabaseConfig) URL() (string, error) { + return readSecretFile(c.URLFile) +} + +type RedisConfig struct { + URLFile string +} + +func (c RedisConfig) URL() (string, error) { + return readSecretFile(c.URLFile) +} + +type S3Config struct { + Endpoint string + Region string + Bucket string + APIAccessKeyFile string + APISecretKeyFile string + BootstrapAccessKeyFile string + BootstrapSecretKeyFile string + MaxUploadBytes int + MaxImagePixels int +} + +func (c S3Config) APIAccessKey() (string, error) { + return readSecretFile(c.APIAccessKeyFile) +} + +func (c S3Config) APISecretKey() (string, error) { + return readSecretFile(c.APISecretKeyFile) +} + +func (c S3Config) BootstrapAccessKey() (string, error) { + return readSecretFile(c.BootstrapAccessKeyFile) +} + +func (c S3Config) BootstrapSecretKey() (string, error) { + return readSecretFile(c.BootstrapSecretKeyFile) +} + +func Load() Config { + return Config{ + Server: ServerConfig{ + Host: getEnv("APP_HOST", "0.0.0.0"), + Port: getEnv("APP_PORT", "8080"), + MaxInflightRequests: getEnvNonNegativeInt("MAX_INFLIGHT_REQUESTS", 0), + SessionTTL: time.Duration(getEnvPositiveInt("SESSION_TTL_SECONDS", 3600)) * time.Second, + IdempotencyTTL: time.Duration(getEnvPositiveInt("IDEMPOTENCY_TTL_SECONDS", 86400)) * time.Second, + InstanceID: getInstanceID("INSTANCE_ID"), + }, + + Logging: LoggingConfig{ + Level: getEnvLogLevel("LOG_LEVEL", slog.LevelInfo), + }, + + Database: DatabaseConfig{ + URLFile: getEnv("DATABASE_URL_FILE", "/run/secrets/database_url"), + }, + + Redis: RedisConfig{ + URLFile: getEnv("CACHE_URL_FILE", "/run/secrets/cache_url"), + }, + + S3: LoadS3(), + + SessionStore: getEnvSessionStore("SESSION_STORE", SessionStorePostgres), + } +} + +func LoadS3() S3Config { + return S3Config{ + Endpoint: getEnv("S3_ENDPOINT", "http://s3:9000"), + Region: getEnv("S3_REGION", "us-east-1"), + Bucket: getEnv("S3_BUCKET", "messenger-attachments"), + APIAccessKeyFile: getEnv("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id"), + APISecretKeyFile: getEnv("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key"), + BootstrapAccessKeyFile: getEnv("S3_BOOTSTRAP_ACCESS_KEY_ID_FILE", "/run/secrets/s3_bootstrap_access_key_id"), + BootstrapSecretKeyFile: getEnv("S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_bootstrap_secret_access_key"), + MaxUploadBytes: getEnvPositiveInt("MAX_UPLOAD_BYTES", 10485760), + MaxImagePixels: getEnvPositiveInt("MAX_IMAGE_PIXELS", 40000000), + } +} + +func getEnv(key, def string) string { + value := os.Getenv(key) + if value == "" { + return def + } + + return value +} + +func getEnvNonNegativeInt(key string, def int) int { + v := os.Getenv(key) + if v == "" { + return def + } + + value, err := strconv.Atoi(v) + if err != nil || value < 0 { + log.Printf("invalid non-negative int for %s=%q, using default %d", key, v, def) + return def + } + + return value +} + +func getEnvPositiveInt(key string, def int) int { + v := os.Getenv(key) + if v == "" { + return def + } + + value, err := strconv.Atoi(v) + if err != nil || value <= 0 { + log.Printf("invalid positive int for %s=%q, using default %d", key, v, def) + return def + } + + return value +} + +func getEnvLogLevel(key string, def slog.Level) slog.Level { + v := os.Getenv(key) + + switch v { + case "DEBUG": + return slog.LevelDebug + case "INFO": + return slog.LevelInfo + case "WARN": + return slog.LevelWarn + case "ERROR": + return slog.LevelError + case "": + return def + default: + log.Printf("invalid log level for %s=%q, using default %s", key, v, def) + return def + } +} + +func readSecretFile(path string) (string, error) { + b, err := os.ReadFile(path) + if err != nil { + return "", fmt.Errorf("read secret file %s: %w", path, err) + } + + value := strings.TrimSpace(string(b)) + if value == "" { + return "", fmt.Errorf("secret file %s is empty", path) + } + + return value, nil +} + +func getEnvSessionStore(key, def string) string { + v := strings.ToLower(os.Getenv(key)) + + switch v { + case "": + return def + case SessionStorePostgres, SessionStoreRedis: + return v + default: + log.Printf("invalid session store for %s=%q, using default %s", key, v, def) + return def + } +} + +func getInstanceID(key string) string { + if v := os.Getenv(key); v != "" { + return v + } + + host, err := os.Hostname() + if err != nil || host == "" { + log.Printf("cannot determine hostname, using \"unknown\" for %s: %v", key, err) + return "unknown" + } + + return host +} diff --git a/internal/credentials/credentials.go b/internal/credentials/credentials.go new file mode 100644 index 00000000..7120a961 --- /dev/null +++ b/internal/credentials/credentials.go @@ -0,0 +1,51 @@ +package credentials + +import ( + "crypto/rand" + "crypto/sha256" + "encoding/base64" + "encoding/hex" + "errors" + + "golang.org/x/crypto/bcrypt" +) + +const tokenSize = 32 + +var ErrPasswordMismatch = errors.New("password mismatch") + +func prehash(plain string) []byte { + sum := sha256.Sum256([]byte(plain)) + return []byte(base64.StdEncoding.EncodeToString(sum[:])) +} + +func HashPassword(plain string) (string, error) { + hash, err := bcrypt.GenerateFromPassword(prehash(plain), bcrypt.DefaultCost) + if err != nil { + return "", err + } + + return string(hash), nil +} + +func VerifyPassword(hash, plain string) error { + err := bcrypt.CompareHashAndPassword([]byte(hash), prehash(plain)) + if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) { + return ErrPasswordMismatch + } + + return err +} + +func NewToken() string { + raw := make([]byte, tokenSize) + _, _ = rand.Read(raw) // never returns an error since Go 1.24 + + return base64.RawURLEncoding.EncodeToString(raw) +} + +func HashToken(token string) string { + sum := sha256.Sum256([]byte(token)) + + return hex.EncodeToString(sum[:]) +} diff --git a/internal/db/attachments.sql.go b/internal/db/attachments.sql.go new file mode 100644 index 00000000..8103dd3a --- /dev/null +++ b/internal/db/attachments.sql.go @@ -0,0 +1,365 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: attachments.sql + +package db + +import ( + "context" + "time" + + "github.com/google/uuid" +) + +const createAttachment = `-- name: CreateAttachment :one +INSERT INTO attachments ( + id, + chat_id, + owner_id, + s3_key, + filename, + content_type, + size_bytes, + sha256, + error_code +) +SELECT + $1::uuid, + $2::uuid, + $3::uuid, + $4::text, + $5::text, + $6::attachment_content_type, + $7::int, + $8::text, + NULLIF($9::text, '') +WHERE EXISTS ( + SELECT 1 FROM chat_members + WHERE chat_id = $2 + AND user_id = $3 +) +RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at +` + +type CreateAttachmentParams struct { + ID uuid.UUID + ChatID uuid.UUID + OwnerID uuid.UUID + S3Key string + Filename string + ContentType AttachmentContentType + SizeBytes int + Sha256 string + ErrorCode string +} + +func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (Attachment, error) { + row := q.db.QueryRow(ctx, createAttachment, + arg.ID, + arg.ChatID, + arg.OwnerID, + arg.S3Key, + arg.Filename, + arg.ContentType, + arg.SizeBytes, + arg.Sha256, + arg.ErrorCode, + ) + var i Attachment + err := row.Scan( + &i.ID, + &i.ChatID, + &i.OwnerID, + &i.S3Key, + &i.Filename, + &i.ContentType, + &i.SizeBytes, + &i.Sha256, + &i.Status, + &i.ErrorCode, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} + +const createMessageAttachment = `-- name: CreateMessageAttachment :exec +INSERT INTO message_attachments ( + message_id, + attachment_id, + position +) VALUES ( + $1, + $2, + $3 +) +` + +type CreateMessageAttachmentParams struct { + MessageID uuid.UUID + AttachmentID uuid.UUID + Position int +} + +func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessageAttachmentParams) error { + _, err := q.db.Exec(ctx, createMessageAttachment, arg.MessageID, arg.AttachmentID, arg.Position) + return err +} + +const deleteQueuedAttachment = `-- name: DeleteQueuedAttachment :execrows +DELETE FROM attachments +WHERE id = $1::uuid AND status = 'queued' +` + +func (q *Queries) DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int64, error) { + result, err := q.db.Exec(ctx, deleteQueuedAttachment, id) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + +const getAttachmentByID = `-- name: GetAttachmentByID :one +SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at FROM attachments +WHERE id = $1::uuid +` + +func (q *Queries) GetAttachmentByID(ctx context.Context, dollar_1 uuid.UUID) (Attachment, error) { + row := q.db.QueryRow(ctx, getAttachmentByID, dollar_1) + var i Attachment + err := row.Scan( + &i.ID, + &i.ChatID, + &i.OwnerID, + &i.S3Key, + &i.Filename, + &i.ContentType, + &i.SizeBytes, + &i.Sha256, + &i.Status, + &i.ErrorCode, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} + +const listAttachmentsByIDs = `-- name: ListAttachmentsByIDs :many +SELECT status +FROM attachments +WHERE chat_id = $1::uuid + AND id = ANY($2::uuid[]) +` + +type ListAttachmentsByIDsParams struct { + ChatID uuid.UUID + Ids []uuid.UUID +} + +func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]AttachmentStatus, error) { + rows, err := q.db.Query(ctx, listAttachmentsByIDs, arg.ChatID, arg.Ids) + if err != nil { + return nil, err + } + defer rows.Close() + items := []AttachmentStatus{} + for rows.Next() { + var status AttachmentStatus + if err := rows.Scan(&status); err != nil { + return nil, err + } + items = append(items, status) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listAttachmentsForReconcile = `-- name: ListAttachmentsForReconcile :many +SELECT + a.id, + a.s3_key, + a.status, + a.size_bytes, + a.created_at, + EXISTS ( + SELECT 1 FROM message_attachments ma + WHERE ma.attachment_id = a.id + ) AS referenced +FROM attachments a +` + +type ListAttachmentsForReconcileRow struct { + ID uuid.UUID + S3Key string + Status AttachmentStatus + SizeBytes int + CreatedAt time.Time + Referenced bool +} + +func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttachmentsForReconcileRow, error) { + rows, err := q.db.Query(ctx, listAttachmentsForReconcile) + if err != nil { + return nil, err + } + defer rows.Close() + items := []ListAttachmentsForReconcileRow{} + for rows.Next() { + var i ListAttachmentsForReconcileRow + if err := rows.Scan( + &i.ID, + &i.S3Key, + &i.Status, + &i.SizeBytes, + &i.CreatedAt, + &i.Referenced, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listMessageAttachmentIDs = `-- name: ListMessageAttachmentIDs :many +SELECT attachment_id +FROM message_attachments +WHERE message_id = $1::uuid +ORDER BY position +` + +func (q *Queries) ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error) { + rows, err := q.db.Query(ctx, listMessageAttachmentIDs, messageID) + if err != nil { + return nil, err + } + defer rows.Close() + items := []uuid.UUID{} + for rows.Next() { + var attachment_id uuid.UUID + if err := rows.Scan(&attachment_id); err != nil { + return nil, err + } + items = append(items, attachment_id) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listMessageAttachmentsByMessageIDs = `-- name: ListMessageAttachmentsByMessageIDs :many +SELECT message_id, attachment_id +FROM message_attachments +WHERE message_id = ANY($1::uuid[]) +ORDER BY message_id, position +` + +type ListMessageAttachmentsByMessageIDsRow struct { + MessageID uuid.UUID + AttachmentID uuid.UUID +} + +func (q *Queries) ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIds []uuid.UUID) ([]ListMessageAttachmentsByMessageIDsRow, error) { + rows, err := q.db.Query(ctx, listMessageAttachmentsByMessageIDs, messageIds) + if err != nil { + return nil, err + } + defer rows.Close() + items := []ListMessageAttachmentsByMessageIDsRow{} + for rows.Next() { + var i ListMessageAttachmentsByMessageIDsRow + if err := rows.Scan(&i.MessageID, &i.AttachmentID); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listStaleAttachmentUploads = `-- name: ListStaleAttachmentUploads :many +SELECT id, s3_key FROM attachments +WHERE status = 'queued' AND created_at < $1::timestamptz +` + +type ListStaleAttachmentUploadsRow struct { + ID uuid.UUID + S3Key string +} + +func (q *Queries) ListStaleAttachmentUploads(ctx context.Context, cutoff time.Time) ([]ListStaleAttachmentUploadsRow, error) { + rows, err := q.db.Query(ctx, listStaleAttachmentUploads, cutoff) + if err != nil { + return nil, err + } + defer rows.Close() + items := []ListStaleAttachmentUploadsRow{} + for rows.Next() { + var i ListStaleAttachmentUploadsRow + if err := rows.Scan(&i.ID, &i.S3Key); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const markAttachmentFailedIfReady = `-- name: MarkAttachmentFailedIfReady :execrows +UPDATE attachments +SET status = 'failed', + error_code = $1::text, + updated_at = now() +WHERE id = $2::uuid AND status = 'ready' +` + +type MarkAttachmentFailedIfReadyParams struct { + ErrorCode string + ID uuid.UUID +} + +func (q *Queries) MarkAttachmentFailedIfReady(ctx context.Context, arg MarkAttachmentFailedIfReadyParams) (int64, error) { + result, err := q.db.Exec(ctx, markAttachmentFailedIfReady, arg.ErrorCode, arg.ID) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + +const markAttachmentReady = `-- name: MarkAttachmentReady :one +UPDATE attachments +SET status = 'ready', updated_at = now() +WHERE id = $1::uuid +RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at +` + +func (q *Queries) MarkAttachmentReady(ctx context.Context, dollar_1 uuid.UUID) (Attachment, error) { + row := q.db.QueryRow(ctx, markAttachmentReady, dollar_1) + var i Attachment + err := row.Scan( + &i.ID, + &i.ChatID, + &i.OwnerID, + &i.S3Key, + &i.Filename, + &i.ContentType, + &i.SizeBytes, + &i.Sha256, + &i.Status, + &i.ErrorCode, + &i.CreatedAt, + &i.UpdatedAt, + ) + return i, err +} diff --git a/internal/db/chats.sql.go b/internal/db/chats.sql.go new file mode 100644 index 00000000..c2794cc7 --- /dev/null +++ b/internal/db/chats.sql.go @@ -0,0 +1,232 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: chats.sql + +package db + +import ( + "context" + "time" + + "github.com/google/uuid" +) + +const addChatMembers = `-- name: AddChatMembers :exec +INSERT INTO chat_members ( + chat_id, + user_id +) +SELECT + $1::uuid, + unnest($2::uuid[]) +` + +type AddChatMembersParams struct { + ChatID uuid.UUID + UserIds []uuid.UUID +} + +func (q *Queries) AddChatMembers(ctx context.Context, arg AddChatMembersParams) error { + _, err := q.db.Exec(ctx, addChatMembers, arg.ChatID, arg.UserIds) + return err +} + +const createChat = `-- name: CreateChat :one +INSERT INTO chats ( + id, + title +) VALUES ( + $1, + $2 +) +RETURNING id, title, created_at +` + +type CreateChatParams struct { + ID uuid.UUID + Title string +} + +func (q *Queries) CreateChat(ctx context.Context, arg CreateChatParams) (Chat, error) { + row := q.db.QueryRow(ctx, createChat, arg.ID, arg.Title) + var i Chat + err := row.Scan(&i.ID, &i.Title, &i.CreatedAt) + return i, err +} + +const getChat = `-- name: GetChat :one +SELECT + c.id, c.title, c.created_at, + ARRAY( + SELECT cm.user_id FROM chat_members cm + WHERE cm.chat_id = c.id + ORDER BY cm.user_id + )::uuid[] AS member_ids +FROM chats c +WHERE c.id = $1 + AND EXISTS ( + SELECT 1 FROM chat_members m + WHERE m.chat_id = c.id + AND m.user_id = $2 + ) +` + +type GetChatParams struct { + ID uuid.UUID + UserID uuid.UUID +} + +type GetChatRow struct { + ID uuid.UUID + Title string + CreatedAt time.Time + MemberIds []uuid.UUID +} + +func (q *Queries) GetChat(ctx context.Context, arg GetChatParams) (GetChatRow, error) { + row := q.db.QueryRow(ctx, getChat, arg.ID, arg.UserID) + var i GetChatRow + err := row.Scan( + &i.ID, + &i.Title, + &i.CreatedAt, + &i.MemberIds, + ) + return i, err +} + +const isChatMember = `-- name: IsChatMember :one +SELECT EXISTS ( + SELECT 1 FROM chat_members + WHERE chat_id = $1 + AND user_id = $2 +) +` + +type IsChatMemberParams struct { + ChatID uuid.UUID + UserID uuid.UUID +} + +func (q *Queries) IsChatMember(ctx context.Context, arg IsChatMemberParams) (bool, error) { + row := q.db.QueryRow(ctx, isChatMember, arg.ChatID, arg.UserID) + var exists bool + err := row.Scan(&exists) + return exists, err +} + +const listChatsAfter = `-- name: ListChatsAfter :many +SELECT + c.id, c.title, c.created_at, + ARRAY( + SELECT cm.user_id FROM chat_members cm + WHERE cm.chat_id = c.id + ORDER BY cm.user_id + )::uuid[] AS member_ids +FROM chats c +JOIN chat_members m ON m.chat_id = c.id +WHERE m.user_id = $1 + AND (c.created_at, c.id) > ( + $2::timestamptz, + $3::uuid + ) +ORDER BY c.created_at, c.id +LIMIT $4 +` + +type ListChatsAfterParams struct { + UserID uuid.UUID + AfterCreatedAt time.Time + AfterID uuid.UUID + Lim int +} + +type ListChatsAfterRow struct { + ID uuid.UUID + Title string + CreatedAt time.Time + MemberIds []uuid.UUID +} + +func (q *Queries) ListChatsAfter(ctx context.Context, arg ListChatsAfterParams) ([]ListChatsAfterRow, error) { + rows, err := q.db.Query(ctx, listChatsAfter, + arg.UserID, + arg.AfterCreatedAt, + arg.AfterID, + arg.Lim, + ) + if err != nil { + return nil, err + } + defer rows.Close() + items := []ListChatsAfterRow{} + for rows.Next() { + var i ListChatsAfterRow + if err := rows.Scan( + &i.ID, + &i.Title, + &i.CreatedAt, + &i.MemberIds, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listChatsFirstPage = `-- name: ListChatsFirstPage :many +SELECT + c.id, c.title, c.created_at, + ARRAY( + SELECT cm.user_id FROM chat_members cm + WHERE cm.chat_id = c.id + ORDER BY cm.user_id + )::uuid[] AS member_ids +FROM chats c +JOIN chat_members m ON m.chat_id = c.id +WHERE m.user_id = $1 +ORDER BY c.created_at, c.id +LIMIT $2 +` + +type ListChatsFirstPageParams struct { + UserID uuid.UUID + Lim int +} + +type ListChatsFirstPageRow struct { + ID uuid.UUID + Title string + CreatedAt time.Time + MemberIds []uuid.UUID +} + +func (q *Queries) ListChatsFirstPage(ctx context.Context, arg ListChatsFirstPageParams) ([]ListChatsFirstPageRow, error) { + rows, err := q.db.Query(ctx, listChatsFirstPage, arg.UserID, arg.Lim) + if err != nil { + return nil, err + } + defer rows.Close() + items := []ListChatsFirstPageRow{} + for rows.Next() { + var i ListChatsFirstPageRow + if err := rows.Scan( + &i.ID, + &i.Title, + &i.CreatedAt, + &i.MemberIds, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/db/db.go b/internal/db/db.go new file mode 100644 index 00000000..468d1fa3 --- /dev/null +++ b/internal/db/db.go @@ -0,0 +1,32 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 + +package db + +import ( + "context" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +type DBTX interface { + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row +} + +func New(db DBTX) *Queries { + return &Queries{db: db} +} + +type Queries struct { + db DBTX +} + +func (q *Queries) WithTx(tx pgx.Tx) *Queries { + return &Queries{ + db: tx, + } +} diff --git a/internal/db/idempotency_keys.sql.go b/internal/db/idempotency_keys.sql.go new file mode 100644 index 00000000..5dc4235d --- /dev/null +++ b/internal/db/idempotency_keys.sql.go @@ -0,0 +1,120 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: idempotency_keys.sql + +package db + +import ( + "context" + "time" + + "github.com/google/uuid" +) + +const createIdempotencyKey = `-- name: CreateIdempotencyKey :execrows +INSERT INTO idempotency_keys ( + user_id, + method, + path, + key, + request_hash, + message_id +) VALUES ( + $1, + $2, + $3, + $4, + $5, + $6 +) +ON CONFLICT DO NOTHING +` + +type CreateIdempotencyKeyParams struct { + UserID uuid.UUID + Method string + Path string + Key string + RequestHash string + MessageID uuid.UUID +} + +func (q *Queries) CreateIdempotencyKey(ctx context.Context, arg CreateIdempotencyKeyParams) (int64, error) { + result, err := q.db.Exec(ctx, createIdempotencyKey, + arg.UserID, + arg.Method, + arg.Path, + arg.Key, + arg.RequestHash, + arg.MessageID, + ) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + +const deleteExpiredIdempotencyKeys = `-- name: DeleteExpiredIdempotencyKeys :execrows +DELETE FROM idempotency_keys +WHERE created_at < $1 +` + +func (q *Queries) DeleteExpiredIdempotencyKeys(ctx context.Context, cutoff time.Time) (int64, error) { + result, err := q.db.Exec(ctx, deleteExpiredIdempotencyKeys, cutoff) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + +const getIdempotentMessage = `-- name: GetIdempotentMessage :one +SELECT + k.request_hash, + m.id, + m.chat_id, + m.sender_id, + m.text, + m.created_at +FROM idempotency_keys AS k +JOIN messages AS m ON m.id = k.message_id +WHERE k.user_id = $1 + AND k.method = $2 + AND k.path = $3 + AND k.key = $4 +` + +type GetIdempotentMessageParams struct { + UserID uuid.UUID + Method string + Path string + Key string +} + +type GetIdempotentMessageRow struct { + RequestHash string + ID uuid.UUID + ChatID uuid.UUID + SenderID uuid.UUID + Text string + CreatedAt time.Time +} + +func (q *Queries) GetIdempotentMessage(ctx context.Context, arg GetIdempotentMessageParams) (GetIdempotentMessageRow, error) { + row := q.db.QueryRow(ctx, getIdempotentMessage, + arg.UserID, + arg.Method, + arg.Path, + arg.Key, + ) + var i GetIdempotentMessageRow + err := row.Scan( + &i.RequestHash, + &i.ID, + &i.ChatID, + &i.SenderID, + &i.Text, + &i.CreatedAt, + ) + return i, err +} diff --git a/internal/db/messages.sql.go b/internal/db/messages.sql.go new file mode 100644 index 00000000..cb579fde --- /dev/null +++ b/internal/db/messages.sql.go @@ -0,0 +1,145 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: messages.sql + +package db + +import ( + "context" + "time" + + "github.com/google/uuid" +) + +const createMessage = `-- name: CreateMessage :one +INSERT INTO messages ( + id, + chat_id, + sender_id, + text +) +SELECT + $1::uuid, + $2::uuid, + $3::uuid, + $4::text +WHERE EXISTS ( + SELECT 1 FROM chat_members + WHERE chat_id = $2 + AND user_id = $3 +) +RETURNING id, chat_id, sender_id, text, created_at +` + +type CreateMessageParams struct { + ID uuid.UUID + ChatID uuid.UUID + SenderID uuid.UUID + Text string +} + +func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) { + row := q.db.QueryRow(ctx, createMessage, + arg.ID, + arg.ChatID, + arg.SenderID, + arg.Text, + ) + var i Message + err := row.Scan( + &i.ID, + &i.ChatID, + &i.SenderID, + &i.Text, + &i.CreatedAt, + ) + return i, err +} + +const listMessagesAfter = `-- name: ListMessagesAfter :many +SELECT id, chat_id, sender_id, text, created_at FROM messages +WHERE chat_id = $1 + AND (created_at, id) > ( + $2::timestamptz, + $3::uuid + ) +ORDER BY created_at, id +LIMIT $4 +` + +type ListMessagesAfterParams struct { + ChatID uuid.UUID + AfterCreatedAt time.Time + AfterID uuid.UUID + Lim int +} + +func (q *Queries) ListMessagesAfter(ctx context.Context, arg ListMessagesAfterParams) ([]Message, error) { + rows, err := q.db.Query(ctx, listMessagesAfter, + arg.ChatID, + arg.AfterCreatedAt, + arg.AfterID, + arg.Lim, + ) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Message{} + for rows.Next() { + var i Message + if err := rows.Scan( + &i.ID, + &i.ChatID, + &i.SenderID, + &i.Text, + &i.CreatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} + +const listMessagesFirstPage = `-- name: ListMessagesFirstPage :many +SELECT id, chat_id, sender_id, text, created_at FROM messages +WHERE chat_id = $1 +ORDER BY created_at, id +LIMIT $2 +` + +type ListMessagesFirstPageParams struct { + ChatID uuid.UUID + Lim int +} + +func (q *Queries) ListMessagesFirstPage(ctx context.Context, arg ListMessagesFirstPageParams) ([]Message, error) { + rows, err := q.db.Query(ctx, listMessagesFirstPage, arg.ChatID, arg.Lim) + if err != nil { + return nil, err + } + defer rows.Close() + items := []Message{} + for rows.Next() { + var i Message + if err := rows.Scan( + &i.ID, + &i.ChatID, + &i.SenderID, + &i.Text, + &i.CreatedAt, + ); err != nil { + return nil, err + } + items = append(items, i) + } + if err := rows.Err(); err != nil { + return nil, err + } + return items, nil +} diff --git a/internal/db/models.go b/internal/db/models.go new file mode 100644 index 00000000..5d69de15 --- /dev/null +++ b/internal/db/models.go @@ -0,0 +1,164 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 + +package db + +import ( + "database/sql/driver" + "fmt" + "time" + + "github.com/google/uuid" +) + +type AttachmentContentType string + +const ( + AttachmentContentTypeImagePng AttachmentContentType = "image/png" + AttachmentContentTypeImageJpeg AttachmentContentType = "image/jpeg" +) + +func (e *AttachmentContentType) Scan(src interface{}) error { + switch s := src.(type) { + case []byte: + *e = AttachmentContentType(s) + case string: + *e = AttachmentContentType(s) + default: + return fmt.Errorf("unsupported scan type for AttachmentContentType: %T", src) + } + return nil +} + +type NullAttachmentContentType struct { + AttachmentContentType AttachmentContentType + Valid bool // Valid is true if AttachmentContentType is not NULL +} + +// Scan implements the Scanner interface. +func (ns *NullAttachmentContentType) Scan(value interface{}) error { + if value == nil { + ns.AttachmentContentType, ns.Valid = "", false + return nil + } + ns.Valid = true + return ns.AttachmentContentType.Scan(value) +} + +// Value implements the driver Valuer interface. +func (ns NullAttachmentContentType) Value() (driver.Value, error) { + if !ns.Valid { + return nil, nil + } + return string(ns.AttachmentContentType), nil +} + +type AttachmentStatus string + +const ( + AttachmentStatusQueued AttachmentStatus = "queued" + AttachmentStatusProcessing AttachmentStatus = "processing" + AttachmentStatusReady AttachmentStatus = "ready" + AttachmentStatusFailed AttachmentStatus = "failed" +) + +func (e *AttachmentStatus) Scan(src interface{}) error { + switch s := src.(type) { + case []byte: + *e = AttachmentStatus(s) + case string: + *e = AttachmentStatus(s) + default: + return fmt.Errorf("unsupported scan type for AttachmentStatus: %T", src) + } + return nil +} + +type NullAttachmentStatus struct { + AttachmentStatus AttachmentStatus + Valid bool // Valid is true if AttachmentStatus is not NULL +} + +// Scan implements the Scanner interface. +func (ns *NullAttachmentStatus) Scan(value interface{}) error { + if value == nil { + ns.AttachmentStatus, ns.Valid = "", false + return nil + } + ns.Valid = true + return ns.AttachmentStatus.Scan(value) +} + +// Value implements the driver Valuer interface. +func (ns NullAttachmentStatus) Value() (driver.Value, error) { + if !ns.Valid { + return nil, nil + } + return string(ns.AttachmentStatus), nil +} + +type Attachment struct { + ID uuid.UUID + ChatID uuid.UUID + OwnerID uuid.UUID + S3Key string + Filename string + ContentType AttachmentContentType + SizeBytes int + Sha256 string + Status AttachmentStatus + ErrorCode *string + CreatedAt time.Time + UpdatedAt time.Time +} + +type Chat struct { + ID uuid.UUID + Title string + CreatedAt time.Time +} + +type ChatMember struct { + ChatID uuid.UUID + UserID uuid.UUID +} + +type IdempotencyKey struct { + UserID uuid.UUID + Method string + Path string + Key string + RequestHash string + MessageID uuid.UUID + CreatedAt time.Time +} + +type Message struct { + ID uuid.UUID + ChatID uuid.UUID + SenderID uuid.UUID + Text string + CreatedAt time.Time +} + +type MessageAttachment struct { + MessageID uuid.UUID + AttachmentID uuid.UUID + Position int +} + +type Session struct { + TokenHash string + UserID uuid.UUID + CreatedAt time.Time + ExpiresAt time.Time +} + +type User struct { + ID uuid.UUID + Username string + DisplayName string + PasswordHash string + CreatedAt time.Time +} diff --git a/internal/db/sessions.sql.go b/internal/db/sessions.sql.go new file mode 100644 index 00000000..fa6abddb --- /dev/null +++ b/internal/db/sessions.sql.go @@ -0,0 +1,81 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: sessions.sql + +package db + +import ( + "context" + "time" + + "github.com/google/uuid" +) + +const createSession = `-- name: CreateSession :exec +INSERT INTO sessions ( + token_hash, + user_id, + expires_at +) VALUES ( + $1, + $2, + $3 +) +` + +type CreateSessionParams struct { + TokenHash string + UserID uuid.UUID + ExpiresAt time.Time +} + +func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) error { + _, err := q.db.Exec(ctx, createSession, arg.TokenHash, arg.UserID, arg.ExpiresAt) + return err +} + +const deleteExpiredSessions = `-- name: DeleteExpiredSessions :execrows +DELETE FROM sessions +WHERE expires_at <= now() +` + +func (q *Queries) DeleteExpiredSessions(ctx context.Context) (int64, error) { + result, err := q.db.Exec(ctx, deleteExpiredSessions) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + +const deleteSession = `-- name: DeleteSession :execrows +DELETE FROM sessions +WHERE token_hash = $1 + AND expires_at > now() +` + +func (q *Queries) DeleteSession(ctx context.Context, tokenHash string) (int64, error) { + result, err := q.db.Exec(ctx, deleteSession, tokenHash) + if err != nil { + return 0, err + } + return result.RowsAffected(), nil +} + +const getActiveSession = `-- name: GetActiveSession :one +SELECT user_id, expires_at FROM sessions +WHERE token_hash = $1 + AND expires_at > now() +` + +type GetActiveSessionRow struct { + UserID uuid.UUID + ExpiresAt time.Time +} + +func (q *Queries) GetActiveSession(ctx context.Context, tokenHash string) (GetActiveSessionRow, error) { + row := q.db.QueryRow(ctx, getActiveSession, tokenHash) + var i GetActiveSessionRow + err := row.Scan(&i.UserID, &i.ExpiresAt) + return i, err +} diff --git a/internal/db/users.sql.go b/internal/db/users.sql.go new file mode 100644 index 00000000..ec8a97f9 --- /dev/null +++ b/internal/db/users.sql.go @@ -0,0 +1,101 @@ +// Code generated by sqlc. DO NOT EDIT. +// versions: +// sqlc v1.31.1 +// source: users.sql + +package db + +import ( + "context" + "time" + + "github.com/google/uuid" +) + +const createUser = `-- name: CreateUser :one +INSERT INTO users ( + id, + username, + display_name, + password_hash +) VALUES ( + $1, + $2, + $3, + $4 +) +RETURNING id, username, display_name, created_at +` + +type CreateUserParams struct { + ID uuid.UUID + Username string + DisplayName string + PasswordHash string +} + +type CreateUserRow struct { + ID uuid.UUID + Username string + DisplayName string + CreatedAt time.Time +} + +func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (CreateUserRow, error) { + row := q.db.QueryRow(ctx, createUser, + arg.ID, + arg.Username, + arg.DisplayName, + arg.PasswordHash, + ) + var i CreateUserRow + err := row.Scan( + &i.ID, + &i.Username, + &i.DisplayName, + &i.CreatedAt, + ) + return i, err +} + +const getUser = `-- name: GetUser :one +SELECT id, username, display_name, created_at FROM users +WHERE id = $1 +` + +type GetUserRow struct { + ID uuid.UUID + Username string + DisplayName string + CreatedAt time.Time +} + +func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (GetUserRow, error) { + row := q.db.QueryRow(ctx, getUser, id) + var i GetUserRow + err := row.Scan( + &i.ID, + &i.Username, + &i.DisplayName, + &i.CreatedAt, + ) + return i, err +} + +const getUserByUsername = `-- name: GetUserByUsername :one +SELECT id, username, display_name, password_hash, created_at FROM users +WHERE username = $1 +` + +func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error) { + row := q.db.QueryRow(ctx, getUserByUsername, username) + var i User + err := row.Scan( + &i.ID, + &i.Username, + &i.DisplayName, + &i.PasswordHash, + &i.CreatedAt, + ) + return i, err +} diff --git a/internal/dto/attachment.go b/internal/dto/attachment.go new file mode 100644 index 00000000..996835f8 --- /dev/null +++ b/internal/dto/attachment.go @@ -0,0 +1,79 @@ +package dto + +import ( + "time" + "unicode" + "unicode/utf8" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/model" +) + +type CreateAttachmentRequest struct { + Filename string +} + +type GetAttachmentContentRequest struct { + Variant string +} + +type VariantResponse struct { + Name string `json:"name"` + ContentType string `json:"content_type"` + SizeBytes int `json:"size_bytes"` + Width int `json:"width"` + Height int `json:"height"` + Sha256 string `json:"sha256"` +} + +type AttachmentResponse struct { + ID uuid.UUID `json:"id"` + ChatID uuid.UUID `json:"chat_id"` + OwnerID uuid.UUID `json:"owner_id"` + Filename string `json:"filename"` + ContentType string `json:"content_type"` + SizeBytes int `json:"size_bytes"` + Sha256 string `json:"sha256"` + Status string `json:"status"` + Variants []VariantResponse `json:"variants"` + ErrorCode *string `json:"error_code"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +func (r CreateAttachmentRequest) Validate() bool { + if n := utf8.RuneCountInString(r.Filename); n < 1 || n > 255 { + return false + } + + for _, ch := range r.Filename { + if ch == '/' || ch == '\\' || ch == 0 || unicode.IsControl(ch) { + return false + } + } + + return true +} + +func (r GetAttachmentContentRequest) Validate() bool { + return r.Variant == "" || r.Variant == "original" +} + +func ToAttachmentResponse(a model.Attachment) AttachmentResponse { + variants := make([]VariantResponse, 0) + return AttachmentResponse{ + ID: a.ID, + ChatID: a.ChatID, + OwnerID: a.OwnerID, + Filename: a.Filename, + ContentType: string(a.ContentType), + SizeBytes: a.SizeBytes, + Sha256: a.Sha256, + Status: string(a.Status), + Variants: variants, + ErrorCode: a.ErrorCode, + CreatedAt: a.CreatedAt, + UpdatedAt: a.UpdatedAt, + } +} diff --git a/internal/dto/chat.go b/internal/dto/chat.go new file mode 100644 index 00000000..f8c2cca0 --- /dev/null +++ b/internal/dto/chat.go @@ -0,0 +1,50 @@ +package dto + +import ( + "mtusi2026-messenger/internal/model" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" +) + +type CreateChatRequest struct { + Title string `json:"title"` + MemberIDs []uuid.UUID `json:"member_ids"` +} + +type ChatResponse struct { + ID uuid.UUID `json:"id"` + Title string `json:"title"` + MemberIDs []uuid.UUID `json:"member_ids"` + CreatedAt time.Time `json:"created_at"` +} + +type ChatPageResponse struct { + Items []ChatResponse `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +func (r CreateChatRequest) Validate() bool { + if strings.TrimSpace(r.Title) == "" || + strings.ContainsRune(r.Title, 0) || + utf8.RuneCountInString(r.Title) > 100 { + return false + } + + if len(r.MemberIDs) < 1 || len(r.MemberIDs) > 100 { + return false + } + + return true +} + +func ToChatResponse(c model.Chat) ChatResponse { + return ChatResponse{ + ID: c.ID, + Title: c.Title, + MemberIDs: c.MemberIDs, + CreatedAt: c.CreatedAt, + } +} diff --git a/internal/dto/error.go b/internal/dto/error.go new file mode 100644 index 00000000..3a46305e --- /dev/null +++ b/internal/dto/error.go @@ -0,0 +1,25 @@ +package dto + +type ErrorCode string + +const ( + ErrorCodeInvalidRequest ErrorCode = "invalid_request" + ErrorCodeUnauthorized ErrorCode = "unauthorized" + ErrorCodeNotFound ErrorCode = "not_found" + ErrorCodeConflict ErrorCode = "conflict" + ErrorCodePayloadTooLarge ErrorCode = "payload_too_large" + ErrorCodeUnsupportedMediaType ErrorCode = "unsupported_media_type" + ErrorCodeRateLimited ErrorCode = "rate_limited" + ErrorCodeUnavailable ErrorCode = "unavailable" + ErrorCodeInternalError ErrorCode = "internal_error" +) + +type ErrorResponse struct { + Error Error `json:"error"` +} + +type Error struct { + Code ErrorCode `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} diff --git a/internal/dto/health.go b/internal/dto/health.go new file mode 100644 index 00000000..8bb35034 --- /dev/null +++ b/internal/dto/health.go @@ -0,0 +1,5 @@ +package dto + +type HealthResponse struct { + Status string `json:"status"` +} diff --git a/internal/dto/message.go b/internal/dto/message.go new file mode 100644 index 00000000..00902c7e --- /dev/null +++ b/internal/dto/message.go @@ -0,0 +1,64 @@ +package dto + +import ( + "mtusi2026-messenger/internal/model" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" +) + +type CreateMessageRequest struct { + Text string `json:"text"` + AttachmentIDs []string `json:"attachment_ids"` +} + +type MessageResponse struct { + ID uuid.UUID `json:"id"` + ChatID uuid.UUID `json:"chat_id"` + SenderID uuid.UUID `json:"sender_id"` + Text string `json:"text"` + AttachmentIDs []uuid.UUID `json:"attachment_ids"` + CreatedAt time.Time `json:"created_at"` +} + +type MessagePageResponse struct { + Items []MessageResponse `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +func (r CreateMessageRequest) Validate() bool { + if strings.ContainsRune(r.Text, 0) || utf8.RuneCountInString(r.Text) > 4000 { + return false + } + if len(r.AttachmentIDs) > 10 { + return false + } + + seen := make(map[string]struct{}, len(r.AttachmentIDs)) + for _, id := range r.AttachmentIDs { + if _, ok := seen[id]; ok { + return false + } + seen[id] = struct{}{} + } + + hasText := strings.TrimSpace(r.Text) != "" + hasAttachment := len(r.AttachmentIDs) > 0 + + return hasText || hasAttachment +} + +func ToMessageResponse(m model.Message) MessageResponse { + ids := make([]uuid.UUID, len(m.AttachmentIDs)) + copy(ids, m.AttachmentIDs) + return MessageResponse{ + ID: m.ID, + ChatID: m.ChatID, + SenderID: m.SenderID, + Text: m.Text, + AttachmentIDs: ids, + CreatedAt: m.CreatedAt, + } +} diff --git a/internal/dto/session.go b/internal/dto/session.go new file mode 100644 index 00000000..f8d1ccf2 --- /dev/null +++ b/internal/dto/session.go @@ -0,0 +1,10 @@ +package dto + +import "time" + +type SessionResponse struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt time.Time `json:"expires_at"` + User UserResponse `json:"user"` +} diff --git a/internal/dto/user.go b/internal/dto/user.go new file mode 100644 index 00000000..27973e1d --- /dev/null +++ b/internal/dto/user.go @@ -0,0 +1,44 @@ +package dto + +import ( + "regexp" + "strings" + "time" + "unicode/utf8" + + "github.com/google/uuid" +) + +var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + +type CreateUserRequest struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` + Password string `json:"password"` +} + +type UserResponse struct { + ID uuid.UUID `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt time.Time `json:"created_at"` +} + +func (r CreateUserRequest) Validate() bool { + if !usernamePattern.MatchString(r.Username) { + return false + } + + displayNameLength := utf8.RuneCountInString(r.DisplayName) + if strings.TrimSpace(r.DisplayName) == "" || + displayNameLength > 100 || + strings.ContainsRune(r.DisplayName, 0) { + return false + } + + if n := utf8.RuneCountInString(r.Password); n < 12 || n > 128 { + return false + } + + return true +} diff --git a/internal/handler/attachment.go b/internal/handler/attachment.go new file mode 100644 index 00000000..5470d7bc --- /dev/null +++ b/internal/handler/attachment.go @@ -0,0 +1,140 @@ +package handler + +import ( + "errors" + "net/http" + "time" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + + "mtusi2026-messenger/internal/dto" + "mtusi2026-messenger/internal/httpx" + "mtusi2026-messenger/internal/middleware" + "mtusi2026-messenger/internal/service" +) + +type AttachmentHandler struct { + service *service.AttachmentService + maxBytes int +} + +func NewAttachmentHandler(service *service.AttachmentService, maxBytes int) *AttachmentHandler { + return &AttachmentHandler{ + service: service, + maxBytes: maxBytes, + } +} + +func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + chatID, err := uuid.Parse(chi.URLParam(r, "chat_id")) + if err != nil { + httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid chat_id", requestID) + return + } + + req := dto.CreateAttachmentRequest{ + Filename: r.URL.Query().Get("filename"), + } + if !req.Validate() { + httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid filename", requestID) + return + } + + if deadline, ok := r.Context().Deadline(); ok { + controller := http.NewResponseController(w) + if err := controller.SetReadDeadline(deadline); err != nil { + httpx.WriteInternalError(w, requestID, err) + return + } + defer func() { _ = controller.SetReadDeadline(time.Time{}) }() + } + + contentType := r.Header.Get("Content-Type") + r.Body = http.MaxBytesReader(w, r.Body, int64(h.maxBytes)) + + att, err := h.service.Upload(r.Context(), userID, chatID, req.Filename, r.Body, contentType) + switch { + case errors.Is(err, service.ErrUploadReadTimeout): + r.Close = true + w.Header().Set("Connection", "close") + httpx.WriteError(w, http.StatusRequestTimeout, dto.ErrorCodeInvalidRequest, "upload body read timed out", requestID) + return + case errors.Is(err, service.ErrInvalidSize): + httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID) + return + case errors.Is(err, service.ErrInvalidImage): + httpx.WriteError(w, http.StatusUnsupportedMediaType, dto.ErrorCodeUnsupportedMediaType, "invalid image", requestID) + return + case errors.Is(err, service.ErrAttachmentNotFound): + httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + httpx.WriteJSON(w, http.StatusCreated, dto.ToAttachmentResponse(att)) +} + +func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + attachmentID, err := uuid.Parse(chi.URLParam(r, "attachment_id")) + if err != nil { + httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid attachment_id", requestID) + return + } + + att, err := h.service.Get(r.Context(), userID, attachmentID) + switch { + case errors.Is(err, service.ErrAttachmentNotFound): + httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + httpx.WriteJSON(w, http.StatusOK, dto.ToAttachmentResponse(att)) +} + +func (h *AttachmentHandler) GetContent(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + attachmentID, err := uuid.Parse(chi.URLParam(r, "attachment_id")) + if err != nil { + httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid attachment_id", requestID) + return + } + + req := dto.GetAttachmentContentRequest{ + Variant: r.URL.Query().Get("variant"), + } + if !req.Validate() { + httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) + return + } + + data, contentType, err := h.service.GetContent(r.Context(), userID, attachmentID) + switch { + case errors.Is(err, service.ErrAttachmentNotReady): + httpx.WriteError(w, http.StatusConflict, dto.ErrorCodeConflict, "attachment not ready", requestID) + return + case errors.Is(err, service.ErrAttachmentNotFound): + httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + w.Header().Set("Content-Type", string(contentType)) + w.WriteHeader(http.StatusOK) + _, _ = w.Write(data) +} diff --git a/internal/handler/auth.go b/internal/handler/auth.go new file mode 100644 index 00000000..13424130 --- /dev/null +++ b/internal/handler/auth.go @@ -0,0 +1,80 @@ +package handler + +import ( + "errors" + "net/http" + + "mtusi2026-messenger/internal/dto" + "mtusi2026-messenger/internal/httpx" + "mtusi2026-messenger/internal/middleware" + "mtusi2026-messenger/internal/service" +) + +type AuthHandler struct { + service *service.AuthService +} + +func NewAuthHandler(s *service.AuthService) *AuthHandler { + return &AuthHandler{service: s} +} + +func (h *AuthHandler) CreateSession(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + + username, plain, ok := r.BasicAuth() + if !ok { + httpx.WriteUnauthorized(w, httpx.BasicChallenge, requestID) + return + } + + if !httpx.RejectBody(w, r, requestID) { + return + } + + session, err := h.service.Login(r.Context(), username, plain) + if errors.Is(err, service.ErrInvalidCredentials) { + httpx.WriteUnauthorized(w, httpx.BasicChallenge, requestID) + return + } + if err != nil { + httpx.WriteInternalError(w, requestID, err) + return + } + + httpx.WriteJSON(w, http.StatusCreated, dto.SessionResponse{ + Token: session.Token, + TokenType: "Bearer", + ExpiresAt: session.ExpiresAt, + User: dto.UserResponse{ + ID: session.User.ID, + Username: session.User.Username, + DisplayName: session.User.DisplayName, + CreatedAt: session.User.CreatedAt, + }, + }) +} + +func (h *AuthHandler) RevokeSession(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + + token, ok := httpx.BearerToken(r) + if !ok { + httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID) + return + } + + err := h.service.Logout(r.Context(), token) + switch { + case errors.Is(err, service.ErrSessionCacheUnavailable): + httpx.WriteError(w, http.StatusServiceUnavailable, dto.ErrorCodeUnavailable, "session cache unavailable", requestID) + return + case errors.Is(err, service.ErrUnauthorized): + httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + w.WriteHeader(http.StatusNoContent) +} diff --git a/internal/handler/chat.go b/internal/handler/chat.go new file mode 100644 index 00000000..e5f9c1e6 --- /dev/null +++ b/internal/handler/chat.go @@ -0,0 +1,150 @@ +package handler + +import ( + "errors" + "net/http" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + + "mtusi2026-messenger/internal/dto" + "mtusi2026-messenger/internal/httpx" + "mtusi2026-messenger/internal/middleware" + "mtusi2026-messenger/internal/service" +) + +type ChatHandler struct { + service *service.ChatService +} + +func NewChatHandler(service *service.ChatService) *ChatHandler { + return &ChatHandler{service: service} +} + +func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + var req dto.CreateChatRequest + if !httpx.DecodeJSONBody(w, r, requestID, &req) { + return + } + + if !req.Validate() { + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid request body", + requestID, + ) + return + } + + chat, err := h.service.Create(r.Context(), userID, req.Title, req.MemberIDs) + switch { + case errors.Is(err, service.ErrNotChatMember): + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "caller must be a member of member_ids", + requestID, + ) + return + case errors.Is(err, service.ErrDuplicateMember): + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "member_ids contains duplicates", + requestID, + ) + return + case errors.Is(err, service.ErrUnknownMember): + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "member_ids contains unknown user", + requestID, + ) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + httpx.WriteJSON(w, http.StatusCreated, dto.ToChatResponse(chat)) +} + +func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + chatID, err := uuid.Parse(chi.URLParam(r, "chat_id")) + if err != nil { + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid chat_id", + requestID, + ) + return + } + + chat, err := h.service.Get(r.Context(), userID, chatID) + switch { + case errors.Is(err, service.ErrChatNotFound): + httpx.WriteError( + w, + http.StatusNotFound, + dto.ErrorCodeNotFound, + "chat not found", + requestID, + ) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + httpx.WriteJSON(w, http.StatusOK, dto.ToChatResponse(chat)) +} + +func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + limit, cursor, ok := httpx.ParsePagination(w, r, requestID) + if !ok { + return + } + + chats, nextCursor, err := h.service.List(r.Context(), userID, limit, cursor) + switch { + case errors.Is(err, service.ErrInvalidCursor): + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid cursor", + requestID, + ) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + items := make([]dto.ChatResponse, 0, len(chats)) + for _, c := range chats { + items = append(items, dto.ToChatResponse(c)) + } + + httpx.WriteJSON(w, http.StatusOK, dto.ChatPageResponse{ + Items: items, + NextCursor: nextCursor, + }) +} diff --git a/internal/handler/health.go b/internal/handler/health.go new file mode 100644 index 00000000..9ffc69a1 --- /dev/null +++ b/internal/handler/health.go @@ -0,0 +1,62 @@ +package handler + +import ( + "context" + "encoding/json" + "log/slog" + "net/http" + "time" + + "mtusi2026-messenger/internal/dto" + "mtusi2026-messenger/internal/httpx" +) + +const readyPingTimeout = 2 * time.Second + +type Pinger interface { + Ping(ctx context.Context) error +} + +type HealthHandler struct { + log *slog.Logger + dependencies []Pinger +} + +func NewHealthHandler(log *slog.Logger, dependencies ...Pinger) *HealthHandler { + return &HealthHandler{log: log, dependencies: dependencies} +} + +func (h *HealthHandler) Live(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + + _ = json.NewEncoder(w).Encode(dto.HealthResponse{ + Status: "ok", + }) +} + +func (h *HealthHandler) Ready(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithTimeout(r.Context(), readyPingTimeout) + defer cancel() + + for _, dependency := range h.dependencies { + if err := dependency.Ping(ctx); err != nil { + h.log.ErrorContext(r.Context(), "readiness check failed", "error", err) + httpx.WriteError( + w, + http.StatusServiceUnavailable, + dto.ErrorCodeUnavailable, + "service unavailable", + w.Header().Get("X-Request-Id"), + ) + return + } + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + + _ = json.NewEncoder(w).Encode(dto.HealthResponse{ + Status: "ok", + }) +} diff --git a/internal/handler/message.go b/internal/handler/message.go new file mode 100644 index 00000000..bd926ba0 --- /dev/null +++ b/internal/handler/message.go @@ -0,0 +1,188 @@ +package handler + +import ( + "errors" + "net/http" + "regexp" + + "github.com/go-chi/chi/v5" + "github.com/google/uuid" + + "mtusi2026-messenger/internal/dto" + "mtusi2026-messenger/internal/httpx" + "mtusi2026-messenger/internal/middleware" + "mtusi2026-messenger/internal/service" +) + +var idempotencyKeyReq = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) + +type MessageHandler struct { + service *service.MessageService +} + +func NewMessageHandler(service *service.MessageService) *MessageHandler { + return &MessageHandler{service: service} +} + +func (h *MessageHandler) Create(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + chatID, err := uuid.Parse(chi.URLParam(r, "chat_id")) + if err != nil { + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid chat_id", + requestID, + ) + return + } + + key := r.Header.Get("Idempotency-Key") + if !idempotencyKeyReq.MatchString(key) { + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid Idempotency-Key", + requestID, + ) + return + } + + var req dto.CreateMessageRequest + if !httpx.DecodeJSONBody(w, r, requestID, &req) { + return + } + + if !req.Validate() { + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid request body", + requestID, + ) + return + } + + attachmentIDs := make([]uuid.UUID, 0, len(req.AttachmentIDs)) + for _, s := range req.AttachmentIDs { + id, err := uuid.Parse(s) + if err != nil { + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid attachment_id", + requestID, + ) + return + } + attachmentIDs = append(attachmentIDs, id) + } + + message, err := h.service.Create(r.Context(), userID, chatID, key, req.Text, attachmentIDs) + switch { + case errors.Is(err, service.ErrMessageChatNotFound): + httpx.WriteError( + w, + http.StatusNotFound, + dto.ErrorCodeNotFound, + "chat not found", + requestID, + ) + return + case errors.Is(err, service.ErrAttachmentNotFound): + httpx.WriteError( + w, + http.StatusNotFound, + dto.ErrorCodeNotFound, + "attachment not found", + requestID, + ) + return + case errors.Is(err, service.ErrAttachmentNotReady): + httpx.WriteError( + w, + http.StatusConflict, + dto.ErrorCodeConflict, + "attachment not ready", + requestID, + ) + return + case errors.Is(err, service.ErrIdempotencyKeyConflict): + httpx.WriteError( + w, + http.StatusConflict, + dto.ErrorCodeConflict, + "idempotency key reused with different request", + requestID, + ) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + httpx.WriteJSON(w, http.StatusCreated, dto.ToMessageResponse(message)) +} + +func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + chatID, err := uuid.Parse(chi.URLParam(r, "chat_id")) + if err != nil { + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid chat_id", + requestID, + ) + return + } + + limit, cursor, ok := httpx.ParsePagination(w, r, requestID) + if !ok { + return + } + + messages, nextCursor, err := h.service.List(r.Context(), userID, chatID, limit, cursor) + switch { + case errors.Is(err, service.ErrMessageChatNotFound): + httpx.WriteError( + w, + http.StatusNotFound, + dto.ErrorCodeNotFound, + "chat not found", + requestID, + ) + return + case errors.Is(err, service.ErrInvalidCursor): + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid cursor", + requestID, + ) + return + case err != nil: + httpx.WriteInternalError(w, requestID, err) + return + } + + items := make([]dto.MessageResponse, 0, len(messages)) + for _, m := range messages { + items = append(items, dto.ToMessageResponse(m)) + } + + httpx.WriteJSON(w, http.StatusOK, dto.MessagePageResponse{ + Items: items, + NextCursor: nextCursor, + }) +} diff --git a/internal/handler/user.go b/internal/handler/user.go new file mode 100644 index 00000000..66cc985a --- /dev/null +++ b/internal/handler/user.go @@ -0,0 +1,95 @@ +package handler + +import ( + "errors" + "net/http" + + "mtusi2026-messenger/internal/dto" + "mtusi2026-messenger/internal/httpx" + "mtusi2026-messenger/internal/middleware" + "mtusi2026-messenger/internal/service" +) + +type UserHandler struct { + service *service.UserService +} + +func NewUserHandler(service *service.UserService) *UserHandler { + return &UserHandler{service: service} +} + +func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + + var req dto.CreateUserRequest + if !httpx.DecodeJSONBody(w, r, requestID, &req) { + return + } + + if !req.Validate() { + httpx.WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "invalid request body", + requestID, + ) + return + } + + user, err := h.service.Create( + r.Context(), + req.Username, + req.DisplayName, + req.Password, + ) + if errors.Is(err, service.ErrUsernameTaken) { + httpx.WriteError( + w, + http.StatusConflict, + dto.ErrorCodeConflict, + "username already taken", + requestID, + ) + return + } + if err != nil { + httpx.WriteInternalError(w, requestID, err) + return + } + + httpx.WriteJSON(w, http.StatusCreated, dto.UserResponse{ + ID: user.ID, + Username: user.Username, + DisplayName: user.DisplayName, + CreatedAt: user.CreatedAt, + }) +} + +func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) { + requestID := middleware.GetRequestID(r.Context()) + userID := middleware.GetUserID(r.Context()) + + user, err := h.service.GetByID(r.Context(), userID) + if errors.Is(err, service.ErrUserNotFound) { + httpx.WriteError( + w, + http.StatusNotFound, + dto.ErrorCodeNotFound, + "user not found", + requestID, + ) + return + } + if err != nil { + httpx.WriteInternalError(w, requestID, err) + return + } + + httpx.WriteJSON(w, http.StatusOK, dto.UserResponse{ + ID: user.ID, + Username: user.Username, + DisplayName: user.DisplayName, + CreatedAt: user.CreatedAt, + }) +} diff --git a/internal/httpx/auth.go b/internal/httpx/auth.go new file mode 100644 index 00000000..b2fb421c --- /dev/null +++ b/internal/httpx/auth.go @@ -0,0 +1,37 @@ +package httpx + +import ( + "net/http" + "strings" + + "mtusi2026-messenger/internal/dto" +) + +const ( + BasicChallenge = `Basic realm="messenger", charset="UTF-8"` + BearerChallenge = `Bearer realm="messenger"` +) + +func BearerToken(r *http.Request) (string, bool) { + const prefix = "Bearer " + + header := r.Header.Get("Authorization") + if len(header) <= len(prefix) || !strings.EqualFold(header[:len(prefix)], prefix) { + return "", false + } + + token := strings.TrimSpace(header[len(prefix):]) + + return token, token != "" +} + +func WriteUnauthorized(w http.ResponseWriter, challenge, requestID string) { + w.Header().Set("WWW-Authenticate", challenge) + WriteError( + w, + http.StatusUnauthorized, + dto.ErrorCodeUnauthorized, + "unauthorized", + requestID, + ) +} diff --git a/internal/httpx/pagination.go b/internal/httpx/pagination.go new file mode 100644 index 00000000..2e7e0222 --- /dev/null +++ b/internal/httpx/pagination.go @@ -0,0 +1,34 @@ +package httpx + +import ( + "net/http" + "strconv" + + "mtusi2026-messenger/internal/dto" +) + +func ParsePagination(w http.ResponseWriter, r *http.Request, requestID string) (limit int, cursor *string, ok bool) { + q := r.URL.Query() + + limit = 50 + if raw := q.Get("limit"); raw != "" { + v, err := strconv.Atoi(raw) + if err != nil || v < 1 || v > 100 { + WriteError( + w, + http.StatusBadRequest, + dto.ErrorCodeInvalidRequest, + "limit must be an integer between 1 and 100", + requestID, + ) + return 0, nil, false + } + limit = v + } + + if raw := q.Get("cursor"); raw != "" { + cursor = &raw + } + + return limit, cursor, true +} diff --git a/internal/httpx/request.go b/internal/httpx/request.go new file mode 100644 index 00000000..4a84ebae --- /dev/null +++ b/internal/httpx/request.go @@ -0,0 +1,49 @@ +package httpx + +import ( + "encoding/json" + "errors" + "io" + "mime" + "net/http" + + "mtusi2026-messenger/internal/dto" +) + +func DecodeJSONBody(w http.ResponseWriter, r *http.Request, requestID string, dst any) bool { + if ct := r.Header.Get("Content-Type"); ct != "" { + mt, _, err := mime.ParseMediaType(ct) + if err != nil || mt != "application/json" { + WriteError(w, http.StatusUnsupportedMediaType, dto.ErrorCodeUnsupportedMediaType, "expected application/json", requestID) + return false + } + } + + decoder := json.NewDecoder(r.Body) + decoder.DisallowUnknownFields() + + if err := decoder.Decode(dst); err != nil { + WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID) + return false + } + + if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) { + WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID) + return false + } + + return true +} + +func RejectBody(w http.ResponseWriter, r *http.Request, requestID string) bool { + if r.Body == nil || r.Body == http.NoBody { + return true + } + + var b [1]byte + if n, _ := r.Body.Read(b[:]); n > 0 { + WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID) + return false + } + return true +} diff --git a/internal/httpx/response.go b/internal/httpx/response.go new file mode 100644 index 00000000..23f2771a --- /dev/null +++ b/internal/httpx/response.go @@ -0,0 +1,68 @@ +package httpx + +import ( + "context" + "encoding/json" + "errors" + "net" + "net/http" + "strings" + + "github.com/jackc/pgx/v5/pgconn" + + "mtusi2026-messenger/internal/dto" + s3store "mtusi2026-messenger/internal/s3" +) + +func WriteJSON(w http.ResponseWriter, status int, body any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(body) +} + +func WriteError(w http.ResponseWriter, status int, code dto.ErrorCode, message, requestID string) { + WriteJSON(w, status, dto.ErrorResponse{ + Error: dto.Error{ + Code: code, + Message: message, + RequestID: requestID, + }, + }) +} + +func WriteInternalError(w http.ResponseWriter, requestID string, err error) { + if isUnavailable(err) { + WriteError( + w, + http.StatusServiceUnavailable, + dto.ErrorCodeUnavailable, + "dependency unavailable", + requestID, + ) + return + } + + WriteError( + w, + http.StatusInternalServerError, + dto.ErrorCodeInternalError, + "internal server error", + requestID, + ) +} + +func isUnavailable(err error) bool { + if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, s3store.ErrUnavailable) { + return true + } + + if _, ok := errors.AsType[net.Error](err); ok { + return true + } + + if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok { + return strings.HasPrefix(pgErr.Code, "08") || pgErr.Code == "57P01" + } + + return false +} diff --git a/internal/logger/logger.go b/internal/logger/logger.go new file mode 100644 index 00000000..89ce6002 --- /dev/null +++ b/internal/logger/logger.go @@ -0,0 +1,14 @@ +package logger + +import ( + "log/slog" + "os" +) + +func New(level slog.Level) *slog.Logger { + return slog.New( + slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ + Level: level, + }), + ) +} diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go new file mode 100644 index 00000000..5ade80c3 --- /dev/null +++ b/internal/middleware/auth.go @@ -0,0 +1,50 @@ +package middleware + +import ( + "context" + "errors" + "net/http" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/httpx" + "mtusi2026-messenger/internal/service" +) + +type userIDContextKey struct{} + +type authenticator interface { + Authenticate(ctx context.Context, token string) (uuid.UUID, error) +} + +func Auth(auth authenticator) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestID := GetRequestID(r.Context()) + + token, ok := httpx.BearerToken(r) + if !ok { + httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID) + return + } + + userID, err := auth.Authenticate(r.Context(), token) + if errors.Is(err, service.ErrUnauthorized) { + httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID) + return + } + if err != nil { + httpx.WriteInternalError(w, requestID, err) + return + } + + ctx := context.WithValue(r.Context(), userIDContextKey{}, userID) + next.ServeHTTP(w, r.WithContext(ctx)) + }) + } +} + +func GetUserID(ctx context.Context) uuid.UUID { + id, _ := ctx.Value(userIDContextKey{}).(uuid.UUID) + return id +} diff --git a/internal/middleware/concurrency.go b/internal/middleware/concurrency.go new file mode 100644 index 00000000..631f4bce --- /dev/null +++ b/internal/middleware/concurrency.go @@ -0,0 +1,41 @@ +package middleware + +import ( + "net/http" + + "mtusi2026-messenger/internal/dto" + "mtusi2026-messenger/internal/httpx" +) + +func ConcurrencyLimit(max int) func(http.Handler) http.Handler { + if max <= 0 { + return func(next http.Handler) http.Handler { + return next + } + } + + slots := make(chan struct{}, max) + + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + select { + case slots <- struct{}{}: + defer func() { + <-slots + }() + + next.ServeHTTP(w, r) + + default: + w.Header().Set("Retry-After", "1") + httpx.WriteError( + w, + http.StatusTooManyRequests, + dto.ErrorCodeRateLimited, + "too many requests", + GetRequestID(r.Context()), + ) + } + }) + } +} diff --git a/internal/middleware/instance_id.go b/internal/middleware/instance_id.go new file mode 100644 index 00000000..5fa86d18 --- /dev/null +++ b/internal/middleware/instance_id.go @@ -0,0 +1,14 @@ +package middleware + +import ( + "net/http" +) + +func InstanceID(id string) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Instance-Id", id) + next.ServeHTTP(w, r) + }) + } +} diff --git a/internal/middleware/logging.go b/internal/middleware/logging.go new file mode 100644 index 00000000..7d7da2cb --- /dev/null +++ b/internal/middleware/logging.go @@ -0,0 +1,53 @@ +package middleware + +import ( + "log/slog" + "net/http" + "time" +) + +type responseWriter struct { + http.ResponseWriter + status int +} + +func (w *responseWriter) Unwrap() http.ResponseWriter { + return w.ResponseWriter +} + +func (w *responseWriter) WriteHeader(status int) { + w.status = status + w.ResponseWriter.WriteHeader(status) +} + +func Logging(log *slog.Logger) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + + writer := &responseWriter{ + ResponseWriter: w, + status: http.StatusOK, + } + + next.ServeHTTP(writer, r) + + level := slog.LevelInfo + + switch { + case writer.status >= 500: + level = slog.LevelError + case writer.status >= 400: + level = slog.LevelWarn + } + + log.Log(r.Context(), level, "http request", + "request_id", GetRequestID(r.Context()), + "method", r.Method, + "path", r.URL.Path, + "status", writer.status, + "duration_ms", time.Since(start).Milliseconds(), + ) + }) + } +} diff --git a/internal/middleware/request_id.go b/internal/middleware/request_id.go new file mode 100644 index 00000000..d62808b0 --- /dev/null +++ b/internal/middleware/request_id.go @@ -0,0 +1,30 @@ +package middleware + +import ( + "context" + "net/http" + + "github.com/google/uuid" +) + +type requestIDContextKey struct{} + +func RequestID(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + requestID := r.Header.Get("X-Request-Id") + + if requestID == "" { + requestID = uuid.NewString() + } + + w.Header().Set("X-Request-Id", requestID) + + ctx := context.WithValue(r.Context(), requestIDContextKey{}, requestID) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + +func GetRequestID(ctx context.Context) string { + id, _ := ctx.Value(requestIDContextKey{}).(string) + return id +} diff --git a/internal/middleware/timeout.go b/internal/middleware/timeout.go new file mode 100644 index 00000000..9486fa30 --- /dev/null +++ b/internal/middleware/timeout.go @@ -0,0 +1,17 @@ +package middleware + +import ( + "context" + "net/http" + "time" +) + +func RequestTimeout(d time.Duration) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + ctx, cancel := context.WithTimeout(r.Context(), d) + defer cancel() + next.ServeHTTP(w, r.WithContext(ctx)) + }) + } +} diff --git a/internal/model/attachment.go b/internal/model/attachment.go new file mode 100644 index 00000000..c3c76ea4 --- /dev/null +++ b/internal/model/attachment.go @@ -0,0 +1,34 @@ +package model + +import ( + "time" + + "github.com/google/uuid" +) + +type ContentType string + +const ( + ContentTypePNG ContentType = "image/png" + ContentTypeJPEG ContentType = "image/jpeg" +) + +type AttachmentStatus string + +const ( + AttachmentStatusReady AttachmentStatus = "ready" +) + +type Attachment struct { + ID uuid.UUID + ChatID uuid.UUID + OwnerID uuid.UUID + Filename string + ContentType ContentType + SizeBytes int + Sha256 string + Status AttachmentStatus + ErrorCode *string + CreatedAt time.Time + UpdatedAt time.Time +} diff --git a/internal/model/chat.go b/internal/model/chat.go new file mode 100644 index 00000000..2c47c490 --- /dev/null +++ b/internal/model/chat.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" + + "github.com/google/uuid" +) + +type Chat struct { + ID uuid.UUID + Title string + MemberIDs []uuid.UUID + CreatedAt time.Time +} diff --git a/internal/model/message.go b/internal/model/message.go new file mode 100644 index 00000000..8db0c61c --- /dev/null +++ b/internal/model/message.go @@ -0,0 +1,16 @@ +package model + +import ( + "time" + + "github.com/google/uuid" +) + +type Message struct { + ID uuid.UUID + ChatID uuid.UUID + SenderID uuid.UUID + Text string + AttachmentIDs []uuid.UUID + CreatedAt time.Time +} diff --git a/internal/model/session.go b/internal/model/session.go new file mode 100644 index 00000000..4129b793 --- /dev/null +++ b/internal/model/session.go @@ -0,0 +1,9 @@ +package model + +import "time" + +type Session struct { + Token string + ExpiresAt time.Time + User User +} diff --git a/internal/model/user.go b/internal/model/user.go new file mode 100644 index 00000000..dc7eff16 --- /dev/null +++ b/internal/model/user.go @@ -0,0 +1,14 @@ +package model + +import ( + "time" + + "github.com/google/uuid" +) + +type User struct { + ID uuid.UUID + Username string + DisplayName string + CreatedAt time.Time +} diff --git a/internal/postgres/errors.go b/internal/postgres/errors.go new file mode 100644 index 00000000..961f069e --- /dev/null +++ b/internal/postgres/errors.go @@ -0,0 +1,25 @@ +package postgres + +import ( + "errors" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" +) + +const ( + CodeUniqueViolation = "23505" + CodeForeignKeyViolation = "23503" + CodeCheckViolation = "23514" +) + +func ErrCode(err error) string { + if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok { + return pgErr.Code + } + return "" +} + +func IsNotFound(err error) bool { + return errors.Is(err, pgx.ErrNoRows) +} diff --git a/internal/postgres/pool.go b/internal/postgres/pool.go new file mode 100644 index 00000000..4a1361aa --- /dev/null +++ b/internal/postgres/pool.go @@ -0,0 +1,34 @@ +package postgres + +import ( + "context" + "fmt" + "time" + + "github.com/jackc/pgx/v5/pgxpool" +) + +func New(ctx context.Context, url string) (*pgxpool.Pool, error) { + cfg, err := pgxpool.ParseConfig(url) + if err != nil { + return nil, fmt.Errorf("parse database url: %w", err) + } + + cfg.MaxConns = 10 + cfg.ConnConfig.ConnectTimeout = 5 * time.Second + cfg.ConnConfig.RuntimeParams["statement_timeout"] = "5000" + + pool, err := pgxpool.NewWithConfig(ctx, cfg) + if err != nil { + return nil, fmt.Errorf("create pool: %w", err) + } + + pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + if err := pool.Ping(pingCtx); err != nil { + pool.Close() + return nil, fmt.Errorf("ping database: %w", err) + } + + return pool, nil +} diff --git a/internal/reconcile/reconcile.go b/internal/reconcile/reconcile.go new file mode 100644 index 00000000..11bb3279 --- /dev/null +++ b/internal/reconcile/reconcile.go @@ -0,0 +1,295 @@ +package reconcile + +import ( + "context" + "fmt" + "log/slog" + "sort" + "time" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/db" + s3store "mtusi2026-messenger/internal/s3" +) + +const ( + Prefix = "chats/" + DefaultGrace = time.Hour + DefaultMaxDeletes = 100 + ErrorCodeObjectMissing = "object_missing" +) + +type Options struct { + DryRun bool + Grace time.Duration + MaxDeletes int +} + +type Report struct { + ScannedObjects int `json:"scanned_objects"` + ScannedRows int `json:"scanned_rows"` + ListPages int `json:"list_pages"` + OrphanObjects []string `json:"orphan_objects"` + MissingAttachments []uuid.UUID `json:"missing_attachments"` + StaleUploads []uuid.UUID `json:"stale_uploads"` + CorruptObjects []string `json:"corrupt_objects"` + SkippedFresh int `json:"skipped_fresh"` + SkippedReferenced int `json:"skipped_referenced"` + SkippedInactive int `json:"skipped_inactive"` + DeletedObjects int `json:"deleted_objects"` + DeletedRows int `json:"deleted_rows"` + MarkedFailed int `json:"marked_failed"` + DryRun bool `json:"dry_run"` + Errors []string `json:"errors"` + DurationMS int64 `json:"duration_ms"` +} + +type ObjectClient interface { + ListAll(ctx context.Context, prefix string) (s3store.ListResult, error) + DeleteAdmin(ctx context.Context, key string) error +} + +type AttachmentQueries interface { + ListAttachmentsForReconcile(ctx context.Context) ([]db.ListAttachmentsForReconcileRow, error) + DeleteQueuedAttachment(ctx context.Context, id uuid.UUID) (int64, error) + MarkAttachmentFailedIfReady(ctx context.Context, arg db.MarkAttachmentFailedIfReadyParams) (int64, error) +} + +type Reconciler struct { + q AttachmentQueries + store ObjectClient + log *slog.Logger + now func() time.Time +} + +type plan struct { + orphans []s3store.ObjectInfo + staleUploads []db.ListAttachmentsForReconcileRow + missing []db.ListAttachmentsForReconcileRow + corrupt []s3store.ObjectInfo + skippedFresh int + skippedReferenced int + skippedInactive int +} + +func New(store ObjectClient, queries AttachmentQueries, log *slog.Logger) *Reconciler { + if log == nil { + log = slog.Default() + } + + return &Reconciler{ + store: store, + q: queries, + log: log, + now: time.Now, + } +} + +func (r *Reconciler) Run(ctx context.Context, opts Options) (Report, error) { + opts = opts.withDefaults() + started := r.now() + + report := Report{ + OrphanObjects: []string{}, + MissingAttachments: []uuid.UUID{}, + StaleUploads: []uuid.UUID{}, + CorruptObjects: []string{}, + DryRun: opts.DryRun, + Errors: []string{}, + } + + listed, err := r.store.ListAll(ctx, Prefix) + if err != nil { + return report, err + } + + rows, err := r.q.ListAttachmentsForReconcile(ctx) + if err != nil { + return report, err + } + + report.ScannedObjects = len(listed.Objects) + report.ScannedRows = len(rows) + report.ListPages = listed.Pages + + p := buildPlan(listed.Objects, rows, started, opts.Grace) + + for _, obj := range p.orphans { + report.OrphanObjects = append(report.OrphanObjects, obj.Key) + } + for _, obj := range p.corrupt { + report.CorruptObjects = append(report.CorruptObjects, obj.Key) + } + for _, row := range p.staleUploads { + report.StaleUploads = append(report.StaleUploads, row.ID) + } + for _, row := range p.missing { + report.MissingAttachments = append(report.MissingAttachments, row.ID) + } + + report.SkippedFresh = p.skippedFresh + report.SkippedReferenced = p.skippedReferenced + report.SkippedInactive = p.skippedInactive + + if !opts.DryRun { + r.apply(ctx, opts, p, &report) + } + + report.DurationMS = r.now().Sub(started).Milliseconds() + + r.log.Info( + "attachment reconcile finished", + "dry_run", opts.DryRun, + "scanned_objects", report.ScannedObjects, + "scanned_rows", report.ScannedRows, + "list_pages", report.ListPages, + "orphan_objects", len(report.OrphanObjects), + "missing_attachments", len(report.MissingAttachments), + "stale_uploads", len(report.StaleUploads), + "corrupt_objects", len(report.CorruptObjects), + "deleted_objects", report.DeletedObjects, + "deleted_rows", report.DeletedRows, + "marked_failed", report.MarkedFailed, + "errors", len(report.Errors), + ) + + return report, nil +} + +func (o Options) withDefaults() Options { + if o.Grace <= 0 { + o.Grace = DefaultGrace + } + if o.MaxDeletes <= 0 { + o.MaxDeletes = DefaultMaxDeletes + } + + return o +} + +func buildPlan( + objects []s3store.ObjectInfo, + rows []db.ListAttachmentsForReconcileRow, + now time.Time, + grace time.Duration, +) plan { + var p plan + + byKey := make(map[string]s3store.ObjectInfo, len(objects)) + for _, obj := range objects { + byKey[obj.Key] = obj + } + + for _, row := range rows { + obj, exists := byKey[row.S3Key] + if exists { + delete(byKey, row.S3Key) + } + + switch row.Status { + case db.AttachmentStatusQueued: + if now.Sub(row.CreatedAt) < grace { + p.skippedFresh++ + continue + } + if row.Referenced { + p.skippedReferenced++ + continue + } + p.staleUploads = append(p.staleUploads, row) + case db.AttachmentStatusReady: + if !exists { + p.missing = append(p.missing, row) + continue + } + if obj.Size != int64(row.SizeBytes) { + p.corrupt = append(p.corrupt, obj) + } + default: + if exists { + p.skippedInactive++ + } + } + } + + for _, obj := range byKey { + if now.Sub(obj.LastModified) < grace { + p.skippedFresh++ + continue + } + p.orphans = append(p.orphans, obj) + } + + sort.Slice(p.orphans, func(i, j int) bool { + return p.orphans[i].Key < p.orphans[j].Key + }) + sort.Slice(p.corrupt, func(i, j int) bool { + return p.corrupt[i].Key < p.corrupt[j].Key + }) + sort.Slice(p.staleUploads, func(i, j int) bool { + return p.staleUploads[i].ID.String() < p.staleUploads[j].ID.String() + }) + sort.Slice(p.missing, func(i, j int) bool { + return p.missing[i].ID.String() < p.missing[j].ID.String() + }) + + return p +} + +func (r *Reconciler) apply(ctx context.Context, opts Options, p plan, report *Report) { + deletes := 0 + + for _, row := range p.staleUploads { + if deletes >= opts.MaxDeletes { + report.Errors = append(report.Errors, "max deletes reached") + break + } + + removed, err := r.q.DeleteQueuedAttachment(ctx, row.ID) + if err != nil { + report.Errors = append(report.Errors, fmt.Sprintf("delete row %s: %v", row.ID, err)) + continue + } + if removed == 0 { + continue + } + + deletes++ + report.DeletedRows++ + + if err := r.store.DeleteAdmin(ctx, row.S3Key); err != nil { + report.Errors = append(report.Errors, fmt.Sprintf("delete object %s: %v", row.S3Key, err)) + continue + } + report.DeletedObjects++ + } + + for _, obj := range p.orphans { + if deletes >= opts.MaxDeletes { + report.Errors = append(report.Errors, "max deletes reached") + break + } + + if err := r.store.DeleteAdmin(ctx, obj.Key); err != nil { + report.Errors = append(report.Errors, fmt.Sprintf("delete object %s: %v", obj.Key, err)) + continue + } + + deletes++ + report.DeletedObjects++ + } + + for _, row := range p.missing { + marked, err := r.q.MarkAttachmentFailedIfReady(ctx, db.MarkAttachmentFailedIfReadyParams{ + ID: row.ID, + ErrorCode: ErrorCodeObjectMissing, + }) + if err != nil { + report.Errors = append(report.Errors, fmt.Sprintf("mark failed %s: %v", row.ID, err)) + continue + } + + report.MarkedFailed += int(marked) + } +} diff --git a/internal/redis/client.go b/internal/redis/client.go new file mode 100644 index 00000000..65ccd3a2 --- /dev/null +++ b/internal/redis/client.go @@ -0,0 +1,32 @@ +package redis + +import ( + "context" + "fmt" + "log/slog" + "time" + + goredis "github.com/redis/go-redis/v9" +) + +func New(ctx context.Context, log *slog.Logger, url string) (*goredis.Client, error) { + opts, err := goredis.ParseURL(url) + if err != nil { + return nil, fmt.Errorf("parse redis url: %w", err) + } + + opts.DialTimeout = 1 * time.Second + opts.ReadTimeout = 200 * time.Millisecond + opts.WriteTimeout = 200 * time.Millisecond + opts.MaxRetries = -1 + + client := goredis.NewClient(opts) + + pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + if err := client.Ping(pingCtx).Err(); err != nil { + log.Warn("redis unavailable, running without cache", "error", err) + } + + return client, nil +} diff --git a/internal/s3/client.go b/internal/s3/client.go new file mode 100644 index 00000000..c51188c4 --- /dev/null +++ b/internal/s3/client.go @@ -0,0 +1,279 @@ +package s3 + +import ( + "context" + "errors" + "io" + "log/slog" + "net" + "net/http" + "time" + + "github.com/aws/aws-sdk-go-v2/aws" + awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http" + awsconfig "github.com/aws/aws-sdk-go-v2/config" + "github.com/aws/aws-sdk-go-v2/credentials" + awss3 "github.com/aws/aws-sdk-go-v2/service/s3" + "github.com/aws/smithy-go" + + appconfig "mtusi2026-messenger/internal/config" +) + +var ( + ErrUnavailable = errors.New("s3 unavailable") + ErrLoadConfig = errors.New("load aws config") + ErrHeadBucket = errors.New("head bucket") + ErrCreateBucket = errors.New("create bucket") + ErrObjectNotFound = errors.New("object not found") + ErrPutObject = errors.New("put object") + ErrGetObject = errors.New("get object") + ErrListObjects = errors.New("list objects") + ErrDeleteObject = errors.New("delete object") +) + +type Client struct { + bucket string + bootstrap *awss3.Client + api *awss3.Client + log *slog.Logger +} + +type ObjectInfo struct { + Key string + Size int64 + LastModified time.Time +} + +type ListResult struct { + Objects []ObjectInfo + Pages int +} + +func New(ctx context.Context, cfg appconfig.S3Config, log *slog.Logger) (*Client, error) { + apiAccessKey, err := cfg.APIAccessKey() + if err != nil { + log.Error("read s3 api access key", "error", err) + return nil, err + } + + apiSecretKey, err := cfg.APISecretKey() + if err != nil { + log.Error("read s3 api secret key", "error", err) + return nil, err + } + + bootstrapAccessKey, err := cfg.BootstrapAccessKey() + if err != nil { + log.Error("read s3 bootstrap access key", "error", err) + return nil, err + } + + bootstrapSecretKey, err := cfg.BootstrapSecretKey() + if err != nil { + log.Error("read s3 bootstrap secret key", "error", err) + return nil, err + } + + bootstrap, err := newS3Client(ctx, log, cfg.Endpoint, cfg.Region, bootstrapAccessKey, bootstrapSecretKey) + if err != nil { + return nil, err + } + + api, err := newS3Client(ctx, log, cfg.Endpoint, cfg.Region, apiAccessKey, apiSecretKey) + if err != nil { + return nil, err + } + + return &Client{ + bucket: cfg.Bucket, + bootstrap: bootstrap, + api: api, + log: log, + }, nil +} + +func (c *Client) Bucket() string { + return c.bucket +} + +func (c *Client) EnsureBucket(ctx context.Context) error { + _, err := c.bootstrap.HeadBucket(ctx, &awss3.HeadBucketInput{ + Bucket: aws.String(c.bucket), + }) + if err == nil { + return nil + } + + if !isNotFound(err) { + c.log.Error("head bucket", "bucket", c.bucket, "error", err) + return ErrHeadBucket + } + + _, err = c.bootstrap.CreateBucket(ctx, &awss3.CreateBucketInput{ + Bucket: aws.String(c.bucket), + }) + if err == nil || isAlreadyExists(err) { + return nil + } + + c.log.Error("create bucket", "bucket", c.bucket, "error", err) + return ErrCreateBucket +} + +func (c *Client) Ping(ctx context.Context) error { + _, err := c.api.HeadBucket(ctx, &awss3.HeadBucketInput{Bucket: aws.String(c.bucket)}) + if err != nil { + c.log.Error("head bucket", "bucket", c.bucket, "error", err) + return operationError(ErrHeadBucket, err) + } + return nil +} + +func (c *Client) Put( + ctx context.Context, + key string, + body io.ReadSeeker, + size int64, + contentType string, +) error { + _, err := c.api.PutObject(ctx, &awss3.PutObjectInput{ + Bucket: aws.String(c.bucket), + Key: aws.String(key), + Body: body, + ContentLength: aws.Int64(size), + ContentType: aws.String(contentType), + }) + if err != nil { + c.log.Error("put object", "bucket", c.bucket, "key", key, "error", err) + return operationError(ErrPutObject, err) + } + + return nil +} + +func (c *Client) Get(ctx context.Context, key string) (*awss3.GetObjectOutput, error) { + out, err := c.api.GetObject(ctx, &awss3.GetObjectInput{ + Bucket: aws.String(c.bucket), + Key: aws.String(key), + }) + if isNotFound(err) { + c.log.Warn("get object", "bucket", c.bucket, "key", key, "error", err) + return nil, ErrObjectNotFound + } + if err != nil { + c.log.Error("get object", "bucket", c.bucket, "key", key, "error", err) + return nil, operationError(ErrGetObject, err) + } + + return out, nil +} + +func (c *Client) ListAll(ctx context.Context, prefix string) (ListResult, error) { + result := ListResult{Objects: []ObjectInfo{}} + + paginator := awss3.NewListObjectsV2Paginator(c.bootstrap, &awss3.ListObjectsV2Input{ + Bucket: aws.String(c.bucket), + Prefix: aws.String(prefix), + }) + + for paginator.HasMorePages() { + page, err := paginator.NextPage(ctx) + if err != nil { + c.log.Error("list objects", "bucket", c.bucket, "prefix", prefix, "error", err) + return ListResult{}, ErrListObjects + } + + result.Pages++ + + for _, obj := range page.Contents { + result.Objects = append(result.Objects, ObjectInfo{ + Key: aws.ToString(obj.Key), + Size: aws.ToInt64(obj.Size), + LastModified: aws.ToTime(obj.LastModified), + }) + } + } + + return result, nil +} + +func (c *Client) DeleteAdmin(ctx context.Context, key string) error { + _, err := c.bootstrap.DeleteObject(ctx, &awss3.DeleteObjectInput{ + Bucket: aws.String(c.bucket), + Key: aws.String(key), + }) + if isNotFound(err) { + return nil + } + if err != nil { + c.log.Error("delete admin object", "bucket", c.bucket, "key", key, "error", err) + return ErrDeleteObject + } + + return nil +} + +func newS3Client( + ctx context.Context, + log *slog.Logger, + endpoint string, + region string, + accessKey string, + secretKey string, +) (*awss3.Client, error) { + cfg, err := awsconfig.LoadDefaultConfig( + ctx, + awsconfig.WithRegion(region), + awsconfig.WithCredentialsProvider( + credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""), + ), + ) + if err != nil { + log.Error("load aws config", "endpoint", endpoint, "region", region, "error", err) + return nil, ErrLoadConfig + } + + return awss3.NewFromConfig(cfg, func(o *awss3.Options) { + o.BaseEndpoint = aws.String(endpoint) + o.UsePathStyle = true + }), nil +} + +func operationError(operation, err error) error { + var networkError net.Error + var responseError *awshttp.ResponseError + if errors.Is(err, context.DeadlineExceeded) || errors.As(err, &networkError) || + (errors.As(err, &responseError) && responseError.HTTPStatusCode() >= 500) { + return ErrUnavailable + } + return operation +} + +func isNotFound(err error) bool { + var responseErr *awshttp.ResponseError + if errors.As(err, &responseErr) && responseErr.HTTPStatusCode() == http.StatusNotFound { + return true + } + + var apiErr smithy.APIError + if errors.As(err, &apiErr) { + switch apiErr.ErrorCode() { + case "NotFound", "NoSuchBucket", "NoSuchKey": + return true + } + } + + return false +} + +func isAlreadyExists(err error) bool { + var apiErr smithy.APIError + if errors.As(err, &apiErr) { + switch apiErr.ErrorCode() { + case "BucketAlreadyOwnedByYou", "BucketAlreadyExists": + return true + } + } + + return false +} diff --git a/internal/s3/config.go b/internal/s3/config.go new file mode 100644 index 00000000..672c33c2 --- /dev/null +++ b/internal/s3/config.go @@ -0,0 +1,99 @@ +package s3 + +import ( + "encoding/json" + "fmt" + "os" + "path/filepath" + + "mtusi2026-messenger/internal/config" +) + +const ( + seaweedUID = 1000 + seaweedGID = 1000 +) + +type Credential struct { + AccessKey string `json:"accessKey"` + SecretKey string `json:"secretKey"` +} + +type Identity struct { + Name string `json:"name"` + Credentials []Credential `json:"credentials"` + Actions []string `json:"actions"` +} + +type Config struct { + Identities []Identity `json:"identities"` +} + +func WriteConfig(output string, cfg config.S3Config) error { + apiAccessKey, err := cfg.APIAccessKey() + if err != nil { + return err + } + + apiSecretKey, err := cfg.APISecretKey() + if err != nil { + return err + } + + bootstrapAccessKey, err := cfg.BootstrapAccessKey() + if err != nil { + return err + } + + bootstrapSecretKey, err := cfg.BootstrapSecretKey() + if err != nil { + return err + } + + config := Config{ + Identities: []Identity{ + { + Name: "bootstrap", + Credentials: []Credential{ + {AccessKey: bootstrapAccessKey, SecretKey: bootstrapSecretKey}, + }, + Actions: []string{"Admin", "Read", "Write", "List", "Tagging"}, + }, + { + Name: "api", + Credentials: []Credential{ + {AccessKey: apiAccessKey, SecretKey: apiSecretKey}, + }, + Actions: []string{"Read", "Write", "List", "Tagging"}, + }, + }, + } + + data, err := json.MarshalIndent(config, "", " ") + if err != nil { + return fmt.Errorf("marshal s3 config: %w", err) + } + + if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil { + return fmt.Errorf("create config directory: %w", err) + } + + tmp := output + ".tmp" + if err := os.WriteFile(tmp, append(data, '\n'), 0o600); err != nil { + return fmt.Errorf("write config: %w", err) + } + + if err := os.Chown(tmp, seaweedUID, seaweedGID); err != nil { + return fmt.Errorf("chown config: %w", err) + } + + if err := os.Chmod(tmp, 0o400); err != nil { + return fmt.Errorf("chmod config: %w", err) + } + + if err := os.Rename(tmp, output); err != nil { + return fmt.Errorf("replace config: %w", err) + } + + return nil +} diff --git a/internal/service/attachment.go b/internal/service/attachment.go new file mode 100644 index 00000000..63112c94 --- /dev/null +++ b/internal/service/attachment.go @@ -0,0 +1,286 @@ +package service + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "image" + _ "image/jpeg" + _ "image/png" + "io" + "net" + "net/http" + "os" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/db" + "mtusi2026-messenger/internal/model" + "mtusi2026-messenger/internal/postgres" + s3store "mtusi2026-messenger/internal/s3" +) + +var ( + ErrAttachmentNotFound = errors.New("attachment not found") + ErrAttachmentNotReady = errors.New("attachment not ready") + ErrInvalidImage = errors.New("invalid image") + ErrInvalidSize = errors.New("invalid size") + ErrUploadReadTimeout = errors.New("upload body read timed out") +) + +type attachmentQueries interface { + CreateAttachment(ctx context.Context, arg db.CreateAttachmentParams) (db.Attachment, error) + GetAttachmentByID(ctx context.Context, id uuid.UUID) (db.Attachment, error) + IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error) + MarkAttachmentReady(ctx context.Context, id uuid.UUID) (db.Attachment, error) +} + +type AttachmentService struct { + q attachmentQueries + s3 *s3store.Client + maxBytes int + maxPixels int + decodeSlot chan struct{} +} + +func NewAttachmentService( + q attachmentQueries, + s3 *s3store.Client, + maxBytes, maxPixels int, +) *AttachmentService { + return &AttachmentService{ + q: q, + s3: s3, + maxBytes: maxBytes, + maxPixels: maxPixels, + decodeSlot: make(chan struct{}, 1), + } +} + +func (s *AttachmentService) Upload( + ctx context.Context, + userID, chatID uuid.UUID, + filename string, + body io.Reader, + contentType string, +) (model.Attachment, error) { + if err := s.ensureMember(ctx, userID, chatID); err != nil { + return model.Attachment{}, err + } + + tmp, err := os.CreateTemp("", "upload-*.tmp") + if err != nil { + return model.Attachment{}, err + } + defer func() { + _ = tmp.Close() + _ = os.Remove(tmp.Name()) + }() + + hasher := sha256.New() + size, err := io.Copy(io.MultiWriter(tmp, hasher), body) + if err != nil { + var maxBytesErr *http.MaxBytesError + if errors.As(err, &maxBytesErr) { + return model.Attachment{}, ErrInvalidSize + } + var networkErr net.Error + if errors.As(err, &networkErr) && networkErr.Timeout() { + return model.Attachment{}, ErrUploadReadTimeout + } + return model.Attachment{}, err + } + if size > int64(s.maxBytes) { + return model.Attachment{}, ErrInvalidSize + } + + if _, err := tmp.Seek(0, io.SeekStart); err != nil { + return model.Attachment{}, err + } + detectedType, err := s.checkImage(ctx, tmp, contentType) + if err != nil { + return model.Attachment{}, err + } + + id, err := uuid.NewV7() + if err != nil { + return model.Attachment{}, err + } + + hash := hex.EncodeToString(hasher.Sum(nil)) + key := "chats/" + chatID.String() + "/attachments/" + id.String() + + if _, err := tmp.Seek(0, io.SeekStart); err != nil { + return model.Attachment{}, err + } + + _, err = s.q.CreateAttachment(ctx, db.CreateAttachmentParams{ + ID: id, + ChatID: chatID, + OwnerID: userID, + S3Key: key, + Filename: filename, + ContentType: db.AttachmentContentType(detectedType), + SizeBytes: int(size), + Sha256: hash, + ErrorCode: "", + }) + if postgres.IsNotFound(err) { + return model.Attachment{}, ErrAttachmentNotFound + } + if err != nil { + return model.Attachment{}, err + } + + if err := s.s3.Put(ctx, key, tmp, size, string(detectedType)); err != nil { + return model.Attachment{}, err + } + + row, err := s.q.MarkAttachmentReady(ctx, id) + if err != nil { + return model.Attachment{}, err + } + + return model.Attachment{ + ID: row.ID, + ChatID: row.ChatID, + OwnerID: row.OwnerID, + Filename: row.Filename, + ContentType: model.ContentType(row.ContentType), + SizeBytes: row.SizeBytes, + Sha256: row.Sha256, + Status: model.AttachmentStatus(row.Status), + ErrorCode: row.ErrorCode, + CreatedAt: row.CreatedAt, + UpdatedAt: row.UpdatedAt, + }, nil +} + +func (s *AttachmentService) Get( + ctx context.Context, + userID, attachmentID uuid.UUID, +) (model.Attachment, error) { + row, err := s.q.GetAttachmentByID(ctx, attachmentID) + if postgres.IsNotFound(err) { + return model.Attachment{}, ErrAttachmentNotFound + } + if err != nil { + return model.Attachment{}, err + } + + if err := s.ensureMember(ctx, userID, row.ChatID); err != nil { + return model.Attachment{}, err + } + + return model.Attachment{ + ID: row.ID, + ChatID: row.ChatID, + OwnerID: row.OwnerID, + Filename: row.Filename, + ContentType: model.ContentType(row.ContentType), + SizeBytes: row.SizeBytes, + Sha256: row.Sha256, + Status: model.AttachmentStatus(row.Status), + ErrorCode: row.ErrorCode, + CreatedAt: row.CreatedAt, + UpdatedAt: row.UpdatedAt, + }, nil +} + +func (s *AttachmentService) GetContent( + ctx context.Context, + userID, attachmentID uuid.UUID, +) ([]byte, model.ContentType, error) { + row, err := s.q.GetAttachmentByID(ctx, attachmentID) + if postgres.IsNotFound(err) { + return nil, "", ErrAttachmentNotFound + } + if err != nil { + return nil, "", err + } + + if err := s.ensureMember(ctx, userID, row.ChatID); err != nil { + return nil, "", err + } + + if model.AttachmentStatus(row.Status) != model.AttachmentStatusReady { + return nil, "", ErrAttachmentNotReady + } + + out, err := s.s3.Get(ctx, row.S3Key) + if err != nil { + return nil, "", err + } + defer out.Body.Close() + + data, err := io.ReadAll(out.Body) + if err != nil { + return nil, "", err + } + + return data, model.ContentType(row.ContentType), nil +} + +func (s *AttachmentService) checkImage(ctx context.Context, r io.ReadSeeker, contentType string) (model.ContentType, error) { + cfg, format, err := image.DecodeConfig(r) + if err != nil { + return "", ErrInvalidImage + } + + if cfg.Width <= 0 || cfg.Height <= 0 { + return "", ErrInvalidImage + } + if cfg.Width > s.maxPixels/cfg.Height { + return "", ErrInvalidSize + } + + var detectedType model.ContentType + switch format { + case "png": + if contentType != string(model.ContentTypePNG) { + return "", ErrInvalidImage + } + detectedType = model.ContentTypePNG + case "jpeg": + if contentType != string(model.ContentTypeJPEG) { + return "", ErrInvalidImage + } + detectedType = model.ContentTypeJPEG + default: + return "", ErrInvalidImage + } + + select { + case s.decodeSlot <- struct{}{}: + defer func() { <-s.decodeSlot }() + case <-ctx.Done(): + return "", ctx.Err() + } + if err := ctx.Err(); err != nil { + return "", err + } + if _, err := r.Seek(0, io.SeekStart); err != nil { + return "", err + } + if _, _, err := image.Decode(r); err != nil { + return "", ErrInvalidImage + } + + return detectedType, nil +} + +func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID uuid.UUID) error { + member, err := s.q.IsChatMember(ctx, db.IsChatMemberParams{ + ChatID: chatID, + UserID: userID, + }) + if err != nil { + return err + } + if !member { + return ErrAttachmentNotFound + } + + return nil +} diff --git a/internal/service/auth.go b/internal/service/auth.go new file mode 100644 index 00000000..6e4ac775 --- /dev/null +++ b/internal/service/auth.go @@ -0,0 +1,188 @@ +package service + +import ( + "context" + "errors" + "log/slog" + "time" + + "mtusi2026-messenger/internal/credentials" + "mtusi2026-messenger/internal/db" + "mtusi2026-messenger/internal/model" + "mtusi2026-messenger/internal/postgres" + + "github.com/google/uuid" + goredis "github.com/redis/go-redis/v9" +) + +const sessionCacheTTL = 60 * time.Second +const cacheOpTimeout = 300 * time.Millisecond + +var ( + ErrInvalidCredentials = errors.New("invalid credentials") + ErrUnauthorized = errors.New("unauthorized") + ErrSessionCacheUnavailable = errors.New("session cache unavailable") +) + +type authQueries interface { + CreateSession(ctx context.Context, arg db.CreateSessionParams) error + DeleteSession(ctx context.Context, tokenHash string) (int64, error) + GetActiveSession(ctx context.Context, tokenHash string) (db.GetActiveSessionRow, error) + GetUserByUsername(ctx context.Context, username string) (db.User, error) +} + +type AuthService struct { + q authQueries + cache *goredis.Client + sessionTTL time.Duration + log *slog.Logger +} + +func NewAuthService(q authQueries, cache *goredis.Client, sessionTTL time.Duration, log *slog.Logger) *AuthService { + return &AuthService{ + q: q, + cache: cache, + sessionTTL: sessionTTL, + log: log, + } +} + +var dummyHash, _ = credentials.HashPassword("dummy-password-for-timing") + +func (s *AuthService) Login(ctx context.Context, username, plain string) (model.Session, error) { + user, err := s.q.GetUserByUsername(ctx, username) + if postgres.IsNotFound(err) { + _ = credentials.VerifyPassword(dummyHash, plain) + return model.Session{}, ErrInvalidCredentials + } + if err != nil { + return model.Session{}, err + } + + if err := credentials.VerifyPassword(user.PasswordHash, plain); err != nil { + if errors.Is(err, credentials.ErrPasswordMismatch) { + return model.Session{}, ErrInvalidCredentials + } + return model.Session{}, err + } + + token := credentials.NewToken() + expiresAt := time.Now().UTC().Add(s.sessionTTL) + + err = s.q.CreateSession(ctx, db.CreateSessionParams{ + TokenHash: credentials.HashToken(token), + UserID: user.ID, + ExpiresAt: expiresAt, + }) + if err != nil { + return model.Session{}, err + } + + return model.Session{ + Token: token, + ExpiresAt: expiresAt, + User: model.User{ + ID: user.ID, + Username: user.Username, + DisplayName: user.DisplayName, + CreatedAt: user.CreatedAt.UTC(), + }, + }, nil +} + +func (s *AuthService) Authenticate(ctx context.Context, token string) (uuid.UUID, error) { + tokenHash := credentials.HashToken(token) + key := cacheKey(tokenHash) + + if id, ok := s.cacheGet(ctx, key); ok { + return id, nil + } + + row, err := s.q.GetActiveSession(ctx, tokenHash) + if postgres.IsNotFound(err) { + return uuid.Nil, ErrUnauthorized + } + if err != nil { + return uuid.Nil, err + } + + s.cacheSet(ctx, key, row.UserID, min(time.Until(row.ExpiresAt), sessionCacheTTL)) + + return row.UserID, nil +} + +func (s *AuthService) Logout(ctx context.Context, token string) error { + tokenHash := credentials.HashToken(token) + + n, err := s.q.DeleteSession(ctx, tokenHash) + if err != nil { + return err + } + + if err := s.cacheDel(ctx, cacheKey(tokenHash)); err != nil { + return ErrSessionCacheUnavailable + } + + if n == 0 { + return ErrUnauthorized + } + return nil +} + +func cacheKey(tokenHash string) string { + return "session:" + tokenHash +} + +func (s *AuthService) cacheGet(ctx context.Context, key string) (uuid.UUID, bool) { + if s.cache == nil { + return uuid.Nil, false + } + + cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout) + defer cancel() + + val, err := s.cache.Get(cctx, key).Result() + switch { + case err == nil: + id, perr := uuid.Parse(val) + if perr != nil { + s.log.Warn("session cache value is not a uuid", "error", perr) + return uuid.Nil, false + } + s.log.Debug("session cache hit") + return id, true + case errors.Is(err, goredis.Nil): + s.log.Debug("session cache miss") + default: + s.log.Warn("session cache get failed, falling back to database", "error", err) + } + return uuid.Nil, false +} + +func (s *AuthService) cacheSet(ctx context.Context, key string, id uuid.UUID, ttl time.Duration) { + if s.cache == nil || ttl <= 0 { + return + } + + cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout) + defer cancel() + + if err := s.cache.Set(cctx, key, id.String(), ttl).Err(); err != nil { + s.log.Warn("session cache set failed", "error", err) + } +} + +func (s *AuthService) cacheDel(ctx context.Context, key string) error { + if s.cache == nil { + return nil + } + + cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout) + defer cancel() + + if err := s.cache.Del(cctx, key).Err(); err != nil { + s.log.Warn("session cache delete failed", "error", err) + return err + } + return nil +} diff --git a/internal/service/chat.go b/internal/service/chat.go new file mode 100644 index 00000000..c00caa8d --- /dev/null +++ b/internal/service/chat.go @@ -0,0 +1,204 @@ +package service + +import ( + "bytes" + "context" + "errors" + "slices" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "mtusi2026-messenger/internal/db" + "mtusi2026-messenger/internal/model" + "mtusi2026-messenger/internal/postgres" +) + +var ( + ErrChatNotFound = errors.New("chat not found") + ErrNotChatMember = errors.New("caller must be a member of member_ids") + ErrDuplicateMember = errors.New("member_ids contains duplicates") + ErrUnknownMember = errors.New("member_ids contains unknown user") +) + +type chatQueries interface { + AddChatMembers(ctx context.Context, arg db.AddChatMembersParams) error + CreateChat(ctx context.Context, arg db.CreateChatParams) (db.Chat, error) + GetChat(ctx context.Context, arg db.GetChatParams) (db.GetChatRow, error) + ListChatsAfter(ctx context.Context, arg db.ListChatsAfterParams) ([]db.ListChatsAfterRow, error) + ListChatsFirstPage(ctx context.Context, arg db.ListChatsFirstPageParams) ([]db.ListChatsFirstPageRow, error) +} + +type ChatService struct { + pool txBeginner + q chatQueries + withTx func(pgx.Tx) chatQueries +} + +func NewChatService(pool *pgxpool.Pool, q *db.Queries) *ChatService { + return &ChatService{ + pool: pool, + q: q, + withTx: func(tx pgx.Tx) chatQueries { + return q.WithTx(tx) + }, + } +} + +func (s *ChatService) Create( + ctx context.Context, + userID uuid.UUID, + title string, + memberIDs []uuid.UUID, +) (model.Chat, error) { + if !slices.Contains(memberIDs, userID) { + return model.Chat{}, ErrNotChatMember + } + + tx, err := s.pool.Begin(ctx) + if err != nil { + return model.Chat{}, err + } + defer tx.Rollback(ctx) + + q := s.withTx(tx) + + id, err := uuid.NewV7() + if err != nil { + return model.Chat{}, err + } + + row, err := q.CreateChat(ctx, db.CreateChatParams{ + ID: id, + Title: title, + }) + if err != nil { + return model.Chat{}, err + } + + err = q.AddChatMembers(ctx, db.AddChatMembersParams{ + ChatID: row.ID, + UserIds: memberIDs, + }) + switch postgres.ErrCode(err) { + case postgres.CodeUniqueViolation: + return model.Chat{}, ErrDuplicateMember + case postgres.CodeForeignKeyViolation: + return model.Chat{}, ErrUnknownMember + } + if err != nil { + return model.Chat{}, err + } + + if err := tx.Commit(ctx); err != nil { + return model.Chat{}, err + } + + ids := slices.Clone(memberIDs) + slices.SortFunc(ids, func(a, b uuid.UUID) int { + return bytes.Compare(a[:], b[:]) + }) + + return model.Chat{ + ID: row.ID, + Title: row.Title, + CreatedAt: row.CreatedAt, + MemberIDs: ids, + }, nil +} + +func (s *ChatService) Get( + ctx context.Context, + userID uuid.UUID, + chatID uuid.UUID, +) (model.Chat, error) { + row, err := s.q.GetChat(ctx, db.GetChatParams{ + ID: chatID, + UserID: userID, + }) + if postgres.IsNotFound(err) { + return model.Chat{}, ErrChatNotFound + } + if err != nil { + return model.Chat{}, err + } + + return model.Chat{ + ID: row.ID, + Title: row.Title, + CreatedAt: row.CreatedAt, + MemberIDs: row.MemberIds, + }, nil +} + +func (s *ChatService) List( + ctx context.Context, + userID uuid.UUID, + limit int, + cursor *string, +) ([]model.Chat, *string, error) { + var chats []model.Chat + + if cursor == nil { + rows, err := s.q.ListChatsFirstPage(ctx, db.ListChatsFirstPageParams{ + UserID: userID, + Lim: limit + 1, + }) + if err != nil { + return nil, nil, err + } + + chats = make([]model.Chat, 0, len(rows)) + for _, r := range rows { + chats = append(chats, model.Chat{ + ID: r.ID, + Title: r.Title, + CreatedAt: r.CreatedAt, + MemberIDs: r.MemberIds, + }) + } + } else { + decoded, derr := decodeCursor(*cursor, uuid.Nil) + if derr != nil { + return nil, nil, ErrInvalidCursor + } + + rows, err := s.q.ListChatsAfter(ctx, db.ListChatsAfterParams{ + UserID: userID, + AfterCreatedAt: decoded.CreatedAt, + AfterID: decoded.ID, + Lim: limit + 1, + }) + if err != nil { + return nil, nil, err + } + + chats = make([]model.Chat, 0, len(rows)) + for _, r := range rows { + chats = append(chats, model.Chat{ + ID: r.ID, + Title: r.Title, + CreatedAt: r.CreatedAt, + MemberIDs: r.MemberIds, + }) + } + } + + hasMore := len(chats) > limit + if hasMore { + chats = chats[:limit] + } + + var next *string + if hasMore { + last := chats[len(chats)-1] + c := encodeCursor(Cursor{ + CreatedAt: last.CreatedAt, + ID: last.ID, + }) + next = &c + } + + return chats, next, nil +} diff --git a/internal/service/cursor.go b/internal/service/cursor.go new file mode 100644 index 00000000..4709543f --- /dev/null +++ b/internal/service/cursor.go @@ -0,0 +1,41 @@ +package service + +import ( + "encoding/base64" + "encoding/json" + "errors" + "time" + + "github.com/google/uuid" +) + +var ErrInvalidCursor = errors.New("invalid cursor") + +type Cursor struct { + ChatID uuid.UUID `json:"chat_id"` + CreatedAt time.Time `json:"created_at"` + ID uuid.UUID `json:"id"` +} + +func encodeCursor(cursor Cursor) string { + data, _ := json.Marshal(cursor) + return base64.RawURLEncoding.EncodeToString(data) +} + +func decodeCursor(raw string, chatID uuid.UUID) (Cursor, error) { + data, err := base64.RawURLEncoding.DecodeString(raw) + if err != nil { + return Cursor{}, ErrInvalidCursor + } + + var cursor Cursor + if err := json.Unmarshal(data, &cursor); err != nil { + return Cursor{}, ErrInvalidCursor + } + + if cursor.ID == uuid.Nil || cursor.CreatedAt.IsZero() || cursor.ChatID != chatID { + return Cursor{}, ErrInvalidCursor + } + + return cursor, nil +} diff --git a/internal/service/message.go b/internal/service/message.go new file mode 100644 index 00000000..a6316e35 --- /dev/null +++ b/internal/service/message.go @@ -0,0 +1,289 @@ +package service + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "net/http" + "sort" + "strings" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgxpool" + + "mtusi2026-messenger/internal/db" + "mtusi2026-messenger/internal/model" + "mtusi2026-messenger/internal/postgres" +) + +var ( + ErrMessageChatNotFound = errors.New("chat not found") + ErrIdempotencyKeyConflict = errors.New("idempotency key reused with different request") +) + +type messageQueries interface { + CreateIdempotencyKey(ctx context.Context, arg db.CreateIdempotencyKeyParams) (int64, error) + CreateMessage(ctx context.Context, arg db.CreateMessageParams) (db.Message, error) + CreateMessageAttachment(ctx context.Context, arg db.CreateMessageAttachmentParams) error + GetIdempotentMessage(ctx context.Context, arg db.GetIdempotentMessageParams) (db.GetIdempotentMessageRow, error) + IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error) + ListAttachmentsByIDs(ctx context.Context, arg db.ListAttachmentsByIDsParams) ([]db.AttachmentStatus, error) + ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error) + ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIDs []uuid.UUID) ([]db.ListMessageAttachmentsByMessageIDsRow, error) + ListMessagesAfter(ctx context.Context, arg db.ListMessagesAfterParams) ([]db.Message, error) + ListMessagesFirstPage(ctx context.Context, arg db.ListMessagesFirstPageParams) ([]db.Message, error) +} + +type MessageService struct { + pool txBeginner + q *db.Queries + withTx func(pgx.Tx) messageQueries +} + +func NewMessageService(pool *pgxpool.Pool, q *db.Queries) *MessageService { + return &MessageService{ + pool: pool, + q: q, + withTx: func(tx pgx.Tx) messageQueries { + return q.WithTx(tx) + }, + } +} + +func (s *MessageService) Create( + ctx context.Context, + userID uuid.UUID, + chatID uuid.UUID, + key string, + text string, + attachmentIDs []uuid.UUID, +) (model.Message, error) { + id, err := uuid.NewV7() + if err != nil { + return model.Message{}, err + } + + if len(attachmentIDs) > 0 { + statuses, err := s.q.ListAttachmentsByIDs(ctx, db.ListAttachmentsByIDsParams{ + ChatID: chatID, + Ids: attachmentIDs, + }) + if err != nil { + return model.Message{}, err + } + if len(statuses) != len(attachmentIDs) { + return model.Message{}, ErrAttachmentNotFound + } + for _, status := range statuses { + if status != db.AttachmentStatusReady { + return model.Message{}, ErrAttachmentNotReady + } + } + } + + hashInput := text + "|" + normalizeAttachmentIDs(attachmentIDs) + sum := sha256.Sum256([]byte(hashInput)) + hash := hex.EncodeToString(sum[:]) + path := "/api/v1/chats/" + chatID.String() + "/messages" + + tx, err := s.pool.Begin(ctx) + if err != nil { + return model.Message{}, err + } + defer tx.Rollback(ctx) + + q := s.withTx(tx) + + inserted, err := q.CreateIdempotencyKey(ctx, db.CreateIdempotencyKeyParams{ + UserID: userID, + Method: http.MethodPost, + Path: path, + Key: key, + RequestHash: hash, + MessageID: id, + }) + if err != nil { + return model.Message{}, err + } + + if inserted == 0 { + isMember, err := q.IsChatMember(ctx, db.IsChatMemberParams{ + ChatID: chatID, + UserID: userID, + }) + if err != nil { + return model.Message{}, err + } + if !isMember { + return model.Message{}, ErrMessageChatNotFound + } + + stored, err := q.GetIdempotentMessage(ctx, db.GetIdempotentMessageParams{ + UserID: userID, + Method: http.MethodPost, + Path: path, + Key: key, + }) + if err != nil { + return model.Message{}, err + } + if stored.RequestHash != hash { + return model.Message{}, ErrIdempotencyKeyConflict + } + + storedAttachmentIDs, err := q.ListMessageAttachmentIDs(ctx, stored.ID) + if err != nil { + return model.Message{}, err + } + + return model.Message{ + ID: stored.ID, + ChatID: stored.ChatID, + SenderID: stored.SenderID, + Text: stored.Text, + AttachmentIDs: storedAttachmentIDs, + CreatedAt: stored.CreatedAt, + }, nil + } + + row, err := q.CreateMessage(ctx, db.CreateMessageParams{ + ID: id, + ChatID: chatID, + SenderID: userID, + Text: text, + }) + if postgres.IsNotFound(err) { + return model.Message{}, ErrMessageChatNotFound + } + if err != nil { + return model.Message{}, err + } + + for i, attachmentID := range attachmentIDs { + if err := q.CreateMessageAttachment(ctx, db.CreateMessageAttachmentParams{ + MessageID: id, + AttachmentID: attachmentID, + Position: i, + }); err != nil { + return model.Message{}, err + } + } + + if err := tx.Commit(ctx); err != nil { + return model.Message{}, err + } + + return model.Message{ + ID: row.ID, + ChatID: row.ChatID, + SenderID: row.SenderID, + Text: row.Text, + AttachmentIDs: attachmentIDs, + CreatedAt: row.CreatedAt, + }, nil +} + +func (s *MessageService) List( + ctx context.Context, + userID uuid.UUID, + chatID uuid.UUID, + limit int, + cursor *string, +) ([]model.Message, *string, error) { + isMember, err := s.q.IsChatMember(ctx, db.IsChatMemberParams{ + ChatID: chatID, + UserID: userID, + }) + if err != nil { + return nil, nil, err + } + if !isMember { + return nil, nil, ErrMessageChatNotFound + } + + var rows []db.Message + + if cursor == nil { + rows, err = s.q.ListMessagesFirstPage(ctx, db.ListMessagesFirstPageParams{ + ChatID: chatID, + Lim: limit + 1, + }) + } else { + decoded, derr := decodeCursor(*cursor, chatID) + if derr != nil { + return nil, nil, ErrInvalidCursor + } + + rows, err = s.q.ListMessagesAfter(ctx, db.ListMessagesAfterParams{ + ChatID: chatID, + AfterCreatedAt: decoded.CreatedAt, + AfterID: decoded.ID, + Lim: limit + 1, + }) + } + if err != nil { + return nil, nil, err + } + + hasMore := len(rows) > limit + if hasMore { + rows = rows[:limit] + } + + attachmentsByMessage := make(map[uuid.UUID][]uuid.UUID, len(rows)) + if len(rows) > 0 { + messageIDs := make([]uuid.UUID, len(rows)) + for i, r := range rows { + messageIDs[i] = r.ID + } + + links, err := s.q.ListMessageAttachmentsByMessageIDs(ctx, messageIDs) + if err != nil { + return nil, nil, err + } + for _, l := range links { + attachmentsByMessage[l.MessageID] = append(attachmentsByMessage[l.MessageID], l.AttachmentID) + } + } + + messages := make([]model.Message, 0, len(rows)) + for _, r := range rows { + messages = append(messages, model.Message{ + ID: r.ID, + ChatID: r.ChatID, + SenderID: r.SenderID, + Text: r.Text, + AttachmentIDs: attachmentsByMessage[r.ID], + CreatedAt: r.CreatedAt, + }) + } + + var next *string + if hasMore { + last := messages[len(messages)-1] + c := encodeCursor(Cursor{ + ChatID: chatID, + CreatedAt: last.CreatedAt, + ID: last.ID, + }) + next = &c + } + + return messages, next, nil +} + +func normalizeAttachmentIDs(ids []uuid.UUID) string { + if len(ids) == 0 { + return "" + } + + parts := make([]string, len(ids)) + for i, id := range ids { + parts[i] = id.String() + } + sort.Strings(parts) + + return strings.Join(parts, ",") +} diff --git a/internal/service/tx.go b/internal/service/tx.go new file mode 100644 index 00000000..9b2b8673 --- /dev/null +++ b/internal/service/tx.go @@ -0,0 +1,11 @@ +package service + +import ( + "context" + + "github.com/jackc/pgx/v5" +) + +type txBeginner interface { + Begin(ctx context.Context) (pgx.Tx, error) +} diff --git a/internal/service/user.go b/internal/service/user.go new file mode 100644 index 00000000..555b8b09 --- /dev/null +++ b/internal/service/user.go @@ -0,0 +1,83 @@ +package service + +import ( + "context" + "errors" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/credentials" + "mtusi2026-messenger/internal/db" + "mtusi2026-messenger/internal/model" + "mtusi2026-messenger/internal/postgres" +) + +var ( + ErrUsernameTaken = errors.New("username already taken") + ErrUserNotFound = errors.New("user not found") +) + +type userQueries interface { + CreateUser(ctx context.Context, arg db.CreateUserParams) (db.CreateUserRow, error) + GetUser(ctx context.Context, id uuid.UUID) (db.GetUserRow, error) +} + +type UserService struct { + q userQueries +} + +func NewUserService(q userQueries) *UserService { + return &UserService{q: q} +} + +func (s *UserService) Create( + ctx context.Context, + username, displayName, plain string, +) (model.User, error) { + hash, err := credentials.HashPassword(plain) + if err != nil { + return model.User{}, err + } + + id, err := uuid.NewV7() + if err != nil { + return model.User{}, err + } + + row, err := s.q.CreateUser(ctx, db.CreateUserParams{ + ID: id, + Username: username, + DisplayName: displayName, + PasswordHash: hash, + }) + if err != nil { + if postgres.ErrCode(err) == postgres.CodeUniqueViolation { + return model.User{}, ErrUsernameTaken + } + return model.User{}, err + } + + return model.User{ + ID: row.ID, + Username: row.Username, + DisplayName: row.DisplayName, + CreatedAt: row.CreatedAt.UTC(), + }, nil +} + +func (s *UserService) GetByID(ctx context.Context, id uuid.UUID) (model.User, error) { + row, err := s.q.GetUser(ctx, id) + if postgres.IsNotFound(err) { + return model.User{}, ErrUserNotFound + } + if err != nil { + return model.User{}, err + } + + return model.User{ + ID: row.ID, + Username: row.Username, + DisplayName: row.DisplayName, + CreatedAt: row.CreatedAt.UTC(), + }, nil +} diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..cd3cda19 --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,5 @@ +#!/bin/sh +set -eu + +docker compose down +docker compose up -d --wait diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100755 index 00000000..b7f8570f --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,8 @@ +#!/bin/sh +set -eu +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" + +service=$(docker inspect -f '{{index .Config.Labels "com.docker.compose.service"}}' "$TARGET_INSTANCE_ID") +[ "$service" = "api" ] || { echo "not an api container" >&2; exit 1; } + +docker stop -t 5 "$TARGET_INSTANCE_ID" >/dev/null diff --git a/sqlc.yaml b/sqlc.yaml new file mode 100644 index 00000000..8743b614 --- /dev/null +++ b/sqlc.yaml @@ -0,0 +1,34 @@ +version: "2" +sql: + - engine: "postgresql" + queries: "db/queries" + schema: "db/migrations" + gen: + go: + package: "db" + out: "internal/db" + sql_package: "pgx/v5" + emit_empty_slices: true + emit_pointers_for_null_types: true + overrides: + - db_type: "integer" + go_type: "int" + - db_type: "pg_catalog.int4" + go_type: "int" + - db_type: "uuid" + go_type: "github.com/google/uuid.UUID" + - db_type: "uuid" + nullable: true + go_type: + import: "github.com/google/uuid" + type: "UUID" + pointer: true + + - db_type: "timestamptz" + go_type: "time.Time" + - db_type: "timestamptz" + nullable: true + go_type: + import: "time" + type: "Time" + pointer: true diff --git a/student-tests/attachment_failover_test.go b/student-tests/attachment_failover_test.go new file mode 100644 index 00000000..e37e1440 --- /dev/null +++ b/student-tests/attachment_failover_test.go @@ -0,0 +1,92 @@ +//go:build failure + +package studenttests_test + +import ( + "bytes" + "context" + "io" + "net/http" + "os/exec" + "strings" + "testing" + "time" + + "mtusi2026-messenger/internal/dto" +) + +func TestAttachmentUploaderFailover(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + original := makePNG(t, 32, 32) + resp := uploadAttachment(t, user, chat.ID, "failover.png", "image/png", original) + requireStatus(t, resp, http.StatusCreated) + uploader := resp.Header.Get("X-Instance-Id") + var att dto.AttachmentResponse + mustDecode(t, resp, &att) + if uploader == "" { + t.Fatal("upload response has no X-Instance-Id") + } + + cmd := exec.Command("docker", "compose", "ps", "-q", "api") + cmd.Dir = ".." + out, err := cmd.Output() + if err != nil { + t.Fatalf("list API replicas: %v", err) + } + replicas := strings.Fields(string(out)) + if len(replicas) < 2 { + t.Fatal("at least two running API replicas required") + } + var target string + for _, id := range replicas { + if strings.HasPrefix(id, uploader) { + target = id + break + } + } + if target == "" { + t.Fatal("upload instance is not an API container in this Compose project") + } + t.Cleanup(func() { + if err := exec.Command("docker", "start", target).Run(); err != nil { + t.Errorf("restart uploader replica: %v", err) + } + }) + if err := exec.Command("docker", "stop", "-t", "5", target).Run(); err != nil { + t.Fatalf("stop uploader replica: %v", err) + } + + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + for ctx.Err() == nil { + req, err := http.NewRequestWithContext(ctx, http.MethodGet, *baseURL+"/api/v1/attachments/"+att.ID.String()+"/content", nil) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Authorization", "Bearer "+user.Token) + response, err := httpClient.Do(req) + if err == nil { + data, readErr := io.ReadAll(response.Body) + response.Body.Close() + if response.StatusCode == http.StatusOK { + downloader := response.Header.Get("X-Instance-Id") + if downloader == "" || downloader == uploader { + t.Fatal("download was not served by another replica") + } + if readErr != nil { + t.Fatal(readErr) + } + if !bytes.Equal(data, original) { + t.Fatal("downloaded original differs from uploaded file") + } + return + } + if response.StatusCode != http.StatusBadGateway && response.StatusCode != http.StatusServiceUnavailable && response.StatusCode != http.StatusGatewayTimeout { + t.Fatalf("unexpected download status: %d", response.StatusCode) + } + } + time.Sleep(200 * time.Millisecond) + } + t.Fatal("download through another replica did not recover within 10s") +} diff --git a/student-tests/attachment_helpers_test.go b/student-tests/attachment_helpers_test.go new file mode 100644 index 00000000..3da7613c --- /dev/null +++ b/student-tests/attachment_helpers_test.go @@ -0,0 +1,106 @@ +//go:build contract || reconcile || failure + +package studenttests_test + +import ( + "bytes" + "encoding/json" + "net/http" + "net/url" + "testing" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/dto" +) + +const maxUploadBytes = 10 * 1024 * 1024 + +func uploadAttachment( + t *testing.T, + actor testUser, + chatID uuid.UUID, + filename string, + contentType string, + body []byte, +) *http.Response { + t.Helper() + + req, err := http.NewRequest( + http.MethodPost, + *baseURL+"/api/v1/chats/"+chatID.String()+"/attachments?filename="+url.QueryEscape(filename), + bytes.NewReader(body), + ) + if err != nil { + t.Fatalf("build upload request: %v", err) + } + req.Header.Set("Content-Type", contentType) + req.Header.Set("Authorization", "Bearer "+actor.Token) + + resp, err := httpClient.Do(req) + if err != nil { + t.Fatalf("upload request failed: %v", err) + } + + return resp +} + +func uploadPNG(t *testing.T, actor testUser, chatID uuid.UUID) dto.AttachmentResponse { + t.Helper() + + resp := uploadAttachment(t, actor, chatID, "sample.png", "image/png", makePNG(t, 32, 32)) + requireStatus(t, resp, http.StatusCreated) + + var att dto.AttachmentResponse + mustDecode(t, resp, &att) + + return att +} + +func postMessage( + t *testing.T, + actor testUser, + chatID uuid.UUID, + text string, + attachmentIDs []uuid.UUID, +) *http.Response { + t.Helper() + + body, err := json.Marshal(map[string]any{ + "text": text, + "attachment_ids": attachmentIDs, + }) + if err != nil { + t.Fatalf("marshal message: %v", err) + } + + req, err := http.NewRequest( + http.MethodPost, + *baseURL+"/api/v1/chats/"+chatID.String()+"/messages", + bytes.NewReader(body), + ) + if err != nil { + t.Fatalf("build message request: %v", err) + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+actor.Token) + req.Header.Set("Idempotency-Key", uuid.NewString()) + + resp, err := httpClient.Do(req) + if err != nil { + t.Fatalf("message request failed: %v", err) + } + + return resp +} + +func requireErrorCode(t *testing.T, resp *http.Response, code dto.ErrorCode) { + t.Helper() + + var apiErr dto.ErrorResponse + mustDecode(t, resp, &apiErr) + + if apiErr.Error.Code != code { + t.Errorf("expected error code %q, got %q", code, apiErr.Error.Code) + } +} diff --git a/student-tests/attachment_load_test.go b/student-tests/attachment_load_test.go new file mode 100644 index 00000000..2d8a8549 --- /dev/null +++ b/student-tests/attachment_load_test.go @@ -0,0 +1,180 @@ +//go:build load + +package studenttests_test + +import ( + "bytes" + "net/http" + "os/exec" + "sort" + "strconv" + "strings" + "sync" + "testing" + "time" +) + +func dockerMem(filter string) map[string]float64 { + out, err := exec.Command("docker", "stats", "--no-stream", "--format", "{{.Name}}\t{{.MemUsage}}").Output() + if err != nil { + return nil + } + m := map[string]float64{} + for _, line := range strings.Split(string(out), "\n") { + parts := strings.SplitN(line, "\t", 2) + if len(parts) != 2 || (filter != "" && !strings.Contains(parts[0], filter)) { + continue + } + used := strings.SplitN(parts[1], "/", 2)[0] + m[parts[0]] = memBytes(used) + } + return m +} + +func memBytes(s string) float64 { + s = strings.TrimSpace(s) + units := []struct { + suf string + mult float64 + }{ + {"GiB", 1 << 30}, {"MiB", 1 << 20}, {"KiB", 1 << 10}, + {"GB", 1e9}, {"MB", 1e6}, {"kB", 1e3}, {"B", 1}, + } + for _, u := range units { + if strings.HasSuffix(s, u.suf) { + v, _ := strconv.ParseFloat(strings.TrimSpace(strings.TrimSuffix(s, u.suf)), 64) + return v * u.mult + } + } + return 0 +} + +func mib(v float64) string { + return strconv.FormatFloat(v/(1<<20), 'f', 1, 64) + "MiB" +} + +func TestConcurrentUploads(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + body := makePNG(t, 1870, 1870) + + url := *baseURL + "/api/v1/chats/" + chat.ID.String() + "/attachments?filename=load.png" + + baseline := dockerMem("api") + + stop := make(chan struct{}) + memDone := make(chan struct{}) + peak := map[string]float64{} + go func() { + defer close(memDone) + for { + for name, v := range dockerMem("api") { + if v > peak[name] { + peak[name] = v + } + } + select { + case <-stop: + return + case <-time.After(500 * time.Millisecond): + } + } + }() + + var ( + mu sync.Mutex + statuses = make(map[int]int) + latencies []time.Duration + errors int + ) + start := time.Now() + deadline := start.Add(*duration) + + var wg sync.WaitGroup + for w := 0; w < *workers; w++ { + wg.Add(1) + go func() { + defer wg.Done() + for time.Now().Before(deadline) { + req, err := http.NewRequest(http.MethodPost, url, bytes.NewReader(body)) + if err != nil { + mu.Lock() + errors++ + mu.Unlock() + return + } + req.Header.Set("Content-Type", "image/png") + req.Header.Set("Authorization", "Bearer "+user.Token) + + reqStart := time.Now() + resp, err := httpClient.Do(req) + latency := time.Since(reqStart) + if err != nil { + mu.Lock() + errors++ + mu.Unlock() + continue + } + + mu.Lock() + statuses[resp.StatusCode]++ + if resp.StatusCode == http.StatusCreated { + latencies = append(latencies, latency) + } + mu.Unlock() + resp.Body.Close() + } + }() + } + wg.Wait() + close(stop) + <-memDone + + elapsed := time.Since(start) + + total := 0 + for _, n := range statuses { + total += n + } + total += errors + + t.Logf("workers=%d duration=%.1fs elapsed=%.1fs", *workers, duration.Seconds(), elapsed.Seconds()) + t.Logf("total uploads=%d transport_errors=%d", total, errors) + for status, n := range statuses { + t.Logf("HTTP %d: %d", status, n) + } + + successful := statuses[http.StatusCreated] + t.Logf("successful RPS: %.2f", float64(successful)/elapsed.Seconds()) + + if len(latencies) > 0 { + sort.Slice(latencies, func(i, j int) bool { return latencies[i] < latencies[j] }) + t.Logf("latency p50=%s p95=%s p99=%s", + percentile(latencies, 0.50), + percentile(latencies, 0.95), + percentile(latencies, 0.99), + ) + } + + names := make(map[string]struct{}, len(baseline)+len(peak)) + for name := range baseline { + names[name] = struct{}{} + } + for name := range peak { + names[name] = struct{}{} + } + sorted := make([]string, 0, len(names)) + for name := range names { + sorted = append(sorted, name) + } + sort.Strings(sorted) + + if len(sorted) == 0 { + t.Log("memory: docker stats not available (no containers matched)") + return + } + t.Log("memory per container (idle -> peak):") + for _, name := range sorted { + t.Logf(" %s: %s -> %s", name, mib(baseline[name]), mib(peak[name])) + } +} diff --git a/student-tests/attachment_test.go b/student-tests/attachment_test.go new file mode 100644 index 00000000..0cdd0ad2 --- /dev/null +++ b/student-tests/attachment_test.go @@ -0,0 +1,139 @@ +//go:build contract + +package studenttests_test + +import ( + "bytes" + "image/jpeg" + "image/png" + "io" + "net/http" + "testing" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/dto" +) + +func TestAttachmentJPEG(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + img, err := png.Decode(bytes.NewReader(makePNG(t, 32, 32))) + if err != nil { + t.Fatalf("decode png: %v", err) + } + var body bytes.Buffer + if err := jpeg.Encode(&body, img, nil); err != nil { + t.Fatalf("encode jpeg: %v", err) + } + + resp := uploadAttachment(t, user, chat.ID, "sample.jpg", "image/jpeg", body.Bytes()) + requireStatus(t, resp, http.StatusCreated) + + var att dto.AttachmentResponse + mustDecode(t, resp, &att) + if att.Status != "ready" { + t.Fatalf("expected ready attachment, got %q", att.Status) + } + + resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.ID.String()+"/content", user.Token, nil) + requireStatus(t, resp, http.StatusOK) + defer resp.Body.Close() + + original, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("read original: %v", err) + } + if !bytes.Equal(original, body.Bytes()) { + t.Error("downloaded JPEG differs from uploaded original") + } +} + +func TestAttachmentTooManyPixels(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + body := makePNG(t, 8000, 5001) + if len(body) > maxUploadBytes { + t.Fatal("pixel-limit fixture exceeds upload byte limit") + } + + resp := uploadAttachment(t, user, chat.ID, "large.png", "image/png", body) + requireStatus(t, resp, http.StatusRequestEntityTooLarge) + requireErrorCode(t, resp, dto.ErrorCodePayloadTooLarge) +} + +func TestAttachmentUnsafeFilename(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + resp := uploadAttachment(t, user, chat.ID, "../image.png", "image/png", makePNG(t, 32, 32)) + requireStatus(t, resp, http.StatusBadRequest) + requireErrorCode(t, resp, dto.ErrorCodeInvalidRequest) +} + +func TestAttachmentTooLarge(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + body := bytes.Repeat([]byte{0x00}, maxUploadBytes+1) + + resp := uploadAttachment(t, user, chat.ID, "big.png", "image/png", body) + requireStatus(t, resp, http.StatusRequestEntityTooLarge) + requireErrorCode(t, resp, dto.ErrorCodePayloadTooLarge) +} + +func TestAttachmentCorruptedImage(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + body := []byte("this is definitely not a png or jpeg image") + + resp := uploadAttachment(t, user, chat.ID, "broken.png", "image/png", body) + requireStatus(t, resp, http.StatusUnsupportedMediaType) + requireErrorCode(t, resp, dto.ErrorCodeUnsupportedMediaType) +} + +func TestAttachmentMimeSubstitution(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + resp := uploadAttachment(t, user, chat.ID, "actually-png.jpg", "image/jpeg", makePNG(t, 32, 32)) + requireStatus(t, resp, http.StatusUnsupportedMediaType) + requireErrorCode(t, resp, dto.ErrorCodeUnsupportedMediaType) +} + +func TestAttachmentForeignID(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + foreign := uuid.New() + + resp := doRequest(t, http.MethodGet, "/api/v1/attachments/"+foreign.String(), user.Token, nil) + requireStatus(t, resp, http.StatusNotFound) + requireErrorCode(t, resp, dto.ErrorCodeNotFound) + + resp = doRequest(t, http.MethodGet, "/api/v1/attachments/"+foreign.String()+"/content", user.Token, nil) + requireStatus(t, resp, http.StatusNotFound) + requireErrorCode(t, resp, dto.ErrorCodeNotFound) + + resp = postMessage(t, user, chat.ID, "", []uuid.UUID{foreign}) + requireStatus(t, resp, http.StatusNotFound) + requireErrorCode(t, resp, dto.ErrorCodeNotFound) +} + +func TestAttachmentFromAnotherChat(t *testing.T) { + user := createUser(t) + member := createUser(t) + + chatA := createChat(t, user, user.ID, member.ID) + chatB := createChat(t, user, user.ID) + + att := uploadPNG(t, user, chatA.ID) + + // The attachment belongs to chatA; linking it to a chatB message must fail. + resp := postMessage(t, user, chatB.ID, "", []uuid.UUID{att.ID}) + requireStatus(t, resp, http.StatusNotFound) + requireErrorCode(t, resp, dto.ErrorCodeNotFound) +} diff --git a/student-tests/chat_rollback_test.go b/student-tests/chat_rollback_test.go new file mode 100644 index 00000000..76cfb8d6 --- /dev/null +++ b/student-tests/chat_rollback_test.go @@ -0,0 +1,55 @@ +//go:build contract + +package studenttests_test + +import ( + "context" + "encoding/json" + "net/http" + "os" + "strings" + "testing" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +func TestCreateChatRollback(t *testing.T) { + data, err := os.ReadFile("../secrets/test_database_url.txt") + if err != nil { + t.Fatalf("read secrets/test_database_url: %v", err) + } + + ctx := context.Background() + conn, err := pgx.Connect(ctx, strings.TrimSpace(string(data))) + if err != nil { + t.Fatalf("connect to database: %v", err) + } + defer conn.Close(ctx) + + creator := createUser(t) + title := "rollback-" + uuid.NewString() + + body, err := json.Marshal(map[string]any{ + "title": title, + "member_ids": []uuid.UUID{creator.ID, uuid.New()}, + }) + if err != nil { + t.Fatalf("marshal body: %v", err) + } + + resp := doRequest(t, http.MethodPost, "/api/v1/chats", creator.Token, body) + resp.Body.Close() + if resp.StatusCode < 400 || resp.StatusCode >= 500 { + t.Fatalf("expected 4xx, got %d", resp.StatusCode) + } + + var n int + err = conn.QueryRow(ctx, "SELECT count(*) FROM chats WHERE title = $1", title).Scan(&n) + if err != nil { + t.Fatalf("count chats: %v", err) + } + if n != 0 { + t.Fatalf("expected no chat rows after failed creation, found %d", n) + } +} diff --git a/student-tests/contract_test.go b/student-tests/contract_test.go new file mode 100644 index 00000000..2aea9f28 --- /dev/null +++ b/student-tests/contract_test.go @@ -0,0 +1,134 @@ +//go:build contract + +package studenttests_test + +import ( + "encoding/json" + "net/http" + "net/url" + "strconv" + "testing" + + "mtusi2026-messenger/internal/dto" +) + +func TestInvalidJSON(t *testing.T) { + resp := doRequest(t, http.MethodPost, "/api/v1/users", "", []byte(`{"username": "broken",`)) + requireStatus(t, resp, http.StatusBadRequest) + + var apiErr dto.ErrorResponse + mustDecode(t, resp, &apiErr) + + if apiErr.Error.Code != dto.ErrorCodeInvalidRequest { + t.Errorf("expected code %q, got %q", dto.ErrorCodeInvalidRequest, apiErr.Error.Code) + } +} + +func TestForeignChat(t *testing.T) { + user1 := createUser(t) + user2 := createUser(t) + user3 := createUser(t) + + chat := createChat(t, user1, user1.ID, user2.ID) + + resp := doRequest(t, http.MethodGet, "/api/v1/chats/"+chat.ID.String(), user3.Token, nil) + requireStatus(t, resp, http.StatusNotFound) + + resp = sendMessage(t, user3, chat.ID, "should not land") + requireStatus(t, resp, http.StatusNotFound) +} + +func TestEmptyMessage(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + for _, text := range []string{"", " ", "\t\n"} { + resp := sendMessage(t, user, chat.ID, text) + requireStatus(t, resp, http.StatusBadRequest) + } +} + +func TestDuplicateUsername(t *testing.T) { + user := createUser(t) + + body, err := json.Marshal(map[string]string{ + "username": user.Username, + "display_name": "Someone else", + "password": testPassword, + }) + if err != nil { + t.Fatalf("marshal body: %v", err) + } + + resp := doRequest(t, http.MethodPost, "/api/v1/users", "", body) + requireStatus(t, resp, http.StatusConflict) + + var apiErr dto.ErrorResponse + mustDecode(t, resp, &apiErr) + + if apiErr.Error.Code != dto.ErrorCodeConflict { + t.Errorf("expected code %q, got %q", dto.ErrorCodeConflict, apiErr.Error.Code) + } +} + +func TestPagination(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + + const total = 25 + const pageSize = 4 + + sent := make(map[string]bool, total) + for i := 0; i < total; i++ { + resp := sendMessage(t, user, chat.ID, "msg") + requireStatus(t, resp, http.StatusCreated) + + var msg dto.MessageResponse + mustDecode(t, resp, &msg) + sent[msg.ID.String()] = true + } + + seen := make(map[string]bool, total) + cursor := "" + pages := 0 + + for { + path := "/api/v1/chats/" + chat.ID.String() + "/messages?limit=" + strconv.Itoa(pageSize) + if cursor != "" { + path += "&cursor=" + url.QueryEscape(cursor) + } + + resp := doRequest(t, http.MethodGet, path, user.Token, nil) + requireStatus(t, resp, http.StatusOK) + + var page dto.MessagePageResponse + mustDecode(t, resp, &page) + pages++ + + for _, msg := range page.Items { + id := msg.ID.String() + if seen[id] { + t.Fatalf("duplicate message id across pages: %s", id) + } + seen[id] = true + } + + if page.NextCursor == nil { + break + } + cursor = *page.NextCursor + + if pages > total { + t.Fatalf("too many pages: got %d, expected at most %d", pages, total) + } + } + + if len(seen) != total { + t.Fatalf("expected %d unique messages across all pages, got %d", total, len(seen)) + } + for id := range sent { + if !seen[id] { + t.Fatalf("message %s sent but never seen while paginating", id) + } + } +} diff --git a/student-tests/dependency_failure_test.go b/student-tests/dependency_failure_test.go new file mode 100644 index 00000000..59d7ba1c --- /dev/null +++ b/student-tests/dependency_failure_test.go @@ -0,0 +1,156 @@ +//go:build failure + +package studenttests_test + +import ( + "bytes" + "io" + "net/http" + "os/exec" + "testing" + "time" + + "mtusi2026-messenger/internal/dto" +) + +func compose(t *testing.T, args ...string) { + t.Helper() + + cmd := exec.Command("docker", append([]string{"compose"}, args...)...) + cmd.Dir = ".." + if out, err := cmd.CombinedOutput(); err != nil { + t.Fatalf("docker compose %v: %v\n%s", args, err, out) + } +} + +func stopService(t *testing.T, name string) { + t.Helper() + + compose(t, "stop", name) + t.Cleanup(func() { + compose(t, "start", name) + waitReady(t) + }) +} + +func waitReady(t *testing.T) { + t.Helper() + + deadline := time.Now().Add(60 * time.Second) + for time.Now().Before(deadline) { + resp, err := httpClient.Get(*baseURL + "/health/ready") + if err == nil { + resp.Body.Close() + if resp.StatusCode == http.StatusOK { + return + } + } + time.Sleep(500 * time.Millisecond) + } + t.Fatal("service did not become ready in 60s") +} + +func TestRedisDown(t *testing.T) { + user := createUser(t) + + stopService(t, "redis") + + // настоящий токен + resp := doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil) + requireStatus(t, resp, http.StatusOK) + resp.Body.Close() + + // выдуманный токен + resp = doRequest(t, http.MethodGet, "/api/v1/users/me", "forged-"+user.Token, nil) + requireStatus(t, resp, http.StatusUnauthorized) + resp.Body.Close() + + // без токена + resp = doRequest(t, http.MethodGet, "/api/v1/users/me", "", nil) + requireStatus(t, resp, http.StatusUnauthorized) + resp.Body.Close() +} + +func TestDatabaseDown(t *testing.T) { + user := createUser(t) // токен ещё не использован, ключа в Redis нет + + stopService(t, "postgres") + + // ключа в кэше нет, проверить сессию негде + resp := doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil) + requireStatus(t, resp, http.StatusServiceUnavailable) + resp.Body.Close() + + // выдуманный токен + resp = doRequest(t, http.MethodGet, "/api/v1/users/me", "forged-"+user.Token, nil) + requireStatus(t, resp, http.StatusServiceUnavailable) + resp.Body.Close() + + // без токена + resp = doRequest(t, http.MethodGet, "/api/v1/users/me", "", nil) + requireStatus(t, resp, http.StatusUnauthorized) + resp.Body.Close() +} + +func TestS3Down(t *testing.T) { + user := createUser(t) + chat := createChat(t, user, user.ID) + original := makePNG(t, 32, 32) + att := uploadPNG(t, user, chat.ID) + + stopService(t, "s3") + for _, path := range []string{"/health/ready", "/api/v1/attachments/" + att.ID.String() + "/content"} { + resp := doRequest(t, http.MethodGet, path, user.Token, nil) + requireStatus(t, resp, http.StatusServiceUnavailable) + requireErrorCode(t, resp, dto.ErrorCodeUnavailable) + } + resp := uploadAttachment(t, user, chat.ID, "outage.png", "image/png", original) + requireStatus(t, resp, http.StatusServiceUnavailable) + requireErrorCode(t, resp, dto.ErrorCodeUnavailable) + resp = doRequest(t, http.MethodGet, "/health/live", "", nil) + requireStatus(t, resp, http.StatusOK) + resp.Body.Close() + + compose(t, "start", "s3") + waitReady(t) + deadline := time.Now().Add(60 * time.Second) + downloaded, uploaded := false, false + for time.Now().Before(deadline) && (!downloaded || !uploaded) { + if !downloaded { + req, err := http.NewRequest(http.MethodGet, *baseURL+"/api/v1/attachments/"+att.ID.String()+"/content", nil) + if err != nil { + t.Fatal(err) + } + req.Header.Set("Authorization", "Bearer "+user.Token) + response, err := httpClient.Do(req) + if err == nil { + data, readErr := io.ReadAll(response.Body) + response.Body.Close() + if response.StatusCode == http.StatusOK && readErr == nil { + if !bytes.Equal(data, original) { + t.Fatal("original changed across S3 outage") + } + downloaded = true + } else if response.StatusCode != http.StatusServiceUnavailable && response.StatusCode != http.StatusBadGateway && response.StatusCode != http.StatusGatewayTimeout { + t.Fatalf("unexpected download recovery status: %d", response.StatusCode) + } + } + } + if !uploaded { + response := uploadAttachment(t, user, chat.ID, "recovery.png", "image/png", original) + status := response.StatusCode + response.Body.Close() + if status == http.StatusCreated { + uploaded = true + } else if status != http.StatusServiceUnavailable { + t.Fatalf("unexpected upload recovery status: %d", status) + } + } + if !downloaded || !uploaded { + time.Sleep(500 * time.Millisecond) + } + } + if !downloaded || !uploaded { + t.Fatalf("S3 did not recover in 60s: download=%t upload=%t", downloaded, uploaded) + } +} diff --git a/student-tests/helpers_test.go b/student-tests/helpers_test.go new file mode 100644 index 00000000..91290f2a --- /dev/null +++ b/student-tests/helpers_test.go @@ -0,0 +1,236 @@ +package studenttests_test + +import ( + "bytes" + "encoding/json" + "flag" + "image" + "image/color" + "image/png" + "io" + "net/http" + "strings" + "testing" + "time" + + "github.com/google/uuid" + + "mtusi2026-messenger/internal/dto" +) + +var baseURL = flag.String("base-url", "http://127.0.0.1:8080", "base URL of the running server") + +var httpClient = &http.Client{ + Timeout: 10 * time.Second, + Transport: &http.Transport{ + MaxIdleConns: 200, + IdleConnTimeout: 30 * time.Second, + MaxIdleConnsPerHost: 200, + MaxConnsPerHost: 200, + }, +} + +type testUser struct { + dto.UserResponse + Token string +} + +const testPassword = "correct-horse-battery" + +func doRequest(t *testing.T, method, path, token string, body []byte) *http.Response { + t.Helper() + + req, err := http.NewRequest(method, *baseURL+path, bytes.NewReader(body)) + if err != nil { + t.Fatalf("build request: %v", err) + } + + if body != nil { + req.Header.Set("Content-Type", "application/json") + } + + if token != "" { + req.Header.Set("Authorization", "Bearer "+token) + } + + resp, err := httpClient.Do(req) + if err != nil { + t.Fatalf("%s %s: request failed: %v", method, path, err) + } + + return resp +} + +func mustDecode(t *testing.T, resp *http.Response, dst any) { + t.Helper() + defer resp.Body.Close() + + data, err := io.ReadAll(resp.Body) + if err != nil { + t.Fatalf("read body: %v", err) + } + + if err := json.Unmarshal(data, dst); err != nil { + t.Fatalf("decode body %q: %v", data, err) + } +} + +func requireStatus(t *testing.T, resp *http.Response, expected int) { + t.Helper() + + if resp.StatusCode != expected { + data, _ := io.ReadAll(resp.Body) + resp.Body.Close() + + t.Fatalf( + "expected status %d, got %d, body: %s", + expected, + resp.StatusCode, + data, + ) + } + + if resp.Header.Get("X-Request-Id") == "" { + t.Errorf("missing X-Request-Id header") + } +} + +func createUser(t *testing.T) testUser { + t.Helper() + + username := "u_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:12] + + body, err := json.Marshal(map[string]string{ + "username": username, + "display_name": "Test " + username, + "password": testPassword, + }) + if err != nil { + t.Fatalf("marshal user: %v", err) + } + + resp := doRequest(t, http.MethodPost, "/api/v1/users", "", body) + requireStatus(t, resp, http.StatusCreated) + + var u dto.UserResponse + mustDecode(t, resp, &u) + + req, err := http.NewRequest( + http.MethodPost, + *baseURL+"/api/v1/auth/sessions", + nil, + ) + if err != nil { + t.Fatalf("build login request: %v", err) + } + req.SetBasicAuth(username, testPassword) + + resp, err = httpClient.Do(req) + if err != nil { + t.Fatalf("login request failed: %v", err) + } + requireStatus(t, resp, http.StatusCreated) + + var session dto.SessionResponse + mustDecode(t, resp, &session) + + return testUser{UserResponse: u, Token: session.Token} +} + +func createChat( + t *testing.T, + creator testUser, + memberIDs ...uuid.UUID, +) dto.ChatResponse { + t.Helper() + + body, err := json.Marshal(map[string]any{ + "title": "Test chat " + uuid.NewString()[:8], + "member_ids": memberIDs, + }) + if err != nil { + t.Fatalf("marshal chat: %v", err) + } + + resp := doRequest( + t, + http.MethodPost, + "/api/v1/chats", + creator.Token, + body, + ) + + requireStatus(t, resp, http.StatusCreated) + + var c dto.ChatResponse + mustDecode(t, resp, &c) + + return c +} + +func sendMessage( + t *testing.T, + actor testUser, + chatID uuid.UUID, + text string, +) *http.Response { + t.Helper() + + resp, err := sendLoadMessage(actor, chatID, text) + if err != nil { + t.Fatalf("send message: %v", err) + } + + return resp +} + +func sendLoadMessage( + actor testUser, + chatID uuid.UUID, + text string, +) (*http.Response, error) { + body, err := json.Marshal(map[string]string{ + "text": text, + }) + if err != nil { + return nil, err + } + + req, err := http.NewRequest( + http.MethodPost, + *baseURL+"/api/v1/chats/"+chatID.String()+"/messages", + bytes.NewReader(body), + ) + if err != nil { + return nil, err + } + + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+actor.Token) + req.Header.Set("Idempotency-Key", uuid.NewString()) + + return httpClient.Do(req) +} + +func makePNG(t *testing.T, width, height int) []byte { + t.Helper() + + img := image.NewRGBA(image.Rect(0, 0, width, height)) + for y := 0; y < height; y++ { + for x := 0; x < width; x++ { + img.Set(x, y, color.RGBA{ + R: uint8((x * 7) % 256), + G: uint8((y * 13) % 256), + B: uint8((x + y) % 256), + A: 255, + }) + } + } + + var buf bytes.Buffer + if err := png.Encode(&buf, img); err != nil { + t.Fatalf("encode png: %v", err) + } + + return buf.Bytes() +} diff --git a/student-tests/idempotency_test.go b/student-tests/idempotency_test.go new file mode 100644 index 00000000..30d42675 --- /dev/null +++ b/student-tests/idempotency_test.go @@ -0,0 +1,169 @@ +//go:build race + +package studenttests_test + +import ( + "bytes" + "encoding/json" + "io" + "net/http" + "net/url" + "sync" + "testing" + + "github.com/google/uuid" +) + +const ( + concurrentRequests = 20 + concurrentRounds = 5 +) + +type keyedResult struct { + status int + instance string + body []byte + err error +} + +func postWithKey(actor testUser, chatID uuid.UUID, key, text string) keyedResult { + body, err := json.Marshal(map[string]string{"text": text}) + if err != nil { + return keyedResult{err: err} + } + + req, err := http.NewRequest( + http.MethodPost, + *baseURL+"/api/v1/chats/"+chatID.String()+"/messages", + bytes.NewReader(body), + ) + if err != nil { + return keyedResult{err: err} + } + req.Header.Set("Content-Type", "application/json") + req.Header.Set("Authorization", "Bearer "+actor.Token) + req.Header.Set("Idempotency-Key", key) + + resp, err := httpClient.Do(req) + if err != nil { + return keyedResult{err: err} + } + defer resp.Body.Close() + + data, err := io.ReadAll(resp.Body) + return keyedResult{ + status: resp.StatusCode, + instance: resp.Header.Get("X-Instance-Id"), + body: data, + err: err, + } +} + +func TestConcurrentIdempotencyAcrossReplicas(t *testing.T) { + alice := createUser(t) + bob := createUser(t) + chat := createChat(t, alice, alice.ID, bob.ID) + + instances := map[string]struct{}{} + + for round := 0; round < concurrentRounds; round++ { + key := uuid.NewString() + text := "concurrent " + key + + results := make([]keyedResult, concurrentRequests) + start := make(chan struct{}) + var wg sync.WaitGroup + + for i := range results { + wg.Add(1) + go func(i int) { + defer wg.Done() + <-start + results[i] = postWithKey(alice, chat.ID, key, text) + }(i) + } + close(start) + wg.Wait() + + createdIDs := map[string]struct{}{} + succeeded := 0 + + for i, r := range results { + if r.err != nil { + t.Fatalf("round %d, request %d: %v", round, i, r.err) + } + if r.instance != "" { + instances[r.instance] = struct{}{} + } + + switch r.status { + case http.StatusOK, http.StatusCreated: + succeeded++ + var m struct { + ID string `json:"id"` + } + if err := json.Unmarshal(r.body, &m); err != nil || m.ID == "" { + t.Fatalf("round %d: bad success body %q: %v", round, r.body, err) + } + createdIDs[m.ID] = struct{}{} + case http.StatusConflict: + default: + t.Errorf("round %d, request %d: unexpected status %d, body: %s", + round, i, r.status, r.body) + } + } + + if succeeded == 0 { + t.Fatalf("round %d: no request succeeded", round) + } + if len(createdIDs) != 1 { + t.Fatalf("round %d: responses returned %d different message ids, want 1: %v", + round, len(createdIDs), createdIDs) + } + + if got := countMessagesWithText(t, bob, chat.ID, text); got != 1 { + t.Fatalf("round %d: %d messages with text %q in history, want exactly 1", + round, got, text) + } + } + + if len(instances) < 2 { + t.Fatalf("requests were served by %d replica(s) %v, need at least 2", + len(instances), instances) + } + t.Logf("served by replicas: %v", instances) +} + +func countMessagesWithText(t *testing.T, actor testUser, chatID uuid.UUID, text string) int { + t.Helper() + + count := 0 + path := "/api/v1/chats/" + chatID.String() + "/messages?limit=100" + + for page := 0; page < 100; page++ { + resp := doRequest(t, http.MethodGet, path, actor.Token, nil) + requireStatus(t, resp, http.StatusOK) + + var body struct { + Items []struct { + Text string `json:"text"` + } `json:"items"` + NextCursor *string `json:"next_cursor"` + } + mustDecode(t, resp, &body) + + for _, m := range body.Items { + if m.Text == text { + count++ + } + } + if body.NextCursor == nil { + return count + } + path = "/api/v1/chats/" + chatID.String() + + "/messages?limit=100&cursor=" + url.QueryEscape(*body.NextCursor) + } + + t.Fatalf("history pagination exceeded safety bound") + return 0 +} diff --git a/student-tests/load_action_test.go b/student-tests/load_action_test.go new file mode 100644 index 00000000..1e387472 --- /dev/null +++ b/student-tests/load_action_test.go @@ -0,0 +1,150 @@ +//go:build load && action + +package studenttests_test + +import ( + "flag" + "io" + "net/http" + "os" + "os/exec" + "strings" + "sync" + "testing" + "time" +) + +var ( + actionScript = flag.String("action", "", "script to run during the load (e.g. scripts/stop-one.sh)") + actionAt = flag.Duration("action-at", 10*time.Second, "when to run the action, from test start") + replica = flag.Int("replica", 1, "which api replica to stop (1-based)") +) + +func resolveReplica(t *testing.T, n int) string { + t.Helper() + out, err := exec.Command("docker", "compose", "ps", "-q", "api").Output() + if err != nil { + t.Fatalf("docker compose ps: %v", err) + } + ids := strings.Fields(string(out)) + if n < 1 || n > len(ids) { + t.Fatalf("replica %d requested, but %d api containers found", n, len(ids)) + } + return ids[n-1] +} + +func actionCommand(path string) *exec.Cmd { + if strings.HasSuffix(path, ".sh") { + return exec.Command("sh", path) + } + return exec.Command(path) +} + +func TestLoadWithAction(t *testing.T) { + if *actionScript == "" { + t.Skip("-action is not set") + } + + if *actionAt+10*time.Second > *duration { + t.Fatalf("-duration (%s) must exceed -action-at (%s) by at least 10s", *duration, *actionAt) + } + + user1 := createUser(t) + user2 := createUser(t) + chat := createChat(t, user1, user1.ID, user2.ID) + + type sample struct { + at time.Duration + ok bool + } + var ( + mu sync.Mutex + samples []sample + wg sync.WaitGroup + ) + + targetID := resolveReplica(t, *replica) + start := time.Now() + deadline := start.Add(*duration) + interval := time.Second / time.Duration(*rate) + + for w := 0; w < *workers; w++ { + wg.Add(1) + go func(id int) { + defer wg.Done() + actor := user1 + if id%2 == 1 { + actor = user2 + } + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for { + <-ticker.C + if time.Now().After(deadline) { + return + } + resp, err := sendLoadMessage(actor, chat.ID, "load-msg") + ok := err == nil && resp.StatusCode == http.StatusCreated + if err == nil { + io.Copy(io.Discard, resp.Body) + resp.Body.Close() + } + mu.Lock() + samples = append(samples, sample{time.Since(start), ok}) + mu.Unlock() + } + }(w) + } + + var actionStart, actionEnd time.Duration + wg.Add(1) + go func() { + defer wg.Done() + time.Sleep(*actionAt) + actionStart = time.Since(start) + cmd := actionCommand(*actionScript) + cmd.Env = append(os.Environ(), "TARGET_INSTANCE_ID="+targetID) + out, err := cmd.CombinedOutput() + actionEnd = time.Since(start) + if err != nil { + t.Errorf("action failed: %v\n%s", err, out) + } + }() + + wg.Wait() + elapsed := time.Since(start) + + seconds := int(elapsed.Seconds()) + 1 + okBySec := make([]int, seconds) + errBySec := make([]int, seconds) + var okTotal, errTotal int + var lastErr time.Duration + + for _, s := range samples { + sec := int(s.at.Seconds()) + if s.ok { + okBySec[sec]++ + okTotal++ + } else { + errBySec[sec]++ + errTotal++ + if s.at > lastErr { + lastErr = s.at + } + } + } + + for i := 0; i < seconds; i++ { + t.Logf("t=%2ds ok=%4d err=%3d", i, okBySec[i], errBySec[i]) + } + + t.Logf("action: started at %.2fs, finished at %.2fs", actionStart.Seconds(), actionEnd.Seconds()) + t.Logf("total ok=%d errors=%d, successful RPS=%.2f", okTotal, errTotal, float64(okTotal)/elapsed.Seconds()) + + if errTotal == 0 || lastErr < actionStart { + t.Logf("recovery time: 0s (no errors after action start)") + } else { + t.Logf("recovery time: %.2fs (last error at %.2fs)", (lastErr - actionStart).Seconds(), lastErr.Seconds()) + } +} diff --git a/student-tests/load_test.go b/student-tests/load_test.go new file mode 100644 index 00000000..a4a0902e --- /dev/null +++ b/student-tests/load_test.go @@ -0,0 +1,162 @@ +//go:build load + +package studenttests_test + +import ( + "flag" + "fmt" + "io" + "net/http" + "os" + "sort" + "sync" + "sync/atomic" + "testing" + "time" +) + +var ( + workers = flag.Int("workers", 20, "number of concurrent workers") + duration = flag.Duration("duration", 5*time.Second, "test duration") + rate = flag.Int("rate", 20, "maximum requests per second per worker") + csvPath = flag.String("csv", "", "append result row to this CSV file") + label = flag.String("label", "", "configuration label for CSV row") +) + +func TestLoad(t *testing.T) { + var ( + statuses = make(map[int]int64) + requestErrors int64 + latencies []time.Duration + statusMu sync.Mutex + latencyMu sync.Mutex + wg sync.WaitGroup + ) + + user1 := createUser(t) + user2 := createUser(t) + chat := createChat(t, user1, user1.ID, user2.ID) + + start := time.Now() + deadline := start.Add(*duration) + + interval := time.Second / time.Duration(*rate) + + for worker := 0; worker < *workers; worker++ { + wg.Add(1) + + go func(workerID int) { + defer wg.Done() + + actor := user1 + if workerID%2 == 1 { + actor = user2 + } + + ticker := time.NewTicker(interval) + defer ticker.Stop() + + for time.Now().Before(deadline) { + <-ticker.C + + if time.Now().After(deadline) { + return + } + + requestStart := time.Now() + + resp, err := sendLoadMessage( + actor, + chat.ID, + "load-msg", + ) + + latency := time.Since(requestStart) + + if err != nil { + if atomic.AddInt64(&requestErrors, 1) == 1 { + t.Logf("first transport error: %v", err) + } + continue + } + + statusMu.Lock() + statuses[resp.StatusCode]++ + statusMu.Unlock() + + if resp.StatusCode == http.StatusCreated { + latencyMu.Lock() + latencies = append(latencies, latency) + latencyMu.Unlock() + } + + io.Copy(io.Discard, resp.Body) + resp.Body.Close() + } + }(worker) + } + + wg.Wait() + + elapsed := time.Since(start) + + var total int64 + for _, count := range statuses { + total += count + } + total += requestErrors + + t.Logf("duration: %.3fs", elapsed.Seconds()) + t.Logf("workers: %d", *workers) + t.Logf("rate per worker: %d req/s", *rate) + t.Logf("target rate: %d req/s", *workers**rate) + t.Logf("total requests: %d", total) + + for status, count := range statuses { + t.Logf("HTTP %d: %d", status, count) + } + + t.Logf("request errors: %d", requestErrors) + + successful := statuses[http.StatusCreated] + + t.Logf("successful RPS: %.2f", + float64(successful)/elapsed.Seconds(), + ) + + if len(latencies) > 0 { + sort.Slice(latencies, func(i, j int) bool { + return latencies[i] < latencies[j] + }) + + t.Logf("successful latency p50: %s", percentile(latencies, 0.50)) + t.Logf("successful latency p95: %s", percentile(latencies, 0.95)) + t.Logf("successful latency p99: %s", percentile(latencies, 0.99)) + } + + if *csvPath != "" { + var p50, p95, p99 time.Duration + if len(latencies) > 0 { + p50 = percentile(latencies, 0.50) + p95 = percentile(latencies, 0.95) + p99 = percentile(latencies, 0.99) + } + f, err := os.OpenFile(*csvPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0o644) + if err != nil { + t.Fatalf("open csv: %v", err) + } + defer f.Close() + fmt.Fprintf(f, "%s,%d,%d,%d,%d,%d,%d,%.2f,%.1f,%.1f,%.1f\n", + *label, *workers, *rate, *workers**rate, + successful, total-successful-requestErrors, requestErrors, + float64(successful)/elapsed.Seconds(), + float64(p50.Microseconds())/1000, + float64(p95.Microseconds())/1000, + float64(p99.Microseconds())/1000) + } +} + +func percentile(values []time.Duration, p float64) time.Duration { + index := int(float64(len(values)-1) * p) + return values[index] +} diff --git a/student-tests/race_test.go b/student-tests/race_test.go new file mode 100644 index 00000000..3a4384dc --- /dev/null +++ b/student-tests/race_test.go @@ -0,0 +1,100 @@ +//go:build race + +package studenttests_test + +import ( + "net/http" + "sync" + "testing" + + "mtusi2026-messenger/internal/dto" +) + +func TestConcurrentMessages(t *testing.T) { + const clients = 10 + const messagesPerClient = 10 + const total = clients * messagesPerClient + + user1 := createUser(t) + user2 := createUser(t) + chat := createChat(t, user1, user1.ID, user2.ID) + + var wg sync.WaitGroup + var mu sync.Mutex + sentIDs := make(map[string]bool, total) + + start := make(chan struct{}) + + for worker := 0; worker < clients; worker++ { + wg.Add(1) + + go func(workerID int) { + defer wg.Done() + + actor := user1 + if workerID%2 == 1 { + actor = user2 + } + + <-start + + for i := 0; i < messagesPerClient; i++ { + resp := sendMessage(t, actor, chat.ID, "race-msg") + if resp.StatusCode != http.StatusCreated { + t.Errorf("worker %d msg %d: expected 201, got %d", workerID, i, resp.StatusCode) + resp.Body.Close() + continue + } + + var m dto.MessageResponse + mustDecode(t, resp, &m) + + mu.Lock() + sentIDs[m.ID.String()] = true + mu.Unlock() + } + }(worker) + } + + close(start) + wg.Wait() + + if t.Failed() { + t.Fatal("one or more send requests failed, see errors above") + } + + if len(sentIDs) != total { + t.Fatalf("expected %d successful sends, got %d unique ids", total, len(sentIDs)) + } + + seen := make(map[string]bool, total) + + path := "/api/v1/chats/" + chat.ID.String() + "/messages?limit=100" + resp := doRequest(t, http.MethodGet, path, user1.Token, nil) + requireStatus(t, resp, http.StatusOK) + + var page dto.MessagePageResponse + mustDecode(t, resp, &page) + + if page.NextCursor != nil { + t.Fatalf("expected next_cursor to be nil, history may contain more than %d messages", total) + } + + for _, msg := range page.Items { + id := msg.ID.String() + if seen[id] { + t.Fatalf("duplicate message id in history: %s", id) + } + if !sentIDs[id] { + t.Fatalf("message %s in history was never sent by this test", id) + } + seen[id] = true + } + + if len(seen) != total { + t.Fatalf("expected exactly %d unique messages in history, got %d", total, len(seen)) + } + + t.Logf("PASS: %d concurrent clients sent %d messages, exactly %d unique messages persisted", + clients, total, len(seen)) +} diff --git a/student-tests/reconcile_test.go b/student-tests/reconcile_test.go new file mode 100644 index 00000000..a95234c4 --- /dev/null +++ b/student-tests/reconcile_test.go @@ -0,0 +1,169 @@ +//go:build reconcile + +package studenttests_test + +import ( + "bytes" + "context" + "encoding/json" + "io" + "net/http" + "os" + "os/exec" + "slices" + "strings" + "testing" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + + "mtusi2026-messenger/internal/reconcile" +) + +func reconcileDB(t *testing.T) *pgx.Conn { + t.Helper() + secret, err := os.ReadFile("../secrets/test_database_url.txt") + if err != nil { + t.Fatalf("read test database URL: %v", err) + } + conn, err := pgx.Connect(context.Background(), strings.TrimSpace(string(secret))) + if err != nil { + t.Fatalf("connect to test database: %v", err) + } + t.Cleanup(func() { conn.Close(context.Background()) }) + return conn +} + +func reconcileSQL(t *testing.T, conn *pgx.Conn, query string, args ...any) { + t.Helper() + if _, err := conn.Exec(context.Background(), query, args...); err != nil { + t.Fatalf("prepare/check reconcile fixture: %v", err) + } +} + +func runReconcile(t *testing.T, dryRun bool, grace string) reconcile.Report { + t.Helper() + ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute) + defer cancel() + dry := "false" + if dryRun { + dry = "true" + } + cmd := exec.CommandContext(ctx, "docker", "compose", "--env-file", ".env", "run", "--rm", "--no-deps", "reconcile", "-json", "-dry-run="+dry, "-grace="+grace) + cmd.Dir = ".." + var stderr bytes.Buffer + cmd.Stderr = &stderr + output, err := cmd.Output() + if err != nil { + t.Fatalf("run reconciler: %v\n%s", err, stderr.String()) + } + var report reconcile.Report + if err := json.Unmarshal(output, &report); err != nil { + t.Fatalf("decode reconcile report: %v\n%s", err, output) + } + t.Logf("dry_run=%t objects=%d rows=%d list_pages=%d duration_ms=%d orphan=%d missing=%d stale=%d skipped_fresh=%d skipped_referenced=%d deleted_objects=%d deleted_rows=%d marked_failed=%d errors=%v", report.DryRun, report.ScannedObjects, report.ScannedRows, report.ListPages, report.DurationMS, len(report.OrphanObjects), len(report.MissingAttachments), len(report.StaleUploads), report.SkippedFresh, report.SkippedReferenced, report.DeletedObjects, report.DeletedRows, report.MarkedFailed, report.Errors) + if len(report.Errors) != 0 { + t.Fatal("reconciler reported errors") + } + return report +} + +func TestReconcileRepair(t *testing.T) { + conn := reconcileDB(t) + user := createUser(t) + chat := createChat(t, user, user.ID) + att := uploadPNG(t, user, chat.ID) + var originalKey string + if err := conn.QueryRow(context.Background(), "SELECT s3_key FROM attachments WHERE id=$1", att.ID).Scan(&originalKey); err != nil { + t.Fatal(err) + } + // Simulate lost metadata for the original object and metadata pointing at a missing object. + reconcileSQL(t, conn, "UPDATE attachments SET s3_key=$2 WHERE id=$1", att.ID, "chats/reconcile-missing/"+uuid.NewString()) + // Only this experiment uses a shortened grace period, to avoid waiting an hour. + time.Sleep(1100 * time.Millisecond) + dry := runReconcile(t, true, "1s") + if !slices.Contains(dry.OrphanObjects, originalKey) || !slices.Contains(dry.MissingAttachments, att.ID) { + t.Fatal("dry-run did not detect both injected inconsistencies") + } + if dry.DeletedObjects != 0 || dry.DeletedRows != 0 || dry.MarkedFailed != 0 { + t.Fatal("dry-run changed data") + } + var status, code string + if err := conn.QueryRow(context.Background(), "SELECT status::text FROM attachments WHERE id=$1", att.ID).Scan(&status); err != nil { + t.Fatal(err) + } + if status != "ready" { + t.Fatalf("dry-run changed status: %s", status) + } + repair := runReconcile(t, false, "1s") + if repair.DeletedObjects < 1 || repair.MarkedFailed < 1 { + t.Fatal("repair did not apply fixes") + } + if err := conn.QueryRow(context.Background(), "SELECT status::text, error_code FROM attachments WHERE id=$1", att.ID).Scan(&status, &code); err != nil { + t.Fatal(err) + } + if status != "failed" || code != "object_missing" { + t.Fatalf("unexpected repaired state: %s %s", status, code) + } + again := runReconcile(t, false, "1s") + if slices.Contains(again.OrphanObjects, originalKey) || slices.Contains(again.MissingAttachments, att.ID) { + t.Fatal("fixture still requires repair on repeat") + } + if again.DeletedObjects != 0 || again.DeletedRows != 0 || again.MarkedFailed != 0 { + t.Fatal("repeat repair changed data; run on an idle test stand") + } + t.Log("PASS: dry-run preserved data; orphan deleted; missing marked failed; repeat made no changes") +} + +func TestReconcileSafety(t *testing.T) { + conn := reconcileDB(t) + user := createUser(t) + chat := createChat(t, user, user.ID) + fresh := uploadPNG(t, user, chat.ID) + used := uploadPNG(t, user, chat.ID) + resp := postMessage(t, user, chat.ID, "", []uuid.UUID{used.ID}) + requireStatus(t, resp, http.StatusCreated) + resp.Body.Close() + // Exercise both grace-period protection and protection of referenced queued uploads. + reconcileSQL(t, conn, "UPDATE attachments SET status='queued' WHERE id=$1", fresh.ID) + reconcileSQL(t, conn, "UPDATE attachments SET status='queued', created_at=now()-interval '2 hours' WHERE id=$1", used.ID) + t.Cleanup(func() { + if _, err := conn.Exec(context.Background(), "UPDATE attachments SET status='ready' WHERE id=ANY($1::uuid[])", []uuid.UUID{fresh.ID, used.ID}); err != nil { + t.Errorf("restore fixture status: %v", err) + } + }) + for _, dryRun := range []bool{true, false} { + report := runReconcile(t, dryRun, "1h") + if report.SkippedFresh < 1 || report.SkippedReferenced < 1 { + t.Fatal("fresh/referenced protection was not exercised") + } + for _, att := range []uuid.UUID{fresh.ID, used.ID} { + var status string + if err := conn.QueryRow(context.Background(), "SELECT status::text FROM attachments WHERE id=$1", att).Scan(&status); err != nil { + t.Fatal(err) + } + if status != "queued" { + t.Fatalf("protected fixture changed status: %s", status) + } + resp := doRequest(t, http.MethodGet, "/api/v1/attachments/"+att.String()+"/content", user.Token, nil) + requireStatus(t, resp, http.StatusOK) + data, err := io.ReadAll(resp.Body) + resp.Body.Close() + if err != nil { + t.Fatal(err) + } + if !bytes.Equal(data, makePNG(t, 32, 32)) { + t.Fatal("protected original changed") + } + } + } + t.Log("PASS: fresh and old referenced uploads preserved, originals unchanged") +} + +func TestReconcileCost(t *testing.T) { + for i := 1; i <= 3; i++ { + t.Logf("scan %d/3", i) + runReconcile(t, true, "1h") + } +} diff --git a/student-tests/registration_race_test.go b/student-tests/registration_race_test.go new file mode 100644 index 00000000..1f49203f --- /dev/null +++ b/student-tests/registration_race_test.go @@ -0,0 +1,77 @@ +//go:build race + +package studenttests_test + +import ( + "bytes" + "encoding/json" + "net/http" + "strings" + "sync" + "testing" + + "github.com/google/uuid" +) + +func TestConcurrentRegistration(t *testing.T) { + const workers = 20 + + username := "u_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:12] + + body, err := json.Marshal(map[string]string{ + "username": username, + "display_name": "Racer", + "password": testPassword, + }) + if err != nil { + t.Fatalf("marshal body: %v", err) + } + + start := make(chan struct{}) + statuses := make(chan int, workers) + + var wg sync.WaitGroup + for i := 0; i < workers; i++ { + wg.Add(1) + go func() { + defer wg.Done() + <-start + + req, err := http.NewRequest(http.MethodPost, *baseURL+"/api/v1/users", bytes.NewReader(body)) + if err != nil { + t.Errorf("build request: %v", err) + return + } + req.Header.Set("Content-Type", "application/json") + + resp, err := httpClient.Do(req) + if err != nil { + t.Errorf("request failed: %v", err) + return + } + defer resp.Body.Close() + + statuses <- resp.StatusCode + }() + } + + close(start) + wg.Wait() + close(statuses) + + created, conflict := 0, 0 + for s := range statuses { + switch s { + case http.StatusCreated: + created++ + case http.StatusConflict: + conflict++ + default: + t.Errorf("unexpected status %d", s) + } + } + + if created != 1 || conflict != workers-1 { + t.Fatalf("expected 1 created and %d conflict, got %d created and %d conflict", workers-1, created, conflict) + } +} diff --git a/student-tests/session_expiry_test.go b/student-tests/session_expiry_test.go new file mode 100644 index 00000000..99eb098c --- /dev/null +++ b/student-tests/session_expiry_test.go @@ -0,0 +1,42 @@ +//go:build failure + +package studenttests_test + +import ( + "net/http" + "os/exec" + "testing" + "time" +) + +const shortTTLContainer = "api-short-ttl" +const shortTTLURL = "http://localhost:8081" + +func TestSessionExpiry(t *testing.T) { + _ = exec.Command("docker", "rm", "-f", shortTTLContainer).Run() + + compose(t, "run", "-d", "--no-deps", "--name", shortTTLContainer, + "-e", "SESSION_TTL_SECONDS=3", + "-p", "127.0.0.1:8081:8080", "api") + t.Cleanup(func() { + _ = exec.Command("docker", "rm", "-f", shortTTLContainer).Run() + }) + + old := *baseURL + *baseURL = shortTTLURL + t.Cleanup(func() { *baseURL = old }) + + waitReady(t) + + user := createUser(t) + + resp := doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil) + requireStatus(t, resp, http.StatusOK) + resp.Body.Close() + + time.Sleep(4 * time.Second) + + resp = doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil) + requireStatus(t, resp, http.StatusUnauthorized) + resp.Body.Close() +} diff --git a/student-tests/session_test.go b/student-tests/session_test.go new file mode 100644 index 00000000..d72097ef --- /dev/null +++ b/student-tests/session_test.go @@ -0,0 +1,58 @@ +//go:build contract + +package studenttests_test + +import ( + "net/http" + "testing" +) + +func basicLogin(t *testing.T, username, password string) *http.Response { + t.Helper() + + req, err := http.NewRequest(http.MethodPost, *baseURL+"/api/v1/auth/sessions", nil) + if err != nil { + t.Fatalf("build login request: %v", err) + } + req.SetBasicAuth(username, password) + + resp, err := httpClient.Do(req) + if err != nil { + t.Fatalf("login request failed: %v", err) + } + return resp +} + +func TestLoginWrongPassword(t *testing.T) { + user := createUser(t) + + resp := basicLogin(t, user.Username, "wrong-"+testPassword) + requireStatus(t, resp, http.StatusUnauthorized) + resp.Body.Close() + + resp = basicLogin(t, "no_such_user_"+user.Username, testPassword) + requireStatus(t, resp, http.StatusUnauthorized) + resp.Body.Close() +} + +func TestLogout(t *testing.T) { + user := createUser(t) + + resp := doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil) + requireStatus(t, resp, http.StatusOK) + resp.Body.Close() + + resp = doRequest(t, http.MethodDelete, "/api/v1/auth/sessions/current", user.Token, nil) + if resp.StatusCode >= 300 { + t.Fatalf("logout: unexpected status %d", resp.StatusCode) + } + resp.Body.Close() + + resp = doRequest(t, http.MethodGet, "/api/v1/users/me", user.Token, nil) + requireStatus(t, resp, http.StatusUnauthorized) + resp.Body.Close() + + resp = doRequest(t, http.MethodDelete, "/api/v1/auth/sessions/current", user.Token, nil) + requireStatus(t, resp, http.StatusUnauthorized) + resp.Body.Close() +} diff --git a/student.mk b/student.mk new file mode 100644 index 00000000..6b2f745d --- /dev/null +++ b/student.mk @@ -0,0 +1,44 @@ +LOAD_ARGS = $(if $(LOAD_WORKERS),-workers=$(LOAD_WORKERS),) \ + $(if $(LOAD_DURATION),-duration=$(LOAD_DURATION),) \ + $(if $(LOAD_RATE),-rate=$(LOAD_RATE),) \ + $(if $(LOAD_CSV),-csv=$(LOAD_CSV),) \ + $(if $(LOAD_LABEL),-label=$(LOAD_LABEL),) +REPLICA ?= 1 +ACTION ?= stop-one.sh +ACTION_AT ?= 10s +DRY_RUN ?= true + +.PHONY: help-go contract-go race-go load-go failure-go load-action-go reconcile reconcile-test-go + +help-go: + @echo "contract-go: contract tests" + @echo "race-go: concurrency tests (-race)" + @echo "load-go: load test" + @echo "failure-go: dependency failure tests (stops redis/postgres/s3 containers)" + @echo "reconcile: attachment audit; DRY_RUN=false to repair" + +contract-go: + go test -tags=contract ./student-tests/... -v -args -base-url=$(BASE_URL) + +race-go: + go test -tags=race ./student-tests/... -race -v -args -base-url=$(BASE_URL) + +load-go: + go test -count=1 -tags=load ./student-tests/... -v -args -base-url=$(BASE_URL) $(LOAD_ARGS) + +failure-go: + go test -tags=failure ./student-tests/... -v -args -base-url=$(BASE_URL) + +load-action-go: + go test -tags=load,action ./student-tests/... -run TestLoadWithAction -v -args \ + -base-url=$(BASE_URL) $(LOAD_ARGS) \ + -replica=$(REPLICA) \ + -action=$(CURDIR)/scripts/$(ACTION) \ + -action-at=$(ACTION_AT) + +reconcile: + docker compose --env-file .env run --rm --no-deps reconcile -dry-run=$(DRY_RUN) + +reconcile-test-go: + docker compose --env-file .env build reconcile + go test -count=1 -tags=reconcile ./student-tests/... -v -args -base-url=$(BASE_URL) diff --git a/web/swagger/index.html b/web/swagger/index.html new file mode 100644 index 00000000..c498b1bf --- /dev/null +++ b/web/swagger/index.html @@ -0,0 +1,21 @@ + + + + + MTUSI Messenger API + + + +
+ + + + + \ No newline at end of file