forked from mtusi2026/mtusi2026lab4
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d862103650 |
+27
-16
@@ -1,20 +1,31 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
# scripts/bootstrap.sh creates ignored .env.lab4 with random synthetic credentials.
|
||||
# This example documents actual Compose inputs; it contains no live secrets.
|
||||
POSTGRES_PASSWORD=replace-with-local-postgres-password
|
||||
REDIS_PASSWORD=replace-with-local-redis-password
|
||||
HOST_PORT=18081
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
S3_REGION=us-east-1
|
||||
API_CPUS=1
|
||||
API_MEMORY=512m
|
||||
POSTGRES_MAX_CONNECTIONS=8
|
||||
APPLICATION_LOG_LEVEL=warn
|
||||
SESSION_TTL_SECONDS=3600
|
||||
API_IMAGE=mtusi-messenger:lab4
|
||||
# Session TTL defaults to 3600; use >=300 for smoke/persistence.
|
||||
# Message idempotency retention is fixed at 24 hours; API never runs migrations.
|
||||
|
||||
# S3 credentials are generated locally by scripts/bootstrap-storage.py.
|
||||
# For a custom bucket, also export S3_BUCKET before make up: the credential
|
||||
# bootstrap reads the process environment, while Compose reads .env.lab4.
|
||||
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
|
||||
# Runtime values fixed in compose.yaml (not configurable through this file):
|
||||
# S3_ENDPOINT=http://s3:9000
|
||||
# S3_REGION=us-east-1
|
||||
# S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access
|
||||
# S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
# Bounded attachment request lifetime; grace must exceed twice this duration.
|
||||
UPLOAD_TIMEOUT=120s
|
||||
RECONCILE_GRACE_PERIOD=1h
|
||||
# SeaweedFS image and persistent bucket bootstrap are pinned in compose.yaml.
|
||||
# API has four upload slots and one decoder per replica, GOMEMLIMIT=384MiB.
|
||||
|
||||
@@ -13,4 +13,22 @@ jobs:
|
||||
- run: python -m pip install -r requirements-dev.txt
|
||||
- run: make check validate PYTHON=python
|
||||
# This job validates the skeleton, not a student solution.
|
||||
# Add build, readiness and make test in a separate application job.
|
||||
# The application job below builds and tests the implemented API separately.
|
||||
|
||||
application:
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: service/go.mod
|
||||
cache-dependency-path: service/go.sum
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.12'
|
||||
- run: make check build vet
|
||||
- run: make up
|
||||
- run: make test test-go test-storage
|
||||
- if: always()
|
||||
run: make down
|
||||
|
||||
@@ -30,3 +30,4 @@ coverage/
|
||||
.vscode/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
/.ignore/
|
||||
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
# syntax=docker/dockerfile:1
|
||||
|
||||
FROM --platform=$BUILDPLATFORM golang:1.27.2-alpine3.23@sha256:9e45f0eb4a63ed37ad6e604950407558b7c738e34e015ae77a5d4ad369cde079 AS build
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY service/go.mod service/go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY service/ ./
|
||||
|
||||
ARG TARGETOS
|
||||
ARG TARGETARCH
|
||||
RUN CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH \
|
||||
go build -mod=readonly -trimpath -ldflags="-s -w" -o /out/messenger ./cmd/app
|
||||
|
||||
FROM alpine:3.23.6@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0
|
||||
|
||||
COPY --from=build --chmod=0555 /out/messenger /usr/local/bin/messenger
|
||||
|
||||
ENV APP_HOST=0.0.0.0 \
|
||||
APP_PORT=8080 \
|
||||
APP_SHUTDOWN_TIMEOUT=8s
|
||||
|
||||
USER 65532:65532
|
||||
EXPOSE 8080
|
||||
STOPSIGNAL SIGTERM
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD wget -q -O /dev/null "http://127.0.0.1:${APP_PORT}/health/ready" || exit 1
|
||||
|
||||
ENTRYPOINT ["/usr/local/bin/messenger"]
|
||||
@@ -1,13 +1,24 @@
|
||||
SHELL := /bin/bash
|
||||
GO ?= go
|
||||
PYTHON ?= python3
|
||||
BASE_URL ?= http://localhost:8080
|
||||
BASE_URL ?= http://localhost:18081
|
||||
CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
OPENAPI_SPEC := contracts/openapi.json
|
||||
GEN_OUTPUT := service/internal/gen/contracts/types.gen.go
|
||||
GEN_PACKAGE := contracts
|
||||
OAPI_CODEGEN ?= $(shell $(GO) env GOPATH)/bin/oapi-codegen
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
.PHONY: help check validate test load test-persistence test-failover openapi-generate certs key-gen create-dir gen-private-key gen-public-key gen-key gen-crt
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@echo "up/down: lab4 stand (S3 attachments); build/vet: Go checks"
|
||||
@echo "test: lab4 API smoke; test-go/test-storage: Go API/storage checks"
|
||||
@echo "restart/restore/rolling-update: infrastructure actions; benchmark/load: synthetic traffic"
|
||||
@echo "check/validate: contracts; test-persistence/test-failover: explicit ACTION_SCRIPT hooks"
|
||||
@echo "evidence: isolated full lab4 acceptance; reconcile/reconcile-apply: storage consistency"
|
||||
@echo "openapi-generate: regenerate current lab4 types; key-gen/certs: local keys"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -28,3 +39,78 @@ 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)
|
||||
|
||||
|
||||
openapi-generate:
|
||||
@mkdir -p $(dir $(GEN_OUTPUT))
|
||||
@echo "Generating Go types from $(OPENAPI_SPEC)..."
|
||||
@set -o pipefail; \
|
||||
$(PYTHON) scripts/generate-types.py | \
|
||||
$(OAPI_CODEGEN) -generate types -package $(GEN_PACKAGE) -o $(GEN_OUTPUT) /dev/stdin
|
||||
|
||||
key-gen: gen-public-key
|
||||
|
||||
create-dir:
|
||||
@mkdir -p certs
|
||||
gen-private-key: | create-dir
|
||||
@openssl genrsa -out certs/private_key.pem 2048
|
||||
gen-public-key: gen-private-key
|
||||
@openssl rsa -in certs/private_key.pem -pubout -outform PEM -out certs/public_key.pem
|
||||
|
||||
certs: gen-crt
|
||||
|
||||
gen-key: | create-dir
|
||||
@openssl genrsa -out ./certs/server.key 2048
|
||||
gen-crt: gen-key
|
||||
@openssl req -new -x509 -key ./certs/server.key -out ./certs/server.crt -days 365
|
||||
|
||||
|
||||
.PHONY: up down test-go
|
||||
up:
|
||||
./scripts/bootstrap.sh
|
||||
./scripts/compose.sh up -d --build --wait --wait-timeout 120
|
||||
|
||||
down:
|
||||
./scripts/compose.sh down
|
||||
|
||||
test-go:
|
||||
cd service && TEST_BASE_URL="$(BASE_URL)" $(GO) test -race -count=1 -v ./...
|
||||
|
||||
RATE ?= 100
|
||||
DURATION ?= 30s
|
||||
BENCH_ARGS ?=
|
||||
|
||||
.PHONY: build vet restart restore rolling-update benchmark test-storage
|
||||
build:
|
||||
cd service && $(GO) build ./...
|
||||
@mkdir -p bin
|
||||
cd service && $(GO) build -o ../bin/benchmark ./cmd/benchmark
|
||||
|
||||
vet:
|
||||
cd service && $(GO) vet ./...
|
||||
|
||||
restart:
|
||||
./scripts/restart.sh
|
||||
|
||||
restore:
|
||||
./scripts/compose.sh up -d --wait --wait-timeout 90 --no-recreate api lb
|
||||
|
||||
rolling-update:
|
||||
./scripts/rolling-update.sh
|
||||
|
||||
benchmark: build
|
||||
./bin/benchmark -base-url "$(BASE_URL)" -rate "$(RATE)" -duration "$(DURATION)" $(BENCH_ARGS)
|
||||
|
||||
test-storage:
|
||||
./scripts/test-storage.sh
|
||||
|
||||
.PHONY: evidence reconcile reconcile-apply
|
||||
|
||||
evidence:
|
||||
$(PYTHON) scripts/acceptance.py
|
||||
|
||||
reconcile:
|
||||
./scripts/compose.sh run --rm -T operator storage reconcile
|
||||
|
||||
reconcile-apply:
|
||||
./scripts/compose.sh run --rm -T operator storage reconcile --apply
|
||||
|
||||
@@ -2,13 +2,13 @@
|
||||
|
||||
Разделить метаданные и бинарное содержимое, добавить изображения в сообщения и сохранить доступ к ним при переключении API-реплики.
|
||||
|
||||
Это **skeleton**, а не готовый сервер. Здесь есть задание, контракт и публичные проверки. Реализацию приложения, Dockerfile и требуемую инфраструктуру пишет студент. Язык и framework свободные; UI не обязателен.
|
||||
Реализация на Go включает основу lab3, синхронные PNG/JPEG-вложения в приватном SeaweedFS, две API-реплики и HAProxy. Реализован трек 2 на 5: сверка orphan/missing, безопасная очистка и восстановление из оригинала. Команды и фактические доказательства собраны в [REPORT.md](REPORT.md); обязательный контракт и публичные проверки сохранены. UI не обязателен.
|
||||
|
||||
[Карта курса](COURSE.md) · [Процесс fork/PR](CONTRIBUTING.md) · [OpenAPI](contracts/openapi.json) · [Семантика API](contracts/README.md) · [Проверки](tests/README.md) · [Отчёт](REPORT.md) · [Примеры JSON](contracts/examples.json) · [Памятка преподавателю](MAINTAINERS.md)
|
||||
|
||||
## Откуда начинаем
|
||||
|
||||
Перенесите минимум lab3. Дополняются поля сообщения и endpoint вложений; прежние текстовые POST остаются валидными.
|
||||
Минимум lab3 перенесён в `service/` и `compose.yaml`. Поля сообщений дополнены ссылками на вложения; прежние текстовые POST остаются валидными.
|
||||
|
||||
**Архитектура:** Клиент → LB → API × 2+ → PostgreSQL (метаданные) + приватное S3-хранилище (байты).
|
||||
|
||||
@@ -46,25 +46,63 @@
|
||||
|
||||
## Как начать и проверить
|
||||
|
||||
```sh
|
||||
# Работает прямо в skeleton, Python 3.9+; приложение не запускается:
|
||||
make check
|
||||
Для локального запуска нужны Docker Compose, OpenSSL и Python 3.9+; для сборки и проверок Go на хосте — Go 1.27+. Команда `make up` создаёт `.env.lab4` со случайными локальными паролями и JWT/S3-ключи в `secrets/lab4`, сохраняя существующие файлы. Эти файлы игнорируются Git. `.env.example` описывает настройки стенда; для изменения порта или числа реплик редактируйте `.env.lab4`.
|
||||
|
||||
# После реализации запустите свой сервер/стенд по REPORT.md.
|
||||
# По умолчанию тест использует http://localhost:8080.
|
||||
```sh
|
||||
make check # Проверка структуры и контракта без запуска API
|
||||
make up # Bootstrap, сборка, миграции и запуск с ожиданием readiness
|
||||
curl --fail http://localhost:18081/health/ready
|
||||
make build vet # Проверки Go на хосте
|
||||
make test-go # Проверки API и вложений с race detector
|
||||
make test-storage # Проверка транзакций в отдельной временной БД
|
||||
make down # Остановка стенда с сохранением volumes
|
||||
```
|
||||
|
||||
Стенд использует Compose-проект `mtusi2026lab4`, образ `mtusi-messenger:lab4` и порт `127.0.0.1:18081`; сети и volumes отделены от lab3. При изменении порта передавайте проверкам `BASE_URL=http://localhost:<порт>`.
|
||||
|
||||
```sh
|
||||
./scripts/compose.sh config --quiet
|
||||
make restart # Перезапуск API/LB, PostgreSQL, Redis и S3 с сохранением данных
|
||||
make rolling-update # Поочерёдная замена API-реплик
|
||||
make restore # Восстановление остановленной API-реплики
|
||||
```
|
||||
|
||||
Публичные проверки lab4:
|
||||
|
||||
```sh
|
||||
make test
|
||||
make test-persistence ACTION_SCRIPT=./scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=./scripts/stop-one.sh
|
||||
make restore
|
||||
```
|
||||
|
||||
`.env.example`, `compose.example.yaml` и `contracts/infrastructure.json` описывают настройки и роли компонентов. **compose.example.yaml — список ролей с пустым services, не запускаемый стенд.** Реализуйте свой `compose.yaml`/`stack.yaml`, закрепите версии образов, заполните локальный `.env` и опишите bootstrap/миграции. Все зависимости поднимаются из этого репозитория; ссылка «у меня БД уже установлена» не заменяет воспроизводимость.
|
||||
`make openapi-generate` генерирует типы из текущего контракта lab4; нужен `oapi-codegen` v2.5.1 (`go install github.com/oapi-codegen/oapi-codegen/v2/cmd/oapi-codegen@v2.5.1`). Для генератора создаётся временное представление nullable/anyOf; исходный OpenAPI сохраняется, ограничения проверяет сервис. `make benchmark` запускает генератор текстового трафика.
|
||||
|
||||
Полная приёмка и измерения выполняются отдельно:
|
||||
|
||||
```sh
|
||||
# Compose-вариант после реализации:
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
# Дождитесь /health/ready; затем make test.
|
||||
# Для Swarm укажите эквивалентные build/push/deploy-команды в REPORT.md.
|
||||
python3 -m venv .venv
|
||||
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||
make evidence PYTHON=.venv/bin/python
|
||||
python3 scripts/write-report.py # Обновить REPORT.md после успешной приёмки
|
||||
```
|
||||
|
||||
`make evidence` использует изолированный Compose-проект `mtusi2026lab4-evidence`, порт `18082` и отдельные volumes. Здесь намеренно останавливаются реплики/хранилища и удаляются синтетические объекты. После успеха этот стенд останавливается с сохранением данных; основной стенд на `18081` не затрагивается. `evidence/acceptance.json` получает `passed=true` только после полного успешного прогона.
|
||||
|
||||
Если приёмка прервалась, остановите её стенд с сохранением данных: `COMPOSE_PROJECT_NAME=mtusi2026lab4-evidence ./scripts/compose.sh down`. Генератор отчёта отклонит незавершённый прогон. S3 endpoint, region и пути credentials зафиксированы в `compose.yaml`; для нестандартного bucket экспортируйте `S3_BUCKET` перед `make up`, чтобы bootstrap и Compose использовали одно имя. Основной стенд и приёмка используют общие локальные файлы credentials; для приёмки оставьте стандартный bucket.
|
||||
|
||||
```sh
|
||||
make reconcile # Dry-run: только диагностика orphan/missing
|
||||
make reconcile-apply # Удалить старые orphan и отметить missing
|
||||
# Восстановление из известного исходного файла с проверкой checksum:
|
||||
./scripts/compose.sh run --rm -T operator storage restore \
|
||||
--id <attachment-uuid> --source /fixtures/sample.png
|
||||
```
|
||||
|
||||
Для произвольного оригинала смонтируйте его в `operator` read-only и передайте путь через `--source`. Reconciler нужно запускать периодически, например каждые 10 минут через планировщик оператора; расписание автоматически не устанавливается. По умолчанию grace — час, upload timeout — 120 секунд. `RECONCILE_GRACE_PERIOD` должен превышать удвоенный `UPLOAD_TIMEOUT`. Сверка не удаляет метаданные и ссылки сообщений; потерянные байты без оригинала или резервной копии не восстанавливаются.
|
||||
|
||||
`compose.example.yaml` остаётся worksheet ролей, рабочий стенд находится в `compose.yaml`. Фактическая конфигурация описана в `.env.example` и [REPORT.md](REPORT.md).
|
||||
|
||||
|
||||
Примеры запросов доступны в `examples/requests.http`, примеры JSON — в `contracts/examples.json`. Для полной проверки схемы (по желанию локально; CI делает её автоматически):
|
||||
|
||||
|
||||
@@ -1,37 +1,166 @@
|
||||
# Отчёт — лабораторная 4
|
||||
# Отчёт — лабораторная 4: S3 и согласованность двух хранилищ
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Поликарпов Даниил Александрович, БПИ2303.
|
||||
- Целевая оценка: **5, трек 2 — согласованность PostgreSQL/S3**.
|
||||
- Измерения: 2026-11-13T07:53:45.321916+00:00; окончание проверки: 2026-11-13T07:58:31.186189+00:00.
|
||||
- HEAD репозитория на момент проверки: `ec540a1a725862a8619fcb758daec4ad197f0dd5`; рабочее дерево содержит незакоммиченную реализацию. Это HEAD основы, а не опубликованный commit решения. Commit/PR ещё не созданы.
|
||||
- Идентификатор полного прогона: `79ad73c1e746481385a84cebffdcf65e`; итог — [acceptance.json](evidence/acceptance.json).
|
||||
- Диагностика, тесты и подготовка отчёта выполнены с помощью OpenAI Codex. Исходники проверок и методики включены в репозиторий.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Нужны Docker Engine и Compose v2+, Go 1.27+, Python 3.9+, Bash, OpenSSL и Git. Проверено: macOS-26.5.1-arm64-arm-64bit-Mach-O, Docker 29.7.2, aarch64, 14 vCPU и 7.75 GiB памяти Docker VM. Нужны свободные loopback-порты 18081 и 18082. Соседние репозитории не требуются.
|
||||
|
||||
## Архитектура и решения
|
||||
```sh
|
||||
python3 -m venv .venv
|
||||
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||
make up
|
||||
make check validate PYTHON=.venv/bin/python
|
||||
make build vet test test-go test-storage
|
||||
make test-persistence ACTION_SCRIPT=./scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=./scripts/stop-one.sh
|
||||
make restore
|
||||
make reconcile
|
||||
# Полный изолированный прогон, включая отказы и исследование:
|
||||
make evidence PYTHON=.venv/bin/python
|
||||
python3 scripts/write-report.py
|
||||
# Безопасная остановка основного стенда, данные сохраняются:
|
||||
make down
|
||||
```
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
`make up` идемпотентно создаёт `.env.lab4`, JWT-ключи и отдельные S3 admin/API-ключи в `secrets/lab4`; существующие пароли и ключи не заменяются. Файлы игнорируются Git и исключены из Docker build context. API монтирует только JWT и собственные S3-ключи, admin-ключи в API не попадают. Единственная публикация основного стенда — `127.0.0.1:18081` через HAProxy.
|
||||
|
||||
Для **намеренного удаления только учебных данных основного стенда** оператор может выполнить `./scripts/compose.sh down -v`. При обычном restart/failover эта команда не применяется. Экспериментальный проект — `mtusi2026lab4-evidence`; его volumes также сохраняются после проверки. Повторная приёмка добавляет синтетические данные к существующим, что отражено в фактических размерах наборов.
|
||||
|
||||
## Архитектура и конфигурация
|
||||
|
||||
Клиент → HAProxy → API × 2 → PostgreSQL + приватный SeaweedFS. Redis хранит кэш неизменного состава чата; серверные сессии остаются в PostgreSQL. Перенесены регистрация/сессии, чаты, keyset-пагинация, общая БД и идемпотентность lab3. Добавлены Attachment, связи с сообщениями, HTTP endpoints, S3, reconciler и операторское восстановление.
|
||||
|
||||
| Роль | Образ / версия | Готовность и сохранность | Доступ |
|
||||
| --- | --- | --- | --- |
|
||||
| API × 2 / migrator | Сборка исходников: Go 1.27.2-alpine3.23 → Alpine 3.23.6 | `/health/live`; `/health/ready` проверяет PostgreSQL и S3 | API-порты не опубликованы; migrator — отдельный одноразовый процесс |
|
||||
| PostgreSQL | 17.11-alpine3.23 | `pg_isready`; `postgres_data` | Только сеть Compose |
|
||||
| Redis | 8.2.11-alpine3.22 | AUTH PING; AOF в `redis_data` | Только сеть Compose |
|
||||
| HAProxy | 3.2-alpine | Проверяет readiness каждой API-реплики | Только loopback 18081 |
|
||||
| SeaweedFS | 4.52 | `s3_data` содержит master/volume/filer; bootstrap проверяет bucket и PUT/DELETE | Только internal-сеть storage, без публикации gateway/master/filer |
|
||||
|
||||
Все digest закреплены в `compose.yaml` и Dockerfile. SeaweedFS: `sha256:57877e32bb90073b37dc109b438ba66e84fd5ed9ced5f5c04f56444ef243ce59`. `s3-init` использует admin identity и создаёт приватный bucket. В его JSON нет anonymous identity. API identity ограничена Read/Write/List конкретного bucket и не имеет Admin; создание другого bucket отвергается. Образы и точные зависимости Go зафиксированы manifest/lock-файлами проекта.
|
||||
|
||||
`.env.example` описывает параметры Compose: HOST_PORT/API_REPLICAS, пароли PostgreSQL/Redis, лимиты API и пула, SESSION_TTL_SECONDS, S3_BUCKET, MAX_UPLOAD_BYTES/MAX_IMAGE_PIXELS, UPLOAD_TIMEOUT и RECONCILE_GRACE_PERIOD. Их изменения задаются в `.env.lab4` или окружении оператора. S3_ENDPOINT, S3_REGION и пути credentials зафиксированы в `compose.yaml`; одноимённые переменные в `.env.lab4` их не переопределяют. В примере они приведены как комментарии для справки.
|
||||
|
||||
Для нестандартного bucket нужно также экспортировать `S3_BUCKET` перед `make up`: генератор S3-политики читает окружение процесса, а не `.env.lab4`. Для сдачи и приёмки используется стандартный `messenger-attachments`. Основной и экспериментальный проекты используют общие локальные файлы credentials; менять bucket/политику при работающем основном стенде не следует.
|
||||
|
||||
По умолчанию: API — 1 CPU/512 MiB каждая, четыре upload slots, один decoder, `GOMEMLIMIT=384MiB`; PostgreSQL — 2 CPU/1 GiB, Redis — 0.25 CPU/128 MiB, S3 — 1 CPU/512 MiB, LB — 0.5 CPU/128 MiB. Это лимиты контейнеров, а не гарантия высокой доступности единственного Docker-host.
|
||||
|
||||
## Синхронная загрузка и транзакционные границы
|
||||
|
||||
1. Проверить сессию и членство в чате; неизвестный и чужой ресурс одинаково дают 404. Проверить отображаемое filename, MIME и Content-Length.
|
||||
2. Прочитать не более 10 MiB через MaxBytesReader во временный файл, одновременно вычисляя SHA-256. Проверить формат и размеры через DecodeConfig, затем полностью декодировать PNG/JPEG под отдельным semaphore. Лимит 40 млн пикселей проверяется до полной распаковки.
|
||||
3. Сервер генерирует уникальный ключ `originals/<UUID>` и получает advisory session lock этого ключа. Filename не участвует ни в локальном пути, ни в S3 key.
|
||||
4. Сохранить оригинал в S3, затем атомарно вставить метаданные в PostgreSQL. Только после подтверждённого сохранения вернуть 201, `status=ready`, `variants=[]`, `error_code=null`.
|
||||
5. Удалить временный файл. После ответа скачивание зависит от S3 и БД, а не от файловой системы загрузившей реплики. Скачивание проходит через авторизованный API без redirect; Content-Length позволяет клиенту обнаружить обрыв передачи.
|
||||
|
||||
Атомарной транзакции PostgreSQL+S3 нет. При неопределённом результате COMMIT API возвращает ошибку, **не выполняя слепой S3 DELETE**: COMMIT мог пройти. Reconciler позже проверяет наличие метаданных. Обрыв тела до завершения чтения не создаёт готовый Attachment. Повтор самостоятельного upload может создать ещё один объект; прозрачных повторов публичного POST нет.
|
||||
|
||||
Сообщение, все связи вложений и результат идемпотентности записываются одной PostgreSQL-транзакцией. Правило «текст или вложение», UTF-8/4000 символов, максимум 10 уникальных ID и доступность вложений проверяет Go-сервис; функции и триггера `message_content` в миграциях нет. FK сохраняют ссылочную целостность. При проверке вложений используются row locks до COMMIT.
|
||||
|
||||
Версия хеша хранится в `hash_version`: legacy v1 учитывает текст, v2 — канонический текст и упорядоченный список ID. Отсутствующий список и `[]` равнозначны. Старые текстовые ключи продолжают воспроизводиться; изменение текста или вложений при том же ключе даёт 409. Порядок вложений сохраняется.
|
||||
|
||||
## Reconciler и восстановление
|
||||
|
||||
```sh
|
||||
make reconcile # По умолчанию dry-run
|
||||
make reconcile-apply # Применить безопасные изменения
|
||||
./scripts/compose.sh run --rm -T operator storage restore \
|
||||
--id <attachment-uuid> --source /fixtures/sample.png
|
||||
```
|
||||
|
||||
Сверка читает S3 страницами по 100 объектов и БД keyset-страницами по 100 метаданных. Обрабатываются только серверные ключи `originals/<UUID>`. Для каждого ключа используется та же advisory lock, что при upload/restore; под ней проверяются метаданные и актуальный HEAD объекта. Если метаданные существуют, объект защищён, даже если он ещё не прикреплён к сообщению. Отсутствующие метаданные плюс возраст старше grace позволяют удалить orphan.
|
||||
|
||||
В обычном стенде upload timeout — 120 секунд, grace — один час; конфигурация требует grace больше удвоенного timeout. Это запас для отменённого запроса/потерянной DB-сессии. Предполагаются синхронизированные часы локального стенда. При неопределённом получении или освобождении lock соединение закрывается, а не возвращается в pool с потенциально удерживаемой блокировкой.
|
||||
|
||||
Missing — метаданные без оригинала. Dry-run только сообщает проблему; apply выставляет внутренний `integrity_missing`, сохраняя metadata, ID и ссылки сообщений. Клиент с доступом получает 503; публичный enum статуса lab4 не меняется. Восстановление требует предоставленного оригинала с совпадающими размером, MIME и SHA-256. После PUT выполняется контрольное чтение; только затем снимается признак повреждения. Неверный оригинал отвергается, повторный restore безопасен.
|
||||
|
||||
При ошибке любой зависимости проход завершается ненулевым кодом, сохраняя частичные счётчики. Уже выполненные безопасные действия не откатываются: общего COMMIT между хранилищами нет. Повторный запуск продолжает исправление. Для регулярной очистки оператору нужно запускать `make reconcile-apply`, например каждые 10 минут через свой планировщик; автоматическое cron-расписание не устанавливается.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Критерий | Команда / наблюдение | Доказательство |
|
||||
| --- | --- | --- |
|
||||
| Структура и полный OpenAPI | `make check build vet`, `make validate` — PASS | [build-checks](evidence/build-checks.txt), [validate](evidence/validate.txt) |
|
||||
| Возможности lab3 и идемпотентность | Go race suite: история/пагинация, 32 конкурентных повтора через 2 реплики | [go-tests](evidence/go-tests.txt) |
|
||||
| Обязательный API lab4 | Публичный smoke: файл, attachment-only message, checksum, ACL — PASS | [smoke](evidence/smoke.txt) |
|
||||
| PNG/JPEG, bytes/pixels, filename | Exact 10 MiB — 201; превышение bytes, chunked и pixels — 413; MIME/corruption — 415; filename — 400 | [go-tests](evidence/go-tests.txt) |
|
||||
| Чужой файл и чат | Metadata/content и cross-chat attachment — 404; duplicate/null ID list — 400 | [go-tests](evidence/go-tests.txt) |
|
||||
| Только Go-проверка содержимого | Пустое сообщение отклоняется; attachment-only сохраняется; триггер отсутствует | [storage-tests](evidence/storage-tests.txt) |
|
||||
| Атомарность сообщений | Ошибка FK/записи ключа откатывает сообщение, связи и результат; legacy replay сохранён | [storage-tests](evidence/storage-tests.txt) |
|
||||
| Приватность и ограниченные ключи | Unsigned GET известного объекта — 403; API CreateBucket запрещён | [failures](evidence/failures.json) |
|
||||
| Persistence | Прежний token и исходный файл пережили restart; до/после видны реальные процессы всех stores | [persistence](evidence/persistence.txt), [snapshots](evidence/failures.json) |
|
||||
| Остановка загрузившей реплики | Поочерёдно остановлены обе; файл скачан другой репликой с тем же SHA-256 | [failures](evidence/failures.json) |
|
||||
| Публичный failover | Запись во время отказа, повторы без дубля, прежняя сессия — PASS | [failover](evidence/failover.txt) |
|
||||
| Недоступность S3 | Liveness 200, readiness/upload 503; reconciler не удаляет/не помечает | [failures](evidence/failures.json), [S3 down](evidence/reconcile-s3-down.txt) |
|
||||
| Отказ PostgreSQL | Сверка прекращается без изменений; после восстановления метаданные сохранены | [failures](evidence/failures.json), [DB down](evidence/reconcile-db-down.txt) |
|
||||
| Прерывание и PUT → ошибка БД | Незавершённый body не создаёт строку; timeout после PUT даёт диагностируемый orphan | [failures](evidence/failures.json) |
|
||||
| Missing и восстановление | Обнаружен; неверный оригинал отвергнут; верный восстановлен дважды без потери ссылки | [failures](evidence/failures.json) |
|
||||
| Память при 10 клиентах | Два размера, три повтора; ограничение admission учитывается в ошибках | [uploads](evidence/uploads.json) |
|
||||
| Безопасная сверка | Fresh и использованные объекты сохранены, старые orphan удалены, повтор ничего не удаляет | [safety](evidence/reconciliation-safety.json), [scans](evidence/reconciliation.json) |
|
||||
| Bootstrap | Повтор сохраняет ключи; существующий bucket допускает bootstrap и проверку записи | [bootstrap-repeat](evidence/bootstrap-repeat.txt), [bucket repeat](evidence/bucket-bootstrap-repeat.txt) |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
Полная команда воспроизведения — `make evidence`. PASS означает завершение перечисленных локальных сценариев, а не автоматическое выставление оценки. Секреты, tokens и raw HTTP-запросы в evidence не сохраняются.
|
||||
|
||||
## Эксперименты и отказы
|
||||
## Эксперименты
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Гипотеза: при неизменяемых серверных ключах, общей блокировке и grace можно повторяемо удалить orphan, сохранив свежие и используемые вложения; missing обнаруживаются, а внешний оригинал позволяет восстановить данные без смены ссылок.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
Изолированный проект использует те же образы и контейнерные ресурсы, но `UPLOAD_TIMEOUT=5s`, `RECONCILE_GRACE_PERIOD=15s`, чтобы ждать grace без часовой паузы. Обычные defaults остаются 120s/1h. Защита свежего объекта отдельно проверяется с grace 1h, независимо от длительности массового заполнения. Наборы содержат также ранее загруженные файлы — фактические размеры указаны ниже.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
### Upload: bytes и память
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Перед каждым из трёх повторов для каждого размера API-контейнеры создаются заново, выполняется одна прогревочная загрузка исходного малого файла, затем barrier запускает 10 клиентов одновременно. Отдельного длительного прогрева нет. Оригинал — RGB PNG 320×200, 111817 байт; вариант 5 MiB содержит валидный ancillary PNG chunk и имеет те же декодированные размеры. Это сравнение стоимости передачи bytes, а не исследование больших изображений.
|
||||
|
||||
| Bytes | Повтор | Длительность, s | Успехи | Успешные RPS | Ошибки | p50/p95/p99, ms | Max peak одной API, MiB | CPU samples API |
|
||||
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
| 111817 | 1 | 0.046 | 10/10 | 219.2 | 0% | 25.4/28.5/28.5 | 13.2 | 0.25%, 2.55% |
|
||||
| 111817 | 2 | 0.030 | 9/10 | 303.6 | 10% | 14.4/16.9/16.9 | 13.0 | 0.13%, 0.13% |
|
||||
| 111817 | 3 | 0.028 | 8/10 | 282.1 | 20% | 14.5/16.2/16.2 | 15.2 | 0.13%, 0.14% |
|
||||
| 5242880 | 1 | 0.548 | 8/10 | 14.6 | 20% | 342.3/419.0/419.0 | 35.2 | 0.10%, 8.72% |
|
||||
| 5242880 | 2 | 0.550 | 8/10 | 14.5 | 20% | 289.6/378.3/378.3 | 34.4 | 0.09%, 8.52% |
|
||||
| 5242880 | 3 | 0.534 | 8/10 | 15.0 | 20% | 292.3/361.2/361.2 | 35.5 | 0.14%, 8.36% |
|
||||
|
||||
Максимальный наблюдавшийся пик одной API: **35.5 MiB** при лимите 512 MiB. Сняты `memory.current` и kernel `memory.peak` cgroup v2: peak относится ко всей жизни заново созданного контейнера, включая startup и прогрев, а не только к интервалу burst. CPU — короткий `docker stats --no-stream` sample; он не даёт надёжной оценки steady-state CPU для столь короткого burst. В errors отдельно учитываются 429, повторов скрыто нет. Percentiles относятся только к успешным запросам; 10 наблюдений недостаточно для устойчивой оценки хвоста. RPS включает клиентскую подготовку, не доказывает максимальную пропускную способность сервера.
|
||||
|
||||
### Стоимость сверки
|
||||
|
||||
После заполнения выдерживается 16 секунд, выполняются три dry-run, apply и повторный apply. В таблице — медиана времени dry-run; счётчики вызовов приведены для первого прохода.
|
||||
|
||||
| Добавлено orphan | Объекты / metadata | Dry-run median, ms | Вызовы S3 | Проверки/страницы БД | Попытки session lock | Удалено apply | Удалено повторно |
|
||||
| --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
| 100 | 270 / 167 | 155 | 272 | 271 | 435 | 101 | 0 |
|
||||
| 1000 | 1169 / 167 | 555 | 1180 | 1170 | 1334 | 1000 | 0 |
|
||||
|
||||
`db_checks` считает проверки наличия, страницы метаданных и изменения; это не все сетевые round trips PostgreSQL. Каждая успешная session lock добавляет SQL получения и освобождения; отдельно показан `lock_attempts`. S3-счётчик включает HEAD bucket, List pages, Head objects и Delete. Стоимость растёт с фактическим числом объектов и метаданных; общий snapshot двух хранилищ не создаётся. Вставка UUID позади keyset cursor может попасть в следующий периодический проход.
|
||||
|
||||
Безопасность проверена предметными наблюдениями: свежий orphan остался, referenced originals не удалялись, invalid key и объект вне prefix остались, после очистки второй apply удалил ноль объектов и missing не появилось. Во время отдельного прохода выполнялся upload; его подтверждённый оригинал скачан с тем же checksum. Это конкурентный прогон, а детерминированное ожидание и отмена advisory lock отдельно проверены на реальном PostgreSQL в storage suite.
|
||||
|
||||
| Воздействие | Наблюдение | Доказательство |
|
||||
| --- | --- | --- |
|
||||
| Uploader 1 | Остановлен именно процесс `ffe0b126d1ef-37dbaa20-b33b-435f-9096-d7a918f55274` | Скачал `ddf8beb720a9-6d79116b-1a2d-435e-80e4-6a59b87714be`; SHA-256 совпал |
|
||||
| Uploader 2 | Остановлен именно процесс `ddf8beb720a9-6d79116b-1a2d-435e-80e4-6a59b87714be` | Скачал `ffe0b126d1ef-4c4cd939-b319-45c4-81d8-2d9449a97b87`; SHA-256 совпал |
|
||||
| Restart PostgreSQL/Redis/S3/API/LB | Прежние данные, session и bytes сохранены; процессы перезапущены или заменены | persistence + snapshots |
|
||||
| S3 остановлен | 200 live, 503 ready/upload; после bootstrap путь записи восстановлен | failures |
|
||||
| PostgreSQL остановлен | Reconciler завершился без удаления, после запуска данные сохранены | failures |
|
||||
| Тело upload оборвано | Строка Attachment не появилась | failures |
|
||||
| PUT успешен, INSERT заблокирован | Upload 503, метаданные не появились, orphan найден и очищен после grace | failures + reconciliation |
|
||||
| Оригинал удалён из S3 | Missing отмечен, затем исходник восстановлен с тем же ID/checksum и ссылкой | failures |
|
||||
|
||||
В этих сценариях потери подтверждённых данных после restart/failover не обнаружены. Время восстановления отдельно не измерялось: проверки подтверждают восстановление в пределах своих таймаутов, но не дают оценки RTO.
|
||||
|
||||
## Ограничения и источники
|
||||
|
||||
Один Docker-host, PostgreSQL, Redis, S3 и LB остаются одиночными точками отказа. Сессии хранятся в БД, Redis — необязательный cache; отказ S3 выводит API из readiness. Ошибки, сформированные самим LB при отсутствии здоровых backend, могут не иметь JSON/identity headers приложения. HTTP допустим только для локального стенда с синтетическими данными; TLS — задание lab7.
|
||||
|
||||
Reconciler не делает резервное копирование, не восстанавливает bytes из SHA-256 и не проверяет checksum каждого существующего объекта при обычном HEAD-обходе. Если оригинала/backup нет, missing остаётся невосстановимым. Временный локальный файл используется до сохранения, но не является долговременным источником оригинала. `GOMEMLIMIT` — мягкий бюджет Go, а не жёсткая гарантия RSS; предел процесса задаёт cgroup, полная распаковка всё ещё ограничена количеством пикселей и одним decoder.
|
||||
|
||||
Периодическую сверку запускает оператор. Потеря БД сессии/сетевые разделения покрываются блокировками, ограничением времени запроса и grace в рамках заявленного локального стенда; глобальной транзакции или защиты от произвольных внешних writers с admin-ключами нет. Нагрузочные результаты привязаны к этому hardware, размерам изображений и короткому burst-профилю.
|
||||
|
||||
Первичные материалы: [SeaweedFS 4.52](https://github.com/seaweedfs/seaweedfs/releases/tag/4.52), [S3 credentials/configuration](https://github.com/seaweedfs/seaweedfs/wiki/S3-Configuration), [AWS SDK for Go v2 endpoints](https://docs.aws.amazon.com/sdk-for-go/v2/developer-guide/configure-endpoints.html), [S3 upload semantics](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html). Использованы стандартные Go image/png/jpeg, AWS SDK v2, pgx, chi и oapi-codegen; версии — в go.mod/go.sum и generated header. Обязательный контракт и fixture предоставлены материалами курса.
|
||||
|
||||
+182
@@ -0,0 +1,182 @@
|
||||
name: mtusi2026lab4
|
||||
x-api: &api
|
||||
image: ${API_IMAGE:-mtusi-messenger:lab4}
|
||||
build: .
|
||||
restart: unless-stopped
|
||||
cpus: ${API_CPUS:-1}
|
||||
mem_limit: ${API_MEMORY:-512m}
|
||||
environment: &environment
|
||||
APP_ENABLED: "true"
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
APP_READ_TIMEOUT: 125s
|
||||
APP_WRITE_TIMEOUT: 125s
|
||||
APP_IDLE_TIMEOUT: 60s
|
||||
APP_SHUTDOWN_TIMEOUT: 8s
|
||||
APPLICATION_LOG_LEVEL: ${APPLICATION_LOG_LEVEL:-warn}
|
||||
APPLICATION_ENVIRONMENT: prod
|
||||
APPLICATION_SHUTDOWN_TIMEOUT: 8s
|
||||
POSTGRES_HOST: postgres
|
||||
POSTGRES_PORT: 5432
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Run scripts/bootstrap.sh}
|
||||
POSTGRES_DATABASE: messenger
|
||||
POSTGRES_VERSION_TABLE: versions
|
||||
POSTGRES_RUN_MIGRATIONS: "false"
|
||||
POSTGRES_MAX_CONNECTIONS: ${POSTGRES_MAX_CONNECTIONS:-8}
|
||||
POSTGRES_CONNECT_TIMEOUT: 500ms
|
||||
POSTGRES_QUERY_TIMEOUT: 2s
|
||||
REDIS_HOST: redis
|
||||
REDIS_PORT: 6379
|
||||
REDIS_DB: 0
|
||||
REDIS_USERNAME: default
|
||||
REDIS_PASSWORD: ${REDIS_PASSWORD:?Run scripts/bootstrap.sh}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
JWT_ACCESS_TOKEN_LIFETIME: 15m
|
||||
JWT_PRIVATE_KEY_PATH: /run/certs/private_key.pem
|
||||
JWT_PUBLIC_KEY_PATH: /run/certs/public_key.pem
|
||||
S3_ENDPOINT: http://s3:9000
|
||||
S3_REGION: us-east-1
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access
|
||||
S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret
|
||||
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
|
||||
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
|
||||
UPLOAD_TIMEOUT: ${UPLOAD_TIMEOUT:-120s}
|
||||
RECONCILE_GRACE_PERIOD: ${RECONCILE_GRACE_PERIOD:-1h}
|
||||
GOMEMLIMIT: 384MiB
|
||||
networks: [default, storage]
|
||||
volumes:
|
||||
- ./secrets/lab4/private_key.pem:/run/certs/private_key.pem:ro
|
||||
- ./secrets/lab4/public_key.pem:/run/certs/public_key.pem:ro
|
||||
- ./secrets/lab4/s3_api_access:/run/secrets/s3_access:ro
|
||||
- ./secrets/lab4/s3_api_secret:/run/secrets/s3_secret:ro
|
||||
stop_grace_period: 10s
|
||||
services:
|
||||
api:
|
||||
<<: *api
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-init:
|
||||
condition: service_completed_successfully
|
||||
migrate:
|
||||
<<: *api
|
||||
# The migration command uses only PostgreSQL: no signing keys or Redis credentials.
|
||||
volumes: []
|
||||
environment:
|
||||
<<: *environment
|
||||
REDIS_PASSWORD: ""
|
||||
restart: "no"
|
||||
command: [migrate]
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
lb:
|
||||
image: ${HAPROXY_IMAGE:-haproxy:3.2-alpine@sha256:96ec3c75a9264ba94e733eeacb44051ed6e7b79bb77a73c2c36f236b6423f1f0}
|
||||
restart: unless-stopped
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
ports:
|
||||
- "127.0.0.1:${HOST_PORT:-18081}:8080"
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_started
|
||||
healthcheck:
|
||||
test: [CMD, wget, -q, -O, /dev/null, http://127.0.0.1:8080/health/ready]
|
||||
interval: 2s
|
||||
timeout: 1s
|
||||
retries: 5
|
||||
stop_grace_period: 10s
|
||||
postgres:
|
||||
image: postgres:17.11-alpine3.23@sha256:05aa77262db9277fde6374bb2f901c9604b8a9373029e1a7993440c6aae48736
|
||||
restart: unless-stopped
|
||||
cpus: 2
|
||||
mem_limit: 1g
|
||||
environment:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Run scripts/bootstrap.sh}
|
||||
POSTGRES_DB: messenger
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: [CMD-SHELL, 'pg_isready -U messenger -d messenger']
|
||||
interval: 2s
|
||||
timeout: 1s
|
||||
retries: 30
|
||||
stop_grace_period: 30s
|
||||
redis:
|
||||
image: redis:8.2.11-alpine3.22@sha256:489a769e0a54dc245896132a15c72d2bc7296719954da99398f43a3877e5f8eb
|
||||
restart: unless-stopped
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
environment:
|
||||
REDIS_PASSWORD: ${REDIS_PASSWORD:?Run scripts/bootstrap.sh}
|
||||
command: [sh, -c, 'exec docker-entrypoint.sh redis-server --appendonly yes --maxmemory 64mb --maxmemory-policy allkeys-lru --requirepass "$$REDIS_PASSWORD"']
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: [CMD-SHELL, 'REDISCLI_AUTH="$$REDIS_PASSWORD" redis-cli --no-auth-warning ping | grep -qx PONG']
|
||||
interval: 2s
|
||||
timeout: 1s
|
||||
retries: 20
|
||||
stop_grace_period: 10s
|
||||
s3:
|
||||
image: chrislusf/seaweedfs:4.52@sha256:57877e32bb90073b37dc109b438ba66e84fd5ed9ced5f5c04f56444ef243ce59
|
||||
restart: unless-stopped
|
||||
cpus: 1
|
||||
mem_limit: 512m
|
||||
networks: [storage]
|
||||
command: [server, '-dir=/data', '-ip=s3', '-ip.bind=0.0.0.0', '-volume.max=4', '-master.volumeSizeLimitMB=128', '-filer', '-s3', '-s3.port=9000', '-s3.config=/etc/seaweedfs/s3.json']
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
- ./secrets/lab4/s3.json:/etc/seaweedfs/s3.json:ro
|
||||
healthcheck:
|
||||
test: [CMD, wget, -q, -O, /dev/null, http://127.0.0.1:9333/cluster/status]
|
||||
interval: 2s
|
||||
timeout: 1s
|
||||
retries: 30
|
||||
stop_grace_period: 30s
|
||||
s3-init:
|
||||
<<: *api
|
||||
restart: "no"
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
command: [storage, bootstrap]
|
||||
volumes:
|
||||
- ./secrets/lab4/s3_admin_access:/run/secrets/s3_access:ro
|
||||
- ./secrets/lab4/s3_admin_secret:/run/secrets/s3_secret:ro
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
operator:
|
||||
<<: *api
|
||||
profiles: [tools]
|
||||
restart: "no"
|
||||
command: [storage, reconcile]
|
||||
volumes:
|
||||
- ./secrets/lab4/s3_admin_access:/run/secrets/s3_access:ro
|
||||
- ./secrets/lab4/s3_admin_secret:/run/secrets/s3_secret:ro
|
||||
- ./tests/fixtures:/fixtures:ro
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on: {}
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
s3_data:
|
||||
|
||||
networks:
|
||||
storage:
|
||||
internal: true
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"run_id": "79ad73c1e746481385a84cebffdcf65e",
|
||||
"passed": true,
|
||||
"checks": [
|
||||
"contract",
|
||||
"go_race",
|
||||
"transaction_rollback",
|
||||
"smoke",
|
||||
"private_bucket",
|
||||
"persistence",
|
||||
"both_uploaders_failover",
|
||||
"public_failover",
|
||||
"s3_down",
|
||||
"interrupted_body",
|
||||
"s3_put_db_timeout",
|
||||
"missing_restore",
|
||||
"10_client_memory",
|
||||
"reconciler_grace",
|
||||
"reconciler_repeat"
|
||||
],
|
||||
"remaining_metadata": 167,
|
||||
"timestamp_utc": "2026-11-13T07:58:31.186189+00:00"
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
$ ./scripts/bootstrap.sh
|
||||
Local lab4 settings and synthetic keys ready; existing files preserved.
|
||||
Local storage credentials ready; existing keys preserved.
|
||||
@@ -0,0 +1,3 @@
|
||||
$ ./scripts/bootstrap.sh
|
||||
Local lab4 settings and synthetic keys ready; existing files preserved.
|
||||
Local storage credentials ready; existing keys preserved.
|
||||
@@ -0,0 +1,7 @@
|
||||
$ ./scripts/compose.sh run --rm -T s3-init
|
||||
{"bucket_ready":true,"write_checked":true}
|
||||
Container mtusi2026lab4-evidence-s3-1 Running
|
||||
Container mtusi2026lab4-evidence-s3-1 Waiting
|
||||
Container mtusi2026lab4-evidence-s3-1 Healthy
|
||||
Container mtusi2026lab4-evidence-s3-init-run-4de376c5f6a7 Creating
|
||||
Container mtusi2026lab4-evidence-s3-init-run-4de376c5f6a7 Created
|
||||
@@ -0,0 +1,6 @@
|
||||
$ make check build vet
|
||||
python3 scripts/check_contract.py
|
||||
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
|
||||
cd service && go build ./...
|
||||
cd service && go build -o ../bin/benchmark ./cmd/benchmark
|
||||
cd service && go vet ./...
|
||||
@@ -0,0 +1 @@
|
||||
$ ./scripts/compose.sh config --quiet
|
||||
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"run_id": "79ad73c1e746481385a84cebffdcf65e",
|
||||
"date_utc": "2026-11-13T07:53:45.321916+00:00",
|
||||
"git_head": "ec540a1a725862a8619fcb758daec4ad197f0dd5",
|
||||
"working_tree_dirty": true,
|
||||
"host": "macOS-26.5.1-arm64-arm-64bit-Mach-O",
|
||||
"docker": {
|
||||
"ServerVersion": "29.7.2",
|
||||
"Architecture": "aarch64",
|
||||
"NCPU": 14,
|
||||
"MemTotal": 8320954368
|
||||
},
|
||||
"project": "mtusi2026lab4-evidence",
|
||||
"limits": {
|
||||
"api_replicas": 2,
|
||||
"api_cpu_each": 1,
|
||||
"api_ram_each": 536870912,
|
||||
"upload_timeout": "5s",
|
||||
"grace": "15s",
|
||||
"upload_slots_each": 4,
|
||||
"decode_slots_each": 1
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
$ /Users/a7tech173/development/gitea.privcloud.tech/mtusi2026/mtusi2026lab4/.venv/bin/python tests/resilience.py --mode failover --action-script scripts/stop-one.sh --base-url http://localhost:18082
|
||||
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
|
||||
{"observed_instances_before": 2, "successful_writes": 132, "failed_attempts": 1, "first_recovery_seconds": 0.833}
|
||||
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,210 @@
|
||||
{
|
||||
"private_bucket": {
|
||||
"anonymous_status": 403,
|
||||
"api_create_bucket_denied": true
|
||||
},
|
||||
"restart": {
|
||||
"before": [
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-api-1",
|
||||
"id": "4a021921f561be4bdee675a490989e099b87e444406bea66dba1d9f82001e376",
|
||||
"started_at": "2026-11-13T07:53:40.063317335Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-api-2",
|
||||
"id": "9b477a3610f71893c9c0dd9d4c4609b6c442852b9ef5d3b1e606669eaf74834c",
|
||||
"started_at": "2026-11-13T07:53:40.148753501Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-lb-1",
|
||||
"id": "b5cc17b8730977dd413e539913c1d0ceb7ae94af0b951bdf99e85198bc0002e8",
|
||||
"started_at": "2026-11-13T07:53:40.236674418Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-postgres-1",
|
||||
"id": "e968972d2ba216ff2c712c079027c62e915dec9cf846f0396c5085913ddccf21",
|
||||
"started_at": "2026-11-13T07:53:07.848785917Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-redis-1",
|
||||
"id": "b300113cef056a81152fe050c653cc69bb33ea3c63eaa8ba687331a030a96b47",
|
||||
"started_at": "2026-11-13T07:53:07.848210958Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-s3-1",
|
||||
"id": "6ea911e2878fae75ed23e88102a9025bccb25bcc5a8921c3c66d6295db84ae3e",
|
||||
"started_at": "2026-11-13T07:53:07.853984417Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
}
|
||||
],
|
||||
"after": [
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-api-1",
|
||||
"id": "ffe0b126d1ef3f136ba1790663bdd510e652d03d12056707257a7994e691b132",
|
||||
"started_at": "2026-11-13T07:55:08.108779Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-api-2",
|
||||
"id": "ddf8beb720a97155c85335baf4225af10c0e8464c6e82c38022ee41453cde56a",
|
||||
"started_at": "2026-11-13T07:55:08.180382834Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-lb-1",
|
||||
"id": "b5cc17b8730977dd413e539913c1d0ceb7ae94af0b951bdf99e85198bc0002e8",
|
||||
"started_at": "2026-11-13T07:55:08.264081834Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-postgres-1",
|
||||
"id": "e968972d2ba216ff2c712c079027c62e915dec9cf846f0396c5085913ddccf21",
|
||||
"started_at": "2026-11-13T07:54:04.77389293Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-redis-1",
|
||||
"id": "b300113cef056a81152fe050c653cc69bb33ea3c63eaa8ba687331a030a96b47",
|
||||
"started_at": "2026-11-13T07:54:04.863248305Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
},
|
||||
{
|
||||
"name": "mtusi2026lab4-evidence-s3-1",
|
||||
"id": "6ea911e2878fae75ed23e88102a9025bccb25bcc5a8921c3c66d6295db84ae3e",
|
||||
"started_at": "2026-11-13T07:54:29.774768469Z",
|
||||
"running": true,
|
||||
"health": "healthy"
|
||||
}
|
||||
]
|
||||
},
|
||||
"failover": [
|
||||
{
|
||||
"stopped_container": "ffe0b126d1ef3f136ba1790663bdd510e652d03d12056707257a7994e691b132",
|
||||
"upload_instance": "ffe0b126d1ef-37dbaa20-b33b-435f-9096-d7a918f55274",
|
||||
"download_instance": "ddf8beb720a9-6d79116b-1a2d-435e-80e4-6a59b87714be",
|
||||
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75"
|
||||
},
|
||||
{
|
||||
"stopped_container": "ddf8beb720a97155c85335baf4225af10c0e8464c6e82c38022ee41453cde56a",
|
||||
"upload_instance": "ddf8beb720a9-6d79116b-1a2d-435e-80e4-6a59b87714be",
|
||||
"download_instance": "ffe0b126d1ef-4c4cd939-b319-45c4-81d8-2d9449a97b87",
|
||||
"sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75"
|
||||
}
|
||||
],
|
||||
"s3_outage": {
|
||||
"live": 200,
|
||||
"ready": 503,
|
||||
"upload": 503,
|
||||
"reconciler": {
|
||||
"apply": true,
|
||||
"objects": 0,
|
||||
"metadata": 0,
|
||||
"orphans": 0,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 0,
|
||||
"s3_requests": 1,
|
||||
"db_checks": 0,
|
||||
"lock_attempts": 0,
|
||||
"duration_ms": 9,
|
||||
"grace_seconds": 15
|
||||
}
|
||||
},
|
||||
"interrupted_upload": {
|
||||
"metadata_unchanged": true
|
||||
},
|
||||
"s3_put_db_timeout": {
|
||||
"upload_status": 503,
|
||||
"metadata_unchanged": true
|
||||
},
|
||||
"dry_run_after_failed_commit": {
|
||||
"apply": false,
|
||||
"objects": 111,
|
||||
"metadata": 109,
|
||||
"orphans": 1,
|
||||
"fresh_orphans": 1,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 109,
|
||||
"s3_requests": 113,
|
||||
"db_checks": 113,
|
||||
"lock_attempts": 219,
|
||||
"duration_ms": 82,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
"missing_repair": {
|
||||
"dry_run": {
|
||||
"apply": false,
|
||||
"objects": 110,
|
||||
"metadata": 109,
|
||||
"orphans": 1,
|
||||
"fresh_orphans": 1,
|
||||
"deleted": 0,
|
||||
"missing": 1,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 108,
|
||||
"s3_requests": 113,
|
||||
"db_checks": 112,
|
||||
"lock_attempts": 218,
|
||||
"duration_ms": 86,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
"apply": {
|
||||
"apply": true,
|
||||
"objects": 110,
|
||||
"metadata": 109,
|
||||
"orphans": 1,
|
||||
"fresh_orphans": 1,
|
||||
"deleted": 0,
|
||||
"missing": 1,
|
||||
"marked_missing": 1,
|
||||
"referenced_objects": 108,
|
||||
"s3_requests": 113,
|
||||
"db_checks": 113,
|
||||
"lock_attempts": 218,
|
||||
"duration_ms": 86,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
"wrong_original_rejected": true,
|
||||
"restored_sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75",
|
||||
"reference_preserved": true,
|
||||
"repeat_safe": true
|
||||
},
|
||||
"database_outage": {
|
||||
"reconciler": {
|
||||
"apply": true,
|
||||
"objects": 0,
|
||||
"metadata": 0,
|
||||
"orphans": 0,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 0,
|
||||
"s3_requests": 0,
|
||||
"db_checks": 0,
|
||||
"lock_attempts": 0,
|
||||
"duration_ms": 30,
|
||||
"grace_seconds": 15
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
$ make test-go BASE_URL=http://localhost:18082
|
||||
cd service && TEST_BASE_URL="http://localhost:18082" go test -race -count=1 -v ./...
|
||||
? gitea.privcloud.tech/mtusi2026/cmd/app [no test files]
|
||||
=== RUN TestLostPostResponseIsNotRetried
|
||||
--- PASS: TestLostPostResponseIsNotRetried (0.00s)
|
||||
PASS
|
||||
ok gitea.privcloud.tech/mtusi2026/cmd/benchmark 2.040s
|
||||
? gitea.privcloud.tech/mtusi2026/internal/adapter/controller/http [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/adapter/repository/cache/outside [no test files]
|
||||
=== RUN TestIdempotencyTransactionAndExpiry
|
||||
idempotency_test.go:27: run make test-storage for an isolated PostgreSQL database
|
||||
--- SKIP: TestIdempotencyTransactionAndExpiry (0.00s)
|
||||
PASS
|
||||
ok gitea.privcloud.tech/mtusi2026/internal/adapter/repository/storage 1.453s
|
||||
? gitea.privcloud.tech/mtusi2026/internal/config [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/dto [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/gen/contracts [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/apperr [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/auth/jwtprovider [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/cache [no test files]
|
||||
=== RUN TestSessionFingerprints
|
||||
--- PASS: TestSessionFingerprints (0.02s)
|
||||
PASS
|
||||
ok gitea.privcloud.tech/mtusi2026/internal/pkg/encrypting 2.575s
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/migrator [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/objectstore [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/postgres [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/postgres/txmanager [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/rest/headers [no test files]
|
||||
=== RUN TestVerifiedSessionIsRequestScoped
|
||||
2026/11/13 10:53:54 ERROR response error="revoked session" status=401 requestId=bc73f964-1689-40e9-94a6-5921464e0107
|
||||
--- PASS: TestVerifiedSessionIsRequestScoped (0.00s)
|
||||
=== RUN TestMissingTokenDoesNotCallSessionStore
|
||||
2026/11/13 10:53:54 ERROR response error="missing or invalid bearer token" status=401 requestId=3bc04393-afad-4fda-a379-ba4c3b190a72
|
||||
--- PASS: TestMissingTokenDoesNotCallSessionStore (0.00s)
|
||||
PASS
|
||||
ok gitea.privcloud.tech/mtusi2026/internal/pkg/rest/middleware 2.320s
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/types [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/types/authprovider [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/types/environment [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/types/role [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/pkg/validator [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/service/attachment [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/service/auth [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/service/chat [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/internal/service/user [no test files]
|
||||
? gitea.privcloud.tech/mtusi2026/migrations [no test files]
|
||||
=== RUN TestInvalidJSON
|
||||
=== RUN TestInvalidJSON/malformed
|
||||
=== RUN TestInvalidJSON/wrong_type
|
||||
=== RUN TestInvalidJSON/unknown_field
|
||||
--- PASS: TestInvalidJSON (0.01s)
|
||||
--- PASS: TestInvalidJSON/malformed (0.01s)
|
||||
--- PASS: TestInvalidJSON/wrong_type (0.00s)
|
||||
--- PASS: TestInvalidJSON/unknown_field (0.00s)
|
||||
=== RUN TestForeignChatIsHidden
|
||||
--- PASS: TestForeignChatIsHidden (0.55s)
|
||||
=== RUN TestEmptyMessage
|
||||
--- PASS: TestEmptyMessage (0.19s)
|
||||
=== RUN TestDuplicateUsername
|
||||
--- PASS: TestDuplicateUsername (0.31s)
|
||||
=== RUN TestMessagePagination
|
||||
--- PASS: TestMessagePagination (0.23s)
|
||||
=== RUN TestAttachmentContractAndErrors
|
||||
=== RUN TestAttachmentContractAndErrors/fake
|
||||
=== RUN TestAttachmentContractAndErrors/mime_mismatch
|
||||
=== RUN TestAttachmentContractAndErrors/wrong_type
|
||||
=== RUN TestAttachmentContractAndErrors/truncated
|
||||
--- PASS: TestAttachmentContractAndErrors (0.88s)
|
||||
--- PASS: TestAttachmentContractAndErrors/fake (0.00s)
|
||||
--- PASS: TestAttachmentContractAndErrors/mime_mismatch (0.00s)
|
||||
--- PASS: TestAttachmentContractAndErrors/wrong_type (0.00s)
|
||||
--- PASS: TestAttachmentContractAndErrors/truncated (0.00s)
|
||||
=== RUN TestConcurrentAttachmentMessage
|
||||
--- PASS: TestConcurrentAttachmentMessage (0.24s)
|
||||
=== RUN TestConcurrentMessages
|
||||
concurrency_test.go:160: 10 clients sent 100 messages; history contains 100 unique messages across 6 pages
|
||||
--- PASS: TestConcurrentMessages (1.80s)
|
||||
=== RUN TestIdempotencyContract
|
||||
--- PASS: TestIdempotencyContract (0.50s)
|
||||
=== RUN TestConcurrentIdempotencyAcrossReplicas
|
||||
idempotency_test.go:103: 32 requests across 2 replicas created exactly one message
|
||||
--- PASS: TestConcurrentIdempotencyAcrossReplicas (0.17s)
|
||||
PASS
|
||||
ok gitea.privcloud.tech/mtusi2026/tests 6.628s
|
||||
@@ -0,0 +1,3 @@
|
||||
$ /Users/a7tech173/development/gitea.privcloud.tech/mtusi2026/mtusi2026lab4/.venv/bin/python tests/resilience.py --mode persistence --action-script scripts/restart.sh --base-url http://localhost:18082
|
||||
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
|
||||
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
|
||||
@@ -0,0 +1,8 @@
|
||||
$ ./scripts/compose.sh run --rm -T operator storage reconcile --apply
|
||||
{"apply":true,"objects":0,"metadata":0,"orphans":0,"fresh_orphans":0,"deleted":0,"missing":0,"marked_missing":0,"referenced_objects":0,"s3_requests":0,"db_checks":0,"lock_attempts":0,"duration_ms":30,"grace_seconds":15}
|
||||
Container mtusi2026lab4-evidence-operator-run-90ffcc11fd1e Creating
|
||||
Container mtusi2026lab4-evidence-operator-run-90ffcc11fd1e Created
|
||||
{"time":"2026-11-13T07:56:53.425170091Z","level":"ERROR","msg":"Connect","host":"postgres","port":5432,"database":"messenger","ts":30921958,"err":"failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: no such host"}
|
||||
{"time":"2026-11-13T07:56:53.425200216Z","level":"ERROR","msg":"Acquire","err":"failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: no such host","ts":30992791}
|
||||
{"time":"2026-11-13T07:56:53.425281299Z","level":"ERROR","msg":"application stopped","error":"failed to connect to `user=messenger database=messenger`: hostname resolving error: lookup postgres on 127.0.0.11:53: no such host"}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
$ ./scripts/compose.sh run --rm -T operator storage reconcile --apply
|
||||
{"apply":true,"objects":0,"metadata":0,"orphans":0,"fresh_orphans":0,"deleted":0,"missing":0,"marked_missing":0,"referenced_objects":0,"s3_requests":1,"db_checks":0,"lock_attempts":0,"duration_ms":9,"grace_seconds":15}
|
||||
Container mtusi2026lab4-evidence-operator-run-c46daade23bf Creating
|
||||
Container mtusi2026lab4-evidence-operator-run-c46daade23bf Created
|
||||
{"time":"2026-11-13T07:56:13.911024003Z","level":"ERROR","msg":"application stopped","error":"operation error S3: HeadBucket, https response error StatusCode: 0, RequestID: , HostID: , request send failed, Head \"http://s3:9000/messenger-attachments\": dial tcp: lookup s3 on 127.0.0.11:53: no such host"}
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"fresh_apply": {
|
||||
"apply": true,
|
||||
"objects": 170,
|
||||
"metadata": 166,
|
||||
"orphans": 2,
|
||||
"fresh_orphans": 2,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 166,
|
||||
"s3_requests": 171,
|
||||
"db_checks": 171,
|
||||
"lock_attempts": 334,
|
||||
"duration_ms": 120,
|
||||
"grace_seconds": 3600
|
||||
},
|
||||
"fresh_key_preserved": true,
|
||||
"concurrent_scan": {
|
||||
"apply": true,
|
||||
"objects": 171,
|
||||
"metadata": 167,
|
||||
"orphans": 2,
|
||||
"fresh_orphans": 1,
|
||||
"deleted": 1,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 173,
|
||||
"db_checks": 173,
|
||||
"lock_attempts": 336,
|
||||
"duration_ms": 117,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
"concurrent_upload_sha256": "99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75"
|
||||
}
|
||||
@@ -0,0 +1,176 @@
|
||||
[
|
||||
{
|
||||
"seeded": 100,
|
||||
"dry_runs": [
|
||||
{
|
||||
"apply": false,
|
||||
"objects": 270,
|
||||
"metadata": 167,
|
||||
"orphans": 101,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 272,
|
||||
"db_checks": 271,
|
||||
"lock_attempts": 435,
|
||||
"duration_ms": 162,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
{
|
||||
"apply": false,
|
||||
"objects": 270,
|
||||
"metadata": 167,
|
||||
"orphans": 101,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 272,
|
||||
"db_checks": 271,
|
||||
"lock_attempts": 435,
|
||||
"duration_ms": 153,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
{
|
||||
"apply": false,
|
||||
"objects": 270,
|
||||
"metadata": 167,
|
||||
"orphans": 101,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 272,
|
||||
"db_checks": 271,
|
||||
"lock_attempts": 435,
|
||||
"duration_ms": 155,
|
||||
"grace_seconds": 15
|
||||
}
|
||||
],
|
||||
"apply": {
|
||||
"apply": true,
|
||||
"objects": 270,
|
||||
"metadata": 167,
|
||||
"orphans": 101,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 101,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 373,
|
||||
"db_checks": 372,
|
||||
"lock_attempts": 435,
|
||||
"duration_ms": 209,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
"repeat_apply": {
|
||||
"apply": true,
|
||||
"objects": 169,
|
||||
"metadata": 167,
|
||||
"orphans": 0,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 170,
|
||||
"db_checks": 170,
|
||||
"lock_attempts": 334,
|
||||
"duration_ms": 112,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
"invalid_key_preserved": true,
|
||||
"outside_prefix_preserved": true
|
||||
},
|
||||
{
|
||||
"seeded": 1000,
|
||||
"dry_runs": [
|
||||
{
|
||||
"apply": false,
|
||||
"objects": 1169,
|
||||
"metadata": 167,
|
||||
"orphans": 1000,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 1180,
|
||||
"db_checks": 1170,
|
||||
"lock_attempts": 1334,
|
||||
"duration_ms": 573,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
{
|
||||
"apply": false,
|
||||
"objects": 1169,
|
||||
"metadata": 167,
|
||||
"orphans": 1000,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 1180,
|
||||
"db_checks": 1170,
|
||||
"lock_attempts": 1334,
|
||||
"duration_ms": 555,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
{
|
||||
"apply": false,
|
||||
"objects": 1169,
|
||||
"metadata": 167,
|
||||
"orphans": 1000,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 1180,
|
||||
"db_checks": 1170,
|
||||
"lock_attempts": 1334,
|
||||
"duration_ms": 551,
|
||||
"grace_seconds": 15
|
||||
}
|
||||
],
|
||||
"apply": {
|
||||
"apply": true,
|
||||
"objects": 1169,
|
||||
"metadata": 167,
|
||||
"orphans": 1000,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 1000,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 2180,
|
||||
"db_checks": 2170,
|
||||
"lock_attempts": 1334,
|
||||
"duration_ms": 952,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
"repeat_apply": {
|
||||
"apply": true,
|
||||
"objects": 169,
|
||||
"metadata": 167,
|
||||
"orphans": 0,
|
||||
"fresh_orphans": 0,
|
||||
"deleted": 0,
|
||||
"missing": 0,
|
||||
"marked_missing": 0,
|
||||
"referenced_objects": 167,
|
||||
"s3_requests": 170,
|
||||
"db_checks": 170,
|
||||
"lock_attempts": 334,
|
||||
"duration_ms": 119,
|
||||
"grace_seconds": 15
|
||||
},
|
||||
"invalid_key_preserved": true,
|
||||
"outside_prefix_preserved": true
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,3 @@
|
||||
$ make test BASE_URL=http://localhost:18082
|
||||
python3 tests/smoke.py --base-url "http://localhost:18082"
|
||||
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,37 @@
|
||||
$ ./scripts/compose.sh down
|
||||
Container mtusi2026lab4-evidence-lb-1 Stopping
|
||||
Container mtusi2026lab4-evidence-redis-1 Stopping
|
||||
Container mtusi2026lab4-evidence-lb-1 Stopped
|
||||
Container mtusi2026lab4-evidence-lb-1 Removing
|
||||
Container mtusi2026lab4-evidence-lb-1 Removed
|
||||
Container mtusi2026lab4-evidence-api-2 Stopping
|
||||
Container mtusi2026lab4-evidence-api-1 Stopping
|
||||
Container mtusi2026lab4-evidence-redis-1 Stopped
|
||||
Container mtusi2026lab4-evidence-redis-1 Removing
|
||||
Container mtusi2026lab4-evidence-redis-1 Removed
|
||||
Container mtusi2026lab4-evidence-api-1 Stopped
|
||||
Container mtusi2026lab4-evidence-api-1 Removing
|
||||
Container mtusi2026lab4-evidence-api-1 Removed
|
||||
Container mtusi2026lab4-evidence-api-2 Stopped
|
||||
Container mtusi2026lab4-evidence-api-2 Removing
|
||||
Container mtusi2026lab4-evidence-api-2 Removed
|
||||
Container mtusi2026lab4-evidence-migrate-1 Stopping
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Stopping
|
||||
Container mtusi2026lab4-evidence-migrate-1 Stopped
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Stopped
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Removing
|
||||
Container mtusi2026lab4-evidence-migrate-1 Removing
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Removed
|
||||
Container mtusi2026lab4-evidence-s3-1 Stopping
|
||||
Container mtusi2026lab4-evidence-migrate-1 Removed
|
||||
Container mtusi2026lab4-evidence-postgres-1 Stopping
|
||||
Container mtusi2026lab4-evidence-postgres-1 Stopped
|
||||
Container mtusi2026lab4-evidence-postgres-1 Removing
|
||||
Container mtusi2026lab4-evidence-postgres-1 Removed
|
||||
Container mtusi2026lab4-evidence-s3-1 Stopped
|
||||
Container mtusi2026lab4-evidence-s3-1 Removing
|
||||
Container mtusi2026lab4-evidence-s3-1 Removed
|
||||
Network mtusi2026lab4-evidence_storage Removing
|
||||
Network mtusi2026lab4-evidence_default Removing
|
||||
Network mtusi2026lab4-evidence_default Removed
|
||||
Network mtusi2026lab4-evidence_storage Removed
|
||||
@@ -0,0 +1,172 @@
|
||||
$ ./scripts/compose.sh up -d --build --wait --wait-timeout 120
|
||||
#1 [internal] load local bake definitions
|
||||
#1 reading from stdin 1.68kB done
|
||||
#1 DONE 0.0s
|
||||
|
||||
#2 [api internal] load build definition from Dockerfile
|
||||
#2 transferring dockerfile: 944B done
|
||||
#2 DONE 0.0s
|
||||
|
||||
#3 [s3-init] resolve image config for docker-image://docker.io/docker/dockerfile:1
|
||||
#3 DONE 3.6s
|
||||
|
||||
#4 [s3-init] docker-image://docker.io/docker/dockerfile:1@sha256:4edf897a3ffa55b89f906fc8cc78afdb3f1834cc9c7083565e611a8a7d5fe99e
|
||||
#4 resolve docker.io/docker/dockerfile:1@sha256:4edf897a3ffa55b89f906fc8cc78afdb3f1834cc9c7083565e611a8a7d5fe99e done
|
||||
#4 CACHED
|
||||
|
||||
#5 [s3-init internal] load metadata for docker.io/library/alpine:3.23.6@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0
|
||||
#5 DONE 0.7s
|
||||
|
||||
#6 [api internal] load metadata for docker.io/library/golang:1.27.2-alpine3.23@sha256:9e45f0eb4a63ed37ad6e604950407558b7c738e34e015ae77a5d4ad369cde079
|
||||
#6 DONE 0.7s
|
||||
|
||||
#7 [migrate internal] load .dockerignore
|
||||
#7 transferring context: 196B done
|
||||
#7 DONE 0.0s
|
||||
|
||||
#8 [s3-init build 1/6] FROM docker.io/library/golang:1.27.2-alpine3.23@sha256:9e45f0eb4a63ed37ad6e604950407558b7c738e34e015ae77a5d4ad369cde079
|
||||
#8 resolve docker.io/library/golang:1.27.2-alpine3.23@sha256:9e45f0eb4a63ed37ad6e604950407558b7c738e34e015ae77a5d4ad369cde079 done
|
||||
#8 DONE 0.0s
|
||||
|
||||
#9 [api stage-1 1/2] FROM docker.io/library/alpine:3.23.6@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0
|
||||
#9 resolve docker.io/library/alpine:3.23.6@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0 done
|
||||
#9 DONE 0.0s
|
||||
|
||||
#10 [s3-init internal] load build context
|
||||
#10 transferring context: 266.10kB 0.0s done
|
||||
#10 transferring context: 266.10kB 0.0s done
|
||||
#10 DONE 0.0s
|
||||
|
||||
#11 [migrate build 2/6] WORKDIR /src
|
||||
#11 CACHED
|
||||
|
||||
#12 [migrate build 3/6] COPY service/go.mod service/go.sum ./
|
||||
#12 CACHED
|
||||
|
||||
#13 [migrate build 4/6] RUN go mod download
|
||||
#13 CACHED
|
||||
|
||||
#14 [migrate build 5/6] COPY service/ ./
|
||||
#14 DONE 0.0s
|
||||
|
||||
#15 [api build 6/6] RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -mod=readonly -trimpath -ldflags="-s -w" -o /out/messenger ./cmd/app
|
||||
#15 DONE 7.8s
|
||||
|
||||
#9 [api stage-1 1/2] FROM docker.io/library/alpine:3.23.6@sha256:85fe1e81d6758c208f3e1eed4338a1997e19d4be002d4dd32d3100c9a8c010a0
|
||||
#9 CACHED
|
||||
|
||||
#16 [migrate stage-1 2/2] COPY --from=build --chmod=0555 /out/messenger /usr/local/bin/messenger
|
||||
#16 DONE 0.0s
|
||||
|
||||
#17 [migrate] exporting to image
|
||||
#17 exporting layers
|
||||
#17 exporting layers 0.5s done
|
||||
#17 exporting manifest sha256:948ad1af6f992219f7dde5f8603ccbe4d82f65f7411590734325f8ece4054d56 done
|
||||
#17 exporting config sha256:b5651d81abddc31715bf9ebe20720b9ae976dfe1bcf2230fe726ae4b64efbd2b done
|
||||
#17 exporting attestation manifest sha256:74933fc7013ee1ad77249c9075e39d1e7a64ea7cf4dc6ce30859e988dbbb60a7 done
|
||||
#17 exporting manifest list sha256:20e4a53b5326121835451e2a95f1b04fdf1254e45e39143c098943ec98819735 done
|
||||
#17 naming to docker.io/library/mtusi-messenger:lab4 done
|
||||
#17 unpacking to docker.io/library/mtusi-messenger:lab4
|
||||
#17 unpacking to docker.io/library/mtusi-messenger:lab4 0.1s done
|
||||
#17 DONE 0.6s
|
||||
|
||||
#18 [s3-init] exporting to image
|
||||
#18 exporting layers 0.5s done
|
||||
#18 exporting manifest sha256:1b344fe5d9c17d81bb90b013ac68d8ce1ac45b8e408e3f0c7b0d8d8e8928a88c done
|
||||
#18 exporting config sha256:09f51d8271d61bcdf3b2770077dcb7b186790f7aa2c318a33ab4b910a1ddb321 done
|
||||
#18 exporting attestation manifest sha256:cdc25996773907cddbcfad92c53bd16c96eca4a73039bac0b87dade8bbff7ee5 done
|
||||
#18 exporting manifest list sha256:e1f8984d98f03ef74ee13c081c7cb133a44cf6d52f7c40b574de43d7f229231c done
|
||||
#18 naming to docker.io/library/mtusi-messenger:lab4 done
|
||||
#18 unpacking to docker.io/library/mtusi-messenger:lab4 0.1s done
|
||||
#18 DONE 0.6s
|
||||
|
||||
#19 [api] exporting to image
|
||||
#19 exporting layers 0.5s done
|
||||
#19 exporting manifest sha256:76240d7f6b0a289ed497879e1b42846dfa453a01922e70d74e9359f36bb726ed done
|
||||
#19 exporting config sha256:4c6f1c5da51ee7ebebdf68baa35acda1a67837791a3329906b594c466747809f done
|
||||
#19 exporting attestation manifest sha256:76d4cf96e9b2b523f6a5839249e6a0a9d0d19cee7dfd7b1b6369d4f7da114c32 done
|
||||
#19 exporting manifest list sha256:dcf1f3e6a116f95109ebe249de73d7d9969523b2fabadfa26f6d9d834a33d8aa done
|
||||
#19 naming to docker.io/library/mtusi-messenger:lab4 done
|
||||
#19 unpacking to docker.io/library/mtusi-messenger:lab4 0.1s done
|
||||
#19 DONE 0.6s
|
||||
|
||||
#20 [api] resolving provenance for metadata file
|
||||
#20 DONE 0.0s
|
||||
|
||||
#21 [s3-init] resolving provenance for metadata file
|
||||
#21 DONE 0.0s
|
||||
|
||||
#22 [migrate] resolving provenance for metadata file
|
||||
#22 DONE 0.0s
|
||||
Image postgres:17.11-alpine3.23@sha256:05aa77262db9277fde6374bb2f901c9604b8a9373029e1a7993440c6aae48736 Pulling
|
||||
Image haproxy:3.2-alpine@sha256:96ec3c75a9264ba94e733eeacb44051ed6e7b79bb77a73c2c36f236b6423f1f0 Pulling
|
||||
Image redis:8.2.11-alpine3.22@sha256:489a769e0a54dc245896132a15c72d2bc7296719954da99398f43a3877e5f8eb Pulling
|
||||
Image postgres:17.11-alpine3.23@sha256:05aa77262db9277fde6374bb2f901c9604b8a9373029e1a7993440c6aae48736 Pulled
|
||||
Image redis:8.2.11-alpine3.22@sha256:489a769e0a54dc245896132a15c72d2bc7296719954da99398f43a3877e5f8eb Pulled
|
||||
Image haproxy:3.2-alpine@sha256:96ec3c75a9264ba94e733eeacb44051ed6e7b79bb77a73c2c36f236b6423f1f0 Pulled
|
||||
Image mtusi-messenger:lab4 Building
|
||||
Image mtusi-messenger:lab4 Building
|
||||
Image mtusi-messenger:lab4 Building
|
||||
Image mtusi-messenger:lab4 Built
|
||||
Image mtusi-messenger:lab4 Built
|
||||
Image mtusi-messenger:lab4 Built
|
||||
Network mtusi2026lab4-evidence_storage Creating
|
||||
Network mtusi2026lab4-evidence_storage Created
|
||||
Network mtusi2026lab4-evidence_default Creating
|
||||
Network mtusi2026lab4-evidence_default Created
|
||||
Container mtusi2026lab4-evidence-redis-1 Creating
|
||||
Container mtusi2026lab4-evidence-postgres-1 Creating
|
||||
Container mtusi2026lab4-evidence-s3-1 Creating
|
||||
Container mtusi2026lab4-evidence-postgres-1 Created
|
||||
Container mtusi2026lab4-evidence-redis-1 Created
|
||||
Container mtusi2026lab4-evidence-migrate-1 Creating
|
||||
Container mtusi2026lab4-evidence-s3-1 Created
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Creating
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Created
|
||||
Container mtusi2026lab4-evidence-migrate-1 Created
|
||||
Container mtusi2026lab4-evidence-api-1 Creating
|
||||
Container mtusi2026lab4-evidence-api-2 Creating
|
||||
Container mtusi2026lab4-evidence-api-2 Created
|
||||
Container mtusi2026lab4-evidence-lb-1 Creating
|
||||
Container mtusi2026lab4-evidence-api-1 Created
|
||||
Container mtusi2026lab4-evidence-lb-1 Created
|
||||
Container mtusi2026lab4-evidence-redis-1 Starting
|
||||
Container mtusi2026lab4-evidence-s3-1 Starting
|
||||
Container mtusi2026lab4-evidence-postgres-1 Starting
|
||||
Container mtusi2026lab4-evidence-redis-1 Started
|
||||
Container mtusi2026lab4-evidence-s3-1 Started
|
||||
Container mtusi2026lab4-evidence-s3-1 Waiting
|
||||
Container mtusi2026lab4-evidence-postgres-1 Started
|
||||
Container mtusi2026lab4-evidence-postgres-1 Waiting
|
||||
Container mtusi2026lab4-evidence-s3-1 Healthy
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Starting
|
||||
Container mtusi2026lab4-evidence-postgres-1 Healthy
|
||||
Container mtusi2026lab4-evidence-migrate-1 Starting
|
||||
Container mtusi2026lab4-evidence-migrate-1 Started
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Started
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Waiting
|
||||
Container mtusi2026lab4-evidence-migrate-1 Waiting
|
||||
Container mtusi2026lab4-evidence-migrate-1 Exited
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Exited
|
||||
Container mtusi2026lab4-evidence-api-1 Starting
|
||||
Container mtusi2026lab4-evidence-api-1 Started
|
||||
Container mtusi2026lab4-evidence-api-2 Starting
|
||||
Container mtusi2026lab4-evidence-api-2 Started
|
||||
Container mtusi2026lab4-evidence-lb-1 Starting
|
||||
Container mtusi2026lab4-evidence-lb-1 Started
|
||||
Container mtusi2026lab4-evidence-postgres-1 Waiting
|
||||
Container mtusi2026lab4-evidence-redis-1 Waiting
|
||||
Container mtusi2026lab4-evidence-migrate-1 Waiting
|
||||
Container mtusi2026lab4-evidence-lb-1 Waiting
|
||||
Container mtusi2026lab4-evidence-api-1 Waiting
|
||||
Container mtusi2026lab4-evidence-api-2 Waiting
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Waiting
|
||||
Container mtusi2026lab4-evidence-s3-1 Waiting
|
||||
Container mtusi2026lab4-evidence-migrate-1 Exited
|
||||
Container mtusi2026lab4-evidence-s3-init-1 Exited
|
||||
Container mtusi2026lab4-evidence-s3-1 Healthy
|
||||
Container mtusi2026lab4-evidence-postgres-1 Healthy
|
||||
Container mtusi2026lab4-evidence-redis-1 Healthy
|
||||
Container mtusi2026lab4-evidence-lb-1 Healthy
|
||||
Container mtusi2026lab4-evidence-api-1 Healthy
|
||||
Container mtusi2026lab4-evidence-api-2 Healthy
|
||||
@@ -0,0 +1,113 @@
|
||||
$ make test-storage
|
||||
./scripts/test-storage.sh
|
||||
=== RUN TestIdempotencyTransactionAndExpiry
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/empty
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/whitespace
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/invalid_UTF8
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/nul
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/too_long
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/duplicate
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/too_many
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/nil_id
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/unknown
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/foreign_chat
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/missing_object
|
||||
=== RUN TestIdempotencyTransactionAndExpiry/object_lock_cancellation_does_not_leak_a_pooled_lock
|
||||
=== NAME TestIdempotencyTransactionAndExpiry
|
||||
idempotency_test.go:295: rollback removed both side effect and key; expired key created one new message
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry (0.11s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules (0.02s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/empty (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/whitespace (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/invalid_UTF8 (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/nul (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/too_long (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/duplicate (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/too_many (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/nil_id (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/unknown (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/foreign_chat (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/service_owns_message_content_rules/missing_object (0.00s)
|
||||
--- PASS: TestIdempotencyTransactionAndExpiry/object_lock_cancellation_does_not_leak_a_pooled_lock (0.05s)
|
||||
PASS
|
||||
2026/11/13 07:54:01 INFO connected to database db=lab4_atomic_1794556440_46549
|
||||
2026/11/13 07:54:01 INFO successfully initialized migrator
|
||||
2026/11/13 07:54:01 INFO successfully loaded migrations error=<nil>
|
||||
2026/11/13 07:54:01 INFO postgres_pool_config max_conns=8 connect_timeout=500ms statement_timeout_ms=2000
|
||||
2026/11/13 07:54:01 INFO successfully connected to postgres pool
|
||||
2026/11/13 07:54:01 ERROR Query err="ERROR: synthetic failure (SQLSTATE P0001)" ts=187.375µs pid=244 sql="INSERT INTO message_idempotency(user_id,method,chat_id,key,request_hash,message_id,expires_at,hash_version)\n VALUES($1,'POST',$2,$3,$4,$5,clock_timestamp()+interval '24 hours',2)\n ON CONFLICT(user_id,method,chat_id,key) DO UPDATE SET request_hash=EXCLUDED.request_hash,\n message_id=EXCLUDED.message_id,expires_at=EXCLUDED.expires_at,hash_version=EXCLUDED.hash_version" args="[a6a209a3-9450-4393-ab7a-0c2859360754 877fa02a-7912-436c-83d1-9f0bc9cc0552 failure-key da0166d9297a757bea3229f89135b665155b9cf24402c98ce373c6388e9d0e1e 8ec1c403-6820-4c60-a2df-3fcbc2095228]"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 WARN chat_cache_read_error chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552 error="cache disabled for storage test"
|
||||
2026/11/13 07:54:01 INFO chat_db_lookup chat_id=877fa02a-7912-436c-83d1-9f0bc9cc0552
|
||||
2026/11/13 07:54:01 ERROR Query err="ERROR: insert or update on table \"message_attachments\" violates foreign key constraint \"message_attachments_attachment_id_chat_id_fkey\" (SQLSTATE 23503)" ts=127.375µs pid=244 sql="INSERT INTO message_attachments(message_id,chat_id,attachment_id,position) VALUES($1,$2,$3,$4)" args="[4ee5dcc7-106c-4641-b38e-06d84880cf1e 877fa02a-7912-436c-83d1-9f0bc9cc0552 7bd482b6-cd37-4061-93c0-7859e2869d25 1]"
|
||||
2026/11/13 07:54:01 ERROR Query sql="SELECT pg_advisory_lock(hashtextextended($1, 42))" args=[originals/bcaecf31-d3db-4b63-8eb3-6c93cea1c0b5] err="timeout: context deadline exceeded" ts=49.253584ms pid=245
|
||||
@@ -0,0 +1,242 @@
|
||||
[
|
||||
{
|
||||
"bytes": 111817,
|
||||
"trial": 1,
|
||||
"clients": 10,
|
||||
"warmup_uploads": 1,
|
||||
"duration_seconds": 0.045627042185515165,
|
||||
"successful": 10,
|
||||
"errors": 0,
|
||||
"error_rate": 0.0,
|
||||
"successful_rps": 219.16827216940695,
|
||||
"p50": 0.02541474997997284,
|
||||
"p95": 0.028515249956399202,
|
||||
"p99": 0.028515249956399202,
|
||||
"status_counts": {
|
||||
"201": 10,
|
||||
"429": 0
|
||||
},
|
||||
"memory": [
|
||||
{
|
||||
"container": "479285b815728028100c778d05babe85b3421168a85f9e9df93d69581016af4a",
|
||||
"memory_current_bytes": 13234176,
|
||||
"memory_peak_bytes": 13832192
|
||||
},
|
||||
{
|
||||
"container": "f444514998d92002c13af6d661d836a9fca8c0c0897e1f8178f392f331330021",
|
||||
"memory_current_bytes": 12759040,
|
||||
"memory_peak_bytes": 13574144
|
||||
}
|
||||
],
|
||||
"cpu": [
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-1",
|
||||
"cpu_percent": "0.25%"
|
||||
},
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-2",
|
||||
"cpu_percent": "2.55%"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"bytes": 111817,
|
||||
"trial": 2,
|
||||
"clients": 10,
|
||||
"warmup_uploads": 1,
|
||||
"duration_seconds": 0.029648167081177235,
|
||||
"successful": 9,
|
||||
"errors": 1,
|
||||
"error_rate": 0.1,
|
||||
"successful_rps": 303.5600809776143,
|
||||
"p50": 0.014442541170865297,
|
||||
"p95": 0.01693266723304987,
|
||||
"p99": 0.01693266723304987,
|
||||
"status_counts": {
|
||||
"201": 9,
|
||||
"429": 1
|
||||
},
|
||||
"memory": [
|
||||
{
|
||||
"container": "766c4aef25644649737f64d33e5df0792acdcdbde755f842a3dfbf0e0bc8d7a1",
|
||||
"memory_current_bytes": 12324864,
|
||||
"memory_peak_bytes": 13078528
|
||||
},
|
||||
{
|
||||
"container": "eb4099b7c393d646e33daa515aed5c70830cc2d7160b4aa47e14397a4b77b08f",
|
||||
"memory_current_bytes": 12996608,
|
||||
"memory_peak_bytes": 13590528
|
||||
}
|
||||
],
|
||||
"cpu": [
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-1",
|
||||
"cpu_percent": "0.13%"
|
||||
},
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-2",
|
||||
"cpu_percent": "0.13%"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"bytes": 111817,
|
||||
"trial": 3,
|
||||
"clients": 10,
|
||||
"warmup_uploads": 1,
|
||||
"duration_seconds": 0.02836104203015566,
|
||||
"successful": 8,
|
||||
"errors": 2,
|
||||
"error_rate": 0.2,
|
||||
"successful_rps": 282.077082763524,
|
||||
"p50": 0.01445808308199048,
|
||||
"p95": 0.016181250102818012,
|
||||
"p99": 0.016181250102818012,
|
||||
"status_counts": {
|
||||
"201": 8,
|
||||
"429": 2
|
||||
},
|
||||
"memory": [
|
||||
{
|
||||
"container": "4a08738b0bf7d79af7b748f93daf080a6785ef2c3544b0adcab6babad2b98250",
|
||||
"memory_current_bytes": 12472320,
|
||||
"memory_peak_bytes": 13578240
|
||||
},
|
||||
{
|
||||
"container": "2638f057e37683bb69ca2dd0186c48b850b59f49cb7ad996cc54f692cfd8dec0",
|
||||
"memory_current_bytes": 12480512,
|
||||
"memory_peak_bytes": 15990784
|
||||
}
|
||||
],
|
||||
"cpu": [
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-1",
|
||||
"cpu_percent": "0.13%"
|
||||
},
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-2",
|
||||
"cpu_percent": "0.14%"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"bytes": 5242880,
|
||||
"trial": 1,
|
||||
"clients": 10,
|
||||
"warmup_uploads": 1,
|
||||
"duration_seconds": 0.5475144158117473,
|
||||
"successful": 8,
|
||||
"errors": 2,
|
||||
"error_rate": 0.2,
|
||||
"successful_rps": 14.611487422005435,
|
||||
"p50": 0.34232541639357805,
|
||||
"p95": 0.4190103751607239,
|
||||
"p99": 0.4190103751607239,
|
||||
"status_counts": {
|
||||
"201": 8,
|
||||
"429": 2
|
||||
},
|
||||
"memory": [
|
||||
{
|
||||
"container": "664f6dfcafa5a0e7a2214d767bf12751b4608ef0ee118f7880a76a57224d22e2",
|
||||
"memory_current_bytes": 12546048,
|
||||
"memory_peak_bytes": 36929536
|
||||
},
|
||||
{
|
||||
"container": "66031c7c8e3e485219b19559e8633bda3861ad246e82bae4e47a59c87c6fb8e6",
|
||||
"memory_current_bytes": 12845056,
|
||||
"memory_peak_bytes": 36102144
|
||||
}
|
||||
],
|
||||
"cpu": [
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-1",
|
||||
"cpu_percent": "0.10%"
|
||||
},
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-2",
|
||||
"cpu_percent": "8.72%"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"bytes": 5242880,
|
||||
"trial": 2,
|
||||
"clients": 10,
|
||||
"warmup_uploads": 1,
|
||||
"duration_seconds": 0.5503349169157445,
|
||||
"successful": 8,
|
||||
"errors": 2,
|
||||
"error_rate": 0.2,
|
||||
"successful_rps": 14.536602628876604,
|
||||
"p50": 0.2895928327925503,
|
||||
"p95": 0.3782998751848936,
|
||||
"p99": 0.3782998751848936,
|
||||
"status_counts": {
|
||||
"201": 8,
|
||||
"429": 2
|
||||
},
|
||||
"memory": [
|
||||
{
|
||||
"container": "cce4850d2fa0564dea54b6b5a1e8197d376019934260aa871c82b442f83d7690",
|
||||
"memory_current_bytes": 12611584,
|
||||
"memory_peak_bytes": 36061184
|
||||
},
|
||||
{
|
||||
"container": "01766d6a85e6156b645c500303479cfaf7d373901a8b7c1559df35ca4362cdfc",
|
||||
"memory_current_bytes": 12718080,
|
||||
"memory_peak_bytes": 35934208
|
||||
}
|
||||
],
|
||||
"cpu": [
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-1",
|
||||
"cpu_percent": "0.09%"
|
||||
},
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-2",
|
||||
"cpu_percent": "8.52%"
|
||||
}
|
||||
]
|
||||
},
|
||||
{
|
||||
"bytes": 5242880,
|
||||
"trial": 3,
|
||||
"clients": 10,
|
||||
"warmup_uploads": 1,
|
||||
"duration_seconds": 0.5343151250854135,
|
||||
"successful": 8,
|
||||
"errors": 2,
|
||||
"error_rate": 0.2,
|
||||
"successful_rps": 14.972437845028535,
|
||||
"p50": 0.29230537498369813,
|
||||
"p95": 0.3612105417996645,
|
||||
"p99": 0.3612105417996645,
|
||||
"status_counts": {
|
||||
"201": 8,
|
||||
"429": 2
|
||||
},
|
||||
"memory": [
|
||||
{
|
||||
"container": "bd0efe3d0c1c9e25a4dd507e11cd39eea552da4cc7288c3baefb89e6b2e90979",
|
||||
"memory_current_bytes": 12541952,
|
||||
"memory_peak_bytes": 37253120
|
||||
},
|
||||
{
|
||||
"container": "73970c1453db19ecf2b51ed020b1f9db82c460927f11ff2711b4627527203a3f",
|
||||
"memory_current_bytes": 12967936,
|
||||
"memory_peak_bytes": 37167104
|
||||
}
|
||||
],
|
||||
"cpu": [
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-1",
|
||||
"cpu_percent": "0.14%"
|
||||
},
|
||||
{
|
||||
"container": "mtusi2026lab4-evidence-api-2",
|
||||
"cpu_percent": "8.36%"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
@@ -0,0 +1,3 @@
|
||||
$ make validate PYTHON=/Users/a7tech173/development/gitea.privcloud.tech/mtusi2026/mtusi2026lab4/.venv/bin/python
|
||||
/Users/a7tech173/development/gitea.privcloud.tech/mtusi2026/mtusi2026lab4/.venv/bin/python scripts/validate_openapi.py
|
||||
PASS: full OpenAPI 3.1 and JSON Schema validation; application NOT tested.
|
||||
@@ -5,3 +5,11 @@
|
||||
В REPORT.md сопоставьте роль → service name → версия/digest → health/readiness → volume → сеть/порт → способ передачи credentials. Приведите bootstrap (schema, bucket, broker topology, а с lab6 provisioning) и безопасное повторение этих команд.
|
||||
|
||||
Приложение строится из исходников. Не используйте заранее существующий образ преподавательского сервера вместо своей реализации. Restart hook перезапускает все соответствующие stores с volumes; failover hook останавливает только заданную API-реплику.
|
||||
|
||||
## Реализованный стенд lab4
|
||||
|
||||
HAProxy публикует только `127.0.0.1:18081`; API-порты не публикуются. Две API-реплики используют общие PostgreSQL, Redis и SeaweedFS 4.52. SeaweedFS подключён только к отдельной internal-сети `storage`, его gateway/master/filer не опубликованы на хосте. Все данные и метаданные SeaweedFS находятся в `s3_data`, PostgreSQL — в `postgres_data`, Redis AOF — в `redis_data`.
|
||||
|
||||
Bootstrap генерирует отдельные admin/API-ключи вне Git; в API монтируются только API-ключи и JWT-файлы. `s3-init` создаёт bucket и проверяет PUT/DELETE служебного объекта. После restart хранилища эта проверка повторяется до запуска API: одного успешного HEAD bucket недостаточно для проверки пути записи после выбора лидера master.
|
||||
|
||||
`operator` запускается только явно через tools profile/Compose run. Reconcile по умолчанию read-only, `--apply` включает изменения. Периодический запуск очистки организует оператор; подробности и ограничения — в REPORT.md. Триггеров проверки содержимого сообщения в миграциях нет: это правило сервиса Go.
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
global
|
||||
maxconn 4096
|
||||
stats socket /tmp/haproxy.sock mode 600 level admin
|
||||
stats socket ipv4@127.0.0.1:9999 level admin
|
||||
|
||||
defaults
|
||||
mode http
|
||||
timeout connect 500ms
|
||||
timeout client 125s
|
||||
timeout server 125s
|
||||
timeout check 500ms
|
||||
retries 0
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 2
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold obsolete 1s
|
||||
hold nx 1s
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
http-request deny deny_status 404 if { path_beg /internal }
|
||||
default_backend api
|
||||
|
||||
backend api
|
||||
balance roundrobin
|
||||
http-reuse always
|
||||
option httpchk
|
||||
http-check send meth GET uri /health/ready ver HTTP/1.1 hdr Host api
|
||||
http-check expect status 200
|
||||
server-template replica 1-4 api:8080 check inter 1s fall 2 rise 1 resolvers docker init-addr none resolve-prefer ipv4
|
||||
|
||||
listen statistics
|
||||
bind :8404
|
||||
stats enable
|
||||
stats uri /stats
|
||||
@@ -0,0 +1,306 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Repeatable lab4 acceptance on an isolated synthetic Compose project.
|
||||
No tokens, request headers, passwords, or filled-in configuration are persisted.
|
||||
"""
|
||||
import concurrent.futures as futures
|
||||
import datetime
|
||||
import hashlib
|
||||
import http.client
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import platform
|
||||
import select
|
||||
import struct
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
import uuid
|
||||
import zlib
|
||||
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0,str(ROOT/'tests'))
|
||||
from client import Client, check
|
||||
OUT=ROOT/'evidence'
|
||||
ENV=dict(os.environ,COMPOSE_PROJECT_NAME='mtusi2026lab4-evidence',HOST_PORT='18082',UPLOAD_TIMEOUT='5s',RECONCILE_GRACE_PERIOD='15s')
|
||||
COMPOSE=['./scripts/compose.sh']
|
||||
BASE='http://localhost:18082'
|
||||
RUN_ID=uuid.uuid4().hex
|
||||
|
||||
def save(name,value):
|
||||
temporary=OUT/(name+'.json.tmp')
|
||||
temporary.write_text(json.dumps(value,indent=2)+'\n')
|
||||
temporary.replace(OUT/(name+'.json'))
|
||||
|
||||
def stage(name):
|
||||
save('acceptance',{'passed':False,'run_id':RUN_ID,'stage':name})
|
||||
print(name,flush=True)
|
||||
|
||||
def command(args, name=None, expected=0, **kw):
|
||||
p=subprocess.run(args,cwd=ROOT,env=ENV,text=True,capture_output=True,timeout=kw.pop('timeout',240),**kw)
|
||||
if name:
|
||||
(OUT/(name+'.txt')).write_text('$ '+' '.join(args)+'\n'+p.stdout+p.stderr)
|
||||
check(p.returncode==expected,'command failed: '+' '.join(args)+' (see evidence/'+str(name)+'.txt)')
|
||||
return p.stdout
|
||||
|
||||
def compose(*args,**kw): return command(COMPOSE+list(args),**kw)
|
||||
def operator(*args,grace=None):
|
||||
options=['-e','RECONCILE_GRACE_PERIOD='+grace] if grace else []
|
||||
output=compose('run','--rm','-T',*options,'operator','storage',*args)
|
||||
return json.loads(output)
|
||||
def sql(query): return compose('exec','-T','postgres','psql','-U','messenger','-d','messenger','-Atc',query).strip()
|
||||
def snapshot():
|
||||
ids=compose('ps','-q','api','postgres','redis','s3','lb').split()
|
||||
results=[]
|
||||
for cid in ids:
|
||||
info=json.loads(command(['docker','inspect','--format','{{json .State}}',cid]))
|
||||
name=command(['docker','inspect','--format','{{.Name}}',cid]).strip().lstrip('/')
|
||||
results.append({'name':name,'id':cid,'started_at':info['StartedAt'],'running':info['Running'],'health':info.get('Health',{}).get('Status')})
|
||||
return results
|
||||
|
||||
def ready(c):
|
||||
deadline=time.monotonic()+90
|
||||
while time.monotonic()<deadline:
|
||||
try: c.call('GET','/health/ready',schema='Health'); return
|
||||
except Exception: time.sleep(.5)
|
||||
raise AssertionError('API did not recover')
|
||||
|
||||
def padded_png(data,size):
|
||||
padding=b'\0'*(size-len(data)-12)
|
||||
chunk=b'paDd'+padding
|
||||
return data[:-12]+struct.pack('>I',len(padding))+chunk+struct.pack('>I',zlib.crc32(chunk))+data[-12:]
|
||||
|
||||
def upload(c,actor,chat,data):
|
||||
return c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',actor=actor,raw=data,expected=(201,429),headers={'Content-Type':'image/png'},schema='Attachment')
|
||||
|
||||
def direct_api(container,path,actor=None,body=b'',content_type='image/png'):
|
||||
# Feed credentials via stdin, never argv or evidence. Only controlled results
|
||||
# are returned to the report, not the raw HTTP request.
|
||||
method='POST' if body else 'GET'
|
||||
headers=[f'{method} {path} HTTP/1.1','Host: api','Connection: close']
|
||||
if actor: headers.append('Authorization: Bearer '+actor['token'])
|
||||
if body: headers.extend(['Content-Type: '+content_type,'Content-Length: '+str(len(body))])
|
||||
raw=('\r\n'.join(headers)+'\r\n\r\n').encode()+body
|
||||
p=subprocess.Popen(['docker','exec','-i',container,'nc','-w','10','127.0.0.1','8080'],stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
|
||||
try:
|
||||
# BusyBox nc closes the socket on stdin EOF. Keep stdin open until the
|
||||
# response starts so the probe itself does not cancel the server context.
|
||||
p.stdin.write(raw);p.stdin.flush()
|
||||
readable,_,_=select.select([p.stdout],[],[],15)
|
||||
check(bool(readable),'internal API probe timed out')
|
||||
line=p.stdout.readline()
|
||||
return int(line.split(b' ',2)[1])
|
||||
finally:
|
||||
p.stdin.close()
|
||||
try: p.wait(timeout=3)
|
||||
except subprocess.TimeoutExpired: p.kill();p.wait()
|
||||
|
||||
def failures(c,actor,chat,data):
|
||||
findings={}
|
||||
att=upload(c,actor,chat,data)['json']
|
||||
c.message(actor,chat,'',attachments=[att['id']])
|
||||
key=sql("SELECT object_key FROM attachments WHERE id='"+att['id']+"'")
|
||||
raw=compose('exec','-T','api','/usr/local/bin/messenger','storage','private-check','--key',key)
|
||||
findings['private_bucket']=json.loads(raw)
|
||||
save('failures',findings)
|
||||
before=snapshot()
|
||||
command([sys.executable,'tests/resilience.py','--mode','persistence','--action-script','scripts/restart.sh','--base-url',BASE],name='persistence')
|
||||
after=snapshot()
|
||||
after_by_name={x['name']:x for x in after}
|
||||
check(all(old['name'] in after_by_name and after_by_name[old['name']]['running'] and
|
||||
(after_by_name[old['name']]['id']!=old['id'] or after_by_name[old['name']]['started_at']!=old['started_at'])
|
||||
for old in before),'restart hook did not restart or replace every store/API/LB')
|
||||
findings['restart']={'before':before,'after':after}
|
||||
save('failures',findings)
|
||||
findings['failover']=[]
|
||||
for target in compose('ps','-q','api').split():
|
||||
hostname=command(['docker','inspect','--format','{{.Config.Hostname}}',target]).strip()
|
||||
for _ in range(20):
|
||||
response=upload(c,actor,chat,data)
|
||||
if response['headers']['X-Instance-Id'].startswith(hostname+'-'):break
|
||||
else: raise AssertionError('cannot route upload to intended replica')
|
||||
attachment=response['json'];alias=response['headers']['X-Instance-Id']
|
||||
stop_env=dict(ENV,TARGET_INSTANCE_ID=alias)
|
||||
p=subprocess.run(['./scripts/stop-one.sh'],cwd=ROOT,env=stop_env,capture_output=True,timeout=15)
|
||||
check(p.returncode==0,'targeted stop failed')
|
||||
deadline=time.monotonic()+10
|
||||
while True:
|
||||
try:
|
||||
downloaded=c.call('GET','/api/v1/attachments/'+attachment['id']+'/content',actor=actor)
|
||||
check(downloaded['bytes']==data,'failover changed original')
|
||||
check(downloaded['headers']['X-Instance-Id']!=alias,'stopped uploader served download')
|
||||
break
|
||||
except Exception:
|
||||
check(time.monotonic()<deadline,'download did not recover');time.sleep(.3)
|
||||
check(command(['docker','inspect','--format','{{.State.Running}}',target]).strip()=='false','target not stopped')
|
||||
findings['failover'].append({'stopped_container':target,'upload_instance':alias,'download_instance':downloaded['headers']['X-Instance-Id'],'sha256':hashlib.sha256(data).hexdigest()})
|
||||
save('failures',findings)
|
||||
compose('up','-d','--no-deps','--wait','api');ready(c)
|
||||
command([sys.executable,'tests/resilience.py','--mode','failover','--action-script','scripts/stop-one.sh','--base-url',BASE],name='failover')
|
||||
compose('up','-d','--no-deps','--wait','api');ready(c)
|
||||
api=compose('ps','-q','api').split()[0]
|
||||
try:
|
||||
compose('stop','s3')
|
||||
check(direct_api(api,'/health/live')==200,'S3 outage killed liveness')
|
||||
check(direct_api(api,'/health/ready')==503,'S3 outage did not fail readiness')
|
||||
status=direct_api(api,'/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',actor,data)
|
||||
check(status==503,'S3 failure upload must return 503')
|
||||
blocked=compose('run','--rm','-T','operator','storage','reconcile','--apply',expected=1,name='reconcile-s3-down')
|
||||
scan=json.loads(blocked)
|
||||
check(scan['deleted']==0 and scan['marked_missing']==0,'S3 outage caused reconciliation writes')
|
||||
findings['s3_outage']={'live':200,'ready':503,'upload':status,'reconciler':scan}
|
||||
save('failures',findings)
|
||||
finally:
|
||||
compose('up','-d','--no-deps','--wait','s3')
|
||||
compose('run','--rm','-T','s3-init')
|
||||
ready(c)
|
||||
# A client disconnect before all bytes are received cannot create a ready row.
|
||||
count_before=sql('SELECT count(*) FROM attachments')
|
||||
conn=http.client.HTTPConnection('localhost',18082,timeout=10)
|
||||
conn.putrequest('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png')
|
||||
conn.putheader('Authorization','Bearer '+actor['token']);conn.putheader('Content-Type','image/png');conn.putheader('Content-Length',len(data)*2);conn.endheaders();conn.send(data[:100]);conn.close();time.sleep(.5)
|
||||
check(sql('SELECT count(*) FROM attachments')==count_before,'interrupted body created metadata')
|
||||
findings['interrupted_upload']={'metadata_unchanged':True}
|
||||
save('failures',findings)
|
||||
# Real database-side timeout after S3 PUT, without production fault switches.
|
||||
lock=subprocess.Popen(COMPOSE+['exec','-T','postgres','psql','-U','messenger','-d','messenger','-Atc',"BEGIN; LOCK TABLE attachments IN ACCESS EXCLUSIVE MODE; SELECT pg_sleep(4); COMMIT;"],cwd=ROOT,env=ENV,stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True)
|
||||
try:
|
||||
deadline=time.monotonic()+3
|
||||
while sql("SELECT EXISTS(SELECT 1 FROM pg_locks WHERE relation='attachments'::regclass AND mode='AccessExclusiveLock' AND granted)")!='t':
|
||||
check(lock.poll() is None and time.monotonic()<deadline,'lock session failed')
|
||||
time.sleep(.05)
|
||||
failed=c.call('POST','/api/v1/chats/'+chat['id']+'/attachments?filename=sample.png',503,actor=actor,raw=data,headers={'Content-Type':'image/png'})
|
||||
check(failed['status']==503,'database timeout not reported')
|
||||
finally:
|
||||
lock.communicate(timeout=10)
|
||||
check(sql('SELECT count(*) FROM attachments')==count_before,'failed metadata write committed')
|
||||
findings['s3_put_db_timeout']={'upload_status':503,'metadata_unchanged':True}
|
||||
save('failures',findings)
|
||||
findings['dry_run_after_failed_commit']=operator('reconcile')
|
||||
check(findings['dry_run_after_failed_commit']['orphans']>=1,'failed DB write did not leave detectable orphan')
|
||||
# Missing objects retain IDs/references and can only be restored from originals.
|
||||
operator('remove-object','--id',att['id'])
|
||||
dry=operator('reconcile');check(dry['missing']>=1,'missing object not detected')
|
||||
apply=operator('reconcile','--apply');check(apply['missing']>=1,'missing object not marked')
|
||||
c.call('GET','/api/v1/attachments/'+att['id'],503,actor=actor)
|
||||
bad=compose('run','--rm','-T','operator','storage','restore','--id',att['id'],'--source','/fixtures/README.md',expected=1)
|
||||
operator('restore','--id',att['id'],'--source','/fixtures/sample.png')
|
||||
operator('restore','--id',att['id'],'--source','/fixtures/sample.png')
|
||||
downloaded=c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=actor)
|
||||
check(downloaded['bytes']==data,'restored bytes changed')
|
||||
history=c.call('GET','/api/v1/chats/'+chat['id']+'/messages',actor=actor,schema='MessagePage')['json']
|
||||
check(any(att['id'] in m['attachment_ids'] for m in history['items']),'restoration lost message reference')
|
||||
findings['missing_repair']={'dry_run':dry,'apply':apply,'wrong_original_rejected':True,'restored_sha256':hashlib.sha256(data).hexdigest(),'reference_preserved':True,'repeat_safe':True}
|
||||
save('failures',findings)
|
||||
count_before=sql('SELECT count(*) FROM attachments')
|
||||
try:
|
||||
compose('stop','postgres')
|
||||
raw=compose('run','--rm','-T','operator','storage','reconcile','--apply',expected=1,name='reconcile-db-down')
|
||||
scan=json.loads(raw)
|
||||
check(scan['deleted']==0 and scan['marked_missing']==0,'DB outage caused reconciliation writes')
|
||||
findings['database_outage']={'reconciler':scan}
|
||||
finally:
|
||||
compose('up','-d','--no-deps','--wait','postgres');ready(c)
|
||||
check(sql('SELECT count(*) FROM attachments')==count_before,'DB restart lost attachment metadata')
|
||||
save('failures',findings)
|
||||
return findings
|
||||
|
||||
def measurements(c,actor,chat,data):
|
||||
records=[]
|
||||
for size in [len(data),5*1024*1024]:
|
||||
body=data if size==len(data) else padded_png(data,size)
|
||||
for trial in range(1,4):
|
||||
compose('up','-d','--no-deps','--force-recreate','--wait','api');ready(c)
|
||||
upload(c,actor,chat,data) # One documented warm-up upload.
|
||||
ids=compose('ps','-q','api').split()
|
||||
cpu=subprocess.Popen(['docker','stats','--no-stream','--format','{{json .}}',*ids],stdout=subprocess.PIPE,stderr=subprocess.PIPE,text=True)
|
||||
barrier=threading.Barrier(10)
|
||||
def work(_): barrier.wait();return upload(Client(BASE,timeout=30),actor,chat,body)
|
||||
start=time.monotonic()
|
||||
with futures.ThreadPoolExecutor(max_workers=10) as pool: results=list(pool.map(work,range(10)))
|
||||
duration=time.monotonic()-start
|
||||
successful=[x['elapsed'] for x in results if x['status']==201]
|
||||
rss=[]
|
||||
for cid in ids:
|
||||
values=command(['docker','exec',cid,'sh','-c','cat /sys/fs/cgroup/memory.current /sys/fs/cgroup/memory.peak']).split()
|
||||
rss.append({'container':cid,'memory_current_bytes':int(values[0]),'memory_peak_bytes':int(values[1])})
|
||||
stats,_=cpu.communicate(timeout=15)
|
||||
cpu_samples=[{'container':v.get('Name'),'cpu_percent':v.get('CPUPerc')} for v in [json.loads(line) for line in stats.splitlines()]]
|
||||
def quantile(p): return sorted(successful)[min(len(successful)-1,int((len(successful)-1)*p))] if successful else None
|
||||
records.append({'bytes':len(body),'trial':trial,'clients':10,'warmup_uploads':1,'duration_seconds':duration,'successful':len(successful),'errors':10-len(successful),'error_rate':(10-len(successful))/10,'successful_rps':len(successful)/duration,'p50':quantile(.5),'p95':quantile(.95),'p99':quantile(.99),'status_counts':{str(code):sum(x['status']==code for x in results) for code in [201,429]},'memory':rss,'cpu':cpu_samples})
|
||||
save('uploads',records)
|
||||
return records
|
||||
|
||||
def research(c,actor,chat,data):
|
||||
results=[]
|
||||
# Demonstrate freshness independently of the duration of mass seeding/scanning.
|
||||
fresh_key=operator('seed')['keys'][0]
|
||||
invalid_key='originals/not-a-server-uuid-'+uuid.uuid4().hex
|
||||
outside_key='unmanaged/'+uuid.uuid4().hex
|
||||
operator('seed','--key',invalid_key)
|
||||
operator('seed','--key',outside_key)
|
||||
fresh=operator('reconcile','--apply',grace='1h')
|
||||
check(operator('object-check','--key',fresh_key)['exists'],'fresh object deleted')
|
||||
# Upload and reconciliation execute concurrently; every acknowledged upload
|
||||
# must remain downloadable. The separate PostgreSQL test checks lock contention.
|
||||
with futures.ThreadPoolExecutor(max_workers=2) as workers:
|
||||
scan=workers.submit(operator,'reconcile','--apply')
|
||||
concurrent_upload=upload(c,actor,chat,data)
|
||||
scan_result=scan.result()
|
||||
check(concurrent_upload['status']==201,'concurrent upload failed')
|
||||
att=concurrent_upload['json']
|
||||
check(c.call('GET','/api/v1/attachments/'+att['id']+'/content',actor=actor)['bytes']==data,'concurrent reconciliation damaged upload')
|
||||
safety={'fresh_apply':fresh,'fresh_key_preserved':True,'concurrent_scan':scan_result,'concurrent_upload_sha256':hashlib.sha256(data).hexdigest()}
|
||||
save('reconciliation-safety',safety)
|
||||
for count in [100,1000]:
|
||||
operator('seed','--count',str(count))
|
||||
# All objects exist before the grace wait starts. Duration of seeding is
|
||||
# intentionally excluded from the scan timing, but dataset size is recorded.
|
||||
time.sleep(16)
|
||||
scans=[operator('reconcile') for _ in range(3)]
|
||||
applied=operator('reconcile','--apply')
|
||||
repeated=operator('reconcile','--apply')
|
||||
check(applied['deleted']>=count,'old orphans not cleaned')
|
||||
check(repeated['deleted']==0 and repeated['missing']==0,'repeat was not safe or valid original deleted')
|
||||
check(operator('object-check','--key',invalid_key)['exists'],'invalid managed key deleted')
|
||||
check(operator('object-check','--key',outside_key)['exists'],'object outside prefix deleted')
|
||||
results.append({'seeded':count,'dry_runs':scans,'apply':applied,'repeat_apply':repeated,'invalid_key_preserved':True,'outside_prefix_preserved':True})
|
||||
save('reconciliation',results)
|
||||
return results
|
||||
|
||||
def main():
|
||||
OUT.mkdir(exist_ok=True)
|
||||
stage("bootstrap")
|
||||
command(['./scripts/bootstrap.sh'],name='bootstrap')
|
||||
before_keys=hashlib.sha256((ROOT/'secrets/lab4/s3.json').read_bytes()).hexdigest()
|
||||
command(['./scripts/bootstrap.sh'],name='bootstrap-repeat')
|
||||
check(before_keys==hashlib.sha256((ROOT/'secrets/lab4/s3.json').read_bytes()).hexdigest(),'bootstrap rotated credentials')
|
||||
compose('config','--quiet',name='compose-config')
|
||||
compose('up','-d','--build','--wait','--wait-timeout','120',name='stand-up',timeout=600)
|
||||
meta={'run_id':RUN_ID,'date_utc':datetime.datetime.now(datetime.timezone.utc).isoformat(),'git_head':command(['git','rev-parse','HEAD']).strip(),'working_tree_dirty':bool(command(['git','status','--porcelain']).strip()),'host':platform.platform(),'docker':json.loads(command(['docker','info','--format','{{json .}}'])),'project':ENV['COMPOSE_PROJECT_NAME'],'limits':{'api_replicas':2,'api_cpu_each':1,'api_ram_each':536870912,'upload_timeout':'5s','grace':'15s','upload_slots_each':4,'decode_slots_each':1}}
|
||||
# Keep a small allow-list; docker info can contain unrelated host settings.
|
||||
meta['docker']={k:meta['docker'][k] for k in ['ServerVersion','Architecture','NCPU','MemTotal']}
|
||||
(OUT/'environment.json').write_text(json.dumps(meta,indent=2)+'\n')
|
||||
compose('run','--rm','-T','s3-init',name='bucket-bootstrap-repeat')
|
||||
c=Client(BASE,timeout=20);ready(c)
|
||||
command(['make','check','build','vet'],name='build-checks')
|
||||
validator=str(ROOT/'.venv/bin/python') if (ROOT/'.venv/bin/python').exists() else sys.executable
|
||||
command(['make','validate','PYTHON='+validator],name='validate')
|
||||
command(['make','test-go','BASE_URL='+BASE],name='go-tests')
|
||||
command(['make','test-storage'],name='storage-tests')
|
||||
command(['make','test','BASE_URL='+BASE],name='smoke')
|
||||
actor,bob,eve,chat=c.bootstrap();data=(ROOT/'tests/fixtures/sample.png').read_bytes()
|
||||
stage('faults')
|
||||
findings=failures(c,actor,chat,data)
|
||||
(OUT/'failures.json').write_text(json.dumps(findings,indent=2)+'\n')
|
||||
stage('uploads')
|
||||
(OUT/'uploads.json').write_text(json.dumps(measurements(c,actor,chat,data),indent=2)+'\n')
|
||||
stage('reconciliation')
|
||||
(OUT/'reconciliation.json').write_text(json.dumps(research(c,actor,chat,data),indent=2)+'\n')
|
||||
result={'run_id':RUN_ID,'passed':True,'checks':['contract','go_race','transaction_rollback','smoke','private_bucket','persistence','both_uploaders_failover','public_failover','s3_down','interrupted_body','s3_put_db_timeout','missing_restore','10_client_memory','reconciler_grace','reconciler_repeat'],'remaining_metadata':int(sql('SELECT count(*) FROM attachments')),'timestamp_utc':datetime.datetime.now(datetime.timezone.utc).isoformat()}
|
||||
compose('down',name='stand-down')
|
||||
save('acceptance',result) # Preserve all experiment volumes.
|
||||
print('PASS: lab4 acceptance; summaries in evidence/. Isolated stand stopped; volumes preserved.',flush=True)
|
||||
|
||||
if __name__=='__main__': main()
|
||||
@@ -0,0 +1,25 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Generate local synthetic storage credentials without rotating existing keys."""
|
||||
import json
|
||||
import os
|
||||
from pathlib import Path
|
||||
import secrets
|
||||
|
||||
root=Path(__file__).resolve().parents[1]/'secrets'/'lab4'
|
||||
root.mkdir(parents=True,exist_ok=True)
|
||||
for name in ('s3_admin_access','s3_admin_secret','s3_api_access','s3_api_secret'):
|
||||
p=root/name
|
||||
if not p.exists():
|
||||
p.write_text(secrets.token_hex(24)+'\n')
|
||||
p.chmod(0o444) # Non-root containers must read these local synthetic secrets.
|
||||
bucket=os.environ.get('S3_BUCKET','messenger-attachments')
|
||||
identities=[]
|
||||
for role,actions in [('admin',['Admin','Read','Write','List','Tagging']),('api',['Read:'+bucket,'Write:'+bucket,'List:'+bucket])]:
|
||||
identities.append({'name':role,'credentials':[{'accessKey':(root/('s3_'+role+'_access')).read_text().strip(),'secretKey':(root/('s3_'+role+'_secret')).read_text().strip()}],'actions':actions})
|
||||
p=root/'s3.json'
|
||||
content=json.dumps({'identities':identities})+'\n'
|
||||
if not p.exists() or p.read_text()!=content:
|
||||
if p.exists(): p.chmod(0o600)
|
||||
p.write_text(content)
|
||||
p.chmod(0o444)
|
||||
print('Local storage credentials ready; existing keys preserved.')
|
||||
Executable
+23
@@ -0,0 +1,23 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
umask 077
|
||||
mkdir -p secrets/lab4
|
||||
if [ ! -f .env.lab4 ]; then
|
||||
{ printf 'POSTGRES_PASSWORD='; openssl rand -hex 24
|
||||
printf 'REDIS_PASSWORD='; openssl rand -hex 24
|
||||
printf 'HOST_PORT=18081\nAPI_REPLICAS=2\n'
|
||||
} > .env.lab4
|
||||
fi
|
||||
if [ ! -f secrets/lab4/private_key.pem ]; then
|
||||
openssl genrsa -out secrets/lab4/private_key.pem 2048 2>/dev/null
|
||||
fi
|
||||
if [ ! -f secrets/lab4/public_key.pem ]; then
|
||||
openssl rsa -in secrets/lab4/private_key.pem -pubout -out secrets/lab4/public_key.pem 2>/dev/null
|
||||
fi
|
||||
# Only synthetic stand keys, readable by the non-root application inside containers.
|
||||
chmod 755 secrets secrets/lab4
|
||||
chmod 444 secrets/lab4/*.pem
|
||||
printf '%s\n' 'Local lab4 settings and synthetic keys ready; existing files preserved.'
|
||||
|
||||
python3 scripts/bootstrap-storage.py
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec docker compose --env-file .env.lab4 "$@"
|
||||
@@ -0,0 +1,17 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Convert a temporary generation view to the oapi-codegen 3.0 subset.
|
||||
Runtime handlers enforce anyOf and required fields; normative contract is untouched.
|
||||
"""
|
||||
import json
|
||||
from pathlib import Path
|
||||
s=json.loads(Path('contracts/openapi.json').read_text())
|
||||
for schema in s['components']['schemas'].values():
|
||||
schema.pop('anyOf',None)
|
||||
for prop in schema.get('properties',{}).values():
|
||||
if prop.get('type')==['string','null']:
|
||||
prop.update(type='string',nullable=True)
|
||||
# Preserve pointers so handlers can distinguish missing fields from empty values.
|
||||
for name,field in [('CreateUser','password'),('CreateMessage','text')]:
|
||||
s['components']['schemas'][name]['properties'][field]['x-go-type-skip-optional-pointer']=False
|
||||
s['components']['schemas'][name]['required'].remove(field)
|
||||
print(json.dumps(s))
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
./scripts/compose.sh stop api lb
|
||||
./scripts/compose.sh restart postgres redis s3
|
||||
./scripts/compose.sh run --rm -T s3-init
|
||||
./scripts/compose.sh up -d --wait --wait-timeout 90 api lb
|
||||
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Drain and replace API processes individually, retaining other container IDs."""
|
||||
import json,os,subprocess,time
|
||||
from pathlib import Path
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
def run(args,input=None):
|
||||
p=subprocess.run(args,cwd=ROOT,input=input,text=True,capture_output=True,timeout=100)
|
||||
if p.returncode:raise RuntimeError(p.stderr.strip()[:300])
|
||||
return p.stdout.strip()
|
||||
def runtime(lb,cmd):return run(['docker','exec','-i',lb,'nc','-w','1','127.0.0.1','9999'],cmd+'\n')
|
||||
def ids():return run(['./scripts/compose.sh','ps','-q','api']).split()
|
||||
def states(lb):
|
||||
lines=runtime(lb,'show servers state').splitlines();head=lines[1].lstrip('# ').split()
|
||||
return [dict(zip(head,l.split())) for l in lines[2:] if l.strip()]
|
||||
def main():
|
||||
initial=ids()
|
||||
if len(initial)<2:raise SystemExit('rolling update requires at least two running API replicas')
|
||||
lb=run(['./scripts/compose.sh','ps','-q','lb'])
|
||||
for old in initial:
|
||||
info=json.loads(run(['docker','inspect',old]))[0]
|
||||
ip=next(iter(info['NetworkSettings']['Networks'].values()))['IPAddress']
|
||||
backend=next(s['srv_name'] for s in states(lb) if s['srv_addr']==ip and s['be_name']=='api')
|
||||
runtime(lb,f'set server api/{backend} state drain')
|
||||
try:
|
||||
deadline=time.monotonic()+10
|
||||
while True:
|
||||
# Existing transactions get a chance to finish before SIGTERM.
|
||||
import csv,io
|
||||
raw=run(['docker','exec',lb,'wget','-qO-','http://127.0.0.1:8404/stats;csv'])
|
||||
row=next(r for r in csv.DictReader(io.StringIO(raw)) if r['# pxname']=='api' and r['svname']==backend)
|
||||
if int(row['scur'])==0:break
|
||||
if time.monotonic()>deadline:raise RuntimeError('backend did not drain')
|
||||
time.sleep(.1)
|
||||
before=set(ids());run(['docker','stop','--time','8',old]);run(['docker','rm',old])
|
||||
run(['./scripts/compose.sh','up','-d','--no-deps','--no-recreate','--scale',f'api={len(initial)}','api'])
|
||||
after=set(ids())
|
||||
if not (before-{old}).issubset(after):raise RuntimeError('another replica was replaced unexpectedly')
|
||||
new=(after-before).pop();deadline=time.monotonic()+60
|
||||
while run(['docker','inspect','--format','{{.State.Health.Status}}',new])!='healthy':
|
||||
if time.monotonic()>deadline:raise RuntimeError('replacement failed readiness')
|
||||
time.sleep(.5)
|
||||
print(json.dumps({'old_container':old,'new_container':new,'name':info['Name'],'drained_backend':backend}),flush=True)
|
||||
finally:runtime(lb,f'set server api/{backend} state ready')
|
||||
time.sleep(3) # allow DNS refresh and a successful active readiness check
|
||||
if __name__=='__main__':main()
|
||||
Executable
+4
@@ -0,0 +1,4 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
exec python3 scripts/rolling-update.py
|
||||
Executable
+17
@@ -0,0 +1,17 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
: "${TARGET_INSTANCE_ID:?missing target alias}"
|
||||
for id in $(./scripts/compose.sh ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$id")
|
||||
case "$TARGET_INSTANCE_ID" in
|
||||
"$hostname"-*)
|
||||
current=$(docker exec "$id" wget -T 2 -qO- http://127.0.0.1:8080/internal/metrics | python3 -c 'import json,sys; print(json.load(sys.stdin)["instance_id"])')
|
||||
[ "$current" = "$TARGET_INSTANCE_ID" ] || { printf '%s\n' 'Target process was already replaced' >&2; exit 1; }
|
||||
docker stop --time 8 "$id" >/dev/null
|
||||
exit 0 ;;
|
||||
|
||||
esac
|
||||
done
|
||||
printf '%s\n' 'Target does not match a running lab4 API container' >&2
|
||||
exit 1
|
||||
Executable
+11
@@ -0,0 +1,11 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
mkdir -p bin
|
||||
(cd service && GOOS=linux GOARCH=$(docker info --format '{{.Architecture}}' | sed 's/aarch64/arm64/;s/x86_64/amd64/') CGO_ENABLED=0 go test -c -o ../bin/storage.test ./internal/adapter/repository/storage)
|
||||
database="lab4_atomic_$(date +%s)_$$"
|
||||
./scripts/compose.sh exec -T postgres psql -U messenger -d messenger -v ON_ERROR_STOP=1 -c "CREATE DATABASE $database" >/dev/null
|
||||
trap './scripts/compose.sh exec -T postgres psql -U messenger -d messenger -c "DROP DATABASE IF EXISTS $database WITH (FORCE)" >/dev/null' EXIT
|
||||
id=$(./scripts/compose.sh ps -q api | head -n 1)
|
||||
docker cp bin/storage.test "$id:/tmp/storage.test" >/dev/null
|
||||
docker exec -e "TEST_DATABASE_NAME=$database" "$id" /tmp/storage.test -test.v
|
||||
@@ -0,0 +1,191 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Build the lab4 report only from a successfully completed acceptance run."""
|
||||
import json
|
||||
from pathlib import Path
|
||||
import statistics
|
||||
|
||||
ROOT=Path(__file__).resolve().parents[1]
|
||||
E=ROOT/'evidence'
|
||||
def read(name):return json.loads((E/(name+'.json')).read_text())
|
||||
a=read('acceptance');env=read('environment')
|
||||
if not a.get('passed') or a.get('run_id')!=env.get('run_id'):
|
||||
raise SystemExit('A completed matching acceptance run is required before writing REPORT.md')
|
||||
f=read('failures');uploads=read('uploads');scans=read('reconciliation');safety=read('reconciliation-safety')
|
||||
limit=env['limits'];hw=env['docker']
|
||||
rows=[]
|
||||
for x in uploads:
|
||||
peak=max(v['memory_peak_bytes'] for v in x['memory'])/1048576
|
||||
cpu=', '.join(v['cpu_percent'] for v in x['cpu'])
|
||||
fmt=lambda value: '-' if value is None else f'{value*1000:.1f}'
|
||||
rows.append(f"| {x['bytes']} | {x['trial']} | {x['duration_seconds']:.3f} | {x['successful']}/10 | {x['successful_rps']:.1f} | {x['error_rate']:.0%} | {fmt(x['p50'])}/{fmt(x['p95'])}/{fmt(x['p99'])} | {peak:.1f} | {cpu} |")
|
||||
scan_rows=[]
|
||||
for x in scans:
|
||||
sample=x['dry_runs'][0]
|
||||
duration=statistics.median(v['duration_ms'] for v in x['dry_runs'])
|
||||
scan_rows.append(f"| {x['seeded']} | {sample['objects']} / {sample['metadata']} | {duration:.0f} | {sample['s3_requests']} | {sample['db_checks']} | {sample['lock_attempts']} | {x['apply']['deleted']} | {x['repeat_apply']['deleted']} |")
|
||||
max_peak=max(v['memory_peak_bytes'] for x in uploads for v in x['memory'])/1048576
|
||||
failover_rows=[]
|
||||
for i,x in enumerate(f['failover'],1):
|
||||
failover_rows.append(f"| Uploader {i} | Остановлен именно процесс `{x['upload_instance']}` | Скачал `{x['download_instance']}`; SHA-256 совпал |")
|
||||
report=f'''# Отчёт — лабораторная 4: S3 и согласованность двух хранилищ
|
||||
|
||||
- Автор, группа: Поликарпов Даниил Александрович, БПИ2303.
|
||||
- Целевая оценка: **5, трек 2 — согласованность PostgreSQL/S3**.
|
||||
- Измерения: {env['date_utc']}; окончание проверки: {a['timestamp_utc']}.
|
||||
- HEAD репозитория на момент проверки: `{env['git_head']}`; рабочее дерево содержит незакоммиченную реализацию. Это HEAD основы, а не опубликованный commit решения. Commit/PR ещё не созданы.
|
||||
- Идентификатор полного прогона: `{a['run_id']}`; итог — [acceptance.json](evidence/acceptance.json).
|
||||
- Реализация, диагностика и подготовка отчёта выполнены с помощью OpenAI Codex. Исходники проверок и методики включены в репозиторий.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
Нужны Docker Engine и Compose v2+, Go 1.27+, Python 3.9+, Bash, OpenSSL и Git. Проверено: {env['host']}, Docker {hw['ServerVersion']}, {hw['Architecture']}, {hw['NCPU']} vCPU и {hw['MemTotal']/1073741824:.2f} GiB памяти Docker VM. Нужны свободные loopback-порты 18081 и 18082. Соседние репозитории не требуются.
|
||||
|
||||
```sh
|
||||
python3 -m venv .venv
|
||||
.venv/bin/python -m pip install -r requirements-dev.txt
|
||||
make up
|
||||
make check validate PYTHON=.venv/bin/python
|
||||
make build vet test test-go test-storage
|
||||
make test-persistence ACTION_SCRIPT=./scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=./scripts/stop-one.sh
|
||||
make restore
|
||||
make reconcile
|
||||
# Полный изолированный прогон, включая отказы и исследование:
|
||||
make evidence PYTHON=.venv/bin/python
|
||||
python3 scripts/write-report.py
|
||||
# Безопасная остановка основного стенда, данные сохраняются:
|
||||
make down
|
||||
```
|
||||
|
||||
`make up` идемпотентно создаёт `.env.lab4`, JWT-ключи и отдельные S3 admin/API-ключи в `secrets/lab4`; существующие пароли и ключи не заменяются. Файлы игнорируются Git и исключены из Docker build context. API монтирует только JWT и собственные S3-ключи, admin-ключи в API не попадают. Единственная публикация основного стенда — `127.0.0.1:18081` через HAProxy.
|
||||
|
||||
Для **намеренного удаления только учебных данных основного стенда** оператор может выполнить `./scripts/compose.sh down -v`. При обычном restart/failover эта команда не применяется. Экспериментальный проект — `mtusi2026lab4-evidence`; его volumes также сохраняются после проверки. Повторная приёмка добавляет синтетические данные к существующим, что отражено в фактических размерах наборов.
|
||||
|
||||
## Архитектура и конфигурация
|
||||
|
||||
Клиент → HAProxy → API × 2 → PostgreSQL + приватный SeaweedFS. Redis хранит кэш неизменного состава чата; серверные сессии остаются в PostgreSQL. Перенесены регистрация/сессии, чаты, keyset-пагинация, общая БД и идемпотентность lab3. Добавлены Attachment, связи с сообщениями, HTTP endpoints, S3, reconciler и операторское восстановление.
|
||||
|
||||
| Роль | Образ / версия | Готовность и сохранность | Доступ |
|
||||
| --- | --- | --- | --- |
|
||||
| API × 2 / migrator | Сборка исходников: Go 1.27.2-alpine3.23 → Alpine 3.23.6 | `/health/live`; `/health/ready` проверяет PostgreSQL и S3 | API-порты не опубликованы; migrator — отдельный одноразовый процесс |
|
||||
| PostgreSQL | 17.11-alpine3.23 | `pg_isready`; `postgres_data` | Только сеть Compose |
|
||||
| Redis | 8.2.11-alpine3.22 | AUTH PING; AOF в `redis_data` | Только сеть Compose |
|
||||
| HAProxy | 3.2-alpine | Проверяет readiness каждой API-реплики | Только loopback 18081 |
|
||||
| SeaweedFS | 4.52 | `s3_data` содержит master/volume/filer; bootstrap проверяет bucket и PUT/DELETE | Только internal-сеть storage, без публикации gateway/master/filer |
|
||||
|
||||
Все digest закреплены в `compose.yaml` и Dockerfile. SeaweedFS: `sha256:57877e32bb90073b37dc109b438ba66e84fd5ed9ced5f5c04f56444ef243ce59`. `s3-init` использует admin identity и создаёт приватный bucket. В его JSON нет anonymous identity. API identity ограничена Read/Write/List конкретного bucket и не имеет Admin; создание другого bucket отвергается. Образы и точные зависимости Go зафиксированы manifest/lock-файлами проекта.
|
||||
|
||||
`.env.example` описывает параметры Compose: HOST_PORT/API_REPLICAS, пароли PostgreSQL/Redis, лимиты API и пула, SESSION_TTL_SECONDS, S3_BUCKET, MAX_UPLOAD_BYTES/MAX_IMAGE_PIXELS, UPLOAD_TIMEOUT и RECONCILE_GRACE_PERIOD. Их изменения задаются в `.env.lab4` или окружении оператора. S3_ENDPOINT, S3_REGION и пути credentials зафиксированы в `compose.yaml`; одноимённые переменные в `.env.lab4` их не переопределяют. В примере они приведены как комментарии для справки.
|
||||
|
||||
Для нестандартного bucket нужно также экспортировать `S3_BUCKET` перед `make up`: генератор S3-политики читает окружение процесса, а не `.env.lab4`. Для сдачи и приёмки используется стандартный `messenger-attachments`. Основной и экспериментальный проекты используют общие локальные файлы credentials; менять bucket/политику при работающем основном стенде не следует.
|
||||
|
||||
По умолчанию: API — 1 CPU/512 MiB каждая, четыре upload slots, один decoder, `GOMEMLIMIT=384MiB`; PostgreSQL — 2 CPU/1 GiB, Redis — 0.25 CPU/128 MiB, S3 — 1 CPU/512 MiB, LB — 0.5 CPU/128 MiB. Это лимиты контейнеров, а не гарантия высокой доступности единственного Docker-host.
|
||||
|
||||
## Синхронная загрузка и транзакционные границы
|
||||
|
||||
1. Проверить сессию и членство в чате; неизвестный и чужой ресурс одинаково дают 404. Проверить отображаемое filename, MIME и Content-Length.
|
||||
2. Прочитать не более 10 MiB через MaxBytesReader во временный файл, одновременно вычисляя SHA-256. Проверить формат и размеры через DecodeConfig, затем полностью декодировать PNG/JPEG под отдельным semaphore. Лимит 40 млн пикселей проверяется до полной распаковки.
|
||||
3. Сервер генерирует уникальный ключ `originals/<UUID>` и получает advisory session lock этого ключа. Filename не участвует ни в локальном пути, ни в S3 key.
|
||||
4. Сохранить оригинал в S3, затем атомарно вставить метаданные в PostgreSQL. Только после подтверждённого сохранения вернуть 201, `status=ready`, `variants=[]`, `error_code=null`.
|
||||
5. Удалить временный файл. После ответа скачивание зависит от S3 и БД, а не от файловой системы загрузившей реплики. Скачивание проходит через авторизованный API без redirect; Content-Length позволяет клиенту обнаружить обрыв передачи.
|
||||
|
||||
Атомарной транзакции PostgreSQL+S3 нет. При неопределённом результате COMMIT API возвращает ошибку, **не выполняя слепой S3 DELETE**: COMMIT мог пройти. Reconciler позже проверяет наличие метаданных. Обрыв тела до завершения чтения не создаёт готовый Attachment. Повтор самостоятельного upload может создать ещё один объект; прозрачных повторов публичного POST нет.
|
||||
|
||||
Сообщение, все связи вложений и результат идемпотентности записываются одной PostgreSQL-транзакцией. Правило «текст или вложение», UTF-8/4000 символов, максимум 10 уникальных ID и доступность вложений проверяет Go-сервис; функции и триггера `message_content` в миграциях нет. FK сохраняют ссылочную целостность. При проверке вложений используются row locks до COMMIT.
|
||||
|
||||
Версия хеша хранится в `hash_version`: legacy v1 учитывает текст, v2 — канонический текст и упорядоченный список ID. Отсутствующий список и `[]` равнозначны. Старые текстовые ключи продолжают воспроизводиться; изменение текста или вложений при том же ключе даёт 409. Порядок вложений сохраняется.
|
||||
|
||||
## Reconciler и восстановление
|
||||
|
||||
```sh
|
||||
make reconcile # По умолчанию dry-run
|
||||
make reconcile-apply # Применить безопасные изменения
|
||||
./scripts/compose.sh run --rm -T operator storage restore \\
|
||||
--id <attachment-uuid> --source /fixtures/sample.png
|
||||
```
|
||||
|
||||
Сверка читает S3 страницами по 100 объектов и БД keyset-страницами по 100 метаданных. Обрабатываются только серверные ключи `originals/<UUID>`. Для каждого ключа используется та же advisory lock, что при upload/restore; под ней проверяются метаданные и актуальный HEAD объекта. Если метаданные существуют, объект защищён, даже если он ещё не прикреплён к сообщению. Отсутствующие метаданные плюс возраст старше grace позволяют удалить orphan.
|
||||
|
||||
В обычном стенде upload timeout — 120 секунд, grace — один час; конфигурация требует grace больше удвоенного timeout. Это запас для отменённого запроса/потерянной DB-сессии. Предполагаются синхронизированные часы локального стенда. При неопределённом получении или освобождении lock соединение закрывается, а не возвращается в pool с потенциально удерживаемой блокировкой.
|
||||
|
||||
Missing — метаданные без оригинала. Dry-run только сообщает проблему; apply выставляет внутренний `integrity_missing`, сохраняя metadata, ID и ссылки сообщений. Клиент с доступом получает 503; публичный enum статуса lab4 не меняется. Восстановление требует предоставленного оригинала с совпадающими размером, MIME и SHA-256. После PUT выполняется контрольное чтение; только затем снимается признак повреждения. Неверный оригинал отвергается, повторный restore безопасен.
|
||||
|
||||
При ошибке любой зависимости проход завершается ненулевым кодом, сохраняя частичные счётчики. Уже выполненные безопасные действия не откатываются: общего COMMIT между хранилищами нет. Повторный запуск продолжает исправление. Для регулярной очистки оператору нужно запускать `make reconcile-apply`, например каждые 10 минут через свой планировщик; автоматическое cron-расписание не устанавливается.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий | Команда / наблюдение | Доказательство |
|
||||
| --- | --- | --- |
|
||||
| Структура и полный OpenAPI | `make check build vet`, `make validate` — PASS | [build-checks](evidence/build-checks.txt), [validate](evidence/validate.txt) |
|
||||
| Возможности lab3 и идемпотентность | Go race suite: история/пагинация, 32 конкурентных повтора через 2 реплики | [go-tests](evidence/go-tests.txt) |
|
||||
| Обязательный API lab4 | Публичный smoke: файл, attachment-only message, checksum, ACL — PASS | [smoke](evidence/smoke.txt) |
|
||||
| PNG/JPEG, bytes/pixels, filename | Exact 10 MiB — 201; превышение bytes, chunked и pixels — 413; MIME/corruption — 415; filename — 400 | [go-tests](evidence/go-tests.txt) |
|
||||
| Чужой файл и чат | Metadata/content и cross-chat attachment — 404; duplicate/null ID list — 400 | [go-tests](evidence/go-tests.txt) |
|
||||
| Только Go-проверка содержимого | Пустое сообщение отклоняется; attachment-only сохраняется; триггер отсутствует | [storage-tests](evidence/storage-tests.txt) |
|
||||
| Атомарность сообщений | Ошибка FK/записи ключа откатывает сообщение, связи и результат; legacy replay сохранён | [storage-tests](evidence/storage-tests.txt) |
|
||||
| Приватность и ограниченные ключи | Unsigned GET известного объекта — {f['private_bucket']['anonymous_status']}; API CreateBucket запрещён | [failures](evidence/failures.json) |
|
||||
| Persistence | Прежний token и исходный файл пережили restart; до/после видны реальные процессы всех stores | [persistence](evidence/persistence.txt), [snapshots](evidence/failures.json) |
|
||||
| Остановка загрузившей реплики | Поочерёдно остановлены обе; файл скачан другой репликой с тем же SHA-256 | [failures](evidence/failures.json) |
|
||||
| Публичный failover | Запись во время отказа, повторы без дубля, прежняя сессия — PASS | [failover](evidence/failover.txt) |
|
||||
| Недоступность S3 | Liveness 200, readiness/upload 503; reconciler не удаляет/не помечает | [failures](evidence/failures.json), [S3 down](evidence/reconcile-s3-down.txt) |
|
||||
| Отказ PostgreSQL | Сверка прекращается без изменений; после восстановления метаданные сохранены | [failures](evidence/failures.json), [DB down](evidence/reconcile-db-down.txt) |
|
||||
| Прерывание и PUT → ошибка БД | Незавершённый body не создаёт строку; timeout после PUT даёт диагностируемый orphan | [failures](evidence/failures.json) |
|
||||
| Missing и восстановление | Обнаружен; неверный оригинал отвергнут; верный восстановлен дважды без потери ссылки | [failures](evidence/failures.json) |
|
||||
| Память при 10 клиентах | Два размера, три повтора; ограничение admission учитывается в ошибках | [uploads](evidence/uploads.json) |
|
||||
| Безопасная сверка | Fresh и использованные объекты сохранены, старые orphan удалены, повтор ничего не удаляет | [safety](evidence/reconciliation-safety.json), [scans](evidence/reconciliation.json) |
|
||||
| Bootstrap | Повтор сохраняет ключи; существующий bucket допускает bootstrap и проверку записи | [bootstrap-repeat](evidence/bootstrap-repeat.txt), [bucket repeat](evidence/bucket-bootstrap-repeat.txt) |
|
||||
|
||||
Полная команда воспроизведения — `make evidence`. PASS означает завершение перечисленных локальных сценариев, а не автоматическое выставление оценки. Секреты, tokens и raw HTTP-запросы в evidence не сохраняются.
|
||||
|
||||
## Эксперименты
|
||||
|
||||
Гипотеза: при неизменяемых серверных ключах, общей блокировке и grace можно повторяемо удалить orphan, сохранив свежие и используемые вложения; missing обнаруживаются, а внешний оригинал позволяет восстановить данные без смены ссылок.
|
||||
|
||||
Изолированный проект использует те же образы и контейнерные ресурсы, но `UPLOAD_TIMEOUT=5s`, `RECONCILE_GRACE_PERIOD=15s`, чтобы ждать grace без часовой паузы. Обычные defaults остаются 120s/1h. Защита свежего объекта отдельно проверяется с grace 1h, независимо от длительности массового заполнения. Наборы содержат также ранее загруженные файлы — фактические размеры указаны ниже.
|
||||
|
||||
### Upload: bytes и память
|
||||
|
||||
Перед каждым из трёх повторов для каждого размера API-контейнеры создаются заново, выполняется одна прогревочная загрузка исходного малого файла, затем barrier запускает 10 клиентов одновременно. Отдельного длительного прогрева нет. Оригинал — RGB PNG 320×200, {uploads[0]['bytes']} байт; вариант 5 MiB содержит валидный ancillary PNG chunk и имеет те же декодированные размеры. Это сравнение стоимости передачи bytes, а не исследование больших изображений.
|
||||
|
||||
| Bytes | Повтор | Длительность, s | Успехи | Успешные RPS | Ошибки | p50/p95/p99, ms | Max peak одной API, MiB | CPU samples API |
|
||||
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
{chr(10).join(rows)}
|
||||
|
||||
Максимальный наблюдавшийся пик одной API: **{max_peak:.1f} MiB** при лимите 512 MiB. Сняты `memory.current` и kernel `memory.peak` cgroup v2: peak относится ко всей жизни заново созданного контейнера, включая startup и прогрев, а не только к интервалу burst. CPU — короткий `docker stats --no-stream` sample; он не даёт надёжной оценки steady-state CPU для столь короткого burst. В errors отдельно учитываются 429, повторов скрыто нет. Percentiles относятся только к успешным запросам; 10 наблюдений недостаточно для устойчивой оценки хвоста. RPS включает клиентскую подготовку, не доказывает максимальную пропускную способность сервера.
|
||||
|
||||
### Стоимость сверки
|
||||
|
||||
После заполнения выдерживается 16 секунд, выполняются три dry-run, apply и повторный apply. В таблице — медиана времени dry-run; счётчики вызовов приведены для первого прохода.
|
||||
|
||||
| Добавлено orphan | Объекты / metadata | Dry-run median, ms | Вызовы S3 | Проверки/страницы БД | Попытки session lock | Удалено apply | Удалено повторно |
|
||||
| --- | --- | --- | --- | --- | --- | --- | --- |
|
||||
{chr(10).join(scan_rows)}
|
||||
|
||||
`db_checks` считает проверки наличия, страницы метаданных и изменения; это не все сетевые round trips PostgreSQL. Каждая успешная session lock добавляет SQL получения и освобождения; отдельно показан `lock_attempts`. S3-счётчик включает HEAD bucket, List pages, Head objects и Delete. Стоимость растёт с фактическим числом объектов и метаданных; общий snapshot двух хранилищ не создаётся. Вставка UUID позади keyset cursor может попасть в следующий периодический проход.
|
||||
|
||||
Безопасность проверена предметными наблюдениями: свежий orphan остался, referenced originals не удалялись, invalid key и объект вне prefix остались, после очистки второй apply удалил ноль объектов и missing не появилось. Во время отдельного прохода выполнялся upload; его подтверждённый оригинал скачан с тем же checksum. Это конкурентный прогон, а детерминированное ожидание и отмена advisory lock отдельно проверены на реальном PostgreSQL в storage suite.
|
||||
|
||||
| Воздействие | Наблюдение | Доказательство |
|
||||
| --- | --- | --- |
|
||||
{chr(10).join(failover_rows)}
|
||||
| Restart PostgreSQL/Redis/S3/API/LB | Прежние данные, session и bytes сохранены; процессы перезапущены или заменены | persistence + snapshots |
|
||||
| S3 остановлен | 200 live, 503 ready/upload; после bootstrap путь записи восстановлен | failures |
|
||||
| PostgreSQL остановлен | Reconciler завершился без удаления, после запуска данные сохранены | failures |
|
||||
| Тело upload оборвано | Строка Attachment не появилась | failures |
|
||||
| PUT успешен, INSERT заблокирован | Upload 503, метаданные не появились, orphan найден и очищен после grace | failures + reconciliation |
|
||||
| Оригинал удалён из S3 | Missing отмечен, затем исходник восстановлен с тем же ID/checksum и ссылкой | failures |
|
||||
|
||||
В этих сценариях потери подтверждённых данных после restart/failover не обнаружены. Время восстановления отдельно не измерялось: проверки подтверждают восстановление в пределах своих таймаутов, но не дают оценки RTO.
|
||||
|
||||
## Ограничения и источники
|
||||
|
||||
Один Docker-host, PostgreSQL, Redis, S3 и LB остаются одиночными точками отказа. Сессии хранятся в БД, Redis — необязательный cache; отказ S3 выводит API из readiness. Ошибки, сформированные самим LB при отсутствии здоровых backend, могут не иметь JSON/identity headers приложения. HTTP допустим только для локального стенда с синтетическими данными; TLS — задание lab7.
|
||||
|
||||
Reconciler не делает резервное копирование, не восстанавливает bytes из SHA-256 и не проверяет checksum каждого существующего объекта при обычном HEAD-обходе. Если оригинала/backup нет, missing остаётся невосстановимым. Временный локальный файл используется до сохранения, но не является долговременным источником оригинала. `GOMEMLIMIT` — мягкий бюджет Go, а не жёсткая гарантия RSS; предел процесса задаёт cgroup, полная распаковка всё ещё ограничена количеством пикселей и одним decoder.
|
||||
|
||||
Периодическую сверку запускает оператор. Потеря БД сессии/сетевые разделения покрываются блокировками, ограничением времени запроса и grace в рамках заявленного локального стенда; глобальной транзакции или защиты от произвольных внешних writers с admin-ключами нет. Нагрузочные результаты привязаны к этому hardware, размерам изображений и короткому burst-профилю.
|
||||
|
||||
Первичные материалы: [SeaweedFS 4.52](https://github.com/seaweedfs/seaweedfs/releases/tag/4.52), [S3 credentials/configuration](https://github.com/seaweedfs/seaweedfs/wiki/S3-Configuration), [AWS SDK for Go v2 endpoints](https://docs.aws.amazon.com/sdk-for-go/v2/developer-guide/configure-endpoints.html), [S3 upload semantics](https://docs.aws.amazon.com/AmazonS3/latest/userguide/upload-objects.html). Использованы стандартные Go image/png/jpeg, AWS SDK v2, pgx, chi и oapi-codegen; версии — в go.mod/go.sum и generated header. Обязательный контракт и fixture предоставлены материалами курса.
|
||||
'''
|
||||
(ROOT/'REPORT.md').write_text(report)
|
||||
print('REPORT.md generated from successful acceptance evidence.')
|
||||
@@ -0,0 +1,150 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"encoding/json"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/adapter/controller/http"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/adapter/repository/cache/outside"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/adapter/repository/storage"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/auth/jwtprovider"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/cache"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/encrypting"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/migrator"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/objectstore"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres/txmanager"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/middleware"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/validator"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/attachment"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/auth"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/chat"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/user"
|
||||
"github.com/google/uuid"
|
||||
stdhttp "net/http"
|
||||
)
|
||||
|
||||
func main() {
|
||||
time.Local = time.UTC
|
||||
if err := run(); err != nil {
|
||||
slog.Error("application stopped", "error", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
cfg, err := config.New()
|
||||
if err != nil {
|
||||
return fmt.Errorf("load server config: %w", err)
|
||||
}
|
||||
|
||||
if len(os.Args) > 1 && os.Args[1] == "migrate" {
|
||||
cfg.Postgres.RunMigrations = true
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
||||
defer cancel()
|
||||
return migrator.New(cfg).Start(ctx)
|
||||
}
|
||||
attachmentConfig, err := config.LoadAttachment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if len(os.Args) > 1 && os.Args[1] == "storage" {
|
||||
return runStorage(cfg, attachmentConfig, os.Args[2:])
|
||||
}
|
||||
|
||||
// API processes never run migrations; the one-shot migrator owns schema changes.
|
||||
cfg.Postgres.RunMigrations = false
|
||||
var level slog.Level
|
||||
if err := level.UnmarshalText([]byte(cfg.Application.LogLevel)); err != nil {
|
||||
return err
|
||||
}
|
||||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: level})))
|
||||
hostname, _ := os.Hostname()
|
||||
instanceID := hostname + "-" + uuid.NewString()
|
||||
slog.Info("instance_started", "instance_id", instanceID)
|
||||
pgx := postgres.New(cfg.Postgres)
|
||||
rx := cache.New(cfg)
|
||||
postgresMigrator := migrator.New(cfg)
|
||||
defer pgx.Close()
|
||||
defer rx.Close()
|
||||
tm := txmanager.New(pgx)
|
||||
provider := jwtprovider.New(cfg)
|
||||
encryptor := encrypting.New()
|
||||
|
||||
chatsRepo := storage.NewChatsRepo(pgx)
|
||||
messageRepo := storage.NewMessagesRepo(pgx)
|
||||
refreshTokensRepo := storage.NewRefreshTokensRepo(pgx)
|
||||
userRepo := storage.NewUsersRepo(pgx)
|
||||
|
||||
chatCache := outside.NewChatCache(rx)
|
||||
|
||||
authSvc := auth.New(provider, tm, encryptor, userRepo, refreshTokensRepo)
|
||||
userSvc := user.New(encryptor, userRepo)
|
||||
chatSvc := chat.New(tm, chatsRepo, messageRepo, chatCache)
|
||||
objectStore := objectstore.New(attachmentConfig)
|
||||
attachmentSvc := attachment.New(&storage.AttachmentsRepo{Pool: pgx}, objectStore, attachmentConfig, chatSvc)
|
||||
|
||||
httpRouter := router.New(
|
||||
middleware.NewAuthMiddleware(authSvc),
|
||||
middleware.Instance(instanceID),
|
||||
middleware.RequestIDMiddleware(),
|
||||
middleware.LoggingMiddleware(),
|
||||
middleware.RequestTimeout(cfg.Postgres.QueryTimeout, attachmentConfig.Timeout),
|
||||
)
|
||||
httpRouter.Get("/internal/metrics", func(w stdhttp.ResponseWriter, r *stdhttp.Request) {
|
||||
stat := pgx.Stat()
|
||||
_ = json.NewEncoder(w).Encode(map[string]any{"instance_id": instanceID, "acquire_count": stat.AcquireCount(), "acquire_duration_ns": stat.AcquireDuration().Nanoseconds(), "empty_acquire_count": stat.EmptyAcquireCount(), "acquired_conns": stat.AcquiredConns(), "total_conns": stat.TotalConns(), "max_conns": stat.MaxConns()})
|
||||
})
|
||||
server := http.New(cfg, httpRouter)
|
||||
requestValidator := validator.NewValidatorImpl()
|
||||
registerRoutes(
|
||||
http.NewAuthHandler(httpRouter, authSvc),
|
||||
http.NewHealthHandler(httpRouter, server, pgx, objectStore),
|
||||
http.NewChatHandler(httpRouter, provider, requestValidator, chatSvc),
|
||||
http.NewUserHandler(httpRouter, provider, requestValidator, userSvc),
|
||||
http.NewAttachmentHandler(httpRouter, provider, attachmentSvc),
|
||||
)
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
if err = postgresMigrator.Start(ctx); err != nil {
|
||||
return fmt.Errorf("start postgres migrator: %w", err)
|
||||
}
|
||||
if err = server.Start(ctx); err != nil {
|
||||
return fmt.Errorf("start HTTP server: %w", err)
|
||||
}
|
||||
if err = rx.Start(ctx); err != nil {
|
||||
slog.Warn("redis unavailable at startup; continuing without cache", "error", err)
|
||||
}
|
||||
slog.Info("HTTP server started", "address", cfg.Server.BindAddress())
|
||||
|
||||
<-ctx.Done()
|
||||
shutdownCtx, cancel := context.WithTimeout(context.Background(), cfg.Server.ShutdownTimeout)
|
||||
defer cancel()
|
||||
if err = server.Stop(shutdownCtx); err != nil {
|
||||
return fmt.Errorf("stop HTTP server: %w", err)
|
||||
}
|
||||
if err = postgresMigrator.Close(shutdownCtx); err != nil {
|
||||
return fmt.Errorf("close postgres migrator: %w", err)
|
||||
}
|
||||
if err = rx.Close(); err != nil {
|
||||
return fmt.Errorf("close redis: %w", err)
|
||||
}
|
||||
pgx.Close()
|
||||
return nil
|
||||
}
|
||||
|
||||
func registerRoutes(handlers ...http.Handler) {
|
||||
for _, handler := range handlers {
|
||||
handler.RegisterRoutes()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,175 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/smithy-go"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/adapter/repository/storage"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/objectstore"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/attachment"
|
||||
)
|
||||
|
||||
func runStorage(cfg *config.Config, c *config.Attachment, args []string) error {
|
||||
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelWarn})))
|
||||
if len(args) == 0 {
|
||||
return fmt.Errorf("storage command required")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
|
||||
defer cancel()
|
||||
store := objectstore.New(c)
|
||||
if args[0] == "bootstrap" {
|
||||
var err error
|
||||
for i := 0; i < 60; i++ {
|
||||
_, err = store.Client.CreateBucket(ctx, &s3.CreateBucketInput{Bucket: &c.Bucket})
|
||||
var api smithy.APIError
|
||||
if err == nil || (errors.As(err, &api) && (api.ErrorCode() == "BucketAlreadyOwnedByYou" || api.ErrorCode() == "BucketAlreadyExists")) {
|
||||
if store.Ping(ctx) == nil {
|
||||
// HEAD alone can succeed before the restarted master/volume write path.
|
||||
// This constant key is outside the application's immutable originals prefix.
|
||||
key := "bootstrap/write-check"
|
||||
if store.Put(ctx, key, "application/octet-stream", strings.NewReader("ready"), 5) == nil && store.Delete(ctx, key) == nil {
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"bucket_ready": true, "write_checked": true})
|
||||
}
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
return fmt.Errorf("storage bootstrap failed: %w", err)
|
||||
}
|
||||
if args[0] == "private-check" {
|
||||
// Unsigned requests must reach the S3 server and receive an auth failure,
|
||||
// rather than passing because a network error hid a public object.
|
||||
fs := flag.NewFlagSet("private-check", flag.ContinueOnError)
|
||||
key := fs.String("key", "", "known object key")
|
||||
if e := fs.Parse(args[1:]); e != nil {
|
||||
return e
|
||||
}
|
||||
client := http.Client{Timeout: 5 * time.Second, CheckRedirect: func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }}
|
||||
resp, e := client.Get(c.Endpoint + "/" + c.Bucket + "/" + *key)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != 401 && resp.StatusCode != 403 {
|
||||
return fmt.Errorf("unsigned GET returned %d", resp.StatusCode)
|
||||
}
|
||||
_, e = store.Client.CreateBucket(ctx, &s3.CreateBucketInput{Bucket: new("forbidden-" + uuid.NewString())})
|
||||
var api smithy.APIError
|
||||
if !errors.As(e, &api) || api.ErrorCode() != "AccessDenied" {
|
||||
return fmt.Errorf("API key bucket creation was not denied")
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"anonymous_status": resp.StatusCode, "api_create_bucket_denied": true})
|
||||
}
|
||||
if args[0] == "object-check" {
|
||||
fs := flag.NewFlagSet("object-check", flag.ContinueOnError)
|
||||
key := fs.String("key", "", "synthetic object key")
|
||||
if e := fs.Parse(args[1:]); e != nil {
|
||||
return e
|
||||
}
|
||||
head, e := store.Head(ctx, *key)
|
||||
if objectstore.Missing(e) {
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"exists": false})
|
||||
}
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"exists": true, "last_modified": head.LastModified, "size_bytes": head.ContentLength})
|
||||
}
|
||||
|
||||
pool := postgres.New(cfg.Postgres)
|
||||
defer pool.Close()
|
||||
svc := attachment.New(&storage.AttachmentsRepo{Pool: pool}, store, c, nil)
|
||||
switch args[0] {
|
||||
case "reconcile":
|
||||
fs := flag.NewFlagSet("reconcile", flag.ContinueOnError)
|
||||
apply := fs.Bool("apply", false, "apply safe changes")
|
||||
if e := fs.Parse(args[1:]); e != nil {
|
||||
return e
|
||||
}
|
||||
result, e := svc.Reconcile(ctx, *apply)
|
||||
_ = json.NewEncoder(os.Stdout).Encode(result)
|
||||
return e
|
||||
case "restore":
|
||||
fs := flag.NewFlagSet("restore", flag.ContinueOnError)
|
||||
id := fs.String("id", "", "attachment id")
|
||||
source := fs.String("source", "", "original file")
|
||||
if e := fs.Parse(args[1:]); e != nil {
|
||||
return e
|
||||
}
|
||||
uid, e := uuid.Parse(*id)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
if e = svc.Restore(ctx, uid, *source); e != nil {
|
||||
return e
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"restored": uid})
|
||||
case "seed":
|
||||
fs := flag.NewFlagSet("seed", flag.ContinueOnError)
|
||||
count := fs.Int("count", 1, "synthetic orphan count")
|
||||
keyOverride := fs.String("key", "", "explicit key for a single synthetic object")
|
||||
if e := fs.Parse(args[1:]); e != nil {
|
||||
return e
|
||||
}
|
||||
if *count < 1 || *count > 10000 {
|
||||
return fmt.Errorf("count must be 1..10000")
|
||||
}
|
||||
keys := []string{}
|
||||
for i := 0; i < *count; i++ {
|
||||
key := attachment.Prefix + uuid.NewString()
|
||||
if *keyOverride != "" {
|
||||
if *count != 1 {
|
||||
return fmt.Errorf("explicit key requires count=1")
|
||||
}
|
||||
key = *keyOverride
|
||||
}
|
||||
if e := store.Put(ctx, key, "image/png", strings.NewReader("synthetic-orphan"), 16); e != nil {
|
||||
return e
|
||||
}
|
||||
if *count == 1 {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"seeded": *count, "keys": keys})
|
||||
case "remove-object":
|
||||
fs := flag.NewFlagSet("remove-object", flag.ContinueOnError)
|
||||
id := fs.String("id", "", "synthetic attachment id")
|
||||
if e := fs.Parse(args[1:]); e != nil {
|
||||
return e
|
||||
}
|
||||
uid, e := uuid.Parse(*id)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
a, e := svc.Repo.Get(ctx, uid)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
conn, unlock, e := svc.Repo.Lock(ctx, a.Key)
|
||||
_ = conn
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
defer unlock()
|
||||
if e = store.Delete(ctx, a.Key); e != nil {
|
||||
return e
|
||||
}
|
||||
return json.NewEncoder(os.Stdout).Encode(map[string]any{"removed_object": uid})
|
||||
default:
|
||||
return fmt.Errorf("unknown storage command")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,260 @@
|
||||
// Command benchmark generates paced lab4 traffic through the public load balancer.
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"slices"
|
||||
"strings"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type client struct {
|
||||
base, token string
|
||||
http *http.Client
|
||||
}
|
||||
|
||||
func (c *client) call(method, path string, body any, key string) (map[string]any, int, error) {
|
||||
var data io.Reader
|
||||
if body != nil {
|
||||
b, e := json.Marshal(body)
|
||||
if e != nil {
|
||||
return nil, 0, e
|
||||
}
|
||||
data = bytes.NewReader(b)
|
||||
}
|
||||
req, e := http.NewRequest(method, c.base+path, data)
|
||||
if e != nil {
|
||||
return nil, 0, e
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if c.token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.token)
|
||||
}
|
||||
if key != "" {
|
||||
req.Header.Set("Idempotency-Key", key)
|
||||
}
|
||||
// net/http otherwise treats Idempotency-Key + GetBody as permission to retry POST.
|
||||
if method == http.MethodPost {
|
||||
req.GetBody = nil
|
||||
}
|
||||
resp, e := c.http.Do(req)
|
||||
if e != nil {
|
||||
return nil, 0, e
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
b, e := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if e != nil {
|
||||
return nil, resp.StatusCode, e
|
||||
}
|
||||
if resp.StatusCode != 200 && resp.StatusCode != 201 {
|
||||
return nil, resp.StatusCode, fmt.Errorf("HTTP %d", resp.StatusCode)
|
||||
}
|
||||
var out map[string]any
|
||||
e = json.Unmarshal(b, &out)
|
||||
return out, resp.StatusCode, e
|
||||
}
|
||||
func (c *client) setup(seed int) (string, error) {
|
||||
username := "bench_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:20]
|
||||
password := "Synthetic-benchmark-123"
|
||||
user, _, e := c.call("POST", "/api/v1/users", map[string]string{"username": username, "display_name": "Benchmark", "password": password}, "")
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
req, _ := http.NewRequest("POST", c.base+"/api/v1/auth/sessions", nil)
|
||||
req.SetBasicAuth(username, password)
|
||||
resp, e := c.http.Do(req)
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
var session struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
e = json.NewDecoder(resp.Body).Decode(&session)
|
||||
resp.Body.Close()
|
||||
if e != nil || resp.StatusCode != 201 || session.Token == "" {
|
||||
return "", fmt.Errorf("login status %d: %v", resp.StatusCode, e)
|
||||
}
|
||||
c.token = session.Token
|
||||
chat, _, e := c.call("POST", "/api/v1/chats", map[string]any{"title": "Benchmark", "member_ids": []any{user["id"]}}, "")
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
path := "/api/v1/chats/" + chat["id"].(string) + "/messages"
|
||||
// Bounded concurrent setup is excluded from the measured interval.
|
||||
var group sync.WaitGroup
|
||||
slots := make(chan struct{}, 8)
|
||||
errs := make(chan error, seed)
|
||||
for i := range seed {
|
||||
slots <- struct{}{}
|
||||
group.Go(func() {
|
||||
defer func() { <-slots }()
|
||||
_, _, e := c.call("POST", path, map[string]string{"text": fmt.Sprintf("seed-%d", i)}, uuid.NewString())
|
||||
if e != nil {
|
||||
errs <- e
|
||||
}
|
||||
})
|
||||
}
|
||||
group.Wait()
|
||||
close(errs)
|
||||
for e := range errs {
|
||||
if e != nil {
|
||||
return "", e
|
||||
}
|
||||
}
|
||||
return path, nil
|
||||
}
|
||||
|
||||
type result struct {
|
||||
MeasurementUnix float64 `json:"measurement_started_unix"`
|
||||
Label string `json:"label"`
|
||||
TargetRate int `json:"target_rps"`
|
||||
Duration float64 `json:"duration_seconds"`
|
||||
Elapsed float64 `json:"elapsed_including_drain_seconds"`
|
||||
Scheduled int `json:"scheduled"`
|
||||
Dropped int `json:"generator_dropped"`
|
||||
Success int `json:"successful_requests"`
|
||||
Errors int `json:"failed_requests"`
|
||||
ReadSuccess int `json:"read_success"`
|
||||
WriteSuccess int `json:"write_success"`
|
||||
SuccessfulRPS float64 `json:"successful_rps"`
|
||||
ErrorRatio float64 `json:"error_ratio_including_drops"`
|
||||
Latency map[string]float64 `json:"successful_latency_ms"`
|
||||
Statuses map[int]int `json:"statuses"`
|
||||
PeakInFlight int64 `json:"peak_in_flight"`
|
||||
Note string `json:"note"`
|
||||
}
|
||||
|
||||
func phase(c *client, path string, rate int, duration time.Duration, maxInFlight int, label string) result {
|
||||
out := result{Label: label, TargetRate: rate, Duration: duration.Seconds(), Statuses: map[int]int{}, Latency: map[string]float64{}, Note: "Paced open-loop, 10ms dispatch batches; 90% first-page GET, 10% POST; no POST retries. Percentiles of successes include scheduler delay; failures and capacity drops reported separately. RPS denominator includes drain."}
|
||||
start := time.Now()
|
||||
out.MeasurementUnix = float64(start.UnixNano()) / 1e9
|
||||
end := start.Add(duration)
|
||||
slots := make(chan struct{}, maxInFlight)
|
||||
var group sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
var active atomic.Int64
|
||||
var peak atomic.Int64
|
||||
latencies := make([]float64, 0, rate*int(duration.Seconds()))
|
||||
index := 0
|
||||
for tick := 0; ; tick++ {
|
||||
now := time.Now()
|
||||
if !now.Before(end) {
|
||||
break
|
||||
}
|
||||
target := start.Add(time.Duration(tick) * 10 * time.Millisecond)
|
||||
if target.After(now) {
|
||||
time.Sleep(time.Until(target))
|
||||
}
|
||||
until := min(int(time.Since(start).Seconds()*float64(rate))+1, int(duration.Seconds()*float64(rate)))
|
||||
for index < until {
|
||||
i := index
|
||||
index++
|
||||
out.Scheduled++
|
||||
scheduled := start.Add(time.Duration(float64(i) / float64(rate) * float64(time.Second)))
|
||||
select {
|
||||
case slots <- struct{}{}:
|
||||
group.Go(func() {
|
||||
n := active.Add(1)
|
||||
for old := peak.Load(); n > old; old = peak.Load() {
|
||||
if peak.CompareAndSwap(old, n) {
|
||||
break
|
||||
}
|
||||
}
|
||||
defer func() { active.Add(-1); <-slots }()
|
||||
method, p := "GET", path+"?limit=50"
|
||||
var body any
|
||||
key := ""
|
||||
if i%10 == 0 {
|
||||
method = "POST"
|
||||
p = path
|
||||
key = uuid.NewString()
|
||||
body = map[string]string{"text": key}
|
||||
}
|
||||
_, status, err := c.call(method, p, body, key)
|
||||
latency := float64(time.Since(scheduled).Microseconds()) / 1000
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
out.Statuses[status]++
|
||||
if err != nil {
|
||||
out.Errors++
|
||||
} else {
|
||||
out.Success++
|
||||
if method == "GET" {
|
||||
out.ReadSuccess++
|
||||
} else {
|
||||
out.WriteSuccess++
|
||||
}
|
||||
latencies = append(latencies, max(0, latency))
|
||||
}
|
||||
})
|
||||
default:
|
||||
out.Dropped++
|
||||
}
|
||||
}
|
||||
}
|
||||
group.Wait()
|
||||
out.Elapsed = time.Since(start).Seconds()
|
||||
out.PeakInFlight = peak.Load()
|
||||
out.SuccessfulRPS = float64(out.Success) / out.Elapsed
|
||||
if out.Scheduled > 0 {
|
||||
out.ErrorRatio = float64(out.Errors+out.Dropped) / float64(out.Scheduled)
|
||||
}
|
||||
slices.Sort(latencies)
|
||||
for name, p := range map[string]float64{"p50": .5, "p95": .95, "p99": .99} {
|
||||
if len(latencies) > 0 {
|
||||
out.Latency[name] = latencies[max(0, int(math.Ceil(p*float64(len(latencies))))-1)]
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
func main() {
|
||||
base := flag.String("base-url", "http://localhost:18081", "public LB URL")
|
||||
rate := flag.Int("rate", 100, "scheduled requests per second")
|
||||
duration := flag.Duration("duration", 30*time.Second, "measured time")
|
||||
warmup := flag.Duration("warmup", 5*time.Second, "warmup excluded from results")
|
||||
seed := flag.Int("seed", 1000, "initial messages")
|
||||
maxFlight := flag.Int("max-in-flight", 256, "bounded in-flight limit")
|
||||
label := flag.String("label", "manual", "case label")
|
||||
output := flag.String("output", "", "JSON destination")
|
||||
flag.Parse()
|
||||
if *rate < 1 || *rate > 100000 || *duration <= 0 || *warmup < 0 || *maxFlight < 1 || *seed < 0 {
|
||||
fmt.Fprintln(os.Stderr, "invalid benchmark settings")
|
||||
os.Exit(2)
|
||||
}
|
||||
c := &client{base: *base, http: &http.Client{Timeout: 2 * time.Second, Transport: &http.Transport{MaxIdleConns: 512, MaxIdleConnsPerHost: 512, MaxConnsPerHost: *maxFlight, IdleConnTimeout: 60 * time.Second}}}
|
||||
path, e := c.setup(*seed)
|
||||
if e != nil {
|
||||
fmt.Fprintln(os.Stderr, "setup:", e)
|
||||
os.Exit(1)
|
||||
}
|
||||
if *warmup > 0 {
|
||||
phase(c, path, *rate, *warmup, *maxFlight, "warmup")
|
||||
}
|
||||
row := phase(c, path, *rate, *duration, *maxFlight, *label)
|
||||
b, e := json.MarshalIndent(row, "", " ")
|
||||
if e != nil {
|
||||
panic(e)
|
||||
}
|
||||
if *output != "" {
|
||||
if e = os.WriteFile(*output, append(b, '\n'), 0644); e != nil {
|
||||
panic(e)
|
||||
}
|
||||
}
|
||||
fmt.Println(string(b))
|
||||
if row.Success == 0 {
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestLostPostResponseIsNotRetried(t *testing.T) {
|
||||
var posts atomic.Int32
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, _ = w.Write([]byte(`{}`))
|
||||
return
|
||||
}
|
||||
posts.Add(1)
|
||||
_, _ = io.Copy(io.Discard, r.Body)
|
||||
conn, _, err := w.(http.Hijacker).Hijack()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
_ = conn.Close()
|
||||
}))
|
||||
defer server.Close()
|
||||
transport := &http.Transport{MaxIdleConnsPerHost: 1}
|
||||
defer transport.CloseIdleConnections()
|
||||
c := &client{base: server.URL, http: &http.Client{Timeout: time.Second, Transport: transport}}
|
||||
if _, _, err := c.call(http.MethodGet, "/warm", nil, ""); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, _, err := c.call(http.MethodPost, "/message", map[string]string{"text": "uncertain result"}, "same-key"); err == nil {
|
||||
t.Fatal("lost response must be reported as an error")
|
||||
}
|
||||
if posts.Load() != 1 {
|
||||
t.Fatalf("transport sent %d POSTs, want exactly 1", posts.Load())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
module gitea.privcloud.tech/mtusi2026
|
||||
|
||||
go 1.27.0
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.8
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.16
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.9
|
||||
github.com/aws/smithy-go v1.28.8
|
||||
github.com/go-chi/chi/v5 v5.5.0
|
||||
github.com/go-playground/validator/v10 v10.30.5
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/jackc/tern/v2 v2.4.3
|
||||
github.com/mcosta74/pgx-slog v0.4.1
|
||||
github.com/oapi-codegen/runtime v1.4.2
|
||||
github.com/redis/go-redis/v9 v9.23.0
|
||||
github.com/spf13/viper v1.21.0
|
||||
github.com/vgarvardt/pgx-google-uuid/v5 v5.6.0
|
||||
go.uber.org/multierr v1.11.0
|
||||
golang.org/x/crypto v0.57.0
|
||||
golang.org/x/sync v0.23.0
|
||||
google.golang.org/grpc v1.85.0
|
||||
)
|
||||
|
||||
require (
|
||||
dario.cat/mergo v1.0.2 // indirect
|
||||
github.com/Masterminds/goutils v1.1.1 // indirect
|
||||
github.com/Masterminds/semver/v3 v3.5.0 // indirect
|
||||
github.com/Masterminds/sprig/v3 v3.3.0 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.26 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.11 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.11 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.11 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.24 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.12 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.11 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.11 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.15 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||
github.com/huandu/xstrings v1.5.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/leodido/go-urn v1.5.0 // indirect
|
||||
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.4.3-0.20260624044523-35f78d5908f1 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/shopspring/decimal v1.4.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
go.uber.org/atomic v1.12.0 // indirect
|
||||
go.yaml.in/yaml/v3 v3.0.5 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
)
|
||||
+147
@@ -0,0 +1,147 @@
|
||||
dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8=
|
||||
dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA=
|
||||
github.com/Masterminds/goutils v1.1.1 h1:5nUrii3FMTL5diU80unEVvNevw1nH4+ZV4DSLVJLSYI=
|
||||
github.com/Masterminds/goutils v1.1.1/go.mod h1:8cTjp+g8YejhMuvIA5y2vz3BpJxksy863GQaJW2MFNU=
|
||||
github.com/Masterminds/semver/v3 v3.5.0 h1:kQceYJfbupGfZOKZQg0kou0DgAKhzDg2NZPAwZ/2OOE=
|
||||
github.com/Masterminds/semver/v3 v3.5.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
|
||||
github.com/Masterminds/sprig/v3 v3.3.0 h1:mQh0Yrg1XPo6vjYXgtf5OtijNAKJRNcTdOOGZe3tPhs=
|
||||
github.com/Masterminds/sprig/v3 v3.3.0/go.mod h1:Zy1iXRYNqNLUolqCpL4uhk6SHUMAOSCzdgBfDb35Lz0=
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.8 h1:fuClo7Un+Bi0EDTnSIPWxVEoJcze+wkGzNA4Vo3jRcQ=
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.8/go.mod h1:I8lFSH4MGrZiw2m7miCrRYHsgpC1+HW8uiVGOiPYPcw=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.26 h1:SJNA4XaM8Z09adeTHf/vME93jzl44lJV70pSawIBXac=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.26/go.mod h1:l0ME5pd2QytH8UZzETcrt90x9Bm+3TyBVeWfVEcZ82c=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.16 h1:cqRWTY4fcctJGqZedMC1Khu4h2VsgRODUOIZwn39n3Y=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.16/go.mod h1:x8JgCOAnbnEVQOLVX6lOtsyTrG2EHgiRNqzku9jiVxA=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.11 h1:LTxHSJ/rwTFfaXqi5mlKKHJOW+shFB+HQ7NrCa4OQ48=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.11/go.mod h1:GF0bVKZVQX4UeUtPmTuWZj3gw0eRCdkVhW0xFnSRLmU=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.11 h1:DGUM0yztI3Zz5kePulT/mMP/gVZFGTTISUiCcqSQtT0=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.11/go.mod h1:kbcdJgN9cgSzZlUqsMveDl9IfexJqOzCihXjQXTNaPk=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.11 h1:kOHCwgWFyfrz9GqnJct8knQw7Apyl3xHH+OGnome77s=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.11/go.mod h1:854cZkT2fdkdrzQklD49uzxkZnajXoelpVNgT/yk4J0=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.24 h1:jGzHjc2jDx08Rji1JTBtMxuXUDG7muTEw+ExYXqFzPg=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.24/go.mod h1:uJt75pK2cEyY80F5C+DK9YkCkJWsMkJViHxLYZRHl8Y=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.12 h1:hfO8FblXX93ZKHXSCqCjehwul1EtNJa9G8iFSOiJ+6w=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.12/go.mod h1:r8Eypc7ZRNdS7BsaLBUUdcXIEuKE6cVHZJ29u+IGaIc=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.11 h1:PO7Tx443ecMI00nIxDrT99DVcbeMBQ6BUtJlDMRnY24=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.11/go.mod h1:n/9RixikO12YvNomZLP8k4TgUSoEkT1W23QIrpFAPvw=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.11 h1:jNP7wZylDeSJhjc1nmjZBuG5deotp3cEYIfMazmzp+Y=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.11/go.mod h1:1US+8JBnF0xNm6GE6wM5u5ugFwSZpkd2686gMuql5tI=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.9 h1:MLHGyNy4melTyk9e63s+9QgLKCHPDJrP+1NfJhtU1g4=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.9/go.mod h1:zfUkiNK2JiYlR1/SnU8aJ8Gj1nMFLkPZ69rVLv6jWPo=
|
||||
github.com/aws/smithy-go v1.28.8 h1:aEmdAq7gVCoGCnvf1NnELlu3I6D/nhqrC321FyFiQkQ=
|
||||
github.com/aws/smithy-go v1.28.8/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
|
||||
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
|
||||
github.com/fsnotify/fsnotify v1.9.0 h1:2Ml+OJNzbYCTzsxtv8vKSFD9PbJjmhYF14k/jKC7S9k=
|
||||
github.com/fsnotify/fsnotify v1.9.0/go.mod h1:8jBTzvmWwFyi3Pb8djgCCO5IBqzKJ/Jwo8TRcHyHii0=
|
||||
github.com/gabriel-vasile/mimetype v1.4.15 h1:05iP/CYtZ/w455R/KZM6rZ5ieAdh99UPtd+d3YzLmaI=
|
||||
github.com/gabriel-vasile/mimetype v1.4.15/go.mod h1:azpTcoLcDZRNgFou5j+APrqQx9HqVPWa6ijYQIIVswQ=
|
||||
github.com/go-chi/chi/v5 v5.5.0 h1:xdKQ1dJmVJsmgahrXSKhUx8NyVPzpwdlyrrQFpMPJgg=
|
||||
github.com/go-chi/chi/v5 v5.5.0/go.mod h1:d1IG7GtKPSjPyBP365PH8sAGVHk3qfB/IhjFNRH1wWM=
|
||||
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
|
||||
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
|
||||
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
|
||||
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
|
||||
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
|
||||
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
|
||||
github.com/go-playground/validator/v10 v10.30.5 h1:YyCXvVShZbs2Sm3Mb53eNOlhRXctSOzW5QJAouCTZL4=
|
||||
github.com/go-playground/validator/v10 v10.30.5/go.mod h1:wEqiaov48pXX1kjhc3Da8y0M0Dtg/BK7gurFBLgwFrQ=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/huandu/xstrings v1.5.0 h1:2ag3IFq9ZDANvthTwTiqSSZLjDc+BedvHPAp5tJy2TI=
|
||||
github.com/huandu/xstrings v1.5.0/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
|
||||
github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6 h1:D/V0gu4zQ3cL2WKeVNVM4r2gLxGGf6McLwgXzRTo2RQ=
|
||||
github.com/jackc/pgerrcode v0.0.0-20250907135507-afb5586c32a6/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.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/jackc/tern/v2 v2.4.3 h1:g293d3OZgW7OFEhsYXgEv0C21jea2boNr0VR4K5I7OY=
|
||||
github.com/jackc/tern/v2 v2.4.3/go.mod h1:rMpMuRYcff5wWLptoTSO1qcDxJ4OodysvK17i2SVBys=
|
||||
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/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||
github.com/leodido/go-urn v1.5.0 h1:pLqT2kq1zpHW/1D18QMjMpdtX7cekxqtJJjg5ANyWw0=
|
||||
github.com/leodido/go-urn v1.5.0/go.mod h1:9BORnCDhdPBJNDEX+w1bJisa8yOKYi116VeO96s4ifE=
|
||||
github.com/mcosta74/pgx-slog v0.4.1 h1:Rt25l/jE5tu1ioqPDrqY17Kv6REdsM0L9WCT+hFw1rw=
|
||||
github.com/mcosta74/pgx-slog v0.4.1/go.mod h1:BCpubkiENkWQ8MvZ4a9LJgWuBzC5UpWoJmQ1/SOlv+M=
|
||||
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
|
||||
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
|
||||
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
|
||||
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||
github.com/oapi-codegen/runtime v1.4.2 h1:GMxFVYLzoYLua+/KvzgSphkyK1lLTReQI9Vf4hvATKE=
|
||||
github.com/oapi-codegen/runtime v1.4.2/go.mod h1:GwV7hC2hviaMzj+ITfHVRESK5J2W/GefVwIND/bMGvU=
|
||||
github.com/pelletier/go-toml/v2 v2.4.3-0.20260624044523-35f78d5908f1 h1:gIZQQ2+wbsdQGWLPDSdJFptR25tBCjSC0KbSIQZ1rgE=
|
||||
github.com/pelletier/go-toml/v2 v2.4.3-0.20260624044523-35f78d5908f1/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
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/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
|
||||
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 h1:+jumHNA0Wrelhe64i8F6HNlS8pkoyMv5sreGx2Ry5Rw=
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8/go.mod h1:3n1Cwaq1E1/1lhQhtRK2ts/ZwZEhjcQeJQ1RuC6Q/8U=
|
||||
github.com/spf13/afero v1.15.0 h1:b/YBCLWAJdFWJTN9cLhiXXcD7mzKn9Dm86dNnfyQw1I=
|
||||
github.com/spf13/afero v1.15.0/go.mod h1:NC2ByUVxtQs4b3sIUphxK0NioZnmxgyCrfzeuq8lxMg=
|
||||
github.com/spf13/cast v1.10.0 h1:h2x0u2shc1QuLHfxi+cTJvs30+ZAHOGRic8uyGTDWxY=
|
||||
github.com/spf13/cast v1.10.0/go.mod h1:jNfB8QC9IA6ZuY2ZjDp0KtFO2LZZlg4S/7bzP6qqeHo=
|
||||
github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
|
||||
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||
github.com/spf13/viper v1.21.0 h1:x5S+0EU27Lbphp4UKm1C+1oQO+rKx36vfCoaVebLFSU=
|
||||
github.com/spf13/viper v1.21.0/go.mod h1:P0lhsswPGWD/1lZJ9ny3fYnVqxiegrlNrEmgLjbTCAY=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/vgarvardt/pgx-google-uuid/v5 v5.6.0 h1:EhPtK0mgrgaTMXpegE69hvoSOVC1Ahk8+QJ9B8b+OdU=
|
||||
github.com/vgarvardt/pgx-google-uuid/v5 v5.6.0/go.mod h1:5LtFrNEkgzxHvXPO9eOvcXsSn9/KeKYgx9kjeI2oXQI=
|
||||
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.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
|
||||
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/crypto v0.57.0 h1:3ZVCjf8Ggz7zneR/EHRVx68Ctf+2pmIMP2UFhh9cC6M=
|
||||
golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260817212433-ac3dfec99bb1 h1:utmqiUzZAEINbLmRyAt1QjVkMyLQ+lvIwCpLaB1rZfw=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20260817212433-ac3dfec99bb1/go.mod h1:4Hqkh8ycfw05ld/3BWL7rJOSfebL2Q+DVDeRgYgxUU8=
|
||||
google.golang.org/grpc v1.85.0 h1:BPBCfuVRQ1qvBhKDOq9uvQllMvO59rKsYhXpp7Dd8vw=
|
||||
google.golang.org/grpc v1.85.0/go.mod h1:0rQrOD58yjal/0EaVbGBANindixsCZNQPDV3VKtaKuQ=
|
||||
google.golang.org/protobuf v1.36.12 h1:pJOKDDOyeXErUroCihFAd5LQuwXBSpVnKGrj5o/fwxc=
|
||||
google.golang.org/protobuf v1.36.12/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
@@ -0,0 +1 @@
|
||||
package http
|
||||
@@ -0,0 +1,91 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/auth/jwtprovider"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/attachment"
|
||||
"io"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type AttachmentHandler struct {
|
||||
router router.Router
|
||||
provider jwtprovider.Provider
|
||||
svc *attachment.Service
|
||||
}
|
||||
|
||||
func NewAttachmentHandler(r router.Router, p jwtprovider.Provider, s *attachment.Service) Handler {
|
||||
return &AttachmentHandler{r, p, s}
|
||||
}
|
||||
func (h *AttachmentHandler) RegisterRoutes() {
|
||||
h.router.Private("POST", "/api/v1/chats/{chat_id}/attachments", h.Upload)
|
||||
h.router.Private("GET", "/api/v1/attachments/{attachment_id}", h.Get)
|
||||
h.router.Private("GET", "/api/v1/attachments/{attachment_id}/content", h.Get)
|
||||
}
|
||||
func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
||||
user, e := framework.ParseToken(r, h.provider)
|
||||
if e != nil {
|
||||
framework.JSONError(r.Context(), w, e)
|
||||
return
|
||||
}
|
||||
id, e := framework.ParseIDFromPathParam(r.Context(), w, r, "chat_id")
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
values := r.URL.Query()["filename"]
|
||||
if len(values) != 1 {
|
||||
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeBadRequest, "expected one filename"))
|
||||
return
|
||||
}
|
||||
a, e := h.svc.Upload(w, r, user.UserID, id, values[0])
|
||||
if e != nil {
|
||||
if apperr.CodeFromError(e) == apperr.CodeTooManyRequests {
|
||||
w.Header().Set("Retry-After", "1")
|
||||
}
|
||||
framework.JSONError(r.Context(), w, e)
|
||||
return
|
||||
}
|
||||
framework.JSONResponse(w, http.StatusCreated, a)
|
||||
}
|
||||
func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
user, e := framework.ParseToken(r, h.provider)
|
||||
if e != nil {
|
||||
framework.JSONError(r.Context(), w, e)
|
||||
return
|
||||
}
|
||||
id, e := framework.ParseIDFromPathParam(r.Context(), w, r, "attachment_id")
|
||||
if e != nil {
|
||||
return
|
||||
}
|
||||
a, e := h.svc.Get(r.Context(), user.UserID, id)
|
||||
if e != nil {
|
||||
framework.JSONError(r.Context(), w, e)
|
||||
return
|
||||
}
|
||||
if !strings.HasSuffix(r.URL.Path, "/content") {
|
||||
framework.JSONResponse(w, 200, a.Public)
|
||||
return
|
||||
}
|
||||
if v := r.URL.Query().Get("variant"); v != "" && v != "original" {
|
||||
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeBadRequest, "invalid variant"))
|
||||
return
|
||||
}
|
||||
obj, e := h.svc.Store.Get(r.Context(), a.Key)
|
||||
if e != nil {
|
||||
framework.JSONError(r.Context(), w, attachment.Unavailable())
|
||||
return
|
||||
}
|
||||
defer obj.Body.Close()
|
||||
w.Header().Set("Content-Type", string(a.Public.ContentType))
|
||||
w.Header().Set("Content-Length", strconv.Itoa(a.Public.SizeBytes))
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
// Once streaming has started a transport failure terminates the response; no JSON
|
||||
// error can be appended to an image. Content-Length exposes incomplete downloads.
|
||||
if _, e = io.Copy(w, obj.Body); e != nil {
|
||||
panic(http.ErrAbortHandler)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/gen/contracts"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/auth"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
router router.Router
|
||||
|
||||
auth auth.API
|
||||
}
|
||||
|
||||
func NewAuthHandler(r router.Router, api auth.API) Handler {
|
||||
return &AuthHandler{
|
||||
router: r,
|
||||
auth: api,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AuthHandler) RegisterRoutes() {
|
||||
h.router.Public(http.MethodPost, "/api/v1/auth/sessions", h.Login)
|
||||
h.router.Private(http.MethodDelete, "/api/v1/auth/sessions/current", h.Logout)
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || username == "" || password == "" {
|
||||
w.Header().Set(
|
||||
"WWW-Authenticate",
|
||||
`Basic realm="messenger", charset="UTF-8"`,
|
||||
)
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeUnauthorized, "invalid credentials"))
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.auth.Login(ctx, dto.Login{
|
||||
Username: username,
|
||||
Password: password,
|
||||
})
|
||||
if err != nil {
|
||||
if apperr.HTTPStatusFromError(err) == http.StatusUnauthorized {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
}
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusCreated, contracts.Session{
|
||||
ExpiresAt: resp.ExpiresAt,
|
||||
Token: resp.Tokens.RefreshToken.Payload,
|
||||
TokenType: resp.TokenType,
|
||||
User: contracts.User{
|
||||
CreatedAt: resp.User.CreatedAt,
|
||||
DisplayName: resp.User.DisplayName,
|
||||
Id: resp.User.ID,
|
||||
Username: resp.User.Username,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AuthHandler) Logout(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
token, exists := framework.Token(r)
|
||||
if !exists {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeUnauthorized, "missing token in header"))
|
||||
return
|
||||
}
|
||||
|
||||
err := h.auth.Logout(ctx, dto.RefreshToken{
|
||||
Payload: token,
|
||||
})
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusNoContent, nil)
|
||||
}
|
||||
@@ -0,0 +1,287 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/gen/contracts"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/auth/jwtprovider"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/validator"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/chat"
|
||||
)
|
||||
|
||||
var messageKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
type ChatHandler struct {
|
||||
router router.Router
|
||||
provider jwtprovider.Provider
|
||||
validator *validator.ValidatorImpl
|
||||
|
||||
chat chat.API
|
||||
}
|
||||
|
||||
func NewChatHandler(r router.Router, provider jwtprovider.Provider, v *validator.ValidatorImpl, api chat.API) Handler {
|
||||
return &ChatHandler{
|
||||
router: r,
|
||||
provider: provider,
|
||||
validator: v,
|
||||
chat: api,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *ChatHandler) RegisterRoutes() {
|
||||
h.router.Private(http.MethodPost, "/api/v1/chats", h.Create)
|
||||
h.router.Private(http.MethodGet, "/api/v1/chats", h.Get)
|
||||
h.router.Private(http.MethodGet, "/api/v1/chats/{chat_id}", h.GetByID)
|
||||
h.router.Private(http.MethodPost, "/api/v1/chats/{chat_id}/messages", h.SendMessage)
|
||||
h.router.Private(http.MethodGet, "/api/v1/chats/{chat_id}/messages", h.GetMessages)
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
userData, err := framework.ParseToken(r, h.provider)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
var req contracts.CreateChat
|
||||
if !framework.Decode(w, r, &req) || !framework.Validate(w, r, h.validator, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if userData.UserID == uuid.Nil ||
|
||||
!validText(req.Title, 100) ||
|
||||
len(req.MemberIds) < 1 ||
|
||||
len(req.MemberIds) > 100 {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid chat fields"))
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.chat.CreateChat(ctx, dto.CreateChat{
|
||||
CreatorID: userData.UserID,
|
||||
Title: req.Title,
|
||||
MemberIDs: req.MemberIds,
|
||||
})
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusCreated, contracts.Chat{
|
||||
Id: resp.ID,
|
||||
Title: resp.Title,
|
||||
MemberIds: resp.MemberIDs,
|
||||
CreatedAt: resp.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
userData, err := framework.ParseToken(r, h.provider)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
limit, afterID, ok := framework.PageParams(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
if userData.UserID == uuid.Nil {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid user id"))
|
||||
return
|
||||
}
|
||||
if afterID != nil && *afterID == uuid.Nil {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid cursor"))
|
||||
return
|
||||
}
|
||||
|
||||
page, err := h.chat.ListForMember(ctx, dto.ChatFilters{
|
||||
UserID: userData.UserID,
|
||||
Limit: limit,
|
||||
AfterID: afterID,
|
||||
})
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]contracts.Chat, 0, len(page.Items))
|
||||
for _, item := range page.Items {
|
||||
items = append(items, contracts.Chat{
|
||||
CreatedAt: item.CreatedAt,
|
||||
Id: item.ID,
|
||||
MemberIds: item.MemberIDs,
|
||||
Title: item.Title,
|
||||
})
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusOK, contracts.ChatPage{
|
||||
Items: items,
|
||||
NextCursor: cursorString(page.NextCursorID),
|
||||
})
|
||||
}
|
||||
|
||||
func (h *ChatHandler) GetByID(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
userData, err := framework.ParseToken(r, h.provider)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
chatID, err := framework.ParseIDFromPathParam(ctx, w, r, "chat_id")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.chat.GetForMember(ctx, dto.ChatMember{
|
||||
UserID: userData.UserID,
|
||||
ChatID: chatID,
|
||||
})
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusOK, contracts.Chat{
|
||||
CreatedAt: resp.CreatedAt,
|
||||
Id: resp.ID,
|
||||
MemberIds: resp.MemberIDs,
|
||||
Title: resp.Title,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *ChatHandler) SendMessage(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
userData, err := framework.ParseToken(r, h.provider)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
chatID, err := framework.ParseIDFromPathParam(ctx, w, r, "chat_id")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
keyValues := r.Header.Values("Idempotency-Key")
|
||||
if len(keyValues) != 1 || !messageKeyPattern.MatchString(keyValues[0]) {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid idempotency key"))
|
||||
return
|
||||
}
|
||||
|
||||
var req struct {
|
||||
Text *string `json:"text"`
|
||||
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
||||
}
|
||||
if !framework.Decode(w, r, &req) || !framework.Validate(w, r, h.validator, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if req.Text == nil {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "text is required"))
|
||||
return
|
||||
}
|
||||
|
||||
ids := []uuid.UUID{}
|
||||
if len(req.AttachmentIDs) > 0 {
|
||||
if bytes.Equal(bytes.TrimSpace(req.AttachmentIDs), []byte("null")) || json.Unmarshal(req.AttachmentIDs, &ids) != nil {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid attachment_ids"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
message, err := h.chat.CreateMessage(ctx, dto.CreateMessage{
|
||||
IdempotencyKey: keyValues[0],
|
||||
ChatID: chatID,
|
||||
SenderID: userData.UserID,
|
||||
Text: *req.Text,
|
||||
AttachmentIDs: ids,
|
||||
})
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusCreated, contracts.Message{
|
||||
Id: message.ID,
|
||||
ChatId: message.ChatID,
|
||||
SenderId: message.SenderID,
|
||||
Text: message.Text,
|
||||
CreatedAt: message.CreatedAt,
|
||||
AttachmentIds: message.AttachmentIDs,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *ChatHandler) GetMessages(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
userData, err := framework.ParseToken(r, h.provider)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
chatID, err := framework.ParseIDFromPathParam(ctx, w, r, "chat_id")
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
limit, afterID, ok := framework.PageParams(w, r)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
page, err := h.chat.ListForChat(ctx, dto.MessageFilters{
|
||||
ChatID: chatID,
|
||||
UserID: userData.UserID,
|
||||
Limit: limit,
|
||||
AfterID: afterID,
|
||||
})
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]contracts.Message, 0, len(page.Items))
|
||||
for _, message := range page.Items {
|
||||
items = append(items, contracts.Message{
|
||||
Id: message.ID,
|
||||
ChatId: message.ChatID,
|
||||
SenderId: message.SenderID,
|
||||
Text: message.Text,
|
||||
CreatedAt: message.CreatedAt,
|
||||
AttachmentIds: message.AttachmentIDs,
|
||||
})
|
||||
}
|
||||
framework.JSONResponse(w, http.StatusOK, contracts.MessagePage{Items: items, NextCursor: cursorString(page.NextCursorID)})
|
||||
}
|
||||
|
||||
func cursorString(id *uuid.UUID) *string {
|
||||
if id == nil {
|
||||
return nil
|
||||
}
|
||||
value := id.String()
|
||||
return &value
|
||||
}
|
||||
|
||||
func validText(value string, maxLength int) bool {
|
||||
return utf8.ValidString(value) && strings.TrimSpace(value) != "" && utf8.RuneCountInString(value) <= maxLength
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"sync/atomic"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
||||
)
|
||||
|
||||
type Server interface {
|
||||
ShouldBeRunning() bool
|
||||
Ready() bool
|
||||
Start(ctx context.Context) error
|
||||
Stop(ctx context.Context) error
|
||||
}
|
||||
|
||||
type server struct {
|
||||
server http.Server
|
||||
cfg *config.Server
|
||||
ready atomic.Bool
|
||||
}
|
||||
|
||||
func New(cfg *config.Config, router router.Router) Server {
|
||||
s := server{
|
||||
server: http.Server{
|
||||
Addr: cfg.Server.BindAddress(),
|
||||
Handler: router,
|
||||
ReadTimeout: cfg.Server.ReadTimeout,
|
||||
WriteTimeout: cfg.Server.WriteTimeout,
|
||||
IdleTimeout: cfg.Server.IdleTimeout,
|
||||
},
|
||||
cfg: cfg.Server,
|
||||
ready: atomic.Bool{},
|
||||
}
|
||||
|
||||
if err := s.validate(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s *server) Start(_ context.Context) error {
|
||||
if !s.ShouldBeRunning() {
|
||||
slog.Info("server should not be running: skip starting")
|
||||
return nil
|
||||
}
|
||||
listener, err := net.Listen("tcp", s.server.Addr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s.ready.Store(true)
|
||||
go func() {
|
||||
defer s.ready.Store(false)
|
||||
if err := s.server.Serve(listener); err != nil &&
|
||||
!errors.Is(err, http.ErrServerClosed) {
|
||||
slog.Error("rest server failed", "error", err)
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *server) Stop(ctx context.Context) error {
|
||||
if !s.ShouldBeRunning() {
|
||||
slog.Info("server should not be running: skip stopping")
|
||||
return nil
|
||||
}
|
||||
|
||||
slog.Info("shutting down rest server...", "address", s.server.Addr)
|
||||
s.ready.Store(false)
|
||||
if err := s.server.Shutdown(ctx); err != nil {
|
||||
slog.Error("rest server shutdown failed", "error", err)
|
||||
_ = s.server.Close()
|
||||
return err
|
||||
}
|
||||
slog.Info("rest server shutdown successfully")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *server) ShouldBeRunning() bool {
|
||||
return s.cfg.Enabled
|
||||
}
|
||||
|
||||
func (s *server) Ready() bool {
|
||||
return s.ready.Load()
|
||||
}
|
||||
|
||||
func (s *server) validate() error {
|
||||
if s.cfg == nil {
|
||||
slog.Error("failed to validate server: provided nil config")
|
||||
return errors.New("failed to validate server: provided nil config")
|
||||
}
|
||||
|
||||
slog.Info("server successfully validated")
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package http
|
||||
|
||||
type Handler interface {
|
||||
RegisterRoutes()
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/gen/contracts"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
||||
)
|
||||
|
||||
type databasePinger interface{ Ping(context.Context) error }
|
||||
|
||||
type HealthHandler struct {
|
||||
router router.Router
|
||||
server Server
|
||||
database databasePinger
|
||||
storage []databasePinger
|
||||
}
|
||||
|
||||
func NewHealthHandler(r router.Router, server Server, database databasePinger, storage ...databasePinger) Handler {
|
||||
return &HealthHandler{
|
||||
router: r,
|
||||
server: server,
|
||||
database: database,
|
||||
storage: storage,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *HealthHandler) RegisterRoutes() {
|
||||
h.router.Public(http.MethodGet, "/health/ready", h.Ready)
|
||||
h.router.Public(http.MethodGet, "/health/live", h.Live)
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Live(w http.ResponseWriter, _ *http.Request) {
|
||||
framework.JSONResponse(w, http.StatusOK, contracts.Health{Status: contracts.Ok})
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Ready(w http.ResponseWriter, r *http.Request) {
|
||||
if !h.server.Ready() {
|
||||
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeUnavailable, "server is not ready"))
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 200*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := h.database.Ping(ctx); err != nil {
|
||||
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeUnavailable, "database unavailable"))
|
||||
return
|
||||
}
|
||||
for _, dependency := range h.storage {
|
||||
if err := dependency.Ping(ctx); err != nil {
|
||||
framework.JSONError(r.Context(), w, apperr.New(apperr.CodeUnavailable, "storage unavailable"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusOK, contracts.Health{Status: contracts.Ok})
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package http
|
||||
@@ -0,0 +1,113 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/gen/contracts"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/auth/jwtprovider"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/framework"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/rest/router"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/validator"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/service/user"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
router router.Router
|
||||
provider jwtprovider.Provider
|
||||
validator *validator.ValidatorImpl
|
||||
|
||||
user user.API
|
||||
}
|
||||
|
||||
func NewUserHandler(r router.Router, provider jwtprovider.Provider, v *validator.ValidatorImpl, api user.API) Handler {
|
||||
return &UserHandler{
|
||||
router: r,
|
||||
provider: provider,
|
||||
validator: v,
|
||||
user: api,
|
||||
}
|
||||
}
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
func (h *UserHandler) RegisterRoutes() {
|
||||
h.router.Public(http.MethodPost, "/api/v1/users", h.Create)
|
||||
h.router.Private(http.MethodGet, "/api/v1/users/me", h.GetMe)
|
||||
}
|
||||
|
||||
func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
var req contracts.CreateUser
|
||||
if !framework.Decode(w, r, &req) || !framework.Validate(w, r, h.validator, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
toCreate := dto.CreateUser{
|
||||
Username: req.Username,
|
||||
DisplayName: req.DisplayName,
|
||||
}
|
||||
|
||||
if req.Password != nil {
|
||||
toCreate.Password = *req.Password
|
||||
}
|
||||
|
||||
if !usernamePattern.MatchString(req.Username) || !utf8.ValidString(req.DisplayName) ||
|
||||
strings.TrimSpace(req.DisplayName) == "" || utf8.RuneCountInString(req.DisplayName) > 100 {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid user fields"))
|
||||
return
|
||||
}
|
||||
passwordLen := utf8.RuneCountInString(toCreate.Password)
|
||||
if !utf8.ValidString(toCreate.Password) || strings.TrimSpace(toCreate.Password) == "" || passwordLen < 12 || passwordLen > 128 {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid password"))
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.user.Create(ctx, toCreate)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusCreated, contracts.User{
|
||||
CreatedAt: resp.CreatedAt,
|
||||
DisplayName: resp.DisplayName,
|
||||
Id: resp.ID,
|
||||
Username: resp.Username,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *UserHandler) GetMe(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := r.Context()
|
||||
|
||||
userData, err := framework.ParseToken(r, h.provider)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
if userData.UserID == uuid.Nil {
|
||||
framework.JSONError(ctx, w, apperr.New(apperr.CodeBadRequest, "invalid user id"))
|
||||
return
|
||||
}
|
||||
|
||||
resp, err := h.user.GetByID(ctx, userData.UserID)
|
||||
if err != nil {
|
||||
framework.JSONError(ctx, w, err)
|
||||
return
|
||||
}
|
||||
|
||||
framework.JSONResponse(w, http.StatusOK, contracts.User{
|
||||
CreatedAt: resp.CreatedAt,
|
||||
DisplayName: resp.DisplayName,
|
||||
Id: resp.ID,
|
||||
Username: resp.Username,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package outside
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/cache"
|
||||
)
|
||||
|
||||
const (
|
||||
// chatRedisKey equals "chat:{chat_id}" path
|
||||
chatRedisKey = "chat:%s"
|
||||
)
|
||||
|
||||
type ChatCache struct {
|
||||
client *cache.Client
|
||||
}
|
||||
|
||||
func NewChatCache(client *cache.Client) *ChatCache {
|
||||
return &ChatCache{
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
type chat struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []uuid.UUID `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (r *ChatCache) Create(ctx context.Context, req dto.Chat) error {
|
||||
data, err := json.Marshal(chat{
|
||||
ID: req.ID,
|
||||
Title: req.Title,
|
||||
MemberIDs: req.MemberIDs,
|
||||
CreatedAt: req.CreatedAt,
|
||||
})
|
||||
if err != nil {
|
||||
return apperr.Wrap(err, apperr.CodeInternal, "failed to marshal chat cache")
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, cache.OperationTimeout)
|
||||
defer cancel()
|
||||
err = r.client.Set(ctx, fmt.Sprintf(chatRedisKey, req.ID), data, 0).Err()
|
||||
if err != nil {
|
||||
return apperr.WrapRedis(err, "failed to create chat cache")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ChatCache) Get(ctx context.Context, chatID uuid.UUID) (*dto.Chat, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, cache.OperationTimeout)
|
||||
defer cancel()
|
||||
data, err := r.client.Get(ctx, fmt.Sprintf(chatRedisKey, chatID.String())).Bytes()
|
||||
if err != nil {
|
||||
return nil, apperr.WrapRedis(err, "failed to read chat cache")
|
||||
}
|
||||
|
||||
var item chat
|
||||
if err = json.Unmarshal(data, &item); err != nil {
|
||||
return nil, apperr.Wrap(err, apperr.CodeInternal, "failed to unmarshal chat cache")
|
||||
}
|
||||
|
||||
return &dto.Chat{
|
||||
ID: item.ID,
|
||||
Title: item.Title,
|
||||
MemberIDs: item.MemberIDs,
|
||||
CreatedAt: item.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func (r *AttachmentsRepo) Page(ctx context.Context, after uuid.UUID) ([]*AttachmentRecord, error) {
|
||||
rows, e := r.Pool.Query(ctx, "SELECT "+AttachmentColumns+" FROM attachments WHERE id>$1 ORDER BY id LIMIT 100", after)
|
||||
if e != nil {
|
||||
return nil, apperr.WrapPostgres(e, "scan failed")
|
||||
}
|
||||
defer rows.Close()
|
||||
out := make([]*AttachmentRecord, 0, 100)
|
||||
for rows.Next() {
|
||||
a, e := ScanAttachment(rows)
|
||||
if e != nil {
|
||||
return nil, e
|
||||
}
|
||||
out = append(out, a)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
func (r *AttachmentsRepo) Exists(ctx context.Context, c *pgxpool.Conn, key string) (bool, error) {
|
||||
var found bool
|
||||
e := c.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM attachments WHERE object_key=$1)", key).Scan(&found)
|
||||
return found, e
|
||||
}
|
||||
func (r *AttachmentsRepo) MarkMissing(ctx context.Context, c *pgxpool.Conn, id uuid.UUID, missing bool) (bool, error) {
|
||||
tag, e := c.Exec(ctx, "UPDATE attachments SET integrity_missing=$2,updated_at=clock_timestamp() WHERE id=$1 AND integrity_missing<>$2", id, missing)
|
||||
return tag.RowsAffected() == 1, e
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/gen/contracts"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
)
|
||||
|
||||
type AttachmentRecord struct {
|
||||
Public contracts.Attachment
|
||||
Key string
|
||||
Missing bool
|
||||
}
|
||||
type AttachmentsRepo struct{ Pool *pgxpool.Pool }
|
||||
|
||||
const AttachmentColumns = "id,chat_id,owner_id,filename,content_type,size_bytes,sha256,created_at,updated_at,object_key,integrity_missing"
|
||||
|
||||
func ScanAttachment(row pgx.Row) (*AttachmentRecord, error) {
|
||||
a := new(AttachmentRecord)
|
||||
p := &a.Public
|
||||
if err := row.Scan(&p.Id, &p.ChatId, &p.OwnerId, &p.Filename, &p.ContentType, &p.SizeBytes, &p.Sha256, &p.CreatedAt, &p.UpdatedAt, &a.Key, &a.Missing); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "attachment unavailable")
|
||||
}
|
||||
p.Status = "ready"
|
||||
p.Variants = []contracts.Variant{}
|
||||
return a, nil
|
||||
}
|
||||
func (r *AttachmentsRepo) Get(ctx context.Context, id uuid.UUID) (*AttachmentRecord, error) {
|
||||
return ScanAttachment(r.Pool.QueryRow(ctx, "SELECT "+AttachmentColumns+" FROM attachments WHERE id=$1", id))
|
||||
}
|
||||
func (r *AttachmentsRepo) Save(ctx context.Context, conn *pgxpool.Conn, a *AttachmentRecord) error {
|
||||
p := a.Public
|
||||
_, e := conn.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,created_at,updated_at,object_key) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, p.Id, p.ChatId, p.OwnerId, p.Filename, p.ContentType, p.SizeBytes, p.Sha256, p.CreatedAt, p.UpdatedAt, a.Key)
|
||||
if e != nil {
|
||||
return apperr.WrapPostgres(e, "failed to save attachment")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Session locks span S3 work and the database commit. A lost connection releases
|
||||
// the lock; the mandatory grace period also exceeds the maximum upload lifetime.
|
||||
func (r *AttachmentsRepo) Lock(ctx context.Context, key string) (*pgxpool.Conn, func(), error) {
|
||||
c, e := r.Pool.Acquire(ctx)
|
||||
if e != nil {
|
||||
return nil, nil, apperr.WrapPostgres(e, "database unavailable")
|
||||
}
|
||||
if _, e = c.Exec(ctx, "SELECT pg_advisory_lock(hashtextextended($1, 42))", key); e != nil {
|
||||
// The server may have acquired the session lock before cancellation was
|
||||
// observed by the client. Never return that uncertain session to the pool.
|
||||
clean, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
_ = c.Conn().Close(clean)
|
||||
cancel()
|
||||
c.Release()
|
||||
return nil, nil, apperr.WrapPostgres(e, "lock unavailable")
|
||||
}
|
||||
return c, func() {
|
||||
// Close rather than return a possibly locked session when unlock fails.
|
||||
clean, cancel := context.WithTimeout(context.Background(), 2*time.Second)
|
||||
defer cancel()
|
||||
if _, e := c.Exec(clean, "SELECT pg_advisory_unlock(hashtextextended($1, 42))", key); e != nil {
|
||||
_ = c.Conn().Close(clean)
|
||||
}
|
||||
c.Release()
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
)
|
||||
|
||||
type ChatsRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewChatsRepo(pool *pgxpool.Pool) *ChatsRepo {
|
||||
return &ChatsRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *ChatsRepo) CreateTx(ctx context.Context, tx pgx.Tx, req dto.Chat) error {
|
||||
const insertChat = `INSERT INTO chats (id, title, created_at) VALUES ($1, $2, $3)`
|
||||
if _, err := tx.Exec(ctx, insertChat, req.ID, req.Title, req.CreatedAt); err != nil {
|
||||
return apperr.WrapPostgres(err, "failed to create chat")
|
||||
}
|
||||
const insertMembers = `INSERT INTO chat_members (chat_id, user_id)
|
||||
SELECT $1, user_id FROM unnest($2::uuid[]) AS members(user_id)`
|
||||
if _, err := tx.Exec(ctx, insertMembers, req.ID, req.MemberIDs); err != nil {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok &&
|
||||
pgErr.Code == pgerrcode.ForeignKeyViolation && pgErr.ConstraintName == "chat_members_user_fk" {
|
||||
return apperr.Wrap(err, apperr.CodeBadRequest, "unknown chat member")
|
||||
}
|
||||
return apperr.WrapPostgres(err, "failed to create chat members")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *ChatsRepo) GetForMember(ctx context.Context, req dto.ChatMember) (*dto.Chat, error) {
|
||||
const query = `SELECT c.id, c.title, c.created_at,
|
||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = c.id ORDER BY m.user_id)
|
||||
FROM chats c
|
||||
WHERE c.id = $1
|
||||
AND EXISTS (SELECT 1 FROM chat_members access WHERE access.chat_id = c.id AND access.user_id = $2)`
|
||||
return scanChat(r.pool.QueryRow(ctx, query, req.ChatID, req.UserID))
|
||||
}
|
||||
|
||||
func (r *ChatsRepo) ListForMember(ctx context.Context, userID uuid.UUID, page dto.PageQuery) ([]dto.Chat, error) {
|
||||
query := `WITH page AS MATERIALIZED (
|
||||
SELECT c.id, c.title, c.created_at
|
||||
FROM chats c JOIN chat_members access ON access.chat_id = c.id
|
||||
WHERE access.user_id = $1`
|
||||
args := []any{userID, page.Limit}
|
||||
if page.After != nil {
|
||||
query += ` AND (c.created_at, c.id) > ($3, $4)`
|
||||
args = append(args, page.After.CreatedAt, page.After.ID)
|
||||
}
|
||||
query += ` ORDER BY c.created_at, c.id LIMIT $2
|
||||
)
|
||||
SELECT p.id, p.title, p.created_at,
|
||||
ARRAY(SELECT m.user_id FROM chat_members m WHERE m.chat_id = p.id ORDER BY m.user_id)
|
||||
FROM page p ORDER BY p.created_at, p.id`
|
||||
rows, err := r.pool.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to list chats")
|
||||
}
|
||||
defer rows.Close()
|
||||
chats := make([]dto.Chat, 0)
|
||||
for rows.Next() {
|
||||
chat, err := scanChat(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
chats = append(chats, *chat)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to list chats")
|
||||
}
|
||||
return chats, nil
|
||||
}
|
||||
|
||||
func scanChat(row pgx.Row) (*dto.Chat, error) {
|
||||
var chat dto.Chat
|
||||
if err := row.Scan(&chat.ID, &chat.Title, &chat.CreatedAt, &chat.MemberIDs); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to get chat")
|
||||
}
|
||||
return &chat, nil
|
||||
}
|
||||
@@ -0,0 +1,303 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/migrator"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres/txmanager"
|
||||
chatsvc "gitea.privcloud.tech/mtusi2026/internal/service/chat"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// The explicit runner supplies a newly created database; never alter the API database.
|
||||
func TestIdempotencyTransactionAndExpiry(t *testing.T) {
|
||||
database := os.Getenv("TEST_DATABASE_NAME")
|
||||
if database == "" {
|
||||
t.Skip("run make test-storage for an isolated PostgreSQL database")
|
||||
}
|
||||
if database == os.Getenv("POSTGRES_DATABASE") {
|
||||
t.Fatal("test database must differ from application database")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cfg, err := config.New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.Postgres.Database = database
|
||||
cfg.Postgres.RunMigrations = true
|
||||
if err = migrator.New(cfg).Start(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pool := postgres.New(cfg.Postgres)
|
||||
defer pool.Close()
|
||||
repo := NewMessagesRepo(pool)
|
||||
user, chat := uuid.New(), uuid.New()
|
||||
if _, err = pool.Exec(ctx, `INSERT INTO users(id,name,username,password_hash) VALUES($1,'Synthetic','atomic_test','unused')`, user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = pool.Exec(ctx, `INSERT INTO chats(id,title) VALUES($1,'Atomic test')`, chat); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = pool.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)`, chat, user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := dto.CreateMessage{ChatID: chat, SenderID: user, Text: "atomic", IdempotencyKey: "atomic-key"}
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = repo.CreateIdempotentTx(ctx, tx, req); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = tx.Rollback(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var messages, keys int
|
||||
if err = pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM messages),(SELECT count(*) FROM message_idempotency)`).Scan(&messages, &keys); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if messages != 0 || keys != 0 {
|
||||
t.Fatalf("rollback left %d messages, %d keys", messages, keys)
|
||||
}
|
||||
tx, err = pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
first, err := repo.CreateIdempotentTx(ctx, tx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Expire the test record only: no shortened production TTL and no wall-clock wait.
|
||||
if _, err = pool.Exec(ctx, `UPDATE message_idempotency SET expires_at=clock_timestamp()-interval '1 second' WHERE user_id=$1`, user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Text = "new after expiry"
|
||||
tx, err = pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := repo.CreateIdempotentTx(ctx, tx, req)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if first.ID == second.ID {
|
||||
t.Fatal("expired key replayed old message")
|
||||
}
|
||||
if err = pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM messages),(SELECT count(*) FROM message_idempotency)`).Scan(&messages, &keys); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if messages != 2 || keys != 1 {
|
||||
t.Fatalf("expiry left %d messages, %d keys; want 2,1", messages, keys)
|
||||
}
|
||||
if _, err = pool.Exec(ctx, `CREATE FUNCTION reject_test_key() RETURNS trigger LANGUAGE plpgsql AS $$ BEGIN IF NEW.key='failure-key' THEN RAISE EXCEPTION 'synthetic failure'; END IF; RETURN NEW; END $$; CREATE TRIGGER reject_test_key BEFORE INSERT ON message_idempotency FOR EACH ROW EXECUTE FUNCTION reject_test_key()`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.IdempotencyKey = "failure-key"
|
||||
tx, err = pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = repo.CreateIdempotentTx(ctx, tx, req); err == nil {
|
||||
t.Fatal("expected injected failure")
|
||||
}
|
||||
if err = tx.Rollback(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM messages),(SELECT count(*) FROM message_idempotency)`).Scan(&messages, &keys); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if messages != 2 || keys != 1 {
|
||||
t.Fatal("failed result persisted a side effect")
|
||||
}
|
||||
if _, err = pool.Exec(ctx, `DROP TRIGGER reject_test_key ON message_idempotency; DROP FUNCTION reject_test_key()`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tx, err = pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = repo.CreateIdempotentTx(ctx, tx, req); err != nil {
|
||||
t.Fatal("transient failure was cached", err)
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
t.Run("service owns message content rules", func(t *testing.T) {
|
||||
svc := chatsvc.New(txmanager.New(pool), NewChatsRepo(pool), repo, uncachedChats{})
|
||||
own, foreign, missing := uuid.New(), uuid.New(), uuid.New()
|
||||
otherChat := uuid.New()
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO chats(id,title) VALUES($1,'Other chat');`, otherChat); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)`, otherChat, user); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, fixture := range []struct {
|
||||
id, chat uuid.UUID
|
||||
missing bool
|
||||
}{{own, chat, false}, {foreign, otherChat, false}, {missing, chat, true}} {
|
||||
if _, err := pool.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,integrity_missing,created_at,updated_at) VALUES($1,$2,$3,'test.png',$4,'image/png',1,$5,$6,now(),now())`, fixture.id, fixture.chat, user, "originals/"+fixture.id.String(), strings.Repeat("a", 64), fixture.missing); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
counts := func() string {
|
||||
t.Helper()
|
||||
var messages, links, keys int
|
||||
if err := pool.QueryRow(ctx, `SELECT (SELECT count(*) FROM messages),(SELECT count(*) FROM message_attachments),(SELECT count(*) FROM message_idempotency)`).Scan(&messages, &links, &keys); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return fmt.Sprintf("%d/%d/%d", messages, links, keys)
|
||||
}
|
||||
for _, tc := range []struct {
|
||||
name, text string
|
||||
ids []uuid.UUID
|
||||
code apperr.Code
|
||||
}{
|
||||
{"empty", "", nil, apperr.CodeBadRequest},
|
||||
{"whitespace", " \t\n\u2003", nil, apperr.CodeBadRequest},
|
||||
{"invalid UTF8", string([]byte{0xff}), nil, apperr.CodeBadRequest},
|
||||
{"nul", "\x00", nil, apperr.CodeBadRequest},
|
||||
{"too long", strings.Repeat("я", 4001), nil, apperr.CodeBadRequest},
|
||||
{"duplicate", "", []uuid.UUID{own, own}, apperr.CodeBadRequest},
|
||||
{"too many", "text", make([]uuid.UUID, 11), apperr.CodeBadRequest},
|
||||
{"nil id", "", []uuid.UUID{uuid.Nil}, apperr.CodeBadRequest},
|
||||
{"unknown", "", []uuid.UUID{uuid.New()}, apperr.CodeNotFound},
|
||||
{"foreign chat", "", []uuid.UUID{foreign}, apperr.CodeNotFound},
|
||||
{"missing object", "", []uuid.UUID{missing}, apperr.CodeUnavailable},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
before := counts()
|
||||
_, err := svc.CreateMessage(ctx, dto.CreateMessage{ChatID: chat, SenderID: user, IdempotencyKey: uuid.NewString(), Text: tc.text, AttachmentIDs: tc.ids})
|
||||
if err == nil || apperr.CodeFromError(err) != tc.code {
|
||||
t.Fatalf("got %v, want code %v", err, tc.code)
|
||||
}
|
||||
if after := counts(); after != before {
|
||||
t.Fatalf("rejected message changed counts: %s -> %s", before, after)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, text := range []string{"", " \t\u2003"} {
|
||||
request := dto.CreateMessage{ChatID: chat, SenderID: user, IdempotencyKey: uuid.NewString(), Text: text, AttachmentIDs: []uuid.UUID{own}}
|
||||
first, err := svc.CreateMessage(ctx, request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
replay, err := svc.CreateMessage(ctx, request)
|
||||
if err != nil || replay.ID != first.ID || len(replay.AttachmentIDs) != 1 || replay.AttachmentIDs[0] != own {
|
||||
t.Fatalf("attachment replay failed: %v, %v", replay, err)
|
||||
}
|
||||
page, err := svc.ListForChat(ctx, dto.MessageFilters{UserID: user, ChatID: chat})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
found := false
|
||||
for _, msg := range page.Items {
|
||||
if msg.ID == first.ID {
|
||||
found = len(msg.AttachmentIDs) == 1 && msg.AttachmentIDs[0] == own
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("history lost attachment")
|
||||
}
|
||||
request.AttachmentIDs = []uuid.UUID{foreign}
|
||||
if _, err = svc.CreateMessage(ctx, request); apperr.CodeFromError(err) != apperr.CodeNotFound {
|
||||
t.Fatalf("foreign replay: %v", err)
|
||||
}
|
||||
}
|
||||
// Preserve legacy hashes written before the attachment migration.
|
||||
legacy := dto.CreateMessage{ChatID: chat, SenderID: user, IdempotencyKey: uuid.NewString(), Text: "legacy"}
|
||||
first, err := svc.CreateMessage(ctx, legacy)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
hash := sha256.Sum256([]byte(legacy.Text))
|
||||
if _, err = pool.Exec(ctx, `UPDATE message_idempotency SET request_hash=$1,hash_version=1 WHERE key=$2`, hash[:], legacy.IdempotencyKey); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
replay, err := svc.CreateMessage(ctx, legacy)
|
||||
if err != nil || replay.ID != first.ID {
|
||||
t.Fatalf("legacy replay failed: %v", err)
|
||||
}
|
||||
legacy.AttachmentIDs = []uuid.UUID{own}
|
||||
if _, err = svc.CreateMessage(ctx, legacy); apperr.CodeFromError(err) != apperr.CodeConflict {
|
||||
t.Fatalf("legacy hash ignored new attachment: %v", err)
|
||||
}
|
||||
textOnly := dto.CreateMessage{ChatID: chat, SenderID: user, IdempotencyKey: uuid.NewString(), Text: "text only"}
|
||||
first, err = svc.CreateMessage(ctx, textOnly)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
textOnly.AttachmentIDs = []uuid.UUID{}
|
||||
replay, err = svc.CreateMessage(ctx, textOnly)
|
||||
if err != nil || replay.ID != first.ID {
|
||||
t.Fatalf("nil and empty attachment lists differ: %v", err)
|
||||
}
|
||||
// Force link failure using the relational FK and prove all preceding writes roll back.
|
||||
before := counts()
|
||||
tx, err := pool.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = repo.CreateIdempotentTx(ctx, tx, dto.CreateMessage{ChatID: chat, SenderID: user, IdempotencyKey: uuid.NewString(), Text: "link rollback", AttachmentIDs: []uuid.UUID{own, uuid.New()}})
|
||||
if err == nil {
|
||||
t.Fatal("expected missing attachment FK failure")
|
||||
}
|
||||
_ = tx.Rollback(ctx)
|
||||
if after := counts(); after != before {
|
||||
t.Fatalf("failed link changed counts: %s -> %s", before, after)
|
||||
}
|
||||
var triggers int
|
||||
if err = pool.QueryRow(ctx, `SELECT count(*) FROM pg_trigger WHERE tgname='message_content'`).Scan(&triggers); err != nil || triggers != 0 {
|
||||
t.Fatalf("message trigger remains: %d, %v", triggers, err)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("object lock cancellation does not leak a pooled lock", func(t *testing.T) {
|
||||
attachments := &AttachmentsRepo{Pool: pool}
|
||||
key := "originals/" + uuid.NewString()
|
||||
_, unlock, err := attachments.Lock(ctx, key)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
waiting, cancel := context.WithTimeout(ctx, 50*time.Millisecond)
|
||||
_, _, err = attachments.Lock(waiting, key)
|
||||
cancel()
|
||||
unlock()
|
||||
if err == nil {
|
||||
t.Fatal("contending lock ignored deadline")
|
||||
}
|
||||
_, release, err := attachments.Lock(ctx, key)
|
||||
if err != nil {
|
||||
t.Fatalf("lock leaked after cancellation: %v", err)
|
||||
}
|
||||
release()
|
||||
})
|
||||
|
||||
t.Log("rollback removed both side effect and key; expired key created one new message")
|
||||
}
|
||||
|
||||
type uncachedChats struct{}
|
||||
|
||||
func (uncachedChats) Create(context.Context, dto.Chat) error { return nil }
|
||||
func (uncachedChats) Get(context.Context, uuid.UUID) (*dto.Chat, error) {
|
||||
return nil, errors.New("cache disabled for storage test")
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
)
|
||||
|
||||
type MessagesRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewMessagesRepo(pool *pgxpool.Pool) *MessagesRepo {
|
||||
return &MessagesRepo{pool: pool}
|
||||
}
|
||||
|
||||
// CreateIdempotentTx serializes only requests sharing an idempotency scope.
|
||||
// The transaction commits both the message and its replay record, or neither.
|
||||
func (r *MessagesRepo) CreateIdempotentTx(ctx context.Context, tx pgx.Tx, req dto.CreateMessage) (*dto.Message, error) {
|
||||
scope := req.SenderID.String() + ":POST:" + req.ChatID.String() + ":" + req.IdempotencyKey
|
||||
if _, err := tx.Exec(ctx, `SELECT pg_advisory_xact_lock(hashtextextended($1, 0))`, scope); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to lock message key")
|
||||
}
|
||||
hash := messageHash(req)
|
||||
var hashVersion int
|
||||
var storedHash []byte
|
||||
var messageID uuid.UUID
|
||||
err := tx.QueryRow(ctx, `SELECT request_hash, message_id, hash_version FROM message_idempotency
|
||||
WHERE user_id=$1 AND method='POST' AND chat_id=$2 AND key=$3 AND expires_at>clock_timestamp()`,
|
||||
req.SenderID, req.ChatID, req.IdempotencyKey).Scan(&storedHash, &messageID, &hashVersion)
|
||||
if err == nil {
|
||||
if hashVersion == 1 && len(req.AttachmentIDs) == 0 {
|
||||
hash = sha256.Sum256([]byte(req.Text))
|
||||
} else if hashVersion != 2 {
|
||||
return nil, apperr.New(apperr.CodeConflict, "idempotency key used with different content")
|
||||
}
|
||||
if !bytes.Equal(hash[:], storedHash) {
|
||||
return nil, apperr.New(apperr.CodeConflict, "idempotency key used with different content")
|
||||
}
|
||||
return scanMessage(tx.QueryRow(ctx, `SELECT `+messageColumns+` FROM messages WHERE id=$1`, messageID))
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return nil, apperr.WrapPostgres(err, "failed to read message key")
|
||||
}
|
||||
message := dto.Message{ID: uuid.New(), ChatID: req.ChatID, SenderID: req.SenderID, Text: req.Text, AttachmentIDs: append([]uuid.UUID{}, req.AttachmentIDs...), CreatedAt: time.Now().UTC().Truncate(time.Microsecond)}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)`,
|
||||
message.ID, message.ChatID, message.SenderID, message.Text, message.CreatedAt); err != nil {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok && pgErr.Code == pgerrcode.ForeignKeyViolation {
|
||||
return nil, apperr.Wrap(err, apperr.CodeNotFound, "chat not found")
|
||||
}
|
||||
return nil, apperr.WrapPostgres(err, "failed to create message")
|
||||
}
|
||||
for position, attachmentID := range req.AttachmentIDs {
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO message_attachments(message_id,chat_id,attachment_id,position) VALUES($1,$2,$3,$4)`, message.ID, message.ChatID, attachmentID, position); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to link attachment")
|
||||
}
|
||||
}
|
||||
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO message_idempotency(user_id,method,chat_id,key,request_hash,message_id,expires_at,hash_version)
|
||||
VALUES($1,'POST',$2,$3,$4,$5,clock_timestamp()+interval '24 hours',2)
|
||||
ON CONFLICT(user_id,method,chat_id,key) DO UPDATE SET request_hash=EXCLUDED.request_hash,
|
||||
message_id=EXCLUDED.message_id,expires_at=EXCLUDED.expires_at,hash_version=EXCLUDED.hash_version`, req.SenderID, req.ChatID, req.IdempotencyKey, hash[:], message.ID); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to save message key")
|
||||
}
|
||||
return &message, nil
|
||||
}
|
||||
|
||||
func (r *MessagesRepo) GetByIDForChat(ctx context.Context, chatID, messageID uuid.UUID) (*dto.Message, error) {
|
||||
const query = `SELECT ` + messageColumns + ` FROM messages WHERE chat_id = $1 AND id = $2`
|
||||
return scanMessage(r.pool.QueryRow(ctx, query, chatID, messageID))
|
||||
}
|
||||
|
||||
func (r *MessagesRepo) ListForChat(ctx context.Context, chatID uuid.UUID, page dto.PageQuery) ([]dto.Message, error) {
|
||||
query := `SELECT ` + messageColumns + ` FROM messages WHERE chat_id = $1`
|
||||
args := []any{chatID, page.Limit}
|
||||
if page.After != nil {
|
||||
query += ` AND (created_at, id) > ($3, $4)`
|
||||
args = append(args, page.After.CreatedAt, page.After.ID)
|
||||
}
|
||||
query += ` ORDER BY created_at, id LIMIT $2`
|
||||
rows, err := r.pool.Query(ctx, query, args...)
|
||||
if err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to list messages")
|
||||
}
|
||||
defer rows.Close()
|
||||
messages := make([]dto.Message, 0)
|
||||
for rows.Next() {
|
||||
message, err := scanMessage(rows)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
messages = append(messages, *message)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to list messages")
|
||||
}
|
||||
return messages, nil
|
||||
}
|
||||
|
||||
func scanMessage(row pgx.Row) (*dto.Message, error) {
|
||||
var message dto.Message
|
||||
if err := row.Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt, &message.AttachmentIDs); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to get message")
|
||||
}
|
||||
return &message, nil
|
||||
}
|
||||
|
||||
const messageColumns = `id, chat_id, sender_id, text, created_at,
|
||||
ARRAY(SELECT attachment_id FROM message_attachments WHERE message_id=messages.id ORDER BY position)`
|
||||
|
||||
func messageHash(req dto.CreateMessage) [32]byte {
|
||||
// Field order and a non-nil list define the canonical v2 representation.
|
||||
canonical := struct {
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []uuid.UUID `json:"attachment_ids"`
|
||||
}{req.Text, append([]uuid.UUID{}, req.AttachmentIDs...)}
|
||||
encoded, _ := json.Marshal(canonical) // strings and UUIDs have no encoding failures.
|
||||
return sha256.Sum256(encoded)
|
||||
}
|
||||
|
||||
func (r *MessagesRepo) AttachmentsForMessageTx(ctx context.Context, tx pgx.Tx, ids []uuid.UUID) ([]dto.MessageAttachment, error) {
|
||||
// Prevent a concurrent integrity update from invalidating the checked rows
|
||||
// until the message and its links have committed together.
|
||||
rows, err := tx.Query(ctx, `SELECT id, chat_id, integrity_missing FROM attachments WHERE id=ANY($1) ORDER BY id FOR SHARE`, ids)
|
||||
if err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to read attachments")
|
||||
}
|
||||
defer rows.Close()
|
||||
result := make([]dto.MessageAttachment, 0, len(ids))
|
||||
for rows.Next() {
|
||||
var a dto.MessageAttachment
|
||||
if err := rows.Scan(&a.ID, &a.ChatID, &a.Missing); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to read attachment")
|
||||
}
|
||||
result = append(result, a)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to read attachments")
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres"
|
||||
)
|
||||
|
||||
type RefreshTokensRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewRefreshTokensRepo(pool *pgxpool.Pool) *RefreshTokensRepo {
|
||||
return &RefreshTokensRepo{
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) Create(ctx context.Context, req dto.StoredRefreshToken) error {
|
||||
return r.create(ctx, r.pool, req)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) CreateTx(ctx context.Context, tx pgx.Tx, req dto.StoredRefreshToken) error {
|
||||
return r.create(ctx, tx, req)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) GetByID(ctx context.Context, id uuid.UUID) (*dto.StoredRefreshToken, error) {
|
||||
return r.getByID(ctx, r.pool, id, false)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) GetByIDTx(ctx context.Context, tx pgx.Tx, id uuid.UUID) (*dto.StoredRefreshToken, error) {
|
||||
return r.getByID(ctx, tx, id, false)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) GetByIDForUpdate(ctx context.Context, id uuid.UUID) (*dto.StoredRefreshToken, error) {
|
||||
return r.getByID(ctx, r.pool, id, true)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) GetByIDForUpdateTx(ctx context.Context, tx pgx.Tx, id uuid.UUID) (*dto.StoredRefreshToken, error) {
|
||||
return r.getByID(ctx, tx, id, true)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) Revoke(ctx context.Context, req dto.RevokeRefreshToken) error {
|
||||
return r.revoke(ctx, r.pool, req)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) RevokeTx(ctx context.Context, tx pgx.Tx, req dto.RevokeRefreshToken) error {
|
||||
return r.revoke(ctx, tx, req)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) RevokeAllByUserID(ctx context.Context, req dto.RevokeRefreshTokensByUserID) error {
|
||||
return r.revokeAllByUserID(ctx, r.pool, req)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) RevokeAllByUserIDTx(ctx context.Context, tx pgx.Tx, req dto.RevokeRefreshTokensByUserID) error {
|
||||
return r.revokeAllByUserID(ctx, tx, req)
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) DeleteExpired(ctx context.Context) error {
|
||||
const query = `
|
||||
DELETE FROM refresh_tokens
|
||||
WHERE expires_at < $1`
|
||||
|
||||
_, err := r.pool.Exec(ctx, query, time.Now())
|
||||
if err != nil {
|
||||
return apperr.WrapPostgres(err, "failed to delete expired refresh tokens")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) create(ctx context.Context, conn postgres.Conn, req dto.StoredRefreshToken) error {
|
||||
const query = `INSERT INTO refresh_tokens (id,
|
||||
user_id,
|
||||
token_hash,
|
||||
user_agent,
|
||||
ip_address,
|
||||
role,
|
||||
expires_at,
|
||||
created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`
|
||||
|
||||
ct, err := conn.Exec(
|
||||
ctx,
|
||||
query,
|
||||
req.ID,
|
||||
req.UserID,
|
||||
req.TokenHash,
|
||||
req.UserAgent,
|
||||
req.IPAddress,
|
||||
req.Role,
|
||||
req.ExpiresAt,
|
||||
req.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return apperr.WrapPostgres(err, "failed to create refresh token")
|
||||
}
|
||||
|
||||
if ct.RowsAffected() == 0 {
|
||||
return apperr.New(apperr.CodeAlreadyExists, "refresh token already exists")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) getByID(ctx context.Context, conn postgres.Conn, id uuid.UUID, lock bool) (*dto.StoredRefreshToken, error) {
|
||||
const query = `SELECT id,
|
||||
user_id,
|
||||
token_hash,
|
||||
user_agent,
|
||||
ip_address,
|
||||
role,
|
||||
expires_at,
|
||||
revoked_at,
|
||||
created_at
|
||||
FROM refresh_tokens
|
||||
WHERE id = $1`
|
||||
const queryLock = `SELECT id,
|
||||
user_id,
|
||||
token_hash,
|
||||
user_agent,
|
||||
ip_address,
|
||||
role,
|
||||
expires_at,
|
||||
revoked_at,
|
||||
created_at
|
||||
FROM refresh_tokens
|
||||
WHERE id = $1 FOR UPDATE`
|
||||
|
||||
use := query
|
||||
if lock {
|
||||
use = queryLock
|
||||
}
|
||||
|
||||
var token dto.StoredRefreshToken
|
||||
err := conn.QueryRow(ctx, use, id).Scan(
|
||||
&token.ID,
|
||||
&token.UserID,
|
||||
&token.TokenHash,
|
||||
&token.UserAgent,
|
||||
&token.IPAddress,
|
||||
&token.Role,
|
||||
&token.ExpiresAt,
|
||||
&token.RevokedAt,
|
||||
&token.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to get refresh by id")
|
||||
}
|
||||
|
||||
return &token, nil
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) revoke(ctx context.Context, conn postgres.Conn, req dto.RevokeRefreshToken) error {
|
||||
const query = `
|
||||
UPDATE refresh_tokens
|
||||
SET revoked_at = $2
|
||||
WHERE id = $1
|
||||
AND revoked_at IS NULL`
|
||||
|
||||
ct, err := conn.Exec(ctx, query, req.ID, req.RevokedAt)
|
||||
if err != nil {
|
||||
return apperr.WrapPostgres(err, "failed to revoke refresh token")
|
||||
}
|
||||
|
||||
if ct.RowsAffected() == 0 {
|
||||
return apperr.New(apperr.CodeNotFound, "refresh token not found")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *RefreshTokensRepo) revokeAllByUserID(ctx context.Context, conn postgres.Conn, req dto.RevokeRefreshTokensByUserID) error {
|
||||
const query = `
|
||||
UPDATE refresh_tokens
|
||||
SET revoked_at = $2
|
||||
WHERE user_id = $1
|
||||
AND revoked_at IS NULL`
|
||||
|
||||
_, err := conn.Exec(ctx, query, req.UserID, req.RevokedAt)
|
||||
if err != nil {
|
||||
return apperr.WrapPostgres(err, "failed to revoke refresh tokens")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/apperr"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/postgres"
|
||||
)
|
||||
|
||||
type UsersRepo struct {
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewUsersRepo(pool *pgxpool.Pool) *UsersRepo {
|
||||
return &UsersRepo{pool: pool}
|
||||
}
|
||||
|
||||
func (r *UsersRepo) Create(ctx context.Context, req dto.CreateStoredUser) error {
|
||||
return r.create(ctx, r.pool, req)
|
||||
}
|
||||
|
||||
func (r *UsersRepo) CreateTx(ctx context.Context, tx pgx.Tx, req dto.CreateStoredUser) error {
|
||||
return r.create(ctx, tx, req)
|
||||
}
|
||||
|
||||
func (r *UsersRepo) GetByID(ctx context.Context, id uuid.UUID) (*dto.User, error) {
|
||||
return r.getByID(ctx, r.pool, id)
|
||||
}
|
||||
|
||||
func (r *UsersRepo) GetByIDTx(ctx context.Context, tx pgx.Tx, id uuid.UUID) (*dto.User, error) {
|
||||
return r.getByID(ctx, tx, id)
|
||||
}
|
||||
|
||||
func (r *UsersRepo) GetByUsername(ctx context.Context, username string) (*dto.User, error) {
|
||||
const query = `SELECT id, username, name, created_at FROM users WHERE username = $1`
|
||||
return scanUser(r.pool.QueryRow(ctx, query, username))
|
||||
}
|
||||
|
||||
func (r *UsersRepo) GetCredentialsByUsername(ctx context.Context, username string) (*dto.UserCredentials, error) {
|
||||
const query = `SELECT id, username, name, created_at, password_hash FROM users WHERE username = $1`
|
||||
|
||||
var credentials dto.UserCredentials
|
||||
err := r.pool.QueryRow(ctx, query, username).Scan(
|
||||
&credentials.User.ID,
|
||||
&credentials.User.Username,
|
||||
&credentials.User.DisplayName,
|
||||
&credentials.User.CreatedAt,
|
||||
&credentials.PasswordHash,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to get user credentials")
|
||||
}
|
||||
return &credentials, nil
|
||||
}
|
||||
|
||||
func (r *UsersRepo) Delete(ctx context.Context, id uuid.UUID) error {
|
||||
const query = `DELETE FROM users WHERE id = $1`
|
||||
|
||||
ct, err := r.pool.Exec(ctx, query, id)
|
||||
if err != nil {
|
||||
return apperr.WrapPostgres(err, "failed to delete user")
|
||||
}
|
||||
if ct.RowsAffected() == 0 {
|
||||
return apperr.New(apperr.CodeNotFound, "user not found")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *UsersRepo) create(ctx context.Context, conn postgres.Conn, req dto.CreateStoredUser) error {
|
||||
const query = `INSERT INTO users (id, username, name, password_hash, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6)`
|
||||
|
||||
_, err := conn.Exec(ctx, query, req.ID, req.Username, req.DisplayName, req.PasswordHash, req.CreatedAt, req.UpdatedAt)
|
||||
if err != nil {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok &&
|
||||
pgErr.Code == pgerrcode.UniqueViolation && pgErr.ConstraintName == "users_username_unique" {
|
||||
return apperr.Wrap(err, apperr.CodeConflict, "username already exists")
|
||||
}
|
||||
return apperr.WrapPostgres(err, "failed to create user")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r *UsersRepo) getByID(ctx context.Context, conn postgres.Conn, id uuid.UUID) (*dto.User, error) {
|
||||
const query = `SELECT id, username, name, created_at FROM users WHERE id = $1`
|
||||
return scanUser(conn.QueryRow(ctx, query, id))
|
||||
}
|
||||
|
||||
func scanUser(row pgx.Row) (*dto.User, error) {
|
||||
var user dto.User
|
||||
if err := row.Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt); err != nil {
|
||||
return nil, apperr.WrapPostgres(err, "failed to get user")
|
||||
}
|
||||
return &user, nil
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package config
|
||||
|
||||
import "github.com/spf13/viper"
|
||||
|
||||
type Loader interface {
|
||||
loadConfig(v *viper.Viper) error
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/environment"
|
||||
)
|
||||
|
||||
type Application struct {
|
||||
LogLevel string
|
||||
ShutdownTimeout time.Duration
|
||||
Environment environment.ApplicationEnvironment
|
||||
RequestRateLimit float64
|
||||
BurstRateLimit int
|
||||
RateLimitTTL time.Duration
|
||||
}
|
||||
|
||||
func (c *Application) Validate() error {
|
||||
environmentErr := c.Environment.Validate()
|
||||
|
||||
switch {
|
||||
case c.LogLevel == "":
|
||||
return wrapValidationError("application", errors.New("log_level cannot be empty in application config"))
|
||||
case c.ShutdownTimeout == 0:
|
||||
return wrapValidationError("application", errors.New("shutdown_timeout cannot be zero in application config"))
|
||||
case environmentErr != nil:
|
||||
return wrapValidationError("application", environmentErr)
|
||||
//case c.RequestRateLimit == 0:
|
||||
// return wrapValidationError("application", errors.New("request_rate_limit cannot be zero in application config"))
|
||||
//case c.BurstRateLimit == 0:
|
||||
// return wrapValidationError("application", errors.New("burst_rate_limit cannot be zero in application config"))
|
||||
//case c.RateLimitTTL == 0:
|
||||
// return wrapValidationError("application", errors.New("rate_limit_ttl cannot be zero in application config"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Application) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("APPLICATION")
|
||||
c.LogLevel = v.GetString("LOG_LEVEL")
|
||||
c.ShutdownTimeout = v.GetDuration("SHUTDOWN_TIMEOUT")
|
||||
c.Environment = environment.ParseEnvironment(v.GetString("ENVIRONMENT"))
|
||||
// todo add cool value
|
||||
c.RequestRateLimit = v.GetFloat64("REQUEST_RATE_LIMIT")
|
||||
c.BurstRateLimit = v.GetInt("BURST_RATE_LIMIT")
|
||||
c.RateLimitTTL = v.GetDuration("RATE_LIMIT_TTL")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Application) copy() *Application {
|
||||
return &Application{
|
||||
LogLevel: c.LogLevel,
|
||||
ShutdownTimeout: c.ShutdownTimeout,
|
||||
Environment: c.Environment,
|
||||
RequestRateLimit: c.RequestRateLimit,
|
||||
BurstRateLimit: c.BurstRateLimit,
|
||||
RateLimitTTL: c.RateLimitTTL,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Attachment struct {
|
||||
Endpoint, Region, Bucket, AccessKey, SecretKey string
|
||||
MaxBytes, MaxPixels int64
|
||||
Timeout, Grace time.Duration
|
||||
Uploads, Decoders int
|
||||
}
|
||||
|
||||
func LoadAttachment() (*Attachment, error) {
|
||||
c := &Attachment{Endpoint: env("S3_ENDPOINT", "http://s3:9000"), Region: env("S3_REGION", "us-east-1"), Bucket: env("S3_BUCKET", "messenger-attachments"), MaxBytes: 10485760, MaxPixels: 40000000, Timeout: 120 * time.Second, Grace: time.Hour, Uploads: 4, Decoders: 1}
|
||||
var err error
|
||||
for key, dst := range map[string]*string{"S3_ACCESS_KEY_ID_FILE": &c.AccessKey, "S3_SECRET_ACCESS_KEY_FILE": &c.SecretKey} {
|
||||
b, e := os.ReadFile(os.Getenv(key))
|
||||
if e != nil {
|
||||
return nil, fmt.Errorf("read %s: %w", key, e)
|
||||
}
|
||||
*dst = strings.TrimSpace(string(b))
|
||||
if *dst == "" {
|
||||
return nil, fmt.Errorf("empty %s", key)
|
||||
}
|
||||
}
|
||||
for key, dst := range map[string]*int64{"MAX_UPLOAD_BYTES": &c.MaxBytes, "MAX_IMAGE_PIXELS": &c.MaxPixels} {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
*dst, err = strconv.ParseInt(v, 10, 64)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid %s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
for key, dst := range map[string]*time.Duration{"UPLOAD_TIMEOUT": &c.Timeout, "RECONCILE_GRACE_PERIOD": &c.Grace} {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
*dst, err = time.ParseDuration(v)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid %s", key)
|
||||
}
|
||||
}
|
||||
}
|
||||
if c.MaxBytes < 1 || c.MaxBytes > 10485760 || c.MaxPixels < 1 || c.MaxPixels > 40000000 || c.Timeout < time.Second || c.Timeout > 120*time.Second || c.Grace <= 2*c.Timeout {
|
||||
return nil, fmt.Errorf("invalid attachment limits or grace period")
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func env(k, d string) string {
|
||||
if v := os.Getenv(k); v != "" {
|
||||
return v
|
||||
}
|
||||
return d
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
"go.uber.org/multierr"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/environment"
|
||||
)
|
||||
|
||||
var defaultConfig = &Config{
|
||||
Server: &Server{
|
||||
Host: "localhost",
|
||||
Port: 8001,
|
||||
ReadTimeout: 10 * time.Second,
|
||||
WriteTimeout: 10 * time.Second,
|
||||
IdleTimeout: 10 * time.Second,
|
||||
ShutdownTimeout: 10 * time.Second,
|
||||
Enabled: false,
|
||||
},
|
||||
Postgres: &Postgres{
|
||||
Host: "localhost",
|
||||
Port: 5432,
|
||||
User: "postgres",
|
||||
Password: "postgres",
|
||||
Database: "postgres",
|
||||
VersionTable: "versions",
|
||||
RunMigrations: false,
|
||||
MaxConnections: 10,
|
||||
ConnectTimeout: 500 * time.Millisecond,
|
||||
QueryTimeout: 2 * time.Second,
|
||||
},
|
||||
Redis: &Redis{
|
||||
Host: "localhost",
|
||||
Port: 6379,
|
||||
Username: "",
|
||||
Password: "",
|
||||
DB: 0,
|
||||
},
|
||||
JWT: &JWT{
|
||||
AccessTokenLifetime: 4 * time.Hour,
|
||||
RefreshTokenLifetime: time.Hour,
|
||||
PublicKeyPath: "certs/private.pem",
|
||||
PrivateKeyPath: "certs/public.pem",
|
||||
},
|
||||
Application: &Application{
|
||||
LogLevel: "info",
|
||||
ShutdownTimeout: 10 * time.Second,
|
||||
Environment: environment.ApplicationEnvironmentDev,
|
||||
RequestRateLimit: 100,
|
||||
},
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Server *Server
|
||||
Postgres *Postgres
|
||||
Redis *Redis
|
||||
JWT *JWT
|
||||
Application *Application
|
||||
}
|
||||
|
||||
func New() (*Config, error) {
|
||||
v := viper.New()
|
||||
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
||||
v.AutomaticEnv()
|
||||
|
||||
cfg := defaultConfig.copy()
|
||||
|
||||
if err := cfg.loadConfig(v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := cfg.Validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return cfg, nil
|
||||
}
|
||||
|
||||
func (c *Config) Validate() error {
|
||||
return multierr.Combine(
|
||||
c.Server.Validate(),
|
||||
c.Postgres.Validate(),
|
||||
c.JWT.Validate(),
|
||||
c.Redis.Validate(),
|
||||
c.Application.Validate(),
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Config) loadConfig(v *viper.Viper) error {
|
||||
return multierr.Combine(
|
||||
c.Postgres.loadConfig(v),
|
||||
c.Server.loadConfig(v),
|
||||
c.JWT.loadConfig(v),
|
||||
c.Redis.loadConfig(v),
|
||||
c.Application.loadConfig(v),
|
||||
)
|
||||
}
|
||||
|
||||
func (c *Config) copy() *Config {
|
||||
return &Config{
|
||||
Server: c.Server.copy(),
|
||||
Postgres: c.Postgres.copy(),
|
||||
Redis: c.Redis.copy(),
|
||||
JWT: c.JWT.copy(),
|
||||
Application: c.Application.copy(),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Cookies struct {
|
||||
Enabled bool
|
||||
Domain string
|
||||
Path string
|
||||
Secure bool
|
||||
SameSite http.SameSite
|
||||
AllowedOrigins []string
|
||||
}
|
||||
|
||||
func (c *Cookies) Validate() error {
|
||||
switch {
|
||||
case c.Domain == "":
|
||||
return wrapValidationError("cookies", errors.New("domain cannot be empty"))
|
||||
case c.Path == "":
|
||||
return wrapValidationError("cookies", errors.New("path cannot be empty"))
|
||||
case c.SameSite == http.SameSiteDefaultMode:
|
||||
return wrapValidationError("cookies", errors.New("same_site_set_to_none_mode cannot be empty"))
|
||||
//case c.Enabled && !c.Secure:
|
||||
// return wrapValidationError("cookies", errors.New("if cookies_enabled equals true then cookies_secure must be true"))
|
||||
case c.AllowedOrigins == nil || len(c.AllowedOrigins) == 0:
|
||||
return wrapValidationError("cookies", errors.New("allowed_origins cannot be empty"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Cookies) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("COOKIES")
|
||||
c.Enabled = v.GetBool("ENABLED")
|
||||
c.Domain = v.GetString("DOMAIN")
|
||||
c.Path = v.GetString("PATH")
|
||||
c.Secure = v.GetBool("SECURE")
|
||||
c.AllowedOrigins = v.GetStringSlice("ALLOWED_ORIGINS")
|
||||
sameSite := v.GetString("SAME_SITE")
|
||||
switch strings.ToLower(strings.TrimSpace(sameSite)) {
|
||||
case "strict":
|
||||
c.SameSite = http.SameSiteStrictMode
|
||||
case "none":
|
||||
c.SameSite = http.SameSiteNoneMode
|
||||
default:
|
||||
c.SameSite = http.SameSiteLaxMode
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Cookies) copy() *Cookies {
|
||||
return &Cookies{
|
||||
Enabled: c.Enabled,
|
||||
Domain: c.Domain,
|
||||
Path: c.Path,
|
||||
Secure: c.Secure,
|
||||
SameSite: c.SameSite,
|
||||
AllowedOrigins: c.AllowedOrigins,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type JWT struct {
|
||||
AccessTokenLifetime time.Duration
|
||||
RefreshTokenLifetime time.Duration
|
||||
PublicKeyPath string
|
||||
PrivateKeyPath string
|
||||
}
|
||||
|
||||
func (c *JWT) Validate() error {
|
||||
switch {
|
||||
case c.AccessTokenLifetime <= 0:
|
||||
return wrapValidationError("jwt", errors.New("access_token_lifetime cannot be empty"))
|
||||
case c.RefreshTokenLifetime <= 0:
|
||||
return wrapValidationError("jwt", errors.New("refresh_token_lifetime cannot be empty"))
|
||||
case c.PrivateKeyPath == "":
|
||||
return wrapValidationError("jwt", errors.New("private_key_path cannot be empty"))
|
||||
case c.PublicKeyPath == "":
|
||||
return wrapValidationError("jwt", errors.New("public_key_path cannot be empty"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *JWT) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("JWT")
|
||||
c.AccessTokenLifetime = v.GetDuration("ACCESS_TOKEN_LIFETIME")
|
||||
c.RefreshTokenLifetime = time.Hour
|
||||
if value := os.Getenv("SESSION_TTL_SECONDS"); value != "" {
|
||||
seconds, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || seconds <= 0 || seconds > int64((1<<63-1)/time.Second) {
|
||||
return wrapValidationError("session", errors.New("SESSION_TTL_SECONDS must be a positive integer duration in seconds"))
|
||||
}
|
||||
c.RefreshTokenLifetime = time.Duration(seconds) * time.Second
|
||||
} else if legacy := v.GetString("REFRESH_TOKEN_LIFETIME"); legacy != "" {
|
||||
duration, err := time.ParseDuration(legacy)
|
||||
if err != nil {
|
||||
return wrapValidationError("jwt", errors.New("invalid refresh_token_lifetime"))
|
||||
}
|
||||
c.RefreshTokenLifetime = duration
|
||||
}
|
||||
c.PublicKeyPath = v.GetString("PUBLIC_KEY_PATH")
|
||||
c.PrivateKeyPath = v.GetString("PRIVATE_KEY_PATH")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *JWT) copy() *JWT {
|
||||
return &JWT{
|
||||
AccessTokenLifetime: c.AccessTokenLifetime,
|
||||
RefreshTokenLifetime: c.RefreshTokenLifetime,
|
||||
PublicKeyPath: c.PublicKeyPath,
|
||||
PrivateKeyPath: c.PrivateKeyPath,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
uri = "postgres://%s:%s@%s:%d/%s?sslmode=disable"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
VersionTable string
|
||||
RunMigrations bool
|
||||
MaxConnections int
|
||||
ConnectTimeout time.Duration
|
||||
QueryTimeout time.Duration
|
||||
}
|
||||
|
||||
func (c *Postgres) Validate() error {
|
||||
switch {
|
||||
case c.Host == "":
|
||||
return wrapValidationError("postgres", errors.New("host cannot be empty"))
|
||||
case c.Port == 0:
|
||||
return wrapValidationError("postgres", errors.New("port cannot be empty"))
|
||||
case c.User == "":
|
||||
return wrapValidationError("postgres", errors.New("user cannot be empty"))
|
||||
case c.Password == "":
|
||||
return wrapValidationError("postgres", errors.New("password cannot be empty"))
|
||||
case c.Database == "":
|
||||
return wrapValidationError("postgres", errors.New("database cannot be empty"))
|
||||
case c.VersionTable == "":
|
||||
return wrapValidationError("postgres", errors.New("version_table name cannot be empty"))
|
||||
case c.MaxConnections <= 0 || int64(c.MaxConnections) > math.MaxInt32:
|
||||
return wrapValidationError("postgres", errors.New("max_connections must be between 1 and 2147483647"))
|
||||
case c.ConnectTimeout <= 0:
|
||||
return wrapValidationError("postgres", errors.New("connect_timeout must be positive"))
|
||||
case c.QueryTimeout < time.Millisecond:
|
||||
return wrapValidationError("postgres", errors.New("query_timeout must be at least 1ms"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Postgres) GetMaxConnections() int32 { return int32(c.MaxConnections) }
|
||||
func (c *Postgres) GetConnectTimeout() time.Duration { return c.ConnectTimeout }
|
||||
func (c *Postgres) GetQueryTimeout() time.Duration { return c.QueryTimeout }
|
||||
|
||||
func (c *Postgres) GetVersionTableName() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return c.VersionTable
|
||||
}
|
||||
|
||||
func (c *Postgres) GetURI() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(uri, c.User, c.Password, c.Host, c.Port, c.Database)
|
||||
}
|
||||
|
||||
func (c *Postgres) GetRunMigrations() bool {
|
||||
if c == nil {
|
||||
return false
|
||||
}
|
||||
return c.RunMigrations
|
||||
}
|
||||
|
||||
func (c *Postgres) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("POSTGRES")
|
||||
c.Host = v.GetString("HOST")
|
||||
c.Port = v.GetInt("PORT")
|
||||
c.User = v.GetString("USER")
|
||||
c.Password = v.GetString("PASSWORD")
|
||||
c.Database = v.GetString("DATABASE")
|
||||
c.VersionTable = v.GetString("VERSION_TABLE")
|
||||
c.RunMigrations = v.GetBool("RUN_MIGRATIONS")
|
||||
c.MaxConnections = v.GetInt("MAX_CONNECTIONS")
|
||||
c.ConnectTimeout = v.GetDuration("CONNECT_TIMEOUT")
|
||||
c.QueryTimeout = v.GetDuration("QUERY_TIMEOUT")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Postgres) copy() *Postgres {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return &Postgres{
|
||||
Host: c.Host,
|
||||
Port: c.Port,
|
||||
User: c.User,
|
||||
Password: c.Password,
|
||||
Database: c.Database,
|
||||
MaxConnections: c.MaxConnections,
|
||||
ConnectTimeout: c.ConnectTimeout,
|
||||
QueryTimeout: c.QueryTimeout,
|
||||
RunMigrations: c.RunMigrations,
|
||||
VersionTable: c.VersionTable,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Redis struct {
|
||||
Host string
|
||||
Port int
|
||||
Username string
|
||||
Password string
|
||||
DB int
|
||||
}
|
||||
|
||||
func (c *Redis) Validate() error {
|
||||
switch {
|
||||
case c.Host == "":
|
||||
return wrapValidationError("redis", errors.New("host cannot be empty"))
|
||||
case c.Port == 0:
|
||||
return wrapValidationError("redis", errors.New("port cannot be empty"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Redis) BuildRedisOptions() *redis.Options {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return &redis.Options{
|
||||
Addr: net.JoinHostPort(c.Host, fmt.Sprintf("%d", c.Port)),
|
||||
Username: c.Username,
|
||||
Password: c.Password,
|
||||
DB: c.DB,
|
||||
DialTimeout: 200 * time.Millisecond,
|
||||
ReadTimeout: 200 * time.Millisecond,
|
||||
WriteTimeout: 200 * time.Millisecond,
|
||||
PoolTimeout: 200 * time.Millisecond,
|
||||
MaxRetries: -1,
|
||||
DialerRetries: 1,
|
||||
ContextTimeoutEnabled: true,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Redis) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("REDIS")
|
||||
c.Host = v.GetString("HOST")
|
||||
c.Port = v.GetInt("PORT")
|
||||
c.DB = v.GetInt("DB")
|
||||
c.Password = v.GetString("PASSWORD")
|
||||
c.Username = v.GetString("USERNAME")
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Redis) copy() *Redis {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return &Redis{
|
||||
Host: c.Host,
|
||||
Port: c.Port,
|
||||
Username: c.Username,
|
||||
Password: c.Password,
|
||||
DB: c.DB,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
Host string
|
||||
Port int
|
||||
ReadTimeout time.Duration
|
||||
WriteTimeout time.Duration
|
||||
IdleTimeout time.Duration
|
||||
ShutdownTimeout time.Duration
|
||||
Enabled bool
|
||||
}
|
||||
|
||||
func (c *Server) Validate() error {
|
||||
if !c.Enabled {
|
||||
return nil
|
||||
}
|
||||
switch {
|
||||
case c.Host == "":
|
||||
return wrapValidationError("server", errors.New("host cannot be empty"))
|
||||
case c.Port == 0:
|
||||
return wrapValidationError("server", errors.New("port cannot be empty"))
|
||||
case c.ReadTimeout == 0:
|
||||
return wrapValidationError("server", errors.New("read_timeout cannot be empty"))
|
||||
case c.WriteTimeout == 0:
|
||||
return wrapValidationError("server", errors.New("write_timeout cannot be empty"))
|
||||
case c.IdleTimeout == 0:
|
||||
return wrapValidationError("server", errors.New("idle_timeout cannot be empty"))
|
||||
case c.ShutdownTimeout == 0:
|
||||
return wrapValidationError("server", errors.New("shutdown_timeout name cannot be empty"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Server) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("APP")
|
||||
c.Host = v.GetString("HOST")
|
||||
c.Port = v.GetInt("PORT")
|
||||
c.ReadTimeout = v.GetDuration("READ_TIMEOUT")
|
||||
c.WriteTimeout = v.GetDuration("WRITE_TIMEOUT")
|
||||
c.IdleTimeout = v.GetDuration("IDLE_TIMEOUT")
|
||||
c.ShutdownTimeout = v.GetDuration("SHUTDOWN_TIMEOUT")
|
||||
c.Enabled = v.GetBool("ENABLED")
|
||||
return c.Validate()
|
||||
}
|
||||
|
||||
func (c *Server) BindAddress() string {
|
||||
return fmt.Sprintf("%s:%d", c.Host, c.Port)
|
||||
}
|
||||
|
||||
func (c *Server) copy() *Server {
|
||||
return &Server{
|
||||
Host: c.Host,
|
||||
Port: c.Port,
|
||||
ReadTimeout: c.ReadTimeout,
|
||||
WriteTimeout: c.WriteTimeout,
|
||||
IdleTimeout: c.IdleTimeout,
|
||||
ShutdownTimeout: c.ShutdownTimeout,
|
||||
Enabled: c.Enabled,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package config
|
||||
|
||||
import "fmt"
|
||||
|
||||
func wrapValidationError(name string, err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("%s validation error: %w", name, err)
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
package dto
|
||||
@@ -0,0 +1,126 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/netip"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/role"
|
||||
)
|
||||
|
||||
// ---- Tokens ----
|
||||
|
||||
type UserDataInToken struct {
|
||||
TokenID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
Role role.Role
|
||||
IsAccess bool
|
||||
|
||||
PhoneNumber *string
|
||||
Email *string
|
||||
|
||||
ExpiresAt time.Time
|
||||
IssuedAt time.Time
|
||||
}
|
||||
|
||||
func (u UserDataInToken) Validate() error {
|
||||
if u.TokenID == uuid.Nil {
|
||||
return errors.New("invalid token id")
|
||||
}
|
||||
if u.UserID == uuid.Nil {
|
||||
return errors.New("invalid user id")
|
||||
}
|
||||
if !u.Role.IsValid() {
|
||||
return errors.New("invalid role")
|
||||
}
|
||||
if u.ExpiresAt.IsZero() {
|
||||
return errors.New("invalid expiry")
|
||||
}
|
||||
if u.IssuedAt.IsZero() {
|
||||
return errors.New("invalid issued")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Token struct {
|
||||
Payload string
|
||||
}
|
||||
|
||||
type (
|
||||
AccessToken Token
|
||||
RefreshToken Token
|
||||
)
|
||||
|
||||
type Tokens struct {
|
||||
AccessToken AccessToken
|
||||
RefreshToken RefreshToken
|
||||
}
|
||||
|
||||
func (t AccessToken) Token() *Token {
|
||||
return new(Token(t))
|
||||
}
|
||||
|
||||
func (t RefreshToken) Token() *Token {
|
||||
return new(Token(t))
|
||||
}
|
||||
|
||||
type StoredRefreshToken struct {
|
||||
ID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
TokenHash string
|
||||
UserAgent string
|
||||
IPAddress netip.Addr
|
||||
Role role.Role
|
||||
ExpiresAt time.Time
|
||||
RevokedAt *time.Time
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type RevokeRefreshToken struct {
|
||||
ID uuid.UUID
|
||||
RevokedAt time.Time
|
||||
}
|
||||
|
||||
type RevokeRefreshTokensByUserID struct {
|
||||
UserID uuid.UUID
|
||||
RevokedAt time.Time
|
||||
}
|
||||
|
||||
// ---- Auth ----
|
||||
|
||||
type Session struct {
|
||||
ExpiresAt time.Time
|
||||
Token string
|
||||
TokenType string
|
||||
User User
|
||||
}
|
||||
|
||||
type Login struct {
|
||||
Username string
|
||||
Password string
|
||||
}
|
||||
|
||||
type LoginResult struct {
|
||||
ExpiresAt time.Time
|
||||
TokenType string
|
||||
User User
|
||||
Tokens Tokens
|
||||
}
|
||||
|
||||
// UserCredentials is internal authentication data, never a public user response.
|
||||
type UserCredentials struct {
|
||||
User User
|
||||
PasswordHash string `json:"-"`
|
||||
}
|
||||
|
||||
type Logout struct {
|
||||
Token string
|
||||
}
|
||||
|
||||
type Register struct {
|
||||
Email string
|
||||
Password string
|
||||
RepeatPassword string
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
MemberIDs []uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type CreateChat struct {
|
||||
CreatorID uuid.UUID
|
||||
Title string
|
||||
MemberIDs []uuid.UUID
|
||||
}
|
||||
|
||||
type ChatMember struct {
|
||||
UserID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
}
|
||||
|
||||
type ChatFilters struct {
|
||||
UserID uuid.UUID
|
||||
Limit *int
|
||||
AfterID *uuid.UUID
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
AttachmentIDs []uuid.UUID
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type CreateMessage struct {
|
||||
AttachmentIDs []uuid.UUID
|
||||
IdempotencyKey string
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
}
|
||||
|
||||
type MessageFilters struct {
|
||||
ChatID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
Limit *int
|
||||
AfterID *uuid.UUID
|
||||
}
|
||||
|
||||
// MessageAttachment contains only the data needed to authorize linking an attachment.
|
||||
type MessageAttachment struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
Missing bool
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Page is a page of records with the ID to use for the next page.
|
||||
type Page[T any] struct {
|
||||
Items []T
|
||||
NextCursorID *uuid.UUID
|
||||
}
|
||||
|
||||
// PagePosition is a resolved database cursor, ordered by creation time and ID.
|
||||
type PagePosition struct {
|
||||
CreatedAt time.Time
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
// PageQuery is prepared by a service; Limit includes the lookahead row.
|
||||
type PageQuery struct {
|
||||
Limit int
|
||||
After *PagePosition
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type CreateUser struct {
|
||||
Username string
|
||||
DisplayName string
|
||||
Password string
|
||||
}
|
||||
|
||||
// CreateStoredUser contains the prepared database record, never a plaintext password.
|
||||
type CreateStoredUser struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string `json:"-"`
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
// Package contracts provides primitives to interact with the openapi HTTP API.
|
||||
//
|
||||
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version v2.5.1 DO NOT EDIT.
|
||||
package contracts
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
openapi_types "github.com/oapi-codegen/runtime/types"
|
||||
)
|
||||
|
||||
const (
|
||||
BasicAuthScopes = "basicAuth.Scopes"
|
||||
BearerAuthScopes = "bearerAuth.Scopes"
|
||||
)
|
||||
|
||||
// Defines values for AttachmentContentType.
|
||||
const (
|
||||
AttachmentContentTypeImagejpeg AttachmentContentType = "image/jpeg"
|
||||
AttachmentContentTypeImagepng AttachmentContentType = "image/png"
|
||||
)
|
||||
|
||||
// Defines values for AttachmentStatus.
|
||||
const (
|
||||
Ready AttachmentStatus = "ready"
|
||||
)
|
||||
|
||||
// Defines values for ErrorErrorCode.
|
||||
const (
|
||||
Conflict ErrorErrorCode = "conflict"
|
||||
InternalError ErrorErrorCode = "internal_error"
|
||||
InvalidRequest ErrorErrorCode = "invalid_request"
|
||||
NotFound ErrorErrorCode = "not_found"
|
||||
PayloadTooLarge ErrorErrorCode = "payload_too_large"
|
||||
RateLimited ErrorErrorCode = "rate_limited"
|
||||
Unauthorized ErrorErrorCode = "unauthorized"
|
||||
Unavailable ErrorErrorCode = "unavailable"
|
||||
UnsupportedMediaType ErrorErrorCode = "unsupported_media_type"
|
||||
)
|
||||
|
||||
// Defines values for HealthStatus.
|
||||
const (
|
||||
Ok HealthStatus = "ok"
|
||||
)
|
||||
|
||||
// Defines values for VariantContentType.
|
||||
const (
|
||||
VariantContentTypeImagejpeg VariantContentType = "image/jpeg"
|
||||
VariantContentTypeImagepng VariantContentType = "image/png"
|
||||
VariantContentTypeImagewebp VariantContentType = "image/webp"
|
||||
)
|
||||
|
||||
// Defines values for VariantName.
|
||||
const (
|
||||
Crop VariantName = "crop"
|
||||
Optimized VariantName = "optimized"
|
||||
Thumbnail VariantName = "thumbnail"
|
||||
)
|
||||
|
||||
// Defines values for DownloadAttachmentParamsVariant.
|
||||
const (
|
||||
Original DownloadAttachmentParamsVariant = "original"
|
||||
)
|
||||
|
||||
// Attachment defines model for Attachment.
|
||||
type Attachment struct {
|
||||
ChatId openapi_types.UUID `json:"chat_id"`
|
||||
ContentType AttachmentContentType `json:"content_type"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
Filename string `json:"filename"`
|
||||
Id openapi_types.UUID `json:"id"`
|
||||
OwnerId openapi_types.UUID `json:"owner_id"`
|
||||
Sha256 string `json:"sha256"`
|
||||
SizeBytes int `json:"size_bytes"`
|
||||
Status AttachmentStatus `json:"status"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Variants []Variant `json:"variants"`
|
||||
}
|
||||
|
||||
// AttachmentContentType defines model for Attachment.ContentType.
|
||||
type AttachmentContentType string
|
||||
|
||||
// AttachmentStatus defines model for Attachment.Status.
|
||||
type AttachmentStatus string
|
||||
|
||||
// Chat defines model for Chat.
|
||||
type Chat struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Id openapi_types.UUID `json:"id"`
|
||||
MemberIds []openapi_types.UUID `json:"member_ids"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
// ChatPage defines model for ChatPage.
|
||||
type ChatPage struct {
|
||||
Items []Chat `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// CreateChat defines model for CreateChat.
|
||||
type CreateChat struct {
|
||||
MemberIds []openapi_types.UUID `json:"member_ids"`
|
||||
Title string `json:"title"`
|
||||
}
|
||||
|
||||
// CreateMessage defines model for CreateMessage.
|
||||
type CreateMessage struct {
|
||||
AttachmentIds *[]openapi_types.UUID `json:"attachment_ids,omitempty"`
|
||||
Text *string `json:"text,omitempty"`
|
||||
}
|
||||
|
||||
// CreateUser defines model for CreateUser.
|
||||
type CreateUser struct {
|
||||
DisplayName string `json:"display_name"`
|
||||
Password *string `json:"password,omitempty"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
// Error defines model for Error.
|
||||
type Error struct {
|
||||
Error struct {
|
||||
Code ErrorErrorCode `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestId string `json:"request_id"`
|
||||
} `json:"error"`
|
||||
}
|
||||
|
||||
// ErrorErrorCode defines model for Error.Error.Code.
|
||||
type ErrorErrorCode string
|
||||
|
||||
// Health defines model for Health.
|
||||
type Health struct {
|
||||
Status HealthStatus `json:"status"`
|
||||
}
|
||||
|
||||
// HealthStatus defines model for Health.Status.
|
||||
type HealthStatus string
|
||||
|
||||
// Message defines model for Message.
|
||||
type Message struct {
|
||||
AttachmentIds []openapi_types.UUID `json:"attachment_ids"`
|
||||
ChatId openapi_types.UUID `json:"chat_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Id openapi_types.UUID `json:"id"`
|
||||
SenderId openapi_types.UUID `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
}
|
||||
|
||||
// MessagePage defines model for MessagePage.
|
||||
type MessagePage struct {
|
||||
Items []Message `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
// Session defines model for Session.
|
||||
type Session struct {
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
User User `json:"user"`
|
||||
}
|
||||
|
||||
// User defines model for User.
|
||||
type User struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Id openapi_types.UUID `json:"id"`
|
||||
Username string `json:"username"`
|
||||
}
|
||||
|
||||
// Variant defines model for Variant.
|
||||
type Variant struct {
|
||||
ContentType VariantContentType `json:"content_type"`
|
||||
Height int `json:"height"`
|
||||
Name VariantName `json:"name"`
|
||||
Sha256 string `json:"sha256"`
|
||||
SizeBytes int `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
}
|
||||
|
||||
// VariantContentType defines model for Variant.ContentType.
|
||||
type VariantContentType string
|
||||
|
||||
// VariantName defines model for Variant.Name.
|
||||
type VariantName string
|
||||
|
||||
// DownloadAttachmentParams defines parameters for DownloadAttachment.
|
||||
type DownloadAttachmentParams struct {
|
||||
Variant *DownloadAttachmentParamsVariant `form:"variant,omitempty" json:"variant,omitempty"`
|
||||
}
|
||||
|
||||
// DownloadAttachmentParamsVariant defines parameters for DownloadAttachment.
|
||||
type DownloadAttachmentParamsVariant string
|
||||
|
||||
// ListChatsParams defines parameters for ListChats.
|
||||
type ListChatsParams struct {
|
||||
Limit *int `form:"limit,omitempty" json:"limit,omitempty"`
|
||||
Cursor *string `form:"cursor,omitempty" json:"cursor,omitempty"`
|
||||
}
|
||||
|
||||
// UploadAttachmentParams defines parameters for UploadAttachment.
|
||||
type UploadAttachmentParams struct {
|
||||
Filename string `form:"filename" json:"filename"`
|
||||
}
|
||||
|
||||
// ListMessagesParams defines parameters for ListMessages.
|
||||
type ListMessagesParams struct {
|
||||
Limit *int `form:"limit,omitempty" json:"limit,omitempty"`
|
||||
Cursor *string `form:"cursor,omitempty" json:"cursor,omitempty"`
|
||||
}
|
||||
|
||||
// CreateMessageParams defines parameters for CreateMessage.
|
||||
type CreateMessageParams struct {
|
||||
// IdempotencyKey Обязателен с lab3. Область: пользователь + метод + путь + ключ; хранить результат не менее 24 ч. Повтор того же JSON возвращает исходный 201 и то же сообщение, другой JSON — 409.
|
||||
IdempotencyKey string `json:"Idempotency-Key"`
|
||||
}
|
||||
|
||||
// CreateChatJSONRequestBody defines body for CreateChat for application/json ContentType.
|
||||
type CreateChatJSONRequestBody = CreateChat
|
||||
|
||||
// CreateMessageJSONRequestBody defines body for CreateMessage for application/json ContentType.
|
||||
type CreateMessageJSONRequestBody = CreateMessage
|
||||
|
||||
// RegisterUserJSONRequestBody defines body for RegisterUser for application/json ContentType.
|
||||
type RegisterUserJSONRequestBody = CreateUser
|
||||
@@ -0,0 +1,274 @@
|
||||
package apperr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
grpcCodes "google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
//go:generate stringer -type=Code -linecomment
|
||||
type Code uint16
|
||||
|
||||
const (
|
||||
// CodeBadRequest = {AppError: BAD_REQUEST, HTTP:400}
|
||||
CodeBadRequest Code = iota
|
||||
// CodePayloadTooLarge = {AppError: REQUEST_ENTITY_TOO_LARGE, HTTP:413}
|
||||
CodePayloadTooLarge
|
||||
// CodeValidation = {AppError: VALIDATION, HTTP:400}
|
||||
CodeValidation
|
||||
// CodeUnauthorized = {AppError: UNAUTHORIZED, HTTP:401}
|
||||
CodeUnauthorized
|
||||
// CodeForbidden = {AppError: FORBIDDEN, HTTP:403}
|
||||
CodeForbidden
|
||||
// CodeNotFound = {AppError: NOT_FOUND, HTTP:404}
|
||||
CodeNotFound
|
||||
// CodeAlreadyExists = {AppError: ALREADY_EXISTS, HTTP:409}
|
||||
CodeAlreadyExists
|
||||
// CodeConflict = {AppError: CONFLICT, HTTP:409}
|
||||
CodeConflict
|
||||
// CodeTooManyRequests = {AppError: TOO_MANY_REQUESTS, HTTP:429}
|
||||
CodeTooManyRequests
|
||||
// CodeTimeout = {AppError: TIMEOUT, HTTP:504}
|
||||
CodeTimeout
|
||||
// CodeInternal = {AppError: INTERNAL, HTTP:500}
|
||||
CodeInternal
|
||||
// CodeServiceUnavailable = {AppError: SERVICE_UNAVAILABLE, HTTP:503}
|
||||
CodeServiceUnavailable
|
||||
// CodeOK = {AppError: OK, HTTP:200}
|
||||
CodeOK
|
||||
// CodeLocked = {AppError: LOCKED, HTTP:423}
|
||||
CodeLocked
|
||||
// CodeUnsupportedMediaType = {AppError: UNSUPPORTED_MEDIA_TYPE, HTTP:415}
|
||||
CodeUnsupportedMediaType
|
||||
// CodeUnavailable = {AppError: SERVICE_UNAVAILABLE, HTTP: 503}
|
||||
CodeUnavailable
|
||||
)
|
||||
|
||||
func (c Code) HTTPStatus() int {
|
||||
if status, ok := httpStatusByCode[c]; ok {
|
||||
return status
|
||||
}
|
||||
return http.StatusInternalServerError
|
||||
}
|
||||
|
||||
func (c Code) GRPCCode() grpcCodes.Code {
|
||||
if code, ok := grpcCodeByCode[c]; ok {
|
||||
return code
|
||||
}
|
||||
return grpcCodes.Internal
|
||||
}
|
||||
|
||||
func (c Code) PostgresCodes() []string {
|
||||
codes := postgresCodesByCode[c]
|
||||
if len(codes) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
cp := make([]string, len(codes))
|
||||
copy(cp, codes)
|
||||
return cp
|
||||
}
|
||||
|
||||
func (c Code) PostgresCode() string {
|
||||
codes := c.PostgresCodes()
|
||||
if len(codes) == 0 {
|
||||
return ""
|
||||
}
|
||||
return codes[0]
|
||||
}
|
||||
|
||||
func CodeFromError(err error) Code {
|
||||
if err == nil {
|
||||
return CodeInternal
|
||||
}
|
||||
|
||||
var coded interface {
|
||||
Code() Code
|
||||
}
|
||||
if errors.As(err, &coded) {
|
||||
return coded.Code()
|
||||
}
|
||||
|
||||
if code, ok := codeFromRedisError(err, false, false); ok {
|
||||
return code
|
||||
}
|
||||
|
||||
return CodeFromPostgresError(err)
|
||||
}
|
||||
|
||||
func HTTPStatusFromError(err error) int {
|
||||
return CodeFromError(err).HTTPStatus()
|
||||
}
|
||||
|
||||
func GRPCCodeFromError(err error) grpcCodes.Code {
|
||||
return CodeFromError(err).GRPCCode()
|
||||
}
|
||||
|
||||
func CodeFromHTTPStatus(status int) Code {
|
||||
if code, ok := codeByHTTPStatus[status]; ok {
|
||||
return code
|
||||
}
|
||||
return CodeInternal
|
||||
}
|
||||
|
||||
func CodeFromGRPC(code grpcCodes.Code) Code {
|
||||
if appCode, ok := codeByGRPCCode[code]; ok {
|
||||
return appCode
|
||||
}
|
||||
return CodeInternal
|
||||
}
|
||||
|
||||
func CodeFromPostgres(pgCode string) Code {
|
||||
if code, ok := codeByPostgresCode[pgCode]; ok {
|
||||
return code
|
||||
}
|
||||
return CodeInternal
|
||||
}
|
||||
|
||||
func CodeFromPostgresError(err error) Code {
|
||||
if err == nil {
|
||||
return CodeInternal
|
||||
}
|
||||
|
||||
if code, ok := codeByPgxError[err]; ok {
|
||||
return code
|
||||
}
|
||||
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
return CodeFromPostgres(pgErr.Code)
|
||||
}
|
||||
|
||||
return CodeInternal
|
||||
}
|
||||
|
||||
var (
|
||||
httpStatusByCode = map[Code]int{
|
||||
CodeBadRequest: http.StatusBadRequest,
|
||||
CodeUnauthorized: http.StatusUnauthorized,
|
||||
CodeForbidden: http.StatusForbidden,
|
||||
CodeNotFound: http.StatusNotFound,
|
||||
CodeAlreadyExists: http.StatusConflict,
|
||||
CodeConflict: http.StatusConflict,
|
||||
CodePayloadTooLarge: http.StatusRequestEntityTooLarge,
|
||||
CodeTooManyRequests: http.StatusTooManyRequests,
|
||||
CodeValidation: http.StatusBadRequest,
|
||||
CodeTimeout: http.StatusGatewayTimeout,
|
||||
CodeInternal: http.StatusInternalServerError,
|
||||
CodeServiceUnavailable: http.StatusServiceUnavailable,
|
||||
CodeUnsupportedMediaType: http.StatusUnsupportedMediaType,
|
||||
CodeUnavailable: http.StatusServiceUnavailable,
|
||||
CodeOK: http.StatusOK,
|
||||
CodeLocked: http.StatusLocked,
|
||||
}
|
||||
codeByHTTPStatus = map[int]Code{
|
||||
http.StatusBadRequest: CodeBadRequest,
|
||||
http.StatusUnauthorized: CodeUnauthorized,
|
||||
http.StatusForbidden: CodeForbidden,
|
||||
http.StatusNotFound: CodeNotFound,
|
||||
http.StatusConflict: CodeConflict,
|
||||
http.StatusRequestEntityTooLarge: CodePayloadTooLarge,
|
||||
http.StatusTooManyRequests: CodeTooManyRequests,
|
||||
http.StatusUnprocessableEntity: CodeValidation,
|
||||
http.StatusGatewayTimeout: CodeTimeout,
|
||||
http.StatusInternalServerError: CodeInternal,
|
||||
http.StatusServiceUnavailable: CodeServiceUnavailable,
|
||||
http.StatusOK: CodeOK,
|
||||
http.StatusLocked: CodeLocked,
|
||||
http.StatusUnsupportedMediaType: CodeUnavailable,
|
||||
}
|
||||
grpcCodeByCode = map[Code]grpcCodes.Code{
|
||||
CodeBadRequest: grpcCodes.InvalidArgument,
|
||||
CodePayloadTooLarge: grpcCodes.ResourceExhausted,
|
||||
CodeValidation: grpcCodes.InvalidArgument,
|
||||
CodeUnauthorized: grpcCodes.Unauthenticated,
|
||||
CodeForbidden: grpcCodes.PermissionDenied,
|
||||
CodeNotFound: grpcCodes.NotFound,
|
||||
CodeAlreadyExists: grpcCodes.AlreadyExists,
|
||||
CodeConflict: grpcCodes.Aborted,
|
||||
CodeTooManyRequests: grpcCodes.ResourceExhausted,
|
||||
CodeTimeout: grpcCodes.DeadlineExceeded,
|
||||
CodeInternal: grpcCodes.Internal,
|
||||
CodeServiceUnavailable: grpcCodes.Unavailable,
|
||||
CodeOK: grpcCodes.OK,
|
||||
CodeLocked: grpcCodes.FailedPrecondition,
|
||||
}
|
||||
codeByGRPCCode = map[grpcCodes.Code]Code{
|
||||
grpcCodes.InvalidArgument: CodeBadRequest,
|
||||
grpcCodes.ResourceExhausted: CodeTooManyRequests,
|
||||
grpcCodes.Unauthenticated: CodeUnauthorized,
|
||||
grpcCodes.PermissionDenied: CodeForbidden,
|
||||
grpcCodes.NotFound: CodeNotFound,
|
||||
grpcCodes.AlreadyExists: CodeAlreadyExists,
|
||||
grpcCodes.Aborted: CodeConflict,
|
||||
grpcCodes.FailedPrecondition: CodeLocked,
|
||||
grpcCodes.DeadlineExceeded: CodeTimeout,
|
||||
grpcCodes.Internal: CodeInternal,
|
||||
grpcCodes.Unimplemented: CodeInternal,
|
||||
grpcCodes.Unavailable: CodeServiceUnavailable,
|
||||
grpcCodes.OK: CodeOK,
|
||||
}
|
||||
postgresCodesByCode = map[Code][]string{
|
||||
CodeBadRequest: {
|
||||
pgerrcode.InvalidTextRepresentation,
|
||||
pgerrcode.InvalidParameterValue,
|
||||
},
|
||||
CodeValidation: {
|
||||
pgerrcode.NotNullViolation,
|
||||
pgerrcode.CheckViolation,
|
||||
pgerrcode.StringDataRightTruncationDataException,
|
||||
pgerrcode.NumericValueOutOfRange,
|
||||
},
|
||||
CodeNotFound: {
|
||||
pgerrcode.ForeignKeyViolation,
|
||||
},
|
||||
CodeAlreadyExists: {
|
||||
pgerrcode.UniqueViolation,
|
||||
},
|
||||
CodeConflict: {
|
||||
pgerrcode.ExclusionViolation,
|
||||
pgerrcode.SerializationFailure,
|
||||
pgerrcode.DeadlockDetected,
|
||||
},
|
||||
CodeLocked: {
|
||||
pgerrcode.LockNotAvailable,
|
||||
pgerrcode.ObjectInUse,
|
||||
},
|
||||
CodeTooManyRequests: {
|
||||
pgerrcode.TooManyConnections,
|
||||
},
|
||||
CodeTimeout: {
|
||||
pgerrcode.QueryCanceled,
|
||||
},
|
||||
CodeInternal: {
|
||||
pgerrcode.ConnectionException,
|
||||
pgerrcode.ConnectionFailure,
|
||||
pgerrcode.SQLClientUnableToEstablishSQLConnection,
|
||||
pgerrcode.SQLServerRejectedEstablishmentOfSQLConnection,
|
||||
pgerrcode.TransactionResolutionUnknown,
|
||||
},
|
||||
CodeServiceUnavailable: {
|
||||
pgerrcode.CannotConnectNow,
|
||||
},
|
||||
}
|
||||
codeByPostgresCode = buildCodeByPostgresCode(postgresCodesByCode)
|
||||
codeByPgxError = map[error]Code{
|
||||
pgx.ErrNoRows: CodeNotFound,
|
||||
pgx.ErrTxClosed: CodeInternal,
|
||||
pgx.ErrTxCommitRollback: CodeConflict,
|
||||
pgx.ErrTooManyRows: CodeInternal,
|
||||
}
|
||||
)
|
||||
|
||||
func buildCodeByPostgresCode(src map[Code][]string) map[string]Code {
|
||||
dst := make(map[string]Code, len(src))
|
||||
for code, pgCodes := range src {
|
||||
for _, pgCode := range pgCodes {
|
||||
dst[pgCode] = code
|
||||
}
|
||||
}
|
||||
return dst
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
// Code generated by "stringer -type=Code -linecomment"; DO NOT EDIT.
|
||||
|
||||
package apperr
|
||||
|
||||
import "strconv"
|
||||
|
||||
func _() {
|
||||
// An "invalid array index" compiler error signifies that the constant values have changed.
|
||||
// Re-run the stringer command to generate them again.
|
||||
var x [1]struct{}
|
||||
_ = x[CodeBadRequest-0]
|
||||
_ = x[CodePayloadTooLarge-1]
|
||||
_ = x[CodeValidation-2]
|
||||
_ = x[CodeUnauthorized-3]
|
||||
_ = x[CodeForbidden-4]
|
||||
_ = x[CodeNotFound-5]
|
||||
_ = x[CodeAlreadyExists-6]
|
||||
_ = x[CodeConflict-7]
|
||||
_ = x[CodeTooManyRequests-8]
|
||||
_ = x[CodeTimeout-9]
|
||||
_ = x[CodeInternal-10]
|
||||
_ = x[CodeServiceUnavailable-11]
|
||||
_ = x[CodeOK-12]
|
||||
_ = x[CodeLocked-13]
|
||||
}
|
||||
|
||||
const _Code_name = "CodeBadRequestCodePayloadTooLargeCodeValidationCodeUnauthorizedCodeForbiddenCodeNotFoundCodeAlreadyExistsCodeConflictCodeTooManyRequestsCodeTimeoutCodeInternalCodeServiceUnavailableCodeOKCodeLocked"
|
||||
|
||||
var _Code_index = [...]uint8{0, 14, 33, 47, 63, 76, 88, 105, 117, 136, 147, 159, 181, 187, 197}
|
||||
|
||||
func (i Code) String() string {
|
||||
idx := int(i) - 0
|
||||
if i < 0 || idx >= len(_Code_index)-1 {
|
||||
return "Code(" + strconv.FormatInt(int64(i), 10) + ")"
|
||||
}
|
||||
return _Code_name[_Code_index[idx]:_Code_index[idx+1]]
|
||||
}
|
||||
@@ -0,0 +1,123 @@
|
||||
package apperr
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
grpcCodes "google.golang.org/grpc/codes"
|
||||
)
|
||||
|
||||
type AppError struct {
|
||||
err error
|
||||
msg string
|
||||
code Code
|
||||
meta metadata
|
||||
}
|
||||
|
||||
func New(code Code, msg string) error {
|
||||
return &AppError{
|
||||
msg: msg,
|
||||
code: code,
|
||||
meta: metadata{},
|
||||
}
|
||||
}
|
||||
|
||||
func Wrap(err error, code Code, msg string) error {
|
||||
m := AppError{
|
||||
err: err,
|
||||
msg: msg,
|
||||
code: code,
|
||||
meta: metadata{},
|
||||
}
|
||||
return &m
|
||||
}
|
||||
|
||||
func WrapAppError(err error, msg string) error {
|
||||
m := AppError{
|
||||
err: err,
|
||||
msg: msg,
|
||||
code: CodeInternal,
|
||||
meta: metadata{},
|
||||
}
|
||||
|
||||
if mistake := As(err); mistake != nil {
|
||||
m.err = mistake
|
||||
m.code = mistake.code
|
||||
m.meta = mistake.meta
|
||||
}
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
func As(err error) *AppError {
|
||||
if mistakeErr, ok := errors.AsType[*AppError](err); ok {
|
||||
return mistakeErr
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func CodeIs(err error, code Code) bool {
|
||||
if appErr := As(err); appErr != nil && appErr.Code() == code {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (e *AppError) Error() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
|
||||
if e.err == nil {
|
||||
if e.msg != "" {
|
||||
return e.msg
|
||||
}
|
||||
return e.code.String()
|
||||
}
|
||||
|
||||
if e.msg == "" {
|
||||
return e.err.Error()
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s: %s", e.msg, e.err.Error())
|
||||
}
|
||||
|
||||
func (e *AppError) Unwrap() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.err
|
||||
}
|
||||
|
||||
func (e *AppError) Code() Code {
|
||||
if e == nil {
|
||||
return CodeInternal
|
||||
}
|
||||
return e.code
|
||||
}
|
||||
|
||||
func (e *AppError) Message() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
return e.msg
|
||||
}
|
||||
|
||||
func (e *AppError) Cause() error {
|
||||
if e == nil {
|
||||
return nil
|
||||
}
|
||||
return e.err
|
||||
}
|
||||
|
||||
func (e *AppError) Metadata() Metadata {
|
||||
return e.meta
|
||||
}
|
||||
|
||||
func (e *AppError) HTTPStatus() int {
|
||||
return e.Code().HTTPStatus()
|
||||
}
|
||||
|
||||
func (e *AppError) GRPCCode() grpcCodes.Code {
|
||||
return e.Code().GRPCCode()
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package apperr
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"maps"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var _ Metadata = (*metadata)(nil)
|
||||
|
||||
type Metadata interface {
|
||||
Get(key metakey) string
|
||||
Clone() map[metakey]string
|
||||
CloneToAnyMap() map[string]any
|
||||
String() string
|
||||
}
|
||||
|
||||
type metadata map[metakey]string
|
||||
|
||||
func (m metadata) Get(key metakey) string {
|
||||
return m[key]
|
||||
}
|
||||
|
||||
func (m metadata) Clone() map[metakey]string {
|
||||
return maps.Clone(m)
|
||||
}
|
||||
|
||||
func (m metadata) CloneToAnyMap() map[string]any {
|
||||
newM := make(map[string]any, len(m))
|
||||
for k, v := range m {
|
||||
newM[k.String()] = v
|
||||
}
|
||||
return newM
|
||||
}
|
||||
|
||||
func (m metadata) String() string {
|
||||
var b strings.Builder
|
||||
for k, v := range m {
|
||||
b.WriteString(fmt.Sprintf("%s: %s, ", k, v))
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package apperr
|
||||
|
||||
type metakey uint16
|
||||
|
||||
const (
|
||||
PgErrorCode metakey = iota
|
||||
RedisErrorCode
|
||||
)
|
||||
|
||||
func (m metakey) String() string {
|
||||
switch m {
|
||||
case PgErrorCode:
|
||||
return "pg_err_code"
|
||||
case RedisErrorCode:
|
||||
return "redis_err_code"
|
||||
default:
|
||||
return "unknown"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package apperr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
func WrapPostgres(err error, msg string) error {
|
||||
m := AppError{
|
||||
err: err,
|
||||
msg: msg,
|
||||
code: CodeInternal,
|
||||
meta: metadata{},
|
||||
}
|
||||
|
||||
m.handlePostgres()
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
func (e *AppError) handlePostgres() {
|
||||
if e.err == nil {
|
||||
e.code = CodeOK
|
||||
return
|
||||
}
|
||||
|
||||
var networkError net.Error
|
||||
var connectError *pgconn.ConnectError
|
||||
if errors.Is(e.err, context.DeadlineExceeded) || errors.Is(e.err, io.EOF) ||
|
||||
errors.Is(e.err, io.ErrUnexpectedEOF) || errors.As(e.err, &networkError) || errors.As(e.err, &connectError) {
|
||||
e.code = CodeServiceUnavailable
|
||||
e.msg = "database unavailable"
|
||||
return
|
||||
}
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](e.err); ok {
|
||||
if strings.HasPrefix(pgErr.Code, "08") || pgErr.Code == "57014" ||
|
||||
pgErr.Code == "57P01" || pgErr.Code == "57P02" || pgErr.Code == "57P03" || pgErr.Code == "53300" {
|
||||
e.code = CodeServiceUnavailable
|
||||
e.msg = "database unavailable"
|
||||
return
|
||||
}
|
||||
e.meta[PgErrorCode] = pgErr.Code
|
||||
e.code = CodeFromPostgres(pgErr.Code)
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case errors.Is(e.err, pgx.ErrNoRows):
|
||||
e.msg = fmt.Sprintf("not found: %s", e.msg)
|
||||
e.code = CodeNotFound
|
||||
return
|
||||
case errors.Is(e.err, pgx.ErrTooManyRows):
|
||||
e.msg = fmt.Sprintf("got too many rows: %s", e.msg)
|
||||
e.code = CodeInternal
|
||||
return
|
||||
case errors.Is(e.err, pgx.ErrTxClosed):
|
||||
e.msg = fmt.Sprintf("transaction is closed: %s", e.msg)
|
||||
e.code = CodeInternal
|
||||
return
|
||||
case errors.Is(e.err, pgx.ErrTxCommitRollback):
|
||||
e.msg = fmt.Sprintf("transaction commit resulted in rollback: %s", e.msg)
|
||||
e.code = CodeInternal
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (e *AppError) PgCode() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
if e.meta == nil {
|
||||
return ""
|
||||
}
|
||||
return e.meta.Get(PgErrorCode)
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
package apperr
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"strings"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func WrapRedis(err error, msg string) error {
|
||||
m := AppError{
|
||||
err: err,
|
||||
msg: msg,
|
||||
meta: metadata{},
|
||||
}
|
||||
|
||||
m.handleRedis()
|
||||
|
||||
return &m
|
||||
}
|
||||
|
||||
func CodeFromRedisError(err error) Code {
|
||||
code, ok := codeFromRedisError(err, true, true)
|
||||
if !ok {
|
||||
return CodeInternal
|
||||
}
|
||||
return code
|
||||
}
|
||||
|
||||
func (e *AppError) handleRedis() {
|
||||
if e.err == nil {
|
||||
e.code = CodeOK
|
||||
return
|
||||
}
|
||||
|
||||
if code := redisErrorCode(e.err); code != "" {
|
||||
e.meta[RedisErrorCode] = code
|
||||
}
|
||||
|
||||
if errors.Is(e.err, redis.Nil) {
|
||||
e.msg = fmt.Sprintf("not found: %s", e.msg)
|
||||
}
|
||||
|
||||
e.code = CodeFromRedisError(e.err)
|
||||
}
|
||||
|
||||
func (e *AppError) RedisCode() string {
|
||||
if e == nil {
|
||||
return ""
|
||||
}
|
||||
if e.meta == nil {
|
||||
return ""
|
||||
}
|
||||
return e.meta.Get(RedisErrorCode)
|
||||
}
|
||||
|
||||
func codeFromRedisError(err error, includeTransport bool, includeProtocolString bool) (Code, bool) {
|
||||
if err == nil {
|
||||
return CodeInternal, false
|
||||
}
|
||||
|
||||
switch {
|
||||
case errors.Is(err, redis.Nil):
|
||||
return CodeNotFound, true
|
||||
case errors.Is(err, redis.TxFailedErr):
|
||||
return CodeConflict, true
|
||||
case errors.Is(err, redis.ErrCrossSlot):
|
||||
return CodeBadRequest, true
|
||||
case errors.Is(err, redis.ErrPoolTimeout):
|
||||
return CodeTimeout, true
|
||||
case errors.Is(err, redis.ErrPoolExhausted):
|
||||
return CodeTooManyRequests, true
|
||||
case errors.Is(err, redis.ErrClosed):
|
||||
return CodeServiceUnavailable, true
|
||||
}
|
||||
|
||||
if includeTransport {
|
||||
if errors.Is(err, context.Canceled) ||
|
||||
errors.Is(err, context.DeadlineExceeded) {
|
||||
return CodeTimeout, true
|
||||
}
|
||||
|
||||
if netErr, ok := errors.AsType[net.Error](err); ok {
|
||||
if netErr.Timeout() {
|
||||
return CodeTimeout, true
|
||||
}
|
||||
return CodeServiceUnavailable, true
|
||||
}
|
||||
}
|
||||
|
||||
hasRedisError := isRedisError(err)
|
||||
if !hasRedisError && !includeProtocolString {
|
||||
return CodeInternal, false
|
||||
}
|
||||
|
||||
switch {
|
||||
case redis.IsExecAbortError(err):
|
||||
return CodeConflict, true
|
||||
case redis.IsMaxClientsError(err):
|
||||
return CodeTooManyRequests, true
|
||||
case redis.IsLoadingError(err),
|
||||
redis.IsReadOnlyError(err),
|
||||
redis.IsClusterDownError(err),
|
||||
redis.IsTryAgainError(err),
|
||||
redis.IsMasterDownError(err),
|
||||
redis.IsNoReplicasError(err),
|
||||
redis.IsOOMError(err):
|
||||
return CodeServiceUnavailable, true
|
||||
case redis.IsAuthError(err),
|
||||
redis.IsPermissionError(err):
|
||||
return CodeInternal, true
|
||||
}
|
||||
|
||||
if _, ok := redis.IsMovedError(err); ok {
|
||||
return CodeServiceUnavailable, true
|
||||
}
|
||||
if _, ok := redis.IsAskError(err); ok {
|
||||
return CodeServiceUnavailable, true
|
||||
}
|
||||
|
||||
if hasRedisError {
|
||||
return CodeInternal, true
|
||||
}
|
||||
|
||||
return CodeInternal, false
|
||||
}
|
||||
|
||||
func isRedisError(err error) bool {
|
||||
if err == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
var redisErr redis.Error
|
||||
return errors.As(err, &redisErr)
|
||||
}
|
||||
|
||||
func redisErrorCode(err error) string {
|
||||
switch {
|
||||
case err == nil:
|
||||
return ""
|
||||
case errors.Is(err, redis.Nil):
|
||||
return "nil"
|
||||
case errors.Is(err, redis.TxFailedErr):
|
||||
return "tx_failed"
|
||||
case errors.Is(err, redis.ErrCrossSlot):
|
||||
return "CROSSSLOT"
|
||||
case errors.Is(err, redis.ErrPoolTimeout):
|
||||
return "pool_timeout"
|
||||
case errors.Is(err, redis.ErrPoolExhausted):
|
||||
return "pool_exhausted"
|
||||
case errors.Is(err, redis.ErrClosed):
|
||||
return "closed"
|
||||
}
|
||||
|
||||
var redisErr redis.Error
|
||||
if !errors.As(err, &redisErr) {
|
||||
return ""
|
||||
}
|
||||
|
||||
msg := strings.TrimSpace(redisErr.Error())
|
||||
msg = strings.TrimPrefix(msg, "ERR ")
|
||||
parts := strings.Fields(msg)
|
||||
if len(parts) == 0 {
|
||||
return ""
|
||||
}
|
||||
return parts[0]
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package jwtprovider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/role"
|
||||
)
|
||||
|
||||
var _ Claims = (*jwtClaims)(nil)
|
||||
|
||||
type Claims interface {
|
||||
GetTopTokenID() (uuid.UUID, bool)
|
||||
GetTopUserID() (uuid.UUID, bool)
|
||||
GetTopRole() (role.Role, bool)
|
||||
GetTopIsAccess() (bool, bool)
|
||||
GetTopExpiresAt() (time.Time, bool)
|
||||
GetTopIssuedAt() (time.Time, bool)
|
||||
}
|
||||
|
||||
type claimsParseFunc func(token *dto.Token) (*jwtClaims, error)
|
||||
|
||||
type jwtClaims struct {
|
||||
jwt.RegisteredClaims
|
||||
|
||||
Nonce string `json:"nonce,omitempty"`
|
||||
IsAccess bool `json:"is_access"`
|
||||
Role role.Role `json:"role"`
|
||||
}
|
||||
|
||||
func (claims *jwtClaims) GetTopTokenID() (uuid.UUID, bool) {
|
||||
parsed, err := uuid.Parse(claims.ID)
|
||||
if err != nil {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
return parsed, true
|
||||
}
|
||||
|
||||
func (claims *jwtClaims) GetTopUserID() (uuid.UUID, bool) {
|
||||
parsed, err := uuid.Parse(claims.Subject)
|
||||
if err != nil {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
return parsed, true
|
||||
}
|
||||
|
||||
func (claims *jwtClaims) GetTopRole() (role.Role, bool) {
|
||||
return claims.Role, true
|
||||
}
|
||||
|
||||
func (claims *jwtClaims) GetTopIsAccess() (bool, bool) {
|
||||
return claims.IsAccess, true
|
||||
}
|
||||
|
||||
func (claims *jwtClaims) GetTopExpiresAt() (time.Time, bool) {
|
||||
if claims.ExpiresAt == nil {
|
||||
return time.Time{}, false
|
||||
}
|
||||
return claims.ExpiresAt.Time, true
|
||||
}
|
||||
|
||||
func (claims *jwtClaims) GetTopIssuedAt() (time.Time, bool) {
|
||||
if claims.IssuedAt == nil {
|
||||
return time.Time{}, false
|
||||
}
|
||||
return claims.IssuedAt.Time, true
|
||||
}
|
||||
|
||||
func (claims *jwtClaims) userData() (*dto.UserDataInToken, error) {
|
||||
var u dto.UserDataInToken
|
||||
|
||||
tokenID, err := uuid.Parse(claims.ID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse token id: %w", err)
|
||||
}
|
||||
u.TokenID = tokenID
|
||||
|
||||
userID, err := uuid.Parse(claims.Subject)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse user id from token %q: %w", claims.Subject, err)
|
||||
}
|
||||
u.UserID = userID
|
||||
|
||||
u.Role = claims.Role
|
||||
u.IsAccess = claims.IsAccess
|
||||
|
||||
if claims.ExpiresAt != nil {
|
||||
u.ExpiresAt = claims.ExpiresAt.Time
|
||||
}
|
||||
|
||||
if claims.IssuedAt != nil {
|
||||
u.IssuedAt = claims.IssuedAt.Time
|
||||
}
|
||||
|
||||
return &u, nil
|
||||
}
|
||||
|
||||
func (p *provider) parseClaimsFromProtectedJWT(token *dto.Token) (*jwtClaims, error) {
|
||||
if token == nil {
|
||||
return nil, ErrTokenIsMissing
|
||||
}
|
||||
|
||||
claims := &jwtClaims{}
|
||||
|
||||
parsed, err := jwt.ParseWithClaims(
|
||||
token.Payload,
|
||||
claims,
|
||||
p.readKeyFunc,
|
||||
jwt.WithValidMethods([]string{jwt.SigningMethodRS256.Alg()}),
|
||||
jwt.WithExpirationRequired(),
|
||||
jwt.WithIssuedAt(),
|
||||
)
|
||||
if err != nil {
|
||||
slog.Error("failed to parse token with claims", "error", err)
|
||||
return nil, ErrInvalidToken
|
||||
}
|
||||
|
||||
if !parsed.Valid {
|
||||
slog.Error("failed to parse token: provided invalid token", "valid", parsed.Valid)
|
||||
return nil, ErrInvalidToken
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
|
||||
func (p *provider) parseClaimsFromExposedJWT(token *dto.Token) (*jwtClaims, error) {
|
||||
if token == nil {
|
||||
return nil, ErrTokenIsMissing
|
||||
}
|
||||
|
||||
claims := &jwtClaims{}
|
||||
|
||||
_, _, err := new(jwt.Parser).ParseUnverified(token.Payload, claims)
|
||||
if err != nil {
|
||||
slog.Error("failed to parse token: provided invalid token")
|
||||
return nil, ErrInvalidToken
|
||||
}
|
||||
|
||||
return claims, nil
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
package jwtprovider
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
const (
|
||||
accessTokenCookieKey = "auth-access-token"
|
||||
refreshTokenCookieKey = "auth-refresh-token"
|
||||
expiresAtCookieKey = "auth-exp"
|
||||
)
|
||||
|
||||
type CookieBuilderFabric struct {
|
||||
fabric func() http.Cookie
|
||||
}
|
||||
|
||||
func newCookieBuilder(
|
||||
domain string,
|
||||
path string,
|
||||
secure bool,
|
||||
sameSite http.SameSite,
|
||||
) *CookieBuilderFabric {
|
||||
return &CookieBuilderFabric{
|
||||
fabric: newCookieFabric(domain, path, secure, sameSite),
|
||||
}
|
||||
}
|
||||
|
||||
func (b *CookieBuilderFabric) Builder() *CookieBuilder {
|
||||
return &CookieBuilder{
|
||||
cookie: b.fabric(),
|
||||
}
|
||||
}
|
||||
|
||||
func newCookieFabric(
|
||||
_ string,
|
||||
path string,
|
||||
secure bool,
|
||||
sameSite http.SameSite) func() http.Cookie {
|
||||
return func() http.Cookie {
|
||||
return http.Cookie{
|
||||
Path: path,
|
||||
Secure: secure,
|
||||
SameSite: sameSite,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type CookieBuilder struct {
|
||||
cookie http.Cookie
|
||||
}
|
||||
|
||||
func (b *CookieBuilder) AccessName() *CookieBuilder {
|
||||
b.cookie.Name = accessTokenCookieKey
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *CookieBuilder) ExpiresName() *CookieBuilder {
|
||||
b.cookie.Name = expiresAtCookieKey
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *CookieBuilder) RefreshName() *CookieBuilder {
|
||||
b.cookie.Name = refreshTokenCookieKey
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *CookieBuilder) Value(value string) *CookieBuilder {
|
||||
b.cookie.Value = value
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *CookieBuilder) HTTPOnly() *CookieBuilder {
|
||||
b.cookie.HttpOnly = true
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *CookieBuilder) MaxAge(maxAge int) *CookieBuilder {
|
||||
b.cookie.MaxAge = maxAge
|
||||
return b
|
||||
}
|
||||
|
||||
func (b *CookieBuilder) Build() *http.Cookie {
|
||||
return new(b.cookie)
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package jwtprovider
|
||||
|
||||
import (
|
||||
"errors"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrDecode = errors.New("failed to decode argument")
|
||||
ErrNotFound = errors.New("not found")
|
||||
ErrUnexpectedAlgorithm = errors.New("unexpected signing algorithm")
|
||||
ErrTokenIsMissing = errors.New("token is missing")
|
||||
ErrInvalidToken = errors.New("provided invalid token")
|
||||
ErrInvalidUserDataInToken = errors.New("invalid user data in token")
|
||||
ErrInvalidTokenClaims = errors.New("provided invalid token claims")
|
||||
ErrInvalidBirthDateFormat = errors.New("provided invalid birth date format")
|
||||
ErrTokenExpired = errors.New("token is expired")
|
||||
ErrFailedToSign = errors.New("failed to sign jwt")
|
||||
)
|
||||
@@ -0,0 +1,26 @@
|
||||
package jwtprovider
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/role"
|
||||
)
|
||||
|
||||
type Provider interface {
|
||||
Parse(token *dto.Token) (*dto.UserDataInToken, error)
|
||||
CreateRefreshAndAccessTokensForUser(userID uuid.UUID, role role.Role) (*dto.Tokens, error)
|
||||
ParseVerified(token *dto.Token) (*dto.UserDataInToken, error)
|
||||
ParseUnverified(token *dto.Token) (*dto.UserDataInToken, error)
|
||||
ValidateUserDataInToken(u *dto.UserDataInToken) error
|
||||
}
|
||||
|
||||
type Cookie interface {
|
||||
Enabled() bool
|
||||
Set(w http.ResponseWriter, tokens *dto.Tokens)
|
||||
Clear(w http.ResponseWriter)
|
||||
AccessToken(r *http.Request) *dto.AccessToken
|
||||
RefreshToken(r *http.Request) *dto.RefreshToken
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package jwtprovider
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
)
|
||||
|
||||
type manager struct {
|
||||
enabled bool
|
||||
cookieFabric *CookieBuilderFabric
|
||||
prv Provider
|
||||
}
|
||||
|
||||
func NewCookieManager(cfg *config.Config, provider Provider) Cookie {
|
||||
m := &manager{
|
||||
//enabled: cfg.Cookies.Enabled,
|
||||
//cookieFabric: newCookieBuilder(
|
||||
// cfg.Cookies.Domain,
|
||||
// cfg.Cookies.Path,
|
||||
// cfg.Cookies.Secure,
|
||||
// cfg.Cookies.SameSite,
|
||||
//),
|
||||
prv: provider,
|
||||
}
|
||||
|
||||
if !m.enabled {
|
||||
return m
|
||||
}
|
||||
|
||||
if err := m.cookieFabric.
|
||||
Builder().
|
||||
AccessName().
|
||||
Value("test").
|
||||
HTTPOnly().
|
||||
MaxAge(0).
|
||||
Build().
|
||||
Valid(); err != nil {
|
||||
panic(fmt.Errorf("cookie validation failed: %w", err))
|
||||
}
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *manager) Enabled() bool {
|
||||
return m.enabled
|
||||
}
|
||||
|
||||
func (m *manager) Set(w http.ResponseWriter, tokens *dto.Tokens) {
|
||||
if !m.enabled || tokens == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if tokens.AccessToken.Payload != "" {
|
||||
m.setAccess(tokens.AccessToken, w)
|
||||
}
|
||||
|
||||
if tokens.RefreshToken.Payload != "" {
|
||||
m.setRefresh(tokens.RefreshToken, w)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *manager) Clear(w http.ResponseWriter) {
|
||||
if !m.enabled {
|
||||
return
|
||||
}
|
||||
|
||||
for _, name := range []string{accessTokenCookieKey, refreshTokenCookieKey, expiresAtCookieKey} {
|
||||
b := m.cookieFabric.
|
||||
Builder().
|
||||
MaxAge(-1)
|
||||
|
||||
switch name {
|
||||
case expiresAtCookieKey:
|
||||
b.ExpiresName()
|
||||
case refreshTokenCookieKey:
|
||||
b.RefreshName().
|
||||
HTTPOnly()
|
||||
case accessTokenCookieKey:
|
||||
b.AccessName().
|
||||
HTTPOnly()
|
||||
}
|
||||
http.SetCookie(w, b.Build())
|
||||
}
|
||||
}
|
||||
|
||||
func (m *manager) AccessToken(r *http.Request) *dto.AccessToken {
|
||||
if !m.enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
c, err := r.Cookie(accessTokenCookieKey)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &dto.AccessToken{
|
||||
Payload: c.Value,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *manager) RefreshToken(r *http.Request) *dto.RefreshToken {
|
||||
if !m.enabled {
|
||||
return nil
|
||||
}
|
||||
|
||||
c, err := r.Cookie(refreshTokenCookieKey)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &dto.RefreshToken{
|
||||
Payload: c.Value,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *manager) setAccess(token dto.AccessToken, w http.ResponseWriter) {
|
||||
accessTTL := m.tokenTTL(token.Token())
|
||||
if accessTTL < 0 {
|
||||
http.SetCookie(w, m.cookieFabric.
|
||||
Builder().
|
||||
AccessName().
|
||||
HTTPOnly().
|
||||
MaxAge(-1).
|
||||
Build(),
|
||||
)
|
||||
http.SetCookie(w, m.cookieFabric.
|
||||
Builder().
|
||||
ExpiresName().
|
||||
MaxAge(-1).
|
||||
Build(),
|
||||
)
|
||||
} else if tokenExpiresAt := m.tokenExpiry(token.Token()); !tokenExpiresAt.IsZero() {
|
||||
http.SetCookie(w, m.cookieFabric.
|
||||
Builder().
|
||||
AccessName().
|
||||
Value(token.Payload).
|
||||
HTTPOnly().
|
||||
MaxAge(accessTTL).
|
||||
Build(),
|
||||
)
|
||||
http.SetCookie(w, m.cookieFabric.
|
||||
Builder().
|
||||
ExpiresName().
|
||||
Value(strconv.FormatInt(tokenExpiresAt.Unix(), 10)).
|
||||
MaxAge(accessTTL).
|
||||
Build(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *manager) setRefresh(token dto.RefreshToken, w http.ResponseWriter) {
|
||||
refreshTTL := m.tokenTTL(token.Token())
|
||||
if refreshTTL < 0 {
|
||||
http.SetCookie(w, m.cookieFabric.
|
||||
Builder().
|
||||
RefreshName().
|
||||
HTTPOnly().
|
||||
MaxAge(-1).
|
||||
Build(),
|
||||
)
|
||||
} else {
|
||||
http.SetCookie(w, m.cookieFabric.
|
||||
Builder().
|
||||
RefreshName().
|
||||
Value(token.Payload).
|
||||
HTTPOnly().
|
||||
MaxAge(refreshTTL).
|
||||
Build(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *manager) tokenTTL(token *dto.Token) int {
|
||||
expiresAt := m.tokenExpiry(token)
|
||||
|
||||
if expiresAt.IsZero() {
|
||||
return -1
|
||||
}
|
||||
|
||||
ttl := int(time.Until(expiresAt).Seconds())
|
||||
if ttl <= 0 {
|
||||
return -1
|
||||
}
|
||||
|
||||
return ttl
|
||||
}
|
||||
|
||||
func (m *manager) tokenExpiry(token *dto.Token) time.Time {
|
||||
data, err := m.prv.ParseUnverified(token)
|
||||
if err != nil {
|
||||
return time.Time{}
|
||||
}
|
||||
return data.ExpiresAt
|
||||
}
|
||||
@@ -0,0 +1,187 @@
|
||||
package jwtprovider
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/dto"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/environment"
|
||||
"gitea.privcloud.tech/mtusi2026/internal/pkg/types/role"
|
||||
)
|
||||
|
||||
var (
|
||||
_ Provider = (*provider)(nil)
|
||||
)
|
||||
|
||||
type provider struct {
|
||||
accessLifetime time.Duration
|
||||
refreshLifetime time.Duration
|
||||
publicKey *rsa.PublicKey
|
||||
privateKey *rsa.PrivateKey
|
||||
parseFunc claimsParseFunc
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) Provider {
|
||||
p := provider{
|
||||
accessLifetime: cfg.JWT.AccessTokenLifetime,
|
||||
refreshLifetime: cfg.JWT.RefreshTokenLifetime,
|
||||
}
|
||||
p.parseFunc = p.parseClaimsFromProtectedJWT
|
||||
|
||||
if cfg.Application.Environment == environment.ApplicationEnvironmentDev {
|
||||
p.parseFunc = p.parseClaimsFromExposedJWT
|
||||
}
|
||||
|
||||
var err error
|
||||
if p.publicKey, err = readPublicKeyFromFile(cfg.JWT.PublicKeyPath); err != nil {
|
||||
slog.Error("failed to read public key", "error", err)
|
||||
panic(fmt.Errorf("failed to read public key: %w", err))
|
||||
}
|
||||
if p.privateKey, err = readPrivateKeyFromFile(cfg.JWT.PrivateKeyPath); err != nil {
|
||||
slog.Error("failed to read private key", "error", err)
|
||||
panic(fmt.Errorf("failed to read private key: %w", err))
|
||||
}
|
||||
|
||||
return &p
|
||||
}
|
||||
|
||||
func (p *provider) Parse(token *dto.Token) (*dto.UserDataInToken, error) {
|
||||
claims, err := p.parseFunc(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return claims.userData()
|
||||
}
|
||||
|
||||
func (p *provider) ParseUnverified(t *dto.Token) (*dto.UserDataInToken, error) {
|
||||
if t == nil {
|
||||
return nil, ErrTokenIsMissing
|
||||
}
|
||||
|
||||
claims, err := p.parseClaimsFromExposedJWT(t)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return claims.userData()
|
||||
}
|
||||
|
||||
func (p *provider) ParseVerified(t *dto.Token) (*dto.UserDataInToken, error) {
|
||||
if t == nil {
|
||||
return nil, ErrTokenIsMissing
|
||||
}
|
||||
claims, err := p.parseClaimsFromProtectedJWT(t)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse token: %w", err)
|
||||
}
|
||||
|
||||
return claims.userData()
|
||||
}
|
||||
|
||||
func (p *provider) CreateAccessTokenForUser(userID uuid.UUID, role role.Role) (*dto.AccessToken, error) {
|
||||
t, err := p.createTokenForUser(userID, true, role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.AccessToken{
|
||||
Payload: t.Payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *provider) CreateRefreshTokenForUser(userID uuid.UUID, role role.Role) (*dto.RefreshToken, error) {
|
||||
t, err := p.createTokenForUser(userID, false, role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.RefreshToken{
|
||||
Payload: t.Payload,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *provider) CreateRefreshAndAccessTokensForUser(userID uuid.UUID, role role.Role) (*dto.Tokens, error) {
|
||||
access, err := p.CreateAccessTokenForUser(userID, role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
refresh, err := p.CreateRefreshTokenForUser(userID, role)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &dto.Tokens{
|
||||
AccessToken: *access,
|
||||
RefreshToken: *refresh,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (p *provider) createTokenForUser(userID uuid.UUID, isAccess bool, role role.Role) (*dto.Token, error) {
|
||||
now := time.Now()
|
||||
var tokenExpiresAt time.Time
|
||||
if isAccess {
|
||||
tokenExpiresAt = now.Add(p.accessLifetime)
|
||||
} else {
|
||||
tokenExpiresAt = now.Add(p.refreshLifetime)
|
||||
}
|
||||
var nonce [32]byte
|
||||
if _, err := rand.Read(nonce[:]); err != nil {
|
||||
return nil, fmt.Errorf("generate token nonce: %w", err)
|
||||
}
|
||||
claims := &jwtClaims{
|
||||
Nonce: base64.RawURLEncoding.EncodeToString(nonce[:]),
|
||||
RegisteredClaims: jwt.RegisteredClaims{
|
||||
Subject: userID.String(),
|
||||
ExpiresAt: jwt.NewNumericDate(tokenExpiresAt),
|
||||
NotBefore: jwt.NewNumericDate(now),
|
||||
IssuedAt: jwt.NewNumericDate(now),
|
||||
ID: uuid.NewString(),
|
||||
},
|
||||
IsAccess: isAccess,
|
||||
Role: role,
|
||||
}
|
||||
t := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
|
||||
|
||||
payload, err := t.SignedString(p.privateKey)
|
||||
if err != nil {
|
||||
slog.Error("failed to sign jwt", "error", err)
|
||||
return nil, ErrFailedToSign
|
||||
}
|
||||
return &dto.Token{Payload: payload}, nil
|
||||
}
|
||||
|
||||
// TODO refactor maybe delete
|
||||
func (p *provider) ValidateUserDataInToken(u *dto.UserDataInToken) error {
|
||||
if u == nil {
|
||||
return ErrInvalidUserDataInToken
|
||||
}
|
||||
|
||||
switch {
|
||||
case u.TokenID == uuid.Nil:
|
||||
return fmt.Errorf("%w: unexpected nil token id", ErrInvalidUserDataInToken)
|
||||
case u.UserID == uuid.Nil:
|
||||
return fmt.Errorf("%w: unexpected nil user id", ErrInvalidUserDataInToken)
|
||||
case u.IssuedAt.IsZero():
|
||||
return fmt.Errorf("%w: unexpected zero issued at", ErrInvalidUserDataInToken)
|
||||
case u.Email != nil && *u.Email == "":
|
||||
return fmt.Errorf("%w: unexpected empty email", ErrInvalidUserDataInToken)
|
||||
case u.PhoneNumber != nil && *u.PhoneNumber == "":
|
||||
return fmt.Errorf("%w: unexpected empty phone number", ErrInvalidUserDataInToken)
|
||||
case u.ExpiresAt.IsZero():
|
||||
return fmt.Errorf("%w: unexpected zero expires at", ErrInvalidUserDataInToken)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *provider) readKeyFunc(token *jwt.Token) (interface{}, error) {
|
||||
if token.Method != jwt.SigningMethodRS256 {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return p.publicKey, nil
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package jwtprovider
|
||||
|
||||
import (
|
||||
"crypto/rsa"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
func readPrivateKeyFromFile(path string) (*rsa.PrivateKey, error) {
|
||||
key, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read public key: %w", err)
|
||||
}
|
||||
|
||||
privateKey, err := jwt.ParseRSAPrivateKeyFromPEM(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse private key: %w", err)
|
||||
}
|
||||
return privateKey, nil
|
||||
}
|
||||
|
||||
func readPublicKeyFromFile(path string) (*rsa.PublicKey, error) {
|
||||
key, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read public key: %w", err)
|
||||
}
|
||||
|
||||
privateKey, err := jwt.ParseRSAPublicKeyFromPEM(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to parse private key: %w", err)
|
||||
}
|
||||
return privateKey, nil
|
||||
}
|
||||
Vendored
+43
@@ -0,0 +1,43 @@
|
||||
package cache
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
|
||||
"gitea.privcloud.tech/mtusi2026/internal/config"
|
||||
)
|
||||
|
||||
const defaultRedisPort = 6379
|
||||
|
||||
// OperationTimeout bounds each optional cache operation independently of the request.
|
||||
const OperationTimeout = 300 * time.Millisecond
|
||||
|
||||
type Client struct {
|
||||
*redis.Client
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) *Client {
|
||||
if cfg.Redis.Port == 0 {
|
||||
cfg.Redis.Port = defaultRedisPort
|
||||
}
|
||||
|
||||
return &Client{
|
||||
Client: redis.NewClient(cfg.Redis.BuildRedisOptions()),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, OperationTimeout)
|
||||
defer cancel()
|
||||
return c.Client.Ping(ctx).Err()
|
||||
}
|
||||
|
||||
func (c *Client) Start(ctx context.Context) error {
|
||||
return c.Ping(ctx)
|
||||
}
|
||||
|
||||
func (c *Client) Stop(_ context.Context) error {
|
||||
return c.Client.Close()
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package encrypting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const (
|
||||
argon2idMemory = 64 * 1024 // KiB
|
||||
argon2idTime = 3
|
||||
argon2idThreads = 4
|
||||
argon2idSaltLen = 16
|
||||
argon2idKeyLen = 32
|
||||
// Only this bounded profile is accepted; stored parameters cannot request arbitrary resources.
|
||||
argon2idPrefix = "$argon2id$v=19$m=65536,t=3,p=4$"
|
||||
)
|
||||
|
||||
func (e *encryptor) EncryptSecretUsingArgon2id(ctx context.Context, secret string) (string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
var salt [argon2idSaltLen]byte
|
||||
if _, err := rand.Read(salt[:]); err != nil {
|
||||
return "", fmt.Errorf("%w: %w", ErrFailedToEncryptPassword, err)
|
||||
}
|
||||
key := argon2.IDKey([]byte(secret), salt[:], argon2idTime, argon2idMemory, argon2idThreads, argon2idKeyLen)
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return argon2idPrefix + base64.RawStdEncoding.EncodeToString(salt[:]) + "$" + base64.RawStdEncoding.EncodeToString(key), nil
|
||||
}
|
||||
|
||||
func (e *encryptor) CompareHashAndSecretUsingArgon2id(ctx context.Context, hash, secret string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
encoding := base64.RawStdEncoding.Strict()
|
||||
if len(hash) != len(argon2idPrefix)+encoding.EncodedLen(argon2idSaltLen)+1+encoding.EncodedLen(argon2idKeyLen) {
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
payload, ok := strings.CutPrefix(hash, argon2idPrefix)
|
||||
if !ok {
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
saltText, keyText, ok := strings.Cut(payload, "$")
|
||||
if !ok {
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
salt, err := encoding.DecodeString(saltText)
|
||||
if err != nil || len(salt) != argon2idSaltLen {
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
storedKey, err := encoding.DecodeString(keyText)
|
||||
if err != nil || len(storedKey) != argon2idKeyLen {
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
key := argon2.IDKey([]byte(secret), salt, argon2idTime, argon2idMemory, argon2idThreads, argon2idKeyLen)
|
||||
if err = ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if subtle.ConstantTimeCompare(storedKey, key) != 1 {
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package encrypting
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type encryptor struct {
|
||||
cost int
|
||||
generateFromPasswordFunc func([]byte, int) ([]byte, error)
|
||||
compareHashAndPasswordFunc func([]byte, []byte) error
|
||||
}
|
||||
|
||||
func New() Encryptor {
|
||||
return &encryptor{
|
||||
cost: bcrypt.DefaultCost,
|
||||
generateFromPasswordFunc: bcrypt.GenerateFromPassword,
|
||||
compareHashAndPasswordFunc: bcrypt.CompareHashAndPassword,
|
||||
}
|
||||
}
|
||||
|
||||
func (e *encryptor) EncryptSecret(_ context.Context, secret string) (string, error) {
|
||||
res, err := e.generateFromPasswordFunc([]byte(secret), e.cost)
|
||||
if err != nil {
|
||||
return "", ErrFailedToEncryptPassword
|
||||
}
|
||||
return string(res), nil
|
||||
}
|
||||
|
||||
// EncryptSecretUsingSum256 hashes a high-entropy session token, not a password.
|
||||
func (e *encryptor) EncryptSecretUsingSum256(ctx context.Context, secret string) (string, error) {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
sum := sha256.Sum256([]byte(secret))
|
||||
return "sha256:" + hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
|
||||
func (e *encryptor) CompareHashAndSecret(_ context.Context, hash, password string) error {
|
||||
err := e.compareHashAndPasswordFunc([]byte(hash), []byte(password))
|
||||
if err != nil {
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *encryptor) CompareHashAndSecretUsingSum256(ctx context.Context, hash, secret string) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
sum := sha256.Sum256([]byte(secret))
|
||||
if payload, ok := strings.CutPrefix(hash, "sha256:"); ok {
|
||||
stored, err := hex.DecodeString(payload)
|
||||
if err != nil || len(stored) != sha256.Size || subtle.ConstantTimeCompare(stored, sum[:]) != 1 {
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
return nil
|
||||
}
|
||||
// Preserve existing lab2/lab3 sessions across a rolling deployment.
|
||||
// Passwords continue to use Argon2id; only old random-token fingerprints use this fallback.
|
||||
if strings.HasPrefix(hash, "$2") {
|
||||
if err := e.compareHashAndPasswordFunc([]byte(hash), []byte(hex.EncodeToString(sum[:]))); err == nil {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return ErrFailedToComparePasswords
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package encrypting
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrFailedToEncryptPassword = errors.New("failed to encrypt password")
|
||||
ErrFailedToComparePasswords = errors.New("failed to compare passwords")
|
||||
)
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user