forked from mtusi2026/mtusi2026lab4
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6960d9bfe9 | ||
|
|
f9444a3965 | ||
|
|
7860d81fa1 |
@@ -16,5 +16,8 @@ backups
|
||||
.venv
|
||||
__pycache__
|
||||
node_modules
|
||||
**/node_modules
|
||||
frontend/dist
|
||||
frontend/.vite
|
||||
evidence
|
||||
.DS_Store
|
||||
|
||||
+6
-2
@@ -1,7 +1,8 @@
|
||||
# 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_INTERNAL_PORT=8080
|
||||
PUBLIC_PORT=8080
|
||||
APP_ENV=lab
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
@@ -11,10 +12,13 @@ SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
S3_ENDPOINT=http://minio: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_OPERATION_TIMEOUT=5s
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
# Keep the Go soft heap limit below the Compose hard limit (256 MiB by default).
|
||||
API_GO_MEMORY_LIMIT=220MiB
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
FROM golang:1.27.1-alpine@sha256:8a5910f31396cd4d89662f56c68b3ae31d374308270a1c3bd96672ee5ed43414 AS builder
|
||||
|
||||
WORKDIR /build
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY cmd/ ./cmd/
|
||||
COPY internal/ ./internal/
|
||||
|
||||
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-w -s" -o /build/server ./cmd/server/main.go
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-w -s" -o /build/reconciler ./cmd/reconciler/main.go
|
||||
|
||||
FROM alpine:3.24@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6
|
||||
|
||||
RUN addgroup -g 1000 appgroup && \
|
||||
adduser -u 1000 -G appgroup -s /bin/sh -D appuser
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=builder /build/server /app/server
|
||||
COPY --from=builder /build/reconciler /app/reconciler
|
||||
|
||||
RUN chown -R appuser:appgroup /app
|
||||
|
||||
USER appuser:appgroup
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=5s --timeout=3s --start-period=5s --retries=10 \
|
||||
CMD wget -qO- http://127.0.0.1:8080/health/ready || exit 1
|
||||
|
||||
|
||||
ENTRYPOINT ["/app/server"]
|
||||
@@ -2,13 +2,17 @@ PYTHON ?= python3
|
||||
BASE_URL ?= http://localhost:8080
|
||||
CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
RECONCILE_GRACE ?= 15m
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
.PHONY: help init-secrets check validate test load test-attachment-boundaries test-attachment-memory test-storage-outage test-minio-policy test-persistence test-failover test-reconcile test-reconcile-scale reconcile-dry-run reconcile-apply
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
|
||||
init-secrets:
|
||||
./scripts/init-secrets.sh
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
|
||||
@@ -21,6 +25,18 @@ test:
|
||||
load:
|
||||
$(PYTHON) tests/load.py --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
test-attachment-memory:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) tests/attachment_memory.py
|
||||
|
||||
test-attachment-boundaries:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) tests/attachment_boundaries.py
|
||||
|
||||
test-storage-outage:
|
||||
BASE_URL="$(BASE_URL)" $(PYTHON) tests/storage_outage.py
|
||||
|
||||
test-minio-policy:
|
||||
./tests/minio_policy.sh
|
||||
|
||||
test-persistence:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
@@ -28,3 +44,15 @@ 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)
|
||||
|
||||
test-reconcile:
|
||||
$(PYTHON) tests/reconcile_integration.py
|
||||
|
||||
test-reconcile-scale:
|
||||
$(PYTHON) tests/reconcile_scale.py
|
||||
|
||||
reconcile-dry-run:
|
||||
docker compose run --rm --entrypoint /app/reconciler api --grace-period "$(RECONCILE_GRACE)"
|
||||
|
||||
reconcile-apply:
|
||||
docker compose run --rm --entrypoint /app/reconciler api --grace-period "$(RECONCILE_GRACE)" --apply
|
||||
|
||||
@@ -1,37 +1,123 @@
|
||||
# Отчёт — лабораторная 4
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Чурилин, БВТ2301
|
||||
- Целевая оценка: **5**, выбран **трек 2 — согласованность PostgreSQL и S3**.
|
||||
- Commit/PR: TODO после публикации.
|
||||
- Использованные источники и библиотеки: Go standard library, `pgx/v5`, `minio-go/v7`, официальный исходный код MinIO и `mc`, Redis Go client. OpenAI Codex использовался для анализа контракта, проектирования, реализации и тестов; все утверждения ниже проверены перечисленными воспроизводимыми командами.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Prerequisites: Docker с Compose v2, GNU Make, Python 3.9+, OpenSSL. Локально проверено в WSL2. MinIO и `mc` собираются из официальных Go-модулей в [infrastructure/minio/Dockerfile](infrastructure/minio/Dockerfile): сервер `RELEASE.2025-10-15T17-29-55Z`, клиент `RELEASE.2025-08-13T08-35-41Z`. Базовые образы закреплены digest.
|
||||
|
||||
```sh
|
||||
cp .env.example .env
|
||||
make init-secrets
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
docker compose --env-file .env ps
|
||||
BASE_URL=http://127.0.0.1:8080 make test
|
||||
python3 -m venv .venv
|
||||
.venv/bin/pip install -r requirements-dev.txt
|
||||
make validate PYTHON=.venv/bin/python
|
||||
```
|
||||
|
||||
`make init-secrets` создаёт недостающие файлы с mode `600` и никогда не перезаписывает существующие значения. Секреты находятся в игнорируемом каталоге `secrets/` и не передаются через переменные окружения контейнера. Если `8080` занят, задаётся `PUBLIC_PORT`, например `PUBLIC_PORT=18080`; внутренний `APP_INTERNAL_PORT=8080` от него не зависит.
|
||||
|
||||
Безопасная остановка сохраняет named volumes:
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env down
|
||||
```
|
||||
|
||||
Намеренное удаление всех учебных данных выполняется только явно:
|
||||
|
||||
```sh
|
||||
docker compose --env-file .env down -v
|
||||
```
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Путь запроса: клиент → HAProxy → две API-реплики → PostgreSQL (источник истины метаданных/сессий) + приватный MinIO bucket (байты); Redis остаётся восстанавливаемым кешем чатов. У каждой API-реплики нет постоянного каталога оригиналов. Во время синхронного upload тело ограниченно копируется в файл OS temp, файл имеет случайное имя, удаляется в `defer` и нужен только для двукратной проверки/потокового S3 PUT.
|
||||
|
||||
Из lab3 сохранены два API-инстанса, HAProxy, PostgreSQL, Redis fallback, bearer-сессии, idempotency сообщений, курсоры, graceful shutdown и persistence/failover hooks. В lab4 добавлены:
|
||||
|
||||
- `Attachment` со статусами `pending/ready/failed`, SHA-256, истинным MIME, размером и размерами изображения;
|
||||
- таблицы `attachments` и `message_attachments`, same-chat foreign keys и deferred constraint «текст или вложение»;
|
||||
- `attachment_ids` во всех слоях Message без нарушения старого JSON (`[]` по умолчанию);
|
||||
- binary `POST`, metadata `GET` и авторизованный streaming `GET /content` без redirect;
|
||||
- общий MinIO volume, idempotent bootstrap приватного bucket и отдельный API user с минимальной policy;
|
||||
- readiness, зависящий от PostgreSQL и MinIO;
|
||||
- one-shot reconciler с dry-run по умолчанию, `--apply`, grace period и условными DB updates.
|
||||
|
||||
Upload возвращает `201 ready` только после последовательности: полное ограниченное чтение → проверка MIME/PNG/JPEG/пикселей и SHA-256 → insert `pending` → успешный S3 PUT → update `ready`. Ошибка PUT удаляет возможный объект и фиксирует `failed`. Ошибка ответа на финальный DB update считается неоднозначной: update мог уже закоммититься, поэтому объект не удаляется. API делает best-effort readback и либо восстанавливает успешный `201`, либо возвращает `503`, сохранив metadata+object для reconciler. Это исключает состояние `ready` без оригинала при потере ответа PostgreSQL.
|
||||
|
||||
Лимиты применяются до неограниченной работы: body читается через `LimitReader(10 MiB + 1)`, `DecodeConfig` проверяет максимум 40 млн пикселей до полного `Decode`, а полный `Decode` проверяет поток/CRC/IDAT/SOS до S3 PUT. Filename не участвует в object key и проверяется на valid UTF-8/blank/path/C0+C1 control characters. Object key формируется сервером как `attachments/{chat_id}/{attachment_id}/original`. На реплику допускаются не более четырёх upload pipeline и одного полного decode одновременно; `GOMEMLIMIT=220MiB` ниже container limit 256 MiB. S3 operation timeout — 5 s, HTTP read/write timeout — 15 s, HAProxy server/client timeout — 30 s, DB query timeout задаётся конфигурацией. Компенсация сначала получает отдельный 1 s budget для DB transition и лишь затем отдельный 1 s budget для best-effort S3 delete.
|
||||
|
||||
Публичный `/health/ready` проверяет PostgreSQL и MinIO и возвращает контрактный JSON `503`, если зависимость недоступна. HAProxy использует отдельный внутренний `/health/lb`, отражающий жизнеспособность/drain процесса, поэтому при общем отказе MinIO запросы не заменяются generic HTML-ошибкой балансировщика, а доходят до API и получают согласованный `503 unavailable`.
|
||||
|
||||
Соответствие ключевых переменных: `PUBLIC_PORT` — host port HAProxy; `APP_INTERNAL_PORT` — порт API в сети Compose; `DATABASE_URL_FILE`, `CURSOR_SIGNING_KEY_FILE`, `S3_ACCESS_KEY_ID_FILE`, `S3_SECRET_ACCESS_KEY_FILE` — Docker secrets; `S3_ENDPOINT=http://minio:9000`; `S3_BUCKET=messenger-attachments`; `S3_OPERATION_TIMEOUT=5s`.
|
||||
|
||||
## Механизм согласованности и трек 5
|
||||
|
||||
Гипотеза: периодический inventory PostgreSQL и MinIO с консервативным grace period способен повторяемо исправлять безопасные orphan/missing состояния, не удаляя свежие объекты и вложения, уже связанные с сообщениями.
|
||||
|
||||
Reconciler сравнивает все `attachments/` objects с метаданными:
|
||||
|
||||
- старый object без metadata удаляется только в apply;
|
||||
- старый `pending` очищает object и условно становится `failed`;
|
||||
- старый `ready` без object условно становится `failed`, только если не используется сообщением;
|
||||
- object у старого `failed` удаляется, только если запись не используется;
|
||||
- свежие записи/objects игнорируются, а referenced missing помечается `manual_repair_required` без мутации;
|
||||
- SQL update берёт row lock, затем отдельным statement на свежем READ COMMITTED snapshot повторно проверяет status, cutoff и отсутствие ссылки; concurrent message/reconciler race либо создаёт ссылку, либо переводит attachment в failed, но не оба состояния одновременно;
|
||||
- object stale-pending удаляется только после успешного условного DB transition, поэтому проигранная гонка приводит к `skipped_concurrent_change`, а не к потере готового оригинала.
|
||||
|
||||
Команды оператора:
|
||||
|
||||
```sh
|
||||
make reconcile-dry-run RECONCILE_GRACE=15m
|
||||
# Просмотреть JSON; затем при согласии:
|
||||
make reconcile-apply RECONCILE_GRACE=15m
|
||||
```
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Доказательство |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Публичный smoke | `BASE_URL=http://127.0.0.1:18080 make test` | PASS: upload/message/download/checksum/outsider 404 | [evidence/lab4-results.md](evidence/lab4-results.md) |
|
||||
| Границы/полная декодировка | `make test-attachment-boundaries` | Ровно 10 MiB и 40 млн пикселей приняты; truncated/corrupt-after-64-KiB отклонены 415; whitespace/C1 filename — 400 | [tests/attachment_boundaries.py](tests/attachment_boundaries.py) |
|
||||
| Failover API | `make test-failover` с указанными в evidence env | 142 успешных записи, 0 ошибок, recovery 2.771 s; файл читается другой репликой | [evidence/lab4-results.md](evidence/lab4-results.md) |
|
||||
| Restart и volumes | `make test-persistence` | Сессия, метаданные и bytes пережили restart PostgreSQL/Redis/MinIO/API/frontend/HAProxy | [scripts/restart.sh](scripts/restart.sh) |
|
||||
| Файловые негативные тесты | `go test ./...` | oversized bytes/pixels, invalid IDAT, truncated PNG/JPEG, corruption after 64 KiB, MIME substitution, unsafe filename, interrupted body, foreign attachment/chat | [internal/application/attachments_test.go](internal/application/attachments_test.go) |
|
||||
| Неоднозначный DB COMMIT | `go test ./internal/application -count=1` | Потеря ответа после commit восстанавливается readback; при одновременном read outage ready metadata сохраняет оригинал | [internal/application/attachments_test.go](internal/application/attachments_test.go) |
|
||||
| Реальный отказ MinIO | `make test-storage-outage` | readiness 503 за 2.004 s, upload 503 за 6.016 s; после recovery прежний original неизменен | [tests/storage_outage.py](tests/storage_outage.py) |
|
||||
| RAM при concurrency ≥10 | `make test-attachment-memory` | 12 × 6,293,028 B, 12/12 success; пики 31,216,107 и 26,214,400 B | [tests/attachment_memory.py](tests/attachment_memory.py) |
|
||||
| Private bucket / API policy | `make test-minio-policy` | Root secrets/env отсутствуют в API; limited user не создаёт bucket; anonymous GET запрещён | [tests/minio_policy.sh](tests/minio_policy.sh) |
|
||||
| Reconciler dry-run/apply/repeat | `make test-reconcile` | 4 issues; 2 objects deleted; 2 metadata failed; repeat 0 mutations; referenced защищён | [tests/reconcile_integration.py](tests/reconcile_integration.py) |
|
||||
| Стоимость обхода | `make test-reconcile-scale` | 100 objects: 9 ms; 1,000 objects: 38 ms core duration | [tests/reconcile_scale.py](tests/reconcile_scale.py) |
|
||||
| Static/race gates | `go test -race ./...`; `go vet ./...`; `make check`; `git diff --check` | PASS | [evidence/lab4-results.md](evidence/lab4-results.md) |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Memory experiment: один прогон, 12 concurrent uploads, каждый PNG 2048×1024 и 6,293,028 bytes; суммарно 75,516,336 bytes. HAProxy распределил запросы по двум репликам. Docker stats опрашивался во время прогона. 12/12 завершились за 0.599 s, наблюдаемые пики — 29.8 и 25.0 MiB, существенно ниже container limit 256 MiB. После измерения отдельный chat prefix и синтетические DB rows удалены.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
Reconciler integration: в отдельном namespace созданы old orphan object, old pending metadata+object, unreferenced ready metadata без object и referenced ready metadata без object. Dry-run нашёл четыре состояния и не мутировал данные. Apply удалил два objects и перевёл две записи в `failed`. Повторный apply сделал ноль мутаций; referenced missing остался ручной задачей. Синтетические данные удалены в `finally`.
|
||||
|
||||
Traversal experiment использовал один и тот же стенд после прогрева, один прогон на размер, отдельный prefix и однобайтовые objects. 100 injected orphans: 109 objects total, 9 ms core и 1,978 ms с запуском one-shot container. 1,000: 1,009 total, 38 ms core и 2,019 ms end-to-end. Это подтверждает повторяемость методики и ожидаемую близкую к линейной стоимость лишь на малом локальном масштабе.
|
||||
|
||||
| Воздействие | Ожидание | Наблюдение | Потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Stop одной API-реплики | Вторая продолжает запись/чтение общего файла | recovery 2.771 s, 142 writes, 0 errors | Нет |
|
||||
| Restart общего стенда | Named volumes сохраняют DB и S3 | persistence PASS | Нет |
|
||||
| S3 PUT error | 503, metadata failed, object отсутствует | Unit injection PASS | Нет ready-данных |
|
||||
| MinIO недоступен | readiness/upload дают JSON 503 до API/LB timeout | 2.004 s / 6.016 s, реальный stop/recovery PASS | Старый original неизменен |
|
||||
| DB finalize response потерян после COMMIT | Readback возвращает ready; при DB outage object не удаляется | Два unit fault-теста PASS | Ready не остаётся без original |
|
||||
| Orphan/missing/stale состояния | Dry-run, безопасный apply, repeat | Integration PASS | Referenced missing не изменён |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- Inventory materializes metadata и object list в RAM; для большого bucket нужны pagination/checkpoints и пакетная выдача issues.
|
||||
- Один reconciler предполагается запускать оператором; перед scheduler нужны PostgreSQL advisory lock/lease и метрики длительности/ошибок.
|
||||
- `referenced_missing_object` намеренно не чинится автоматически: требуется восстановление bytes из backup или операторское решение.
|
||||
- Один-node MinIO и локальные измерения не доказывают HA самого object storage; это учебный стенд.
|
||||
- Experiment содержит по одному измерению на размер без статистических повторов; для production нужны warm-up, серии, p50/p95/p99, CPU/IO и существенно больший inventory.
|
||||
- Оригинал проверяется SHA-256 при upload и клиентом в smoke; каждый download не пересчитывает hash на лету.
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/adapter/objectstore"
|
||||
"mtusi2026lab4/internal/adapter/postgres"
|
||||
"mtusi2026lab4/internal/config"
|
||||
"mtusi2026lab4/internal/database"
|
||||
"mtusi2026lab4/internal/reconcile"
|
||||
)
|
||||
|
||||
func main() {
|
||||
apply := flag.Bool("apply", false, "apply safe repairs; default is dry-run")
|
||||
gracePeriod := flag.Duration("grace-period", 15*time.Minute,
|
||||
"ignore metadata and objects newer than this duration")
|
||||
timeout := flag.Duration("timeout", 2*time.Minute, "maximum reconciliation runtime")
|
||||
flag.Parse()
|
||||
|
||||
if *gracePeriod <= 0 || *timeout <= 0 {
|
||||
log.Fatal("[FATAL] grace-period and timeout must be positive")
|
||||
}
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Invalid configuration: %v", err)
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), *timeout)
|
||||
defer cancel()
|
||||
databasePool, err := database.Open(ctx, cfg)
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] PostgreSQL initialization failed: %v", err)
|
||||
}
|
||||
defer databasePool.Close()
|
||||
metadataStore := postgres.NewPostgreSQLStore(databasePool, cfg.DatabaseQueryTimeout)
|
||||
|
||||
objectStore, err := objectstore.NewMinIOStore(
|
||||
cfg.S3Endpoint,
|
||||
cfg.S3Region,
|
||||
cfg.S3Bucket,
|
||||
cfg.S3AccessKeyID,
|
||||
cfg.S3SecretAccessKey,
|
||||
cfg.S3OperationTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Invalid object storage configuration: %v", err)
|
||||
}
|
||||
if err = objectStore.Ping(ctx); err != nil {
|
||||
log.Fatalf("[FATAL] Object storage unavailable: %v", err)
|
||||
}
|
||||
|
||||
reconciler, err := reconcile.New(metadataStore, objectStore)
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Reconciler initialization failed: %v", err)
|
||||
}
|
||||
result, err := reconciler.Run(ctx, *gracePeriod, *apply)
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Reconciliation failed: %v", err)
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(os.Stdout)
|
||||
encoder.SetIndent("", " ")
|
||||
if err = encoder.Encode(result); err != nil {
|
||||
log.Fatalf("[FATAL] Encode reconciliation result: %v", err)
|
||||
}
|
||||
if !*apply && len(result.Issues) > 0 {
|
||||
fmt.Fprintln(os.Stderr, "dry-run found reconciliation issues; rerun with --apply after review")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
cacheadapter "mtusi2026lab4/internal/adapter/cache"
|
||||
"mtusi2026lab4/internal/adapter/health"
|
||||
"mtusi2026lab4/internal/adapter/objectstore"
|
||||
"mtusi2026lab4/internal/adapter/postgres"
|
||||
redisadapter "mtusi2026lab4/internal/adapter/redis"
|
||||
"mtusi2026lab4/internal/application"
|
||||
"mtusi2026lab4/internal/config"
|
||||
"mtusi2026lab4/internal/database"
|
||||
"mtusi2026lab4/internal/transport/http/handlers"
|
||||
"mtusi2026lab4/internal/transport/http/middleware"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg, err := config.Load()
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Invalid configuration: %v", err)
|
||||
}
|
||||
|
||||
startupContext, startupCancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
cfg.DatabaseConnectTimeout,
|
||||
)
|
||||
|
||||
databasePool, err := database.Open(startupContext, cfg)
|
||||
startupCancel()
|
||||
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] PostgreSQL initialization failed: %v", err)
|
||||
}
|
||||
defer databasePool.Close()
|
||||
|
||||
postgresStore := postgres.NewPostgreSQLStore(
|
||||
databasePool,
|
||||
cfg.DatabaseQueryTimeout,
|
||||
)
|
||||
minioStore, err := objectstore.NewMinIOStore(
|
||||
cfg.S3Endpoint,
|
||||
cfg.S3Region,
|
||||
cfg.S3Bucket,
|
||||
cfg.S3AccessKeyID,
|
||||
cfg.S3SecretAccessKey,
|
||||
cfg.S3OperationTimeout,
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Invalid object storage configuration: %v", err)
|
||||
}
|
||||
storageContext, storageCancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
cfg.S3OperationTimeout,
|
||||
)
|
||||
if err = minioStore.Ping(storageContext); err != nil {
|
||||
log.Printf("[WARN] Object storage unavailable at startup: %v", err)
|
||||
}
|
||||
storageCancel()
|
||||
|
||||
var applicationStore application.Repository = postgresStore
|
||||
|
||||
if cfg.RedisURL != "" {
|
||||
redisChatCache, cacheError := redisadapter.NewRedisChatCache(
|
||||
cfg.RedisURL,
|
||||
cfg.RedisCacheTTL,
|
||||
cfg.RedisOperationTimeout,
|
||||
cfg.RedisPoolSize,
|
||||
)
|
||||
if cacheError != nil {
|
||||
log.Fatalf("[FATAL] Invalid Redis configuration: %v", cacheError)
|
||||
}
|
||||
defer func() {
|
||||
if closeError := redisChatCache.Close(); closeError != nil {
|
||||
log.Printf("[WARN] Redis client close failed: %v", closeError)
|
||||
}
|
||||
}()
|
||||
|
||||
cacheContext, cacheCancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
cfg.RedisOperationTimeout,
|
||||
)
|
||||
if cacheError = redisChatCache.Ping(cacheContext); cacheError != nil {
|
||||
log.Printf("[WARN] Redis unavailable at startup; PostgreSQL fallback is active: %v", cacheError)
|
||||
} else {
|
||||
log.Println("[INFO] Redis chat cache is available")
|
||||
}
|
||||
cacheCancel()
|
||||
|
||||
applicationStore = cacheadapter.NewCachedRepository(
|
||||
postgresStore,
|
||||
redisChatCache,
|
||||
log.Default(),
|
||||
)
|
||||
}
|
||||
|
||||
cursorCodec, err := application.NewCursorCodec(cfg.CursorSigningKey)
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Invalid cursor configuration: %v", err)
|
||||
}
|
||||
|
||||
service, err := application.NewService(
|
||||
applicationStore,
|
||||
cursorCodec,
|
||||
cfg.SessionTTL,
|
||||
application.WithIdempotencyTTL(cfg.IdempotencyTTL),
|
||||
application.WithObjectStore(minioStore),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("[FATAL] Application initialization failed: %v", err)
|
||||
}
|
||||
|
||||
handler := handlers.NewHandler(
|
||||
service,
|
||||
handlers.WithReadiness(
|
||||
health.NewMultiChecker(databasePool, minioStore),
|
||||
cfg.ReadinessTimeout,
|
||||
),
|
||||
handlers.WithSessionCookieSecure(cfg.SessionCookieSecure),
|
||||
)
|
||||
|
||||
mux := http.NewServeMux()
|
||||
|
||||
mux.HandleFunc("GET /health/live", handler.HealthLive)
|
||||
mux.HandleFunc("GET /health/lb", handler.HealthLive)
|
||||
mux.HandleFunc("GET /health/ready", handler.HealthReady)
|
||||
|
||||
mux.HandleFunc(
|
||||
"POST /api/v1/auth/sessions",
|
||||
handler.CreateSession,
|
||||
)
|
||||
|
||||
mux.HandleFunc("POST /api/v1/users", handler.CreateUser)
|
||||
|
||||
requireAuth := func(next http.HandlerFunc) http.HandlerFunc {
|
||||
return middleware.RequireAuth(service, next)
|
||||
}
|
||||
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/users/me",
|
||||
requireAuth(handler.GetMe),
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"DELETE /api/v1/auth/sessions/current",
|
||||
requireAuth(handler.DeleteCurrentSession),
|
||||
)
|
||||
|
||||
mux.HandleFunc(
|
||||
"POST /api/v1/chats",
|
||||
requireAuth(handler.CreateChat),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/chats",
|
||||
requireAuth(handler.ListChats),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/chats/{chat_id}",
|
||||
requireAuth(handler.GetChat),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"POST /api/v1/chats/{chat_id}/messages",
|
||||
requireAuth(handler.CreateMessage),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/chats/{chat_id}/messages",
|
||||
requireAuth(handler.GetMessages),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"POST /api/v1/chats/{chat_id}/attachments",
|
||||
requireAuth(handler.UploadAttachment),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/attachments/{attachment_id}",
|
||||
requireAuth(handler.GetAttachment),
|
||||
)
|
||||
mux.HandleFunc(
|
||||
"GET /api/v1/attachments/{attachment_id}/content",
|
||||
requireAuth(handler.DownloadAttachment),
|
||||
)
|
||||
|
||||
var rootHandler http.Handler = mux
|
||||
rootHandler = middleware.Recovery(rootHandler)
|
||||
rootHandler = middleware.Logger(rootHandler)
|
||||
availabilityGate := middleware.NewAvailabilityGate()
|
||||
rootHandler = availabilityGate.Middleware(rootHandler)
|
||||
rootHandler = middleware.RequestID(rootHandler)
|
||||
rootHandler = middleware.InstanceID(cfg.InstanceID, rootHandler)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: cfg.HTTPAddress(),
|
||||
Handler: rootHandler,
|
||||
ReadTimeout: 15 * time.Second,
|
||||
WriteTimeout: 15 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
serverErrors := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
log.Printf(
|
||||
"[INFO] Server instance %s starting on %s",
|
||||
cfg.InstanceID,
|
||||
server.Addr,
|
||||
)
|
||||
serverErrors <- server.ListenAndServe()
|
||||
}()
|
||||
|
||||
shutdownSignals := make(chan os.Signal, 1)
|
||||
signal.Notify(
|
||||
shutdownSignals,
|
||||
os.Interrupt,
|
||||
syscall.SIGTERM,
|
||||
)
|
||||
defer signal.Stop(shutdownSignals)
|
||||
|
||||
select {
|
||||
case serverError := <-serverErrors:
|
||||
if !errors.Is(serverError, http.ErrServerClosed) {
|
||||
log.Fatalf("[FATAL] Server crashed: %v", serverError)
|
||||
}
|
||||
|
||||
case signalValue := <-shutdownSignals:
|
||||
log.Printf(
|
||||
"[INFO] Signal %v received. Starting graceful shutdown...",
|
||||
signalValue,
|
||||
)
|
||||
|
||||
availabilityGate.BeginDrain()
|
||||
log.Printf(
|
||||
"[INFO] Readiness disabled; draining new traffic for %s",
|
||||
cfg.ShutdownDrainDelay,
|
||||
)
|
||||
time.Sleep(cfg.ShutdownDrainDelay)
|
||||
|
||||
shutdownContext, shutdownCancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
cfg.ShutdownTimeout,
|
||||
)
|
||||
defer shutdownCancel()
|
||||
|
||||
if err := server.Shutdown(shutdownContext); err != nil {
|
||||
log.Printf(
|
||||
"[WARN] Graceful shutdown failed: %v. Forcing close.",
|
||||
err,
|
||||
)
|
||||
_ = server.Close()
|
||||
}
|
||||
|
||||
log.Println("[INFO] Server stopped gracefully.")
|
||||
}
|
||||
}
|
||||
+343
@@ -0,0 +1,343 @@
|
||||
services:
|
||||
load-balancer:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: infrastructure/haproxy/Dockerfile
|
||||
image: mtusi2026lab4-load-balancer
|
||||
ports:
|
||||
- "127.0.0.1:${PUBLIC_PORT:-8080}:8080"
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
frontend:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD-SHELL",
|
||||
"wget -qO- http://127.0.0.1:8080/health/ready || exit 1",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 3s
|
||||
stop_grace_period: 10s
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
frontend:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: infrastructure/nginx/Dockerfile
|
||||
image: mtusi2026lab4-web
|
||||
expose:
|
||||
- "8080"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:8080/ >/dev/null || exit 1"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 3s
|
||||
stop_grace_period: 10s
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.10"
|
||||
memory: 64M
|
||||
|
||||
postgres:
|
||||
image: postgres:16.15-alpine3.24@sha256:721873c34ceb9f8d8fc265984940dc982404c105f19ad51be9fdc5970a6080ea
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-messenger}
|
||||
POSTGRES_DB: ${POSTGRES_DB:-messenger}
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets:
|
||||
- postgres_password
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD-SHELL",
|
||||
"pg_isready -U ${POSTGRES_USER:-messenger} -d ${POSTGRES_DB:-messenger}",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 5s
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "${POSTGRES_CPU_LIMIT:-1.00}"
|
||||
memory: ${POSTGRES_MEMORY_LIMIT:-512M}
|
||||
|
||||
redis:
|
||||
image: redis:8.10.2-alpine3.23@sha256:3811787313eba226a2ef38658c6ccb91cd5e110edc89c37767de373120a0e5a0
|
||||
command:
|
||||
[
|
||||
"redis-server",
|
||||
"--save",
|
||||
"",
|
||||
"--appendonly",
|
||||
"no",
|
||||
"--maxmemory",
|
||||
"64mb",
|
||||
"--maxmemory-policy",
|
||||
"allkeys-lru",
|
||||
]
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 3s
|
||||
stop_grace_period: 10s
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
|
||||
migrate:
|
||||
image: migrate/migrate:v4.20.1@sha256:76cc2074cb6642631f34a898ced71e6aeaa6b1a4d78c4daa743275a22e0c5be7
|
||||
entrypoint: ["/bin/sh", "-ec"]
|
||||
command:
|
||||
- >
|
||||
exec migrate
|
||||
-path=/migrations
|
||||
-database="$$(cat /run/secrets/database_url)"
|
||||
up
|
||||
secrets:
|
||||
- database_url
|
||||
volumes:
|
||||
- ./migrations:/migrations:ro
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 64M
|
||||
|
||||
minio:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: infrastructure/minio/Dockerfile
|
||||
target: server
|
||||
args:
|
||||
MINIO_VERSION: RELEASE.2025-10-15T17-29-55Z
|
||||
image: mtusi2026lab4-minio:RELEASE.2025-10-15T17-29-55Z
|
||||
command: ["server", "/data", "--address", ":9000"]
|
||||
environment:
|
||||
MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user
|
||||
MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password
|
||||
MINIO_BROWSER: "off"
|
||||
secrets:
|
||||
- minio_root_user
|
||||
- minio_root_password
|
||||
volumes:
|
||||
- minio_data:/data
|
||||
expose:
|
||||
- "9000"
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -qO- http://127.0.0.1:9000/minio/health/live >/dev/null || exit 1"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
start_period: 10s
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.75"
|
||||
memory: 512M
|
||||
|
||||
minio-bootstrap:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: infrastructure/minio/Dockerfile
|
||||
target: client
|
||||
args:
|
||||
MC_VERSION: RELEASE.2025-08-13T08-35-41Z
|
||||
image: mtusi2026lab4-mc:RELEASE.2025-08-13T08-35-41Z
|
||||
entrypoint: ["/bin/sh", "-ec"]
|
||||
command:
|
||||
- |
|
||||
root_user="$$(cat /run/secrets/minio_root_user)"
|
||||
root_password="$$(cat /run/secrets/minio_root_password)"
|
||||
api_user="$$(cat /run/secrets/s3_access_key_id)"
|
||||
api_password="$$(cat /run/secrets/s3_secret_access_key)"
|
||||
mc alias set local http://minio:9000 "$$root_user" "$$root_password" --api S3v4
|
||||
mc mb --ignore-existing "local/$${S3_BUCKET}"
|
||||
mc anonymous set none "local/$${S3_BUCKET}"
|
||||
sed "s/__BUCKET__/$${S3_BUCKET}/g" /config/api-policy.template.json > /tmp/api-policy.json
|
||||
mc admin policy create local messenger-api /tmp/api-policy.json
|
||||
if ! mc admin user info local "$$api_user" >/dev/null 2>&1; then
|
||||
mc admin user add local "$$api_user" "$$api_password"
|
||||
fi
|
||||
mc admin policy attach local messenger-api --user "$$api_user"
|
||||
environment:
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
secrets:
|
||||
- minio_root_user
|
||||
- minio_root_password
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
volumes:
|
||||
- ./infrastructure/minio/api-policy.template.json:/config/api-policy.template.json:ro
|
||||
depends_on:
|
||||
minio:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
environment:
|
||||
APP_HOST: ${APP_HOST:-0.0.0.0}
|
||||
APP_PORT: ${APP_INTERNAL_PORT:-8080}
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
SESSION_STORE: ${SESSION_STORE:-postgres}
|
||||
SESSION_COOKIE_SECURE: ${SESSION_COOKIE_SECURE:-false}
|
||||
CURSOR_SIGNING_KEY_FILE: ${CURSOR_SIGNING_KEY_FILE:-/run/secrets/cursor_signing_key}
|
||||
|
||||
CACHE_URL: ${CACHE_URL:-redis://redis:6379/0}
|
||||
CACHE_TTL: ${CACHE_TTL:-5m}
|
||||
CACHE_OPERATION_TIMEOUT: ${CACHE_OPERATION_TIMEOUT:-250ms}
|
||||
CACHE_POOL_SIZE: ${CACHE_POOL_SIZE:-10}
|
||||
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://minio:9000}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID_FILE: ${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}
|
||||
S3_SECRET_ACCESS_KEY_FILE: ${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}
|
||||
S3_OPERATION_TIMEOUT: ${S3_OPERATION_TIMEOUT:-5s}
|
||||
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
|
||||
DB_MAX_CONNS: ${DB_MAX_CONNS:-10}
|
||||
DB_MIN_CONNS: ${DB_MIN_CONNS:-2}
|
||||
DB_MAX_CONN_LIFETIME: ${DB_MAX_CONN_LIFETIME:-30m}
|
||||
DB_MAX_CONN_IDLE_TIME: ${DB_MAX_CONN_IDLE_TIME:-5m}
|
||||
DB_HEALTH_CHECK_PERIOD: ${DB_HEALTH_CHECK_PERIOD:-30s}
|
||||
DB_CONNECT_TIMEOUT: ${DB_CONNECT_TIMEOUT:-5s}
|
||||
DB_QUERY_TIMEOUT: ${DB_QUERY_TIMEOUT:-3s}
|
||||
|
||||
READINESS_TIMEOUT: ${READINESS_TIMEOUT:-2s}
|
||||
SHUTDOWN_DRAIN_DELAY: ${SHUTDOWN_DRAIN_DELAY:-2s}
|
||||
SHUTDOWN_TIMEOUT: ${SHUTDOWN_TIMEOUT:-10s}
|
||||
GOMEMLIMIT: ${API_GO_MEMORY_LIMIT:-220MiB}
|
||||
secrets:
|
||||
- database_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
- cursor_signing_key
|
||||
expose:
|
||||
- "8080"
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
minio-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits:
|
||||
cpus: "${API_CPU_LIMIT:-0.50}"
|
||||
memory: ${API_MEMORY_LIMIT:-256M}
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD-SHELL",
|
||||
"wget -qO- http://127.0.0.1:$${APP_PORT:-8080}/health/ready || exit 1",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 5s
|
||||
stop_grace_period: 15s
|
||||
restart: unless-stopped
|
||||
|
||||
redis-insight:
|
||||
image: redis/redisinsight:3.8.0@sha256:b5e19ee240abef6edb435871b90ff8a210995422e8e018ab61c0339d318a1f84
|
||||
profiles: ["tools"]
|
||||
ports:
|
||||
- "127.0.0.1:${REDIS_INSIGHT_PORT:-5540}:5540"
|
||||
volumes:
|
||||
- redis_insight_data:/data
|
||||
depends_on:
|
||||
redis:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.50"
|
||||
memory: 256M
|
||||
|
||||
postgres-restore:
|
||||
image: postgres:16.15-alpine3.24@sha256:721873c34ceb9f8d8fc265984940dc982404c105f19ad51be9fdc5970a6080ea
|
||||
profiles: ["recovery"]
|
||||
environment:
|
||||
POSTGRES_USER: ${POSTGRES_USER:-messenger}
|
||||
POSTGRES_DB: ${POSTGRES_RESTORE_DB:-messenger_restore_admin}
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets:
|
||||
- postgres_password
|
||||
volumes:
|
||||
- postgres_restore_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD-SHELL",
|
||||
"pg_isready -U ${POSTGRES_USER:-messenger} -d ${POSTGRES_RESTORE_DB:-messenger_restore_admin}",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
start_period: 5s
|
||||
restart: "no"
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.00"
|
||||
memory: 512M
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_insight_data:
|
||||
minio_data:
|
||||
postgres_restore_data:
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password.txt
|
||||
database_url:
|
||||
file: ./secrets/database_url.txt
|
||||
cursor_signing_key:
|
||||
file: ./secrets/cursor_signing_key.txt
|
||||
minio_root_user:
|
||||
file: ./secrets/minio_root_user.txt
|
||||
minio_root_password:
|
||||
file: ./secrets/minio_root_password.txt
|
||||
s3_access_key_id:
|
||||
file: ./secrets/s3_access_key_id.txt
|
||||
s3_secret_access_key:
|
||||
file: ./secrets/s3_secret_access_key.txt
|
||||
@@ -0,0 +1,19 @@
|
||||
# Lab 4 verification summary
|
||||
|
||||
Date: 2026-10-29. Environment: local Docker Compose on WSL2; two API replicas; PostgreSQL 16.15; Redis 8.10.2; MinIO release `RELEASE.2025-10-15T17-29-55Z` built from the official source module.
|
||||
|
||||
| Check | Command | Observed result |
|
||||
| --- | --- | --- |
|
||||
| Public contract | `BASE_URL=http://127.0.0.1:18080 make test` | PASS: upload, attachment-only message, authorized byte download, checksum/content type and outsider 404 |
|
||||
| Exact boundaries / decode | `BASE_URL=http://127.0.0.1:18080 make test-attachment-boundaries` | Exact 10 MiB and 40,000,000 pixels accepted; truncated/corrupted-after-64-KiB PNG rejected 415; blank/C1 filename rejected 400 |
|
||||
| One-replica failover | `PUBLIC_PORT=18080 BASE_URL=http://127.0.0.1:18080 ACTION_SCRIPT=./scripts/stop-one.sh make test-failover` | 2 instances before failure; 142 successful writes; 0 failed attempts; first recovery 2.771 s; attachment unchanged |
|
||||
| Full persistence restart | `PUBLIC_PORT=18080 BASE_URL=http://127.0.0.1:18080 ACTION_SCRIPT=./scripts/restart.sh make test-persistence` | PASS after restarting PostgreSQL, Redis, MinIO, both API replicas, frontend and HAProxy; existing session, metadata and bytes survived |
|
||||
| Real MinIO outage | `BASE_URL=http://127.0.0.1:18080 make test-storage-outage` | Public readiness returned JSON 503 in 2.004 s, upload returned JSON 503 in 6.016 s; after recovery the 111,817-byte previous original was byte-identical |
|
||||
| Private bucket / least privilege | `make test-minio-policy` | API containers had no MinIO root env/secrets; limited credentials could put/read/delete only in configured bucket; creating another bucket and anonymous GET failed |
|
||||
| Concurrent upload memory | `BASE_URL=http://127.0.0.1:18080 make test-attachment-memory` | 12/12 successful 6,293,028-byte PNG uploads (75,516,336 bytes total) in 0.599 s; peak API memory 31,216,107 and 26,214,400 bytes |
|
||||
| Reconciler behavior | `make test-reconcile` | Dry-run found 4 injected issues; apply deleted 2 objects and marked 2 attachments failed; repeat made 0 mutations; 1 referenced missing attachment remained protected/manual |
|
||||
| Reconciler traversal | `make test-reconcile-scale` | 100 orphan objects: 109 objects scanned, 9 ms core / 1,978 ms including container startup. 1,000 orphans: 1,009 scanned, 38 ms core / 2,019 ms including startup |
|
||||
| Ambiguous DB COMMIT fault | `go test ./internal/application -count=1` | Successful finalize with lost response is recovered by readback; if DB remains unavailable, ready metadata keeps its object and returns an honest error |
|
||||
| Go quality gates | `go test ./...`; `go test -race ./...`; `go vet ./...`; `make check`; `git diff --check` | PASS |
|
||||
|
||||
The measurements are single local runs, not production capacity claims. The reconciler currently materializes both inventories in memory, so its measured near-linear small-scale cost must be re-evaluated before using it with a large bucket.
|
||||
@@ -0,0 +1,3 @@
|
||||
node_modules
|
||||
dist
|
||||
coverage
|
||||
@@ -0,0 +1,17 @@
|
||||
<!doctype html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<meta name="theme-color" content="#11110f" />
|
||||
<meta
|
||||
name="description"
|
||||
content="Минималистичный веб-клиент распределённого MTUCI Messenger"
|
||||
/>
|
||||
<title>MTUCI Messenger</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
Generated
+1688
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,27 @@
|
||||
{
|
||||
"name": "mtusi-messenger-web",
|
||||
"private": true,
|
||||
"version": "0.1.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite --host 0.0.0.0",
|
||||
"build": "tsc -b && vite build",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest"
|
||||
},
|
||||
"dependencies": {
|
||||
"@tanstack/react-query": "^5.104.5",
|
||||
"lucide-react": "^1.61.0",
|
||||
"react": "^19.3.1",
|
||||
"react-dom": "^19.3.1",
|
||||
"react-router-dom": "^7.18.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/react": "^19.3.0",
|
||||
"@types/react-dom": "^19.3.0",
|
||||
"@vitejs/plugin-react": "^6.1.2",
|
||||
"typescript": "^7.0.2",
|
||||
"vite": "^8.3.5",
|
||||
"vitest": "^5.0.5"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
import type {
|
||||
ApiErrorBody,
|
||||
Chat,
|
||||
CreateChatInput,
|
||||
LoginInput,
|
||||
Message,
|
||||
Page,
|
||||
RegisterInput,
|
||||
Session,
|
||||
User,
|
||||
} from "./types";
|
||||
|
||||
const publicApi = "/api/v1";
|
||||
const browserApi = "/web-api/v1";
|
||||
|
||||
export class ApiError extends Error {
|
||||
readonly status: number;
|
||||
readonly code?: string;
|
||||
readonly requestId?: string;
|
||||
|
||||
constructor(status: number, body: ApiErrorBody) {
|
||||
super(body.error?.message || `HTTP ${status}`);
|
||||
this.name = "ApiError";
|
||||
this.status = status;
|
||||
this.code = body.error?.code;
|
||||
this.requestId = body.error?.request_id;
|
||||
}
|
||||
}
|
||||
|
||||
type RequestOptions = Omit<RequestInit, "body"> & {
|
||||
body?: unknown;
|
||||
};
|
||||
|
||||
async function request<T>(path: string, options: RequestOptions = {}): Promise<T> {
|
||||
const headers = new Headers(options.headers);
|
||||
if (options.body !== undefined) {
|
||||
headers.set("Content-Type", "application/json");
|
||||
}
|
||||
|
||||
const response = await fetch(path, {
|
||||
...options,
|
||||
headers,
|
||||
credentials: "same-origin",
|
||||
body: options.body === undefined ? undefined : JSON.stringify(options.body),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
let body: ApiErrorBody = {};
|
||||
try {
|
||||
body = (await response.json()) as ApiErrorBody;
|
||||
} catch {
|
||||
// Preserve a useful HTTP-level error when a proxy returned non-JSON.
|
||||
}
|
||||
throw new ApiError(response.status, body);
|
||||
}
|
||||
|
||||
if (response.status === 204) {
|
||||
return undefined as T;
|
||||
}
|
||||
return (await response.json()) as T;
|
||||
}
|
||||
|
||||
function encodeBasic({ username, password }: LoginInput): string {
|
||||
const bytes = new TextEncoder().encode(`${username}:${password}`);
|
||||
let binary = "";
|
||||
for (const byte of bytes) {
|
||||
binary += String.fromCharCode(byte);
|
||||
}
|
||||
return btoa(binary);
|
||||
}
|
||||
|
||||
async function collectPages<T>(path: string): Promise<T[]> {
|
||||
const items: T[] = [];
|
||||
let cursor: string | null = null;
|
||||
|
||||
for (let pageIndex = 0; pageIndex < 100; pageIndex += 1) {
|
||||
const search = new URLSearchParams({ limit: "100" });
|
||||
if (cursor) {
|
||||
search.set("cursor", cursor);
|
||||
}
|
||||
const page = await request<Page<T>>(`${path}?${search.toString()}`);
|
||||
items.push(...page.items);
|
||||
cursor = page.next_cursor;
|
||||
if (!cursor) {
|
||||
return items;
|
||||
}
|
||||
}
|
||||
throw new Error("pagination safety limit exceeded");
|
||||
}
|
||||
|
||||
export const api = {
|
||||
register(input: RegisterInput): Promise<User> {
|
||||
return request<User>(`${publicApi}/users`, {
|
||||
method: "POST",
|
||||
body: {
|
||||
username: input.username,
|
||||
display_name: input.displayName,
|
||||
password: input.password,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
login(input: LoginInput): Promise<Session> {
|
||||
return request<Session>(`${publicApi}/auth/sessions`, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Authorization: `Basic ${encodeBasic(input)}`,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
logout(): Promise<void> {
|
||||
return request<void>(`${browserApi}/auth/sessions/current`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
},
|
||||
|
||||
me(): Promise<User> {
|
||||
return request<User>(`${browserApi}/users/me`);
|
||||
},
|
||||
|
||||
chats(): Promise<Chat[]> {
|
||||
return collectPages<Chat>(`${browserApi}/chats`);
|
||||
},
|
||||
|
||||
chat(chatID: string): Promise<Chat> {
|
||||
return request<Chat>(`${browserApi}/chats/${encodeURIComponent(chatID)}`);
|
||||
},
|
||||
|
||||
createChat(input: CreateChatInput): Promise<Chat> {
|
||||
return request<Chat>(`${browserApi}/chats`, {
|
||||
method: "POST",
|
||||
body: {
|
||||
title: input.title,
|
||||
member_ids: input.memberIds,
|
||||
},
|
||||
});
|
||||
},
|
||||
|
||||
messages(chatID: string): Promise<Message[]> {
|
||||
return collectPages<Message>(
|
||||
`${browserApi}/chats/${encodeURIComponent(chatID)}/messages`,
|
||||
);
|
||||
},
|
||||
|
||||
sendMessage(
|
||||
chatID: string,
|
||||
text: string,
|
||||
idempotencyKey: string,
|
||||
attachmentIDs: string[] = [],
|
||||
): Promise<Message> {
|
||||
return request<Message>(
|
||||
`${browserApi}/chats/${encodeURIComponent(chatID)}/messages`,
|
||||
{
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Idempotency-Key": idempotencyKey,
|
||||
},
|
||||
body: { text, attachment_ids: attachmentIDs },
|
||||
},
|
||||
);
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,58 @@
|
||||
export type User = {
|
||||
id: string;
|
||||
username: string;
|
||||
display_name: string;
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type Session = {
|
||||
token: string;
|
||||
token_type: "Bearer";
|
||||
expires_at: string;
|
||||
user: User;
|
||||
};
|
||||
|
||||
export type Chat = {
|
||||
id: string;
|
||||
title: string;
|
||||
member_ids: string[];
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type Message = {
|
||||
id: string;
|
||||
chat_id: string;
|
||||
sender_id: string;
|
||||
text: string;
|
||||
attachment_ids: string[];
|
||||
created_at: string;
|
||||
};
|
||||
|
||||
export type Page<T> = {
|
||||
items: T[];
|
||||
next_cursor: string | null;
|
||||
};
|
||||
|
||||
export type ApiErrorBody = {
|
||||
error?: {
|
||||
code?: string;
|
||||
message?: string;
|
||||
request_id?: string;
|
||||
};
|
||||
};
|
||||
|
||||
export type RegisterInput = {
|
||||
username: string;
|
||||
displayName: string;
|
||||
password: string;
|
||||
};
|
||||
|
||||
export type CreateChatInput = {
|
||||
title: string;
|
||||
memberIds: string[];
|
||||
};
|
||||
|
||||
export type LoginInput = {
|
||||
username: string;
|
||||
password: string;
|
||||
};
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Navigate, Outlet, Route, Routes, useLocation } from "react-router-dom";
|
||||
|
||||
import { useAuth } from "../auth/AuthProvider";
|
||||
import { LoadingScreen } from "../components/LoadingScreen";
|
||||
import { AuthPage } from "../pages/AuthPage";
|
||||
import { MessengerPage } from "../pages/MessengerPage";
|
||||
|
||||
function ProtectedRoute() {
|
||||
const { user, isLoading } = useAuth();
|
||||
const location = useLocation();
|
||||
|
||||
if (isLoading) {
|
||||
return <LoadingScreen />;
|
||||
}
|
||||
if (!user) {
|
||||
return <Navigate to="/login" replace state={{ from: location.pathname }} />;
|
||||
}
|
||||
return <Outlet />;
|
||||
}
|
||||
|
||||
function PublicOnlyRoute() {
|
||||
const { user, isLoading } = useAuth();
|
||||
|
||||
if (isLoading) {
|
||||
return <LoadingScreen />;
|
||||
}
|
||||
if (user) {
|
||||
return <Navigate to="/app" replace />;
|
||||
}
|
||||
return <Outlet />;
|
||||
}
|
||||
|
||||
export function App() {
|
||||
return (
|
||||
<Routes>
|
||||
<Route element={<PublicOnlyRoute />}>
|
||||
<Route path="/login" element={<AuthPage initialMode="login" />} />
|
||||
<Route path="/register" element={<AuthPage initialMode="register" />} />
|
||||
</Route>
|
||||
|
||||
<Route element={<ProtectedRoute />}>
|
||||
<Route path="/app" element={<MessengerPage />} />
|
||||
<Route path="/app/chats/:chatId" element={<MessengerPage />} />
|
||||
<Route path="/app/profile" element={<MessengerPage profileOpen />} />
|
||||
</Route>
|
||||
|
||||
<Route path="/" element={<Navigate to="/app" replace />} />
|
||||
<Route path="*" element={<Navigate to="/app" replace />} />
|
||||
</Routes>
|
||||
);
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 540 KiB |
@@ -0,0 +1,90 @@
|
||||
import { useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
createContext,
|
||||
useCallback,
|
||||
useContext,
|
||||
type PropsWithChildren,
|
||||
} from "react";
|
||||
|
||||
import { api, ApiError } from "../api/client";
|
||||
import type { LoginInput, RegisterInput, User } from "../api/types";
|
||||
|
||||
type AuthContextValue = {
|
||||
user: User | null;
|
||||
isLoading: boolean;
|
||||
login: (input: LoginInput) => Promise<void>;
|
||||
register: (input: RegisterInput) => Promise<void>;
|
||||
logout: () => Promise<void>;
|
||||
};
|
||||
|
||||
const AuthContext = createContext<AuthContextValue | null>(null);
|
||||
const meQueryKey = ["current-user"] as const;
|
||||
|
||||
async function loadCurrentUser(): Promise<User | null> {
|
||||
try {
|
||||
return await api.me();
|
||||
} catch (error) {
|
||||
if (error instanceof ApiError && error.status === 401) {
|
||||
return null;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export function AuthProvider({ children }: PropsWithChildren) {
|
||||
const queryClient = useQueryClient();
|
||||
const meQuery = useQuery({
|
||||
queryKey: meQueryKey,
|
||||
queryFn: loadCurrentUser,
|
||||
retry: false,
|
||||
staleTime: 30_000,
|
||||
});
|
||||
|
||||
const login = useCallback(
|
||||
async (input: LoginInput) => {
|
||||
const session = await api.login(input);
|
||||
queryClient.setQueryData(meQueryKey, session.user);
|
||||
},
|
||||
[queryClient],
|
||||
);
|
||||
|
||||
const register = useCallback(
|
||||
async (input: RegisterInput) => {
|
||||
await api.register(input);
|
||||
const session = await api.login({
|
||||
username: input.username,
|
||||
password: input.password,
|
||||
});
|
||||
queryClient.setQueryData(meQueryKey, session.user);
|
||||
},
|
||||
[queryClient],
|
||||
);
|
||||
|
||||
const logout = useCallback(async () => {
|
||||
await api.logout();
|
||||
queryClient.setQueryData(meQueryKey, null);
|
||||
queryClient.removeQueries({ queryKey: ["chats"] });
|
||||
}, [queryClient]);
|
||||
|
||||
return (
|
||||
<AuthContext.Provider
|
||||
value={{
|
||||
user: meQuery.data ?? null,
|
||||
isLoading: meQuery.isLoading,
|
||||
login,
|
||||
register,
|
||||
logout,
|
||||
}}
|
||||
>
|
||||
{children}
|
||||
</AuthContext.Provider>
|
||||
);
|
||||
}
|
||||
|
||||
export function useAuth(): AuthContextValue {
|
||||
const context = useContext(AuthContext);
|
||||
if (!context) {
|
||||
throw new Error("useAuth must be used within AuthProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { initials } from "../lib/format";
|
||||
|
||||
type AvatarProps = {
|
||||
label: string;
|
||||
seed?: string;
|
||||
size?: "small" | "medium" | "large";
|
||||
};
|
||||
|
||||
function tone(seed: string): number {
|
||||
let hash = 0;
|
||||
for (const character of seed) {
|
||||
hash = (hash * 31 + character.codePointAt(0)!) >>> 0;
|
||||
}
|
||||
return hash % 5;
|
||||
}
|
||||
|
||||
export function Avatar({ label, seed = label, size = "medium" }: AvatarProps) {
|
||||
return (
|
||||
<span
|
||||
aria-hidden="true"
|
||||
className={`avatar avatar--${size} avatar--tone-${tone(seed)}`}
|
||||
>
|
||||
{initials(label)}
|
||||
</span>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
import { MessageSquarePlus, Search, UsersRound, X } from "lucide-react";
|
||||
import { useMemo, useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import type { Chat, User } from "../api/types";
|
||||
import { formatMemberCount } from "../lib/format";
|
||||
import { Avatar } from "./Avatar";
|
||||
|
||||
type ChatListProps = {
|
||||
chats: Chat[];
|
||||
currentUser: User;
|
||||
error: boolean;
|
||||
isLoading: boolean;
|
||||
selectedChatID?: string;
|
||||
onCreateChat: () => void;
|
||||
};
|
||||
|
||||
export function ChatList({
|
||||
chats,
|
||||
currentUser,
|
||||
error,
|
||||
isLoading,
|
||||
selectedChatID,
|
||||
onCreateChat,
|
||||
}: ChatListProps) {
|
||||
const navigate = useNavigate();
|
||||
const [search, setSearch] = useState("");
|
||||
|
||||
const filteredChats = useMemo(() => {
|
||||
const normalized = search.trim().toLocaleLowerCase("ru");
|
||||
if (!normalized) {
|
||||
return chats;
|
||||
}
|
||||
return chats.filter((chat) =>
|
||||
chat.title.toLocaleLowerCase("ru").includes(normalized),
|
||||
);
|
||||
}, [chats, search]);
|
||||
|
||||
return (
|
||||
<aside className="chat-sidebar">
|
||||
<header className="chat-sidebar__header">
|
||||
<div>
|
||||
<p className="eyebrow">Сообщения</p>
|
||||
<h1>Чаты</h1>
|
||||
</div>
|
||||
<button
|
||||
aria-label="Создать чат"
|
||||
className="soft-icon-button"
|
||||
onClick={onCreateChat}
|
||||
type="button"
|
||||
>
|
||||
<MessageSquarePlus aria-hidden="true" />
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<div className="search-box">
|
||||
<Search aria-hidden="true" />
|
||||
<input
|
||||
aria-label="Поиск чатов"
|
||||
onChange={(event) => setSearch(event.target.value)}
|
||||
placeholder="Поиск"
|
||||
value={search}
|
||||
/>
|
||||
{search && (
|
||||
<button aria-label="Очистить поиск" onClick={() => setSearch("")} type="button">
|
||||
<X aria-hidden="true" />
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="chat-list" aria-live="polite">
|
||||
{isLoading && (
|
||||
<div className="sidebar-state">
|
||||
<span className="loading-line" />
|
||||
<p>Загружаем чаты…</p>
|
||||
</div>
|
||||
)}
|
||||
{error && (
|
||||
<div className="sidebar-state sidebar-state--error">
|
||||
<p>Не удалось загрузить список чатов.</p>
|
||||
</div>
|
||||
)}
|
||||
{!isLoading && !error && filteredChats.length === 0 && (
|
||||
<div className="sidebar-state">
|
||||
<UsersRound aria-hidden="true" />
|
||||
<p>{search ? "Ничего не найдено" : "У вас пока нет чатов"}</p>
|
||||
{!search && (
|
||||
<button className="text-button" onClick={onCreateChat} type="button">
|
||||
Создать первый
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{filteredChats.map((chat) => (
|
||||
<button
|
||||
className={`chat-row ${selectedChatID === chat.id ? "chat-row--active" : ""}`}
|
||||
key={chat.id}
|
||||
onClick={() => navigate(`/app/chats/${chat.id}`)}
|
||||
type="button"
|
||||
>
|
||||
<Avatar label={chat.title} seed={chat.id} />
|
||||
<span className="chat-row__body">
|
||||
<strong>{chat.title}</strong>
|
||||
<small>{formatMemberCount(chat.member_ids.length)}</small>
|
||||
</span>
|
||||
{chat.member_ids.includes(currentUser.id) && (
|
||||
<span className="chat-row__status" title="Вы участник">
|
||||
•
|
||||
</span>
|
||||
)}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
|
||||
<footer className="sidebar-footer">
|
||||
<span className="status-dot" />
|
||||
API подключён
|
||||
</footer>
|
||||
</aside>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
||||
import {
|
||||
ArrowLeft,
|
||||
CheckCheck,
|
||||
RefreshCcw,
|
||||
Send,
|
||||
ShieldCheck,
|
||||
UsersRound,
|
||||
} from "lucide-react";
|
||||
import {
|
||||
useEffect,
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
type FormEvent,
|
||||
type KeyboardEvent,
|
||||
} from "react";
|
||||
|
||||
import { api } from "../api/client";
|
||||
import type { Chat, Message, User } from "../api/types";
|
||||
import {
|
||||
dayKey,
|
||||
formatDay,
|
||||
formatMemberCount,
|
||||
formatMessageTime,
|
||||
shortID,
|
||||
} from "../lib/format";
|
||||
import { createIdempotencyKey } from "../lib/idempotency";
|
||||
import { Avatar } from "./Avatar";
|
||||
|
||||
type ChatViewProps = {
|
||||
chat: Chat;
|
||||
currentUser: User;
|
||||
onBack: () => void;
|
||||
};
|
||||
|
||||
type SendEnvelope = {
|
||||
text: string;
|
||||
idempotencyKey: string;
|
||||
};
|
||||
|
||||
export function ChatView({ chat, currentUser, onBack }: ChatViewProps) {
|
||||
const queryClient = useQueryClient();
|
||||
const messagesEnd = useRef<HTMLDivElement>(null);
|
||||
const [draft, setDraft] = useState("");
|
||||
const [failedEnvelope, setFailedEnvelope] = useState<SendEnvelope | null>(null);
|
||||
|
||||
const messagesQuery = useQuery({
|
||||
queryKey: ["messages", chat.id],
|
||||
queryFn: () => api.messages(chat.id),
|
||||
refetchInterval: 3_000,
|
||||
staleTime: 1_000,
|
||||
});
|
||||
|
||||
const messages = messagesQuery.data ?? [];
|
||||
|
||||
useEffect(() => {
|
||||
messagesEnd.current?.scrollIntoView({
|
||||
behavior: messages.length > 1 ? "smooth" : "auto",
|
||||
block: "end",
|
||||
});
|
||||
}, [chat.id, messages.length]);
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: (envelope: SendEnvelope) =>
|
||||
api.sendMessage(chat.id, envelope.text, envelope.idempotencyKey),
|
||||
onSuccess: (message) => {
|
||||
setFailedEnvelope(null);
|
||||
queryClient.setQueryData<Message[]>(
|
||||
["messages", chat.id],
|
||||
(current = []) => {
|
||||
if (current.some((item) => item.id === message.id)) {
|
||||
return current;
|
||||
}
|
||||
return [...current, message];
|
||||
},
|
||||
);
|
||||
},
|
||||
onError: (_error, envelope) => {
|
||||
setFailedEnvelope(envelope);
|
||||
},
|
||||
});
|
||||
|
||||
const groups = useMemo(() => {
|
||||
const result: Array<{ key: string; label: string; messages: Message[] }> = [];
|
||||
for (const message of messages) {
|
||||
const key = dayKey(message.created_at);
|
||||
const current = result.at(-1);
|
||||
if (!current || current.key !== key) {
|
||||
result.push({
|
||||
key,
|
||||
label: formatDay(message.created_at),
|
||||
messages: [message],
|
||||
});
|
||||
} else {
|
||||
current.messages.push(message);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}, [messages]);
|
||||
|
||||
function send(envelope: SendEnvelope) {
|
||||
if (!mutation.isPending) {
|
||||
mutation.mutate(envelope);
|
||||
}
|
||||
}
|
||||
|
||||
function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
const text = draft.trim();
|
||||
if (!text || Array.from(text).length > 4_000) {
|
||||
return;
|
||||
}
|
||||
const envelope = { text, idempotencyKey: createIdempotencyKey() };
|
||||
setDraft("");
|
||||
setFailedEnvelope(null);
|
||||
send(envelope);
|
||||
}
|
||||
|
||||
function handleKeyDown(event: KeyboardEvent<HTMLTextAreaElement>) {
|
||||
if (event.key === "Enter" && !event.shiftKey) {
|
||||
event.preventDefault();
|
||||
event.currentTarget.form?.requestSubmit();
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<section className="conversation">
|
||||
<header className="conversation-header">
|
||||
<button
|
||||
aria-label="Назад к списку чатов"
|
||||
className="mobile-back-button"
|
||||
onClick={onBack}
|
||||
type="button"
|
||||
>
|
||||
<ArrowLeft aria-hidden="true" />
|
||||
</button>
|
||||
<Avatar label={chat.title} seed={chat.id} size="small" />
|
||||
<div className="conversation-header__title">
|
||||
<strong>{chat.title}</strong>
|
||||
<span>{formatMemberCount(chat.member_ids.length)}</span>
|
||||
</div>
|
||||
<div className="conversation-header__security">
|
||||
<ShieldCheck aria-hidden="true" />
|
||||
<span>сессия защищена</span>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div className="message-canvas">
|
||||
<div className="message-canvas__shade" />
|
||||
<div className="message-stream" aria-live="polite">
|
||||
{messagesQuery.isLoading && (
|
||||
<div className="floating-state">
|
||||
<span className="loading-line" />
|
||||
Загружаем историю
|
||||
</div>
|
||||
)}
|
||||
|
||||
{messagesQuery.isError && (
|
||||
<div className="floating-state floating-state--error">
|
||||
<p>История временно недоступна</p>
|
||||
<button onClick={() => messagesQuery.refetch()} type="button">
|
||||
<RefreshCcw aria-hidden="true" /> Повторить
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!messagesQuery.isLoading && !messagesQuery.isError && messages.length === 0 && (
|
||||
<div className="floating-state">
|
||||
<UsersRound aria-hidden="true" />
|
||||
<p>Сообщений пока нет. Начните разговор.</p>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{groups.map((group) => (
|
||||
<div className="message-day" key={group.key}>
|
||||
<div className="date-divider"><span>{group.label}</span></div>
|
||||
{group.messages.map((message) => {
|
||||
const own = message.sender_id === currentUser.id;
|
||||
return (
|
||||
<article
|
||||
className={`message-bubble ${own ? "message-bubble--own" : ""}`}
|
||||
key={message.id}
|
||||
>
|
||||
<span className="message-bubble__sender">
|
||||
{own ? "Вы" : shortID(message.sender_id)}
|
||||
</span>
|
||||
<p>{message.text}</p>
|
||||
<span className="message-bubble__meta">
|
||||
{formatMessageTime(message.created_at)}
|
||||
{own && <CheckCheck aria-label="Доставлено серверу" />}
|
||||
</span>
|
||||
</article>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
))}
|
||||
<div ref={messagesEnd} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<footer className="composer">
|
||||
{failedEnvelope && (
|
||||
<div className="send-error" role="alert">
|
||||
<span>Отправка не подтверждена.</span>
|
||||
<button onClick={() => send(failedEnvelope)} type="button">
|
||||
Повторить с тем же ключом
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
<form onSubmit={submit}>
|
||||
<textarea
|
||||
aria-label="Текст сообщения"
|
||||
maxLength={4_000}
|
||||
onChange={(event) => setDraft(event.target.value)}
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder="Сообщение…"
|
||||
rows={1}
|
||||
value={draft}
|
||||
/>
|
||||
<span className="composer__counter">
|
||||
{draft.length > 3_600 ? `${Array.from(draft).length}/4000` : ""}
|
||||
</span>
|
||||
<button
|
||||
aria-label="Отправить"
|
||||
className="send-button"
|
||||
disabled={!draft.trim() || mutation.isPending}
|
||||
type="submit"
|
||||
>
|
||||
<Send aria-hidden="true" />
|
||||
</button>
|
||||
</form>
|
||||
</footer>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { Plus, UserRoundPlus, X } from "lucide-react";
|
||||
import { useEffect, useState, type FormEvent } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import { api, ApiError } from "../api/client";
|
||||
import type { Chat, User } from "../api/types";
|
||||
import { shortID } from "../lib/format";
|
||||
|
||||
type CreateChatDialogProps = {
|
||||
currentUser: User;
|
||||
onClose: () => void;
|
||||
};
|
||||
|
||||
const uuidPattern =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||
|
||||
export function CreateChatDialog({
|
||||
currentUser,
|
||||
onClose,
|
||||
}: CreateChatDialogProps) {
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
const [title, setTitle] = useState("");
|
||||
const [memberInput, setMemberInput] = useState("");
|
||||
const [validationError, setValidationError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
function closeOnEscape(event: KeyboardEvent) {
|
||||
if (event.key === "Escape") {
|
||||
onClose();
|
||||
}
|
||||
}
|
||||
window.addEventListener("keydown", closeOnEscape);
|
||||
return () => window.removeEventListener("keydown", closeOnEscape);
|
||||
}, [onClose]);
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: api.createChat,
|
||||
onSuccess: (chat) => {
|
||||
queryClient.setQueryData<Chat[]>(["chats"], (current = []) => [
|
||||
...current,
|
||||
chat,
|
||||
]);
|
||||
onClose();
|
||||
navigate(`/app/chats/${chat.id}`);
|
||||
},
|
||||
});
|
||||
|
||||
function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setValidationError("");
|
||||
|
||||
const enteredIDs = memberInput
|
||||
.split(/[\s,;]+/)
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean);
|
||||
const memberIds = Array.from(new Set([currentUser.id, ...enteredIDs]));
|
||||
|
||||
if (!title.trim() || Array.from(title.trim()).length > 100) {
|
||||
setValidationError("Название должно содержать от 1 до 100 символов");
|
||||
return;
|
||||
}
|
||||
const invalidID = enteredIDs.find((id) => !uuidPattern.test(id));
|
||||
if (invalidID) {
|
||||
setValidationError(`Некорректный UUID: ${invalidID}`);
|
||||
return;
|
||||
}
|
||||
mutation.mutate({ title: title.trim(), memberIds });
|
||||
}
|
||||
|
||||
const serverError =
|
||||
mutation.error instanceof ApiError
|
||||
? mutation.error.message
|
||||
: mutation.error
|
||||
? "Не удалось создать чат"
|
||||
: "";
|
||||
|
||||
return (
|
||||
<div className="dialog-backdrop" onMouseDown={onClose}>
|
||||
<section
|
||||
aria-labelledby="create-chat-title"
|
||||
aria-modal="true"
|
||||
className="dialog-card"
|
||||
onMouseDown={(event) => event.stopPropagation()}
|
||||
role="dialog"
|
||||
>
|
||||
<header className="dialog-card__header">
|
||||
<div>
|
||||
<p className="eyebrow">Новый разговор</p>
|
||||
<h2 id="create-chat-title">Создать чат</h2>
|
||||
</div>
|
||||
<button
|
||||
aria-label="Закрыть"
|
||||
className="soft-icon-button"
|
||||
onClick={onClose}
|
||||
type="button"
|
||||
>
|
||||
<X aria-hidden="true" />
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<form className="dialog-form" onSubmit={submit}>
|
||||
<label>
|
||||
<span>Название</span>
|
||||
<div className="input-shell">
|
||||
<Plus aria-hidden="true" />
|
||||
<input
|
||||
autoFocus
|
||||
maxLength={100}
|
||||
onChange={(event) => setTitle(event.target.value)}
|
||||
placeholder="Например, Команда проекта"
|
||||
required
|
||||
value={title}
|
||||
/>
|
||||
</div>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>UUID участников</span>
|
||||
<div className="textarea-shell">
|
||||
<UserRoundPlus aria-hidden="true" />
|
||||
<textarea
|
||||
onChange={(event) => setMemberInput(event.target.value)}
|
||||
placeholder="Вставьте UUID через пробел, запятую или с новой строки"
|
||||
rows={4}
|
||||
value={memberInput}
|
||||
/>
|
||||
</div>
|
||||
<small>
|
||||
Ваш ID добавлен автоматически: <code>{shortID(currentUser.id)}</code>
|
||||
</small>
|
||||
</label>
|
||||
|
||||
{(validationError || serverError) && (
|
||||
<div className="form-error" role="alert">
|
||||
{validationError || serverError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button className="primary-button" disabled={mutation.isPending} type="submit">
|
||||
{mutation.isPending ? "Создаём…" : "Создать чат"}
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
import { MessageSquarePlus } from "lucide-react";
|
||||
|
||||
type EmptyChatProps = {
|
||||
hasChats: boolean;
|
||||
onCreateChat: () => void;
|
||||
};
|
||||
|
||||
export function EmptyChat({ hasChats, onCreateChat }: EmptyChatProps) {
|
||||
return (
|
||||
<section className="conversation conversation--empty">
|
||||
<div className="empty-chat-card">
|
||||
<span className="empty-chat-card__line" />
|
||||
<p className="eyebrow">MTUCI Messenger</p>
|
||||
<h2>{hasChats ? "Выберите чат" : "Начните разговор"}</h2>
|
||||
<p>
|
||||
{hasChats
|
||||
? "История и новые сообщения появятся здесь."
|
||||
: "Создайте первый чат и пригласите участников по UUID."}
|
||||
</p>
|
||||
{!hasChats && (
|
||||
<button className="primary-button" onClick={onCreateChat} type="button">
|
||||
<MessageSquarePlus aria-hidden="true" />
|
||||
Создать чат
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
</section>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
export function LoadingScreen() {
|
||||
return (
|
||||
<main className="loading-screen" aria-live="polite">
|
||||
<div className="brand-mark brand-mark--large" aria-hidden="true">
|
||||
M
|
||||
</div>
|
||||
<p>Подключаемся к Messenger</p>
|
||||
<span className="loading-line" />
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { MessagesSquare, UserRound } from "lucide-react";
|
||||
import { Link } from "react-router-dom";
|
||||
|
||||
import type { User } from "../api/types";
|
||||
import { Avatar } from "./Avatar";
|
||||
|
||||
type NavigationRailProps = {
|
||||
user: User;
|
||||
profileOpen: boolean;
|
||||
};
|
||||
|
||||
export function NavigationRail({ user, profileOpen }: NavigationRailProps) {
|
||||
return (
|
||||
<nav className="navigation-rail" aria-label="Основная навигация">
|
||||
<Link className="rail-brand" to="/app" aria-label="MTUCI Messenger">
|
||||
M
|
||||
</Link>
|
||||
|
||||
<div className="rail-actions">
|
||||
<Link
|
||||
className={`rail-action ${profileOpen ? "" : "rail-action--active"}`}
|
||||
to="/app"
|
||||
>
|
||||
<MessagesSquare aria-hidden="true" />
|
||||
<span>Чаты</span>
|
||||
</Link>
|
||||
<Link
|
||||
className={`rail-action ${profileOpen ? "rail-action--active" : ""}`}
|
||||
to="/app/profile"
|
||||
>
|
||||
<UserRound aria-hidden="true" />
|
||||
<span>Профиль</span>
|
||||
</Link>
|
||||
</div>
|
||||
|
||||
<Link className="rail-profile" to="/app/profile" aria-label="Открыть профиль">
|
||||
<Avatar label={user.display_name} seed={user.id} size="small" />
|
||||
<span className="presence-dot" />
|
||||
</Link>
|
||||
</nav>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
import {
|
||||
CalendarDays,
|
||||
Check,
|
||||
Copy,
|
||||
LogOut,
|
||||
ShieldCheck,
|
||||
UserRound,
|
||||
X,
|
||||
} from "lucide-react";
|
||||
import { useState } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import type { User } from "../api/types";
|
||||
import { useAuth } from "../auth/AuthProvider";
|
||||
import { Avatar } from "./Avatar";
|
||||
|
||||
type ProfileDrawerProps = {
|
||||
user: User;
|
||||
};
|
||||
|
||||
const createdAtFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
day: "numeric",
|
||||
month: "long",
|
||||
year: "numeric",
|
||||
});
|
||||
|
||||
export function ProfileDrawer({ user }: ProfileDrawerProps) {
|
||||
const { logout } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const [copied, setCopied] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const [isLoggingOut, setIsLoggingOut] = useState(false);
|
||||
|
||||
async function copyID() {
|
||||
await navigator.clipboard.writeText(user.id);
|
||||
setCopied(true);
|
||||
window.setTimeout(() => setCopied(false), 1800);
|
||||
}
|
||||
|
||||
async function submitLogout() {
|
||||
setError("");
|
||||
setIsLoggingOut(true);
|
||||
try {
|
||||
await logout();
|
||||
navigate("/login", { replace: true });
|
||||
} catch {
|
||||
setError("Не удалось завершить сессию. Попробуйте ещё раз.");
|
||||
setIsLoggingOut(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
aria-label="Закрыть профиль"
|
||||
className="drawer-backdrop"
|
||||
onClick={() => navigate("/app")}
|
||||
type="button"
|
||||
/>
|
||||
<aside className="profile-drawer" aria-label="Профиль пользователя">
|
||||
<header className="profile-drawer__header">
|
||||
<p className="eyebrow">Ваш аккаунт</p>
|
||||
<button
|
||||
aria-label="Закрыть"
|
||||
className="soft-icon-button"
|
||||
onClick={() => navigate("/app")}
|
||||
type="button"
|
||||
>
|
||||
<X aria-hidden="true" />
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<div className="profile-identity">
|
||||
<Avatar label={user.display_name} seed={user.id} size="large" />
|
||||
<h2>{user.display_name}</h2>
|
||||
<p>@{user.username}</p>
|
||||
<span className="profile-badge">
|
||||
<ShieldCheck aria-hidden="true" />
|
||||
серверная сессия
|
||||
</span>
|
||||
</div>
|
||||
|
||||
<dl className="profile-details">
|
||||
<div>
|
||||
<dt><UserRound aria-hidden="true" /> ID аккаунта</dt>
|
||||
<dd>
|
||||
<code>{user.id}</code>
|
||||
<button
|
||||
aria-label="Скопировать ID"
|
||||
className="copy-button"
|
||||
onClick={copyID}
|
||||
type="button"
|
||||
>
|
||||
{copied ? <Check /> : <Copy />}
|
||||
</button>
|
||||
</dd>
|
||||
</div>
|
||||
<div>
|
||||
<dt><CalendarDays aria-hidden="true" /> Дата регистрации</dt>
|
||||
<dd>{createdAtFormatter.format(new Date(user.created_at))}</dd>
|
||||
</div>
|
||||
</dl>
|
||||
|
||||
<div className="profile-roadmap">
|
||||
<strong>Следующие версии</strong>
|
||||
<p>Аватар, редактирование имени и смена пароля появятся после API-контрактов.</p>
|
||||
</div>
|
||||
|
||||
{error && <div className="form-error" role="alert">{error}</div>}
|
||||
|
||||
<button
|
||||
className="logout-button"
|
||||
disabled={isLoggingOut}
|
||||
onClick={submitLogout}
|
||||
type="button"
|
||||
>
|
||||
<LogOut aria-hidden="true" />
|
||||
{isLoggingOut ? "Завершаем…" : "Выйти из аккаунта"}
|
||||
</button>
|
||||
</aside>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
const timeFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
hour: "2-digit",
|
||||
minute: "2-digit",
|
||||
});
|
||||
|
||||
const dayFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
day: "numeric",
|
||||
month: "long",
|
||||
});
|
||||
|
||||
const fullDateFormatter = new Intl.DateTimeFormat("ru-RU", {
|
||||
day: "numeric",
|
||||
month: "long",
|
||||
year: "numeric",
|
||||
});
|
||||
|
||||
export function formatMessageTime(value: string): string {
|
||||
return timeFormatter.format(new Date(value));
|
||||
}
|
||||
|
||||
export function formatDay(value: string): string {
|
||||
const date = new Date(value);
|
||||
const now = new Date();
|
||||
const sameYear = date.getFullYear() === now.getFullYear();
|
||||
return (sameYear ? dayFormatter : fullDateFormatter).format(date);
|
||||
}
|
||||
|
||||
export function dayKey(value: string): string {
|
||||
const date = new Date(value);
|
||||
return [date.getFullYear(), date.getMonth(), date.getDate()].join("-");
|
||||
}
|
||||
|
||||
export function formatMemberCount(count: number): string {
|
||||
const mod10 = count % 10;
|
||||
const mod100 = count % 100;
|
||||
if (mod10 === 1 && mod100 !== 11) {
|
||||
return `${count} участник`;
|
||||
}
|
||||
if (mod10 >= 2 && mod10 <= 4 && (mod100 < 12 || mod100 > 14)) {
|
||||
return `${count} участника`;
|
||||
}
|
||||
return `${count} участников`;
|
||||
}
|
||||
|
||||
export function shortID(id: string): string {
|
||||
return id.length > 12 ? `${id.slice(0, 8)}…` : id;
|
||||
}
|
||||
|
||||
export function initials(value: string): string {
|
||||
const parts = value.trim().split(/\s+/).filter(Boolean);
|
||||
if (parts.length === 0) {
|
||||
return "?";
|
||||
}
|
||||
return parts
|
||||
.slice(0, 2)
|
||||
.map((part) => Array.from(part)[0]?.toUpperCase() ?? "")
|
||||
.join("");
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { createIdempotencyKey } from "./idempotency";
|
||||
|
||||
describe("createIdempotencyKey", () => {
|
||||
it("creates an API-compatible unique key", () => {
|
||||
vi.stubGlobal("crypto", {
|
||||
randomUUID: () => "12345678-1234-1234-1234-123456789abc",
|
||||
});
|
||||
|
||||
const key = createIdempotencyKey();
|
||||
|
||||
expect(key).toMatch(/^[A-Za-z0-9._:-]{1,128}$/);
|
||||
expect(key).toContain("12345678123412341234123456789abc");
|
||||
expect(key.length).toBeLessThanOrEqual(128);
|
||||
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,4 @@
|
||||
export function createIdempotencyKey(): string {
|
||||
const random = crypto.randomUUID().replaceAll("-", "");
|
||||
return `web-${Date.now().toString(36)}-${random}`;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { StrictMode } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import { BrowserRouter } from "react-router-dom";
|
||||
|
||||
import { App } from "./app/App";
|
||||
import { AuthProvider } from "./auth/AuthProvider";
|
||||
import "./styles.css";
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
retry: 1,
|
||||
refetchOnWindowFocus: true,
|
||||
},
|
||||
mutations: {
|
||||
retry: false,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StrictMode>
|
||||
<QueryClientProvider client={queryClient}>
|
||||
<BrowserRouter>
|
||||
<AuthProvider>
|
||||
<App />
|
||||
</AuthProvider>
|
||||
</BrowserRouter>
|
||||
</QueryClientProvider>
|
||||
</StrictMode>,
|
||||
);
|
||||
@@ -0,0 +1,190 @@
|
||||
import {
|
||||
ArrowRight,
|
||||
Eye,
|
||||
EyeOff,
|
||||
LockKeyhole,
|
||||
MessageCircleMore,
|
||||
UserRound,
|
||||
} from "lucide-react";
|
||||
import { useState, type FormEvent } from "react";
|
||||
import { Link, useNavigate } from "react-router-dom";
|
||||
|
||||
import { ApiError } from "../api/client";
|
||||
import { useAuth } from "../auth/AuthProvider";
|
||||
|
||||
type AuthMode = "login" | "register";
|
||||
|
||||
type AuthPageProps = {
|
||||
initialMode: AuthMode;
|
||||
};
|
||||
|
||||
function errorMessage(error: unknown): string {
|
||||
if (error instanceof ApiError) {
|
||||
if (error.status === 401) {
|
||||
return "Неверный логин или пароль";
|
||||
}
|
||||
if (error.status === 409) {
|
||||
return "Такой username уже занят";
|
||||
}
|
||||
return error.message;
|
||||
}
|
||||
return "Не удалось связаться с сервером. Попробуйте ещё раз.";
|
||||
}
|
||||
|
||||
export function AuthPage({ initialMode }: AuthPageProps) {
|
||||
const { login, register } = useAuth();
|
||||
const navigate = useNavigate();
|
||||
const [username, setUsername] = useState("");
|
||||
const [displayName, setDisplayName] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [showPassword, setShowPassword] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
const [isSubmitting, setIsSubmitting] = useState(false);
|
||||
|
||||
const isRegister = initialMode === "register";
|
||||
|
||||
async function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setError("");
|
||||
|
||||
if (!/^[a-z0-9_]{3,32}$/.test(username)) {
|
||||
setError("Username: 3–32 символа, только a–z, цифры и _");
|
||||
return;
|
||||
}
|
||||
if (isRegister && displayName.trim().length === 0) {
|
||||
setError("Укажите отображаемое имя");
|
||||
return;
|
||||
}
|
||||
if (password.length < 12 || Array.from(password).length > 128) {
|
||||
setError("Пароль должен содержать от 12 до 128 символов");
|
||||
return;
|
||||
}
|
||||
|
||||
setIsSubmitting(true);
|
||||
try {
|
||||
if (isRegister) {
|
||||
await register({ username, displayName: displayName.trim(), password });
|
||||
} else {
|
||||
await login({ username, password });
|
||||
}
|
||||
navigate("/app", { replace: true });
|
||||
} catch (submitError) {
|
||||
setError(errorMessage(submitError));
|
||||
} finally {
|
||||
setIsSubmitting(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="auth-page">
|
||||
<section className="auth-story" aria-label="О приложении">
|
||||
<div className="auth-brand">
|
||||
<div className="brand-mark" aria-hidden="true">
|
||||
M
|
||||
</div>
|
||||
<span>MTUCI Messenger</span>
|
||||
</div>
|
||||
|
||||
<div className="auth-copy">
|
||||
<p className="eyebrow">Распределённый мессенджер</p>
|
||||
<h1>Разговоры, которые остаются с вами.</h1>
|
||||
<p>
|
||||
Минималистичный клиент поверх отказоустойчивого Go API. Одна сессия,
|
||||
несколько реплик, никаких лишних отвлечений.
|
||||
</p>
|
||||
<div className="auth-status">
|
||||
<span className="status-dot" />
|
||||
защищённая серверная сессия
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
|
||||
<section className="auth-panel">
|
||||
<div className="auth-card">
|
||||
<MessageCircleMore className="auth-card__icon" aria-hidden="true" />
|
||||
<p className="eyebrow">{isRegister ? "Новый аккаунт" : "С возвращением"}</p>
|
||||
<h2>{isRegister ? "Создать профиль" : "Войти в Messenger"}</h2>
|
||||
<p className="auth-card__lead">
|
||||
{isRegister
|
||||
? "Данные профиля можно будет расширить в следующих версиях."
|
||||
: "Продолжите общение с того места, где остановились."}
|
||||
</p>
|
||||
|
||||
<form className="auth-form" onSubmit={submit}>
|
||||
{isRegister && (
|
||||
<label>
|
||||
<span>Отображаемое имя</span>
|
||||
<div className="input-shell">
|
||||
<UserRound aria-hidden="true" />
|
||||
<input
|
||||
autoComplete="name"
|
||||
maxLength={100}
|
||||
onChange={(event) => setDisplayName(event.target.value)}
|
||||
placeholder="Как к вам обращаться"
|
||||
required
|
||||
value={displayName}
|
||||
/>
|
||||
</div>
|
||||
</label>
|
||||
)}
|
||||
|
||||
<label>
|
||||
<span>Username</span>
|
||||
<div className="input-shell">
|
||||
<UserRound aria-hidden="true" />
|
||||
<input
|
||||
autoCapitalize="none"
|
||||
autoComplete="username"
|
||||
maxLength={32}
|
||||
onChange={(event) => setUsername(event.target.value.toLowerCase())}
|
||||
placeholder="username"
|
||||
required
|
||||
value={username}
|
||||
/>
|
||||
</div>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>Пароль</span>
|
||||
<div className="input-shell">
|
||||
<LockKeyhole aria-hidden="true" />
|
||||
<input
|
||||
autoComplete={isRegister ? "new-password" : "current-password"}
|
||||
maxLength={128}
|
||||
minLength={12}
|
||||
onChange={(event) => setPassword(event.target.value)}
|
||||
placeholder="Минимум 12 символов"
|
||||
required
|
||||
type={showPassword ? "text" : "password"}
|
||||
value={password}
|
||||
/>
|
||||
<button
|
||||
aria-label={showPassword ? "Скрыть пароль" : "Показать пароль"}
|
||||
className="icon-button"
|
||||
onClick={() => setShowPassword((value) => !value)}
|
||||
type="button"
|
||||
>
|
||||
{showPassword ? <EyeOff /> : <Eye />}
|
||||
</button>
|
||||
</div>
|
||||
</label>
|
||||
|
||||
{error && <div className="form-error" role="alert">{error}</div>}
|
||||
|
||||
<button className="primary-button" disabled={isSubmitting} type="submit">
|
||||
<span>{isSubmitting ? "Подождите…" : isRegister ? "Создать аккаунт" : "Войти"}</span>
|
||||
{!isSubmitting && <ArrowRight aria-hidden="true" />}
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<p className="auth-switch">
|
||||
{isRegister ? "Уже есть аккаунт?" : "Впервые здесь?"}{" "}
|
||||
<Link to={isRegister ? "/login" : "/register"}>
|
||||
{isRegister ? "Войти" : "Создать профиль"}
|
||||
</Link>
|
||||
</p>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useState } from "react";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
|
||||
import { api } from "../api/client";
|
||||
import { useAuth } from "../auth/AuthProvider";
|
||||
import { ChatList } from "../components/ChatList";
|
||||
import { ChatView } from "../components/ChatView";
|
||||
import { CreateChatDialog } from "../components/CreateChatDialog";
|
||||
import { EmptyChat } from "../components/EmptyChat";
|
||||
import { NavigationRail } from "../components/NavigationRail";
|
||||
import { ProfileDrawer } from "../components/ProfileDrawer";
|
||||
|
||||
type MessengerPageProps = {
|
||||
profileOpen?: boolean;
|
||||
};
|
||||
|
||||
export function MessengerPage({ profileOpen = false }: MessengerPageProps) {
|
||||
const { user } = useAuth();
|
||||
const { chatId } = useParams();
|
||||
const navigate = useNavigate();
|
||||
const [createChatOpen, setCreateChatOpen] = useState(false);
|
||||
|
||||
const chatsQuery = useQuery({
|
||||
queryKey: ["chats"],
|
||||
queryFn: api.chats,
|
||||
refetchInterval: 10_000,
|
||||
staleTime: 5_000,
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const chats = chatsQuery.data ?? [];
|
||||
const selectedChat = chatId
|
||||
? chats.find((chat) => chat.id === chatId)
|
||||
: undefined;
|
||||
|
||||
return (
|
||||
<main
|
||||
className={[
|
||||
"messenger-shell",
|
||||
chatId ? "messenger-shell--chat-open" : "",
|
||||
profileOpen ? "messenger-shell--drawer-open" : "",
|
||||
].join(" ")}
|
||||
>
|
||||
<NavigationRail profileOpen={profileOpen} user={user} />
|
||||
|
||||
<ChatList
|
||||
chats={chats}
|
||||
currentUser={user}
|
||||
error={chatsQuery.isError}
|
||||
isLoading={chatsQuery.isLoading}
|
||||
onCreateChat={() => setCreateChatOpen(true)}
|
||||
selectedChatID={chatId}
|
||||
/>
|
||||
|
||||
{selectedChat ? (
|
||||
<ChatView
|
||||
chat={selectedChat}
|
||||
currentUser={user}
|
||||
onBack={() => navigate("/app")}
|
||||
/>
|
||||
) : (
|
||||
<EmptyChat
|
||||
hasChats={chats.length > 0}
|
||||
onCreateChat={() => setCreateChatOpen(true)}
|
||||
/>
|
||||
)}
|
||||
|
||||
{profileOpen && <ProfileDrawer user={user} />}
|
||||
{createChatOpen && (
|
||||
<CreateChatDialog
|
||||
currentUser={user}
|
||||
onClose={() => setCreateChatOpen(false)}
|
||||
/>
|
||||
)}
|
||||
</main>
|
||||
);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
Vendored
+1
@@ -0,0 +1 @@
|
||||
/// <reference types="vite/client" />
|
||||
@@ -0,0 +1,21 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
||||
"target": "ES2022",
|
||||
"useDefineForClassFields": true,
|
||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||
"allowJs": false,
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"allowSyntheticDefaultImports": true,
|
||||
"strict": true,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"resolveJsonModule": true,
|
||||
"isolatedModules": true,
|
||||
"noEmit": true,
|
||||
"jsx": "react-jsx"
|
||||
},
|
||||
"include": ["src"]
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"files": [],
|
||||
"references": [
|
||||
{ "path": "./tsconfig.app.json" },
|
||||
{ "path": "./tsconfig.node.json" }
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
|
||||
"target": "ES2023",
|
||||
"lib": ["ES2023"],
|
||||
"module": "ESNext",
|
||||
"skipLibCheck": true,
|
||||
"moduleResolution": "Bundler",
|
||||
"allowImportingTsExtensions": true,
|
||||
"verbatimModuleSyntax": true,
|
||||
"moduleDetection": "force",
|
||||
"noEmit": true
|
||||
},
|
||||
"include": ["vite.config.ts"]
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { defineConfig } from "vitest/config";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
port: 5173,
|
||||
strictPort: true,
|
||||
proxy: {
|
||||
"/api": "http://localhost:8080",
|
||||
"/web-api": "http://localhost:8080",
|
||||
"/health": "http://localhost:8080",
|
||||
},
|
||||
},
|
||||
preview: {
|
||||
port: 4173,
|
||||
strictPort: true,
|
||||
},
|
||||
test: {
|
||||
environment: "node",
|
||||
include: ["src/**/*.test.ts"],
|
||||
},
|
||||
});
|
||||
@@ -0,0 +1,35 @@
|
||||
module mtusi2026lab4
|
||||
|
||||
go 1.27.1
|
||||
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/minio/minio-go/v7 v7.4.1
|
||||
github.com/redis/go-redis/v9 v9.23.0
|
||||
golang.org/x/crypto v0.57.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/klauspost/compress v1.19.2 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
|
||||
github.com/klauspost/crc32 v1.3.0 // indirect
|
||||
github.com/minio/crc64nvme v1.1.1 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/tinylib/msgp v1.6.4 // indirect
|
||||
github.com/zeebo/xxh3 v1.1.0 // indirect
|
||||
go.uber.org/atomic v1.12.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/net v0.60.0 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
gopkg.in/ini.v1 v1.67.3 // indirect
|
||||
)
|
||||
@@ -0,0 +1,77 @@
|
||||
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/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/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/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
|
||||
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
||||
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
|
||||
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
|
||||
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
|
||||
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
github.com/minio/minio-go/v7 v7.4.1 h1:PtU/zIOfQRWZ+QydXAZysc+xZYHdG+BBR4Z6TUvy73w=
|
||||
github.com/minio/minio-go/v7 v7.4.1/go.mod h1:nQnQi7zTfXR4lREtYXZQWqo5aw0Rqv+ZqCGrTORygRk=
|
||||
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/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/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
|
||||
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
|
||||
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||
github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
|
||||
github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
|
||||
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
|
||||
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.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.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
|
||||
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw=
|
||||
gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1,7 @@
|
||||
FROM haproxy:3.2-alpine@sha256:dfba86f031bd4c9039f10f8cc5de75991d1b6766d5460f0335b980408503baad
|
||||
|
||||
COPY infrastructure/haproxy/haproxy.cfg /usr/local/etc/haproxy/haproxy.cfg
|
||||
|
||||
USER haproxy
|
||||
|
||||
EXPOSE 8080
|
||||
@@ -0,0 +1,66 @@
|
||||
global
|
||||
log stdout format raw local0
|
||||
maxconn 2048
|
||||
|
||||
defaults
|
||||
log global
|
||||
mode http
|
||||
option httplog
|
||||
timeout connect 1s
|
||||
timeout client 30s
|
||||
timeout server 30s
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold other 1s
|
||||
hold refused 1s
|
||||
hold nx 1s
|
||||
hold timeout 1s
|
||||
hold obsolete 1s
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
|
||||
acl api_request path_beg /api/ /web-api/ /health/
|
||||
acl web_api path_beg /web-api/
|
||||
acl has_session req.cook(mtusi_session) -m found
|
||||
acl message_create method POST
|
||||
acl message_path path_reg ^/(api|web-api)/v1/chats/[^/]+/messages$
|
||||
|
||||
http-request del-header Authorization if web_api
|
||||
http-request set-header Authorization "Bearer %[req.cook(mtusi_session)]" if web_api has_session
|
||||
http-request del-header Cookie if api_request
|
||||
http-request replace-path ^/web-api/(.*)$ /api/\1 if web_api
|
||||
|
||||
use_backend message_backend if message_create message_path
|
||||
use_backend api_backend if api_request
|
||||
default_backend frontend_backend
|
||||
|
||||
backend api_backend
|
||||
balance leastconn
|
||||
option httpchk
|
||||
http-check send meth GET uri /health/lb ver HTTP/1.1 hdr Host api
|
||||
http-check expect status 200
|
||||
default-server check inter 1s fastinter 500ms downinter 1s fall 2 rise 2 resolvers docker init-addr libc,none
|
||||
server-template api 8 api:8080
|
||||
|
||||
backend message_backend
|
||||
balance leastconn
|
||||
option httpchk
|
||||
option redispatch
|
||||
option http-buffer-request
|
||||
retries 3
|
||||
retry-on conn-failure empty-response response-timeout 502 503 504
|
||||
http-check send meth GET uri /health/lb ver HTTP/1.1 hdr Host api
|
||||
http-check expect status 200
|
||||
default-server check inter 1s fastinter 500ms downinter 1s fall 2 rise 2 resolvers docker init-addr libc,none
|
||||
server-template api 8 api:8080
|
||||
|
||||
backend frontend_backend
|
||||
option httpchk GET /
|
||||
http-check expect status 200
|
||||
server frontend frontend:8080 check inter 2s fall 2 rise 2 resolvers docker init-addr libc,none
|
||||
@@ -0,0 +1,40 @@
|
||||
FROM golang:1.27.1-alpine@sha256:8a5910f31396cd4d89662f56c68b3ae31d374308270a1c3bd96672ee5ed43414 AS minio-builder
|
||||
|
||||
ARG MINIO_VERSION=RELEASE.2025-10-15T17-29-55Z
|
||||
RUN CGO_ENABLED=0 GOBIN=/out go install github.com/minio/minio@${MINIO_VERSION}
|
||||
|
||||
FROM golang:1.27.1-alpine@sha256:8a5910f31396cd4d89662f56c68b3ae31d374308270a1c3bd96672ee5ed43414 AS mc-builder
|
||||
|
||||
ARG MC_VERSION=RELEASE.2025-08-13T08-35-41Z
|
||||
RUN CGO_ENABLED=0 GOBIN=/out go install github.com/minio/mc@${MC_VERSION}
|
||||
|
||||
FROM alpine:3.24@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6 AS server
|
||||
|
||||
RUN apk add --no-cache ca-certificates wget && \
|
||||
addgroup -g 1000 minio && \
|
||||
adduser -u 1000 -G minio -s /sbin/nologin -D minio && \
|
||||
mkdir -p /data && \
|
||||
chown minio:minio /data
|
||||
|
||||
COPY --from=minio-builder /out/minio /usr/local/bin/minio
|
||||
|
||||
USER minio:minio
|
||||
VOLUME ["/data"]
|
||||
EXPOSE 9000
|
||||
|
||||
HEALTHCHECK --interval=5s --timeout=3s --start-period=5s --retries=20 \
|
||||
CMD wget -qO- http://127.0.0.1:9000/minio/health/live >/dev/null || exit 1
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/minio"]
|
||||
CMD ["server", "/data", "--address", ":9000"]
|
||||
|
||||
FROM alpine:3.24@sha256:294b683cb724975bec92580e1e685676bd4b50bda910ddb8c51d4cabeaec77e6 AS client
|
||||
|
||||
RUN apk add --no-cache ca-certificates && \
|
||||
addgroup -g 1000 minio && \
|
||||
adduser -u 1000 -G minio -s /sbin/nologin -D minio
|
||||
|
||||
COPY --from=mc-builder /out/mc /usr/local/bin/mc
|
||||
|
||||
USER minio:minio
|
||||
ENTRYPOINT ["/usr/local/bin/mc"]
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": [
|
||||
"s3:GetBucketLocation",
|
||||
"s3:ListBucket"
|
||||
],
|
||||
"Resource": [
|
||||
"arn:aws:s3:::__BUCKET__"
|
||||
]
|
||||
},
|
||||
{
|
||||
"Effect": "Allow",
|
||||
"Action": [
|
||||
"s3:GetObject",
|
||||
"s3:PutObject",
|
||||
"s3:DeleteObject"
|
||||
],
|
||||
"Resource": [
|
||||
"arn:aws:s3:::__BUCKET__/*"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
FROM node:24-alpine@sha256:44fce696f38ba8c987c4db89954051000bbd05ffd8d8659ba15ed4a8602e937f AS frontend-build
|
||||
|
||||
WORKDIR /frontend
|
||||
COPY frontend/package.json frontend/package-lock.json ./
|
||||
RUN npm ci
|
||||
|
||||
COPY frontend/ ./
|
||||
RUN npm test && npm run build
|
||||
|
||||
FROM nginx:1.30.6-alpine@sha256:970364228b1e4f932abc22d321932816782cca8a20b85a77552a7cfe9acfb3b5
|
||||
|
||||
COPY infrastructure/nginx/nginx.conf /etc/nginx/nginx.conf
|
||||
COPY --from=frontend-build /frontend/dist /usr/share/nginx/html
|
||||
|
||||
EXPOSE 8080
|
||||
@@ -0,0 +1,32 @@
|
||||
worker_processes auto;
|
||||
|
||||
events {
|
||||
worker_connections 512;
|
||||
}
|
||||
|
||||
http {
|
||||
include /etc/nginx/mime.types;
|
||||
default_type application/octet-stream;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
server_name _;
|
||||
root /usr/share/nginx/html;
|
||||
index index.html;
|
||||
|
||||
add_header X-Content-Type-Options "nosniff" always;
|
||||
add_header Referrer-Policy "no-referrer" always;
|
||||
add_header X-Frame-Options "DENY" always;
|
||||
add_header Content-Security-Policy "default-src 'self'; img-src 'self' data:; style-src 'self'; script-src 'self'; connect-src 'self'; frame-ancestors 'none'; base-uri 'self'; form-action 'self'" always;
|
||||
|
||||
location /assets/ {
|
||||
try_files $uri =404;
|
||||
expires 1y;
|
||||
add_header Cache-Control "public, immutable";
|
||||
}
|
||||
|
||||
location / {
|
||||
try_files $uri $uri/ /index.html;
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+99
@@ -0,0 +1,99 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log"
|
||||
|
||||
"mtusi2026lab4/internal/adapter/redis"
|
||||
"mtusi2026lab4/internal/application"
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
// CachedRepository декорирует основной Repository. Все операции, кроме чтения
|
||||
// и создания чата, автоматически делегируются встроенному Repository.
|
||||
type CachedRepository struct {
|
||||
application.Repository
|
||||
chatCache redis.ChatCache
|
||||
logger *log.Logger
|
||||
}
|
||||
|
||||
func NewCachedRepository(
|
||||
repository application.Repository,
|
||||
chatCache redis.ChatCache,
|
||||
logger *log.Logger,
|
||||
) *CachedRepository {
|
||||
if logger == nil {
|
||||
logger = log.Default()
|
||||
}
|
||||
|
||||
return &CachedRepository{
|
||||
Repository: repository,
|
||||
chatCache: chatCache,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CachedRepository) CreateChat(
|
||||
ctx context.Context,
|
||||
title string,
|
||||
memberIDs []string,
|
||||
) (domain.Chat, error) {
|
||||
chat, err := r.Repository.CreateChat(ctx, title, memberIDs)
|
||||
if err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
|
||||
// Запись в PostgreSQL уже подтверждена. Ошибка необязательного кэша не
|
||||
// должна превращать успешную бизнес-операцию в 503.
|
||||
if err = r.chatCache.Set(ctx, chat); err != nil {
|
||||
r.logger.Printf("[WARN] Redis chat cache write failed: %v", err)
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (r *CachedRepository) GetChatByID(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
callerID string,
|
||||
) (domain.Chat, error) {
|
||||
chat, err := r.chatCache.Get(ctx, chatID)
|
||||
if err == nil {
|
||||
if !containsMember(chat.MemberIDs, callerID) {
|
||||
return domain.Chat{}, domain.ErrNotFound
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
shouldFillCache := errors.Is(err, redis.ErrMiss)
|
||||
if !shouldFillCache {
|
||||
r.logger.Printf("[WARN] Redis chat cache read failed: %v", err)
|
||||
}
|
||||
|
||||
chat, err = r.Repository.GetChatByID(ctx, chatID, callerID)
|
||||
if err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
|
||||
if shouldFillCache {
|
||||
if err = r.chatCache.Set(ctx, chat); err != nil {
|
||||
r.logger.Printf("[WARN] Redis chat cache fill failed: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func containsMember(memberIDs []string, callerID string) bool {
|
||||
for _, memberID := range memberIDs {
|
||||
if memberID == callerID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
var _ application.Repository = (*CachedRepository)(nil)
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"testing"
|
||||
|
||||
"mtusi2026lab4/internal/adapter/memory"
|
||||
"mtusi2026lab4/internal/adapter/redis"
|
||||
"mtusi2026lab4/internal/application"
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type fakeChatCache struct {
|
||||
chats map[string]domain.Chat
|
||||
getErr error
|
||||
setErr error
|
||||
sets int
|
||||
}
|
||||
|
||||
func (c *fakeChatCache) Get(
|
||||
_ context.Context,
|
||||
chatID string,
|
||||
) (domain.Chat, error) {
|
||||
if c.getErr != nil {
|
||||
return domain.Chat{}, c.getErr
|
||||
}
|
||||
|
||||
chat, exists := c.chats[chatID]
|
||||
if !exists {
|
||||
return domain.Chat{}, redis.ErrMiss
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (c *fakeChatCache) Set(
|
||||
_ context.Context,
|
||||
chat domain.Chat,
|
||||
) error {
|
||||
c.sets++
|
||||
if c.setErr != nil {
|
||||
return c.setErr
|
||||
}
|
||||
c.chats[chat.ID] = chat
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type countingRepository struct {
|
||||
application.Repository
|
||||
getChatCalls int
|
||||
}
|
||||
|
||||
func (r *countingRepository) GetChatByID(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
callerID string,
|
||||
) (domain.Chat, error) {
|
||||
r.getChatCalls++
|
||||
return r.Repository.GetChatByID(ctx, chatID, callerID)
|
||||
}
|
||||
|
||||
func cachedRepositoryFixture(
|
||||
t *testing.T,
|
||||
) (*countingRepository, *fakeChatCache, domain.User, domain.User, domain.Chat) {
|
||||
t.Helper()
|
||||
|
||||
legacy := memory.NewMemoryStore()
|
||||
member, err := legacy.CreateUser("cache_member", "Участник")
|
||||
if err != nil {
|
||||
t.Fatalf("create member: %v", err)
|
||||
}
|
||||
outsider, err := legacy.CreateUser("cache_outsider", "Посторонний")
|
||||
if err != nil {
|
||||
t.Fatalf("create outsider: %v", err)
|
||||
}
|
||||
chat, err := legacy.CreateChat("Кешируемый чат", []string{member.ID})
|
||||
if err != nil {
|
||||
t.Fatalf("create chat: %v", err)
|
||||
}
|
||||
|
||||
repository := &countingRepository{
|
||||
Repository: memory.NewMemoryRepository(legacy),
|
||||
}
|
||||
chatCache := &fakeChatCache{
|
||||
chats: make(map[string]domain.Chat),
|
||||
}
|
||||
|
||||
return repository, chatCache, member, outsider, chat
|
||||
}
|
||||
|
||||
func TestCachedRepositoryMissThenHit(t *testing.T) {
|
||||
repository, chatCache, member, _, expected := cachedRepositoryFixture(t)
|
||||
cached := NewCachedRepository(
|
||||
repository,
|
||||
chatCache,
|
||||
log.New(io.Discard, "", 0),
|
||||
)
|
||||
|
||||
first, err := cached.GetChatByID(context.Background(), expected.ID, member.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("first read: %v", err)
|
||||
}
|
||||
second, err := cached.GetChatByID(context.Background(), expected.ID, member.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("second read: %v", err)
|
||||
}
|
||||
|
||||
if repository.getChatCalls != 1 {
|
||||
t.Fatalf("expected one source read, got %d", repository.getChatCalls)
|
||||
}
|
||||
if chatCache.sets != 1 {
|
||||
t.Fatalf("expected one cache fill, got %d", chatCache.sets)
|
||||
}
|
||||
if first.ID != expected.ID || second.ID != expected.ID {
|
||||
t.Fatal("cache changed chat identity")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachedRepositoryFallsBackWhenCacheUnavailable(t *testing.T) {
|
||||
repository, chatCache, member, _, expected := cachedRepositoryFixture(t)
|
||||
chatCache.getErr = errors.New("redis unavailable")
|
||||
chatCache.setErr = errors.New("redis unavailable")
|
||||
cached := NewCachedRepository(
|
||||
repository,
|
||||
chatCache,
|
||||
log.New(io.Discard, "", 0),
|
||||
)
|
||||
|
||||
chat, err := cached.GetChatByID(context.Background(), expected.ID, member.ID)
|
||||
if err != nil {
|
||||
t.Fatalf("fallback read: %v", err)
|
||||
}
|
||||
if chat.ID != expected.ID || repository.getChatCalls != 1 {
|
||||
t.Fatal("source repository was not used after cache failure")
|
||||
}
|
||||
if chatCache.sets != 0 {
|
||||
t.Fatal("failed cache read must not trigger another cache operation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachedRepositoryChecksMembershipOnHit(t *testing.T) {
|
||||
repository, chatCache, _, outsider, expected := cachedRepositoryFixture(t)
|
||||
chatCache.chats[expected.ID] = expected
|
||||
cached := NewCachedRepository(
|
||||
repository,
|
||||
chatCache,
|
||||
log.New(io.Discard, "", 0),
|
||||
)
|
||||
|
||||
_, err := cached.GetChatByID(context.Background(), expected.ID, outsider.ID)
|
||||
if !errors.Is(err, domain.ErrNotFound) {
|
||||
t.Fatalf("expected ErrNotFound for outsider, got %v", err)
|
||||
}
|
||||
if repository.getChatCalls != 0 {
|
||||
t.Fatal("cache hit unexpectedly queried source repository")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCachedRepositoryIgnoresWriteFailure(t *testing.T) {
|
||||
repository, chatCache, member, _, _ := cachedRepositoryFixture(t)
|
||||
chatCache.setErr = errors.New("redis unavailable")
|
||||
cached := NewCachedRepository(
|
||||
repository,
|
||||
chatCache,
|
||||
log.New(io.Discard, "", 0),
|
||||
)
|
||||
|
||||
chat, err := cached.CreateChat(
|
||||
context.Background(),
|
||||
"Новый чат",
|
||||
[]string{member.ID},
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create chat with unavailable cache: %v", err)
|
||||
}
|
||||
if chat.ID == "" || chatCache.sets != 1 {
|
||||
t.Fatal("chat was not created before best-effort cache write")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Checker interface {
|
||||
Ping(ctx context.Context) error
|
||||
}
|
||||
|
||||
type MultiChecker struct {
|
||||
checkers []Checker
|
||||
}
|
||||
|
||||
func NewMultiChecker(checkers ...Checker) *MultiChecker {
|
||||
filtered := make([]Checker, 0, len(checkers))
|
||||
for _, checker := range checkers {
|
||||
if checker != nil {
|
||||
filtered = append(filtered, checker)
|
||||
}
|
||||
}
|
||||
return &MultiChecker{checkers: filtered}
|
||||
}
|
||||
|
||||
func (checker *MultiChecker) Ping(ctx context.Context) error {
|
||||
for index, dependency := range checker.checkers {
|
||||
if err := dependency.Ping(ctx); err != nil {
|
||||
return fmt.Errorf("dependency %d: %w", index, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package memory
|
||||
|
||||
import "mtusi2026lab4/internal/domain"
|
||||
|
||||
var (
|
||||
ErrNotFound = domain.ErrNotFound
|
||||
ErrConflict = domain.ErrConflict
|
||||
)
|
||||
@@ -0,0 +1,377 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/application"
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type memorySession struct {
|
||||
userID string
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
type memoryIdempotencyScope struct {
|
||||
actorID string
|
||||
method string
|
||||
canonicalPath string
|
||||
key string
|
||||
}
|
||||
|
||||
type memoryIdempotencyResult struct {
|
||||
requestHash [32]byte
|
||||
message domain.Message
|
||||
expiresAt time.Time
|
||||
}
|
||||
|
||||
type MemoryRepository struct {
|
||||
legacy *MemoryStore
|
||||
|
||||
mu sync.RWMutex
|
||||
passwordByUserID map[string]string
|
||||
userIDByUsername map[string]string
|
||||
sessionsByHashKey map[[32]byte]memorySession
|
||||
idempotencyByScope map[memoryIdempotencyScope]memoryIdempotencyResult
|
||||
}
|
||||
|
||||
func NewMemoryRepository(legacy *MemoryStore) *MemoryRepository {
|
||||
return &MemoryRepository{
|
||||
legacy: legacy,
|
||||
passwordByUserID: make(map[string]string),
|
||||
userIDByUsername: make(map[string]string),
|
||||
sessionsByHashKey: make(map[[32]byte]memorySession),
|
||||
idempotencyByScope: make(map[memoryIdempotencyScope]memoryIdempotencyResult),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) CreateUser(
|
||||
ctx context.Context,
|
||||
username string,
|
||||
displayName string,
|
||||
passwordHash string,
|
||||
) (domain.User, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
|
||||
user, err := r.legacy.CreateUser(username, displayName)
|
||||
if err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
r.passwordByUserID[user.ID] = passwordHash
|
||||
r.userIDByUsername[user.Username] = user.ID
|
||||
r.mu.Unlock()
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) GetUserByID(
|
||||
ctx context.Context,
|
||||
userID string,
|
||||
) (domain.User, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
|
||||
return r.legacy.GetUserByID(userID)
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) GetUserByUsername(
|
||||
ctx context.Context,
|
||||
username string,
|
||||
) (domain.User, string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.User{}, "", err
|
||||
}
|
||||
|
||||
r.mu.RLock()
|
||||
userID, exists := r.userIDByUsername[username]
|
||||
passwordHash := r.passwordByUserID[userID]
|
||||
r.mu.RUnlock()
|
||||
|
||||
if !exists {
|
||||
return domain.User{}, "", ErrNotFound
|
||||
}
|
||||
|
||||
user, err := r.legacy.GetUserByID(userID)
|
||||
if err != nil {
|
||||
return domain.User{}, "", err
|
||||
}
|
||||
|
||||
return user, passwordHash, nil
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) CreateChat(
|
||||
ctx context.Context,
|
||||
title string,
|
||||
memberIDs []string,
|
||||
) (domain.Chat, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
|
||||
return r.legacy.CreateChat(title, memberIDs)
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) GetChatByID(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
callerID string,
|
||||
) (domain.Chat, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
|
||||
return r.legacy.GetChatByID(chatID, callerID)
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) GetChatsForUser(
|
||||
ctx context.Context,
|
||||
userID string,
|
||||
limit int,
|
||||
cursor *domain.ChatCursor,
|
||||
) ([]domain.Chat, bool, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
return r.legacy.GetChatsForUser(userID, limit, cursor)
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) CreateMessageIdempotently(
|
||||
ctx context.Context,
|
||||
write application.IdempotentMessageWrite,
|
||||
) (domain.Message, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
|
||||
scope := memoryIdempotencyScope{
|
||||
actorID: write.ActorID,
|
||||
method: write.Method,
|
||||
canonicalPath: write.CanonicalPath,
|
||||
key: write.Key,
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
|
||||
if _, err := r.legacy.GetChatByID(write.ChatID, write.ActorID); err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
for _, attachmentID := range write.AttachmentIDs {
|
||||
attachment, err := r.legacy.GetAttachmentByID(attachmentID)
|
||||
if err != nil || attachment.ChatID != write.ChatID {
|
||||
return domain.Message{}, ErrNotFound
|
||||
}
|
||||
if attachment.Status != domain.AttachmentStatusReady {
|
||||
return domain.Message{}, application.ErrAttachmentNotReady
|
||||
}
|
||||
}
|
||||
|
||||
if stored, exists := r.idempotencyByScope[scope]; exists {
|
||||
if stored.expiresAt.After(now) {
|
||||
if stored.requestHash != write.RequestHash {
|
||||
return domain.Message{}, application.ErrIdempotencyConflict
|
||||
}
|
||||
return stored.message, nil
|
||||
}
|
||||
delete(r.idempotencyByScope, scope)
|
||||
}
|
||||
|
||||
message, err := r.legacy.CreateMessage(
|
||||
write.ChatID,
|
||||
write.ActorID,
|
||||
write.Text,
|
||||
write.AttachmentIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return domain.Message{}, err
|
||||
}
|
||||
r.idempotencyByScope[scope] = memoryIdempotencyResult{
|
||||
requestHash: write.RequestHash,
|
||||
message: message,
|
||||
expiresAt: now.Add(write.TTL),
|
||||
}
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) GetMessages(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
callerID string,
|
||||
limit int,
|
||||
cursor *domain.MessageCursor,
|
||||
) ([]domain.Message, bool, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
return r.legacy.GetMessages(
|
||||
chatID,
|
||||
callerID,
|
||||
limit,
|
||||
cursor,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) CreateSession(
|
||||
ctx context.Context,
|
||||
tokenHash []byte,
|
||||
userID string,
|
||||
expiresAt time.Time,
|
||||
) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
key, err := memoryTokenHashKey(tokenHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
r.sessionsByHashKey[key] = memorySession{
|
||||
userID: userID,
|
||||
expiresAt: expiresAt,
|
||||
}
|
||||
r.mu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) GetUserBySession(
|
||||
ctx context.Context,
|
||||
tokenHash []byte,
|
||||
) (domain.User, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.User{}, err
|
||||
}
|
||||
|
||||
key, err := memoryTokenHashKey(tokenHash)
|
||||
if err != nil {
|
||||
return domain.User{}, ErrNotFound
|
||||
}
|
||||
|
||||
r.mu.RLock()
|
||||
session, exists := r.sessionsByHashKey[key]
|
||||
r.mu.RUnlock()
|
||||
|
||||
if !exists || !session.expiresAt.After(time.Now().UTC()) {
|
||||
return domain.User{}, ErrNotFound
|
||||
}
|
||||
|
||||
return r.legacy.GetUserByID(session.userID)
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) DeleteSession(
|
||||
ctx context.Context,
|
||||
tokenHash []byte,
|
||||
) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
key, err := memoryTokenHashKey(tokenHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
r.mu.Lock()
|
||||
delete(r.sessionsByHashKey, key)
|
||||
r.mu.Unlock()
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) CreateAttachment(
|
||||
ctx context.Context,
|
||||
attachment domain.Attachment,
|
||||
) (domain.Attachment, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if err := r.legacy.PutAttachment(attachment); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) MarkAttachmentReady(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
updatedAt time.Time,
|
||||
) (domain.Attachment, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
return r.legacy.UpdateAttachmentStatus(
|
||||
attachmentID,
|
||||
domain.AttachmentStatusReady,
|
||||
nil,
|
||||
updatedAt,
|
||||
)
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) MarkAttachmentFailed(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
errorCode string,
|
||||
updatedAt time.Time,
|
||||
) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err := r.legacy.UpdateAttachmentStatus(
|
||||
attachmentID,
|
||||
domain.AttachmentStatusFailed,
|
||||
&errorCode,
|
||||
updatedAt,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
func (r *MemoryRepository) GetAttachment(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
callerID string,
|
||||
) (domain.Attachment, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
attachment, err := r.legacy.GetAttachmentByID(attachmentID)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if _, err = r.legacy.GetChatByID(attachment.ChatID, callerID); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if attachment.Variants == nil {
|
||||
attachment.Variants = make([]domain.AttachmentVariant, 0)
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func memoryTokenHashKey(tokenHash []byte) ([32]byte, error) {
|
||||
var key [32]byte
|
||||
|
||||
if len(tokenHash) != len(key) {
|
||||
return key, fmt.Errorf(
|
||||
"session token hash must contain %d bytes",
|
||||
len(key),
|
||||
)
|
||||
}
|
||||
|
||||
copy(key[:], tokenHash)
|
||||
|
||||
return key, nil
|
||||
}
|
||||
|
||||
var _ application.Repository = (*MemoryRepository)(nil)
|
||||
@@ -0,0 +1,371 @@
|
||||
package memory
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sort"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type MemoryStore struct {
|
||||
mu sync.RWMutex
|
||||
|
||||
usersByID map[string]domain.User //key = user UUID
|
||||
usersByUsername map[string]string //key = username для быстрой проверки уникальности
|
||||
|
||||
chatsByID map[string]domain.Chat //key = chat UUID
|
||||
|
||||
messagesByChatID map[string][]domain.Message // key = chat UUID, value = слайс сообщений
|
||||
|
||||
attachmentsByID map[string]domain.Attachment
|
||||
}
|
||||
|
||||
func NewMemoryStore() *MemoryStore {
|
||||
return &MemoryStore{
|
||||
usersByID: make(map[string]domain.User),
|
||||
usersByUsername: make(map[string]string),
|
||||
chatsByID: make(map[string]domain.Chat),
|
||||
messagesByChatID: make(map[string][]domain.Message),
|
||||
attachmentsByID: make(map[string]domain.Attachment),
|
||||
}
|
||||
}
|
||||
|
||||
// ---пользователи---
|
||||
|
||||
func (s *MemoryStore) CreateUser(username, displayName string) (domain.User, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
// дубликат username -> 409 Conflict
|
||||
if _, exists := s.usersByUsername[username]; exists {
|
||||
return domain.User{}, ErrConflict
|
||||
}
|
||||
|
||||
user := domain.User{
|
||||
ID: uuid.NewString(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
|
||||
s.usersByID[user.ID] = user
|
||||
s.usersByUsername[user.Username] = user.ID
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetUserByID(id string) (domain.User, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
user, exists := s.usersByID[id]
|
||||
if !exists {
|
||||
return domain.User{}, ErrNotFound
|
||||
}
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// ---чаты---
|
||||
|
||||
func (s *MemoryStore) CreateChat(title string, memberIDs []string) (domain.Chat, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
//все участники рил существуют
|
||||
for _, memberID := range memberIDs {
|
||||
if _, exists := s.usersByID[memberID]; !exists {
|
||||
return domain.Chat{}, fmt.Errorf(
|
||||
"%w: member %s",
|
||||
ErrNotFound,
|
||||
memberID,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
chat := domain.Chat{
|
||||
ID: uuid.NewString(),
|
||||
Title: title,
|
||||
MemberIDs: append([]string(nil), memberIDs...), //копирую слайс для изоляции
|
||||
CreatedAt: time.Now().UTC(),
|
||||
}
|
||||
|
||||
s.chatsByID[chat.ID] = chat
|
||||
s.messagesByChatID[chat.ID] = make([]domain.Message, 0)
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetChatByID(chatID, callerID string) (domain.Chat, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
chat, exists := s.chatsByID[chatID]
|
||||
if !exists {
|
||||
return domain.Chat{}, ErrNotFound
|
||||
}
|
||||
|
||||
// Требование безопасности: посторонний получает 404, а не 403
|
||||
isMember := false
|
||||
for _, m := range chat.MemberIDs {
|
||||
if m == callerID {
|
||||
isMember = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if !isMember {
|
||||
return domain.Chat{}, ErrNotFound
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetChatsForUser(
|
||||
userID string,
|
||||
limit int,
|
||||
cursorData *domain.ChatCursor,
|
||||
) ([]domain.Chat, bool, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
chats := make([]domain.Chat, 0)
|
||||
|
||||
for _, chat := range s.chatsByID {
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == userID {
|
||||
chats = append(chats, chat)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(chats, func(i, j int) bool {
|
||||
if chats[i].CreatedAt.Equal(chats[j].CreatedAt) {
|
||||
return chats[i].ID < chats[j].ID
|
||||
}
|
||||
|
||||
return chats[i].CreatedAt.Before(chats[j].CreatedAt)
|
||||
})
|
||||
|
||||
start := 0
|
||||
|
||||
if cursorData != nil {
|
||||
start = sort.Search(len(chats), func(index int) bool {
|
||||
chat := chats[index]
|
||||
|
||||
if chat.CreatedAt.After(cursorData.CreatedAt) {
|
||||
return true
|
||||
}
|
||||
|
||||
return chat.CreatedAt.Equal(cursorData.CreatedAt) &&
|
||||
chat.ID > cursorData.ChatID
|
||||
})
|
||||
}
|
||||
|
||||
if start >= len(chats) {
|
||||
return make([]domain.Chat, 0), false, nil
|
||||
}
|
||||
|
||||
end := start + limit
|
||||
if end > len(chats) {
|
||||
end = len(chats)
|
||||
}
|
||||
|
||||
items := make([]domain.Chat, end-start)
|
||||
copy(items, chats[start:end])
|
||||
return items, end < len(chats), nil
|
||||
}
|
||||
|
||||
// PutAttachment stores metadata for adapter and application tests. Binary
|
||||
// content intentionally does not belong to this in-memory metadata store.
|
||||
func (s *MemoryStore) PutAttachment(attachment domain.Attachment) error {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
if _, exists := s.attachmentsByID[attachment.ID]; exists {
|
||||
return ErrConflict
|
||||
}
|
||||
|
||||
chat, exists := s.chatsByID[attachment.ChatID]
|
||||
if !exists {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
ownerIsMember := false
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == attachment.OwnerID {
|
||||
ownerIsMember = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !ownerIsMember {
|
||||
return ErrNotFound
|
||||
}
|
||||
|
||||
s.attachmentsByID[attachment.ID] = attachment
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetAttachmentByID(
|
||||
attachmentID string,
|
||||
) (domain.Attachment, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
attachment, exists := s.attachmentsByID[attachmentID]
|
||||
if !exists {
|
||||
return domain.Attachment{}, ErrNotFound
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) UpdateAttachmentStatus(
|
||||
attachmentID string,
|
||||
status domain.AttachmentStatus,
|
||||
errorCode *string,
|
||||
updatedAt time.Time,
|
||||
) (domain.Attachment, error) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
attachment, exists := s.attachmentsByID[attachmentID]
|
||||
if !exists {
|
||||
return domain.Attachment{}, ErrNotFound
|
||||
}
|
||||
if attachment.Status != domain.AttachmentStatusPending {
|
||||
return domain.Attachment{}, ErrConflict
|
||||
}
|
||||
attachment.Status = status
|
||||
attachment.ErrorCode = errorCode
|
||||
attachment.UpdatedAt = updatedAt
|
||||
if attachment.Variants == nil {
|
||||
attachment.Variants = make([]domain.AttachmentVariant, 0)
|
||||
}
|
||||
s.attachmentsByID[attachmentID] = attachment
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
// ---Сообщения---
|
||||
|
||||
func (s *MemoryStore) CreateMessage(
|
||||
chatID, senderID, text string,
|
||||
attachmentLists ...[]string,
|
||||
) (domain.Message, error) {
|
||||
messageID := uuid.NewString()
|
||||
attachmentIDs := make([]string, 0)
|
||||
if len(attachmentLists) > 0 {
|
||||
attachmentIDs = append(attachmentIDs, attachmentLists[0]...)
|
||||
}
|
||||
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
chat, exists := s.chatsByID[chatID]
|
||||
if !exists {
|
||||
return domain.Message{}, ErrNotFound
|
||||
}
|
||||
|
||||
isMember := false
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == senderID {
|
||||
isMember = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isMember {
|
||||
return domain.Message{}, ErrNotFound
|
||||
}
|
||||
|
||||
createdAt := time.Now().UTC()
|
||||
|
||||
messages := s.messagesByChatID[chatID]
|
||||
if len(messages) > 0 {
|
||||
lastCreatedAt := messages[len(messages)-1].CreatedAt
|
||||
|
||||
// Обеспечиваем строго возрастающее время внутри каждого чата.
|
||||
if !createdAt.After(lastCreatedAt) {
|
||||
createdAt = lastCreatedAt.Add(time.Nanosecond)
|
||||
}
|
||||
}
|
||||
|
||||
message := domain.Message{
|
||||
ID: messageID,
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
AttachmentIDs: attachmentIDs,
|
||||
CreatedAt: createdAt,
|
||||
}
|
||||
|
||||
s.messagesByChatID[chatID] = append(messages, message)
|
||||
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (s *MemoryStore) GetMessages(
|
||||
chatID, callerID string,
|
||||
limit int,
|
||||
cursorData *domain.MessageCursor,
|
||||
) ([]domain.Message, bool, error) {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
chat, exists := s.chatsByID[chatID]
|
||||
if !exists {
|
||||
return nil, false, ErrNotFound
|
||||
}
|
||||
|
||||
isMember := false
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == callerID {
|
||||
isMember = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isMember {
|
||||
return nil, false, ErrNotFound
|
||||
}
|
||||
|
||||
messages := s.messagesByChatID[chatID]
|
||||
|
||||
start := 0
|
||||
|
||||
if cursorData != nil {
|
||||
// Срез уже упорядочен во время записи, поэтому вместо
|
||||
// полного прохода используем бинарный поиск.
|
||||
start = sort.Search(len(messages), func(index int) bool {
|
||||
message := messages[index]
|
||||
|
||||
if message.CreatedAt.After(cursorData.CreatedAt) {
|
||||
return true
|
||||
}
|
||||
|
||||
return message.CreatedAt.Equal(cursorData.CreatedAt) &&
|
||||
message.ID > cursorData.MessageID
|
||||
})
|
||||
}
|
||||
|
||||
if start >= len(messages) {
|
||||
return make([]domain.Message, 0), false, nil
|
||||
}
|
||||
|
||||
end := len(messages)
|
||||
if limit > 0 && end-start > limit {
|
||||
end = start + limit
|
||||
}
|
||||
|
||||
// копируем только возвращаемую страницу, а не всю историю, изменяя внутренний срез
|
||||
items := make([]domain.Message, end-start)
|
||||
for index, message := range messages[start:end] {
|
||||
message.AttachmentIDs = append(
|
||||
make([]string, 0, len(message.AttachmentIDs)),
|
||||
message.AttachmentIDs...,
|
||||
)
|
||||
items[index] = message
|
||||
}
|
||||
return items, end < len(messages), nil
|
||||
}
|
||||
@@ -0,0 +1,201 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/application"
|
||||
|
||||
minio "github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type MinIOStore struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
operationTimeout time.Duration
|
||||
}
|
||||
|
||||
type StoredObject struct {
|
||||
Key string
|
||||
SizeBytes int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
func NewMinIOStore(
|
||||
endpoint string,
|
||||
region string,
|
||||
bucket string,
|
||||
accessKeyID string,
|
||||
secretAccessKey string,
|
||||
operationTimeout time.Duration,
|
||||
) (*MinIOStore, error) {
|
||||
parsedEndpoint, err := url.Parse(endpoint)
|
||||
if err != nil || parsedEndpoint.Host == "" {
|
||||
return nil, fmt.Errorf("S3 endpoint must be an absolute HTTP(S) URL")
|
||||
}
|
||||
if parsedEndpoint.Scheme != "http" && parsedEndpoint.Scheme != "https" {
|
||||
return nil, fmt.Errorf("S3 endpoint scheme must be http or https")
|
||||
}
|
||||
if parsedEndpoint.Path != "" && parsedEndpoint.Path != "/" {
|
||||
return nil, fmt.Errorf("S3 endpoint must not contain a path")
|
||||
}
|
||||
if parsedEndpoint.RawQuery != "" || parsedEndpoint.Fragment != "" || parsedEndpoint.User != nil {
|
||||
return nil, fmt.Errorf("S3 endpoint must not contain credentials, query or fragment")
|
||||
}
|
||||
if strings.TrimSpace(bucket) == "" || strings.TrimSpace(accessKeyID) == "" ||
|
||||
strings.TrimSpace(secretAccessKey) == "" {
|
||||
return nil, fmt.Errorf("S3 bucket and credentials are required")
|
||||
}
|
||||
if operationTimeout <= 0 {
|
||||
return nil, fmt.Errorf("S3 operation timeout must be positive")
|
||||
}
|
||||
|
||||
client, err := minio.New(parsedEndpoint.Host, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
|
||||
Secure: parsedEndpoint.Scheme == "https",
|
||||
Region: region,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create MinIO client: %w", err)
|
||||
}
|
||||
return &MinIOStore{client: client, bucket: bucket, operationTimeout: operationTimeout}, nil
|
||||
}
|
||||
|
||||
func (store *MinIOStore) Put(ctx context.Context, object application.ObjectPut) error {
|
||||
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
||||
defer cancel()
|
||||
upload, err := store.client.PutObject(
|
||||
operationContext,
|
||||
store.bucket,
|
||||
object.Key,
|
||||
object.Body,
|
||||
object.SizeBytes,
|
||||
minio.PutObjectOptions{ContentType: object.ContentType},
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("put MinIO object: %w", err)
|
||||
}
|
||||
if upload.Size != object.SizeBytes {
|
||||
return fmt.Errorf("put MinIO object: stored %d bytes, want %d", upload.Size, object.SizeBytes)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MinIOStore) Get(
|
||||
ctx context.Context,
|
||||
key string,
|
||||
) (application.ObjectContent, error) {
|
||||
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
||||
stat, err := store.client.StatObject(
|
||||
operationContext,
|
||||
store.bucket,
|
||||
key,
|
||||
minio.StatObjectOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return application.ObjectContent{}, fmt.Errorf("stat MinIO object: %w", err)
|
||||
}
|
||||
object, err := store.client.GetObject(
|
||||
operationContext,
|
||||
store.bucket,
|
||||
key,
|
||||
minio.GetObjectOptions{},
|
||||
)
|
||||
if err != nil {
|
||||
cancel()
|
||||
return application.ObjectContent{}, fmt.Errorf("get MinIO object: %w", err)
|
||||
}
|
||||
firstByte := make([]byte, 1)
|
||||
readCount, readError := object.Read(firstByte)
|
||||
if readError != nil && readCount == 0 {
|
||||
_ = object.Close()
|
||||
cancel()
|
||||
return application.ObjectContent{}, fmt.Errorf("read MinIO object: %w", readError)
|
||||
}
|
||||
if readCount != 1 {
|
||||
_ = object.Close()
|
||||
cancel()
|
||||
return application.ObjectContent{}, fmt.Errorf("read MinIO object: expected non-empty object")
|
||||
}
|
||||
return application.ObjectContent{
|
||||
Body: &cancelingReadCloser{
|
||||
Reader: io.MultiReader(bytes.NewReader(firstByte), object),
|
||||
Closer: object,
|
||||
cancel: cancel,
|
||||
},
|
||||
SizeBytes: stat.Size,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (store *MinIOStore) List(
|
||||
ctx context.Context,
|
||||
prefix string,
|
||||
) ([]StoredObject, error) {
|
||||
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
||||
defer cancel()
|
||||
|
||||
objects := make([]StoredObject, 0)
|
||||
for object := range store.client.ListObjects(
|
||||
operationContext,
|
||||
store.bucket,
|
||||
minio.ListObjectsOptions{Prefix: prefix, Recursive: true},
|
||||
) {
|
||||
if object.Err != nil {
|
||||
return nil, fmt.Errorf("list MinIO objects: %w", object.Err)
|
||||
}
|
||||
objects = append(objects, StoredObject{
|
||||
Key: object.Key,
|
||||
SizeBytes: object.Size,
|
||||
LastModified: object.LastModified.UTC(),
|
||||
})
|
||||
}
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
func (store *MinIOStore) Delete(ctx context.Context, key string) error {
|
||||
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
||||
defer cancel()
|
||||
if err := store.client.RemoveObject(
|
||||
operationContext,
|
||||
store.bucket,
|
||||
key,
|
||||
minio.RemoveObjectOptions{},
|
||||
); err != nil {
|
||||
return fmt.Errorf("delete MinIO object: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *MinIOStore) Ping(ctx context.Context) error {
|
||||
operationContext, cancel := context.WithTimeout(ctx, store.operationTimeout)
|
||||
defer cancel()
|
||||
exists, err := store.client.BucketExists(operationContext, store.bucket)
|
||||
if err != nil {
|
||||
return fmt.Errorf("check MinIO bucket: %w", err)
|
||||
}
|
||||
if !exists {
|
||||
return fmt.Errorf("MinIO bucket %q does not exist", store.bucket)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type cancelingReadCloser struct {
|
||||
io.Reader
|
||||
io.Closer
|
||||
cancel context.CancelFunc
|
||||
}
|
||||
|
||||
func (reader *cancelingReadCloser) Close() error {
|
||||
err := reader.Closer.Close()
|
||||
reader.cancel()
|
||||
return err
|
||||
}
|
||||
|
||||
var _ application.ObjectStore = (*MinIOStore)(nil)
|
||||
@@ -0,0 +1,37 @@
|
||||
package objectstore
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestNewMinIOStoreValidatesConfiguration(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
endpoint string
|
||||
bucket string
|
||||
accessKey string
|
||||
secretKey string
|
||||
timeout time.Duration
|
||||
wantError bool
|
||||
}{
|
||||
{name: "valid HTTP", endpoint: "http://minio:9000", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second},
|
||||
{name: "valid HTTPS", endpoint: "https://s3.example.test", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second},
|
||||
{name: "relative endpoint", endpoint: "minio:9000", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second, wantError: true},
|
||||
{name: "endpoint path", endpoint: "http://minio:9000/path", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second, wantError: true},
|
||||
{name: "embedded credentials", endpoint: "http://user:pass@minio:9000", bucket: "attachments", accessKey: "api", secretKey: "secret", timeout: time.Second, wantError: true},
|
||||
{name: "empty bucket", endpoint: "http://minio:9000", accessKey: "api", secretKey: "secret", timeout: time.Second, wantError: true},
|
||||
{name: "empty credentials", endpoint: "http://minio:9000", bucket: "attachments", timeout: time.Second, wantError: true},
|
||||
{name: "invalid timeout", endpoint: "http://minio:9000", bucket: "attachments", accessKey: "api", secretKey: "secret", wantError: true},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
_, err := NewMinIOStore(testCase.endpoint, "us-east-1", testCase.bucket,
|
||||
testCase.accessKey, testCase.secretKey, testCase.timeout)
|
||||
if (err != nil) != testCase.wantError {
|
||||
t.Fatalf("NewMinIOStore() error = %v, wantError %v", err, testCase.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,306 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
"mtusi2026lab4/internal/reconcile"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *PostgreSQLStore) CreateAttachment(
|
||||
ctx context.Context,
|
||||
attachment domain.Attachment,
|
||||
) (domain.Attachment, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
INSERT INTO attachments (
|
||||
id, chat_id, owner_id, object_key, filename, content_type,
|
||||
size_bytes, sha256, width, height, status, error_code,
|
||||
created_at, updated_at
|
||||
)
|
||||
VALUES (
|
||||
$1, $2, $3, $4, $5, $6,
|
||||
$7, decode($8, 'hex'), $9, $10, $11, $12,
|
||||
$13, $14
|
||||
)
|
||||
RETURNING created_at, updated_at
|
||||
`,
|
||||
attachment.ID,
|
||||
attachment.ChatID,
|
||||
attachment.OwnerID,
|
||||
attachment.ObjectKey,
|
||||
attachment.Filename,
|
||||
attachment.ContentType,
|
||||
attachment.SizeBytes,
|
||||
attachment.SHA256,
|
||||
attachment.Width,
|
||||
attachment.Height,
|
||||
attachment.Status,
|
||||
attachment.ErrorCode,
|
||||
attachment.CreatedAt,
|
||||
attachment.UpdatedAt,
|
||||
).Scan(&attachment.CreatedAt, &attachment.UpdatedAt)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("insert attachment metadata: %w", err)
|
||||
}
|
||||
if attachment.Variants == nil {
|
||||
attachment.Variants = make([]domain.AttachmentVariant, 0)
|
||||
}
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) MarkAttachmentReady(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
updatedAt time.Time,
|
||||
) (domain.Attachment, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var attachment domain.Attachment
|
||||
var sha256Bytes []byte
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
UPDATE attachments
|
||||
SET status = 'ready', error_code = NULL, updated_at = $2
|
||||
WHERE id = $1 AND status = 'pending'
|
||||
RETURNING
|
||||
id::text, chat_id::text, owner_id::text, object_key,
|
||||
filename, content_type, size_bytes, sha256, width, height,
|
||||
status, error_code, created_at, updated_at
|
||||
`,
|
||||
attachmentID,
|
||||
updatedAt,
|
||||
).Scan(
|
||||
&attachment.ID,
|
||||
&attachment.ChatID,
|
||||
&attachment.OwnerID,
|
||||
&attachment.ObjectKey,
|
||||
&attachment.Filename,
|
||||
&attachment.ContentType,
|
||||
&attachment.SizeBytes,
|
||||
&sha256Bytes,
|
||||
&attachment.Width,
|
||||
&attachment.Height,
|
||||
&attachment.Status,
|
||||
&attachment.ErrorCode,
|
||||
&attachment.CreatedAt,
|
||||
&attachment.UpdatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.Attachment{}, ErrConflict
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("mark attachment ready: %w", err)
|
||||
}
|
||||
attachment.SHA256 = fmt.Sprintf("%x", sha256Bytes)
|
||||
attachment.Variants = make([]domain.AttachmentVariant, 0)
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) MarkAttachmentFailed(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
errorCode string,
|
||||
updatedAt time.Time,
|
||||
) error {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
commandTag, err := s.pool.Exec(
|
||||
queryContext,
|
||||
`
|
||||
UPDATE attachments
|
||||
SET status = 'failed', error_code = $2, updated_at = $3
|
||||
WHERE id = $1 AND status = 'pending'
|
||||
`,
|
||||
attachmentID,
|
||||
errorCode,
|
||||
updatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("mark attachment failed: %w", err)
|
||||
}
|
||||
if commandTag.RowsAffected() != 1 {
|
||||
return ErrConflict
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) GetAttachment(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
callerID string,
|
||||
) (domain.Attachment, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var attachment domain.Attachment
|
||||
var sha256Bytes []byte
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
a.id::text, a.chat_id::text, a.owner_id::text, a.object_key,
|
||||
a.filename, a.content_type, a.size_bytes, a.sha256,
|
||||
a.width, a.height, a.status, a.error_code,
|
||||
a.created_at, a.updated_at
|
||||
FROM attachments AS a
|
||||
JOIN chat_members AS cm
|
||||
ON cm.chat_id = a.chat_id AND cm.user_id = $2
|
||||
WHERE a.id = $1
|
||||
`,
|
||||
attachmentID,
|
||||
callerID,
|
||||
).Scan(
|
||||
&attachment.ID,
|
||||
&attachment.ChatID,
|
||||
&attachment.OwnerID,
|
||||
&attachment.ObjectKey,
|
||||
&attachment.Filename,
|
||||
&attachment.ContentType,
|
||||
&attachment.SizeBytes,
|
||||
&sha256Bytes,
|
||||
&attachment.Width,
|
||||
&attachment.Height,
|
||||
&attachment.Status,
|
||||
&attachment.ErrorCode,
|
||||
&attachment.CreatedAt,
|
||||
&attachment.UpdatedAt,
|
||||
)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.Attachment{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("get attachment metadata: %w", err)
|
||||
}
|
||||
attachment.SHA256 = fmt.Sprintf("%x", sha256Bytes)
|
||||
attachment.Variants = make([]domain.AttachmentVariant, 0)
|
||||
return attachment, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) ListAttachmentInventory(
|
||||
ctx context.Context,
|
||||
) ([]reconcile.AttachmentRecord, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
rows, err := s.pool.Query(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
a.id::text,
|
||||
a.object_key,
|
||||
a.status,
|
||||
a.updated_at,
|
||||
EXISTS (
|
||||
SELECT 1
|
||||
FROM message_attachments AS ma
|
||||
WHERE ma.attachment_id = a.id
|
||||
) AS referenced
|
||||
FROM attachments AS a
|
||||
ORDER BY a.id
|
||||
`,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("query attachment inventory: %w", err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
records := make([]reconcile.AttachmentRecord, 0)
|
||||
for rows.Next() {
|
||||
var record reconcile.AttachmentRecord
|
||||
if err = rows.Scan(
|
||||
&record.ID,
|
||||
&record.ObjectKey,
|
||||
&record.Status,
|
||||
&record.UpdatedAt,
|
||||
&record.Referenced,
|
||||
); err != nil {
|
||||
return nil, fmt.Errorf("scan attachment inventory: %w", err)
|
||||
}
|
||||
records = append(records, record)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, fmt.Errorf("iterate attachment inventory: %w", err)
|
||||
}
|
||||
return records, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) MarkAttachmentFailedIfUnreferenced(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
expectedStatus domain.AttachmentStatus,
|
||||
updatedBefore time.Time,
|
||||
errorCode string,
|
||||
updatedAt time.Time,
|
||||
) (bool, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
transaction, err := s.pool.BeginTx(queryContext, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("begin attachment reconciliation transaction: %w", err)
|
||||
}
|
||||
defer func() { _ = transaction.Rollback(queryContext) }()
|
||||
|
||||
var lockedAttachmentID string
|
||||
err = transaction.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT id::text
|
||||
FROM attachments
|
||||
WHERE id = $1
|
||||
FOR UPDATE
|
||||
`,
|
||||
attachmentID,
|
||||
).Scan(&lockedAttachmentID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return false, nil
|
||||
}
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("lock attachment during reconciliation: %w", err)
|
||||
}
|
||||
|
||||
// This is deliberately a second statement. Under READ COMMITTED it gets a
|
||||
// fresh snapshot after the row lock was acquired, so a message transaction
|
||||
// that linked this attachment while we waited is visible to NOT EXISTS.
|
||||
commandTag, err := transaction.Exec(
|
||||
queryContext,
|
||||
`
|
||||
UPDATE attachments AS a
|
||||
SET status = 'failed', error_code = $4, updated_at = $5
|
||||
WHERE a.id = $1
|
||||
AND a.status = $2
|
||||
AND a.updated_at <= $3
|
||||
AND NOT EXISTS (
|
||||
SELECT 1
|
||||
FROM message_attachments AS ma
|
||||
WHERE ma.attachment_id = a.id
|
||||
)
|
||||
`,
|
||||
attachmentID,
|
||||
expectedStatus,
|
||||
updatedBefore,
|
||||
errorCode,
|
||||
updatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("mark attachment failed during reconciliation: %w", err)
|
||||
}
|
||||
if err = transaction.Commit(queryContext); err != nil {
|
||||
return false, fmt.Errorf("commit attachment reconciliation: %w", err)
|
||||
}
|
||||
return commandTag.RowsAffected() == 1, nil
|
||||
}
|
||||
|
||||
var _ reconcile.MetadataStore = (*PostgreSQLStore)(nil)
|
||||
@@ -0,0 +1,278 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *PostgreSQLStore) CreateChat(
|
||||
ctx context.Context,
|
||||
title string,
|
||||
memberIDs []string,
|
||||
) (domain.Chat, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
transaction, err := s.pool.Begin(queryContext)
|
||||
if err != nil {
|
||||
return domain.Chat{}, fmt.Errorf(
|
||||
"begin create chat transaction: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = transaction.Rollback(queryContext)
|
||||
}()
|
||||
|
||||
chat := domain.Chat{
|
||||
ID: uuid.NewString(),
|
||||
Title: title,
|
||||
MemberIDs: append([]string(nil), memberIDs...),
|
||||
}
|
||||
|
||||
err = transaction.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
INSERT INTO chats (
|
||||
id,
|
||||
title
|
||||
)
|
||||
VALUES ($1, $2)
|
||||
RETURNING created_at
|
||||
`,
|
||||
chat.ID,
|
||||
chat.Title,
|
||||
).Scan(&chat.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.Chat{}, fmt.Errorf("insert chat: %w", err)
|
||||
}
|
||||
|
||||
for position, memberID := range memberIDs {
|
||||
commandTag, insertErr := transaction.Exec(
|
||||
queryContext,
|
||||
`
|
||||
INSERT INTO chat_members (
|
||||
chat_id,
|
||||
user_id,
|
||||
position
|
||||
)
|
||||
SELECT
|
||||
$1,
|
||||
id,
|
||||
$3
|
||||
FROM users
|
||||
WHERE id = $2
|
||||
`,
|
||||
chat.ID,
|
||||
memberID,
|
||||
position,
|
||||
)
|
||||
if insertErr != nil {
|
||||
return domain.Chat{}, fmt.Errorf(
|
||||
"insert chat member %s: %w",
|
||||
memberID,
|
||||
insertErr,
|
||||
)
|
||||
}
|
||||
|
||||
// INSERT ... SELECT не вставляет строку, если пользователя нет.
|
||||
// Возврат из функции активирует deferred Rollback: уже созданные
|
||||
// chat и предыдущие membership-строки не останутся в базе.
|
||||
if commandTag.RowsAffected() != 1 {
|
||||
return domain.Chat{}, fmt.Errorf(
|
||||
"%w: member %s",
|
||||
ErrNotFound,
|
||||
memberID,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if err = transaction.Commit(queryContext); err != nil {
|
||||
return domain.Chat{}, fmt.Errorf(
|
||||
"commit create chat transaction: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) GetChatByID(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
callerID string,
|
||||
) (domain.Chat, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var chat domain.Chat
|
||||
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
chat.id::text,
|
||||
chat.title,
|
||||
chat.created_at,
|
||||
array_agg(
|
||||
member.user_id::text
|
||||
ORDER BY member.position
|
||||
)
|
||||
FROM chats AS chat
|
||||
JOIN chat_members AS caller
|
||||
ON caller.chat_id = chat.id
|
||||
AND caller.user_id = $2
|
||||
JOIN chat_members AS member
|
||||
ON member.chat_id = chat.id
|
||||
WHERE chat.id = $1
|
||||
GROUP BY
|
||||
chat.id,
|
||||
chat.title,
|
||||
chat.created_at
|
||||
`,
|
||||
chatID,
|
||||
callerID,
|
||||
).Scan(
|
||||
&chat.ID,
|
||||
&chat.Title,
|
||||
&chat.CreatedAt,
|
||||
&chat.MemberIDs,
|
||||
)
|
||||
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.Chat{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Chat{}, fmt.Errorf("select chat by id: %w", err)
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) GetChatsForUser(
|
||||
ctx context.Context,
|
||||
userID string,
|
||||
limit int,
|
||||
cursor *domain.ChatCursor,
|
||||
) ([]domain.Chat, bool, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var (
|
||||
rows pgx.Rows
|
||||
err error
|
||||
)
|
||||
|
||||
if cursor == nil {
|
||||
rows, err = s.pool.Query(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
chat.id::text,
|
||||
chat.title,
|
||||
chat.created_at,
|
||||
array_agg(
|
||||
member.user_id::text
|
||||
ORDER BY member.position
|
||||
)
|
||||
FROM chat_members AS own_membership
|
||||
JOIN chats AS chat
|
||||
ON chat.id = own_membership.chat_id
|
||||
JOIN chat_members AS member
|
||||
ON member.chat_id = chat.id
|
||||
WHERE own_membership.user_id = $1
|
||||
GROUP BY
|
||||
chat.id,
|
||||
chat.title,
|
||||
chat.created_at
|
||||
ORDER BY
|
||||
chat.created_at,
|
||||
chat.id
|
||||
LIMIT $2
|
||||
`,
|
||||
userID,
|
||||
limit+1,
|
||||
)
|
||||
} else {
|
||||
rows, err = s.pool.Query(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
chat.id::text,
|
||||
chat.title,
|
||||
chat.created_at,
|
||||
array_agg(
|
||||
member.user_id::text
|
||||
ORDER BY member.position
|
||||
)
|
||||
FROM chat_members AS own_membership
|
||||
JOIN chats AS chat
|
||||
ON chat.id = own_membership.chat_id
|
||||
JOIN chat_members AS member
|
||||
ON member.chat_id = chat.id
|
||||
WHERE own_membership.user_id = $1
|
||||
AND (chat.created_at, chat.id) > ($2, $3)
|
||||
GROUP BY
|
||||
chat.id,
|
||||
chat.title,
|
||||
chat.created_at
|
||||
ORDER BY
|
||||
chat.created_at,
|
||||
chat.id
|
||||
LIMIT $4
|
||||
`,
|
||||
userID,
|
||||
cursor.CreatedAt,
|
||||
cursor.ChatID,
|
||||
limit+1,
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf(
|
||||
"query chats for user: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
items := make([]domain.Chat, 0, limit+1)
|
||||
|
||||
for rows.Next() {
|
||||
var chat domain.Chat
|
||||
|
||||
if err := rows.Scan(
|
||||
&chat.ID,
|
||||
&chat.Title,
|
||||
&chat.CreatedAt,
|
||||
&chat.MemberIDs,
|
||||
); err != nil {
|
||||
return nil, false, fmt.Errorf(
|
||||
"scan chat page: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
items = append(items, chat)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, false, fmt.Errorf(
|
||||
"iterate chat page: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
hasMore := len(items) > limit
|
||||
if hasMore {
|
||||
items = items[:limit]
|
||||
}
|
||||
return items, hasMore, nil
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package postgres
|
||||
|
||||
import "mtusi2026lab4/internal/application"
|
||||
|
||||
var _ application.Repository = (*PostgreSQLStore)(nil)
|
||||
@@ -0,0 +1,8 @@
|
||||
package postgres
|
||||
|
||||
import "mtusi2026lab4/internal/domain"
|
||||
|
||||
var (
|
||||
ErrNotFound = domain.ErrNotFound
|
||||
ErrConflict = domain.ErrConflict
|
||||
)
|
||||
@@ -0,0 +1,423 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/application"
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *PostgreSQLStore) CreateMessageIdempotently(
|
||||
ctx context.Context,
|
||||
write application.IdempotentMessageWrite,
|
||||
) (domain.Message, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
transaction, err := s.pool.BeginTx(queryContext, pgx.TxOptions{})
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("begin idempotent message transaction: %w", err)
|
||||
}
|
||||
defer func() { _ = transaction.Rollback(queryContext) }()
|
||||
|
||||
var isMember bool
|
||||
err = transaction.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM chat_members
|
||||
WHERE chat_id = $1
|
||||
AND user_id = $2
|
||||
)
|
||||
`,
|
||||
write.ChatID,
|
||||
write.ActorID,
|
||||
).Scan(&isMember)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("check chat membership: %w", err)
|
||||
}
|
||||
if !isMember {
|
||||
return domain.Message{}, ErrNotFound
|
||||
}
|
||||
|
||||
attachmentUUIDs := make([]uuid.UUID, len(write.AttachmentIDs))
|
||||
for index, attachmentID := range write.AttachmentIDs {
|
||||
parsedID, parseErr := uuid.Parse(attachmentID)
|
||||
if parseErr != nil {
|
||||
return domain.Message{}, fmt.Errorf("parse validated attachment id: %w", parseErr)
|
||||
}
|
||||
attachmentUUIDs[index] = parsedID
|
||||
}
|
||||
|
||||
if len(attachmentUUIDs) > 0 {
|
||||
rows, queryError := transaction.Query(
|
||||
queryContext,
|
||||
`
|
||||
SELECT id::text, status
|
||||
FROM attachments
|
||||
WHERE chat_id = $1
|
||||
AND id = ANY($2::uuid[])
|
||||
ORDER BY id
|
||||
FOR UPDATE
|
||||
`,
|
||||
write.ChatID,
|
||||
attachmentUUIDs,
|
||||
)
|
||||
if queryError != nil {
|
||||
return domain.Message{}, fmt.Errorf("lock message attachments: %w", queryError)
|
||||
}
|
||||
availableCount := 0
|
||||
readyCount := 0
|
||||
for rows.Next() {
|
||||
var attachmentID string
|
||||
var status domain.AttachmentStatus
|
||||
if queryError = rows.Scan(&attachmentID, &status); queryError != nil {
|
||||
rows.Close()
|
||||
return domain.Message{}, fmt.Errorf("scan locked message attachment: %w", queryError)
|
||||
}
|
||||
availableCount++
|
||||
if status == domain.AttachmentStatusReady {
|
||||
readyCount++
|
||||
}
|
||||
}
|
||||
queryError = rows.Err()
|
||||
rows.Close()
|
||||
if queryError != nil {
|
||||
return domain.Message{}, fmt.Errorf("iterate locked message attachments: %w", queryError)
|
||||
}
|
||||
if availableCount != len(attachmentUUIDs) {
|
||||
return domain.Message{}, ErrNotFound
|
||||
}
|
||||
if readyCount != len(attachmentUUIDs) {
|
||||
return domain.Message{}, application.ErrAttachmentNotReady
|
||||
}
|
||||
}
|
||||
|
||||
_, err = transaction.Exec(
|
||||
queryContext,
|
||||
`
|
||||
DELETE FROM message_idempotency
|
||||
WHERE user_id = $1
|
||||
AND method = $2
|
||||
AND canonical_path = $3
|
||||
AND idempotency_key = $4
|
||||
AND expires_at <= CURRENT_TIMESTAMP
|
||||
`,
|
||||
write.ActorID,
|
||||
write.Method,
|
||||
write.CanonicalPath,
|
||||
write.Key,
|
||||
)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("delete expired idempotency key: %w", err)
|
||||
}
|
||||
|
||||
message := domain.Message{
|
||||
ID: uuid.NewString(),
|
||||
ChatID: write.ChatID,
|
||||
SenderID: write.ActorID,
|
||||
Text: write.Text,
|
||||
AttachmentIDs: append([]string(nil), write.AttachmentIDs...),
|
||||
}
|
||||
|
||||
var storedHash []byte
|
||||
var storedMessageID string
|
||||
err = transaction.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
INSERT INTO message_idempotency (
|
||||
user_id,
|
||||
method,
|
||||
canonical_path,
|
||||
idempotency_key,
|
||||
request_hash,
|
||||
message_id,
|
||||
expires_at
|
||||
)
|
||||
VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
CURRENT_TIMESTAMP + $7 * INTERVAL '1 second'
|
||||
)
|
||||
ON CONFLICT DO NOTHING
|
||||
RETURNING request_hash, message_id::text
|
||||
`,
|
||||
write.ActorID,
|
||||
write.Method,
|
||||
write.CanonicalPath,
|
||||
write.Key,
|
||||
write.RequestHash[:],
|
||||
message.ID,
|
||||
int64(write.TTL/time.Second),
|
||||
).Scan(&storedHash, &storedMessageID)
|
||||
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
err = transaction.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT request_hash, message_id::text
|
||||
FROM message_idempotency
|
||||
WHERE user_id = $1
|
||||
AND method = $2
|
||||
AND canonical_path = $3
|
||||
AND idempotency_key = $4
|
||||
`,
|
||||
write.ActorID,
|
||||
write.Method,
|
||||
write.CanonicalPath,
|
||||
write.Key,
|
||||
).Scan(&storedHash, &storedMessageID)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("read idempotency result: %w", err)
|
||||
}
|
||||
|
||||
if !bytes.Equal(storedHash, write.RequestHash[:]) {
|
||||
return domain.Message{}, application.ErrIdempotencyConflict
|
||||
}
|
||||
|
||||
err = transaction.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
id::text,
|
||||
chat_id::text,
|
||||
sender_id::text,
|
||||
text,
|
||||
created_at,
|
||||
COALESCE(
|
||||
ARRAY(
|
||||
SELECT attachment_id::text
|
||||
FROM message_attachments
|
||||
WHERE message_id = messages.id
|
||||
ORDER BY position
|
||||
), ARRAY[]::text[]
|
||||
)
|
||||
FROM messages
|
||||
WHERE id = $1
|
||||
`,
|
||||
storedMessageID,
|
||||
).Scan(
|
||||
&message.ID,
|
||||
&message.ChatID,
|
||||
&message.SenderID,
|
||||
&message.Text,
|
||||
&message.CreatedAt,
|
||||
&message.AttachmentIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("read idempotent message: %w", err)
|
||||
}
|
||||
|
||||
if err = transaction.Commit(queryContext); err != nil {
|
||||
return domain.Message{}, fmt.Errorf("commit idempotency replay: %w", err)
|
||||
}
|
||||
return message, nil
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("claim idempotency key: %w", err)
|
||||
}
|
||||
|
||||
err = transaction.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text
|
||||
)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING created_at
|
||||
`,
|
||||
message.ID,
|
||||
message.ChatID,
|
||||
message.SenderID,
|
||||
message.Text,
|
||||
).Scan(&message.CreatedAt)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("insert message: %w", err)
|
||||
}
|
||||
|
||||
if len(attachmentUUIDs) > 0 {
|
||||
_, err = transaction.Exec(
|
||||
queryContext,
|
||||
`
|
||||
INSERT INTO message_attachments (
|
||||
message_id,
|
||||
chat_id,
|
||||
attachment_id,
|
||||
position
|
||||
)
|
||||
SELECT $1, $2, attachment_id, (ordinality - 1)::smallint
|
||||
FROM unnest($3::uuid[]) WITH ORDINALITY AS input(attachment_id, ordinality)
|
||||
`,
|
||||
message.ID,
|
||||
message.ChatID,
|
||||
attachmentUUIDs,
|
||||
)
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("link message attachments: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
if err = transaction.Commit(queryContext); err != nil {
|
||||
return domain.Message{}, fmt.Errorf("commit idempotent message: %w", err)
|
||||
}
|
||||
|
||||
return message, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) GetMessages(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
callerID string,
|
||||
limit int,
|
||||
cursor *domain.MessageCursor,
|
||||
) ([]domain.Message, bool, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var isMember bool
|
||||
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM chat_members
|
||||
WHERE chat_id = $1
|
||||
AND user_id = $2
|
||||
)
|
||||
`,
|
||||
chatID,
|
||||
callerID,
|
||||
).Scan(&isMember)
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf(
|
||||
"check chat membership: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
if !isMember {
|
||||
return nil, false, ErrNotFound
|
||||
}
|
||||
|
||||
var rows pgx.Rows
|
||||
|
||||
if cursor == nil {
|
||||
rows, err = s.pool.Query(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
id::text,
|
||||
chat_id::text,
|
||||
sender_id::text,
|
||||
text,
|
||||
created_at,
|
||||
COALESCE(
|
||||
ARRAY(
|
||||
SELECT attachment_id::text
|
||||
FROM message_attachments
|
||||
WHERE message_id = messages.id
|
||||
ORDER BY position
|
||||
), ARRAY[]::text[]
|
||||
)
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY
|
||||
created_at,
|
||||
id
|
||||
LIMIT $2
|
||||
`,
|
||||
chatID,
|
||||
limit+1,
|
||||
)
|
||||
} else {
|
||||
rows, err = s.pool.Query(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
id::text,
|
||||
chat_id::text,
|
||||
sender_id::text,
|
||||
text,
|
||||
created_at,
|
||||
COALESCE(
|
||||
ARRAY(
|
||||
SELECT attachment_id::text
|
||||
FROM message_attachments
|
||||
WHERE message_id = messages.id
|
||||
ORDER BY position
|
||||
), ARRAY[]::text[]
|
||||
)
|
||||
FROM messages
|
||||
WHERE chat_id = $1
|
||||
AND (created_at, id) > ($2, $3)
|
||||
ORDER BY
|
||||
created_at,
|
||||
id
|
||||
LIMIT $4
|
||||
`,
|
||||
chatID,
|
||||
cursor.CreatedAt,
|
||||
cursor.MessageID,
|
||||
limit+1,
|
||||
)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, false, fmt.Errorf(
|
||||
"query messages: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
items := make([]domain.Message, 0, limit+1)
|
||||
|
||||
for rows.Next() {
|
||||
var message domain.Message
|
||||
|
||||
if err := rows.Scan(
|
||||
&message.ID,
|
||||
&message.ChatID,
|
||||
&message.SenderID,
|
||||
&message.Text,
|
||||
&message.CreatedAt,
|
||||
&message.AttachmentIDs,
|
||||
); err != nil {
|
||||
return nil, false, fmt.Errorf(
|
||||
"scan message page: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
items = append(items, message)
|
||||
}
|
||||
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, false, fmt.Errorf(
|
||||
"iterate message page: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
hasMore := len(items) > limit
|
||||
if hasMore {
|
||||
items = items[:limit]
|
||||
}
|
||||
return items, hasMore, nil
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *PostgreSQLStore) CreateSession(
|
||||
ctx context.Context,
|
||||
tokenHash []byte,
|
||||
userID string,
|
||||
expiresAt time.Time,
|
||||
) error {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.pool.Exec(
|
||||
queryContext,
|
||||
`
|
||||
INSERT INTO sessions (
|
||||
token_hash,
|
||||
user_id,
|
||||
expires_at
|
||||
)
|
||||
VALUES ($1, $2, $3)
|
||||
`,
|
||||
tokenHash,
|
||||
userID,
|
||||
expiresAt,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("insert session: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) GetUserBySession(
|
||||
ctx context.Context,
|
||||
tokenHash []byte,
|
||||
) (domain.User, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var user domain.User
|
||||
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
app_user.id::text,
|
||||
app_user.username,
|
||||
app_user.display_name,
|
||||
app_user.created_at
|
||||
FROM sessions AS session
|
||||
JOIN users AS app_user
|
||||
ON app_user.id = session.user_id
|
||||
WHERE session.token_hash = $1
|
||||
AND session.expires_at > CURRENT_TIMESTAMP
|
||||
`,
|
||||
tokenHash,
|
||||
).Scan(
|
||||
&user.ID,
|
||||
&user.Username,
|
||||
&user.DisplayName,
|
||||
&user.CreatedAt,
|
||||
)
|
||||
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.User{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf(
|
||||
"select user by session: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) DeleteSession(
|
||||
ctx context.Context,
|
||||
tokenHash []byte,
|
||||
) error {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
_, err := s.pool.Exec(
|
||||
queryContext,
|
||||
`
|
||||
DELETE FROM sessions
|
||||
WHERE token_hash = $1
|
||||
`,
|
||||
tokenHash,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("delete session: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type PostgreSQLStore struct {
|
||||
pool *pgxpool.Pool
|
||||
queryTimeout time.Duration
|
||||
}
|
||||
|
||||
func NewPostgreSQLStore(
|
||||
pool *pgxpool.Pool,
|
||||
queryTimeout time.Duration,
|
||||
) *PostgreSQLStore {
|
||||
return &PostgreSQLStore{
|
||||
pool: pool,
|
||||
queryTimeout: queryTimeout,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) Ping(ctx context.Context) error {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
return s.pool.Ping(queryContext)
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) Close() {
|
||||
s.pool.Close()
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) withTimeout(
|
||||
ctx context.Context,
|
||||
) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(ctx, s.queryTimeout)
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
func (s *PostgreSQLStore) CreateUser(
|
||||
ctx context.Context,
|
||||
username string,
|
||||
displayName string,
|
||||
passwordHash string,
|
||||
) (domain.User, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
user := domain.User{
|
||||
ID: uuid.NewString(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
}
|
||||
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
INSERT INTO users (
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
password_hash
|
||||
)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING created_at
|
||||
`,
|
||||
user.ID,
|
||||
user.Username,
|
||||
user.DisplayName,
|
||||
passwordHash,
|
||||
).Scan(&user.CreatedAt)
|
||||
|
||||
if err == nil {
|
||||
return user, nil
|
||||
}
|
||||
|
||||
var postgresError *pgconn.PgError
|
||||
if errors.As(err, &postgresError) &&
|
||||
postgresError.Code == "23505" &&
|
||||
postgresError.ConstraintName == "users_username_unique" {
|
||||
return domain.User{}, ErrConflict
|
||||
}
|
||||
|
||||
return domain.User{}, fmt.Errorf("insert user: %w", err)
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) GetUserByID(
|
||||
ctx context.Context,
|
||||
userID string,
|
||||
) (domain.User, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var user domain.User
|
||||
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
id::text,
|
||||
username,
|
||||
display_name,
|
||||
created_at
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
`,
|
||||
userID,
|
||||
).Scan(
|
||||
&user.ID,
|
||||
&user.Username,
|
||||
&user.DisplayName,
|
||||
&user.CreatedAt,
|
||||
)
|
||||
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.User{}, ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf("select user by id: %w", err)
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (s *PostgreSQLStore) GetUserByUsername(
|
||||
ctx context.Context,
|
||||
username string,
|
||||
) (domain.User, string, error) {
|
||||
queryContext, cancel := s.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
var (
|
||||
user domain.User
|
||||
passwordHash string
|
||||
)
|
||||
|
||||
err := s.pool.QueryRow(
|
||||
queryContext,
|
||||
`
|
||||
SELECT
|
||||
id::text,
|
||||
username,
|
||||
display_name,
|
||||
created_at,
|
||||
password_hash
|
||||
FROM users
|
||||
WHERE username = $1
|
||||
`,
|
||||
username,
|
||||
).Scan(
|
||||
&user.ID,
|
||||
&user.Username,
|
||||
&user.DisplayName,
|
||||
&user.CreatedAt,
|
||||
&passwordHash,
|
||||
)
|
||||
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.User{}, "", ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return domain.User{}, "", fmt.Errorf(
|
||||
"select user by username: %w",
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
return user, passwordHash, nil
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const chatKeyPrefix = "chat:v2:"
|
||||
|
||||
var ErrMiss = errors.New("cache miss")
|
||||
|
||||
// ChatCache хранит только производную копию неизменяемых данных чата.
|
||||
// PostgreSQL остаётся источником истины.
|
||||
type ChatCache interface {
|
||||
Get(ctx context.Context, chatID string) (domain.Chat, error)
|
||||
Set(ctx context.Context, chat domain.Chat) error
|
||||
}
|
||||
|
||||
type RedisChatCache struct {
|
||||
client *goredis.Client
|
||||
ttl time.Duration
|
||||
operationTimeout time.Duration
|
||||
}
|
||||
|
||||
func NewRedisChatCache(
|
||||
redisURL string,
|
||||
ttl time.Duration,
|
||||
operationTimeout time.Duration,
|
||||
poolSize int,
|
||||
) (*RedisChatCache, error) {
|
||||
options, err := goredis.ParseURL(strings.TrimSpace(redisURL))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse Redis URL: %w", err)
|
||||
}
|
||||
|
||||
options.DialTimeout = operationTimeout
|
||||
options.ReadTimeout = operationTimeout
|
||||
options.WriteTimeout = operationTimeout
|
||||
options.PoolTimeout = operationTimeout
|
||||
options.PoolSize = poolSize
|
||||
options.MaxRetries = -1
|
||||
|
||||
return &RedisChatCache{
|
||||
client: goredis.NewClient(options),
|
||||
ttl: ttl,
|
||||
operationTimeout: operationTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *RedisChatCache) Ping(ctx context.Context) error {
|
||||
operationContext, cancel := c.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
if err := c.client.Ping(operationContext).Err(); err != nil {
|
||||
return fmt.Errorf("ping Redis: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *RedisChatCache) Close() error {
|
||||
return c.client.Close()
|
||||
}
|
||||
|
||||
func (c *RedisChatCache) Get(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
) (domain.Chat, error) {
|
||||
operationContext, cancel := c.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
payload, err := c.client.Get(
|
||||
operationContext,
|
||||
chatKey(chatID),
|
||||
).Bytes()
|
||||
if errors.Is(err, goredis.Nil) {
|
||||
return domain.Chat{}, ErrMiss
|
||||
}
|
||||
if err != nil {
|
||||
return domain.Chat{}, fmt.Errorf("get cached chat: %w", err)
|
||||
}
|
||||
|
||||
var chat domain.Chat
|
||||
if err = json.Unmarshal(payload, &chat); err != nil {
|
||||
c.deleteInvalidValue(operationContext, chatID)
|
||||
return domain.Chat{}, fmt.Errorf("decode cached chat: %w", err)
|
||||
}
|
||||
|
||||
if err = validateCachedChat(chat, chatID); err != nil {
|
||||
c.deleteInvalidValue(operationContext, chatID)
|
||||
return domain.Chat{}, err
|
||||
}
|
||||
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (c *RedisChatCache) Set(
|
||||
ctx context.Context,
|
||||
chat domain.Chat,
|
||||
) error {
|
||||
payload, err := json.Marshal(chat)
|
||||
if err != nil {
|
||||
return fmt.Errorf("encode chat for cache: %w", err)
|
||||
}
|
||||
|
||||
operationContext, cancel := c.withTimeout(ctx)
|
||||
defer cancel()
|
||||
|
||||
if err = c.client.Set(
|
||||
operationContext,
|
||||
chatKey(chat.ID),
|
||||
payload,
|
||||
c.ttl,
|
||||
).Err(); err != nil {
|
||||
return fmt.Errorf("cache chat: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *RedisChatCache) withTimeout(
|
||||
ctx context.Context,
|
||||
) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(ctx, c.operationTimeout)
|
||||
}
|
||||
|
||||
func (c *RedisChatCache) deleteInvalidValue(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
) {
|
||||
_ = c.client.Del(ctx, chatKey(chatID)).Err()
|
||||
}
|
||||
|
||||
func chatKey(chatID string) string {
|
||||
return chatKeyPrefix + chatID
|
||||
}
|
||||
|
||||
func validateCachedChat(chat domain.Chat, expectedID string) error {
|
||||
if chat.ID != expectedID {
|
||||
return errors.New("cached chat id does not match cache key")
|
||||
}
|
||||
if _, err := uuid.Parse(chat.ID); err != nil {
|
||||
return errors.New("cached chat contains invalid id")
|
||||
}
|
||||
if strings.TrimSpace(chat.Title) == "" || chat.CreatedAt.IsZero() {
|
||||
return errors.New("cached chat is incomplete")
|
||||
}
|
||||
if len(chat.MemberIDs) == 0 {
|
||||
return errors.New("cached chat has no members")
|
||||
}
|
||||
|
||||
seenMembers := make(map[string]struct{}, len(chat.MemberIDs))
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if _, err := uuid.Parse(memberID); err != nil {
|
||||
return errors.New("cached chat contains invalid member id")
|
||||
}
|
||||
if _, exists := seenMembers[memberID]; exists {
|
||||
return errors.New("cached chat contains duplicate member id")
|
||||
}
|
||||
seenMembers[memberID] = struct{}{}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ ChatCache = (*RedisChatCache)(nil)
|
||||
@@ -0,0 +1,65 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type AttachmentDownload struct {
|
||||
Body io.ReadCloser
|
||||
ContentType string
|
||||
Filename string
|
||||
SizeBytes int64
|
||||
}
|
||||
|
||||
func (s *Service) GetAttachment(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
actorID string,
|
||||
) (domain.Attachment, error) {
|
||||
return s.repository.GetAttachment(ctx, attachmentID, actorID)
|
||||
}
|
||||
|
||||
func (s *Service) DownloadAttachment(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
actorID string,
|
||||
variant string,
|
||||
) (AttachmentDownload, error) {
|
||||
attachment, err := s.repository.GetAttachment(ctx, attachmentID, actorID)
|
||||
if err != nil {
|
||||
return AttachmentDownload{}, err
|
||||
}
|
||||
if attachment.Status != domain.AttachmentStatusReady {
|
||||
return AttachmentDownload{}, ErrAttachmentNotReady
|
||||
}
|
||||
if variant == "" {
|
||||
variant = "original"
|
||||
}
|
||||
if variant != "original" {
|
||||
return AttachmentDownload{}, domain.ErrNotFound
|
||||
}
|
||||
if s.objectStore == nil {
|
||||
return AttachmentDownload{}, ErrStorageUnavailable
|
||||
}
|
||||
|
||||
object, err := s.objectStore.Get(ctx, attachment.ObjectKey)
|
||||
if err != nil {
|
||||
return AttachmentDownload{}, fmt.Errorf("%w: get object", ErrStorageUnavailable)
|
||||
}
|
||||
if object.Body == nil || object.SizeBytes != attachment.SizeBytes {
|
||||
if object.Body != nil {
|
||||
_ = object.Body.Close()
|
||||
}
|
||||
return AttachmentDownload{}, fmt.Errorf("%w: object metadata mismatch", ErrStorageUnavailable)
|
||||
}
|
||||
return AttachmentDownload{
|
||||
Body: object.Body,
|
||||
ContentType: attachment.ContentType,
|
||||
Filename: attachment.Filename,
|
||||
SizeBytes: attachment.SizeBytes,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,246 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const attachmentPutFailedCode = "object_store_put_failed"
|
||||
const attachmentFinalizeFailedCode = "metadata_finalize_failed"
|
||||
|
||||
func (s *Service) UploadAttachment(
|
||||
ctx context.Context,
|
||||
command UploadAttachmentCommand,
|
||||
) (domain.Attachment, error) {
|
||||
if s.objectStore == nil {
|
||||
return domain.Attachment{}, ErrStorageUnavailable
|
||||
}
|
||||
if command.Body == nil {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: attachment body is required", ErrInvalidInput)
|
||||
}
|
||||
if err := domain.ValidateAttachmentFilename(command.Filename); err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
|
||||
}
|
||||
if err := domain.ValidateAttachmentContentType(command.DeclaredContentType); err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: %v", ErrUnsupportedMediaType, err)
|
||||
}
|
||||
if _, err := s.repository.GetChatByID(ctx, command.ChatID, command.ActorID); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if err := acquireAttachmentSlot(ctx, s.attachmentUploadSlots); err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: wait for upload capacity", ErrStorageUnavailable)
|
||||
}
|
||||
defer releaseAttachmentSlot(s.attachmentUploadSlots)
|
||||
|
||||
stagedFile, err := os.CreateTemp("", "mtusi-attachment-*")
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("create bounded upload staging file: %w", err)
|
||||
}
|
||||
stagedPath := stagedFile.Name()
|
||||
defer func() {
|
||||
_ = stagedFile.Close()
|
||||
_ = os.Remove(stagedPath)
|
||||
}()
|
||||
|
||||
hasher := sha256.New()
|
||||
limitedBody := io.LimitReader(command.Body, domain.MaxAttachmentSizeBytes+1)
|
||||
sizeBytes, err := io.Copy(io.MultiWriter(stagedFile, hasher), limitedBody)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: read attachment body", ErrInvalidInput)
|
||||
}
|
||||
if sizeBytes > domain.MaxAttachmentSizeBytes {
|
||||
return domain.Attachment{}, ErrPayloadTooLarge
|
||||
}
|
||||
|
||||
if _, err = stagedFile.Seek(0, io.SeekStart); err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("rewind staged attachment: %w", err)
|
||||
}
|
||||
imageConfig, format, err := image.DecodeConfig(stagedFile)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: decode image header", ErrInvalidImage)
|
||||
}
|
||||
actualContentType, supported := contentTypeForImageFormat(format)
|
||||
if !supported || actualContentType != command.DeclaredContentType {
|
||||
return domain.Attachment{}, ErrUnsupportedMediaType
|
||||
}
|
||||
if imageConfig.Width < 1 || imageConfig.Height < 1 {
|
||||
return domain.Attachment{}, ErrInvalidImage
|
||||
}
|
||||
if int64(imageConfig.Width) > int64(domain.MaxAttachmentPixels)/int64(imageConfig.Height) {
|
||||
return domain.Attachment{}, ErrPayloadTooLarge
|
||||
}
|
||||
|
||||
sha256Hex := hex.EncodeToString(hasher.Sum(nil))
|
||||
if err = domain.ValidateReadyAttachmentMetadata(
|
||||
command.Filename,
|
||||
actualContentType,
|
||||
sizeBytes,
|
||||
sha256Hex,
|
||||
imageConfig.Width,
|
||||
imageConfig.Height,
|
||||
); err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: %v", ErrInvalidImage, err)
|
||||
}
|
||||
|
||||
if _, err = stagedFile.Seek(0, io.SeekStart); err != nil {
|
||||
return domain.Attachment{}, fmt.Errorf("rewind staged attachment for validation: %w", err)
|
||||
}
|
||||
if err = s.validateCompleteImage(ctx, stagedFile); err != nil {
|
||||
if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) {
|
||||
return domain.Attachment{}, fmt.Errorf("%w: wait for image decoder", ErrStorageUnavailable)
|
||||
}
|
||||
return domain.Attachment{}, fmt.Errorf("%w: decode complete image", ErrInvalidImage)
|
||||
}
|
||||
|
||||
now := s.now().UTC()
|
||||
attachmentID := uuid.NewString()
|
||||
attachment := domain.Attachment{
|
||||
ID: attachmentID,
|
||||
ChatID: command.ChatID,
|
||||
OwnerID: command.ActorID,
|
||||
Filename: command.Filename,
|
||||
ObjectKey: "attachments/" + command.ChatID + "/" + attachmentID + "/original",
|
||||
ContentType: actualContentType,
|
||||
SizeBytes: sizeBytes,
|
||||
SHA256: sha256Hex,
|
||||
Width: imageConfig.Width,
|
||||
Height: imageConfig.Height,
|
||||
Status: domain.AttachmentStatusPending,
|
||||
Variants: make([]domain.AttachmentVariant, 0),
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
|
||||
attachment, err = s.repository.CreateAttachment(ctx, attachment)
|
||||
if err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
if _, err = stagedFile.Seek(0, io.SeekStart); err != nil {
|
||||
s.cleanupFailedAttachment(ctx, attachment, attachmentFinalizeFailedCode, false)
|
||||
return domain.Attachment{}, fmt.Errorf("rewind staged attachment for storage: %w", err)
|
||||
}
|
||||
err = s.objectStore.Put(ctx, ObjectPut{
|
||||
Key: attachment.ObjectKey,
|
||||
ContentType: attachment.ContentType,
|
||||
SizeBytes: attachment.SizeBytes,
|
||||
Body: stagedFile,
|
||||
})
|
||||
if err != nil {
|
||||
s.cleanupFailedAttachment(ctx, attachment, attachmentPutFailedCode, true)
|
||||
return domain.Attachment{}, fmt.Errorf("%w: put object", ErrStorageUnavailable)
|
||||
}
|
||||
|
||||
readyAttachment, err := s.repository.MarkAttachmentReady(ctx, attachment.ID, s.now().UTC())
|
||||
if err != nil {
|
||||
// The UPDATE may already have committed even when its response was lost.
|
||||
// Deleting the object here could turn durable ready metadata into a
|
||||
// permanently broken attachment. A best-effort read may recover a 201;
|
||||
// otherwise both sides are preserved for the grace-period reconciler.
|
||||
if resolved, ok := s.resolveAmbiguousAttachmentFinalize(ctx, attachment); ok {
|
||||
return resolved, nil
|
||||
}
|
||||
return domain.Attachment{}, fmt.Errorf("finalize attachment metadata: %w", err)
|
||||
}
|
||||
if readyAttachment.Variants == nil {
|
||||
readyAttachment.Variants = make([]domain.AttachmentVariant, 0)
|
||||
}
|
||||
return readyAttachment, nil
|
||||
}
|
||||
|
||||
func acquireAttachmentSlot(ctx context.Context, slots chan struct{}) error {
|
||||
select {
|
||||
case slots <- struct{}{}:
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
func releaseAttachmentSlot(slots chan struct{}) {
|
||||
<-slots
|
||||
}
|
||||
|
||||
func (s *Service) validateCompleteImage(ctx context.Context, body io.Reader) error {
|
||||
if err := acquireAttachmentSlot(ctx, s.attachmentDecodeSlots); err != nil {
|
||||
return err
|
||||
}
|
||||
defer releaseAttachmentSlot(s.attachmentDecodeSlots)
|
||||
_, _, err := image.Decode(body)
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Service) resolveAmbiguousAttachmentFinalize(
|
||||
requestContext context.Context,
|
||||
attachment domain.Attachment,
|
||||
) (domain.Attachment, bool) {
|
||||
resolveContext, cancel := context.WithTimeout(
|
||||
context.WithoutCancel(requestContext),
|
||||
s.attachmentCleanupTimeout,
|
||||
)
|
||||
defer cancel()
|
||||
|
||||
stored, err := s.repository.GetAttachment(
|
||||
resolveContext,
|
||||
attachment.ID,
|
||||
attachment.OwnerID,
|
||||
)
|
||||
if err != nil || stored.Status != domain.AttachmentStatusReady {
|
||||
return domain.Attachment{}, false
|
||||
}
|
||||
if stored.Variants == nil {
|
||||
stored.Variants = make([]domain.AttachmentVariant, 0)
|
||||
}
|
||||
return stored, true
|
||||
}
|
||||
|
||||
func contentTypeForImageFormat(format string) (string, bool) {
|
||||
switch format {
|
||||
case "png":
|
||||
return "image/png", true
|
||||
case "jpeg":
|
||||
return "image/jpeg", true
|
||||
default:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) cleanupFailedAttachment(
|
||||
requestContext context.Context,
|
||||
attachment domain.Attachment,
|
||||
errorCode string,
|
||||
deleteObject bool,
|
||||
) {
|
||||
metadataContext, metadataCancel := context.WithTimeout(
|
||||
context.WithoutCancel(requestContext),
|
||||
s.attachmentCleanupTimeout,
|
||||
)
|
||||
_ = s.repository.MarkAttachmentFailed(
|
||||
metadataContext,
|
||||
attachment.ID,
|
||||
errorCode,
|
||||
s.now().UTC(),
|
||||
)
|
||||
metadataCancel()
|
||||
|
||||
if !deleteObject {
|
||||
return
|
||||
}
|
||||
deleteContext, deleteCancel := context.WithTimeout(
|
||||
context.WithoutCancel(requestContext),
|
||||
s.attachmentCleanupTimeout,
|
||||
)
|
||||
defer deleteCancel()
|
||||
_ = s.objectStore.Delete(deleteContext, attachment.ObjectKey)
|
||||
}
|
||||
@@ -0,0 +1,524 @@
|
||||
package application_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"io"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/adapter/memory"
|
||||
. "mtusi2026lab4/internal/application"
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type fakeObjectStore struct {
|
||||
mu sync.Mutex
|
||||
objects map[string][]byte
|
||||
putError error
|
||||
blockDelete bool
|
||||
putCalls int
|
||||
deleteCalls int
|
||||
lastPutKey string
|
||||
}
|
||||
|
||||
type alwaysFailReader struct{}
|
||||
|
||||
func (alwaysFailReader) Read([]byte) (int, error) {
|
||||
return 0, errors.New("injected interrupted upload")
|
||||
}
|
||||
|
||||
type finalizeFailureRepository struct {
|
||||
Repository
|
||||
}
|
||||
|
||||
func (repository *finalizeFailureRepository) MarkAttachmentReady(
|
||||
context.Context,
|
||||
string,
|
||||
time.Time,
|
||||
) (domain.Attachment, error) {
|
||||
return domain.Attachment{}, errors.New("injected metadata finalize failure")
|
||||
}
|
||||
|
||||
type commitThenDisconnectRepository struct {
|
||||
Repository
|
||||
hideReadback bool
|
||||
}
|
||||
|
||||
func (repository *commitThenDisconnectRepository) MarkAttachmentReady(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
updatedAt time.Time,
|
||||
) (domain.Attachment, error) {
|
||||
if _, err := repository.Repository.MarkAttachmentReady(ctx, attachmentID, updatedAt); err != nil {
|
||||
return domain.Attachment{}, err
|
||||
}
|
||||
return domain.Attachment{}, errors.New("injected connection loss after commit")
|
||||
}
|
||||
|
||||
func (repository *commitThenDisconnectRepository) GetAttachment(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
actorID string,
|
||||
) (domain.Attachment, error) {
|
||||
if repository.hideReadback {
|
||||
return domain.Attachment{}, errors.New("injected database outage during readback")
|
||||
}
|
||||
return repository.Repository.GetAttachment(ctx, attachmentID, actorID)
|
||||
}
|
||||
|
||||
func newFakeObjectStore() *fakeObjectStore {
|
||||
return &fakeObjectStore{objects: make(map[string][]byte)}
|
||||
}
|
||||
|
||||
func (store *fakeObjectStore) Put(_ context.Context, object ObjectPut) error {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
store.putCalls++
|
||||
store.lastPutKey = object.Key
|
||||
if store.putError != nil {
|
||||
return store.putError
|
||||
}
|
||||
payload, err := io.ReadAll(object.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if int64(len(payload)) != object.SizeBytes {
|
||||
return errors.New("declared object size differs")
|
||||
}
|
||||
store.objects[object.Key] = payload
|
||||
return nil
|
||||
}
|
||||
|
||||
func (store *fakeObjectStore) Get(_ context.Context, key string) (ObjectContent, error) {
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
payload, exists := store.objects[key]
|
||||
if !exists {
|
||||
return ObjectContent{}, errors.New("object not found")
|
||||
}
|
||||
copyOfPayload := append([]byte(nil), payload...)
|
||||
return ObjectContent{
|
||||
Body: io.NopCloser(bytes.NewReader(copyOfPayload)),
|
||||
SizeBytes: int64(len(copyOfPayload)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (store *fakeObjectStore) Delete(ctx context.Context, key string) error {
|
||||
store.mu.Lock()
|
||||
store.deleteCalls++
|
||||
blockDelete := store.blockDelete
|
||||
store.mu.Unlock()
|
||||
if blockDelete {
|
||||
<-ctx.Done()
|
||||
return ctx.Err()
|
||||
}
|
||||
store.mu.Lock()
|
||||
defer store.mu.Unlock()
|
||||
delete(store.objects, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func newAttachmentService(
|
||||
t *testing.T,
|
||||
objectStore ObjectStore,
|
||||
) (*Service, *memory.MemoryStore, string, string) {
|
||||
t.Helper()
|
||||
legacyStore := memory.NewMemoryStore()
|
||||
repository := memory.NewMemoryRepository(legacyStore)
|
||||
user, err := repository.CreateUser(context.Background(), "uploader", "Uploader", "hash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chat, err := repository.CreateChat(context.Background(), "Images", []string{user.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
codec, err := NewCursorCodec("attachment-test-key-with-more-than-thirty-two-bytes")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service, err := NewService(repository, codec, time.Hour, WithObjectStore(objectStore))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return service, legacyStore, user.ID, chat.ID
|
||||
}
|
||||
|
||||
func encodeTestImage(t *testing.T, contentType string) []byte {
|
||||
t.Helper()
|
||||
var payload bytes.Buffer
|
||||
img := image.NewRGBA(image.Rect(0, 0, 2, 2))
|
||||
img.Set(0, 0, color.RGBA{R: 255, A: 255})
|
||||
var err error
|
||||
if contentType == "image/png" {
|
||||
err = png.Encode(&payload, img)
|
||||
} else {
|
||||
err = jpeg.Encode(&payload, img, nil)
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return payload.Bytes()
|
||||
}
|
||||
|
||||
func corruptPNGAfterPrefix(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
img := image.NewNRGBA(image.Rect(0, 0, 512, 512))
|
||||
state := uint32(0x12345678)
|
||||
for index := range img.Pix {
|
||||
state ^= state << 13
|
||||
state ^= state >> 17
|
||||
state ^= state << 5
|
||||
img.Pix[index] = byte(state)
|
||||
}
|
||||
var payload bytes.Buffer
|
||||
if err := png.Encode(&payload, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
corrupted := payload.Bytes()
|
||||
if len(corrupted) <= 70_000 {
|
||||
t.Fatalf("test PNG is unexpectedly small: %d", len(corrupted))
|
||||
}
|
||||
corrupted[70_000] ^= 0xff
|
||||
return corrupted
|
||||
}
|
||||
|
||||
func jpegWithoutScanData(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
payload := encodeTestImage(t, "image/jpeg")
|
||||
sos := bytes.Index(payload, []byte{0xff, 0xda})
|
||||
if sos < 0 || sos+4 > len(payload) {
|
||||
t.Fatal("encoded JPEG has no SOS marker")
|
||||
}
|
||||
segmentLength := int(binary.BigEndian.Uint16(payload[sos+2 : sos+4]))
|
||||
endOfHeader := sos + 2 + segmentLength
|
||||
if endOfHeader > len(payload) {
|
||||
t.Fatal("invalid SOS segment length in test JPEG")
|
||||
}
|
||||
return append([]byte(nil), payload[:endOfHeader]...)
|
||||
}
|
||||
|
||||
func TestUploadAttachmentStoresValidatedImage(t *testing.T) {
|
||||
objectStore := newFakeObjectStore()
|
||||
service, metadataStore, actorID, chatID := newAttachmentService(t, objectStore)
|
||||
payload := encodeTestImage(t, "image/png")
|
||||
|
||||
attachment, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: actorID,
|
||||
ChatID: chatID,
|
||||
Filename: "кот.png",
|
||||
DeclaredContentType: "image/png",
|
||||
Body: bytes.NewReader(payload),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if attachment.Status != domain.AttachmentStatusReady {
|
||||
t.Fatalf("status = %q, want ready", attachment.Status)
|
||||
}
|
||||
if attachment.ObjectKey == "" || strings.Contains(attachment.ObjectKey, attachment.Filename) {
|
||||
t.Fatalf("object key must be server-generated: %q", attachment.ObjectKey)
|
||||
}
|
||||
if attachment.Width != 2 || attachment.Height != 2 || len(attachment.SHA256) != 64 {
|
||||
t.Fatalf("unexpected image metadata: %+v", attachment)
|
||||
}
|
||||
if !bytes.Equal(objectStore.objects[attachment.ObjectKey], payload) {
|
||||
t.Fatal("stored object differs from uploaded bytes")
|
||||
}
|
||||
stored, err := metadataStore.GetAttachmentByID(attachment.ID)
|
||||
if err != nil || stored.Status != domain.AttachmentStatusReady {
|
||||
t.Fatalf("ready metadata was not persisted: %+v, %v", stored, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAttachmentRejectsInvalidInputBeforeStorage(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
filename string
|
||||
declaredType string
|
||||
payload []byte
|
||||
wantError error
|
||||
}{
|
||||
{name: "unsafe filename", filename: "../image.png", declaredType: "image/png", payload: encodeTestImage(t, "image/png"), wantError: ErrInvalidInput},
|
||||
{name: "unsupported declared type", filename: "image.gif", declaredType: "image/gif", payload: []byte("GIF89a"), wantError: ErrUnsupportedMediaType},
|
||||
{name: "MIME substitution", filename: "image.jpg", declaredType: "image/jpeg", payload: encodeTestImage(t, "image/png"), wantError: ErrUnsupportedMediaType},
|
||||
{name: "corrupt image", filename: "image.png", declaredType: "image/png", payload: []byte("not a png"), wantError: ErrInvalidImage},
|
||||
{name: "PNG header without image data", filename: "truncated.png", declaredType: "image/png", payload: pngConfigurationOnly(1, 1), wantError: ErrInvalidImage},
|
||||
{name: "PNG corruption after 64 KiB", filename: "corrupted.png", declaredType: "image/png", payload: corruptPNGAfterPrefix(t), wantError: ErrInvalidImage},
|
||||
{name: "JPEG without scan data", filename: "truncated.jpg", declaredType: "image/jpeg", payload: jpegWithoutScanData(t), wantError: ErrInvalidImage},
|
||||
{name: "too large", filename: "image.png", declaredType: "image/png", payload: bytes.Repeat([]byte{0}, domain.MaxAttachmentSizeBytes+1), wantError: ErrPayloadTooLarge},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
objectStore := newFakeObjectStore()
|
||||
service, _, actorID, chatID := newAttachmentService(t, objectStore)
|
||||
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: actorID,
|
||||
ChatID: chatID,
|
||||
Filename: testCase.filename,
|
||||
DeclaredContentType: testCase.declaredType,
|
||||
Body: bytes.NewReader(testCase.payload),
|
||||
})
|
||||
if !errors.Is(err, testCase.wantError) {
|
||||
t.Fatalf("error = %v, want %v", err, testCase.wantError)
|
||||
}
|
||||
if objectStore.putCalls != 0 {
|
||||
t.Fatalf("invalid upload reached object storage %d times", objectStore.putCalls)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAttachmentCompensatesObjectStoreFailure(t *testing.T) {
|
||||
objectStore := newFakeObjectStore()
|
||||
objectStore.putError = errors.New("injected put failure")
|
||||
service, metadataStore, actorID, chatID := newAttachmentService(t, objectStore)
|
||||
|
||||
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: actorID,
|
||||
ChatID: chatID,
|
||||
Filename: "image.jpg",
|
||||
DeclaredContentType: "image/jpeg",
|
||||
Body: bytes.NewReader(encodeTestImage(t, "image/jpeg")),
|
||||
})
|
||||
if !errors.Is(err, ErrStorageUnavailable) {
|
||||
t.Fatalf("error = %v, want ErrStorageUnavailable", err)
|
||||
}
|
||||
if objectStore.deleteCalls != 1 {
|
||||
t.Fatalf("compensating deletes = %d, want 1", objectStore.deleteCalls)
|
||||
}
|
||||
|
||||
objectStore.mu.Lock()
|
||||
keyParts := strings.Split(objectStore.lastPutKey, "/")
|
||||
objectStore.mu.Unlock()
|
||||
if len(keyParts) != 4 {
|
||||
t.Fatalf("unexpected generated object key: %q", objectStore.lastPutKey)
|
||||
}
|
||||
failedAttachment, lookupErr := metadataStore.GetAttachmentByID(keyParts[2])
|
||||
if lookupErr != nil {
|
||||
t.Fatal(lookupErr)
|
||||
}
|
||||
if failedAttachment.Status != domain.AttachmentStatusFailed || failedAttachment.ErrorCode == nil {
|
||||
t.Fatalf("failed upload metadata was not finalized: %+v", failedAttachment)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAttachmentMarksFailedBeforeBlockedObjectCleanup(t *testing.T) {
|
||||
objectStore := newFakeObjectStore()
|
||||
objectStore.putError = errors.New("injected put failure")
|
||||
objectStore.blockDelete = true
|
||||
service, metadataStore, actorID, chatID := newAttachmentService(t, objectStore)
|
||||
|
||||
startedAt := time.Now()
|
||||
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: actorID, ChatID: chatID, Filename: "blocked-cleanup.png",
|
||||
DeclaredContentType: "image/png", Body: bytes.NewReader(encodeTestImage(t, "image/png")),
|
||||
})
|
||||
if !errors.Is(err, ErrStorageUnavailable) {
|
||||
t.Fatalf("error = %v, want ErrStorageUnavailable", err)
|
||||
}
|
||||
if elapsed := time.Since(startedAt); elapsed > 2*time.Second {
|
||||
t.Fatalf("bounded cleanup took %s", elapsed)
|
||||
}
|
||||
keyParts := strings.Split(objectStore.lastPutKey, "/")
|
||||
failed, lookupErr := metadataStore.GetAttachmentByID(keyParts[2])
|
||||
if lookupErr != nil {
|
||||
t.Fatal(lookupErr)
|
||||
}
|
||||
if failed.Status != domain.AttachmentStatusFailed || failed.ErrorCode == nil {
|
||||
t.Fatalf("blocked object cleanup consumed DB cleanup context: %+v", failed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAttachmentRejectsInterruptedBodyBeforeMetadata(t *testing.T) {
|
||||
objectStore := newFakeObjectStore()
|
||||
service, metadataStore, actorID, chatID := newAttachmentService(t, objectStore)
|
||||
payload := encodeTestImage(t, "image/png")
|
||||
|
||||
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: actorID,
|
||||
ChatID: chatID,
|
||||
Filename: "interrupted.png",
|
||||
DeclaredContentType: "image/png",
|
||||
Body: io.MultiReader(bytes.NewReader(payload[:8]), alwaysFailReader{}),
|
||||
})
|
||||
if !errors.Is(err, ErrInvalidInput) {
|
||||
t.Fatalf("error = %v, want ErrInvalidInput", err)
|
||||
}
|
||||
if objectStore.putCalls != 0 {
|
||||
t.Fatal("interrupted upload reached object storage")
|
||||
}
|
||||
if _, lookupErr := metadataStore.GetAttachmentByID("missing"); !errors.Is(lookupErr, domain.ErrNotFound) {
|
||||
t.Fatalf("unexpected metadata lookup result: %v", lookupErr)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAttachmentPreservesObjectOnMetadataFinalizeFailure(t *testing.T) {
|
||||
legacyStore := memory.NewMemoryStore()
|
||||
repository := memory.NewMemoryRepository(legacyStore)
|
||||
user, err := repository.CreateUser(context.Background(), "finalize", "Finalize", "hash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chat, err := repository.CreateChat(context.Background(), "Finalize", []string{user.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
codec, err := NewCursorCodec("finalize-test-key-with-more-than-thirty-two-bytes")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objectStore := newFakeObjectStore()
|
||||
service, err := NewService(
|
||||
&finalizeFailureRepository{Repository: repository},
|
||||
codec,
|
||||
time.Hour,
|
||||
WithObjectStore(objectStore),
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: user.ID,
|
||||
ChatID: chat.ID,
|
||||
Filename: "finalize.png",
|
||||
DeclaredContentType: "image/png",
|
||||
Body: bytes.NewReader(encodeTestImage(t, "image/png")),
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("metadata finalize failure was hidden")
|
||||
}
|
||||
if objectStore.deleteCalls != 0 || len(objectStore.objects) != 1 {
|
||||
t.Fatalf("ambiguous object must be preserved: deletes=%d objects=%d", objectStore.deleteCalls, len(objectStore.objects))
|
||||
}
|
||||
keyParts := strings.Split(objectStore.lastPutKey, "/")
|
||||
pending, lookupErr := legacyStore.GetAttachmentByID(keyParts[2])
|
||||
if lookupErr != nil {
|
||||
t.Fatal(lookupErr)
|
||||
}
|
||||
if pending.Status != domain.AttachmentStatusPending || pending.ErrorCode != nil {
|
||||
t.Fatalf("ambiguous metadata must remain reconcilable: %+v", pending)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAttachmentRecoversWhenCommitResponseIsLost(t *testing.T) {
|
||||
legacyStore := memory.NewMemoryStore()
|
||||
baseRepository := memory.NewMemoryRepository(legacyStore)
|
||||
user, err := baseRepository.CreateUser(context.Background(), "commit-lost", "Commit Lost", "hash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chat, err := baseRepository.CreateChat(context.Background(), "Commit Lost", []string{user.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
codec, err := NewCursorCodec("commit-lost-test-key-with-more-than-thirty-two-bytes")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objectStore := newFakeObjectStore()
|
||||
repository := &commitThenDisconnectRepository{Repository: baseRepository}
|
||||
service, err := NewService(repository, codec, time.Hour, WithObjectStore(objectStore))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
attachment, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: user.ID, ChatID: chat.ID, Filename: "commit.png",
|
||||
DeclaredContentType: "image/png", Body: bytes.NewReader(encodeTestImage(t, "image/png")),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("committed ready attachment was not recovered: %v", err)
|
||||
}
|
||||
if attachment.Status != domain.AttachmentStatusReady || len(objectStore.objects) != 1 || objectStore.deleteCalls != 0 {
|
||||
t.Fatalf("lost commit response damaged attachment: attachment=%+v deletes=%d objects=%d",
|
||||
attachment, objectStore.deleteCalls, len(objectStore.objects))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAttachmentNeverDeletesObjectAfterAmbiguousCommit(t *testing.T) {
|
||||
legacyStore := memory.NewMemoryStore()
|
||||
baseRepository := memory.NewMemoryRepository(legacyStore)
|
||||
user, err := baseRepository.CreateUser(context.Background(), "commit-outage", "Commit Outage", "hash")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chat, err := baseRepository.CreateChat(context.Background(), "Commit Outage", []string{user.ID})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
codec, err := NewCursorCodec("commit-outage-test-key-with-more-than-thirty-two-bytes")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objectStore := newFakeObjectStore()
|
||||
repository := &commitThenDisconnectRepository{Repository: baseRepository, hideReadback: true}
|
||||
service, err := NewService(repository, codec, time.Hour, WithObjectStore(objectStore))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
_, err = service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: user.ID, ChatID: chat.ID, Filename: "commit.png",
|
||||
DeclaredContentType: "image/png", Body: bytes.NewReader(encodeTestImage(t, "image/png")),
|
||||
})
|
||||
if err == nil {
|
||||
t.Fatal("database outage after commit was hidden")
|
||||
}
|
||||
keyParts := strings.Split(objectStore.lastPutKey, "/")
|
||||
stored, lookupErr := legacyStore.GetAttachmentByID(keyParts[2])
|
||||
if lookupErr != nil {
|
||||
t.Fatal(lookupErr)
|
||||
}
|
||||
if stored.Status != domain.AttachmentStatusReady || len(objectStore.objects) != 1 || objectStore.deleteCalls != 0 {
|
||||
t.Fatalf("committed ready metadata lost its original: metadata=%+v deletes=%d objects=%d",
|
||||
stored, objectStore.deleteCalls, len(objectStore.objects))
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadAttachmentRejectsPixelLimitBeforeFullDecode(t *testing.T) {
|
||||
objectStore := newFakeObjectStore()
|
||||
service, _, actorID, chatID := newAttachmentService(t, objectStore)
|
||||
|
||||
_, err := service.UploadAttachment(context.Background(), UploadAttachmentCommand{
|
||||
ActorID: actorID,
|
||||
ChatID: chatID,
|
||||
Filename: "huge.png",
|
||||
DeclaredContentType: "image/png",
|
||||
Body: bytes.NewReader(pngConfigurationOnly(10_000, 4_001)),
|
||||
})
|
||||
if !errors.Is(err, ErrPayloadTooLarge) {
|
||||
t.Fatalf("error = %v, want ErrPayloadTooLarge", err)
|
||||
}
|
||||
if objectStore.putCalls != 0 {
|
||||
t.Fatalf("oversized decoded image reached object storage %d times", objectStore.putCalls)
|
||||
}
|
||||
}
|
||||
|
||||
func pngConfigurationOnly(width uint32, height uint32) []byte {
|
||||
payload := make([]byte, 33)
|
||||
copy(payload[0:8], []byte{137, 80, 78, 71, 13, 10, 26, 10})
|
||||
binary.BigEndian.PutUint32(payload[8:12], 13)
|
||||
copy(payload[12:16], "IHDR")
|
||||
binary.BigEndian.PutUint32(payload[16:20], width)
|
||||
binary.BigEndian.PutUint32(payload[20:24], height)
|
||||
payload[24] = 8
|
||||
payload[25] = 6
|
||||
payload[26] = 0
|
||||
payload[27] = 0
|
||||
payload[28] = 0
|
||||
binary.BigEndian.PutUint32(payload[29:33], crc32.ChecksumIEEE(payload[12:29]))
|
||||
return payload
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
func (s *Service) CreateChat(ctx context.Context, command CreateChatCommand) (domain.Chat, error) {
|
||||
if err := domain.ValidateChatInput(command.Title, command.MemberIDs, command.ActorID); err != nil {
|
||||
return domain.Chat{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
|
||||
}
|
||||
return s.repository.CreateChat(ctx, command.Title, command.MemberIDs)
|
||||
}
|
||||
|
||||
func (s *Service) GetChat(ctx context.Context, actorID, chatID string) (domain.Chat, error) {
|
||||
return s.repository.GetChatByID(ctx, chatID, actorID)
|
||||
}
|
||||
|
||||
func (s *Service) ListChats(
|
||||
ctx context.Context,
|
||||
actorID string,
|
||||
limit int,
|
||||
cursorToken string,
|
||||
) (domain.ChatPage, error) {
|
||||
var cursor *domain.ChatCursor
|
||||
if cursorToken != "" {
|
||||
decoded, err := s.cursors.DecodeChat(cursorToken, actorID)
|
||||
if err != nil {
|
||||
return domain.ChatPage{}, ErrInvalidCursor
|
||||
}
|
||||
cursor = decoded
|
||||
}
|
||||
|
||||
items, hasMore, err := s.repository.GetChatsForUser(ctx, actorID, limit, cursor)
|
||||
if err != nil {
|
||||
return domain.ChatPage{}, err
|
||||
}
|
||||
page := domain.ChatPage{Items: items}
|
||||
if page.Items == nil {
|
||||
page.Items = make([]domain.Chat, 0)
|
||||
}
|
||||
if hasMore {
|
||||
last := page.Items[len(page.Items)-1]
|
||||
next, encodeErr := s.cursors.EncodeChat(domain.ChatCursor{
|
||||
CreatedAt: last.CreatedAt, ChatID: last.ID, UserID: actorID,
|
||||
})
|
||||
if encodeErr != nil {
|
||||
return domain.ChatPage{}, encodeErr
|
||||
}
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package application
|
||||
|
||||
import "io"
|
||||
|
||||
type CreateUserCommand struct {
|
||||
Username string
|
||||
DisplayName string
|
||||
Password string
|
||||
}
|
||||
|
||||
type CreateSessionCommand struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
type CreateChatCommand struct {
|
||||
ActorID string
|
||||
Title string
|
||||
MemberIDs []string
|
||||
}
|
||||
|
||||
type SendMessageCommand struct {
|
||||
ActorID string
|
||||
ChatID string
|
||||
Text string
|
||||
AttachmentIDs []string
|
||||
Method string
|
||||
CanonicalPath string
|
||||
IdempotencyKey string
|
||||
}
|
||||
|
||||
type UploadAttachmentCommand struct {
|
||||
ActorID string
|
||||
ChatID string
|
||||
Filename string
|
||||
DeclaredContentType string
|
||||
Body io.Reader
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const cursorVersion = 1
|
||||
|
||||
type CursorCodec struct {
|
||||
key []byte
|
||||
}
|
||||
|
||||
type cursorPayload struct {
|
||||
Version int `json:"v"`
|
||||
Kind string `json:"kind"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
ID string `json:"id"`
|
||||
ScopeID string `json:"scope_id"`
|
||||
}
|
||||
|
||||
func NewCursorCodec(key string) (*CursorCodec, error) {
|
||||
if len(key) < 32 {
|
||||
return nil, fmt.Errorf("cursor signing key must contain at least 32 bytes")
|
||||
}
|
||||
return &CursorCodec{key: []byte(key)}, nil
|
||||
}
|
||||
|
||||
func (c *CursorCodec) EncodeChat(cursor domain.ChatCursor) (string, error) {
|
||||
return c.encode(cursorPayload{
|
||||
Version: cursorVersion, Kind: "chat", CreatedAt: cursor.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
ID: cursor.ChatID, ScopeID: cursor.UserID,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *CursorCodec) DecodeChat(token, expectedUserID string) (*domain.ChatCursor, error) {
|
||||
payload, err := c.decode(token, "chat", expectedUserID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &domain.ChatCursor{CreatedAt: payload.createdAt, ChatID: payload.id, UserID: payload.scopeID}, nil
|
||||
}
|
||||
|
||||
func (c *CursorCodec) EncodeMessage(cursor domain.MessageCursor) (string, error) {
|
||||
return c.encode(cursorPayload{
|
||||
Version: cursorVersion, Kind: "message", CreatedAt: cursor.CreatedAt.UTC().Format(time.RFC3339Nano),
|
||||
ID: cursor.MessageID, ScopeID: cursor.ChatID,
|
||||
})
|
||||
}
|
||||
|
||||
func (c *CursorCodec) DecodeMessage(token, expectedChatID string) (*domain.MessageCursor, error) {
|
||||
payload, err := c.decode(token, "message", expectedChatID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &domain.MessageCursor{CreatedAt: payload.createdAt, MessageID: payload.id, ChatID: payload.scopeID}, nil
|
||||
}
|
||||
|
||||
func (c *CursorCodec) encode(payload cursorPayload) (string, error) {
|
||||
encodedPayload, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("encode cursor payload: %w", err)
|
||||
}
|
||||
mac := hmac.New(sha256.New, c.key)
|
||||
_, _ = mac.Write(encodedPayload)
|
||||
return base64.RawURLEncoding.EncodeToString(encodedPayload) + "." +
|
||||
base64.RawURLEncoding.EncodeToString(mac.Sum(nil)), nil
|
||||
}
|
||||
|
||||
type decodedCursor struct {
|
||||
createdAt time.Time
|
||||
id string
|
||||
scopeID string
|
||||
}
|
||||
|
||||
func (c *CursorCodec) decode(token, expectedKind, expectedScopeID string) (decodedCursor, error) {
|
||||
parts := strings.Split(token, ".")
|
||||
if len(parts) != 2 {
|
||||
return decodedCursor{}, ErrInvalidCursor
|
||||
}
|
||||
payloadBytes, err := base64.RawURLEncoding.DecodeString(parts[0])
|
||||
if err != nil {
|
||||
return decodedCursor{}, ErrInvalidCursor
|
||||
}
|
||||
signature, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
return decodedCursor{}, ErrInvalidCursor
|
||||
}
|
||||
mac := hmac.New(sha256.New, c.key)
|
||||
_, _ = mac.Write(payloadBytes)
|
||||
if !hmac.Equal(signature, mac.Sum(nil)) {
|
||||
return decodedCursor{}, ErrInvalidCursor
|
||||
}
|
||||
|
||||
var payload cursorPayload
|
||||
if err = json.Unmarshal(payloadBytes, &payload); err != nil ||
|
||||
payload.Version != cursorVersion || payload.Kind != expectedKind ||
|
||||
payload.ScopeID != expectedScopeID {
|
||||
return decodedCursor{}, ErrInvalidCursor
|
||||
}
|
||||
createdAt, err := time.Parse(time.RFC3339Nano, payload.CreatedAt)
|
||||
if err != nil {
|
||||
return decodedCursor{}, ErrInvalidCursor
|
||||
}
|
||||
if _, err = uuid.Parse(payload.ID); err != nil {
|
||||
return decodedCursor{}, ErrInvalidCursor
|
||||
}
|
||||
if _, err = uuid.Parse(payload.ScopeID); err != nil {
|
||||
return decodedCursor{}, ErrInvalidCursor
|
||||
}
|
||||
return decodedCursor{createdAt: createdAt.UTC(), id: payload.ID, scopeID: payload.ScopeID}, nil
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
const cursorTestKey = "cursor-test-key-with-more-than-thirty-two-bytes"
|
||||
|
||||
func TestCursorCodecRoundTrip(t *testing.T) {
|
||||
codec, err := NewCursorCodec(cursorTestKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
expected := domain.MessageCursor{
|
||||
CreatedAt: time.Date(2026, 10, 18, 12, 30, 0, 123, time.UTC),
|
||||
MessageID: "42eea32e-d9b5-4bc6-b4fc-94e9b0477adf",
|
||||
ChatID: "9fa3f49c-e62e-4209-8414-d6d5f69805bf",
|
||||
}
|
||||
token, err := codec.EncodeMessage(expected)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual, err := codec.DecodeMessage(token, expected.ChatID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if *actual != expected {
|
||||
t.Fatalf("cursor changed after round trip: got %#v, want %#v", *actual, expected)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursorCodecRejectsTamperingAndWrongScope(t *testing.T) {
|
||||
codec, err := NewCursorCodec(cursorTestKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
token, err := codec.EncodeChat(domain.ChatCursor{
|
||||
CreatedAt: time.Now().UTC(),
|
||||
ChatID: "42eea32e-d9b5-4bc6-b4fc-94e9b0477adf",
|
||||
UserID: "9fa3f49c-e62e-4209-8414-d6d5f69805bf",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
parts := strings.Split(token, ".")
|
||||
signature, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
signature[0] ^= 1
|
||||
tampered := parts[0] + "." + base64.RawURLEncoding.EncodeToString(signature)
|
||||
if _, err = codec.DecodeChat(tampered, "9fa3f49c-e62e-4209-8414-d6d5f69805bf"); !errors.Is(err, ErrInvalidCursor) {
|
||||
t.Fatalf("tampered cursor: got %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
if _, err = codec.DecodeChat(token, "e6ca4af0-69ed-4784-8ce0-ee035299fe08"); !errors.Is(err, ErrInvalidCursor) {
|
||||
t.Fatalf("wrong scope: got %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
if _, err = codec.DecodeMessage(token, "9fa3f49c-e62e-4209-8414-d6d5f69805bf"); !errors.Is(err, ErrInvalidCursor) {
|
||||
t.Fatalf("wrong cursor kind: got %v, want ErrInvalidCursor", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursorCodecRequiresStrongSharedKey(t *testing.T) {
|
||||
if _, err := NewCursorCodec("too-short"); err == nil {
|
||||
t.Fatal("expected short cursor signing key to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCursorCodecRejectsSignedInvalidFieldTypesBeforeSQL(t *testing.T) {
|
||||
codec, err := NewCursorCodec(cursorTestKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
chatID := "9fa3f49c-e62e-4209-8414-d6d5f69805bf"
|
||||
validMessageID := "42eea32e-d9b5-4bc6-b4fc-94e9b0477adf"
|
||||
testCases := []cursorPayload{
|
||||
{Version: cursorVersion, Kind: "message", CreatedAt: "not-a-time", ID: validMessageID, ScopeID: chatID},
|
||||
{Version: cursorVersion, Kind: "message", CreatedAt: time.Now().UTC().Format(time.RFC3339Nano), ID: "not-a-uuid", ScopeID: chatID},
|
||||
}
|
||||
for _, payload := range testCases {
|
||||
token, encodeErr := codec.encode(payload)
|
||||
if encodeErr != nil {
|
||||
t.Fatal(encodeErr)
|
||||
}
|
||||
if _, decodeErr := codec.DecodeMessage(token, chatID); !errors.Is(decodeErr, ErrInvalidCursor) {
|
||||
t.Fatalf("invalid signed cursor returned %v, want ErrInvalidCursor", decodeErr)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package application
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrInvalidCursor = errors.New("invalid cursor")
|
||||
ErrInvalidCredentials = errors.New("invalid credentials")
|
||||
ErrInvalidInput = errors.New("invalid input")
|
||||
ErrCredentialState = errors.New("invalid stored credential state")
|
||||
ErrIdempotencyConflict = errors.New("idempotency key is already used for another request")
|
||||
ErrAttachmentNotReady = errors.New("attachment is not ready")
|
||||
ErrPayloadTooLarge = errors.New("payload too large")
|
||||
ErrUnsupportedMediaType = errors.New("unsupported media type")
|
||||
ErrInvalidImage = errors.New("invalid image")
|
||||
ErrStorageUnavailable = errors.New("object storage unavailable")
|
||||
)
|
||||
@@ -0,0 +1,88 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type canonicalMessageRequest struct {
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
func (s *Service) SendMessage(ctx context.Context, command SendMessageCommand) (domain.Message, error) {
|
||||
attachmentIDs := append([]string(nil), command.AttachmentIDs...)
|
||||
if attachmentIDs == nil {
|
||||
attachmentIDs = make([]string, 0)
|
||||
}
|
||||
|
||||
if err := domain.ValidateMessageInput(command.Text, attachmentIDs); err != nil {
|
||||
return domain.Message{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
|
||||
}
|
||||
for index, attachmentID := range attachmentIDs {
|
||||
attachmentIDs[index] = uuid.MustParse(attachmentID).String()
|
||||
}
|
||||
|
||||
canonicalBody, err := json.Marshal(canonicalMessageRequest{
|
||||
Text: command.Text,
|
||||
AttachmentIDs: attachmentIDs,
|
||||
})
|
||||
if err != nil {
|
||||
return domain.Message{}, fmt.Errorf("canonicalize message request: %w", err)
|
||||
}
|
||||
|
||||
return s.repository.CreateMessageIdempotently(ctx, IdempotentMessageWrite{
|
||||
ActorID: command.ActorID,
|
||||
ChatID: command.ChatID,
|
||||
Text: command.Text,
|
||||
AttachmentIDs: attachmentIDs,
|
||||
Method: command.Method,
|
||||
CanonicalPath: command.CanonicalPath,
|
||||
Key: command.IdempotencyKey,
|
||||
RequestHash: sha256.Sum256(canonicalBody),
|
||||
TTL: s.idempotencyTTL,
|
||||
})
|
||||
}
|
||||
|
||||
func (s *Service) ListMessages(
|
||||
ctx context.Context,
|
||||
actorID string,
|
||||
chatID string,
|
||||
limit int,
|
||||
cursorToken string,
|
||||
) (domain.MessagePage, error) {
|
||||
var cursor *domain.MessageCursor
|
||||
if cursorToken != "" {
|
||||
decoded, err := s.cursors.DecodeMessage(cursorToken, chatID)
|
||||
if err != nil {
|
||||
return domain.MessagePage{}, ErrInvalidCursor
|
||||
}
|
||||
cursor = decoded
|
||||
}
|
||||
|
||||
items, hasMore, err := s.repository.GetMessages(ctx, chatID, actorID, limit, cursor)
|
||||
if err != nil {
|
||||
return domain.MessagePage{}, err
|
||||
}
|
||||
page := domain.MessagePage{Items: items}
|
||||
if page.Items == nil {
|
||||
page.Items = make([]domain.Message, 0)
|
||||
}
|
||||
if hasMore {
|
||||
last := page.Items[len(page.Items)-1]
|
||||
next, encodeErr := s.cursors.EncodeMessage(domain.MessageCursor{
|
||||
CreatedAt: last.CreatedAt, MessageID: last.ID, ChatID: chatID,
|
||||
})
|
||||
if encodeErr != nil {
|
||||
return domain.MessagePage{}, encodeErr
|
||||
}
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type UserRepository interface {
|
||||
CreateUser(ctx context.Context, username, displayName, passwordHash string) (domain.User, error)
|
||||
GetUserByUsername(ctx context.Context, username string) (domain.User, string, error)
|
||||
}
|
||||
|
||||
type SessionRepository interface {
|
||||
CreateSession(ctx context.Context, tokenHash []byte, userID string, expiresAt time.Time) error
|
||||
GetUserBySession(ctx context.Context, tokenHash []byte) (domain.User, error)
|
||||
DeleteSession(ctx context.Context, tokenHash []byte) error
|
||||
}
|
||||
|
||||
type ChatRepository interface {
|
||||
CreateChat(ctx context.Context, title string, memberIDs []string) (domain.Chat, error)
|
||||
GetChatByID(ctx context.Context, chatID, callerID string) (domain.Chat, error)
|
||||
GetChatsForUser(ctx context.Context, userID string, limit int, cursor *domain.ChatCursor) ([]domain.Chat, bool, error)
|
||||
}
|
||||
|
||||
type IdempotentMessageWrite struct {
|
||||
ActorID string
|
||||
ChatID string
|
||||
Text string
|
||||
AttachmentIDs []string
|
||||
Method string
|
||||
CanonicalPath string
|
||||
Key string
|
||||
RequestHash [32]byte
|
||||
TTL time.Duration
|
||||
}
|
||||
|
||||
type MessageRepository interface {
|
||||
CreateMessageIdempotently(ctx context.Context, write IdempotentMessageWrite) (domain.Message, error)
|
||||
GetMessages(ctx context.Context, chatID, callerID string, limit int, cursor *domain.MessageCursor) ([]domain.Message, bool, error)
|
||||
}
|
||||
|
||||
type AttachmentRepository interface {
|
||||
CreateAttachment(ctx context.Context, attachment domain.Attachment) (domain.Attachment, error)
|
||||
MarkAttachmentReady(ctx context.Context, attachmentID string, updatedAt time.Time) (domain.Attachment, error)
|
||||
MarkAttachmentFailed(ctx context.Context, attachmentID, errorCode string, updatedAt time.Time) error
|
||||
GetAttachment(ctx context.Context, attachmentID, callerID string) (domain.Attachment, error)
|
||||
}
|
||||
|
||||
type Repository interface {
|
||||
UserRepository
|
||||
SessionRepository
|
||||
ChatRepository
|
||||
MessageRepository
|
||||
AttachmentRepository
|
||||
}
|
||||
|
||||
type ObjectPut struct {
|
||||
Key string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Body io.Reader
|
||||
}
|
||||
|
||||
type ObjectContent struct {
|
||||
Body io.ReadCloser
|
||||
SizeBytes int64
|
||||
}
|
||||
|
||||
type ObjectStore interface {
|
||||
Put(ctx context.Context, object ObjectPut) error
|
||||
Get(ctx context.Context, key string) (ObjectContent, error)
|
||||
Delete(ctx context.Context, key string) error
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
const MinimumIdempotencyTTL = 24 * time.Hour
|
||||
const defaultAttachmentCleanupTimeout = time.Second
|
||||
const defaultAttachmentUploadConcurrency = 4
|
||||
const defaultAttachmentDecodeConcurrency = 1
|
||||
|
||||
type ServiceOption func(*Service) error
|
||||
|
||||
type Service struct {
|
||||
repository Repository
|
||||
cursors *CursorCodec
|
||||
sessionTTL time.Duration
|
||||
idempotencyTTL time.Duration
|
||||
now func() time.Time
|
||||
objectStore ObjectStore
|
||||
attachmentCleanupTimeout time.Duration
|
||||
attachmentUploadSlots chan struct{}
|
||||
attachmentDecodeSlots chan struct{}
|
||||
}
|
||||
|
||||
func NewService(repository Repository, cursors *CursorCodec, sessionTTL time.Duration, options ...ServiceOption) (*Service, error) {
|
||||
if repository == nil {
|
||||
return nil, fmt.Errorf("repository is required")
|
||||
}
|
||||
if cursors == nil {
|
||||
return nil, fmt.Errorf("cursor codec is required")
|
||||
}
|
||||
if sessionTTL <= 0 {
|
||||
return nil, fmt.Errorf("session TTL must be positive")
|
||||
}
|
||||
service := &Service{
|
||||
repository: repository,
|
||||
cursors: cursors,
|
||||
sessionTTL: sessionTTL,
|
||||
idempotencyTTL: MinimumIdempotencyTTL,
|
||||
now: func() time.Time { return time.Now().UTC() },
|
||||
attachmentCleanupTimeout: defaultAttachmentCleanupTimeout,
|
||||
attachmentUploadSlots: make(chan struct{}, defaultAttachmentUploadConcurrency),
|
||||
attachmentDecodeSlots: make(chan struct{}, defaultAttachmentDecodeConcurrency),
|
||||
}
|
||||
for _, option := range options {
|
||||
if err := option(service); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
return service, nil
|
||||
}
|
||||
|
||||
func WithIdempotencyTTL(ttl time.Duration) ServiceOption {
|
||||
return func(service *Service) error {
|
||||
if ttl < MinimumIdempotencyTTL {
|
||||
return fmt.Errorf("idempotency TTL must be at least %s", MinimumIdempotencyTTL)
|
||||
}
|
||||
service.idempotencyTTL = ttl
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithObjectStore(objectStore ObjectStore) ServiceOption {
|
||||
return func(service *Service) error {
|
||||
if objectStore == nil {
|
||||
return fmt.Errorf("object store is required")
|
||||
}
|
||||
service.objectStore = objectStore
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
"mtusi2026lab4/internal/security"
|
||||
)
|
||||
|
||||
const dummyPasswordHash = "$argon2id$v=19$m=19456,t=2,p=1$" +
|
||||
"AAAAAAAAAAAAAAAAAAAAAA$" +
|
||||
"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"
|
||||
|
||||
func (s *Service) CreateSession(ctx context.Context, command CreateSessionCommand) (domain.Session, error) {
|
||||
user, passwordHash, err := s.repository.GetUserByUsername(ctx, command.Username)
|
||||
userNotFound := errors.Is(err, domain.ErrNotFound)
|
||||
if userNotFound {
|
||||
passwordHash = dummyPasswordHash
|
||||
} else if err != nil {
|
||||
return domain.Session{}, err
|
||||
}
|
||||
|
||||
passwordMatches, verifyErr := security.VerifyPassword(command.Password, passwordHash)
|
||||
if verifyErr != nil {
|
||||
return domain.Session{}, fmt.Errorf("%w: %v", ErrCredentialState, verifyErr)
|
||||
}
|
||||
if userNotFound || !passwordMatches {
|
||||
return domain.Session{}, ErrInvalidCredentials
|
||||
}
|
||||
|
||||
token, tokenHash, err := security.GenerateSessionToken()
|
||||
if err != nil {
|
||||
return domain.Session{}, fmt.Errorf("generate session token: %w", err)
|
||||
}
|
||||
expiresAt := s.now().Add(s.sessionTTL)
|
||||
if err = s.repository.CreateSession(ctx, tokenHash, user.ID, expiresAt); err != nil {
|
||||
return domain.Session{}, err
|
||||
}
|
||||
return domain.Session{
|
||||
Token: token, TokenType: "Bearer", ExpiresAt: expiresAt, User: user,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *Service) Authenticate(ctx context.Context, token string) (domain.User, []byte, error) {
|
||||
tokenHash := security.HashSessionToken(token)
|
||||
user, err := s.repository.GetUserBySession(ctx, tokenHash)
|
||||
if err != nil {
|
||||
return domain.User{}, nil, err
|
||||
}
|
||||
return user, tokenHash, nil
|
||||
}
|
||||
|
||||
func (s *Service) DeleteSession(ctx context.Context, tokenHash []byte) error {
|
||||
return s.repository.DeleteSession(ctx, tokenHash)
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package application
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
"mtusi2026lab4/internal/security"
|
||||
)
|
||||
|
||||
func (s *Service) CreateUser(ctx context.Context, command CreateUserCommand) (domain.User, error) {
|
||||
if err := domain.ValidateUser(command.Username, command.DisplayName); err != nil {
|
||||
return domain.User{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
|
||||
}
|
||||
if err := security.ValidatePassword(command.Password); err != nil {
|
||||
return domain.User{}, fmt.Errorf("%w: %v", ErrInvalidInput, err)
|
||||
}
|
||||
|
||||
passwordHash, err := security.HashPassword(command.Password)
|
||||
if err != nil {
|
||||
return domain.User{}, fmt.Errorf("hash password: %w", err)
|
||||
}
|
||||
return s.repository.CreateUser(
|
||||
ctx,
|
||||
command.Username,
|
||||
command.DisplayName,
|
||||
passwordHash,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,465 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"os"
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
HTTPHost string
|
||||
HTTPPort string
|
||||
InstanceID string
|
||||
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
SessionCookieSecure bool
|
||||
CursorSigningKey string
|
||||
|
||||
DatabaseURL string
|
||||
DatabaseMaxConns int32
|
||||
DatabaseMinConns int32
|
||||
DatabaseMaxConnLifetime time.Duration
|
||||
DatabaseMaxConnIdleTime time.Duration
|
||||
DatabaseHealthCheckPeriod time.Duration
|
||||
DatabaseConnectTimeout time.Duration
|
||||
DatabaseQueryTimeout time.Duration
|
||||
|
||||
RedisURL string
|
||||
RedisCacheTTL time.Duration
|
||||
RedisOperationTimeout time.Duration
|
||||
RedisPoolSize int
|
||||
|
||||
S3Endpoint string
|
||||
S3Region string
|
||||
S3Bucket string
|
||||
S3AccessKeyID string
|
||||
S3SecretAccessKey string
|
||||
S3OperationTimeout time.Duration
|
||||
|
||||
ReadinessTimeout time.Duration
|
||||
ShutdownDrainDelay time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
}
|
||||
|
||||
func Load() (Config, error) {
|
||||
instanceID, err := instanceIDFromEnv()
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
sessionTTL, err := secondsDurationFromEnv(
|
||||
"SESSION_TTL_SECONDS",
|
||||
3600,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
idempotencyTTL, err := secondsDurationFromEnv(
|
||||
"IDEMPOTENCY_TTL_SECONDS",
|
||||
86400,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
sessionCookieSecure, err := boolFromEnv("SESSION_COOKIE_SECURE", false)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
databaseURL, err := requiredValueOrFile(
|
||||
"DATABASE_URL",
|
||||
"DATABASE_URL_FILE",
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
cursorSigningKey, err := requiredValueOrFile(
|
||||
"CURSOR_SIGNING_KEY",
|
||||
"CURSOR_SIGNING_KEY_FILE",
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
s3AccessKeyID, err := requiredValueOrFile(
|
||||
"S3_ACCESS_KEY_ID",
|
||||
"S3_ACCESS_KEY_ID_FILE",
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3SecretAccessKey, err := requiredValueOrFile(
|
||||
"S3_SECRET_ACCESS_KEY",
|
||||
"S3_SECRET_ACCESS_KEY_FILE",
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
s3OperationTimeout, err := durationFromEnv("S3_OPERATION_TIMEOUT", 5*time.Second)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
databaseMaxConns, err := int32FromEnv("DB_MAX_CONNS", 10, 1)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
databaseMinConns, err := int32FromEnv("DB_MIN_CONNS", 2, 0)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
databaseMaxConnLifetime, err := durationFromEnv(
|
||||
"DB_MAX_CONN_LIFETIME",
|
||||
30*time.Minute,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
databaseMaxConnIdleTime, err := durationFromEnv(
|
||||
"DB_MAX_CONN_IDLE_TIME",
|
||||
5*time.Minute,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
databaseHealthCheckPeriod, err := durationFromEnv(
|
||||
"DB_HEALTH_CHECK_PERIOD",
|
||||
30*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
databaseConnectTimeout, err := durationFromEnv(
|
||||
"DB_CONNECT_TIMEOUT",
|
||||
5*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
databaseQueryTimeout, err := durationFromEnv(
|
||||
"DB_QUERY_TIMEOUT",
|
||||
3*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
redisURL, err := optionalValueOrFile("CACHE_URL", "CACHE_URL_FILE")
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
redisCacheTTL, err := durationFromEnv(
|
||||
"CACHE_TTL",
|
||||
5*time.Minute,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
redisOperationTimeout, err := durationFromEnv(
|
||||
"CACHE_OPERATION_TIMEOUT",
|
||||
250*time.Millisecond,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
redisPoolSize, err := int32FromEnv("CACHE_POOL_SIZE", 10, 1)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
readinessTimeout, err := durationFromEnv(
|
||||
"READINESS_TIMEOUT",
|
||||
2*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
shutdownDrainDelay, err := durationFromEnv(
|
||||
"SHUTDOWN_DRAIN_DELAY",
|
||||
2*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
shutdownTimeout, err := durationFromEnv(
|
||||
"SHUTDOWN_TIMEOUT",
|
||||
10*time.Second,
|
||||
)
|
||||
if err != nil {
|
||||
return Config{}, err
|
||||
}
|
||||
|
||||
if databaseMinConns > databaseMaxConns {
|
||||
return Config{}, errors.New(
|
||||
"DB_MIN_CONNS must not be greater than DB_MAX_CONNS",
|
||||
)
|
||||
}
|
||||
|
||||
if idempotencyTTL < 24*time.Hour {
|
||||
return Config{}, errors.New(
|
||||
"IDEMPOTENCY_TTL_SECONDS must be at least 86400",
|
||||
)
|
||||
}
|
||||
|
||||
if shutdownDrainDelay >= shutdownTimeout {
|
||||
return Config{}, errors.New(
|
||||
"SHUTDOWN_DRAIN_DELAY must be less than SHUTDOWN_TIMEOUT",
|
||||
)
|
||||
}
|
||||
|
||||
return Config{
|
||||
HTTPHost: envOrDefault("APP_HOST", "0.0.0.0"),
|
||||
HTTPPort: envOrDefault("APP_PORT", "8080"),
|
||||
InstanceID: instanceID,
|
||||
SessionTTL: sessionTTL,
|
||||
IdempotencyTTL: idempotencyTTL,
|
||||
SessionCookieSecure: sessionCookieSecure,
|
||||
CursorSigningKey: cursorSigningKey,
|
||||
|
||||
DatabaseURL: databaseURL,
|
||||
DatabaseMaxConns: databaseMaxConns,
|
||||
DatabaseMinConns: databaseMinConns,
|
||||
DatabaseMaxConnLifetime: databaseMaxConnLifetime,
|
||||
DatabaseMaxConnIdleTime: databaseMaxConnIdleTime,
|
||||
DatabaseHealthCheckPeriod: databaseHealthCheckPeriod,
|
||||
DatabaseConnectTimeout: databaseConnectTimeout,
|
||||
DatabaseQueryTimeout: databaseQueryTimeout,
|
||||
|
||||
RedisURL: redisURL,
|
||||
RedisCacheTTL: redisCacheTTL,
|
||||
RedisOperationTimeout: redisOperationTimeout,
|
||||
RedisPoolSize: int(redisPoolSize),
|
||||
|
||||
S3Endpoint: envOrDefault("S3_ENDPOINT", "http://minio:9000"),
|
||||
S3Region: envOrDefault("S3_REGION", "us-east-1"),
|
||||
S3Bucket: envOrDefault("S3_BUCKET", "messenger-attachments"),
|
||||
S3AccessKeyID: s3AccessKeyID,
|
||||
S3SecretAccessKey: s3SecretAccessKey,
|
||||
S3OperationTimeout: s3OperationTimeout,
|
||||
ReadinessTimeout: readinessTimeout,
|
||||
ShutdownDrainDelay: shutdownDrainDelay,
|
||||
ShutdownTimeout: shutdownTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
var instanceIDPattern = regexp.MustCompile(`^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$`)
|
||||
|
||||
func instanceIDFromEnv() (string, error) {
|
||||
instanceID := strings.TrimSpace(os.Getenv("INSTANCE_ID"))
|
||||
if instanceID == "" {
|
||||
hostname, err := os.Hostname()
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("resolve instance id from hostname: %w", err)
|
||||
}
|
||||
instanceID = hostname
|
||||
}
|
||||
|
||||
if !instanceIDPattern.MatchString(instanceID) {
|
||||
return "", errors.New(
|
||||
"INSTANCE_ID or hostname must match [A-Za-z0-9][A-Za-z0-9._-]{0,127}",
|
||||
)
|
||||
}
|
||||
|
||||
return instanceID, nil
|
||||
}
|
||||
|
||||
func (c Config) HTTPAddress() string {
|
||||
return net.JoinHostPort(c.HTTPHost, c.HTTPPort)
|
||||
}
|
||||
|
||||
func requiredValueOrFile(valueEnv, fileEnv string) (string, error) {
|
||||
value, valueExists := os.LookupEnv(valueEnv)
|
||||
filePath, fileExists := os.LookupEnv(fileEnv)
|
||||
|
||||
if valueExists && fileExists {
|
||||
return "", fmt.Errorf(
|
||||
"%s and %s must not be set at the same time",
|
||||
valueEnv,
|
||||
fileEnv,
|
||||
)
|
||||
}
|
||||
|
||||
if valueExists {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s must not be empty", valueEnv)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
if fileExists {
|
||||
filePath = strings.TrimSpace(filePath)
|
||||
if filePath == "" {
|
||||
return "", fmt.Errorf("%s must not be empty", fileEnv)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read %s: %w", fileEnv, err)
|
||||
}
|
||||
|
||||
value = strings.TrimSpace(string(data))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("file from %s must not be empty", fileEnv)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
return "", fmt.Errorf("%s or %s must be set", valueEnv, fileEnv)
|
||||
}
|
||||
|
||||
func optionalValueOrFile(valueEnv, fileEnv string) (string, error) {
|
||||
value, valueExists := os.LookupEnv(valueEnv)
|
||||
filePath, fileExists := os.LookupEnv(fileEnv)
|
||||
|
||||
if valueExists && fileExists {
|
||||
return "", fmt.Errorf(
|
||||
"%s and %s must not be set at the same time",
|
||||
valueEnv,
|
||||
fileEnv,
|
||||
)
|
||||
}
|
||||
|
||||
if valueExists {
|
||||
value = strings.TrimSpace(value)
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("%s must not be empty", valueEnv)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
if fileExists {
|
||||
filePath = strings.TrimSpace(filePath)
|
||||
if filePath == "" {
|
||||
return "", fmt.Errorf("%s must not be empty", fileEnv)
|
||||
}
|
||||
|
||||
data, err := os.ReadFile(filePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read %s: %w", fileEnv, err)
|
||||
}
|
||||
|
||||
value = strings.TrimSpace(string(data))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("file from %s must not be empty", fileEnv)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
return "", nil
|
||||
}
|
||||
|
||||
func envOrDefault(name, fallback string) string {
|
||||
value := strings.TrimSpace(os.Getenv(name))
|
||||
if value == "" {
|
||||
return fallback
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func int32FromEnv(name string, fallback, minimum int32) (int32, error) {
|
||||
raw := strings.TrimSpace(os.Getenv(name))
|
||||
if raw == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
|
||||
value, err := strconv.ParseInt(raw, 10, 32)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%s must be an integer: %w", name, err)
|
||||
}
|
||||
|
||||
if value < int64(minimum) {
|
||||
return 0, fmt.Errorf("%s must be at least %d", name, minimum)
|
||||
}
|
||||
|
||||
return int32(value), nil
|
||||
}
|
||||
|
||||
func boolFromEnv(name string, fallback bool) (bool, error) {
|
||||
raw := strings.TrimSpace(os.Getenv(name))
|
||||
if raw == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
|
||||
value, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("%s must be true or false: %w", name, err)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func durationFromEnv(name string, fallback time.Duration) (time.Duration, error) {
|
||||
raw := strings.TrimSpace(os.Getenv(name))
|
||||
if raw == "" {
|
||||
return fallback, nil
|
||||
}
|
||||
|
||||
value, err := time.ParseDuration(raw)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf(
|
||||
"%s must be a Go duration such as 3s or 5m: %w",
|
||||
name,
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
if value <= 0 {
|
||||
return 0, fmt.Errorf("%s must be positive", name)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func secondsDurationFromEnv(
|
||||
name string,
|
||||
fallbackSeconds int64,
|
||||
) (time.Duration, error) {
|
||||
raw := strings.TrimSpace(os.Getenv(name))
|
||||
if raw == "" {
|
||||
raw = strconv.FormatInt(fallbackSeconds, 10)
|
||||
}
|
||||
|
||||
seconds, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf(
|
||||
"%s must be an integer number of seconds: %w",
|
||||
name,
|
||||
err,
|
||||
)
|
||||
}
|
||||
|
||||
if seconds <= 0 {
|
||||
return 0, fmt.Errorf("%s must be positive", name)
|
||||
}
|
||||
|
||||
duration, err := time.ParseDuration(raw + "s")
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("%s is too large: %w", name, err)
|
||||
}
|
||||
|
||||
return duration, nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestInstanceIDFromEnvironment(t *testing.T) {
|
||||
t.Setenv("INSTANCE_ID", "api-replica_01")
|
||||
|
||||
instanceID, err := instanceIDFromEnv()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if instanceID != "api-replica_01" {
|
||||
t.Fatalf("got %q, want api-replica_01", instanceID)
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceIDRejectsUnsafeHeaderValue(t *testing.T) {
|
||||
t.Setenv("INSTANCE_ID", "api-1\r\nX-Forged: true")
|
||||
|
||||
if _, err := instanceIDFromEnv(); err == nil {
|
||||
t.Fatal("expected unsafe instance ID to be rejected")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInstanceIDFallsBackToHostname(t *testing.T) {
|
||||
t.Setenv("INSTANCE_ID", "")
|
||||
|
||||
instanceID, err := instanceIDFromEnv()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if instanceID == "" {
|
||||
t.Fatal("hostname fallback returned an empty instance ID")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package config
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestBoolFromEnv(t *testing.T) {
|
||||
t.Run("fallback", func(t *testing.T) {
|
||||
t.Setenv("TEST_BOOL", "")
|
||||
value, err := boolFromEnv("TEST_BOOL", true)
|
||||
if err != nil || !value {
|
||||
t.Fatalf("boolFromEnv() = %v, %v; want true, nil", value, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("explicit false", func(t *testing.T) {
|
||||
t.Setenv("TEST_BOOL", "false")
|
||||
value, err := boolFromEnv("TEST_BOOL", true)
|
||||
if err != nil || value {
|
||||
t.Fatalf("boolFromEnv() = %v, %v; want false, nil", value, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("invalid", func(t *testing.T) {
|
||||
t.Setenv("TEST_BOOL", "sometimes")
|
||||
_, err := boolFromEnv("TEST_BOOL", false)
|
||||
if err == nil {
|
||||
t.Fatal("boolFromEnv() error = nil, want validation error")
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"mtusi2026lab4/internal/config"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func Open(
|
||||
ctx context.Context,
|
||||
cfg config.Config,
|
||||
) (*pgxpool.Pool, error) {
|
||||
poolConfig, err := pgxpool.ParseConfig(cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse PostgreSQL configuration: %w", err)
|
||||
}
|
||||
|
||||
poolConfig.MaxConns = cfg.DatabaseMaxConns
|
||||
poolConfig.MinConns = cfg.DatabaseMinConns
|
||||
poolConfig.MaxConnLifetime = cfg.DatabaseMaxConnLifetime
|
||||
poolConfig.MaxConnIdleTime = cfg.DatabaseMaxConnIdleTime
|
||||
poolConfig.HealthCheckPeriod = cfg.DatabaseHealthCheckPeriod
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create PostgreSQL pool: %w", err)
|
||||
}
|
||||
|
||||
// NewWithConfig создаёт структуру пула, но не гарантирует,
|
||||
// что PostgreSQL уже доступен. Ping обеспечивает fail-fast.
|
||||
if err := pool.Ping(ctx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping PostgreSQL: %w", err)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
const (
|
||||
MaxAttachmentFilenameCodePoints = 255
|
||||
MaxAttachmentSizeBytes = 10 * 1024 * 1024
|
||||
MaxAttachmentPixels = 40_000_000
|
||||
)
|
||||
|
||||
type AttachmentVariant struct {
|
||||
Name string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
Width int
|
||||
Height int
|
||||
SHA256 string
|
||||
}
|
||||
|
||||
type AttachmentStatus string
|
||||
|
||||
const (
|
||||
AttachmentStatusPending AttachmentStatus = "pending"
|
||||
AttachmentStatusReady AttachmentStatus = "ready"
|
||||
AttachmentStatusFailed AttachmentStatus = "failed"
|
||||
)
|
||||
|
||||
// Attachment contains metadata only. ObjectKey points to bytes in the
|
||||
// private object store and must never be derived from Filename.
|
||||
type Attachment struct {
|
||||
ID string
|
||||
ChatID string
|
||||
OwnerID string
|
||||
Filename string
|
||||
ObjectKey string
|
||||
ContentType string
|
||||
SizeBytes int64
|
||||
SHA256 string
|
||||
Width int
|
||||
Height int
|
||||
Status AttachmentStatus
|
||||
Variants []AttachmentVariant
|
||||
ErrorCode *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func ValidateAttachmentFilename(filename string) error {
|
||||
if !utf8.ValidString(filename) {
|
||||
return fmt.Errorf("filename must be valid UTF-8")
|
||||
}
|
||||
filenameLength := utf8.RuneCountInString(filename)
|
||||
if filenameLength < 1 || filenameLength > MaxAttachmentFilenameCodePoints {
|
||||
return fmt.Errorf("filename length must be between 1 and %d code points", MaxAttachmentFilenameCodePoints)
|
||||
}
|
||||
if strings.TrimSpace(filename) == "" || filename == "." || filename == ".." {
|
||||
return fmt.Errorf("filename is unsafe")
|
||||
}
|
||||
if strings.ContainsAny(filename, `/\\`) {
|
||||
return fmt.Errorf("filename must not contain path separators")
|
||||
}
|
||||
for _, character := range filename {
|
||||
if unicode.IsControl(character) {
|
||||
return fmt.Errorf("filename must not contain control characters")
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateAttachmentContentType(contentType string) error {
|
||||
if contentType != "image/png" && contentType != "image/jpeg" {
|
||||
return fmt.Errorf("content type must be image/png or image/jpeg")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateReadyAttachmentMetadata checks the bounded metadata produced after
|
||||
// the upload stream has been identified, decoded and hashed. It deliberately
|
||||
// does not trust an HTTP Content-Type header.
|
||||
func ValidateReadyAttachmentMetadata(
|
||||
filename string,
|
||||
contentType string,
|
||||
sizeBytes int64,
|
||||
sha256Hex string,
|
||||
width int,
|
||||
height int,
|
||||
) error {
|
||||
if err := ValidateAttachmentFilename(filename); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ValidateAttachmentContentType(contentType); err != nil {
|
||||
return err
|
||||
}
|
||||
if sizeBytes < 1 || sizeBytes > MaxAttachmentSizeBytes {
|
||||
return fmt.Errorf("attachment size must be between 1 and %d bytes", MaxAttachmentSizeBytes)
|
||||
}
|
||||
if width < 1 || height < 1 {
|
||||
return fmt.Errorf("image dimensions must be positive and contain at most %d pixels", MaxAttachmentPixels)
|
||||
}
|
||||
if int64(width) > int64(MaxAttachmentPixels)/int64(height) {
|
||||
return fmt.Errorf("image dimensions must be positive and contain at most %d pixels", MaxAttachmentPixels)
|
||||
}
|
||||
if len(sha256Hex) != 64 || sha256Hex != strings.ToLower(sha256Hex) {
|
||||
return fmt.Errorf("sha256 must contain 64 lowercase hexadecimal characters")
|
||||
}
|
||||
decodedHash, err := hex.DecodeString(sha256Hex)
|
||||
if err != nil || len(decodedHash) != 32 {
|
||||
return fmt.Errorf("sha256 must contain 64 lowercase hexadecimal characters")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"math"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateReadyAttachmentMetadata(t *testing.T) {
|
||||
validHash := strings.Repeat("ab", 32)
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
filename string
|
||||
contentType string
|
||||
sizeBytes int64
|
||||
sha256 string
|
||||
width int
|
||||
height int
|
||||
wantError bool
|
||||
}{
|
||||
{name: "png", filename: "кот.png", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1},
|
||||
{name: "jpeg at limits", filename: strings.Repeat("界", 255), contentType: "image/jpeg", sizeBytes: MaxAttachmentSizeBytes, sha256: validHash, width: 8_000, height: 5_000},
|
||||
{name: "empty filename", filename: "", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "blank filename", filename: " ", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "dot segment", filename: "..", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "forward slash", filename: "folder/image.png", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "backslash", filename: `folder\\image.png`, contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "control character", filename: "image\n.png", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "C1 control character", filename: "image\u0085.png", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "invalid UTF-8", filename: string([]byte{'x', 0xff, '.', 'p', 'n', 'g'}), contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "filename too long", filename: strings.Repeat("a", 256), contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "unsupported MIME", filename: "image.gif", contentType: "image/gif", sizeBytes: 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "empty file", filename: "image.png", contentType: "image/png", sizeBytes: 0, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "file too large", filename: "image.png", contentType: "image/png", sizeBytes: MaxAttachmentSizeBytes + 1, sha256: validHash, width: 1, height: 1, wantError: true},
|
||||
{name: "zero width", filename: "image.png", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 0, height: 1, wantError: true},
|
||||
{name: "too many pixels", filename: "image.png", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: 8_001, height: 5_000, wantError: true},
|
||||
{name: "dimension multiplication cannot overflow", filename: "image.png", contentType: "image/png", sizeBytes: 1, sha256: validHash, width: math.MaxInt, height: math.MaxInt, wantError: true},
|
||||
{name: "short hash", filename: "image.png", contentType: "image/png", sizeBytes: 1, sha256: "ab", width: 1, height: 1, wantError: true},
|
||||
{name: "uppercase hash", filename: "image.png", contentType: "image/png", sizeBytes: 1, sha256: strings.ToUpper(validHash), width: 1, height: 1, wantError: true},
|
||||
{name: "non-hex hash", filename: "image.png", contentType: "image/png", sizeBytes: 1, sha256: strings.Repeat("zz", 32), width: 1, height: 1, wantError: true},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
err := ValidateReadyAttachmentMetadata(
|
||||
testCase.filename,
|
||||
testCase.contentType,
|
||||
testCase.sizeBytes,
|
||||
testCase.sha256,
|
||||
testCase.width,
|
||||
testCase.height,
|
||||
)
|
||||
if (err != nil) != testCase.wantError {
|
||||
t.Fatalf("ValidateReadyAttachmentMetadata() error = %v, wantError %v", err, testCase.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Chat struct {
|
||||
ID string
|
||||
Title string
|
||||
MemberIDs []string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type ChatCursor struct {
|
||||
CreatedAt time.Time
|
||||
ChatID string
|
||||
UserID string
|
||||
}
|
||||
|
||||
type ChatPage struct {
|
||||
Items []Chat
|
||||
NextCursor *string
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package domain
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrConflict = errors.New("conflict")
|
||||
)
|
||||
@@ -0,0 +1,23 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Message struct {
|
||||
ID string
|
||||
ChatID string
|
||||
SenderID string
|
||||
Text string
|
||||
AttachmentIDs []string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type MessageCursor struct {
|
||||
CreatedAt time.Time
|
||||
MessageID string
|
||||
ChatID string
|
||||
}
|
||||
|
||||
type MessagePage struct {
|
||||
Items []Message
|
||||
NextCursor *string
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestValidateMessageInput(t *testing.T) {
|
||||
validAttachment := "11111111-1111-4111-8111-111111111111"
|
||||
|
||||
testCases := []struct {
|
||||
name string
|
||||
text string
|
||||
attachmentIDs []string
|
||||
wantError bool
|
||||
}{
|
||||
{name: "text only", text: "hello"},
|
||||
{name: "attachment only", attachmentIDs: []string{validAttachment}},
|
||||
{name: "whitespace with attachment", text: " ", attachmentIDs: []string{validAttachment}},
|
||||
{name: "empty", wantError: true},
|
||||
{name: "whitespace only", text: " ", wantError: true},
|
||||
{name: "text too long", text: strings.Repeat("界", 4001), wantError: true},
|
||||
{name: "invalid attachment uuid", text: "hello", attachmentIDs: []string{"not-a-uuid"}, wantError: true},
|
||||
{
|
||||
name: "duplicate attachment uuid ignores case",
|
||||
text: "hello",
|
||||
attachmentIDs: []string{
|
||||
"AAAAAAAA-AAAA-4AAA-8AAA-AAAAAAAAAAAA",
|
||||
"aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
|
||||
},
|
||||
wantError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
err := ValidateMessageInput(testCase.text, testCase.attachmentIDs)
|
||||
if (err != nil) != testCase.wantError {
|
||||
t.Fatalf("ValidateMessageInput() error = %v, wantError %v", err, testCase.wantError)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateMessageInputAttachmentLimit(t *testing.T) {
|
||||
attachmentIDs := []string{
|
||||
"00000000-0000-4000-8000-000000000001",
|
||||
"00000000-0000-4000-8000-000000000002",
|
||||
"00000000-0000-4000-8000-000000000003",
|
||||
"00000000-0000-4000-8000-000000000004",
|
||||
"00000000-0000-4000-8000-000000000005",
|
||||
"00000000-0000-4000-8000-000000000006",
|
||||
"00000000-0000-4000-8000-000000000007",
|
||||
"00000000-0000-4000-8000-000000000008",
|
||||
"00000000-0000-4000-8000-000000000009",
|
||||
"00000000-0000-4000-8000-000000000010",
|
||||
}
|
||||
|
||||
if err := ValidateMessageInput("", attachmentIDs); err != nil {
|
||||
t.Fatalf("10 attachments must be accepted: %v", err)
|
||||
}
|
||||
|
||||
attachmentIDs = append(
|
||||
attachmentIDs,
|
||||
"00000000-0000-4000-8000-000000000011",
|
||||
)
|
||||
if err := ValidateMessageInput("hello", attachmentIDs); err == nil {
|
||||
t.Fatal("11 attachments must be rejected")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
type Session struct {
|
||||
Token string
|
||||
TokenType string
|
||||
ExpiresAt time.Time
|
||||
User User
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package domain
|
||||
|
||||
import "time"
|
||||
|
||||
// User is the transport-independent representation of a messenger user.
|
||||
type User struct {
|
||||
ID string
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const MaxMessageAttachments = 10
|
||||
|
||||
var usernameRegex = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
func ValidateUser(username, displayName string) error {
|
||||
if !usernameRegex.MatchString(username) {
|
||||
return fmt.Errorf("username must match [a-z0-9_]{3,32}")
|
||||
}
|
||||
|
||||
nameRunes := utf8.RuneCountInString(displayName)
|
||||
if nameRunes < 1 || nameRunes > 100 {
|
||||
return fmt.Errorf("display_name length must be between 1 and 100 code points")
|
||||
}
|
||||
if strings.TrimSpace(displayName) == "" {
|
||||
return fmt.Errorf("display_name cannot be only whitespace")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Validate remains as a compatibility convenience for existing callers.
|
||||
func (u User) Validate() error {
|
||||
return ValidateUser(u.Username, u.DisplayName)
|
||||
}
|
||||
|
||||
func ValidateChatInput(title string, memberIDs []string, callerID string) error {
|
||||
titleRunes := utf8.RuneCountInString(title)
|
||||
if titleRunes < 1 || titleRunes > 100 || strings.TrimSpace(title) == "" {
|
||||
return fmt.Errorf("invalid chat title")
|
||||
}
|
||||
if len(memberIDs) == 0 {
|
||||
return fmt.Errorf("member_ids cannot be empty")
|
||||
}
|
||||
if len(memberIDs) > 100 {
|
||||
return fmt.Errorf("member_ids must contain at most 100 users")
|
||||
}
|
||||
|
||||
hasCaller := false
|
||||
seen := make(map[string]struct{}, len(memberIDs))
|
||||
for _, id := range memberIDs {
|
||||
if _, err := uuid.Parse(id); err != nil {
|
||||
return fmt.Errorf("invalid member uuid: %s", id)
|
||||
}
|
||||
if _, exists := seen[id]; exists {
|
||||
return fmt.Errorf("duplicate member_id: %s", id)
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
if id == callerID {
|
||||
hasCaller = true
|
||||
}
|
||||
}
|
||||
|
||||
if !hasCaller {
|
||||
return fmt.Errorf("caller must be in member_ids")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ValidateMessageInput(text string, attachmentIDs []string) error {
|
||||
textRunes := utf8.RuneCountInString(text)
|
||||
if textRunes > 4000 {
|
||||
return fmt.Errorf("message text length must be at most 4000 code points")
|
||||
}
|
||||
if len(attachmentIDs) > MaxMessageAttachments {
|
||||
return fmt.Errorf("attachment_ids must contain at most %d items", MaxMessageAttachments)
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{}, len(attachmentIDs))
|
||||
for _, attachmentID := range attachmentIDs {
|
||||
parsedID, err := uuid.Parse(attachmentID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("invalid attachment uuid: %s", attachmentID)
|
||||
}
|
||||
canonicalID := parsedID.String()
|
||||
if _, exists := seen[canonicalID]; exists {
|
||||
return fmt.Errorf("duplicate attachment_id: %s", attachmentID)
|
||||
}
|
||||
seen[canonicalID] = struct{}{}
|
||||
}
|
||||
|
||||
if strings.TrimSpace(text) == "" && len(attachmentIDs) == 0 {
|
||||
return fmt.Errorf("message must contain non-blank text or at least one attachment")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ValidateMessageText remains for callers that create text-only messages.
|
||||
func ValidateMessageText(text string) error {
|
||||
return ValidateMessageInput(text, nil)
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/adapter/objectstore"
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
const (
|
||||
IssueOrphanObject = "orphan_object"
|
||||
IssueMissingObject = "missing_object"
|
||||
IssueStalePending = "stale_pending"
|
||||
IssueFailedObject = "failed_object"
|
||||
IssueReferencedMissing = "referenced_missing_object"
|
||||
)
|
||||
|
||||
type AttachmentRecord struct {
|
||||
ID string
|
||||
ObjectKey string
|
||||
Status domain.AttachmentStatus
|
||||
UpdatedAt time.Time
|
||||
Referenced bool
|
||||
}
|
||||
|
||||
type MetadataStore interface {
|
||||
ListAttachmentInventory(ctx context.Context) ([]AttachmentRecord, error)
|
||||
MarkAttachmentFailedIfUnreferenced(
|
||||
ctx context.Context,
|
||||
attachmentID string,
|
||||
expectedStatus domain.AttachmentStatus,
|
||||
updatedBefore time.Time,
|
||||
errorCode string,
|
||||
updatedAt time.Time,
|
||||
) (bool, error)
|
||||
}
|
||||
|
||||
type ObjectInventory interface {
|
||||
List(ctx context.Context, prefix string) ([]objectstore.StoredObject, error)
|
||||
Delete(ctx context.Context, key string) error
|
||||
}
|
||||
|
||||
type Issue struct {
|
||||
Kind string `json:"kind"`
|
||||
AttachmentID string `json:"attachment_id,omitempty"`
|
||||
ObjectKey string `json:"object_key"`
|
||||
Protected bool `json:"protected,omitempty"`
|
||||
Action string `json:"action"`
|
||||
}
|
||||
|
||||
type Result struct {
|
||||
DryRun bool `json:"dry_run"`
|
||||
GracePeriod time.Duration `json:"-"`
|
||||
GracePeriodSeconds int64 `json:"grace_period_seconds"`
|
||||
ScannedAttachments int `json:"scanned_attachments"`
|
||||
ScannedObjects int `json:"scanned_objects"`
|
||||
Issues []Issue `json:"issues"`
|
||||
DeletedObjects int `json:"deleted_objects"`
|
||||
FailedAttachments int `json:"failed_attachments"`
|
||||
Duration time.Duration `json:"-"`
|
||||
DurationMillis int64 `json:"duration_ms"`
|
||||
}
|
||||
|
||||
type Reconciler struct {
|
||||
metadata MetadataStore
|
||||
objects ObjectInventory
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func New(metadata MetadataStore, objects ObjectInventory) (*Reconciler, error) {
|
||||
if metadata == nil || objects == nil {
|
||||
return nil, fmt.Errorf("metadata and object stores are required")
|
||||
}
|
||||
return &Reconciler{
|
||||
metadata: metadata,
|
||||
objects: objects,
|
||||
now: func() time.Time { return time.Now().UTC() },
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *Reconciler) Run(
|
||||
ctx context.Context,
|
||||
gracePeriod time.Duration,
|
||||
apply bool,
|
||||
) (Result, error) {
|
||||
if gracePeriod <= 0 {
|
||||
return Result{}, fmt.Errorf("grace period must be positive")
|
||||
}
|
||||
startedAt := r.now().UTC()
|
||||
cutoff := startedAt.Add(-gracePeriod)
|
||||
result := Result{
|
||||
DryRun: !apply,
|
||||
GracePeriod: gracePeriod,
|
||||
GracePeriodSeconds: int64(gracePeriod / time.Second),
|
||||
Issues: make([]Issue, 0),
|
||||
}
|
||||
|
||||
attachments, err := r.metadata.ListAttachmentInventory(ctx)
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("list attachment metadata: %w", err)
|
||||
}
|
||||
objects, err := r.objects.List(ctx, "attachments/")
|
||||
if err != nil {
|
||||
return Result{}, fmt.Errorf("list attachment objects: %w", err)
|
||||
}
|
||||
result.ScannedAttachments = len(attachments)
|
||||
result.ScannedObjects = len(objects)
|
||||
|
||||
attachmentsByKey := make(map[string]AttachmentRecord, len(attachments))
|
||||
for _, attachment := range attachments {
|
||||
attachmentsByKey[attachment.ObjectKey] = attachment
|
||||
}
|
||||
objectsByKey := make(map[string]objectstore.StoredObject, len(objects))
|
||||
for _, object := range objects {
|
||||
objectsByKey[object.Key] = object
|
||||
if _, exists := attachmentsByKey[object.Key]; exists {
|
||||
continue
|
||||
}
|
||||
if object.LastModified.IsZero() || object.LastModified.After(cutoff) {
|
||||
continue
|
||||
}
|
||||
issue := Issue{Kind: IssueOrphanObject, ObjectKey: object.Key, Action: "would_delete"}
|
||||
if apply {
|
||||
if err = r.objects.Delete(ctx, object.Key); err != nil {
|
||||
return result, fmt.Errorf("delete orphan object %q: %w", object.Key, err)
|
||||
}
|
||||
issue.Action = "deleted"
|
||||
result.DeletedObjects++
|
||||
}
|
||||
result.Issues = append(result.Issues, issue)
|
||||
}
|
||||
|
||||
for _, attachment := range attachments {
|
||||
if attachment.UpdatedAt.After(cutoff) {
|
||||
continue
|
||||
}
|
||||
_, objectExists := objectsByKey[attachment.ObjectKey]
|
||||
switch {
|
||||
case attachment.Status == domain.AttachmentStatusReady && !objectExists:
|
||||
issue := Issue{
|
||||
Kind: IssueMissingObject,
|
||||
AttachmentID: attachment.ID,
|
||||
ObjectKey: attachment.ObjectKey,
|
||||
Protected: attachment.Referenced,
|
||||
Action: "would_mark_failed",
|
||||
}
|
||||
if attachment.Referenced {
|
||||
issue.Kind = IssueReferencedMissing
|
||||
issue.Action = "manual_repair_required"
|
||||
} else if apply {
|
||||
changed, markError := r.metadata.MarkAttachmentFailedIfUnreferenced(
|
||||
ctx, attachment.ID, attachment.Status, cutoff,
|
||||
"reconciler_missing_object", startedAt,
|
||||
)
|
||||
if markError != nil {
|
||||
return result, fmt.Errorf("mark missing attachment %q failed: %w", attachment.ID, markError)
|
||||
}
|
||||
if changed {
|
||||
issue.Action = "marked_failed"
|
||||
result.FailedAttachments++
|
||||
} else {
|
||||
issue.Action = "skipped_concurrent_change"
|
||||
}
|
||||
}
|
||||
result.Issues = append(result.Issues, issue)
|
||||
|
||||
case attachment.Status == domain.AttachmentStatusPending:
|
||||
issue := Issue{
|
||||
Kind: IssueStalePending, AttachmentID: attachment.ID,
|
||||
ObjectKey: attachment.ObjectKey, Protected: attachment.Referenced,
|
||||
Action: "would_mark_failed",
|
||||
}
|
||||
if attachment.Referenced {
|
||||
issue.Action = "manual_repair_required"
|
||||
} else if apply {
|
||||
changed, markError := r.metadata.MarkAttachmentFailedIfUnreferenced(
|
||||
ctx, attachment.ID, attachment.Status, cutoff,
|
||||
"reconciler_stale_pending", startedAt,
|
||||
)
|
||||
if markError != nil {
|
||||
return result, fmt.Errorf("mark stale attachment %q failed: %w", attachment.ID, markError)
|
||||
}
|
||||
if changed {
|
||||
issue.Action = "marked_failed"
|
||||
result.FailedAttachments++
|
||||
if objectExists {
|
||||
if err = r.objects.Delete(ctx, attachment.ObjectKey); err != nil {
|
||||
return result, fmt.Errorf("delete stale pending object %q: %w", attachment.ObjectKey, err)
|
||||
}
|
||||
result.DeletedObjects++
|
||||
}
|
||||
} else {
|
||||
issue.Action = "skipped_concurrent_change"
|
||||
}
|
||||
}
|
||||
result.Issues = append(result.Issues, issue)
|
||||
|
||||
case attachment.Status == domain.AttachmentStatusFailed && objectExists:
|
||||
issue := Issue{
|
||||
Kind: IssueFailedObject, AttachmentID: attachment.ID,
|
||||
ObjectKey: attachment.ObjectKey, Protected: attachment.Referenced,
|
||||
Action: "would_delete",
|
||||
}
|
||||
if attachment.Referenced {
|
||||
issue.Action = "manual_repair_required"
|
||||
} else if apply {
|
||||
if err = r.objects.Delete(ctx, attachment.ObjectKey); err != nil {
|
||||
return result, fmt.Errorf("delete failed attachment object %q: %w", attachment.ObjectKey, err)
|
||||
}
|
||||
issue.Action = "deleted"
|
||||
result.DeletedObjects++
|
||||
}
|
||||
result.Issues = append(result.Issues, issue)
|
||||
}
|
||||
}
|
||||
|
||||
sort.Slice(result.Issues, func(i, j int) bool {
|
||||
if result.Issues[i].ObjectKey == result.Issues[j].ObjectKey {
|
||||
return result.Issues[i].Kind < result.Issues[j].Kind
|
||||
}
|
||||
return result.Issues[i].ObjectKey < result.Issues[j].ObjectKey
|
||||
})
|
||||
result.Duration = r.now().UTC().Sub(startedAt)
|
||||
result.DurationMillis = result.Duration.Milliseconds()
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/adapter/objectstore"
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type fakeMetadataStore struct {
|
||||
records []AttachmentRecord
|
||||
markError error
|
||||
skipMark bool
|
||||
}
|
||||
|
||||
func (store *fakeMetadataStore) ListAttachmentInventory(
|
||||
context.Context,
|
||||
) ([]AttachmentRecord, error) {
|
||||
return append([]AttachmentRecord(nil), store.records...), nil
|
||||
}
|
||||
|
||||
func (store *fakeMetadataStore) MarkAttachmentFailedIfUnreferenced(
|
||||
_ context.Context,
|
||||
attachmentID string,
|
||||
expectedStatus domain.AttachmentStatus,
|
||||
updatedBefore time.Time,
|
||||
errorCode string,
|
||||
updatedAt time.Time,
|
||||
) (bool, error) {
|
||||
if store.markError != nil {
|
||||
return false, store.markError
|
||||
}
|
||||
if store.skipMark {
|
||||
return false, nil
|
||||
}
|
||||
for index := range store.records {
|
||||
record := &store.records[index]
|
||||
if record.ID != attachmentID || record.Status != expectedStatus ||
|
||||
record.UpdatedAt.After(updatedBefore) || record.Referenced {
|
||||
continue
|
||||
}
|
||||
record.Status = domain.AttachmentStatusFailed
|
||||
record.UpdatedAt = updatedAt
|
||||
_ = errorCode
|
||||
return true, nil
|
||||
}
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func TestReconcilerDoesNotDeleteAfterConcurrentMetadataChange(t *testing.T) {
|
||||
old := time.Now().UTC().Add(-2 * time.Hour)
|
||||
metadata := &fakeMetadataStore{
|
||||
records: []AttachmentRecord{{
|
||||
ID: "pending-race", ObjectKey: "attachments/pending-race",
|
||||
Status: domain.AttachmentStatusPending, UpdatedAt: old,
|
||||
}},
|
||||
skipMark: true,
|
||||
}
|
||||
objects := &fakeObjectInventory{objects: map[string]objectstore.StoredObject{
|
||||
"attachments/pending-race": {Key: "attachments/pending-race", LastModified: old},
|
||||
}}
|
||||
reconciler, err := New(metadata, objects)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
result, err := reconciler.Run(context.Background(), time.Hour, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if result.DeletedObjects != 0 || result.FailedAttachments != 0 {
|
||||
t.Fatalf("concurrent metadata change must suppress deletion: %+v", result)
|
||||
}
|
||||
if _, exists := objects.objects["attachments/pending-race"]; !exists {
|
||||
t.Fatal("reconciler deleted an object after losing the metadata race")
|
||||
}
|
||||
if len(result.Issues) != 1 || result.Issues[0].Action != "skipped_concurrent_change" {
|
||||
t.Fatalf("unexpected race result: %+v", result.Issues)
|
||||
}
|
||||
}
|
||||
|
||||
type fakeObjectInventory struct {
|
||||
objects map[string]objectstore.StoredObject
|
||||
deleteError error
|
||||
}
|
||||
|
||||
func (store *fakeObjectInventory) List(
|
||||
context.Context,
|
||||
string,
|
||||
) ([]objectstore.StoredObject, error) {
|
||||
objects := make([]objectstore.StoredObject, 0, len(store.objects))
|
||||
for _, object := range store.objects {
|
||||
objects = append(objects, object)
|
||||
}
|
||||
return objects, nil
|
||||
}
|
||||
|
||||
func (store *fakeObjectInventory) Delete(_ context.Context, key string) error {
|
||||
if store.deleteError != nil {
|
||||
return store.deleteError
|
||||
}
|
||||
delete(store.objects, key)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestReconcilerDryRunApplyAndRepeat(t *testing.T) {
|
||||
now := time.Now().UTC()
|
||||
old := now.Add(-2 * time.Hour)
|
||||
fresh := now.Add(-5 * time.Minute)
|
||||
metadata := &fakeMetadataStore{records: []AttachmentRecord{
|
||||
{ID: "ready-present", ObjectKey: "attachments/ready-present", Status: domain.AttachmentStatusReady, UpdatedAt: old},
|
||||
{ID: "ready-missing", ObjectKey: "attachments/ready-missing", Status: domain.AttachmentStatusReady, UpdatedAt: old},
|
||||
{ID: "ready-used-missing", ObjectKey: "attachments/ready-used-missing", Status: domain.AttachmentStatusReady, UpdatedAt: old, Referenced: true},
|
||||
{ID: "pending-old", ObjectKey: "attachments/pending-old", Status: domain.AttachmentStatusPending, UpdatedAt: old},
|
||||
{ID: "failed-old", ObjectKey: "attachments/failed-old", Status: domain.AttachmentStatusFailed, UpdatedAt: old},
|
||||
{ID: "pending-fresh", ObjectKey: "attachments/pending-fresh", Status: domain.AttachmentStatusPending, UpdatedAt: fresh},
|
||||
}}
|
||||
objects := &fakeObjectInventory{objects: map[string]objectstore.StoredObject{
|
||||
"attachments/ready-present": {Key: "attachments/ready-present", LastModified: old},
|
||||
"attachments/pending-old": {Key: "attachments/pending-old", LastModified: old},
|
||||
"attachments/failed-old": {Key: "attachments/failed-old", LastModified: old},
|
||||
"attachments/orphan-old": {Key: "attachments/orphan-old", LastModified: old},
|
||||
"attachments/orphan-fresh": {Key: "attachments/orphan-fresh", LastModified: fresh},
|
||||
}}
|
||||
reconciler, err := New(metadata, objects)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
dryRun, err := reconciler.Run(context.Background(), time.Hour, false)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !dryRun.DryRun || len(dryRun.Issues) != 5 ||
|
||||
dryRun.DeletedObjects != 0 || dryRun.FailedAttachments != 0 {
|
||||
t.Fatalf("unexpected dry-run result: %+v", dryRun)
|
||||
}
|
||||
if len(objects.objects) != 5 {
|
||||
t.Fatal("dry-run changed object storage")
|
||||
}
|
||||
for _, record := range metadata.records {
|
||||
if record.ID == "ready-missing" && record.Status != domain.AttachmentStatusReady {
|
||||
t.Fatal("dry-run changed metadata")
|
||||
}
|
||||
}
|
||||
|
||||
applied, err := reconciler.Run(context.Background(), time.Hour, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if applied.DeletedObjects != 3 || applied.FailedAttachments != 2 {
|
||||
t.Fatalf("unexpected apply result: %+v", applied)
|
||||
}
|
||||
if _, exists := objects.objects["attachments/orphan-old"]; exists {
|
||||
t.Fatal("old orphan was not deleted")
|
||||
}
|
||||
if _, exists := objects.objects["attachments/orphan-fresh"]; !exists {
|
||||
t.Fatal("fresh orphan must be protected by grace period")
|
||||
}
|
||||
if _, exists := objects.objects["attachments/ready-present"]; !exists {
|
||||
t.Fatal("healthy object was deleted")
|
||||
}
|
||||
for _, issue := range applied.Issues {
|
||||
if issue.AttachmentID == "ready-used-missing" &&
|
||||
(!issue.Protected || issue.Action != "manual_repair_required") {
|
||||
t.Fatalf("referenced attachment was not protected: %+v", issue)
|
||||
}
|
||||
}
|
||||
|
||||
repeated, err := reconciler.Run(context.Background(), time.Hour, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if repeated.DeletedObjects != 0 || repeated.FailedAttachments != 0 {
|
||||
t.Fatalf("repeat must be mutation-idempotent: %+v", repeated)
|
||||
}
|
||||
if len(repeated.Issues) != 1 || !repeated.Issues[0].Protected {
|
||||
t.Fatalf("repeat should retain only protected manual issue: %+v", repeated)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReconcilerReportsMutationFailures(t *testing.T) {
|
||||
old := time.Now().UTC().Add(-2 * time.Hour)
|
||||
metadata := &fakeMetadataStore{records: []AttachmentRecord{
|
||||
{ID: "missing", ObjectKey: "attachments/missing", Status: domain.AttachmentStatusReady, UpdatedAt: old},
|
||||
}}
|
||||
metadata.markError = errors.New("injected database failure")
|
||||
objects := &fakeObjectInventory{objects: make(map[string]objectstore.StoredObject)}
|
||||
reconciler, err := New(metadata, objects)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = reconciler.Run(context.Background(), time.Hour, true); err == nil {
|
||||
t.Fatal("injected database mutation failure was hidden")
|
||||
}
|
||||
|
||||
metadata.records = nil
|
||||
metadata.markError = nil
|
||||
objects.objects["attachments/orphan"] = objectstore.StoredObject{
|
||||
Key: "attachments/orphan", LastModified: old,
|
||||
}
|
||||
objects.deleteError = errors.New("injected object-store failure")
|
||||
if _, err = reconciler.Run(context.Background(), time.Hour, true); err == nil {
|
||||
t.Fatal("injected object-store mutation failure was hidden")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const (
|
||||
argonMemory uint32 = 19 * 1024
|
||||
argonIterations uint32 = 2
|
||||
argonParallelism uint8 = 1
|
||||
argonSaltLength = 16
|
||||
argonKeyLength uint32 = 32
|
||||
|
||||
minPasswordLength = 12
|
||||
maxPasswordLength = 128
|
||||
)
|
||||
|
||||
var ErrInvalidPasswordHash = errors.New("invalid password hash")
|
||||
|
||||
func ValidatePassword(password string) error {
|
||||
if !utf8.ValidString(password) {
|
||||
return errors.New("password must be valid UTF-8")
|
||||
}
|
||||
|
||||
length := utf8.RuneCountInString(password)
|
||||
if length < minPasswordLength || length > maxPasswordLength {
|
||||
return fmt.Errorf(
|
||||
"password length must be between %d and %d code points",
|
||||
minPasswordLength,
|
||||
maxPasswordLength,
|
||||
)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func HashPassword(password string) (string, error) {
|
||||
if err := ValidatePassword(password); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
salt := make([]byte, argonSaltLength)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return "", fmt.Errorf("generate password salt: %w", err)
|
||||
}
|
||||
|
||||
hash := argon2.IDKey(
|
||||
[]byte(password),
|
||||
salt,
|
||||
argonIterations,
|
||||
argonMemory,
|
||||
argonParallelism,
|
||||
argonKeyLength,
|
||||
)
|
||||
|
||||
encodedSalt := base64.RawStdEncoding.EncodeToString(salt)
|
||||
encodedHash := base64.RawStdEncoding.EncodeToString(hash)
|
||||
|
||||
encoded := fmt.Sprintf(
|
||||
"$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version,
|
||||
argonMemory,
|
||||
argonIterations,
|
||||
argonParallelism,
|
||||
encodedSalt,
|
||||
encodedHash,
|
||||
)
|
||||
|
||||
return encoded, nil
|
||||
}
|
||||
|
||||
func VerifyPassword(
|
||||
password string,
|
||||
encodedHash string,
|
||||
) (bool, error) {
|
||||
parameters, salt, expectedHash, err := decodePasswordHash(encodedHash)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
actualHash := argon2.IDKey(
|
||||
[]byte(password),
|
||||
salt,
|
||||
parameters.iterations,
|
||||
parameters.memory,
|
||||
parameters.parallelism,
|
||||
uint32(len(expectedHash)),
|
||||
)
|
||||
|
||||
matches := subtle.ConstantTimeCompare(
|
||||
actualHash,
|
||||
expectedHash,
|
||||
) == 1
|
||||
|
||||
return matches, nil
|
||||
}
|
||||
|
||||
type argonParameters struct {
|
||||
memory uint32
|
||||
iterations uint32
|
||||
parallelism uint8
|
||||
}
|
||||
|
||||
func decodePasswordHash(
|
||||
encodedHash string,
|
||||
) (argonParameters, []byte, []byte, error) {
|
||||
parts := strings.Split(encodedHash, "$")
|
||||
if len(parts) != 6 ||
|
||||
parts[0] != "" ||
|
||||
parts[1] != "argon2id" {
|
||||
return argonParameters{}, nil, nil, ErrInvalidPasswordHash
|
||||
}
|
||||
|
||||
var version int
|
||||
if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil {
|
||||
return argonParameters{}, nil, nil, ErrInvalidPasswordHash
|
||||
}
|
||||
|
||||
if version != argon2.Version {
|
||||
return argonParameters{}, nil, nil, ErrInvalidPasswordHash
|
||||
}
|
||||
|
||||
var parameters argonParameters
|
||||
if _, err := fmt.Sscanf(
|
||||
parts[3],
|
||||
"m=%d,t=%d,p=%d",
|
||||
¶meters.memory,
|
||||
¶meters.iterations,
|
||||
¶meters.parallelism,
|
||||
); err != nil {
|
||||
return argonParameters{}, nil, nil, ErrInvalidPasswordHash
|
||||
}
|
||||
|
||||
// Ограничения не позволяют повреждённой строке из БД запросить
|
||||
// гигантский объём памяти или процессорного времени.
|
||||
if parameters.memory < 8*1024 ||
|
||||
parameters.memory > 256*1024 ||
|
||||
parameters.iterations < 1 ||
|
||||
parameters.iterations > 10 ||
|
||||
parameters.parallelism < 1 ||
|
||||
parameters.parallelism > 8 {
|
||||
return argonParameters{}, nil, nil, ErrInvalidPasswordHash
|
||||
}
|
||||
|
||||
salt, err := base64.RawStdEncoding.DecodeString(parts[4])
|
||||
if err != nil || len(salt) < 16 || len(salt) > 64 {
|
||||
return argonParameters{}, nil, nil, ErrInvalidPasswordHash
|
||||
}
|
||||
|
||||
hash, err := base64.RawStdEncoding.DecodeString(parts[5])
|
||||
if err != nil || len(hash) < 16 || len(hash) > 64 {
|
||||
return argonParameters{}, nil, nil, ErrInvalidPasswordHash
|
||||
}
|
||||
|
||||
return parameters, salt, hash, nil
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPasswordHashAndVerify(t *testing.T) {
|
||||
password := "Correct-password-123"
|
||||
|
||||
encodedHash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("HashPassword returned error: %v", err)
|
||||
}
|
||||
|
||||
if strings.Contains(encodedHash, password) {
|
||||
t.Fatal("encoded hash contains plaintext password")
|
||||
}
|
||||
|
||||
matches, err := VerifyPassword(password, encodedHash)
|
||||
if err != nil {
|
||||
t.Fatalf("VerifyPassword returned error: %v", err)
|
||||
}
|
||||
if !matches {
|
||||
t.Fatal("correct password did not match")
|
||||
}
|
||||
|
||||
matches, err = VerifyPassword("Wrong-password-123", encodedHash)
|
||||
if err != nil {
|
||||
t.Fatalf("VerifyPassword returned error: %v", err)
|
||||
}
|
||||
if matches {
|
||||
t.Fatal("wrong password matched")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordHashesUseDifferentSalts(t *testing.T) {
|
||||
password := "Correct-password-123"
|
||||
|
||||
firstHash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("first HashPassword returned error: %v", err)
|
||||
}
|
||||
|
||||
secondHash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatalf("second HashPassword returned error: %v", err)
|
||||
}
|
||||
|
||||
if firstHash == secondHash {
|
||||
t.Fatal("equal passwords produced equal encoded hashes")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPasswordValidation(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
password string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "minimum length",
|
||||
password: "123456789012",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "unicode",
|
||||
password: "пароль-проверка-123",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "colon is allowed",
|
||||
password: "password:with:colon",
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "too short",
|
||||
password: "short",
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, testCase := range testCases {
|
||||
t.Run(testCase.name, func(t *testing.T) {
|
||||
err := ValidatePassword(testCase.password)
|
||||
|
||||
if testCase.wantErr && err == nil {
|
||||
t.Fatal("expected validation error")
|
||||
}
|
||||
if !testCase.wantErr && err != nil {
|
||||
t.Fatalf("unexpected validation error: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMalformedPasswordHash(t *testing.T) {
|
||||
_, err := VerifyPassword(
|
||||
"Correct-password-123",
|
||||
"not-an-argon2-hash",
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatal("expected malformed hash error")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const sessionTokenBytes = 32
|
||||
|
||||
func GenerateSessionToken() (string, []byte, error) {
|
||||
randomBytes := make([]byte, sessionTokenBytes)
|
||||
|
||||
if _, err := rand.Read(randomBytes); err != nil {
|
||||
return "", nil, fmt.Errorf("generate session token: %w", err)
|
||||
}
|
||||
|
||||
token := base64.RawURLEncoding.EncodeToString(randomBytes)
|
||||
tokenHash := HashSessionToken(token)
|
||||
|
||||
return token, tokenHash, nil
|
||||
}
|
||||
|
||||
func HashSessionToken(token string) []byte {
|
||||
digest := sha256.Sum256([]byte(token))
|
||||
|
||||
hash := make([]byte, len(digest))
|
||||
copy(hash, digest[:])
|
||||
|
||||
return hash
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package security
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestGenerateSessionToken(t *testing.T) {
|
||||
firstToken, firstHash, err := GenerateSessionToken()
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateSessionToken returned error: %v", err)
|
||||
}
|
||||
|
||||
secondToken, secondHash, err := GenerateSessionToken()
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateSessionToken returned error: %v", err)
|
||||
}
|
||||
|
||||
if firstToken == secondToken {
|
||||
t.Fatal("generated session tokens are equal")
|
||||
}
|
||||
|
||||
if bytes.Equal(firstHash, secondHash) {
|
||||
t.Fatal("generated session token hashes are equal")
|
||||
}
|
||||
|
||||
if len(firstHash) != 32 {
|
||||
t.Fatalf("expected 32-byte token hash, got %d", len(firstHash))
|
||||
}
|
||||
|
||||
recalculatedHash := HashSessionToken(firstToken)
|
||||
if !bytes.Equal(firstHash, recalculatedHash) {
|
||||
t.Fatal("token hash is not reproducible")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type AttachmentVariantResponse struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type AttachmentResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
OwnerID string `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
Status domain.AttachmentStatus `json:"status"`
|
||||
Variants []AttachmentVariantResponse `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func AttachmentFromDomain(attachment domain.Attachment) AttachmentResponse {
|
||||
variants := make([]AttachmentVariantResponse, 0, len(attachment.Variants))
|
||||
for _, variant := range attachment.Variants {
|
||||
variants = append(variants, AttachmentVariantResponse{
|
||||
Name: variant.Name,
|
||||
ContentType: variant.ContentType,
|
||||
SizeBytes: variant.SizeBytes,
|
||||
Width: variant.Width,
|
||||
Height: variant.Height,
|
||||
SHA256: variant.SHA256,
|
||||
})
|
||||
}
|
||||
return AttachmentResponse{
|
||||
ID: attachment.ID,
|
||||
ChatID: attachment.ChatID,
|
||||
OwnerID: attachment.OwnerID,
|
||||
Filename: attachment.Filename,
|
||||
ContentType: attachment.ContentType,
|
||||
SizeBytes: attachment.SizeBytes,
|
||||
SHA256: attachment.SHA256,
|
||||
Status: attachment.Status,
|
||||
Variants: variants,
|
||||
ErrorCode: attachment.ErrorCode,
|
||||
CreatedAt: attachment.CreatedAt,
|
||||
UpdatedAt: attachment.UpdatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type CreateChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
|
||||
type ChatResponse struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type ChatPageResponse struct {
|
||||
Items []ChatResponse `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
func ChatFromDomain(chat domain.Chat) ChatResponse {
|
||||
return ChatResponse{
|
||||
ID: chat.ID, Title: chat.Title, MemberIDs: chat.MemberIDs, CreatedAt: chat.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func ChatPageFromDomain(page domain.ChatPage) ChatPageResponse {
|
||||
items := make([]ChatResponse, len(page.Items))
|
||||
for index, chat := range page.Items {
|
||||
items[index] = ChatFromDomain(chat)
|
||||
}
|
||||
return ChatPageResponse{Items: items, NextCursor: page.NextCursor}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type CreateMessageRequest struct {
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
ID string `json:"id"`
|
||||
ChatID string `json:"chat_id"`
|
||||
SenderID string `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type MessagePageResponse struct {
|
||||
Items []MessageResponse `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
func MessageFromDomain(message domain.Message) MessageResponse {
|
||||
attachmentIDs := append([]string(nil), message.AttachmentIDs...)
|
||||
if attachmentIDs == nil {
|
||||
attachmentIDs = make([]string, 0)
|
||||
}
|
||||
|
||||
return MessageResponse{
|
||||
ID: message.ID, ChatID: message.ChatID, SenderID: message.SenderID,
|
||||
Text: message.Text, AttachmentIDs: attachmentIDs, CreatedAt: message.CreatedAt,
|
||||
}
|
||||
}
|
||||
|
||||
func MessagePageFromDomain(page domain.MessagePage) MessagePageResponse {
|
||||
items := make([]MessageResponse, len(page.Items))
|
||||
for index, message := range page.Items {
|
||||
items[index] = MessageFromDomain(message)
|
||||
}
|
||||
return MessagePageResponse{Items: items, NextCursor: page.NextCursor}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type SessionResponse struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
User UserResponse `json:"user"`
|
||||
}
|
||||
|
||||
func SessionFromDomain(session domain.Session) SessionResponse {
|
||||
return SessionResponse{
|
||||
Token: session.Token, TokenType: session.TokenType, ExpiresAt: session.ExpiresAt,
|
||||
User: UserFromDomain(session.User),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"mtusi2026lab4/internal/domain"
|
||||
)
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type UserResponse struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func UserFromDomain(user domain.User) UserResponse {
|
||||
return UserResponse{
|
||||
ID: user.ID, Username: user.Username, DisplayName: user.DisplayName, CreatedAt: user.CreatedAt,
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user