forked from mtusi2026/mtusi2026lab5
Compare commits
23
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c872d95208 | ||
|
|
346720166b | ||
|
|
179c795758 | ||
|
|
f8ca7a7218 | ||
|
|
5442dfd3bc | ||
|
|
6ba7db4b7d | ||
|
|
6e09da0b46 | ||
|
|
f2000a7883 | ||
|
|
97b43a9c98 | ||
|
|
be10119910 | ||
|
|
73d57e31a5 | ||
|
|
9149dddf11 | ||
|
|
eafc437933 | ||
|
|
4a6176fe81 | ||
|
|
b35faa5af8 | ||
|
|
19152e7d18 | ||
|
|
8b0ab5440d | ||
|
|
4dbbd65bcf | ||
|
|
425ffc6182 | ||
|
|
d745395aa0 | ||
|
|
e889d2e292 | ||
|
|
f2fc4b3e1f | ||
|
|
36d9b0c8f6 |
@@ -18,3 +18,4 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
.agent
|
||||
|
||||
+13
-6
@@ -1,24 +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
|
||||
MAX_INFLIGHT_REQUESTS=0
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
|
||||
LOG_LEVEL=INFO
|
||||
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
|
||||
# Session store: postgres | redis (redis requires CACHE_URL_FILE)
|
||||
SESSION_STORE=redis
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
|
||||
API_REPLICAS=2
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
S3_REGION=us-east-1
|
||||
S3_BUCKET=messenger-attachments
|
||||
S3_ACCESS_KEY_ID_FILE=/run/secrets/s3_access_key_id
|
||||
S3_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_secret_access_key
|
||||
S3_BOOTSTRAP_ACCESS_KEY_ID_FILE=/run/secrets/s3_bootstrap_access_key_id
|
||||
S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE=/run/secrets/s3_bootstrap_secret_access_key
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
|
||||
BROKER_URL_FILE=/run/secrets/broker_url
|
||||
BROKER_PASSWORD_FILE=/run/secrets/broker_password
|
||||
WORKER_CONCURRENCY=2
|
||||
JOB_MAX_ATTEMPTS=5
|
||||
JOB_TIMEOUT_SECONDS=60
|
||||
|
||||
@@ -28,5 +28,6 @@ coverage/
|
||||
.DS_Store
|
||||
.idea/
|
||||
.vscode/
|
||||
.agent/
|
||||
# Keep small, sanitized evidence summaries in evidence/ under version control.
|
||||
# Inspect git diff --cached: ignore rules are not a secret scanner.
|
||||
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
FROM golang:1.27.1 AS build
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /out/server ./cmd/server
|
||||
|
||||
FROM gcr.io/distroless/static-debian12:nonroot
|
||||
WORKDIR /app
|
||||
COPY --from=build /out/server .
|
||||
COPY contracts ./contracts
|
||||
COPY web/swagger ./web/swagger
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["/app/server", "healthcheck"]
|
||||
|
||||
ENTRYPOINT ["/app/server"]
|
||||
@@ -0,0 +1,8 @@
|
||||
FROM golang:1.27.1 AS build
|
||||
ENV CGO_ENABLED=0
|
||||
RUN go install github.com/pressly/goose/v3/cmd/goose@v3.28.0
|
||||
|
||||
FROM alpine:3.24
|
||||
COPY --from=build /go/bin/goose /usr/local/bin/goose
|
||||
COPY db/migrations /migrations
|
||||
ENTRYPOINT ["sh", "-c", "goose -dir /migrations postgres \"$(cat /run/secrets/database_url)\" up"]
|
||||
@@ -0,0 +1,14 @@
|
||||
FROM golang:1.27.1 AS build
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -o /out/reconcile ./cmd/reconcile
|
||||
|
||||
FROM gcr.io/distroless/static-debian12:nonroot
|
||||
WORKDIR /app
|
||||
COPY --from=build /out/reconcile .
|
||||
|
||||
ENTRYPOINT ["/app/reconcile"]
|
||||
@@ -0,0 +1,13 @@
|
||||
FROM golang:1.27.1 AS build
|
||||
ENV CGO_ENABLED=0
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
RUN go build -o /out/s3config ./cmd/s3config
|
||||
|
||||
FROM gcr.io/distroless/static-debian12:nonroot
|
||||
COPY --from=build /out/s3config /usr/local/bin/s3config
|
||||
ENTRYPOINT ["/usr/local/bin/s3config"]
|
||||
@@ -0,0 +1,13 @@
|
||||
FROM golang:1.27.1 AS build
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -tags=nodynamic -o /out/worker ./cmd/worker
|
||||
|
||||
FROM gcr.io/distroless/static-debian12:nonroot
|
||||
WORKDIR /app
|
||||
COPY --from=build /out/worker .
|
||||
ENTRYPOINT ["/app/worker"]
|
||||
@@ -28,3 +28,5 @@ test-persistence:
|
||||
test-failover:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
include student.mk
|
||||
|
||||
@@ -1,37 +1,146 @@
|
||||
# Отчёт — лабораторная 5
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Каменски Лин, БПИ2301
|
||||
- Целевая оценка; выбранный трек 5, если нужен: 5; трек 2
|
||||
- Commit/PR: [Pull Request 83](https://gitea.privcloud.tech/mtusi2026/mtusi2026lab5/pulls/83)
|
||||
- Использованные источники, библиотеки и помощь ИИ: материалы лабораторной; Go 1.27.1, chi v5, pgx v5, sqlc, goose, AWS SDK for Go v2, amqp091-go, imaging, gen2brain/webp, golang.org/x/image, PostgreSQL 18.6, Redis 8.8.3, Nginx 1.31.7, Docker Compose, SeaweedFS 4.51, RabbitMQ 4.2.9; Codex - для анализа реализации и проверок, оформления отчёта
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Требуются Go 1.26+ и Docker с Compose. Образы: `golang:1.27.1`, `postgres:18.6`, `redis:8.8.3-alpine3.23`, `nginx:1.31.7-alpine3.24`, `chrislusf/seaweedfs:4.51`, `rabbitmq:4.2.9-alpine`.
|
||||
|
||||
Создать локальные секреты - файлы в `secrets/`, в каждый записать значение без лишних символов:
|
||||
|
||||
- `database_url.txt` - URL PostgreSQL для API и worker;
|
||||
- `cache_url.txt` - URL Redis;
|
||||
- `postgres_password.txt` - пароль пользователя PostgreSQL `messenger`;
|
||||
- `test_database_url.txt` - URL БД для тестов с хоста;
|
||||
- `s3_access_key_id.txt` - S3 access key ID для API и worker;
|
||||
- `s3_secret_access_key.txt` - S3 secret access key для API и worker;
|
||||
- `s3_bootstrap_access_key_id.txt` - bootstrap access key ID;
|
||||
- `s3_bootstrap_secret_access_key.txt` - bootstrap secret access key;
|
||||
- `broker_url.txt` - AMQP URL для API и worker;
|
||||
- `broker_password.txt` - пароль пользователя RabbitMQ `messenger`.
|
||||
|
||||
Пример (значения заменить своими):
|
||||
|
||||
```bash
|
||||
mkdir -p secrets
|
||||
printf '%s' 'CHANGE_ME' > secrets/postgres_password.txt
|
||||
printf '%s' 'postgres://messenger:CHANGE_ME@postgres:5432/messenger?sslmode=disable' > secrets/database_url.txt
|
||||
printf '%s' 'redis://redis:6379/0' > secrets/cache_url.txt
|
||||
```
|
||||
|
||||
Скопировать шаблон переменных окружения:
|
||||
|
||||
```bash
|
||||
cp .env.example .env
|
||||
```
|
||||
|
||||
Сборка и запуск:
|
||||
|
||||
```bash
|
||||
docker compose up --build -d
|
||||
```
|
||||
|
||||
Готовность API проверяется запросом к `/health/ready`; после этого API доступен на http://localhost:8080.
|
||||
|
||||
Сваггер доступен на http://localhost:8080/swagger/.
|
||||
|
||||
Безопасная остановка (данные PostgreSQL, S3 и RabbitMQ в volumes сохраняются):
|
||||
```bash
|
||||
docker compose down
|
||||
```
|
||||
|
||||
Намеренное удаление учебных данных:
|
||||
```bash
|
||||
docker compose down -v
|
||||
```
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab4, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
**Процессы/хранилища:** клиент → nginx → `api` × `API_REPLICAS` (по умолчанию 2) → staging в SeaweedFS S3 + PostgreSQL → outbox publisher → RabbitMQ → `worker` → оригинал и варианты в S3 + результат в PostgreSQL. Redis используется как кэш сессий с fallback в PostgreSQL. Единственная публичная точка входа API - `127.0.0.1:8080`.
|
||||
|
||||
**Источник истины** - PostgreSQL: метаданные вложений, состояния заданий, попытки, lease (срок захвата задания обработчиком), версии pipeline и outbox. Байты оригиналов и вариантов хранятся в приватном S3 bucket `messenger-attachments`. Сообщения брокера содержат ID и служебные поля, без байтов изображений и credentials.
|
||||
|
||||
**Что перенесено из lab4:** Go-приложение и PostgreSQL, аутентификация, сессии, чаты, сообщения, идемпотентность и `X-Instance-Id`, Redis-кэш сессий, S3-хранилище (SeaweedFS), API вложений и `attachment_ids` в сообщениях, reconciler, `compose.yaml`, `deploy/nginx.conf`, `Dockerfile`, `Dockerfile.migrate`, `Dockerfile.s3config`, `Dockerfile.reconcile`, `scripts/restart.sh`, `scripts/stop-one.sh`, `student-tests`, `student.mk`, `Makefile`, `web/swagger`, `contracts`.
|
||||
|
||||
**Что изменено в lab5:** upload возвращает `202 queued` после сохранения оригинала в staging и транзакции с метаданными и outbox; добавлены RabbitMQ и отдельный worker, состояния `processing`/`ready`/`failed`, thumbnail JPEG, crop JPEG и optimized WebP; миграция pipeline с таблицами вариантов, outbox и audit replay; ограниченные повторы, lease recovery, команды просмотра failed, replay и upgrade версии pipeline. Обработка изображений выполняется в worker, API проверяет загружаемый файл и сохраняет источник.
|
||||
|
||||
**Транзакционные границы.** API сначала сохраняет проверенный оригинал в staging, затем в одной транзакции PostgreSQL создаёт вложение `queued` и запись outbox. Ответ `202` возвращается после COMMIT. Worker записывает оригинал и все варианты в S3, затем под блокировкой строки проверяет job ID, версию, попытку и срок захвата задания. Метаданные вариантов и переход в `ready` фиксируются одной транзакцией. Общей транзакции S3 и PostgreSQL нет: сбой между операциями может оставить объекты без опубликованного результата.
|
||||
|
||||
**Брокер и подтверждения.** RabbitMQ хранит durable очередь `attachments.process.v1`, сообщения публикуются как persistent. Publisher в API читает outbox и отмечает публикацию только после publisher confirm - подтверждения приёма брокером. При сбое запись остаётся для повторной публикации. Consumer ack подтверждает обработку: worker отправляет его после устойчивой фиксации результата, повтора или `failed`. Доставка at least once допускает дубли; повтор готового задания проверяет размеры и SHA-256 объектов и не создаёт новые варианты.
|
||||
|
||||
**Обработка изображений.** Оригинал сохраняется без изменения байтов. Thumbnail - JPEG с качеством 85, до 256×256 с сохранением пропорций, без увеличения; crop - JPEG с качеством 85, центральная обрезка 128×128 с допустимым увеличением. Optimized - WebP, до 1280×1280 с сохранением пропорций, без увеличения, Method 4, качество 80 для v1 и 75 для v2. Перед обработкой применяется EXIF orientation; EXIF/XMP/ICC в варианты не переносятся. Прозрачность в JPEG заменяется белым фоном, в WebP сохраняется.
|
||||
|
||||
**Ограничения обработки.** Размер загрузки - до 10 MiB, число пикселей - до 40 млн. Worker проверяет размер и SHA-256 источника, размеры изображения проверяются до полного декодирования. По умолчанию работают 2 consumer с prefetch 1, одновременно декодируется одно изображение. Декодирование, resize и кодирование выполняются в дочернем процессе, который завершается при истечении таймаута задания (по умолчанию 60 с). Лимиты контейнера worker - 1 CPU и 1 GiB RAM, общие для основного и дочернего процессов; отдельного лимита памяти дочернего процесса нет.
|
||||
|
||||
**Повторы и восстановление.** По умолчанию допускается 5 попыток. Для временных ошибок используется экспоненциальная задержка со случайным разбросом (backoff с jitter): 1-2 с, 2-4 с и далее до 32-64 с. Повреждённый или отсутствующий источник переводит задание в `failed` с `invalid_source`, исчерпание попыток - с `retry_exhausted`. Возврат в `queued` и outbox следующей попытки фиксируются одной транзакцией. Если worker завершился во время обработки, после истечения срока захвата задания фоновая проверка возвращает его на повтор либо фиксирует `failed`, если попытки закончились.
|
||||
|
||||
**Реестр failed и replay.** Неуспешные задания хранятся в PostgreSQL и доступны через `worker -list-failed`. Явный replay разрешён только для `failed` с сохранённым staging и без записанных вариантов. Перед повтором проверяются размер и SHA-256 источника; переход в `queued`, новый бюджет попыток, запись оператора и причины в audit и outbox фиксируются одной транзакцией. Job ID и версия сохраняются. Готовое задание повторно запустить через replay нельзя.
|
||||
|
||||
**Очистка staging.** Worker удаляет источник из staging после COMMIT состояния `ready`; ошибка удаления логируется и не отменяет готовый результат. Остатки staging для `ready`/`failed` и объекты без строки БД удаляет явный запуск reconciler, по умолчанию после 24 часов. Для связанных объектов срок считается от `updated_at`, для объектов без строки - от времени изменения в S3. Источники `queued`/`processing` сохраняются независимо от возраста. Периодической очистки staging нет; после удаления источника replay недоступен.
|
||||
|
||||
**Версии pipeline.** Новые загрузки используют v1. Команда `worker -upgrade UUID` переводит незавершённое вложение v1 (`queued`/`processing` с staging) на v2, сохраняя attachment ID, job ID и исходные байты. Смена версии и outbox фиксируются одной транзакцией. Варианты хранятся по ключам `variants/<attachment_id>/v<version>/<variant>`, уникальность в БД - `(attachment_id, pipeline_version, variant)`. Проверка версии, попытки и срока захвата не позволяет старому worker опубликовать результат вместо нового. Upgrade готового или failed-вложения не поддерживается; остаточные S3-объекты старой версии автоматически не очищаются.
|
||||
|
||||
**Переменные `.env.example`:** `APP_HOST`, `APP_PORT` - адрес и порт API; `MAX_INFLIGHT_REQUESTS` - ограничитель одновременных запросов (0 - выключен); `SESSION_TTL_SECONDS` - срок жизни сессии; `LOG_LEVEL` - уровень логов; `DATABASE_URL_FILE`, `CACHE_URL_FILE` - пути к secrets с URL PostgreSQL и Redis; `SESSION_STORE` - режим хранения сессий (`postgres` или `redis`); `API_REPLICAS` - число реплик API; `IDEMPOTENCY_TTL_SECONDS` - срок хранения ключей идемпотентности; `S3_ENDPOINT`, `S3_REGION`, `S3_BUCKET` - настройки S3; `S3_ACCESS_KEY_ID_FILE`, `S3_SECRET_ACCESS_KEY_FILE`, `S3_BOOTSTRAP_ACCESS_KEY_ID_FILE`, `S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE` - пути к S3 secrets; `MAX_UPLOAD_BYTES`, `MAX_IMAGE_PIXELS` - лимиты загрузки; `BROKER_URL_FILE` - путь к secret с AMQP URL; `BROKER_PASSWORD_FILE` - путь к secret с паролем RabbitMQ; `WORKER_CONCURRENCY` - число consumer; `JOB_MAX_ATTEMPTS` - лимит попыток; `JOB_TIMEOUT_SECONDS` - таймаут задания и срок его захвата; `STAGING_RETENTION_SECONDS` - срок хранения остатков staging перед очисткой reconciler.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
| Публичный smoke | `make test` | `PASS` | `evidence/make-test.txt` |
|
||||
| Уровень 3 |
|
||||
| JPEG, MIME, целостность, лимиты размера и пикселей, безопасный filename, проверка доступа к вложениям | `make contract-go` | `PASS` | `evidence/contract-go.txt` |
|
||||
| Сохранность задания после перезапуска API и broker; queued → ready; неизменный оригинал и JPEG thumbnail; отсутствие дублей вариантов при повторной доставке | `make worker-go`: `TestWorkerPersistenceAndDuplicate` | `PASS` | `evidence/worker-go.txt` |
|
||||
| Ограниченный retry, failed и безопасный error_code | `make worker-go`: `TestWorkerRetryExhausted` | `PASS` | `evidence/worker-go.txt` |
|
||||
| Уровень 4 |
|
||||
| Размеры thumbnail, сохранение пропорций, отсутствие увеличения, белый фон вместо прозрачности | `make contract-go`: `TestThumbnail` | `PASS` | `evidence/contract-go.txt` |
|
||||
| Центральная обрезка crop 128×128, включая маленькие изображения | `make contract-go`: `TestCrop` | `PASS` | `evidence/contract-go.txt` |
|
||||
| Optimized WebP, ограничение размеров, сохранение пропорций и прозрачности | `make contract-go`: `TestOptimized` | `PASS` | `evidence/contract-go.txt` |
|
||||
| Применение EXIF orientation и удаление EXIF из вариантов | `make contract-go`: `TestVariantsEXIF` | `PASS` | `evidence/contract-go.txt` |
|
||||
| Ограничение времени обработки, завершение дочернего процесса, работоспособность worker после таймаута | `make worker-go`: `TestWorkerProcessingTimeout` | `PASS` | `evidence/worker-go.txt` |
|
||||
| Восстановление задания после COMMIT и падения API до публикации в broker | `make worker-go`: `TestWorkerOutboxRecovery` | `PASS` | `evidence/worker-go.txt` |
|
||||
| Реестр failed, явный replay с audit, отказ повтора без источника и повторного replay готового задания | `make worker-go`: `TestWorkerReplay` | `PASS` | `evidence/worker-go.txt` |
|
||||
| Падение worker после записи объектов до COMMIT/ack, восстановление после истечения срока захвата задания | `make worker-go`: `TestWorkerLeaseRecovery` | `PASS` | `evidence/worker-go.txt` |
|
||||
| Уровень 5, трек 2 |
|
||||
| Позднее завершение v1 после готовности v2 не меняет текущий результат | `make pipeline-go`: `TestPipelineStaleProcessing` | `PASS` | `evidence/pipeline-go.txt` |
|
||||
| Обратный порядок доставки v2/v1 и дубль v2 не меняют результат и не создают новые варианты | `make pipeline-go`: `TestPipelineReorderAndDuplicate` | `PASS` | `evidence/pipeline-go.txt` |
|
||||
| Падение до первой записи результата в S3, восстановление того же задания после истечения срока захвата | `make pipeline-go`: `TestPipelineCrashBeforeWrite` | `PASS` | `evidence/pipeline-go.txt` |
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
### Трек 2: доказанная идемпотентность pipeline
|
||||
|
||||
**Гипотеза:** повторная доставка и изменение порядка заданий не меняют готовый результат. Старый worker не может заменить результат новой версии своим. После падения API или worker принятое задание восстанавливается и доходит до ready без повторной загрузки файла.
|
||||
|
||||
Инварианты: attachment ID, job ID и байты оригинала сохраняются; после готовности v2 старые задания и дубли не меняют метаданные и байты текущих вариантов; в БД остаётся один набор из трёх вариантов текущей версии; после падения до или после записи объектов то же задание доходит до ready.
|
||||
|
||||
Методика: `make pipeline-go` выполняет три последовательных сценария на свободном стенде с пустой очередью. Каждый создаёт через API синтетическое PNG 320×200 и переводит вложение на v2 штатной командой upgrade. Временный S3-прокси задерживает запись объекта в S3 для проверки позднего завершения v1 и падения до первой записи результата. Для перестановки доставки тест откладывает публикацию v1 через `available_at` в outbox, затем повторно публикует v2 сбросом `published_at`. Проверяются API, байты объектов, строки БД и освобождение очереди после ack. Падение после записи объектов проверяет `TestWorkerLeaseRecovery` из `make worker-go`: INSERT вариантов блокируется, worker завершается через SIGKILL до COMMIT/ack, затем ожидается естественное истечение срока захвата задания.
|
||||
|
||||
Стенд и лимиты описаны выше. Обычный worker использует 2 consumer с prefetch 1; временные worker в pipeline-тестах - 1 consumer, таймаут 120 с для задержанной v1 и 15 с для падения до записи. В тесте падения после записи таймаут также 15 с. Приведён один сохранённый прогон каждого сценария, без нагрузочного профиля и отдельного прогрева; RPS, перцентили задержек и фактическое потребление CPU/RAM не измерялись.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| Задержка записи optimized WebP версии v1 в S3 до готовности v2 | Старый worker не заменит результат v2 | v2 осталась ready, метаданные и байты не изменились; 0 строк вариантов v1 и 3 строки v2; старое задание подтверждено | Не измерялось / потери нет |
|
||||
| Доставка v1 после v2, затем повтор v2 | Порядок и дубли не повлияют на готовый результат | Job ID, версия, попытка, метаданные и все 4 объекта не изменились; новых строк вариантов и outbox нет | Не измерялось / потери нет |
|
||||
| SIGKILL worker до записи первого объекта результата в S3 | То же задание завершится после восстановления | После истечения срока захвата получен ready v2 на попытке 2, сохранены job ID и оригинал, доступны 3 варианта | Не измерялось / потери нет |
|
||||
| SIGKILL worker после записи объектов в S3 до COMMIT/ack | Повтор завершит то же задание без дублей | После истечения срока захвата получен ready на попытке 2, сохранены job ID и оригинал, в БД 3 варианта | Не измерялось / потери нет |
|
||||
| SIGKILL API после COMMIT до публикации в broker | Outbox восстановит публикацию без создания нового задания | То же задание автоматически опубликовано и завершено на попытке 1, оригинал сохранён, доступны 3 варианта | Не измерялось / потери нет |
|
||||
|
||||
Результаты: все сценарии прошли. Длительности pipeline-тестов - 12,67 / 41,43 / 26,05 с, всего 80,150 с (`evidence/pipeline-go.txt`). Длительности теста падения после записи объектов в S3 - 26,54 с, восстановления outbox - 33,15 с (`evidence/worker-go.txt`). Это полное время тестов с подготовкой и очисткой, время восстановления отдельно не измерялось.
|
||||
|
||||
**Вывод:** гипотеза подтверждена в проверенных сценариях. Принятые задания завершились после восстановления, дубли и старая версия не изменили текущий результат. Доставка остаётся at least once: падение после публикации до фиксации outbox или после COMMIT результата до ack допускает повторную доставку. Идемпотентность результата не означает exactly-once доставку или общую транзакцию S3 и PostgreSQL.
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
- Nginx, PostgreSQL и SeaweedFS работают в одном экземпляре на одном хосте. Реплики API не защищают от отказа этих компонентов или самого хоста.
|
||||
- Если удалить сессию из Redis не удалось, logout возвращает `503 unavailable`: сессия уже удалена из PostgreSQL, но старый токен может приниматься из кэша до 60 с. Несмотря на `503`, может потребоваться повторный вход. Пока Redis недоступен, получить `204` нельзя, хотя сессия в БД уже удалена; после восстановления повторный logout вернёт `401`.
|
||||
- Полное декодирование требует памяти под распакованное изображение. Для ограничения расхода RAM на каждой API-реплике работает один декодер, поэтому параллельные загрузки ожидают своей очереди.
|
||||
- Общей транзакции PostgreSQL и S3 нет: расхождения исправляются последующей сверкой. Reconciler отмечает отсутствие оригинала, но не восстанавливает байты. Сохранность volumes не заменяет резервные копии хранилищ.
|
||||
- Reconciler загружает списки объектов и метаданных целиком в память, поэтому расход памяти растёт с объёмом хранилища.
|
||||
- RabbitMQ также работает в одном экземпляре. Проверены перезапуск API и broker с сохранением volumes, падение worker до и после записи объектов, повторная доставка и устаревшая версия задания. Потеря volumes, отказ хоста и одновременные отказы нескольких хранилищ не проверялись.
|
||||
- При удалении staging сбой COMMIT после удаления объекта может оставить старый ключ в БД. Reconciler не сверяет варианты изображений; остатки объектов старой версии автоматически не удаляются.
|
||||
- Replay доступен только для failed с сохранённым staging и без записанных вариантов. Upgrade поддерживает только незавершённую v1; повторная обработка готовых вложений не реализована.
|
||||
- Очистка остатков staging требует явного запуска reconciler. Источники queued/processing защищены от удаления независимо от возраста.
|
||||
- Память ограничена на уровне контейнера worker, отдельного лимита дочернего процесса нет. Поведение при исчерпании памяти и длительной перегрузке не исследовалось. Проверки идемпотентности выполнены на небольших PNG 320×200, без измерения пропускной способности и времени восстановления.
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"mtusi2026-messenger/internal/config"
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
"mtusi2026-messenger/internal/reconcile"
|
||||
s3store "mtusi2026-messenger/internal/s3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if err := run(); err != nil {
|
||||
fmt.Fprintln(os.Stderr, "reconcile:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func run() error {
|
||||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
dryRun := flag.Bool("dry-run", true, "report findings without changing PostgreSQL or S3")
|
||||
grace := flag.Duration("grace", reconcile.DefaultGrace, "minimum age before an object or row may be repaired")
|
||||
maxDeletes := flag.Int("max-deletes", reconcile.DefaultMaxDeletes, "maximum destructive repairs per run")
|
||||
asJSON := flag.Bool("json", false, "print the report as JSON")
|
||||
flag.Parse()
|
||||
|
||||
cfg := config.Load()
|
||||
log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: cfg.Logging.Level}))
|
||||
|
||||
databaseURL, err := cfg.Database.URL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
pool, err := postgres.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
s3Client, err := s3store.New(ctx, cfg.S3, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reconciler := reconcile.New(pool, s3Client, db.New(pool), log)
|
||||
|
||||
report, err := reconciler.Run(ctx, reconcile.Options{
|
||||
DryRun: *dryRun,
|
||||
Grace: *grace,
|
||||
MaxDeletes: *maxDeletes,
|
||||
StagingRetention: cfg.S3.StagingRetention,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if *asJSON {
|
||||
encoder := json.NewEncoder(os.Stdout)
|
||||
encoder.SetIndent("", " ")
|
||||
if err := encoder.Encode(report); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
printReport(report)
|
||||
}
|
||||
|
||||
if len(report.Errors) > 0 {
|
||||
return fmt.Errorf("completed with %d errors", len(report.Errors))
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func printReport(report reconcile.Report) {
|
||||
fmt.Printf("dry_run=%t duration_ms=%d\n", report.DryRun, report.DurationMS)
|
||||
fmt.Printf(
|
||||
"scanned objects=%d rows=%d list_pages=%d\n",
|
||||
report.ScannedObjects,
|
||||
report.ScannedRows,
|
||||
report.ListPages,
|
||||
)
|
||||
fmt.Printf(
|
||||
"found staging_objects=%d orphan_objects=%d missing_attachments=%d corrupt_objects=%d\n",
|
||||
len(report.StagingObjects),
|
||||
len(report.OrphanObjects),
|
||||
len(report.MissingAttachments),
|
||||
len(report.CorruptObjects),
|
||||
)
|
||||
fmt.Printf(
|
||||
"skipped fresh=%d inactive=%d\n",
|
||||
report.SkippedFresh,
|
||||
report.SkippedInactive,
|
||||
)
|
||||
fmt.Printf(
|
||||
"changes deleted_objects=%d marked_failed=%d\n",
|
||||
report.DeletedObjects,
|
||||
report.MarkedFailed,
|
||||
)
|
||||
|
||||
for _, key := range report.StagingObjects {
|
||||
fmt.Printf("staging_object %s\n", key)
|
||||
}
|
||||
for _, key := range report.OrphanObjects {
|
||||
fmt.Printf("orphan_object %s\n", key)
|
||||
}
|
||||
for _, key := range report.CorruptObjects {
|
||||
fmt.Printf("corrupt_object %s\n", key)
|
||||
}
|
||||
for _, id := range report.MissingAttachments {
|
||||
fmt.Printf("missing_attachment %s\n", id)
|
||||
}
|
||||
for _, message := range report.Errors {
|
||||
fmt.Printf("error %s\n", message)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"mtusi2026-messenger/internal/config"
|
||||
"mtusi2026-messenger/internal/s3"
|
||||
)
|
||||
|
||||
func main() {
|
||||
cfg := config.LoadS3()
|
||||
|
||||
err := s3.WriteConfig("/config/s3.json", cfg)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "s3config:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"mtusi2026-messenger/internal/app"
|
||||
)
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && os.Args[1] == "healthcheck" {
|
||||
os.Exit(app.Healthcheck())
|
||||
}
|
||||
|
||||
if err := app.Run(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,221 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
"mtusi2026-messenger/internal/config"
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/logger"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
s3store "mtusi2026-messenger/internal/s3"
|
||||
"mtusi2026-messenger/internal/worker"
|
||||
)
|
||||
|
||||
var (
|
||||
errConflictingReplayFlags = errors.New("replay and list-failed cannot be combined")
|
||||
errInvalidReplayID = errors.New("replay must be an attachment UUID")
|
||||
errMissingReplayAudit = errors.New("replay requires nonempty operator and reason")
|
||||
errConflictingUpgradeFlags = errors.New("upgrade cannot be combined with replay or list-failed")
|
||||
errInvalidUpgradeID = errors.New("upgrade must be an attachment UUID")
|
||||
)
|
||||
|
||||
func main() {
|
||||
maxPixels := flag.Int("process-image", 0, "process an image from stdin with the given pixel limit")
|
||||
pipelineVersion := flag.Int("pipeline-version", 1, "pipeline version for process-image")
|
||||
replay := flag.String("replay", "", "replay a failed attachment by UUID; requires retained staging and no existing variants")
|
||||
operator := flag.String("operator", "", "operator requesting replay")
|
||||
reason := flag.String("reason", "", "reason for replay")
|
||||
listFailed := flag.Bool("list-failed", false, "list up to 100 failed attachments")
|
||||
upgrade := flag.String("upgrade", "", "upgrade a v1 queued or processing attachment to v2; requires retained staging")
|
||||
flag.Parse()
|
||||
|
||||
if *maxPixels > 0 {
|
||||
if err := worker.ProcessImage(*maxPixels, *pipelineVersion); err != nil {
|
||||
if errors.Is(err, worker.ErrInvalidSource) {
|
||||
os.Exit(2)
|
||||
}
|
||||
|
||||
fmt.Fprintln(os.Stderr, "process image:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGINT, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
var err error
|
||||
switch {
|
||||
case *upgrade != "" && (*listFailed || *replay != ""):
|
||||
err = errConflictingUpgradeFlags
|
||||
case *listFailed && *replay != "":
|
||||
err = errConflictingReplayFlags
|
||||
case *listFailed:
|
||||
err = runListFailed(ctx)
|
||||
case *replay != "":
|
||||
err = runReplay(ctx, *replay, *operator, *reason)
|
||||
case *upgrade != "":
|
||||
err = runUpgrade(ctx, *upgrade)
|
||||
default:
|
||||
err = runWorker(ctx)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "worker:", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func runListFailed(ctx context.Context) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, pool, err := loadDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
rows, err := db.New(pool).ListFailedAttachments(ctx, 100)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, row := range rows {
|
||||
code := "-"
|
||||
if row.ErrorCode != nil {
|
||||
code = *row.ErrorCode
|
||||
}
|
||||
|
||||
fmt.Printf("%s attempts=%d error_code=%s\n", row.ID, row.Attempts, code)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func runReplay(ctx context.Context, replay, operator, reason string) error {
|
||||
replayID, err := uuid.Parse(replay)
|
||||
if err != nil {
|
||||
return errInvalidReplayID
|
||||
}
|
||||
|
||||
operator, reason = strings.TrimSpace(operator), strings.TrimSpace(reason)
|
||||
if operator == "" || reason == "" {
|
||||
return errMissingReplayAudit
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cfg, pool, err := loadDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
log := logger.New(cfg.Logging.Level)
|
||||
storage, err := s3store.NewAPI(ctx, cfg.S3, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := worker.Replay(ctx, pool, storage, replayID, operator, reason); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("attachment replay queued", "attachment_id", replayID, "operator", operator)
|
||||
return nil
|
||||
}
|
||||
|
||||
func runUpgrade(ctx context.Context, upgrade string) error {
|
||||
id, err := uuid.Parse(upgrade)
|
||||
if err != nil {
|
||||
return errInvalidUpgradeID
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
defer cancel()
|
||||
|
||||
cfg, pool, err := loadDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
log := logger.New(cfg.Logging.Level)
|
||||
storage, err := s3store.NewAPI(ctx, cfg.S3, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := worker.Upgrade(ctx, pool, storage, id); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("attachment pipeline upgrade queued", "attachment_id", id, "pipeline_version", 2)
|
||||
return nil
|
||||
}
|
||||
|
||||
func runWorker(ctx context.Context) error {
|
||||
cfg, pool, err := loadDatabase(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
brokerURL, err := cfg.Broker.URL()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
log := logger.New(cfg.Logging.Level)
|
||||
storage, err := s3store.NewAPI(ctx, cfg.S3, log)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
w := worker.New(
|
||||
pool,
|
||||
storage,
|
||||
brokerURL,
|
||||
config.LoadWorker(),
|
||||
cfg.S3.MaxUploadBytes,
|
||||
cfg.S3.MaxImagePixels,
|
||||
log,
|
||||
)
|
||||
w.Run(ctx)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func loadDatabase(ctx context.Context) (config.Config, *pgxpool.Pool, error) {
|
||||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
|
||||
cfg := config.Load()
|
||||
databaseURL, err := cfg.Database.URL()
|
||||
if err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
|
||||
pool, err := postgres.New(ctx, databaseURL)
|
||||
if err != nil {
|
||||
return config.Config{}, nil, err
|
||||
}
|
||||
|
||||
return cfg, pool, nil
|
||||
}
|
||||
+258
@@ -0,0 +1,258 @@
|
||||
services:
|
||||
api:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile
|
||||
image: mtusi-messenger:lab5
|
||||
env_file:
|
||||
- .env
|
||||
secrets:
|
||||
- database_url
|
||||
- cache_url
|
||||
- broker_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
- s3_bootstrap_access_key_id
|
||||
- s3_bootstrap_secret_access_key
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
resources:
|
||||
limits:
|
||||
cpus: "${API_CPUS:-0.5}"
|
||||
memory: ${API_MEM:-512M}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
|
||||
worker:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.worker
|
||||
image: mtusi-messenger-worker:lab5
|
||||
env_file:
|
||||
- .env
|
||||
secrets:
|
||||
- database_url
|
||||
- broker_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 1G
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3:
|
||||
condition: service_healthy
|
||||
rabbitmq:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
|
||||
postgres:
|
||||
image: postgres:18.6
|
||||
ports:
|
||||
- "127.0.0.1:5432:5432"
|
||||
environment:
|
||||
POSTGRES_USER: messenger
|
||||
POSTGRES_DB: messenger
|
||||
POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password
|
||||
secrets:
|
||||
- postgres_password
|
||||
volumes:
|
||||
- pgdata:/var/lib/postgresql
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -h 127.0.0.1 -U $${POSTGRES_USER} -d $${POSTGRES_DB}"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "1.0"
|
||||
memory: 512M
|
||||
restart: unless-stopped
|
||||
|
||||
migrate:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.migrate
|
||||
secrets:
|
||||
- database_url
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 128M
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
restart: "no"
|
||||
|
||||
redis:
|
||||
image: redis:8.8.3-alpine3.23
|
||||
command: ["redis-server", "--save", "", "--appendonly", "no"]
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 10
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 128M
|
||||
restart: unless-stopped
|
||||
|
||||
rabbitmq:
|
||||
image: rabbitmq:4.2.9-alpine
|
||||
hostname: rabbitmq
|
||||
environment:
|
||||
RABBITMQ_DEFAULT_USER: messenger
|
||||
BROKER_PASSWORD_FILE: ${BROKER_PASSWORD_FILE:-/run/secrets/broker_password}
|
||||
entrypoint: ["/bin/sh", "-ec"]
|
||||
command:
|
||||
- |
|
||||
RABBITMQ_DEFAULT_PASS="$$(cat "$$BROKER_PASSWORD_FILE")"
|
||||
export RABBITMQ_DEFAULT_PASS
|
||||
exec docker-entrypoint.sh rabbitmq-server
|
||||
secrets:
|
||||
- broker_password
|
||||
volumes:
|
||||
- rabbitmq:/var/lib/rabbitmq
|
||||
healthcheck:
|
||||
test: ["CMD", "rabbitmq-diagnostics", "-q", "check_running"]
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 10
|
||||
start_period: 30s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 512M
|
||||
restart: unless-stopped
|
||||
|
||||
s3-config-init:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.s3config
|
||||
image: mtusi-messenger-s3config:lab4
|
||||
entrypoint: ["/usr/local/bin/s3config"]
|
||||
user: "0:0"
|
||||
env_file:
|
||||
- .env
|
||||
secrets:
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
- s3_bootstrap_access_key_id
|
||||
- s3_bootstrap_secret_access_key
|
||||
volumes:
|
||||
- s3config:/config
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 64M
|
||||
restart: "no"
|
||||
|
||||
s3:
|
||||
image: chrislusf/seaweedfs:4.51
|
||||
command:
|
||||
- "server"
|
||||
- "-dir=/data"
|
||||
- "-volume.max=10"
|
||||
- "-s3"
|
||||
- "-s3.port=9000"
|
||||
- "-s3.iam=false"
|
||||
- "-filer.disableHttp"
|
||||
- "-s3.port.iceberg=0"
|
||||
- "-s3.port.lance=0"
|
||||
- "-s3.config=/config/s3.json"
|
||||
depends_on:
|
||||
s3-config-init:
|
||||
condition: service_completed_successfully
|
||||
volumes:
|
||||
- s3config:/config:ro
|
||||
- seaweedfs:/data
|
||||
healthcheck:
|
||||
test:
|
||||
- CMD-SHELL
|
||||
- wget -qO- http://127.0.0.1:9000/healthz >/dev/null && wget -qO- http://127.0.0.1:9333/dir/status | grep -q '"Url":'
|
||||
interval: 10s
|
||||
timeout: 5s
|
||||
retries: 5
|
||||
start_period: 20s
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.5"
|
||||
memory: 512M
|
||||
restart: unless-stopped
|
||||
|
||||
load_balancer:
|
||||
image: nginx:1.31.7-alpine3.24
|
||||
ports:
|
||||
- "127.0.0.1:8080:8080"
|
||||
volumes:
|
||||
- ./deploy/nginx.conf:/etc/nginx/nginx.conf:ro
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: ${LB_CPUS:-0.5}
|
||||
memory: 128M
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
restart: unless-stopped
|
||||
|
||||
reconcile:
|
||||
build:
|
||||
context: .
|
||||
dockerfile: Dockerfile.reconcile
|
||||
image: mtusi-messenger-reconcile:lab4
|
||||
profiles: ["tools"]
|
||||
env_file:
|
||||
- .env
|
||||
secrets:
|
||||
- database_url
|
||||
- s3_access_key_id
|
||||
- s3_secret_access_key
|
||||
- s3_bootstrap_access_key_id
|
||||
- s3_bootstrap_secret_access_key
|
||||
deploy:
|
||||
resources:
|
||||
limits:
|
||||
cpus: "0.25"
|
||||
memory: 64M
|
||||
restart: "no"
|
||||
|
||||
secrets:
|
||||
postgres_password:
|
||||
file: ./secrets/postgres_password.txt
|
||||
database_url:
|
||||
file: ./secrets/database_url.txt
|
||||
cache_url:
|
||||
file: ./secrets/cache_url.txt
|
||||
broker_password:
|
||||
file: ./secrets/broker_password.txt
|
||||
broker_url:
|
||||
file: ./secrets/broker_url.txt
|
||||
s3_access_key_id:
|
||||
file: ./secrets/s3_access_key_id.txt
|
||||
s3_secret_access_key:
|
||||
file: ./secrets/s3_secret_access_key.txt
|
||||
s3_bootstrap_access_key_id:
|
||||
file: ./secrets/s3_bootstrap_access_key_id.txt
|
||||
s3_bootstrap_secret_access_key:
|
||||
file: ./secrets/s3_bootstrap_secret_access_key.txt
|
||||
|
||||
volumes:
|
||||
pgdata:
|
||||
seaweedfs:
|
||||
s3config:
|
||||
rabbitmq:
|
||||
@@ -0,0 +1,51 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE users (
|
||||
id uuid PRIMARY KEY,
|
||||
username text NOT NULL UNIQUE
|
||||
CONSTRAINT users_username_format CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL
|
||||
CONSTRAINT users_display_name_not_blank CHECK (display_name ~ '\S'),
|
||||
password_hash text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE sessions (
|
||||
token_hash text PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_sessions_expires_at ON sessions (expires_at);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL
|
||||
CONSTRAINT chats_title_not_blank CHECK (title ~ '\S'),
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE TABLE chat_members (
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
PRIMARY KEY (chat_id, user_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_chat_members_user_id ON chat_members (user_id);
|
||||
|
||||
CREATE TABLE messages (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
sender_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT,
|
||||
text text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_messages_chat_history ON messages (chat_id, created_at, id);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE messages;
|
||||
DROP TABLE chat_members;
|
||||
DROP TABLE chats;
|
||||
DROP TABLE sessions;
|
||||
DROP TABLE users;
|
||||
@@ -0,0 +1,17 @@
|
||||
-- +goose Up
|
||||
CREATE TABLE idempotency_keys (
|
||||
user_id uuid NOT NULL REFERENCES users (id) ON DELETE CASCADE,
|
||||
method text NOT NULL,
|
||||
path text NOT NULL,
|
||||
key text NOT NULL,
|
||||
request_hash text NOT NULL,
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE
|
||||
DEFERRABLE INITIALLY DEFERRED,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (user_id, method, path, key)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_idempotency_keys_created_at ON idempotency_keys (created_at);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE idempotency_keys;
|
||||
@@ -0,0 +1,38 @@
|
||||
-- +goose Up
|
||||
CREATE TYPE attachment_content_type AS ENUM ('image/png', 'image/jpeg');
|
||||
CREATE TYPE attachment_status AS ENUM ('queued', 'processing', 'ready', 'failed');
|
||||
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats (id) ON DELETE CASCADE,
|
||||
owner_id uuid NOT NULL REFERENCES users (id) ON DELETE RESTRICT,
|
||||
s3_key text NOT NULL UNIQUE,
|
||||
filename text NOT NULL,
|
||||
content_type attachment_content_type NOT NULL,
|
||||
size_bytes int NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
status attachment_status NOT NULL DEFAULT 'queued',
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
updated_at timestamptz NOT NULL DEFAULT now()
|
||||
);
|
||||
|
||||
CREATE INDEX idx_attachments_chat_id ON attachments (chat_id, created_at);
|
||||
CREATE INDEX idx_attachments_owner_id ON attachments (owner_id);
|
||||
CREATE INDEX idx_attachments_queued_created_at ON attachments (created_at) WHERE status = 'queued';
|
||||
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages (id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE RESTRICT,
|
||||
position int NOT NULL CHECK (position >= 0 AND position < 10),
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
UNIQUE (message_id, position)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_message_attachments_attachment_id ON message_attachments (attachment_id);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE message_attachments;
|
||||
DROP TABLE attachments;
|
||||
DROP TYPE attachment_status;
|
||||
DROP TYPE attachment_content_type;
|
||||
@@ -0,0 +1,67 @@
|
||||
-- +goose Up
|
||||
CREATE TYPE attachment_variant_name AS ENUM ('thumbnail', 'crop', 'optimized');
|
||||
|
||||
ALTER TABLE attachments
|
||||
ADD COLUMN staging_key text UNIQUE,
|
||||
ADD COLUMN job_id uuid UNIQUE,
|
||||
ADD COLUMN pipeline_version int NOT NULL DEFAULT 1 CHECK (pipeline_version > 0),
|
||||
ADD COLUMN attempts int NOT NULL DEFAULT 0 CHECK (attempts >= 0),
|
||||
ADD COLUMN retry_base int NOT NULL DEFAULT 0 CHECK (retry_base >= 0 AND retry_base <= attempts),
|
||||
ADD COLUMN width int CHECK (width > 0),
|
||||
ADD COLUMN height int CHECK (height > 0),
|
||||
ADD COLUMN lease_until timestamptz;
|
||||
|
||||
CREATE INDEX idx_attachments_expired_lease ON attachments (lease_until) WHERE status = 'processing';
|
||||
|
||||
CREATE TABLE attachment_variants (
|
||||
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
||||
pipeline_version int NOT NULL CHECK (pipeline_version > 0),
|
||||
variant attachment_variant_name NOT NULL,
|
||||
s3_key text NOT NULL UNIQUE,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg', 'image/webp')),
|
||||
size_bytes int NOT NULL CHECK (size_bytes > 0),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
width int NOT NULL CHECK (width > 0),
|
||||
height int NOT NULL CHECK (height > 0),
|
||||
PRIMARY KEY (attachment_id, pipeline_version, variant)
|
||||
);
|
||||
|
||||
CREATE TABLE outbox (
|
||||
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
||||
job_id uuid NOT NULL REFERENCES attachments (job_id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
||||
pipeline_version int NOT NULL CHECK (pipeline_version > 0),
|
||||
attempt int NOT NULL CHECK (attempt > 0),
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
available_at timestamptz NOT NULL DEFAULT now(),
|
||||
published_at timestamptz,
|
||||
UNIQUE (job_id, pipeline_version, attempt)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_outbox_pending ON outbox (available_at, id) WHERE published_at IS NULL;
|
||||
|
||||
CREATE TABLE attachment_replays (
|
||||
attachment_id uuid NOT NULL REFERENCES attachments (id) ON DELETE CASCADE,
|
||||
attempt int NOT NULL CHECK (attempt > 0),
|
||||
previous_error_code text,
|
||||
operator text NOT NULL,
|
||||
reason text NOT NULL,
|
||||
created_at timestamptz NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (attachment_id, attempt)
|
||||
);
|
||||
|
||||
-- +goose Down
|
||||
DROP TABLE attachment_replays;
|
||||
DROP TABLE outbox;
|
||||
DROP TABLE attachment_variants;
|
||||
DROP TYPE attachment_variant_name;
|
||||
DROP INDEX idx_attachments_expired_lease;
|
||||
ALTER TABLE attachments
|
||||
DROP COLUMN height,
|
||||
DROP COLUMN width,
|
||||
DROP COLUMN lease_until,
|
||||
DROP COLUMN retry_base,
|
||||
DROP COLUMN attempts,
|
||||
DROP COLUMN pipeline_version,
|
||||
DROP COLUMN job_id,
|
||||
DROP COLUMN staging_key;
|
||||
@@ -0,0 +1,14 @@
|
||||
-- name: CreateAttachmentReplay :exec
|
||||
INSERT INTO attachment_replays (
|
||||
attachment_id,
|
||||
attempt,
|
||||
previous_error_code,
|
||||
operator,
|
||||
reason
|
||||
) VALUES (
|
||||
sqlc.arg(attachment_id),
|
||||
sqlc.arg(attempt),
|
||||
sqlc.narg(previous_error_code),
|
||||
sqlc.arg(operator),
|
||||
sqlc.arg(reason)
|
||||
);
|
||||
@@ -0,0 +1,28 @@
|
||||
-- name: CreateAttachmentVariant :exec
|
||||
INSERT INTO attachment_variants (
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
variant,
|
||||
s3_key,
|
||||
content_type,
|
||||
size_bytes,
|
||||
width,
|
||||
height,
|
||||
sha256
|
||||
) VALUES (
|
||||
sqlc.arg(attachment_id),
|
||||
sqlc.arg(pipeline_version),
|
||||
sqlc.arg(variant),
|
||||
sqlc.arg(s3_key),
|
||||
sqlc.arg(content_type),
|
||||
sqlc.arg(size_bytes),
|
||||
sqlc.arg(width),
|
||||
sqlc.arg(height),
|
||||
sqlc.arg(sha256)
|
||||
);
|
||||
|
||||
-- name: ListAttachmentVariants :many
|
||||
SELECT * FROM attachment_variants
|
||||
WHERE attachment_id = sqlc.arg(attachment_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
ORDER BY variant;
|
||||
@@ -0,0 +1,227 @@
|
||||
-- name: CreateAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id,
|
||||
chat_id,
|
||||
owner_id,
|
||||
s3_key,
|
||||
staging_key,
|
||||
job_id,
|
||||
filename,
|
||||
content_type,
|
||||
size_bytes,
|
||||
sha256,
|
||||
width,
|
||||
height,
|
||||
error_code
|
||||
)
|
||||
SELECT
|
||||
sqlc.arg(id)::uuid,
|
||||
sqlc.arg(chat_id)::uuid,
|
||||
sqlc.arg(owner_id)::uuid,
|
||||
sqlc.arg(s3_key)::text,
|
||||
sqlc.arg(staging_key)::text,
|
||||
sqlc.arg(job_id)::uuid,
|
||||
sqlc.arg(filename)::text,
|
||||
sqlc.arg(content_type)::attachment_content_type,
|
||||
sqlc.arg(size_bytes)::int,
|
||||
sqlc.arg(sha256)::text,
|
||||
sqlc.arg(width)::int,
|
||||
sqlc.arg(height)::int,
|
||||
NULLIF(sqlc.arg(error_code)::text, '')
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND user_id = sqlc.arg(owner_id)
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: CreateMessageAttachment :exec
|
||||
INSERT INTO message_attachments (
|
||||
message_id,
|
||||
attachment_id,
|
||||
position
|
||||
) VALUES (
|
||||
sqlc.arg(message_id),
|
||||
sqlc.arg(attachment_id),
|
||||
sqlc.arg(position)
|
||||
);
|
||||
|
||||
-- name: GetAttachmentByID :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = $1::uuid;
|
||||
|
||||
-- name: LockAttachmentByStagingKey :one
|
||||
SELECT * FROM attachments
|
||||
WHERE staging_key = sqlc.arg(staging_key)::text
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: ListAttachmentsByIDs :many
|
||||
SELECT status
|
||||
FROM attachments
|
||||
WHERE chat_id = sqlc.arg(chat_id)::uuid
|
||||
AND id = ANY(sqlc.arg(ids)::uuid[]);
|
||||
|
||||
-- name: ListMessageAttachmentIDs :many
|
||||
SELECT attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = sqlc.arg(message_id)::uuid
|
||||
ORDER BY position;
|
||||
|
||||
-- name: ListMessageAttachmentsByMessageIDs :many
|
||||
SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = ANY(sqlc.arg(message_ids)::uuid[])
|
||||
ORDER BY message_id, position;
|
||||
|
||||
-- name: ListAttachmentsForReconcile :many
|
||||
SELECT id, s3_key, staging_key, status, size_bytes, updated_at
|
||||
FROM attachments;
|
||||
|
||||
-- name: ListFailedAttachments :many
|
||||
SELECT id, job_id, error_code, attempts, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'failed'
|
||||
ORDER BY updated_at, id
|
||||
LIMIT sqlc.arg(batch_size)::int;
|
||||
|
||||
-- name: LockAttachmentJob :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND job_id = sqlc.arg(job_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now()
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: LockExpiredAttachmentJobs :many
|
||||
SELECT * FROM attachments
|
||||
WHERE status = 'processing'
|
||||
AND lease_until <= now()
|
||||
ORDER BY lease_until, id
|
||||
LIMIT sqlc.arg(batch_size)::int
|
||||
FOR UPDATE SKIP LOCKED;
|
||||
|
||||
-- name: LockFailedAttachmentForReplay :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'failed'
|
||||
AND staging_key IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_variants
|
||||
WHERE attachment_id = attachments.id
|
||||
)
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: LockAttachmentForUpgrade :one
|
||||
SELECT * FROM attachments
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND pipeline_version = 1
|
||||
AND status IN ('queued', 'processing')
|
||||
AND staging_key IS NOT NULL
|
||||
FOR UPDATE;
|
||||
|
||||
-- name: ClearAttachmentStagingKey :exec
|
||||
UPDATE attachments
|
||||
SET staging_key = NULL
|
||||
WHERE id = sqlc.arg(id)::uuid;
|
||||
|
||||
-- name: MarkAttachmentFailedIfReady :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid AND status = 'ready';
|
||||
|
||||
-- name: ClaimAttachmentJob :one
|
||||
UPDATE attachments
|
||||
SET status = 'processing',
|
||||
attempts = attempts + 1,
|
||||
lease_until = sqlc.arg(lease_until)::timestamptz,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND job_id = sqlc.arg(job_id)::uuid
|
||||
AND pipeline_version = sqlc.arg(pipeline_version)::int
|
||||
AND status = 'queued'
|
||||
AND attempts + 1 = sqlc.arg(attempt)::int
|
||||
RETURNING *;
|
||||
|
||||
-- name: CompleteAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: RetryAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: UpgradeAttachmentPipeline :execrows
|
||||
UPDATE attachments
|
||||
SET pipeline_version = 2,
|
||||
status = 'queued',
|
||||
retry_base = attempts,
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND pipeline_version = 1
|
||||
AND status IN ('queued', 'processing')
|
||||
AND staging_key IS NOT NULL;
|
||||
|
||||
-- name: ReplayAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
retry_base = attempts,
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'failed'
|
||||
AND staging_key IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_variants
|
||||
WHERE attachment_id = attachments.id
|
||||
);
|
||||
|
||||
-- name: FailAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = sqlc.arg(error_code)::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = sqlc.arg(attempt)::int
|
||||
AND lease_until > now();
|
||||
|
||||
-- name: RecoverExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now();
|
||||
|
||||
-- name: FailExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = 'retry_exhausted',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = sqlc.arg(id)::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now();
|
||||
@@ -0,0 +1,73 @@
|
||||
-- name: CreateChat :one
|
||||
INSERT INTO chats (
|
||||
id,
|
||||
title
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(title)
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: AddChatMembers :exec
|
||||
INSERT INTO chat_members (
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
SELECT
|
||||
sqlc.arg(chat_id)::uuid,
|
||||
unnest(sqlc.arg(user_ids)::uuid[]);
|
||||
|
||||
-- name: IsChatMember :one
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND user_id = sqlc.arg(user_id)
|
||||
);
|
||||
|
||||
-- name: GetChat :one
|
||||
SELECT
|
||||
c.*,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
WHERE c.id = sqlc.arg(id)
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM chat_members m
|
||||
WHERE m.chat_id = c.id
|
||||
AND m.user_id = sqlc.arg(user_id)
|
||||
);
|
||||
|
||||
-- name: ListChatsFirstPage :many
|
||||
SELECT
|
||||
c.*,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = sqlc.arg(user_id)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT sqlc.arg(lim);
|
||||
|
||||
-- name: ListChatsAfter :many
|
||||
SELECT
|
||||
c.*,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = sqlc.arg(user_id)
|
||||
AND (c.created_at, c.id) > (
|
||||
sqlc.arg(after_created_at)::timestamptz,
|
||||
sqlc.arg(after_id)::uuid
|
||||
)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT sqlc.arg(lim);
|
||||
@@ -0,0 +1,36 @@
|
||||
-- name: CreateIdempotencyKey :execrows
|
||||
INSERT INTO idempotency_keys (
|
||||
user_id,
|
||||
method,
|
||||
path,
|
||||
key,
|
||||
request_hash,
|
||||
message_id
|
||||
) VALUES (
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(method),
|
||||
sqlc.arg(path),
|
||||
sqlc.arg(key),
|
||||
sqlc.arg(request_hash),
|
||||
sqlc.arg(message_id)
|
||||
)
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- name: GetIdempotentMessage :one
|
||||
SELECT
|
||||
k.request_hash,
|
||||
m.id,
|
||||
m.chat_id,
|
||||
m.sender_id,
|
||||
m.text,
|
||||
m.created_at
|
||||
FROM idempotency_keys AS k
|
||||
JOIN messages AS m ON m.id = k.message_id
|
||||
WHERE k.user_id = sqlc.arg(user_id)
|
||||
AND k.method = sqlc.arg(method)
|
||||
AND k.path = sqlc.arg(path)
|
||||
AND k.key = sqlc.arg(key);
|
||||
|
||||
-- name: DeleteExpiredIdempotencyKeys :execrows
|
||||
DELETE FROM idempotency_keys
|
||||
WHERE created_at < sqlc.arg(cutoff);
|
||||
@@ -0,0 +1,34 @@
|
||||
-- name: CreateMessage :one
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text
|
||||
)
|
||||
SELECT
|
||||
sqlc.arg(id)::uuid,
|
||||
sqlc.arg(chat_id)::uuid,
|
||||
sqlc.arg(sender_id)::uuid,
|
||||
sqlc.arg(text)::text
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND user_id = sqlc.arg(sender_id)
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: ListMessagesFirstPage :many
|
||||
SELECT * FROM messages
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
ORDER BY created_at, id
|
||||
LIMIT sqlc.arg(lim);
|
||||
|
||||
-- name: ListMessagesAfter :many
|
||||
SELECT * FROM messages
|
||||
WHERE chat_id = sqlc.arg(chat_id)
|
||||
AND (created_at, id) > (
|
||||
sqlc.arg(after_created_at)::timestamptz,
|
||||
sqlc.arg(after_id)::uuid
|
||||
)
|
||||
ORDER BY created_at, id
|
||||
LIMIT sqlc.arg(lim);
|
||||
@@ -0,0 +1,32 @@
|
||||
-- name: CreateOutboxEntry :one
|
||||
INSERT INTO outbox (
|
||||
job_id,
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
attempt,
|
||||
available_at
|
||||
)
|
||||
SELECT
|
||||
job_id,
|
||||
id,
|
||||
pipeline_version,
|
||||
sqlc.arg(attempt)::int,
|
||||
sqlc.arg(available_at)::timestamptz
|
||||
FROM attachments
|
||||
WHERE id = sqlc.arg(attachment_id)::uuid
|
||||
AND job_id IS NOT NULL
|
||||
RETURNING *;
|
||||
|
||||
-- name: LockPendingOutboxEntries :many
|
||||
SELECT * FROM outbox
|
||||
WHERE published_at IS NULL
|
||||
AND available_at <= now()
|
||||
ORDER BY available_at, id
|
||||
LIMIT sqlc.arg(batch_size)::int
|
||||
FOR UPDATE SKIP LOCKED;
|
||||
|
||||
-- name: MarkOutboxPublished :execrows
|
||||
UPDATE outbox
|
||||
SET published_at = now()
|
||||
WHERE id = sqlc.arg(id)::bigint
|
||||
AND published_at IS NULL;
|
||||
@@ -0,0 +1,24 @@
|
||||
-- name: CreateSession :exec
|
||||
INSERT INTO sessions (
|
||||
token_hash,
|
||||
user_id,
|
||||
expires_at
|
||||
) VALUES (
|
||||
sqlc.arg(token_hash),
|
||||
sqlc.arg(user_id),
|
||||
sqlc.arg(expires_at)
|
||||
);
|
||||
|
||||
-- name: GetActiveSession :one
|
||||
SELECT user_id, expires_at FROM sessions
|
||||
WHERE token_hash = sqlc.arg(token_hash)
|
||||
AND expires_at > now();
|
||||
|
||||
-- name: DeleteSession :execrows
|
||||
DELETE FROM sessions
|
||||
WHERE token_hash = sqlc.arg(token_hash)
|
||||
AND expires_at > now();
|
||||
|
||||
-- name: DeleteExpiredSessions :execrows
|
||||
DELETE FROM sessions
|
||||
WHERE expires_at <= now();
|
||||
@@ -0,0 +1,21 @@
|
||||
-- name: CreateUser :one
|
||||
INSERT INTO users (
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
password_hash
|
||||
) VALUES (
|
||||
sqlc.arg(id),
|
||||
sqlc.arg(username),
|
||||
sqlc.arg(display_name),
|
||||
sqlc.arg(password_hash)
|
||||
)
|
||||
RETURNING id, username, display_name, created_at;
|
||||
|
||||
-- name: GetUser :one
|
||||
SELECT id, username, display_name, created_at FROM users
|
||||
WHERE id = sqlc.arg(id);
|
||||
|
||||
-- name: GetUserByUsername :one
|
||||
SELECT * FROM users
|
||||
WHERE username = sqlc.arg(username);
|
||||
@@ -0,0 +1,33 @@
|
||||
events {
|
||||
worker_connections 4096;
|
||||
}
|
||||
|
||||
http {
|
||||
resolver 127.0.0.11 valid=2s ipv6=off;
|
||||
|
||||
server {
|
||||
listen 8080;
|
||||
|
||||
client_max_body_size 11m;
|
||||
|
||||
location ~ ^/api/v1/chats/[^/]+/messages$ {
|
||||
set $backend http://api:8080;
|
||||
proxy_pass $backend;
|
||||
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 6s;
|
||||
proxy_next_upstream error timeout http_500 http_502 non_idempotent;
|
||||
proxy_next_upstream_tries 2;
|
||||
}
|
||||
|
||||
location / {
|
||||
set $backend http://api:8080;
|
||||
proxy_pass $backend;
|
||||
|
||||
proxy_connect_timeout 1s;
|
||||
proxy_read_timeout 6s;
|
||||
proxy_next_upstream error timeout http_500 http_502;
|
||||
proxy_next_upstream_tries 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
> make contract-go
|
||||
go test -tags=contract ./student-tests/... -v -args -base-url=http://localhost:8080
|
||||
=== RUN TestAttachmentJPEG
|
||||
--- PASS: TestAttachmentJPEG (0.42s)
|
||||
=== RUN TestAttachmentTooManyPixels
|
||||
--- PASS: TestAttachmentTooManyPixels (2.12s)
|
||||
=== RUN TestAttachmentUnsafeFilename
|
||||
--- PASS: TestAttachmentUnsafeFilename (0.14s)
|
||||
=== RUN TestAttachmentTooLarge
|
||||
--- PASS: TestAttachmentTooLarge (0.22s)
|
||||
=== RUN TestAttachmentCorruptedImage
|
||||
--- PASS: TestAttachmentCorruptedImage (0.22s)
|
||||
=== RUN TestAttachmentMimeSubstitution
|
||||
--- PASS: TestAttachmentMimeSubstitution (0.16s)
|
||||
=== RUN TestAttachmentForeignID
|
||||
--- PASS: TestAttachmentForeignID (0.22s)
|
||||
=== RUN TestAttachmentFromAnotherChat
|
||||
--- PASS: TestAttachmentFromAnotherChat (1.02s)
|
||||
=== RUN TestCreateChatRollback
|
||||
--- PASS: TestCreateChatRollback (0.16s)
|
||||
=== RUN TestInvalidJSON
|
||||
--- PASS: TestInvalidJSON (0.00s)
|
||||
=== RUN TestForeignChat
|
||||
--- PASS: TestForeignChat (0.60s)
|
||||
=== RUN TestEmptyMessage
|
||||
--- PASS: TestEmptyMessage (0.20s)
|
||||
=== RUN TestDuplicateUsername
|
||||
--- PASS: TestDuplicateUsername (0.24s)
|
||||
=== RUN TestPagination
|
||||
--- PASS: TestPagination (0.31s)
|
||||
=== RUN TestThumbnail
|
||||
=== RUN TestThumbnail/landscape
|
||||
=== RUN TestThumbnail/portrait
|
||||
=== RUN TestThumbnail/small
|
||||
=== RUN TestThumbnail/thin
|
||||
--- PASS: TestThumbnail (2.18s)
|
||||
--- PASS: TestThumbnail/landscape (0.35s)
|
||||
--- PASS: TestThumbnail/portrait (0.62s)
|
||||
--- PASS: TestThumbnail/small (0.40s)
|
||||
--- PASS: TestThumbnail/thin (0.82s)
|
||||
=== RUN TestVariantsEXIF
|
||||
=== RUN TestVariantsEXIF/thumbnail
|
||||
=== RUN TestVariantsEXIF/optimized
|
||||
--- PASS: TestVariantsEXIF (1.19s)
|
||||
--- PASS: TestVariantsEXIF/thumbnail (0.80s)
|
||||
--- PASS: TestVariantsEXIF/optimized (0.39s)
|
||||
=== RUN TestCrop
|
||||
=== RUN TestCrop/landscape
|
||||
=== RUN TestCrop/portrait
|
||||
=== RUN TestCrop/small
|
||||
--- PASS: TestCrop (1.61s)
|
||||
--- PASS: TestCrop/landscape (0.62s)
|
||||
--- PASS: TestCrop/portrait (0.39s)
|
||||
--- PASS: TestCrop/small (0.60s)
|
||||
=== RUN TestOptimized
|
||||
=== RUN TestOptimized/landscape
|
||||
=== RUN TestOptimized/portrait
|
||||
=== RUN TestOptimized/small
|
||||
--- PASS: TestOptimized (2.50s)
|
||||
--- PASS: TestOptimized/landscape (0.84s)
|
||||
--- PASS: TestOptimized/portrait (1.06s)
|
||||
--- PASS: TestOptimized/small (0.59s)
|
||||
=== RUN TestLoginWrongPassword
|
||||
--- PASS: TestLoginWrongPassword (0.22s)
|
||||
=== RUN TestLogout
|
||||
--- PASS: TestLogout (0.18s)
|
||||
PASS
|
||||
ok mtusi2026-messenger/student-tests 13.930s
|
||||
@@ -0,0 +1,3 @@
|
||||
> make test
|
||||
python3 tests/smoke.py --base-url "http://localhost:8080"
|
||||
PASS: lab5 public API scenario; infrastructure and grading evidence remain separate.
|
||||
@@ -0,0 +1,13 @@
|
||||
> make pipeline-go
|
||||
go test -count=1 -tags=nodynamic,pipeline ./student-tests/... -v -args -base-url=http://localhost:8080
|
||||
=== RUN TestPipelineStaleProcessing
|
||||
pipeline_fault_test.go:174: PASS attachment=01a1e881-531b-7c12-8bcc-1bb759d11d06: held v1 PUT; v2 ready; late v1 wrote S3 but could not replace v2; stale redelivery acknowledged
|
||||
--- PASS: TestPipelineStaleProcessing (12.67s)
|
||||
=== RUN TestPipelineReorderAndDuplicate
|
||||
pipeline_fault_test.go:245: PASS attachment=01a1e881-8af0-70c3-a95d-6fde917efa7f: v2 completed before v1 delivery; stale v1 and duplicate v2 acknowledged without changes
|
||||
--- PASS: TestPipelineReorderAndDuplicate (41.43s)
|
||||
=== RUN TestPipelineCrashBeforeWrite
|
||||
pipeline_fault_test.go:301: PASS attachment=01a1e882-22d9-749f-ad66-36ecf038165b: SIGKILL before first result PUT; natural lease recovery; same job ready on v2 attempt2
|
||||
--- PASS: TestPipelineCrashBeforeWrite (26.05s)
|
||||
PASS
|
||||
ok mtusi2026-messenger/student-tests 80.150s
|
||||
@@ -0,0 +1,40 @@
|
||||
> docker compose ps
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
mtusi2026lab5-api-1 mtusi-messenger:lab5 "/app/server" api 35 minutes ago Up 35 minutes (healthy) 8080/tcp
|
||||
mtusi2026lab5-api-2 mtusi-messenger:lab5 "/app/server" api 35 minutes ago Up 35 minutes (healthy) 8080/tcp
|
||||
mtusi2026lab5-load_balancer-1 nginx:1.31.7-alpine3.24 "/docker-entrypoint.…" load_balancer 35 minutes ago Up 35 minutes 127.0.0.1:8080->8080/tcp
|
||||
mtusi2026lab5-postgres-1 postgres:18.6 "docker-entrypoint.s…" postgres 35 minutes ago Up 35 minutes (healthy) 127.0.0.1:5432->5432/tcp
|
||||
mtusi2026lab5-rabbitmq-1 rabbitmq:4.2.9-alpine "/bin/sh -ec 'RABBIT…" rabbitmq 35 minutes ago Up 35 minutes (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp
|
||||
mtusi2026lab5-redis-1 redis:8.8.3-alpine3.23 "docker-entrypoint.s…" redis 35 minutes ago Up 35 minutes (healthy) 6379/tcp
|
||||
mtusi2026lab5-s3-1 chrislusf/seaweedfs:4.51 "/entrypoint.sh serv…" s3 35 minutes ago Up 35 minutes (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp
|
||||
mtusi2026lab5-worker-1 mtusi-messenger-worker:lab5 "/app/worker" worker 35 minutes ago Up 35 minutes
|
||||
> make worker-go
|
||||
go test -count=1 -tags=worker ./student-tests/... -v -args -base-url=http://localhost:8080
|
||||
=== RUN TestWorkerPersistenceAndDuplicate
|
||||
worker_test.go:116: PASS attachment=01a1e86c-a8f6-7816-82aa-f21d58b7d859: queued survives API/broker restart; ready; duplicate acknowledged with unchanged result
|
||||
--- PASS: TestWorkerPersistenceAndDuplicate (33.88s)
|
||||
=== RUN TestWorkerRetryExhausted
|
||||
worker_test.go:161: PASS attachment=01a1e86d-245d-78aa-b370-288b7b9aae01: 5 attempts, failed/retry_exhausted, content/link rejected
|
||||
--- PASS: TestWorkerRetryExhausted (39.01s)
|
||||
=== RUN TestWorkerProcessingTimeout
|
||||
--- PASS: TestWorkerProcessingTimeout (10.11s)
|
||||
=== RUN TestWorkerReplay
|
||||
--- PASS: TestWorkerReplay (15.20s)
|
||||
=== RUN TestWorkerOutboxRecovery
|
||||
worker_test.go:342: PASS attachment=01a1e86e-2d67-70db-98e7-ea3f137bb7c1: commit, API SIGKILL before publication, automatic outbox recovery, same job ready
|
||||
--- PASS: TestWorkerOutboxRecovery (33.15s)
|
||||
=== RUN TestWorkerLeaseRecovery
|
||||
worker_test.go:425: PASS attachment=01a1e86e-b2b4-7d23-a1cb-0ba40351a02f: SIGKILL after S3 writes, expired lease recovered, same job ready on attempt 2
|
||||
--- PASS: TestWorkerLeaseRecovery (26.54s)
|
||||
PASS
|
||||
ok mtusi2026-messenger/student-tests 157.912s
|
||||
> docker compose ps
|
||||
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
|
||||
mtusi2026lab5-api-1 mtusi-messenger:lab5 "/app/server" api 38 minutes ago Up About a minute (healthy) 8080/tcp
|
||||
mtusi2026lab5-api-2 mtusi-messenger:lab5 "/app/server" api 38 minutes ago Up About a minute (healthy) 8080/tcp
|
||||
mtusi2026lab5-load_balancer-1 nginx:1.31.7-alpine3.24 "/docker-entrypoint.…" load_balancer 38 minutes ago Up 38 minutes 127.0.0.1:8080->8080/tcp
|
||||
mtusi2026lab5-postgres-1 postgres:18.6 "docker-entrypoint.s…" postgres 39 minutes ago Up 38 minutes (healthy) 127.0.0.1:5432->5432/tcp
|
||||
mtusi2026lab5-rabbitmq-1 rabbitmq:4.2.9-alpine "/bin/sh -ec 'RABBIT…" rabbitmq 2 minutes ago Up About a minute (healthy) 4369/tcp, 5671-5672/tcp, 15691-15692/tcp, 25672/tcp
|
||||
mtusi2026lab5-redis-1 redis:8.8.3-alpine3.23 "docker-entrypoint.s…" redis 39 minutes ago Up 38 minutes (healthy) 6379/tcp
|
||||
mtusi2026lab5-s3-1 chrislusf/seaweedfs:4.51 "/entrypoint.sh serv…" s3 38 minutes ago Up 38 minutes (healthy) 7333/tcp, 8080/tcp, 8333/tcp, 8888/tcp, 9333/tcp, 18080/tcp, 18888/tcp, 19333/tcp
|
||||
mtusi2026lab5-worker-1 mtusi-messenger-worker:lab5 "/app/worker" worker 38 minutes ago Up 41 seconds
|
||||
@@ -0,0 +1,46 @@
|
||||
module mtusi2026-messenger
|
||||
|
||||
go 1.26.0
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.5
|
||||
github.com/aws/aws-sdk-go-v2/config v1.33.13
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.13
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6
|
||||
github.com/aws/smithy-go v1.28.7
|
||||
github.com/disintegration/imaging v1.6.2
|
||||
github.com/gen2brain/webp v0.6.4
|
||||
github.com/go-chi/chi/v5 v5.5.0
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.11.0
|
||||
github.com/joho/godotenv v1.5.1
|
||||
github.com/rabbitmq/amqp091-go v1.10.0
|
||||
github.com/redis/go-redis/v9 v9.23.0
|
||||
golang.org/x/crypto v0.58.0
|
||||
golang.org/x/image v0.48.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.12.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.39.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1 // indirect
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.51.6 // indirect
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/ebitengine/purego v0.10.1 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
go.uber.org/atomic v1.12.0 // indirect
|
||||
golang.org/x/sync v0.25.0 // indirect
|
||||
golang.org/x/sys v0.50.0 // indirect
|
||||
golang.org/x/text v0.44.0 // indirect
|
||||
)
|
||||
@@ -0,0 +1,97 @@
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.5 h1:5yA+A+BiXbDq1ZLbYwPViwhEGDMdR0MEhL8Jh2LnLS4=
|
||||
github.com/aws/aws-sdk-go-v2 v1.47.5/go.mod h1:iNIkogrRvFwm+1OQIUOP2lPBtlXZboOIUWiCa+pHEMw=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25 h1:MW0Iql1SOTgQPj8mCxrU/eH/K/AIDWi08ItOsXaWRLU=
|
||||
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.7.25/go.mod h1:+ucVYmHYB3JqVueY0E1RHahdXAnKQbIHqybSbvOUbOM=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.33.13 h1:u52x6ISQOx172tpwM0VRxhJzEsTJYpMcEYUGM2A1wrw=
|
||||
github.com/aws/aws-sdk-go-v2/config v1.33.13/go.mod h1:/ixtz6XPHb42zekjQ0LqD9XjTpTysNWhIGwufY1OzI4=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.13 h1:j4z41OtspmQ3ftQycaDunJ26b69a9SHfaSVUPqgRv8k=
|
||||
github.com/aws/aws-sdk-go-v2/credentials v1.20.13/go.mod h1:SgPA1oS3b+dlMxZb618jDmGNscpXSCaxz15igS52OlE=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5 h1:iXjy2SFhGYNB1jWZBhQqq8HbhsbPF23w+/zbMvPXsTg=
|
||||
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.20.5/go.mod h1:k0RUiBnyXU0AXRJ9tJmY2jQke6XUIX2PNVnWtvgaPtc=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8 h1:gzIWc3DTPfPbWKrQnmL6BV6WD7NPD1htxzgdk/bQ/Z4=
|
||||
github.com/aws/aws-sdk-go-v2/internal/configsources v1.5.8/go.mod h1:vvUA5cDAZWGo4R14FZPZfXfJCmgsafOWEq78Kg0D+ug=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8 h1:PWv+vG8aCLS8qd+HPdYhK7Vees90aKHWFFQ1AGYD/8E=
|
||||
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.8.8/go.mod h1:xxPZEpflYHJA12366ZGyq66z9dHPCoeCIu99Jck+MPw=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8 h1:WOp5De9iWdEHDPh2Kb0tBP3vGBm/dVAuRGh4a4tqduo=
|
||||
github.com/aws/aws-sdk-go-v2/internal/v4a v1.5.8/go.mod h1:z8W8YgfhbWl62XqcTtK+CxPrE+iHGRVUOtrrbIOp7ZY=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23 h1:MCIR6iZaCKO4UE0hUvoWWLI6JVjppopV64JfAJaMjng=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.13.23/go.mod h1:pphWFS+z0MmIPivj8c/6bATd1Z+Av7Ust75DPdEFsPA=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9 h1:GACgECI422Xt5n4Pojr3O0aocPv8lfH4PJe2Aon5Wv8=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.11.9/go.mod h1:lPfnRkHpn1qWZJLbVEw10rIqSuCee1pNQTyGkUjAYh8=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8 h1:+PMpWlJYOn2tm7sm6OZ/ZLd4jStl17T7wjNapd1Rczs=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.14.8/go.mod h1:cnG3xZLGQL4c/bt8pjdzlFtZsW7epBBuNS04a5xMjdo=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8 h1:RS1Wt+K0r4X0ci9Vf4JYTUfHzsQihYA2LgGJeEneKpk=
|
||||
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.20.8/go.mod h1:adMnTy5kyoUW86jhXnGSVc/mvMQI4GZd1qYWMeaS8M8=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6 h1:to7+Vr3rLkpYRwVKJ5QRriI0eE306vrVN/6TnVHgxvY=
|
||||
github.com/aws/aws-sdk-go-v2/service/s3 v1.114.6/go.mod h1:Jc89ASa2k7HMAskj3bSnt8bf+k+p2IApzkoXnEjkgUw=
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.12.1 h1:WZLC+k/CZx11kZGQDqu9mpA8yhiXGX2I//jhuJE1FDo=
|
||||
github.com/aws/aws-sdk-go-v2/service/signin v1.12.1/go.mod h1:wxAk0D2oVBSjuXEIzvcKxTZfM90zticdAbh/VD42L0Y=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.39.1 h1:4VB/dnDo7pmXSLQMszSoBzWndENuc7mzwTpD6lQn77A=
|
||||
github.com/aws/aws-sdk-go-v2/service/sso v1.39.1/go.mod h1:UGsEYgWz4Gi+46vDQ5zkFLqUgJziyOE2SXjZjgmfmHc=
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1 h1:w8yui2ZqdHmGwVBDGvazoIvKw/DnLENBLkYnN8q8iqo=
|
||||
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.44.1/go.mod h1:gJSdIY6YUzDvqQLcMtPVZgBRHrFwzHryvpE8Igzbxps=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.51.6 h1:BVjKrtRUtSYKztBCCfE1oAB+d3ViKcW2cKZfserOqMQ=
|
||||
github.com/aws/aws-sdk-go-v2/service/sts v1.51.6/go.mod h1:eSukFLeLkPMm+1OxDR+F0joauZdyBkF47Ow7MqjZiJw=
|
||||
github.com/aws/smithy-go v1.28.7 h1:h/n1SHrwVtaDYLz8mR1/Kq4Xh4RaFbFW7bOYckg9mfE=
|
||||
github.com/aws/smithy-go v1.28.7/go.mod h1:YE2RhdIuDbA5E5bTdciG9KrW3+TiEONeUWCqxX9i1Fc=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||
github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c=
|
||||
github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
|
||||
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
|
||||
github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY=
|
||||
github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ=
|
||||
github.com/gen2brain/webp v0.6.4 h1:SUDdmxADOAiPQ+5ylNmuHhuYf2dOi0KgKZHL5vpVCNU=
|
||||
github.com/gen2brain/webp v0.6.4/go.mod h1:iGWMaCSw7t3I/Cv9llzEKmpnR36S8lS8VL/ZVjxU0JE=
|
||||
github.com/go-chi/chi/v5 v5.5.0 h1:xdKQ1dJmVJsmgahrXSKhUx8NyVPzpwdlyrrQFpMPJgg=
|
||||
github.com/go-chi/chi/v5 v5.5.0/go.mod h1:d1IG7GtKPSjPyBP365PH8sAGVHk3qfB/IhjFNRH1wWM=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
|
||||
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
|
||||
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/rabbitmq/amqp091-go v1.10.0 h1:STpn5XsHlHGcecLmMFCtg7mqq0RnD+zFr4uzukfVhBw=
|
||||
github.com/rabbitmq/amqp091-go v1.10.0/go.mod h1:Hy4jKW5kQART1u+JkDTF9YYOQUHXqMuhrgxOEeS7G4o=
|
||||
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
|
||||
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
|
||||
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
|
||||
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
|
||||
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
|
||||
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
|
||||
go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk=
|
||||
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
|
||||
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
|
||||
go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
|
||||
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
|
||||
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
|
||||
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
|
||||
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/image v0.48.0 h1:1AvuPcuoOTJpA7s4VZ5umrb90T3So0rkB/bSGmxkxik=
|
||||
golang.org/x/image v0.48.0/go.mod h1:mmtg64iqJKGqEgy+r13dfybl85tmxUd2lMnmaiSdWYY=
|
||||
golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs=
|
||||
golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs=
|
||||
golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.44.0 h1:YItdp/mQNGnsmrgpcdA5yQ3bnRxCfo/Tj/iByGW9GGo=
|
||||
golang.org/x/text v0.44.0/go.mod h1:S4FR1waQ9LbjQTls97SyRjDaUUVkaM+cXwwuG/G+MwY=
|
||||
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,167 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
|
||||
"mtusi2026-messenger/internal/config"
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/logger"
|
||||
"mtusi2026-messenger/internal/outbox"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
"mtusi2026-messenger/internal/redis"
|
||||
s3store "mtusi2026-messenger/internal/s3"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
func Run() error {
|
||||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
||||
return err
|
||||
}
|
||||
|
||||
cfg := config.Load()
|
||||
|
||||
log := logger.New(cfg.Logging.Level).With("instance_id", cfg.Server.InstanceID)
|
||||
|
||||
dbURL, err := cfg.Database.URL()
|
||||
if err != nil {
|
||||
log.Error("read database url", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
pool, err := postgres.New(context.Background(), dbURL)
|
||||
if err != nil {
|
||||
log.Error("connect to database", "error", err)
|
||||
return err
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
queries := db.New(pool)
|
||||
|
||||
var rdb *goredis.Client
|
||||
if cfg.UseCache() {
|
||||
cacheURL, err := cfg.Redis.URL()
|
||||
if err != nil {
|
||||
log.Error("read redis url", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
rdb, err = redis.New(context.Background(), log, cacheURL)
|
||||
if err != nil {
|
||||
log.Error("create redis client", "error", err)
|
||||
return err
|
||||
}
|
||||
defer rdb.Close()
|
||||
}
|
||||
|
||||
log.Info("session store configured", "store", cfg.SessionStore)
|
||||
|
||||
s3Client, err := s3store.New(context.Background(), cfg.S3, log)
|
||||
if err != nil {
|
||||
log.Error("create s3 client", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
ensureCtx, cancelEnsure := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
err = s3Client.EnsureBucket(ensureCtx)
|
||||
cancelEnsure()
|
||||
if err != nil {
|
||||
log.Error("ensure s3 bucket", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("s3 storage ready", "endpoint", cfg.S3.Endpoint, "bucket", s3Client.Bucket())
|
||||
|
||||
brokerURL, err := cfg.Broker.URL()
|
||||
if err != nil {
|
||||
log.Error("read broker url", "error", err)
|
||||
return err
|
||||
}
|
||||
publisher := outbox.NewPublisher(pool, brokerURL, log)
|
||||
|
||||
backgroundCtx, stopBackground := context.WithCancel(context.Background())
|
||||
var backgroundWG sync.WaitGroup
|
||||
backgroundWG.Go(func() {
|
||||
publisher.Run(backgroundCtx)
|
||||
})
|
||||
backgroundWG.Go(func() {
|
||||
runSessionCleanup(backgroundCtx, log, queries, sessionCleanupInterval)
|
||||
})
|
||||
backgroundWG.Go(func() {
|
||||
runIdempotencyCleanup(backgroundCtx, log, queries, cfg.Server.IdempotencyTTL, idempotencyCleanupInterval)
|
||||
})
|
||||
|
||||
defer func() {
|
||||
stopBackground()
|
||||
backgroundWG.Wait()
|
||||
}()
|
||||
|
||||
authService := service.NewAuthService(queries, rdb, cfg.Server.SessionTTL, log)
|
||||
userService := service.NewUserService(queries)
|
||||
chatService := service.NewChatService(pool, queries)
|
||||
messageService := service.NewMessageService(pool, queries)
|
||||
attachmentService := service.NewAttachmentService(pool, queries, s3Client, cfg.S3.MaxUploadBytes, cfg.S3.MaxImagePixels)
|
||||
|
||||
router := NewRouter(
|
||||
log,
|
||||
cfg.Server.InstanceID,
|
||||
cfg.Server.MaxInflightRequests,
|
||||
cfg.S3.MaxUploadBytes,
|
||||
pool,
|
||||
s3Client,
|
||||
authService,
|
||||
userService,
|
||||
chatService,
|
||||
messageService,
|
||||
attachmentService,
|
||||
)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: cfg.Server.Host + ":" + cfg.Server.Port,
|
||||
Handler: router,
|
||||
}
|
||||
|
||||
serverErrors := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
log.Info("server starting", "addr", server.Addr)
|
||||
serverErrors <- server.ListenAndServe()
|
||||
}()
|
||||
|
||||
signalCh := make(chan os.Signal, 1)
|
||||
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
|
||||
defer signal.Stop(signalCh)
|
||||
|
||||
select {
|
||||
case err := <-serverErrors:
|
||||
if err == http.ErrServerClosed {
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Error("server failed", "error", err)
|
||||
return err
|
||||
|
||||
case sig := <-signalCh:
|
||||
log.Info("shutdown signal received", "signal", sig.String())
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(ctx); err != nil {
|
||||
log.Error("server shutdown failed", "error", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Info("server stopped")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
sessionCleanupInterval = time.Hour
|
||||
idempotencyCleanupInterval = time.Hour
|
||||
)
|
||||
|
||||
type sessionCleaner interface {
|
||||
DeleteExpiredSessions(ctx context.Context) (int64, error)
|
||||
}
|
||||
|
||||
type idempotencyCleaner interface {
|
||||
DeleteExpiredIdempotencyKeys(ctx context.Context, cutoff time.Time) (int64, error)
|
||||
}
|
||||
|
||||
func runSessionCleanup(ctx context.Context, log *slog.Logger, q sessionCleaner, interval time.Duration) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
n, err := q.DeleteExpiredSessions(ctx)
|
||||
if err != nil {
|
||||
if ctx.Err() == nil {
|
||||
log.Error("delete expired sessions", "error", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if n > 0 {
|
||||
log.Info("expired sessions deleted", "count", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func runIdempotencyCleanup(
|
||||
ctx context.Context,
|
||||
log *slog.Logger,
|
||||
q idempotencyCleaner,
|
||||
ttl time.Duration,
|
||||
interval time.Duration,
|
||||
) {
|
||||
ticker := time.NewTicker(interval)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
n, err := q.DeleteExpiredIdempotencyKeys(ctx, time.Now().Add(-ttl))
|
||||
if err != nil {
|
||||
if ctx.Err() == nil {
|
||||
log.Error("delete expired idempotency keys", "error", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if n > 0 {
|
||||
log.Info("expired idempotency keys deleted", "count", n)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"mtusi2026-messenger/internal/config"
|
||||
|
||||
"github.com/joho/godotenv"
|
||||
)
|
||||
|
||||
func Healthcheck() int {
|
||||
if err := godotenv.Load(); err != nil && !os.IsNotExist(err) {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: load env:", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
cfg := config.Load()
|
||||
url := fmt.Sprintf("http://localhost:%s/health/ready", cfg.Server.Port)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: build request:", err)
|
||||
return 1
|
||||
}
|
||||
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: request failed:", err)
|
||||
return 1
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
fmt.Fprintln(os.Stderr, "healthcheck: unexpected status", resp.StatusCode)
|
||||
return 1
|
||||
}
|
||||
|
||||
return 0
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
"mtusi2026-messenger/internal/handler"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
func NewRouter(
|
||||
log *slog.Logger,
|
||||
instanceID string,
|
||||
maxInflightRequests int,
|
||||
maxUploadBytes int,
|
||||
db handler.Pinger,
|
||||
storage handler.Pinger,
|
||||
authService *service.AuthService,
|
||||
userService *service.UserService,
|
||||
chatService *service.ChatService,
|
||||
messageService *service.MessageService,
|
||||
attachmentService *service.AttachmentService,
|
||||
) http.Handler {
|
||||
router := chi.NewRouter()
|
||||
|
||||
healthHandler := handler.NewHealthHandler(log, db, storage)
|
||||
authHandler := handler.NewAuthHandler(authService)
|
||||
userHandler := handler.NewUserHandler(userService)
|
||||
chatHandler := handler.NewChatHandler(chatService)
|
||||
messageHandler := handler.NewMessageHandler(messageService)
|
||||
attachmentHandler := handler.NewAttachmentHandler(attachmentService, maxUploadBytes)
|
||||
|
||||
router.Use(middleware.InstanceID(instanceID))
|
||||
router.Use(middleware.RequestTimeout(5 * time.Second))
|
||||
router.Use(middleware.RequestID)
|
||||
router.Use(middleware.Logging(log))
|
||||
router.Use(middleware.ConcurrencyLimit(maxInflightRequests))
|
||||
|
||||
router.Get("/health/live", healthHandler.Live)
|
||||
router.Get("/health/ready", healthHandler.Ready)
|
||||
|
||||
router.Post("/api/v1/users", userHandler.Create)
|
||||
|
||||
router.Post("/api/v1/auth/sessions", authHandler.CreateSession)
|
||||
router.Delete("/api/v1/auth/sessions/current", authHandler.RevokeSession)
|
||||
|
||||
router.Group(func(r chi.Router) {
|
||||
r.Use(middleware.Auth(authService))
|
||||
|
||||
r.Get("/api/v1/users/me", userHandler.Me)
|
||||
|
||||
r.Post("/api/v1/chats", chatHandler.Create)
|
||||
r.Get("/api/v1/chats", chatHandler.List)
|
||||
r.Get("/api/v1/chats/{chat_id}", chatHandler.Get)
|
||||
|
||||
r.Post("/api/v1/chats/{chat_id}/messages", messageHandler.Create)
|
||||
r.Get("/api/v1/chats/{chat_id}/messages", messageHandler.List)
|
||||
|
||||
r.Post("/api/v1/chats/{chat_id}/attachments", attachmentHandler.Upload)
|
||||
r.Get("/api/v1/attachments/{attachment_id}", attachmentHandler.Get)
|
||||
r.Get("/api/v1/attachments/{attachment_id}/content", attachmentHandler.GetContent)
|
||||
})
|
||||
|
||||
router.Handle("/swagger/*", http.StripPrefix("/swagger/", http.FileServer(http.Dir("web/swagger"))))
|
||||
router.Handle("/contracts/*", http.StripPrefix("/contracts/", http.FileServer(http.Dir("contracts"))))
|
||||
|
||||
return router
|
||||
}
|
||||
@@ -0,0 +1,259 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"log/slog"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
SessionStorePostgres = "postgres"
|
||||
SessionStoreRedis = "redis"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Server ServerConfig
|
||||
Logging LoggingConfig
|
||||
Database DatabaseConfig
|
||||
Redis RedisConfig
|
||||
Broker BrokerConfig
|
||||
S3 S3Config
|
||||
SessionStore string
|
||||
}
|
||||
|
||||
func (c Config) UseCache() bool {
|
||||
return c.SessionStore == SessionStoreRedis
|
||||
}
|
||||
|
||||
type ServerConfig struct {
|
||||
Host string
|
||||
Port string
|
||||
MaxInflightRequests int
|
||||
SessionTTL time.Duration
|
||||
IdempotencyTTL time.Duration
|
||||
InstanceID string
|
||||
}
|
||||
|
||||
type LoggingConfig struct {
|
||||
Level slog.Level
|
||||
}
|
||||
|
||||
type DatabaseConfig struct {
|
||||
URLFile string
|
||||
}
|
||||
|
||||
func (c DatabaseConfig) URL() (string, error) {
|
||||
return readSecretFile(c.URLFile)
|
||||
}
|
||||
|
||||
type RedisConfig struct {
|
||||
URLFile string
|
||||
}
|
||||
|
||||
func (c RedisConfig) URL() (string, error) {
|
||||
return readSecretFile(c.URLFile)
|
||||
}
|
||||
|
||||
type BrokerConfig struct {
|
||||
URLFile string
|
||||
}
|
||||
|
||||
func (c BrokerConfig) URL() (string, error) {
|
||||
return readSecretFile(c.URLFile)
|
||||
}
|
||||
|
||||
type S3Config struct {
|
||||
Endpoint string
|
||||
Region string
|
||||
Bucket string
|
||||
APIAccessKeyFile string
|
||||
APISecretKeyFile string
|
||||
BootstrapAccessKeyFile string
|
||||
BootstrapSecretKeyFile string
|
||||
MaxUploadBytes int
|
||||
MaxImagePixels int
|
||||
StagingRetention time.Duration
|
||||
}
|
||||
|
||||
func (c S3Config) APIAccessKey() (string, error) {
|
||||
return readSecretFile(c.APIAccessKeyFile)
|
||||
}
|
||||
|
||||
func (c S3Config) APISecretKey() (string, error) {
|
||||
return readSecretFile(c.APISecretKeyFile)
|
||||
}
|
||||
|
||||
func (c S3Config) BootstrapAccessKey() (string, error) {
|
||||
return readSecretFile(c.BootstrapAccessKeyFile)
|
||||
}
|
||||
|
||||
func (c S3Config) BootstrapSecretKey() (string, error) {
|
||||
return readSecretFile(c.BootstrapSecretKeyFile)
|
||||
}
|
||||
|
||||
func Load() Config {
|
||||
return Config{
|
||||
Server: ServerConfig{
|
||||
Host: getEnv("APP_HOST", "0.0.0.0"),
|
||||
Port: getEnv("APP_PORT", "8080"),
|
||||
MaxInflightRequests: getEnvNonNegativeInt("MAX_INFLIGHT_REQUESTS", 0),
|
||||
SessionTTL: time.Duration(getEnvPositiveInt("SESSION_TTL_SECONDS", 3600)) * time.Second,
|
||||
IdempotencyTTL: time.Duration(getEnvPositiveInt("IDEMPOTENCY_TTL_SECONDS", 86400)) * time.Second,
|
||||
InstanceID: getInstanceID("INSTANCE_ID"),
|
||||
},
|
||||
|
||||
Logging: LoggingConfig{
|
||||
Level: getEnvLogLevel("LOG_LEVEL", slog.LevelInfo),
|
||||
},
|
||||
|
||||
Database: DatabaseConfig{
|
||||
URLFile: getEnv("DATABASE_URL_FILE", "/run/secrets/database_url"),
|
||||
},
|
||||
|
||||
Redis: RedisConfig{
|
||||
URLFile: getEnv("CACHE_URL_FILE", "/run/secrets/cache_url"),
|
||||
},
|
||||
|
||||
Broker: BrokerConfig{
|
||||
URLFile: getEnv("BROKER_URL_FILE", "/run/secrets/broker_url"),
|
||||
},
|
||||
|
||||
S3: LoadS3(),
|
||||
|
||||
SessionStore: getEnvSessionStore("SESSION_STORE", SessionStorePostgres),
|
||||
}
|
||||
}
|
||||
|
||||
func LoadS3() S3Config {
|
||||
return S3Config{
|
||||
Endpoint: getEnv("S3_ENDPOINT", "http://s3:9000"),
|
||||
Region: getEnv("S3_REGION", "us-east-1"),
|
||||
Bucket: getEnv("S3_BUCKET", "messenger-attachments"),
|
||||
APIAccessKeyFile: getEnv("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id"),
|
||||
APISecretKeyFile: getEnv("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key"),
|
||||
BootstrapAccessKeyFile: getEnv("S3_BOOTSTRAP_ACCESS_KEY_ID_FILE", "/run/secrets/s3_bootstrap_access_key_id"),
|
||||
BootstrapSecretKeyFile: getEnv("S3_BOOTSTRAP_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_bootstrap_secret_access_key"),
|
||||
MaxUploadBytes: getEnvPositiveInt("MAX_UPLOAD_BYTES", 10485760),
|
||||
MaxImagePixels: getEnvPositiveInt("MAX_IMAGE_PIXELS", 40000000),
|
||||
StagingRetention: time.Duration(getEnvPositiveInt("STAGING_RETENTION_SECONDS", 86400)) * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
type WorkerConfig struct {
|
||||
Concurrency int
|
||||
MaxAttempts int
|
||||
Timeout time.Duration
|
||||
}
|
||||
|
||||
func LoadWorker() WorkerConfig {
|
||||
return WorkerConfig{
|
||||
Concurrency: getEnvPositiveInt("WORKER_CONCURRENCY", 2),
|
||||
MaxAttempts: getEnvPositiveInt("JOB_MAX_ATTEMPTS", 5),
|
||||
Timeout: time.Duration(getEnvPositiveInt("JOB_TIMEOUT_SECONDS", 60)) * time.Second,
|
||||
}
|
||||
}
|
||||
|
||||
func getEnv(key, def string) string {
|
||||
value := os.Getenv(key)
|
||||
if value == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func getEnvNonNegativeInt(key string, def int) int {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(v)
|
||||
if err != nil || value < 0 {
|
||||
log.Printf("invalid non-negative int for %s=%q, using default %d", key, v, def)
|
||||
return def
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func getEnvPositiveInt(key string, def int) int {
|
||||
v := os.Getenv(key)
|
||||
if v == "" {
|
||||
return def
|
||||
}
|
||||
|
||||
value, err := strconv.Atoi(v)
|
||||
if err != nil || value <= 0 {
|
||||
log.Printf("invalid positive int for %s=%q, using default %d", key, v, def)
|
||||
return def
|
||||
}
|
||||
|
||||
return value
|
||||
}
|
||||
|
||||
func getEnvLogLevel(key string, def slog.Level) slog.Level {
|
||||
v := os.Getenv(key)
|
||||
|
||||
switch v {
|
||||
case "DEBUG":
|
||||
return slog.LevelDebug
|
||||
case "INFO":
|
||||
return slog.LevelInfo
|
||||
case "WARN":
|
||||
return slog.LevelWarn
|
||||
case "ERROR":
|
||||
return slog.LevelError
|
||||
case "":
|
||||
return def
|
||||
default:
|
||||
log.Printf("invalid log level for %s=%q, using default %s", key, v, def)
|
||||
return def
|
||||
}
|
||||
}
|
||||
|
||||
func readSecretFile(path string) (string, error) {
|
||||
b, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read secret file %s: %w", path, err)
|
||||
}
|
||||
|
||||
value := strings.TrimSpace(string(b))
|
||||
if value == "" {
|
||||
return "", fmt.Errorf("secret file %s is empty", path)
|
||||
}
|
||||
|
||||
return value, nil
|
||||
}
|
||||
|
||||
func getEnvSessionStore(key, def string) string {
|
||||
v := strings.ToLower(os.Getenv(key))
|
||||
|
||||
switch v {
|
||||
case "":
|
||||
return def
|
||||
case SessionStorePostgres, SessionStoreRedis:
|
||||
return v
|
||||
default:
|
||||
log.Printf("invalid session store for %s=%q, using default %s", key, v, def)
|
||||
return def
|
||||
}
|
||||
}
|
||||
|
||||
func getInstanceID(key string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
|
||||
host, err := os.Hostname()
|
||||
if err != nil || host == "" {
|
||||
log.Printf("cannot determine hostname, using \"unknown\" for %s: %v", key, err)
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
return host
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package credentials
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
const tokenSize = 32
|
||||
|
||||
var ErrPasswordMismatch = errors.New("password mismatch")
|
||||
|
||||
func prehash(plain string) []byte {
|
||||
sum := sha256.Sum256([]byte(plain))
|
||||
return []byte(base64.StdEncoding.EncodeToString(sum[:]))
|
||||
}
|
||||
|
||||
func HashPassword(plain string) (string, error) {
|
||||
hash, err := bcrypt.GenerateFromPassword(prehash(plain), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return string(hash), nil
|
||||
}
|
||||
|
||||
func VerifyPassword(hash, plain string) error {
|
||||
err := bcrypt.CompareHashAndPassword([]byte(hash), prehash(plain))
|
||||
if errors.Is(err, bcrypt.ErrMismatchedHashAndPassword) {
|
||||
return ErrPasswordMismatch
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func NewToken() string {
|
||||
raw := make([]byte, tokenSize)
|
||||
_, _ = rand.Read(raw) // never returns an error since Go 1.24
|
||||
|
||||
return base64.RawURLEncoding.EncodeToString(raw)
|
||||
}
|
||||
|
||||
func HashToken(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: attachment_replays.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createAttachmentReplay = `-- name: CreateAttachmentReplay :exec
|
||||
INSERT INTO attachment_replays (
|
||||
attachment_id,
|
||||
attempt,
|
||||
previous_error_code,
|
||||
operator,
|
||||
reason
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5
|
||||
)
|
||||
`
|
||||
|
||||
type CreateAttachmentReplayParams struct {
|
||||
AttachmentID uuid.UUID
|
||||
Attempt int
|
||||
PreviousErrorCode *string
|
||||
Operator string
|
||||
Reason string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAttachmentReplay(ctx context.Context, arg CreateAttachmentReplayParams) error {
|
||||
_, err := q.db.Exec(ctx, createAttachmentReplay,
|
||||
arg.AttachmentID,
|
||||
arg.Attempt,
|
||||
arg.PreviousErrorCode,
|
||||
arg.Operator,
|
||||
arg.Reason,
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: attachment_variants.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createAttachmentVariant = `-- name: CreateAttachmentVariant :exec
|
||||
INSERT INTO attachment_variants (
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
variant,
|
||||
s3_key,
|
||||
content_type,
|
||||
size_bytes,
|
||||
width,
|
||||
height,
|
||||
sha256
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6,
|
||||
$7,
|
||||
$8,
|
||||
$9
|
||||
)
|
||||
`
|
||||
|
||||
type CreateAttachmentVariantParams struct {
|
||||
AttachmentID uuid.UUID
|
||||
PipelineVersion int
|
||||
Variant AttachmentVariantName
|
||||
S3Key string
|
||||
ContentType string
|
||||
SizeBytes int
|
||||
Width int
|
||||
Height int
|
||||
Sha256 string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAttachmentVariant(ctx context.Context, arg CreateAttachmentVariantParams) error {
|
||||
_, err := q.db.Exec(ctx, createAttachmentVariant,
|
||||
arg.AttachmentID,
|
||||
arg.PipelineVersion,
|
||||
arg.Variant,
|
||||
arg.S3Key,
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Width,
|
||||
arg.Height,
|
||||
arg.Sha256,
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
const listAttachmentVariants = `-- name: ListAttachmentVariants :many
|
||||
SELECT attachment_id, pipeline_version, variant, s3_key, content_type, size_bytes, sha256, width, height FROM attachment_variants
|
||||
WHERE attachment_id = $1::uuid
|
||||
AND pipeline_version = $2::int
|
||||
ORDER BY variant
|
||||
`
|
||||
|
||||
type ListAttachmentVariantsParams struct {
|
||||
AttachmentID uuid.UUID
|
||||
PipelineVersion int
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentVariants(ctx context.Context, arg ListAttachmentVariantsParams) ([]AttachmentVariant, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentVariants, arg.AttachmentID, arg.PipelineVersion)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []AttachmentVariant{}
|
||||
for rows.Next() {
|
||||
var i AttachmentVariant
|
||||
if err := rows.Scan(
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Variant,
|
||||
&i.S3Key,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -0,0 +1,799 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: attachments.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const claimAttachmentJob = `-- name: ClaimAttachmentJob :one
|
||||
UPDATE attachments
|
||||
SET status = 'processing',
|
||||
attempts = attempts + 1,
|
||||
lease_until = $1::timestamptz,
|
||||
updated_at = now()
|
||||
WHERE id = $2::uuid
|
||||
AND job_id = $3::uuid
|
||||
AND pipeline_version = $4::int
|
||||
AND status = 'queued'
|
||||
AND attempts + 1 = $5::int
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until
|
||||
`
|
||||
|
||||
type ClaimAttachmentJobParams struct {
|
||||
LeaseUntil time.Time
|
||||
ID uuid.UUID
|
||||
JobID uuid.UUID
|
||||
PipelineVersion int
|
||||
Attempt int
|
||||
}
|
||||
|
||||
func (q *Queries) ClaimAttachmentJob(ctx context.Context, arg ClaimAttachmentJobParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, claimAttachmentJob,
|
||||
arg.LeaseUntil,
|
||||
arg.ID,
|
||||
arg.JobID,
|
||||
arg.PipelineVersion,
|
||||
arg.Attempt,
|
||||
)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const clearAttachmentStagingKey = `-- name: ClearAttachmentStagingKey :exec
|
||||
UPDATE attachments
|
||||
SET staging_key = NULL
|
||||
WHERE id = $1::uuid
|
||||
`
|
||||
|
||||
func (q *Queries) ClearAttachmentStagingKey(ctx context.Context, id uuid.UUID) error {
|
||||
_, err := q.db.Exec(ctx, clearAttachmentStagingKey, id)
|
||||
return err
|
||||
}
|
||||
|
||||
const completeAttachmentJob = `-- name: CompleteAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'ready',
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = $2::int
|
||||
AND lease_until > now()
|
||||
`
|
||||
|
||||
type CompleteAttachmentJobParams struct {
|
||||
ID uuid.UUID
|
||||
Attempt int
|
||||
}
|
||||
|
||||
func (q *Queries) CompleteAttachmentJob(ctx context.Context, arg CompleteAttachmentJobParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, completeAttachmentJob, arg.ID, arg.Attempt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const createAttachment = `-- name: CreateAttachment :one
|
||||
INSERT INTO attachments (
|
||||
id,
|
||||
chat_id,
|
||||
owner_id,
|
||||
s3_key,
|
||||
staging_key,
|
||||
job_id,
|
||||
filename,
|
||||
content_type,
|
||||
size_bytes,
|
||||
sha256,
|
||||
width,
|
||||
height,
|
||||
error_code
|
||||
)
|
||||
SELECT
|
||||
$1::uuid,
|
||||
$2::uuid,
|
||||
$3::uuid,
|
||||
$4::text,
|
||||
$5::text,
|
||||
$6::uuid,
|
||||
$7::text,
|
||||
$8::attachment_content_type,
|
||||
$9::int,
|
||||
$10::text,
|
||||
$11::int,
|
||||
$12::int,
|
||||
NULLIF($13::text, '')
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = $2
|
||||
AND user_id = $3
|
||||
)
|
||||
RETURNING id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until
|
||||
`
|
||||
|
||||
type CreateAttachmentParams struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
S3Key string
|
||||
StagingKey string
|
||||
JobID uuid.UUID
|
||||
Filename string
|
||||
ContentType AttachmentContentType
|
||||
SizeBytes int
|
||||
Sha256 string
|
||||
Width int
|
||||
Height int
|
||||
ErrorCode string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateAttachment(ctx context.Context, arg CreateAttachmentParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, createAttachment,
|
||||
arg.ID,
|
||||
arg.ChatID,
|
||||
arg.OwnerID,
|
||||
arg.S3Key,
|
||||
arg.StagingKey,
|
||||
arg.JobID,
|
||||
arg.Filename,
|
||||
arg.ContentType,
|
||||
arg.SizeBytes,
|
||||
arg.Sha256,
|
||||
arg.Width,
|
||||
arg.Height,
|
||||
arg.ErrorCode,
|
||||
)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const createMessageAttachment = `-- name: CreateMessageAttachment :exec
|
||||
INSERT INTO message_attachments (
|
||||
message_id,
|
||||
attachment_id,
|
||||
position
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3
|
||||
)
|
||||
`
|
||||
|
||||
type CreateMessageAttachmentParams struct {
|
||||
MessageID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
Position int
|
||||
}
|
||||
|
||||
func (q *Queries) CreateMessageAttachment(ctx context.Context, arg CreateMessageAttachmentParams) error {
|
||||
_, err := q.db.Exec(ctx, createMessageAttachment, arg.MessageID, arg.AttachmentID, arg.Position)
|
||||
return err
|
||||
}
|
||||
|
||||
const failAttachmentJob = `-- name: FailAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = $1::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = $2::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = $3::int
|
||||
AND lease_until > now()
|
||||
`
|
||||
|
||||
type FailAttachmentJobParams struct {
|
||||
ErrorCode string
|
||||
ID uuid.UUID
|
||||
Attempt int
|
||||
}
|
||||
|
||||
func (q *Queries) FailAttachmentJob(ctx context.Context, arg FailAttachmentJobParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, failAttachmentJob, arg.ErrorCode, arg.ID, arg.Attempt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const failExpiredAttachmentJob = `-- name: FailExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = 'retry_exhausted',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now()
|
||||
`
|
||||
|
||||
func (q *Queries) FailExpiredAttachmentJob(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, failExpiredAttachmentJob, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getAttachmentByID = `-- name: GetAttachmentByID :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
`
|
||||
|
||||
func (q *Queries) GetAttachmentByID(ctx context.Context, dollar_1 uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, getAttachmentByID, dollar_1)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listAttachmentsByIDs = `-- name: ListAttachmentsByIDs :many
|
||||
SELECT status
|
||||
FROM attachments
|
||||
WHERE chat_id = $1::uuid
|
||||
AND id = ANY($2::uuid[])
|
||||
`
|
||||
|
||||
type ListAttachmentsByIDsParams struct {
|
||||
ChatID uuid.UUID
|
||||
Ids []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentsByIDs(ctx context.Context, arg ListAttachmentsByIDsParams) ([]AttachmentStatus, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentsByIDs, arg.ChatID, arg.Ids)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []AttachmentStatus{}
|
||||
for rows.Next() {
|
||||
var status AttachmentStatus
|
||||
if err := rows.Scan(&status); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, status)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listAttachmentsForReconcile = `-- name: ListAttachmentsForReconcile :many
|
||||
SELECT id, s3_key, staging_key, status, size_bytes, updated_at
|
||||
FROM attachments
|
||||
`
|
||||
|
||||
type ListAttachmentsForReconcileRow struct {
|
||||
ID uuid.UUID
|
||||
S3Key string
|
||||
StagingKey *string
|
||||
Status AttachmentStatus
|
||||
SizeBytes int
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ListAttachmentsForReconcile(ctx context.Context) ([]ListAttachmentsForReconcileRow, error) {
|
||||
rows, err := q.db.Query(ctx, listAttachmentsForReconcile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListAttachmentsForReconcileRow{}
|
||||
for rows.Next() {
|
||||
var i ListAttachmentsForReconcileRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.S3Key,
|
||||
&i.StagingKey,
|
||||
&i.Status,
|
||||
&i.SizeBytes,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listFailedAttachments = `-- name: ListFailedAttachments :many
|
||||
SELECT id, job_id, error_code, attempts, updated_at
|
||||
FROM attachments
|
||||
WHERE status = 'failed'
|
||||
ORDER BY updated_at, id
|
||||
LIMIT $1::int
|
||||
`
|
||||
|
||||
type ListFailedAttachmentsRow struct {
|
||||
ID uuid.UUID
|
||||
JobID *uuid.UUID
|
||||
ErrorCode *string
|
||||
Attempts int
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) ListFailedAttachments(ctx context.Context, batchSize int) ([]ListFailedAttachmentsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listFailedAttachments, batchSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListFailedAttachmentsRow{}
|
||||
for rows.Next() {
|
||||
var i ListFailedAttachmentsRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.JobID,
|
||||
&i.ErrorCode,
|
||||
&i.Attempts,
|
||||
&i.UpdatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMessageAttachmentIDs = `-- name: ListMessageAttachmentIDs :many
|
||||
SELECT attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = $1::uuid
|
||||
ORDER BY position
|
||||
`
|
||||
|
||||
func (q *Queries) ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error) {
|
||||
rows, err := q.db.Query(ctx, listMessageAttachmentIDs, messageID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []uuid.UUID{}
|
||||
for rows.Next() {
|
||||
var attachment_id uuid.UUID
|
||||
if err := rows.Scan(&attachment_id); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, attachment_id)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMessageAttachmentsByMessageIDs = `-- name: ListMessageAttachmentsByMessageIDs :many
|
||||
SELECT message_id, attachment_id
|
||||
FROM message_attachments
|
||||
WHERE message_id = ANY($1::uuid[])
|
||||
ORDER BY message_id, position
|
||||
`
|
||||
|
||||
type ListMessageAttachmentsByMessageIDsRow struct {
|
||||
MessageID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIds []uuid.UUID) ([]ListMessageAttachmentsByMessageIDsRow, error) {
|
||||
rows, err := q.db.Query(ctx, listMessageAttachmentsByMessageIDs, messageIds)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListMessageAttachmentsByMessageIDsRow{}
|
||||
for rows.Next() {
|
||||
var i ListMessageAttachmentsByMessageIDsRow
|
||||
if err := rows.Scan(&i.MessageID, &i.AttachmentID); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockAttachmentByStagingKey = `-- name: LockAttachmentByStagingKey :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE staging_key = $1::text
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
func (q *Queries) LockAttachmentByStagingKey(ctx context.Context, stagingKey string) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, lockAttachmentByStagingKey, stagingKey)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockAttachmentForUpgrade = `-- name: LockAttachmentForUpgrade :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
AND pipeline_version = 1
|
||||
AND status IN ('queued', 'processing')
|
||||
AND staging_key IS NOT NULL
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
func (q *Queries) LockAttachmentForUpgrade(ctx context.Context, id uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, lockAttachmentForUpgrade, id)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockAttachmentJob = `-- name: LockAttachmentJob :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'processing'
|
||||
AND job_id = $2::uuid
|
||||
AND pipeline_version = $3::int
|
||||
AND attempts = $4::int
|
||||
AND lease_until > now()
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
type LockAttachmentJobParams struct {
|
||||
ID uuid.UUID
|
||||
JobID uuid.UUID
|
||||
PipelineVersion int
|
||||
Attempt int
|
||||
}
|
||||
|
||||
func (q *Queries) LockAttachmentJob(ctx context.Context, arg LockAttachmentJobParams) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, lockAttachmentJob,
|
||||
arg.ID,
|
||||
arg.JobID,
|
||||
arg.PipelineVersion,
|
||||
arg.Attempt,
|
||||
)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockExpiredAttachmentJobs = `-- name: LockExpiredAttachmentJobs :many
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE status = 'processing'
|
||||
AND lease_until <= now()
|
||||
ORDER BY lease_until, id
|
||||
LIMIT $1::int
|
||||
FOR UPDATE SKIP LOCKED
|
||||
`
|
||||
|
||||
func (q *Queries) LockExpiredAttachmentJobs(ctx context.Context, batchSize int) ([]Attachment, error) {
|
||||
rows, err := q.db.Query(ctx, lockExpiredAttachmentJobs, batchSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Attachment{}
|
||||
for rows.Next() {
|
||||
var i Attachment
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const lockFailedAttachmentForReplay = `-- name: LockFailedAttachmentForReplay :one
|
||||
SELECT id, chat_id, owner_id, s3_key, filename, content_type, size_bytes, sha256, status, error_code, created_at, updated_at, staging_key, job_id, pipeline_version, attempts, retry_base, width, height, lease_until FROM attachments
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'failed'
|
||||
AND staging_key IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_variants
|
||||
WHERE attachment_id = attachments.id
|
||||
)
|
||||
FOR UPDATE
|
||||
`
|
||||
|
||||
func (q *Queries) LockFailedAttachmentForReplay(ctx context.Context, id uuid.UUID) (Attachment, error) {
|
||||
row := q.db.QueryRow(ctx, lockFailedAttachmentForReplay, id)
|
||||
var i Attachment
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.OwnerID,
|
||||
&i.S3Key,
|
||||
&i.Filename,
|
||||
&i.ContentType,
|
||||
&i.SizeBytes,
|
||||
&i.Sha256,
|
||||
&i.Status,
|
||||
&i.ErrorCode,
|
||||
&i.CreatedAt,
|
||||
&i.UpdatedAt,
|
||||
&i.StagingKey,
|
||||
&i.JobID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempts,
|
||||
&i.RetryBase,
|
||||
&i.Width,
|
||||
&i.Height,
|
||||
&i.LeaseUntil,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const markAttachmentFailedIfReady = `-- name: MarkAttachmentFailedIfReady :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'failed',
|
||||
error_code = $1::text,
|
||||
updated_at = now()
|
||||
WHERE id = $2::uuid AND status = 'ready'
|
||||
`
|
||||
|
||||
type MarkAttachmentFailedIfReadyParams struct {
|
||||
ErrorCode string
|
||||
ID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) MarkAttachmentFailedIfReady(ctx context.Context, arg MarkAttachmentFailedIfReadyParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, markAttachmentFailedIfReady, arg.ErrorCode, arg.ID)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const recoverExpiredAttachmentJob = `-- name: RecoverExpiredAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'processing'
|
||||
AND lease_until <= now()
|
||||
`
|
||||
|
||||
func (q *Queries) RecoverExpiredAttachmentJob(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, recoverExpiredAttachmentJob, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const replayAttachmentJob = `-- name: ReplayAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
retry_base = attempts,
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
AND status = 'failed'
|
||||
AND staging_key IS NOT NULL
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM attachment_variants
|
||||
WHERE attachment_id = attachments.id
|
||||
)
|
||||
`
|
||||
|
||||
func (q *Queries) ReplayAttachmentJob(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, replayAttachmentJob, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const retryAttachmentJob = `-- name: RetryAttachmentJob :execrows
|
||||
UPDATE attachments
|
||||
SET status = 'queued',
|
||||
error_code = $1::text,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = $2::uuid
|
||||
AND status = 'processing'
|
||||
AND attempts = $3::int
|
||||
AND lease_until > now()
|
||||
`
|
||||
|
||||
type RetryAttachmentJobParams struct {
|
||||
ErrorCode string
|
||||
ID uuid.UUID
|
||||
Attempt int
|
||||
}
|
||||
|
||||
func (q *Queries) RetryAttachmentJob(ctx context.Context, arg RetryAttachmentJobParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, retryAttachmentJob, arg.ErrorCode, arg.ID, arg.Attempt)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const upgradeAttachmentPipeline = `-- name: UpgradeAttachmentPipeline :execrows
|
||||
UPDATE attachments
|
||||
SET pipeline_version = 2,
|
||||
status = 'queued',
|
||||
retry_base = attempts,
|
||||
error_code = NULL,
|
||||
lease_until = NULL,
|
||||
updated_at = now()
|
||||
WHERE id = $1::uuid
|
||||
AND pipeline_version = 1
|
||||
AND status IN ('queued', 'processing')
|
||||
AND staging_key IS NOT NULL
|
||||
`
|
||||
|
||||
func (q *Queries) UpgradeAttachmentPipeline(ctx context.Context, id uuid.UUID) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, upgradeAttachmentPipeline, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
@@ -0,0 +1,232 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: chats.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const addChatMembers = `-- name: AddChatMembers :exec
|
||||
INSERT INTO chat_members (
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
SELECT
|
||||
$1::uuid,
|
||||
unnest($2::uuid[])
|
||||
`
|
||||
|
||||
type AddChatMembersParams struct {
|
||||
ChatID uuid.UUID
|
||||
UserIds []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) AddChatMembers(ctx context.Context, arg AddChatMembersParams) error {
|
||||
_, err := q.db.Exec(ctx, addChatMembers, arg.ChatID, arg.UserIds)
|
||||
return err
|
||||
}
|
||||
|
||||
const createChat = `-- name: CreateChat :one
|
||||
INSERT INTO chats (
|
||||
id,
|
||||
title
|
||||
) VALUES (
|
||||
$1,
|
||||
$2
|
||||
)
|
||||
RETURNING id, title, created_at
|
||||
`
|
||||
|
||||
type CreateChatParams struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateChat(ctx context.Context, arg CreateChatParams) (Chat, error) {
|
||||
row := q.db.QueryRow(ctx, createChat, arg.ID, arg.Title)
|
||||
var i Chat
|
||||
err := row.Scan(&i.ID, &i.Title, &i.CreatedAt)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getChat = `-- name: GetChat :one
|
||||
SELECT
|
||||
c.id, c.title, c.created_at,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
WHERE c.id = $1
|
||||
AND EXISTS (
|
||||
SELECT 1 FROM chat_members m
|
||||
WHERE m.chat_id = c.id
|
||||
AND m.user_id = $2
|
||||
)
|
||||
`
|
||||
|
||||
type GetChatParams struct {
|
||||
ID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
type GetChatRow struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
MemberIds []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) GetChat(ctx context.Context, arg GetChatParams) (GetChatRow, error) {
|
||||
row := q.db.QueryRow(ctx, getChat, arg.ID, arg.UserID)
|
||||
var i GetChatRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.CreatedAt,
|
||||
&i.MemberIds,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const isChatMember = `-- name: IsChatMember :one
|
||||
SELECT EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = $1
|
||||
AND user_id = $2
|
||||
)
|
||||
`
|
||||
|
||||
type IsChatMemberParams struct {
|
||||
ChatID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) IsChatMember(ctx context.Context, arg IsChatMemberParams) (bool, error) {
|
||||
row := q.db.QueryRow(ctx, isChatMember, arg.ChatID, arg.UserID)
|
||||
var exists bool
|
||||
err := row.Scan(&exists)
|
||||
return exists, err
|
||||
}
|
||||
|
||||
const listChatsAfter = `-- name: ListChatsAfter :many
|
||||
SELECT
|
||||
c.id, c.title, c.created_at,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1
|
||||
AND (c.created_at, c.id) > (
|
||||
$2::timestamptz,
|
||||
$3::uuid
|
||||
)
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type ListChatsAfterParams struct {
|
||||
UserID uuid.UUID
|
||||
AfterCreatedAt time.Time
|
||||
AfterID uuid.UUID
|
||||
Lim int
|
||||
}
|
||||
|
||||
type ListChatsAfterRow struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
MemberIds []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListChatsAfter(ctx context.Context, arg ListChatsAfterParams) ([]ListChatsAfterRow, error) {
|
||||
rows, err := q.db.Query(ctx, listChatsAfter,
|
||||
arg.UserID,
|
||||
arg.AfterCreatedAt,
|
||||
arg.AfterID,
|
||||
arg.Lim,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListChatsAfterRow{}
|
||||
for rows.Next() {
|
||||
var i ListChatsAfterRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.CreatedAt,
|
||||
&i.MemberIds,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listChatsFirstPage = `-- name: ListChatsFirstPage :many
|
||||
SELECT
|
||||
c.id, c.title, c.created_at,
|
||||
ARRAY(
|
||||
SELECT cm.user_id FROM chat_members cm
|
||||
WHERE cm.chat_id = c.id
|
||||
ORDER BY cm.user_id
|
||||
)::uuid[] AS member_ids
|
||||
FROM chats c
|
||||
JOIN chat_members m ON m.chat_id = c.id
|
||||
WHERE m.user_id = $1
|
||||
ORDER BY c.created_at, c.id
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListChatsFirstPageParams struct {
|
||||
UserID uuid.UUID
|
||||
Lim int
|
||||
}
|
||||
|
||||
type ListChatsFirstPageRow struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
MemberIds []uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) ListChatsFirstPage(ctx context.Context, arg ListChatsFirstPageParams) ([]ListChatsFirstPageRow, error) {
|
||||
rows, err := q.db.Query(ctx, listChatsFirstPage, arg.UserID, arg.Lim)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []ListChatsFirstPageRow{}
|
||||
for rows.Next() {
|
||||
var i ListChatsFirstPageRow
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.Title,
|
||||
&i.CreatedAt,
|
||||
&i.MemberIds,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
type DBTX interface {
|
||||
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||
}
|
||||
|
||||
func New(db DBTX) *Queries {
|
||||
return &Queries{db: db}
|
||||
}
|
||||
|
||||
type Queries struct {
|
||||
db DBTX
|
||||
}
|
||||
|
||||
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||
return &Queries{
|
||||
db: tx,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: idempotency_keys.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createIdempotencyKey = `-- name: CreateIdempotencyKey :execrows
|
||||
INSERT INTO idempotency_keys (
|
||||
user_id,
|
||||
method,
|
||||
path,
|
||||
key,
|
||||
request_hash,
|
||||
message_id
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4,
|
||||
$5,
|
||||
$6
|
||||
)
|
||||
ON CONFLICT DO NOTHING
|
||||
`
|
||||
|
||||
type CreateIdempotencyKeyParams struct {
|
||||
UserID uuid.UUID
|
||||
Method string
|
||||
Path string
|
||||
Key string
|
||||
RequestHash string
|
||||
MessageID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CreateIdempotencyKey(ctx context.Context, arg CreateIdempotencyKeyParams) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, createIdempotencyKey,
|
||||
arg.UserID,
|
||||
arg.Method,
|
||||
arg.Path,
|
||||
arg.Key,
|
||||
arg.RequestHash,
|
||||
arg.MessageID,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deleteExpiredIdempotencyKeys = `-- name: DeleteExpiredIdempotencyKeys :execrows
|
||||
DELETE FROM idempotency_keys
|
||||
WHERE created_at < $1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteExpiredIdempotencyKeys(ctx context.Context, cutoff time.Time) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteExpiredIdempotencyKeys, cutoff)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getIdempotentMessage = `-- name: GetIdempotentMessage :one
|
||||
SELECT
|
||||
k.request_hash,
|
||||
m.id,
|
||||
m.chat_id,
|
||||
m.sender_id,
|
||||
m.text,
|
||||
m.created_at
|
||||
FROM idempotency_keys AS k
|
||||
JOIN messages AS m ON m.id = k.message_id
|
||||
WHERE k.user_id = $1
|
||||
AND k.method = $2
|
||||
AND k.path = $3
|
||||
AND k.key = $4
|
||||
`
|
||||
|
||||
type GetIdempotentMessageParams struct {
|
||||
UserID uuid.UUID
|
||||
Method string
|
||||
Path string
|
||||
Key string
|
||||
}
|
||||
|
||||
type GetIdempotentMessageRow struct {
|
||||
RequestHash string
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) GetIdempotentMessage(ctx context.Context, arg GetIdempotentMessageParams) (GetIdempotentMessageRow, error) {
|
||||
row := q.db.QueryRow(ctx, getIdempotentMessage,
|
||||
arg.UserID,
|
||||
arg.Method,
|
||||
arg.Path,
|
||||
arg.Key,
|
||||
)
|
||||
var i GetIdempotentMessageRow
|
||||
err := row.Scan(
|
||||
&i.RequestHash,
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.SenderID,
|
||||
&i.Text,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: messages.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createMessage = `-- name: CreateMessage :one
|
||||
INSERT INTO messages (
|
||||
id,
|
||||
chat_id,
|
||||
sender_id,
|
||||
text
|
||||
)
|
||||
SELECT
|
||||
$1::uuid,
|
||||
$2::uuid,
|
||||
$3::uuid,
|
||||
$4::text
|
||||
WHERE EXISTS (
|
||||
SELECT 1 FROM chat_members
|
||||
WHERE chat_id = $2
|
||||
AND user_id = $3
|
||||
)
|
||||
RETURNING id, chat_id, sender_id, text, created_at
|
||||
`
|
||||
|
||||
type CreateMessageParams struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
}
|
||||
|
||||
func (q *Queries) CreateMessage(ctx context.Context, arg CreateMessageParams) (Message, error) {
|
||||
row := q.db.QueryRow(ctx, createMessage,
|
||||
arg.ID,
|
||||
arg.ChatID,
|
||||
arg.SenderID,
|
||||
arg.Text,
|
||||
)
|
||||
var i Message
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.SenderID,
|
||||
&i.Text,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const listMessagesAfter = `-- name: ListMessagesAfter :many
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
||||
WHERE chat_id = $1
|
||||
AND (created_at, id) > (
|
||||
$2::timestamptz,
|
||||
$3::uuid
|
||||
)
|
||||
ORDER BY created_at, id
|
||||
LIMIT $4
|
||||
`
|
||||
|
||||
type ListMessagesAfterParams struct {
|
||||
ChatID uuid.UUID
|
||||
AfterCreatedAt time.Time
|
||||
AfterID uuid.UUID
|
||||
Lim int
|
||||
}
|
||||
|
||||
func (q *Queries) ListMessagesAfter(ctx context.Context, arg ListMessagesAfterParams) ([]Message, error) {
|
||||
rows, err := q.db.Query(ctx, listMessagesAfter,
|
||||
arg.ChatID,
|
||||
arg.AfterCreatedAt,
|
||||
arg.AfterID,
|
||||
arg.Lim,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Message{}
|
||||
for rows.Next() {
|
||||
var i Message
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.SenderID,
|
||||
&i.Text,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listMessagesFirstPage = `-- name: ListMessagesFirstPage :many
|
||||
SELECT id, chat_id, sender_id, text, created_at FROM messages
|
||||
WHERE chat_id = $1
|
||||
ORDER BY created_at, id
|
||||
LIMIT $2
|
||||
`
|
||||
|
||||
type ListMessagesFirstPageParams struct {
|
||||
ChatID uuid.UUID
|
||||
Lim int
|
||||
}
|
||||
|
||||
func (q *Queries) ListMessagesFirstPage(ctx context.Context, arg ListMessagesFirstPageParams) ([]Message, error) {
|
||||
rows, err := q.db.Query(ctx, listMessagesFirstPage, arg.ChatID, arg.Lim)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Message{}
|
||||
for rows.Next() {
|
||||
var i Message
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.ChatID,
|
||||
&i.SenderID,
|
||||
&i.Text,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
@@ -0,0 +1,247 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql/driver"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AttachmentContentType string
|
||||
|
||||
const (
|
||||
AttachmentContentTypeImagePng AttachmentContentType = "image/png"
|
||||
AttachmentContentTypeImageJpeg AttachmentContentType = "image/jpeg"
|
||||
)
|
||||
|
||||
func (e *AttachmentContentType) Scan(src interface{}) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = AttachmentContentType(s)
|
||||
case string:
|
||||
*e = AttachmentContentType(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for AttachmentContentType: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullAttachmentContentType struct {
|
||||
AttachmentContentType AttachmentContentType
|
||||
Valid bool // Valid is true if AttachmentContentType is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullAttachmentContentType) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
ns.AttachmentContentType, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.AttachmentContentType.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullAttachmentContentType) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.AttachmentContentType), nil
|
||||
}
|
||||
|
||||
type AttachmentStatus string
|
||||
|
||||
const (
|
||||
AttachmentStatusQueued AttachmentStatus = "queued"
|
||||
AttachmentStatusProcessing AttachmentStatus = "processing"
|
||||
AttachmentStatusReady AttachmentStatus = "ready"
|
||||
AttachmentStatusFailed AttachmentStatus = "failed"
|
||||
)
|
||||
|
||||
func (e *AttachmentStatus) Scan(src interface{}) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = AttachmentStatus(s)
|
||||
case string:
|
||||
*e = AttachmentStatus(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for AttachmentStatus: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullAttachmentStatus struct {
|
||||
AttachmentStatus AttachmentStatus
|
||||
Valid bool // Valid is true if AttachmentStatus is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullAttachmentStatus) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
ns.AttachmentStatus, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.AttachmentStatus.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullAttachmentStatus) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.AttachmentStatus), nil
|
||||
}
|
||||
|
||||
type AttachmentVariantName string
|
||||
|
||||
const (
|
||||
AttachmentVariantNameThumbnail AttachmentVariantName = "thumbnail"
|
||||
AttachmentVariantNameCrop AttachmentVariantName = "crop"
|
||||
AttachmentVariantNameOptimized AttachmentVariantName = "optimized"
|
||||
)
|
||||
|
||||
func (e *AttachmentVariantName) Scan(src interface{}) error {
|
||||
switch s := src.(type) {
|
||||
case []byte:
|
||||
*e = AttachmentVariantName(s)
|
||||
case string:
|
||||
*e = AttachmentVariantName(s)
|
||||
default:
|
||||
return fmt.Errorf("unsupported scan type for AttachmentVariantName: %T", src)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type NullAttachmentVariantName struct {
|
||||
AttachmentVariantName AttachmentVariantName
|
||||
Valid bool // Valid is true if AttachmentVariantName is not NULL
|
||||
}
|
||||
|
||||
// Scan implements the Scanner interface.
|
||||
func (ns *NullAttachmentVariantName) Scan(value interface{}) error {
|
||||
if value == nil {
|
||||
ns.AttachmentVariantName, ns.Valid = "", false
|
||||
return nil
|
||||
}
|
||||
ns.Valid = true
|
||||
return ns.AttachmentVariantName.Scan(value)
|
||||
}
|
||||
|
||||
// Value implements the driver Valuer interface.
|
||||
func (ns NullAttachmentVariantName) Value() (driver.Value, error) {
|
||||
if !ns.Valid {
|
||||
return nil, nil
|
||||
}
|
||||
return string(ns.AttachmentVariantName), nil
|
||||
}
|
||||
|
||||
type Attachment struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
S3Key string
|
||||
Filename string
|
||||
ContentType AttachmentContentType
|
||||
SizeBytes int
|
||||
Sha256 string
|
||||
Status AttachmentStatus
|
||||
ErrorCode *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
StagingKey *string
|
||||
JobID *uuid.UUID
|
||||
PipelineVersion int
|
||||
Attempts int
|
||||
RetryBase int
|
||||
Width *int32
|
||||
Height *int32
|
||||
LeaseUntil *time.Time
|
||||
}
|
||||
|
||||
type AttachmentReplay struct {
|
||||
AttachmentID uuid.UUID
|
||||
Attempt int
|
||||
PreviousErrorCode *string
|
||||
Operator string
|
||||
Reason string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type AttachmentVariant struct {
|
||||
AttachmentID uuid.UUID
|
||||
PipelineVersion int
|
||||
Variant AttachmentVariantName
|
||||
S3Key string
|
||||
ContentType string
|
||||
SizeBytes int
|
||||
Sha256 string
|
||||
Width int
|
||||
Height int
|
||||
}
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type ChatMember struct {
|
||||
ChatID uuid.UUID
|
||||
UserID uuid.UUID
|
||||
}
|
||||
|
||||
type IdempotencyKey struct {
|
||||
UserID uuid.UUID
|
||||
Method string
|
||||
Path string
|
||||
Key string
|
||||
RequestHash string
|
||||
MessageID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type Message struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
type MessageAttachment struct {
|
||||
MessageID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
Position int
|
||||
}
|
||||
|
||||
type Outbox struct {
|
||||
ID int64
|
||||
JobID uuid.UUID
|
||||
AttachmentID uuid.UUID
|
||||
PipelineVersion int
|
||||
Attempt int
|
||||
CreatedAt time.Time
|
||||
AvailableAt time.Time
|
||||
PublishedAt *time.Time
|
||||
}
|
||||
|
||||
type Session struct {
|
||||
TokenHash string
|
||||
UserID uuid.UUID
|
||||
CreatedAt time.Time
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,108 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: outbox.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createOutboxEntry = `-- name: CreateOutboxEntry :one
|
||||
INSERT INTO outbox (
|
||||
job_id,
|
||||
attachment_id,
|
||||
pipeline_version,
|
||||
attempt,
|
||||
available_at
|
||||
)
|
||||
SELECT
|
||||
job_id,
|
||||
id,
|
||||
pipeline_version,
|
||||
$1::int,
|
||||
$2::timestamptz
|
||||
FROM attachments
|
||||
WHERE id = $3::uuid
|
||||
AND job_id IS NOT NULL
|
||||
RETURNING id, job_id, attachment_id, pipeline_version, attempt, created_at, available_at, published_at
|
||||
`
|
||||
|
||||
type CreateOutboxEntryParams struct {
|
||||
Attempt int
|
||||
AvailableAt time.Time
|
||||
AttachmentID uuid.UUID
|
||||
}
|
||||
|
||||
func (q *Queries) CreateOutboxEntry(ctx context.Context, arg CreateOutboxEntryParams) (Outbox, error) {
|
||||
row := q.db.QueryRow(ctx, createOutboxEntry, arg.Attempt, arg.AvailableAt, arg.AttachmentID)
|
||||
var i Outbox
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.JobID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempt,
|
||||
&i.CreatedAt,
|
||||
&i.AvailableAt,
|
||||
&i.PublishedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const lockPendingOutboxEntries = `-- name: LockPendingOutboxEntries :many
|
||||
SELECT id, job_id, attachment_id, pipeline_version, attempt, created_at, available_at, published_at FROM outbox
|
||||
WHERE published_at IS NULL
|
||||
AND available_at <= now()
|
||||
ORDER BY available_at, id
|
||||
LIMIT $1::int
|
||||
FOR UPDATE SKIP LOCKED
|
||||
`
|
||||
|
||||
func (q *Queries) LockPendingOutboxEntries(ctx context.Context, batchSize int) ([]Outbox, error) {
|
||||
rows, err := q.db.Query(ctx, lockPendingOutboxEntries, batchSize)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
items := []Outbox{}
|
||||
for rows.Next() {
|
||||
var i Outbox
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.JobID,
|
||||
&i.AttachmentID,
|
||||
&i.PipelineVersion,
|
||||
&i.Attempt,
|
||||
&i.CreatedAt,
|
||||
&i.AvailableAt,
|
||||
&i.PublishedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const markOutboxPublished = `-- name: MarkOutboxPublished :execrows
|
||||
UPDATE outbox
|
||||
SET published_at = now()
|
||||
WHERE id = $1::bigint
|
||||
AND published_at IS NULL
|
||||
`
|
||||
|
||||
func (q *Queries) MarkOutboxPublished(ctx context.Context, id int64) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, markOutboxPublished, id)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: sessions.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createSession = `-- name: CreateSession :exec
|
||||
INSERT INTO sessions (
|
||||
token_hash,
|
||||
user_id,
|
||||
expires_at
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3
|
||||
)
|
||||
`
|
||||
|
||||
type CreateSessionParams struct {
|
||||
TokenHash string
|
||||
UserID uuid.UUID
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) CreateSession(ctx context.Context, arg CreateSessionParams) error {
|
||||
_, err := q.db.Exec(ctx, createSession, arg.TokenHash, arg.UserID, arg.ExpiresAt)
|
||||
return err
|
||||
}
|
||||
|
||||
const deleteExpiredSessions = `-- name: DeleteExpiredSessions :execrows
|
||||
DELETE FROM sessions
|
||||
WHERE expires_at <= now()
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteExpiredSessions(ctx context.Context) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteExpiredSessions)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const deleteSession = `-- name: DeleteSession :execrows
|
||||
DELETE FROM sessions
|
||||
WHERE token_hash = $1
|
||||
AND expires_at > now()
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteSession(ctx context.Context, tokenHash string) (int64, error) {
|
||||
result, err := q.db.Exec(ctx, deleteSession, tokenHash)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return result.RowsAffected(), nil
|
||||
}
|
||||
|
||||
const getActiveSession = `-- name: GetActiveSession :one
|
||||
SELECT user_id, expires_at FROM sessions
|
||||
WHERE token_hash = $1
|
||||
AND expires_at > now()
|
||||
`
|
||||
|
||||
type GetActiveSessionRow struct {
|
||||
UserID uuid.UUID
|
||||
ExpiresAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) GetActiveSession(ctx context.Context, tokenHash string) (GetActiveSessionRow, error) {
|
||||
row := q.db.QueryRow(ctx, getActiveSession, tokenHash)
|
||||
var i GetActiveSessionRow
|
||||
err := row.Scan(&i.UserID, &i.ExpiresAt)
|
||||
return i, err
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
// Code generated by sqlc. DO NOT EDIT.
|
||||
// versions:
|
||||
// sqlc v1.31.1
|
||||
// source: users.sql
|
||||
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const createUser = `-- name: CreateUser :one
|
||||
INSERT INTO users (
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
password_hash
|
||||
) VALUES (
|
||||
$1,
|
||||
$2,
|
||||
$3,
|
||||
$4
|
||||
)
|
||||
RETURNING id, username, display_name, created_at
|
||||
`
|
||||
|
||||
type CreateUserParams struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
PasswordHash string
|
||||
}
|
||||
|
||||
type CreateUserRow struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (CreateUserRow, error) {
|
||||
row := q.db.QueryRow(ctx, createUser,
|
||||
arg.ID,
|
||||
arg.Username,
|
||||
arg.DisplayName,
|
||||
arg.PasswordHash,
|
||||
)
|
||||
var i CreateUserRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.DisplayName,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUser = `-- name: GetUser :one
|
||||
SELECT id, username, display_name, created_at FROM users
|
||||
WHERE id = $1
|
||||
`
|
||||
|
||||
type GetUserRow struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
|
||||
func (q *Queries) GetUser(ctx context.Context, id uuid.UUID) (GetUserRow, error) {
|
||||
row := q.db.QueryRow(ctx, getUser, id)
|
||||
var i GetUserRow
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.DisplayName,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
|
||||
const getUserByUsername = `-- name: GetUserByUsername :one
|
||||
SELECT id, username, display_name, password_hash, created_at FROM users
|
||||
WHERE username = $1
|
||||
`
|
||||
|
||||
func (q *Queries) GetUserByUsername(ctx context.Context, username string) (User, error) {
|
||||
row := q.db.QueryRow(ctx, getUserByUsername, username)
|
||||
var i User
|
||||
err := row.Scan(
|
||||
&i.ID,
|
||||
&i.Username,
|
||||
&i.DisplayName,
|
||||
&i.PasswordHash,
|
||||
&i.CreatedAt,
|
||||
)
|
||||
return i, err
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/model"
|
||||
)
|
||||
|
||||
type CreateAttachmentRequest struct {
|
||||
Filename string
|
||||
}
|
||||
|
||||
type GetAttachmentContentRequest struct {
|
||||
Variant string
|
||||
}
|
||||
|
||||
type VariantResponse struct {
|
||||
Name model.AttachmentVariantName `json:"name"`
|
||||
ContentType model.ContentType `json:"content_type"`
|
||||
SizeBytes int `json:"size_bytes"`
|
||||
Width int `json:"width"`
|
||||
Height int `json:"height"`
|
||||
Sha256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type AttachmentResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ChatID uuid.UUID `json:"chat_id"`
|
||||
OwnerID uuid.UUID `json:"owner_id"`
|
||||
Filename string `json:"filename"`
|
||||
ContentType model.ContentType `json:"content_type"`
|
||||
SizeBytes int `json:"size_bytes"`
|
||||
Sha256 string `json:"sha256"`
|
||||
Status model.AttachmentStatus `json:"status"`
|
||||
Variants []VariantResponse `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (r CreateAttachmentRequest) Validate() bool {
|
||||
if n := utf8.RuneCountInString(r.Filename); n < 1 || n > 255 {
|
||||
return false
|
||||
}
|
||||
|
||||
for _, ch := range r.Filename {
|
||||
if ch == '/' || ch == '\\' || ch == 0 || unicode.IsControl(ch) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (r GetAttachmentContentRequest) Validate() bool {
|
||||
switch model.AttachmentVariantName(r.Variant) {
|
||||
case "",
|
||||
model.AttachmentContentOriginal,
|
||||
model.AttachmentVariantNameThumbnail,
|
||||
model.AttachmentVariantNameCrop,
|
||||
model.AttachmentVariantNameOptimized:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func ToAttachmentResponse(a model.Attachment) AttachmentResponse {
|
||||
variants := make([]VariantResponse, 0, len(a.Variants))
|
||||
for _, v := range a.Variants {
|
||||
variants = append(variants, VariantResponse{
|
||||
Name: v.Name,
|
||||
ContentType: v.ContentType,
|
||||
SizeBytes: v.SizeBytes,
|
||||
Width: v.Width,
|
||||
Height: v.Height,
|
||||
Sha256: v.Sha256,
|
||||
})
|
||||
}
|
||||
return AttachmentResponse{
|
||||
ID: a.ID,
|
||||
ChatID: a.ChatID,
|
||||
OwnerID: a.OwnerID,
|
||||
Filename: a.Filename,
|
||||
ContentType: a.ContentType,
|
||||
SizeBytes: a.SizeBytes,
|
||||
Sha256: a.Sha256,
|
||||
Status: a.Status,
|
||||
Variants: variants,
|
||||
ErrorCode: a.ErrorCode,
|
||||
CreatedAt: a.CreatedAt,
|
||||
UpdatedAt: a.UpdatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CreateChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []uuid.UUID `json:"member_ids"`
|
||||
}
|
||||
|
||||
type ChatResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []uuid.UUID `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type ChatPageResponse struct {
|
||||
Items []ChatResponse `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
func (r CreateChatRequest) Validate() bool {
|
||||
if strings.TrimSpace(r.Title) == "" ||
|
||||
strings.ContainsRune(r.Title, 0) ||
|
||||
utf8.RuneCountInString(r.Title) > 100 {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(r.MemberIDs) < 1 || len(r.MemberIDs) > 100 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func ToChatResponse(c model.Chat) ChatResponse {
|
||||
return ChatResponse{
|
||||
ID: c.ID,
|
||||
Title: c.Title,
|
||||
MemberIDs: c.MemberIDs,
|
||||
CreatedAt: c.CreatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package dto
|
||||
|
||||
type ErrorCode string
|
||||
|
||||
const (
|
||||
ErrorCodeInvalidRequest ErrorCode = "invalid_request"
|
||||
ErrorCodeUnauthorized ErrorCode = "unauthorized"
|
||||
ErrorCodeNotFound ErrorCode = "not_found"
|
||||
ErrorCodeConflict ErrorCode = "conflict"
|
||||
ErrorCodePayloadTooLarge ErrorCode = "payload_too_large"
|
||||
ErrorCodeUnsupportedMediaType ErrorCode = "unsupported_media_type"
|
||||
ErrorCodeRateLimited ErrorCode = "rate_limited"
|
||||
ErrorCodeUnavailable ErrorCode = "unavailable"
|
||||
ErrorCodeInternalError ErrorCode = "internal_error"
|
||||
)
|
||||
|
||||
type ErrorResponse struct {
|
||||
Error Error `json:"error"`
|
||||
}
|
||||
|
||||
type Error struct {
|
||||
Code ErrorCode `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package dto
|
||||
|
||||
type HealthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CreateMessageRequest struct {
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []string `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
type MessageResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ChatID uuid.UUID `json:"chat_id"`
|
||||
SenderID uuid.UUID `json:"sender_id"`
|
||||
Text string `json:"text"`
|
||||
AttachmentIDs []uuid.UUID `json:"attachment_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type MessagePageResponse struct {
|
||||
Items []MessageResponse `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
func (r CreateMessageRequest) Validate() bool {
|
||||
if strings.ContainsRune(r.Text, 0) || utf8.RuneCountInString(r.Text) > 4000 {
|
||||
return false
|
||||
}
|
||||
if len(r.AttachmentIDs) > 10 {
|
||||
return false
|
||||
}
|
||||
|
||||
seen := make(map[string]struct{}, len(r.AttachmentIDs))
|
||||
for _, id := range r.AttachmentIDs {
|
||||
if _, ok := seen[id]; ok {
|
||||
return false
|
||||
}
|
||||
seen[id] = struct{}{}
|
||||
}
|
||||
|
||||
hasText := strings.TrimSpace(r.Text) != ""
|
||||
hasAttachment := len(r.AttachmentIDs) > 0
|
||||
|
||||
return hasText || hasAttachment
|
||||
}
|
||||
|
||||
func ToMessageResponse(m model.Message) MessageResponse {
|
||||
ids := make([]uuid.UUID, len(m.AttachmentIDs))
|
||||
copy(ids, m.AttachmentIDs)
|
||||
return MessageResponse{
|
||||
ID: m.ID,
|
||||
ChatID: m.ChatID,
|
||||
SenderID: m.SenderID,
|
||||
Text: m.Text,
|
||||
AttachmentIDs: ids,
|
||||
CreatedAt: m.CreatedAt,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package dto
|
||||
|
||||
import "time"
|
||||
|
||||
type SessionResponse struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
User UserResponse `json:"user"`
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package dto
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
type CreateUserRequest struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type UserResponse struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (r CreateUserRequest) Validate() bool {
|
||||
if !usernamePattern.MatchString(r.Username) {
|
||||
return false
|
||||
}
|
||||
|
||||
displayNameLength := utf8.RuneCountInString(r.DisplayName)
|
||||
if strings.TrimSpace(r.DisplayName) == "" ||
|
||||
displayNameLength > 100 ||
|
||||
strings.ContainsRune(r.DisplayName, 0) {
|
||||
return false
|
||||
}
|
||||
|
||||
if n := utf8.RuneCountInString(r.Password); n < 12 || n > 128 {
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type AttachmentHandler struct {
|
||||
service *service.AttachmentService
|
||||
maxBytes int
|
||||
}
|
||||
|
||||
func NewAttachmentHandler(service *service.AttachmentService, maxBytes int) *AttachmentHandler {
|
||||
return &AttachmentHandler{
|
||||
service: service,
|
||||
maxBytes: maxBytes,
|
||||
}
|
||||
}
|
||||
|
||||
func (h *AttachmentHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid chat_id", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
req := dto.CreateAttachmentRequest{
|
||||
Filename: r.URL.Query().Get("filename"),
|
||||
}
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid filename", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
if deadline, ok := r.Context().Deadline(); ok {
|
||||
controller := http.NewResponseController(w)
|
||||
if err := controller.SetReadDeadline(deadline); err != nil {
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
defer func() { _ = controller.SetReadDeadline(time.Time{}) }()
|
||||
}
|
||||
|
||||
contentType := r.Header.Get("Content-Type")
|
||||
r.Body = http.MaxBytesReader(w, r.Body, int64(h.maxBytes))
|
||||
|
||||
att, err := h.service.Upload(r.Context(), userID, chatID, req.Filename, r.Body, contentType)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrUploadReadTimeout):
|
||||
r.Close = true
|
||||
w.Header().Set("Connection", "close")
|
||||
httpx.WriteError(w, http.StatusRequestTimeout, dto.ErrorCodeInvalidRequest, "upload body read timed out", requestID)
|
||||
return
|
||||
case errors.Is(err, service.ErrInvalidSize):
|
||||
httpx.WriteError(w, http.StatusRequestEntityTooLarge, dto.ErrorCodePayloadTooLarge, "file too large", requestID)
|
||||
return
|
||||
case errors.Is(err, service.ErrInvalidImage):
|
||||
httpx.WriteError(w, http.StatusUnsupportedMediaType, dto.ErrorCodeUnsupportedMediaType, "invalid image", requestID)
|
||||
return
|
||||
case errors.Is(err, service.ErrAttachmentNotFound):
|
||||
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusAccepted, dto.ToAttachmentResponse(att))
|
||||
}
|
||||
|
||||
func (h *AttachmentHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
attachmentID, err := uuid.Parse(chi.URLParam(r, "attachment_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid attachment_id", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
att, err := h.service.Get(r.Context(), userID, attachmentID)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrAttachmentNotFound):
|
||||
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.ToAttachmentResponse(att))
|
||||
}
|
||||
|
||||
func (h *AttachmentHandler) GetContent(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
attachmentID, err := uuid.Parse(chi.URLParam(r, "attachment_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid attachment_id", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
req := dto.GetAttachmentContentRequest{
|
||||
Variant: r.URL.Query().Get("variant"),
|
||||
}
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
||||
return
|
||||
}
|
||||
|
||||
data, contentType, err := h.service.GetContent(r.Context(), userID, attachmentID, req.Variant)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrAttachmentNotReady):
|
||||
httpx.WriteError(w, http.StatusConflict, dto.ErrorCodeConflict, "attachment not ready", requestID)
|
||||
return
|
||||
case errors.Is(err, service.ErrAttachmentNotFound):
|
||||
httpx.WriteError(w, http.StatusNotFound, dto.ErrorCodeNotFound, "not found", requestID)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", string(contentType))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = w.Write(data)
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type AuthHandler struct {
|
||||
service *service.AuthService
|
||||
}
|
||||
|
||||
func NewAuthHandler(s *service.AuthService) *AuthHandler {
|
||||
return &AuthHandler{service: s}
|
||||
}
|
||||
|
||||
func (h *AuthHandler) CreateSession(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
|
||||
username, plain, ok := r.BasicAuth()
|
||||
if !ok {
|
||||
httpx.WriteUnauthorized(w, httpx.BasicChallenge, requestID)
|
||||
return
|
||||
}
|
||||
|
||||
if !httpx.RejectBody(w, r, requestID) {
|
||||
return
|
||||
}
|
||||
|
||||
session, err := h.service.Login(r.Context(), username, plain)
|
||||
if errors.Is(err, service.ErrInvalidCredentials) {
|
||||
httpx.WriteUnauthorized(w, httpx.BasicChallenge, requestID)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusCreated, dto.SessionResponse{
|
||||
Token: session.Token,
|
||||
TokenType: "Bearer",
|
||||
ExpiresAt: session.ExpiresAt,
|
||||
User: dto.UserResponse{
|
||||
ID: session.User.ID,
|
||||
Username: session.User.Username,
|
||||
DisplayName: session.User.DisplayName,
|
||||
CreatedAt: session.User.CreatedAt,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func (h *AuthHandler) RevokeSession(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
|
||||
token, ok := httpx.BearerToken(r)
|
||||
if !ok {
|
||||
httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID)
|
||||
return
|
||||
}
|
||||
|
||||
err := h.service.Logout(r.Context(), token)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrSessionCacheUnavailable):
|
||||
httpx.WriteError(w, http.StatusServiceUnavailable, dto.ErrorCodeUnavailable, "session cache unavailable", requestID)
|
||||
return
|
||||
case errors.Is(err, service.ErrUnauthorized):
|
||||
httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
@@ -0,0 +1,150 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type ChatHandler struct {
|
||||
service *service.ChatService
|
||||
}
|
||||
|
||||
func NewChatHandler(service *service.ChatService) *ChatHandler {
|
||||
return &ChatHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
var req dto.CreateChatRequest
|
||||
if !httpx.DecodeJSONBody(w, r, requestID, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid request body",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.service.Create(r.Context(), userID, req.Title, req.MemberIDs)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrNotChatMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"caller must be a member of member_ids",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrDuplicateMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"member_ids contains duplicates",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrUnknownMember):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"member_ids contains unknown user",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusCreated, dto.ToChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *ChatHandler) Get(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid chat_id",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
chat, err := h.service.Get(r.Context(), userID, chatID)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrChatNotFound):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"chat not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.ToChatResponse(chat))
|
||||
}
|
||||
|
||||
func (h *ChatHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
limit, cursor, ok := httpx.ParsePagination(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
chats, nextCursor, err := h.service.List(r.Context(), userID, limit, cursor)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrInvalidCursor):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid cursor",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]dto.ChatResponse, 0, len(chats))
|
||||
for _, c := range chats {
|
||||
items = append(items, dto.ToChatResponse(c))
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.ChatPageResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
)
|
||||
|
||||
const readyPingTimeout = 2 * time.Second
|
||||
|
||||
type Pinger interface {
|
||||
Ping(ctx context.Context) error
|
||||
}
|
||||
|
||||
type HealthHandler struct {
|
||||
log *slog.Logger
|
||||
dependencies []Pinger
|
||||
}
|
||||
|
||||
func NewHealthHandler(log *slog.Logger, dependencies ...Pinger) *HealthHandler {
|
||||
return &HealthHandler{log: log, dependencies: dependencies}
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Live(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
_ = json.NewEncoder(w).Encode(dto.HealthResponse{
|
||||
Status: "ok",
|
||||
})
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Ready(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), readyPingTimeout)
|
||||
defer cancel()
|
||||
|
||||
for _, dependency := range h.dependencies {
|
||||
if err := dependency.Ping(ctx); err != nil {
|
||||
h.log.ErrorContext(r.Context(), "readiness check failed", "error", err)
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusServiceUnavailable,
|
||||
dto.ErrorCodeUnavailable,
|
||||
"service unavailable",
|
||||
w.Header().Get("X-Request-Id"),
|
||||
)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
|
||||
_ = json.NewEncoder(w).Encode(dto.HealthResponse{
|
||||
Status: "ok",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"regexp"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
var idempotencyKeyReq = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
type MessageHandler struct {
|
||||
service *service.MessageService
|
||||
}
|
||||
|
||||
func NewMessageHandler(service *service.MessageService) *MessageHandler {
|
||||
return &MessageHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *MessageHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid chat_id",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
key := r.Header.Get("Idempotency-Key")
|
||||
if !idempotencyKeyReq.MatchString(key) {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid Idempotency-Key",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
var req dto.CreateMessageRequest
|
||||
if !httpx.DecodeJSONBody(w, r, requestID, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid request body",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
attachmentIDs := make([]uuid.UUID, 0, len(req.AttachmentIDs))
|
||||
for _, s := range req.AttachmentIDs {
|
||||
id, err := uuid.Parse(s)
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid attachment_id",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
attachmentIDs = append(attachmentIDs, id)
|
||||
}
|
||||
|
||||
message, err := h.service.Create(r.Context(), userID, chatID, key, req.Text, attachmentIDs)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrMessageChatNotFound):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"chat not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrAttachmentNotFound):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"attachment not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrAttachmentNotReady):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusConflict,
|
||||
dto.ErrorCodeConflict,
|
||||
"attachment not ready",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrIdempotencyKeyConflict):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusConflict,
|
||||
dto.ErrorCodeConflict,
|
||||
"idempotency key reused with different request",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusCreated, dto.ToMessageResponse(message))
|
||||
}
|
||||
|
||||
func (h *MessageHandler) List(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
chatID, err := uuid.Parse(chi.URLParam(r, "chat_id"))
|
||||
if err != nil {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid chat_id",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
limit, cursor, ok := httpx.ParsePagination(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
messages, nextCursor, err := h.service.List(r.Context(), userID, chatID, limit, cursor)
|
||||
switch {
|
||||
case errors.Is(err, service.ErrMessageChatNotFound):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"chat not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case errors.Is(err, service.ErrInvalidCursor):
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid cursor",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
case err != nil:
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
items := make([]dto.MessageResponse, 0, len(messages))
|
||||
for _, m := range messages {
|
||||
items = append(items, dto.ToMessageResponse(m))
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.MessagePageResponse{
|
||||
Items: items,
|
||||
NextCursor: nextCursor,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/middleware"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type UserHandler struct {
|
||||
service *service.UserService
|
||||
}
|
||||
|
||||
func NewUserHandler(service *service.UserService) *UserHandler {
|
||||
return &UserHandler{service: service}
|
||||
}
|
||||
|
||||
func (h *UserHandler) Create(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
|
||||
var req dto.CreateUserRequest
|
||||
if !httpx.DecodeJSONBody(w, r, requestID, &req) {
|
||||
return
|
||||
}
|
||||
|
||||
if !req.Validate() {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"invalid request body",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
user, err := h.service.Create(
|
||||
r.Context(),
|
||||
req.Username,
|
||||
req.DisplayName,
|
||||
req.Password,
|
||||
)
|
||||
if errors.Is(err, service.ErrUsernameTaken) {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusConflict,
|
||||
dto.ErrorCodeConflict,
|
||||
"username already taken",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusCreated, dto.UserResponse{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
CreatedAt: user.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
func (h *UserHandler) Me(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := middleware.GetRequestID(r.Context())
|
||||
userID := middleware.GetUserID(r.Context())
|
||||
|
||||
user, err := h.service.GetByID(r.Context(), userID)
|
||||
if errors.Is(err, service.ErrUserNotFound) {
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusNotFound,
|
||||
dto.ErrorCodeNotFound,
|
||||
"user not found",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
httpx.WriteJSON(w, http.StatusOK, dto.UserResponse{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
CreatedAt: user.CreatedAt,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
const (
|
||||
BasicChallenge = `Basic realm="messenger", charset="UTF-8"`
|
||||
BearerChallenge = `Bearer realm="messenger"`
|
||||
)
|
||||
|
||||
func BearerToken(r *http.Request) (string, bool) {
|
||||
const prefix = "Bearer "
|
||||
|
||||
header := r.Header.Get("Authorization")
|
||||
if len(header) <= len(prefix) || !strings.EqualFold(header[:len(prefix)], prefix) {
|
||||
return "", false
|
||||
}
|
||||
|
||||
token := strings.TrimSpace(header[len(prefix):])
|
||||
|
||||
return token, token != ""
|
||||
}
|
||||
|
||||
func WriteUnauthorized(w http.ResponseWriter, challenge, requestID string) {
|
||||
w.Header().Set("WWW-Authenticate", challenge)
|
||||
WriteError(
|
||||
w,
|
||||
http.StatusUnauthorized,
|
||||
dto.ErrorCodeUnauthorized,
|
||||
"unauthorized",
|
||||
requestID,
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
func ParsePagination(w http.ResponseWriter, r *http.Request, requestID string) (limit int, cursor *string, ok bool) {
|
||||
q := r.URL.Query()
|
||||
|
||||
limit = 50
|
||||
if raw := q.Get("limit"); raw != "" {
|
||||
v, err := strconv.Atoi(raw)
|
||||
if err != nil || v < 1 || v > 100 {
|
||||
WriteError(
|
||||
w,
|
||||
http.StatusBadRequest,
|
||||
dto.ErrorCodeInvalidRequest,
|
||||
"limit must be an integer between 1 and 100",
|
||||
requestID,
|
||||
)
|
||||
return 0, nil, false
|
||||
}
|
||||
limit = v
|
||||
}
|
||||
|
||||
if raw := q.Get("cursor"); raw != "" {
|
||||
cursor = &raw
|
||||
}
|
||||
|
||||
return limit, cursor, true
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"mime"
|
||||
"net/http"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
)
|
||||
|
||||
func DecodeJSONBody(w http.ResponseWriter, r *http.Request, requestID string, dst any) bool {
|
||||
if ct := r.Header.Get("Content-Type"); ct != "" {
|
||||
mt, _, err := mime.ParseMediaType(ct)
|
||||
if err != nil || mt != "application/json" {
|
||||
WriteError(w, http.StatusUnsupportedMediaType, dto.ErrorCodeUnsupportedMediaType, "expected application/json", requestID)
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
|
||||
if err := decoder.Decode(dst); err != nil {
|
||||
WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID)
|
||||
return false
|
||||
}
|
||||
|
||||
if err := decoder.Decode(&struct{}{}); !errors.Is(err, io.EOF) {
|
||||
WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func RejectBody(w http.ResponseWriter, r *http.Request, requestID string) bool {
|
||||
if r.Body == nil || r.Body == http.NoBody {
|
||||
return true
|
||||
}
|
||||
|
||||
var b [1]byte
|
||||
if n, _ := r.Body.Read(b[:]); n > 0 {
|
||||
WriteError(w, http.StatusBadRequest, dto.ErrorCodeInvalidRequest, "invalid request body", requestID)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
s3store "mtusi2026-messenger/internal/s3"
|
||||
)
|
||||
|
||||
func WriteJSON(w http.ResponseWriter, status int, body any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
_ = json.NewEncoder(w).Encode(body)
|
||||
}
|
||||
|
||||
func WriteError(w http.ResponseWriter, status int, code dto.ErrorCode, message, requestID string) {
|
||||
WriteJSON(w, status, dto.ErrorResponse{
|
||||
Error: dto.Error{
|
||||
Code: code,
|
||||
Message: message,
|
||||
RequestID: requestID,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func WriteInternalError(w http.ResponseWriter, requestID string, err error) {
|
||||
if isUnavailable(err) {
|
||||
WriteError(
|
||||
w,
|
||||
http.StatusServiceUnavailable,
|
||||
dto.ErrorCodeUnavailable,
|
||||
"dependency unavailable",
|
||||
requestID,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
WriteError(
|
||||
w,
|
||||
http.StatusInternalServerError,
|
||||
dto.ErrorCodeInternalError,
|
||||
"internal server error",
|
||||
requestID,
|
||||
)
|
||||
}
|
||||
|
||||
func isUnavailable(err error) bool {
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, s3store.ErrUnavailable) {
|
||||
return true
|
||||
}
|
||||
|
||||
if _, ok := errors.AsType[net.Error](err); ok {
|
||||
return true
|
||||
}
|
||||
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
return strings.HasPrefix(pgErr.Code, "08") || pgErr.Code == "57P01"
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package logger
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"os"
|
||||
)
|
||||
|
||||
func New(level slog.Level) *slog.Logger {
|
||||
return slog.New(
|
||||
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
|
||||
Level: level,
|
||||
}),
|
||||
)
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
"mtusi2026-messenger/internal/service"
|
||||
)
|
||||
|
||||
type userIDContextKey struct{}
|
||||
|
||||
type authenticator interface {
|
||||
Authenticate(ctx context.Context, token string) (uuid.UUID, error)
|
||||
}
|
||||
|
||||
func Auth(auth authenticator) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := GetRequestID(r.Context())
|
||||
|
||||
token, ok := httpx.BearerToken(r)
|
||||
if !ok {
|
||||
httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID)
|
||||
return
|
||||
}
|
||||
|
||||
userID, err := auth.Authenticate(r.Context(), token)
|
||||
if errors.Is(err, service.ErrUnauthorized) {
|
||||
httpx.WriteUnauthorized(w, httpx.BearerChallenge, requestID)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
httpx.WriteInternalError(w, requestID, err)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), userIDContextKey{}, userID)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func GetUserID(ctx context.Context) uuid.UUID {
|
||||
id, _ := ctx.Value(userIDContextKey{}).(uuid.UUID)
|
||||
return id
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"mtusi2026-messenger/internal/dto"
|
||||
"mtusi2026-messenger/internal/httpx"
|
||||
)
|
||||
|
||||
func ConcurrencyLimit(max int) func(http.Handler) http.Handler {
|
||||
if max <= 0 {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return next
|
||||
}
|
||||
}
|
||||
|
||||
slots := make(chan struct{}, max)
|
||||
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
select {
|
||||
case slots <- struct{}{}:
|
||||
defer func() {
|
||||
<-slots
|
||||
}()
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
|
||||
default:
|
||||
w.Header().Set("Retry-After", "1")
|
||||
httpx.WriteError(
|
||||
w,
|
||||
http.StatusTooManyRequests,
|
||||
dto.ErrorCodeRateLimited,
|
||||
"too many requests",
|
||||
GetRequestID(r.Context()),
|
||||
)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func InstanceID(id string) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Instance-Id", id)
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
type responseWriter struct {
|
||||
http.ResponseWriter
|
||||
status int
|
||||
}
|
||||
|
||||
func (w *responseWriter) Unwrap() http.ResponseWriter {
|
||||
return w.ResponseWriter
|
||||
}
|
||||
|
||||
func (w *responseWriter) WriteHeader(status int) {
|
||||
w.status = status
|
||||
w.ResponseWriter.WriteHeader(status)
|
||||
}
|
||||
|
||||
func Logging(log *slog.Logger) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
|
||||
writer := &responseWriter{
|
||||
ResponseWriter: w,
|
||||
status: http.StatusOK,
|
||||
}
|
||||
|
||||
next.ServeHTTP(writer, r)
|
||||
|
||||
level := slog.LevelInfo
|
||||
|
||||
switch {
|
||||
case writer.status >= 500:
|
||||
level = slog.LevelError
|
||||
case writer.status >= 400:
|
||||
level = slog.LevelWarn
|
||||
}
|
||||
|
||||
log.Log(r.Context(), level, "http request",
|
||||
"request_id", GetRequestID(r.Context()),
|
||||
"method", r.Method,
|
||||
"path", r.URL.Path,
|
||||
"status", writer.status,
|
||||
"duration_ms", time.Since(start).Milliseconds(),
|
||||
)
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type requestIDContextKey struct{}
|
||||
|
||||
func RequestID(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := r.Header.Get("X-Request-Id")
|
||||
|
||||
if requestID == "" {
|
||||
requestID = uuid.NewString()
|
||||
}
|
||||
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
|
||||
ctx := context.WithValue(r.Context(), requestIDContextKey{}, requestID)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
|
||||
func GetRequestID(ctx context.Context) string {
|
||||
id, _ := ctx.Value(requestIDContextKey{}).(string)
|
||||
return id
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package middleware
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
func RequestTimeout(d time.Duration) func(http.Handler) http.Handler {
|
||||
return func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx, cancel := context.WithTimeout(r.Context(), d)
|
||||
defer cancel()
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ContentType string
|
||||
|
||||
const (
|
||||
ContentTypePNG ContentType = "image/png"
|
||||
ContentTypeJPEG ContentType = "image/jpeg"
|
||||
ContentTypeWebP ContentType = "image/webp"
|
||||
)
|
||||
|
||||
type AttachmentStatus string
|
||||
|
||||
const (
|
||||
AttachmentStatusQueued AttachmentStatus = "queued"
|
||||
AttachmentStatusProcessing AttachmentStatus = "processing"
|
||||
AttachmentStatusReady AttachmentStatus = "ready"
|
||||
AttachmentStatusFailed AttachmentStatus = "failed"
|
||||
)
|
||||
|
||||
const AttachmentContentOriginal = "original"
|
||||
|
||||
type AttachmentVariantName string
|
||||
|
||||
const (
|
||||
AttachmentVariantNameThumbnail AttachmentVariantName = "thumbnail"
|
||||
AttachmentVariantNameCrop AttachmentVariantName = "crop"
|
||||
AttachmentVariantNameOptimized AttachmentVariantName = "optimized"
|
||||
)
|
||||
|
||||
type AttachmentVariant struct {
|
||||
Name AttachmentVariantName
|
||||
ContentType ContentType
|
||||
SizeBytes int
|
||||
Width int
|
||||
Height int
|
||||
Sha256 string
|
||||
}
|
||||
|
||||
type Attachment struct {
|
||||
Variants []AttachmentVariant
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
OwnerID uuid.UUID
|
||||
Filename string
|
||||
ContentType ContentType
|
||||
SizeBytes int
|
||||
Sha256 string
|
||||
Status AttachmentStatus
|
||||
Width *int32
|
||||
Height *int32
|
||||
ErrorCode *string
|
||||
CreatedAt time.Time
|
||||
UpdatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type AttachmentJob struct {
|
||||
SchemaVersion int `json:"schema_version"`
|
||||
JobID uuid.UUID `json:"job_id"`
|
||||
Type string `json:"type"`
|
||||
AttachmentID uuid.UUID `json:"attachment_id"`
|
||||
PipelineVersion int `json:"pipeline_version"`
|
||||
Attempt int `json:"attempt"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
ID uuid.UUID
|
||||
Title string
|
||||
MemberIDs []uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type Message struct {
|
||||
ID uuid.UUID
|
||||
ChatID uuid.UUID
|
||||
SenderID uuid.UUID
|
||||
Text string
|
||||
AttachmentIDs []uuid.UUID
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
type Session struct {
|
||||
Token string
|
||||
ExpiresAt time.Time
|
||||
User User
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uuid.UUID
|
||||
Username string
|
||||
DisplayName string
|
||||
CreatedAt time.Time
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package outbox
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"mtusi2026-messenger/internal/rabbitmq"
|
||||
)
|
||||
|
||||
const (
|
||||
pollInterval = time.Second
|
||||
retryInterval = 2 * time.Second
|
||||
publishTimeout = 5 * time.Second
|
||||
)
|
||||
|
||||
type Publisher struct {
|
||||
pool *pgxpool.Pool
|
||||
url string
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func NewPublisher(pool *pgxpool.Pool, url string, log *slog.Logger) *Publisher {
|
||||
return &Publisher{
|
||||
pool: pool,
|
||||
url: url,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Publisher) Run(ctx context.Context) {
|
||||
for ctx.Err() == nil {
|
||||
client, err := rabbitmq.New(p.url)
|
||||
if err == nil {
|
||||
p.log.Info("outbox publisher connected")
|
||||
err = p.publish(ctx, client)
|
||||
_ = client.Close()
|
||||
}
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
p.log.Error("outbox publisher retry", "error", err)
|
||||
if !wait(ctx, retryInterval) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (p *Publisher) publish(ctx context.Context, client *rabbitmq.Client) error {
|
||||
for ctx.Err() == nil {
|
||||
published, err := p.publishOne(ctx, client)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !published && !wait(ctx, pollInterval) {
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
return ctx.Err()
|
||||
}
|
||||
|
||||
func (p *Publisher) publishOne(ctx context.Context, client *rabbitmq.Client) (bool, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, publishTimeout)
|
||||
defer cancel()
|
||||
|
||||
tx, err := p.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
q := db.New(tx)
|
||||
rows, err := q.LockPendingOutboxEntries(ctx, 1)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return false, nil
|
||||
}
|
||||
row := rows[0]
|
||||
body, err := json.Marshal(model.AttachmentJob{
|
||||
SchemaVersion: 1,
|
||||
JobID: row.JobID,
|
||||
Type: "attachment.process.v1",
|
||||
AttachmentID: row.AttachmentID,
|
||||
PipelineVersion: row.PipelineVersion,
|
||||
Attempt: row.Attempt,
|
||||
CreatedAt: row.CreatedAt.UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := client.Publish(ctx, row.JobID.String(), body); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if _, err := q.MarkOutboxPublished(ctx, row.ID); err != nil {
|
||||
return false, err
|
||||
}
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return false, err
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
|
||||
func wait(ctx context.Context, delay time.Duration) bool {
|
||||
timer := time.NewTimer(delay)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return false
|
||||
case <-timer.C:
|
||||
return true
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
const (
|
||||
CodeUniqueViolation = "23505"
|
||||
CodeForeignKeyViolation = "23503"
|
||||
CodeCheckViolation = "23514"
|
||||
)
|
||||
|
||||
func ErrCode(err error) string {
|
||||
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
|
||||
return pgErr.Code
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func IsNotFound(err error) bool {
|
||||
return errors.Is(err, pgx.ErrNoRows)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func New(ctx context.Context, url string) (*pgxpool.Pool, error) {
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse database url: %w", err)
|
||||
}
|
||||
|
||||
cfg.MaxConns = 10
|
||||
cfg.ConnConfig.ConnectTimeout = 5 * time.Second
|
||||
cfg.ConnConfig.RuntimeParams["statement_timeout"] = "5000"
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("create pool: %w", err)
|
||||
}
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
if err := pool.Ping(pingCtx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("ping database: %w", err)
|
||||
}
|
||||
|
||||
return pool, nil
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package rabbitmq
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
amqp "github.com/rabbitmq/amqp091-go"
|
||||
)
|
||||
|
||||
const QueueName = "attachments.process.v1"
|
||||
|
||||
var (
|
||||
ErrConnect = errors.New("connect to rabbitmq")
|
||||
ErrNotRouted = errors.New("rabbitmq message not routed")
|
||||
ErrNack = errors.New("rabbitmq publish rejected")
|
||||
ErrPrefetch = errors.New("rabbitmq prefetch must be positive")
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
conn *amqp.Connection
|
||||
channel *amqp.Channel
|
||||
returns <-chan amqp.Return
|
||||
}
|
||||
|
||||
func New(url string) (*Client, error) {
|
||||
conn, err := amqp.DialConfig(url, amqp.Config{
|
||||
Dial: amqp.DefaultDial(5 * time.Second),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, ErrConnect
|
||||
}
|
||||
|
||||
channel, err := conn.Channel()
|
||||
if err != nil {
|
||||
_ = conn.CloseDeadline(time.Now().Add(time.Second))
|
||||
return nil, err
|
||||
}
|
||||
if _, err := channel.QueueDeclare(QueueName, true, false, false, false, nil); err != nil {
|
||||
_ = conn.CloseDeadline(time.Now().Add(time.Second))
|
||||
return nil, err
|
||||
}
|
||||
if err := channel.Confirm(false); err != nil {
|
||||
_ = conn.CloseDeadline(time.Now().Add(time.Second))
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
conn: conn,
|
||||
channel: channel,
|
||||
returns: channel.NotifyReturn(make(chan amqp.Return, 1)),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// Publish must not be called concurrently on the same client.
|
||||
func (c *Client) Publish(ctx context.Context, messageID string, body []byte) error {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
stop := context.AfterFunc(ctx, func() {
|
||||
_ = c.conn.CloseDeadline(time.Now())
|
||||
})
|
||||
defer stop()
|
||||
|
||||
confirmation, err := c.channel.PublishWithDeferredConfirm("", QueueName, true, false, amqp.Publishing{
|
||||
ContentType: "application/json",
|
||||
DeliveryMode: amqp.Persistent,
|
||||
MessageId: messageID,
|
||||
Body: body,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ack, err := confirmation.WaitContext(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !ack {
|
||||
return ErrNack
|
||||
}
|
||||
|
||||
select {
|
||||
case _, ok := <-c.returns:
|
||||
if !ok {
|
||||
return amqp.ErrClosed
|
||||
}
|
||||
return ErrNotRouted
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) Consume(ctx context.Context, prefetch int) (<-chan amqp.Delivery, error) {
|
||||
if prefetch < 1 {
|
||||
return nil, ErrPrefetch
|
||||
}
|
||||
if err := c.channel.Qos(prefetch, 0, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return c.channel.ConsumeWithContext(ctx, QueueName, "", false, false, false, false, nil)
|
||||
}
|
||||
|
||||
func (c *Client) Close() error {
|
||||
return c.conn.CloseDeadline(time.Now().Add(time.Second))
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
s3store "mtusi2026-messenger/internal/s3"
|
||||
)
|
||||
|
||||
const (
|
||||
Prefix = "chats/"
|
||||
StagingPrefix = "staging/"
|
||||
DefaultStagingRetention = 24 * time.Hour
|
||||
DefaultGrace = time.Hour
|
||||
DefaultMaxDeletes = 100
|
||||
ErrorCodeObjectMissing = "object_missing"
|
||||
)
|
||||
|
||||
type Options struct {
|
||||
DryRun bool
|
||||
Grace time.Duration
|
||||
MaxDeletes int
|
||||
StagingRetention time.Duration
|
||||
}
|
||||
|
||||
type Report struct {
|
||||
ScannedObjects int `json:"scanned_objects"`
|
||||
ScannedRows int `json:"scanned_rows"`
|
||||
ListPages int `json:"list_pages"`
|
||||
StagingObjects []string `json:"staging_objects"`
|
||||
OrphanObjects []string `json:"orphan_objects"`
|
||||
MissingAttachments []uuid.UUID `json:"missing_attachments"`
|
||||
CorruptObjects []string `json:"corrupt_objects"`
|
||||
SkippedFresh int `json:"skipped_fresh"`
|
||||
SkippedInactive int `json:"skipped_inactive"`
|
||||
DeletedObjects int `json:"deleted_objects"`
|
||||
MarkedFailed int `json:"marked_failed"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
Errors []string `json:"errors"`
|
||||
DurationMS int64 `json:"duration_ms"`
|
||||
}
|
||||
|
||||
type ObjectClient interface {
|
||||
ListAll(ctx context.Context, prefix string) (s3store.ListResult, error)
|
||||
DeleteAdmin(ctx context.Context, key string) error
|
||||
}
|
||||
|
||||
type txBeginner interface {
|
||||
Begin(ctx context.Context) (pgx.Tx, error)
|
||||
}
|
||||
|
||||
type AttachmentQueries interface {
|
||||
LockAttachmentByStagingKey(ctx context.Context, stagingKey string) (db.Attachment, error)
|
||||
ClearAttachmentStagingKey(ctx context.Context, id uuid.UUID) error
|
||||
ListAttachmentsForReconcile(ctx context.Context) ([]db.ListAttachmentsForReconcileRow, error)
|
||||
MarkAttachmentFailedIfReady(ctx context.Context, arg db.MarkAttachmentFailedIfReadyParams) (int64, error)
|
||||
}
|
||||
|
||||
type Reconciler struct {
|
||||
pool txBeginner
|
||||
q AttachmentQueries
|
||||
withTx func(pgx.Tx) AttachmentQueries
|
||||
store ObjectClient
|
||||
log *slog.Logger
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
type plan struct {
|
||||
orphans []s3store.ObjectInfo
|
||||
staging []s3store.ObjectInfo
|
||||
missing []db.ListAttachmentsForReconcileRow
|
||||
corrupt []s3store.ObjectInfo
|
||||
skippedFresh int
|
||||
skippedInactive int
|
||||
}
|
||||
|
||||
func New(
|
||||
pool *pgxpool.Pool,
|
||||
store ObjectClient,
|
||||
queries *db.Queries,
|
||||
log *slog.Logger,
|
||||
) *Reconciler {
|
||||
if log == nil {
|
||||
log = slog.Default()
|
||||
}
|
||||
|
||||
return &Reconciler{
|
||||
pool: pool,
|
||||
withTx: func(tx pgx.Tx) AttachmentQueries {
|
||||
return queries.WithTx(tx)
|
||||
},
|
||||
store: store,
|
||||
q: queries,
|
||||
log: log,
|
||||
now: time.Now,
|
||||
}
|
||||
}
|
||||
|
||||
func (r *Reconciler) Run(ctx context.Context, opts Options) (Report, error) {
|
||||
opts = opts.withDefaults()
|
||||
started := r.now()
|
||||
|
||||
report := Report{
|
||||
OrphanObjects: []string{},
|
||||
StagingObjects: []string{},
|
||||
MissingAttachments: []uuid.UUID{},
|
||||
CorruptObjects: []string{},
|
||||
DryRun: opts.DryRun,
|
||||
Errors: []string{},
|
||||
}
|
||||
|
||||
listed, err := r.store.ListAll(ctx, Prefix)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
staging, err := r.store.ListAll(ctx, StagingPrefix)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
rows, err := r.q.ListAttachmentsForReconcile(ctx)
|
||||
if err != nil {
|
||||
return report, err
|
||||
}
|
||||
|
||||
report.ScannedObjects = len(listed.Objects) + len(staging.Objects)
|
||||
report.ScannedRows = len(rows)
|
||||
report.ListPages = listed.Pages + staging.Pages
|
||||
|
||||
p := buildPlan(listed.Objects, staging.Objects, rows, started, opts)
|
||||
|
||||
for _, obj := range p.orphans {
|
||||
report.OrphanObjects = append(report.OrphanObjects, obj.Key)
|
||||
}
|
||||
for _, obj := range p.staging {
|
||||
report.StagingObjects = append(report.StagingObjects, obj.Key)
|
||||
}
|
||||
for _, obj := range p.corrupt {
|
||||
report.CorruptObjects = append(report.CorruptObjects, obj.Key)
|
||||
}
|
||||
for _, row := range p.missing {
|
||||
report.MissingAttachments = append(report.MissingAttachments, row.ID)
|
||||
}
|
||||
|
||||
report.SkippedFresh = p.skippedFresh
|
||||
report.SkippedInactive = p.skippedInactive
|
||||
|
||||
if !opts.DryRun {
|
||||
r.apply(ctx, opts, p, &report)
|
||||
}
|
||||
|
||||
report.DurationMS = r.now().Sub(started).Milliseconds()
|
||||
|
||||
r.log.Info(
|
||||
"attachment reconcile finished",
|
||||
"dry_run", opts.DryRun,
|
||||
"scanned_objects", report.ScannedObjects,
|
||||
"scanned_rows", report.ScannedRows,
|
||||
"list_pages", report.ListPages,
|
||||
"orphan_objects", len(report.OrphanObjects),
|
||||
"staging_objects", len(report.StagingObjects),
|
||||
"missing_attachments", len(report.MissingAttachments),
|
||||
"corrupt_objects", len(report.CorruptObjects),
|
||||
"deleted_objects", report.DeletedObjects,
|
||||
"marked_failed", report.MarkedFailed,
|
||||
"errors", len(report.Errors),
|
||||
)
|
||||
|
||||
return report, nil
|
||||
}
|
||||
|
||||
func (o Options) withDefaults() Options {
|
||||
if o.Grace <= 0 {
|
||||
o.Grace = DefaultGrace
|
||||
}
|
||||
if o.MaxDeletes <= 0 {
|
||||
o.MaxDeletes = DefaultMaxDeletes
|
||||
}
|
||||
|
||||
if o.StagingRetention <= 0 {
|
||||
o.StagingRetention = DefaultStagingRetention
|
||||
}
|
||||
|
||||
return o
|
||||
}
|
||||
|
||||
func buildPlan(
|
||||
objects []s3store.ObjectInfo,
|
||||
staging []s3store.ObjectInfo,
|
||||
rows []db.ListAttachmentsForReconcileRow,
|
||||
now time.Time,
|
||||
opts Options,
|
||||
) plan {
|
||||
var p plan
|
||||
|
||||
byKey := make(map[string]s3store.ObjectInfo, len(objects))
|
||||
for _, obj := range objects {
|
||||
byKey[obj.Key] = obj
|
||||
}
|
||||
|
||||
byStagingKey := make(map[string]db.ListAttachmentsForReconcileRow, len(rows))
|
||||
for _, row := range rows {
|
||||
if row.StagingKey != nil {
|
||||
byStagingKey[*row.StagingKey] = row
|
||||
}
|
||||
|
||||
obj, exists := byKey[row.S3Key]
|
||||
if exists {
|
||||
delete(byKey, row.S3Key)
|
||||
}
|
||||
|
||||
switch row.Status {
|
||||
case db.AttachmentStatusQueued:
|
||||
p.skippedInactive++
|
||||
case db.AttachmentStatusReady:
|
||||
if !exists {
|
||||
p.missing = append(p.missing, row)
|
||||
continue
|
||||
}
|
||||
if obj.Size != int64(row.SizeBytes) {
|
||||
p.corrupt = append(p.corrupt, obj)
|
||||
}
|
||||
default:
|
||||
if exists {
|
||||
p.skippedInactive++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, obj := range byKey {
|
||||
if now.Sub(obj.LastModified) < opts.Grace {
|
||||
p.skippedFresh++
|
||||
continue
|
||||
}
|
||||
p.orphans = append(p.orphans, obj)
|
||||
}
|
||||
|
||||
for _, obj := range staging {
|
||||
retainedSince := obj.LastModified
|
||||
if row, exists := byStagingKey[obj.Key]; exists {
|
||||
if row.Status != db.AttachmentStatusReady && row.Status != db.AttachmentStatusFailed {
|
||||
p.skippedInactive++
|
||||
continue
|
||||
}
|
||||
|
||||
retainedSince = row.UpdatedAt
|
||||
}
|
||||
|
||||
if now.Sub(retainedSince) < opts.StagingRetention {
|
||||
p.skippedFresh++
|
||||
continue
|
||||
}
|
||||
|
||||
p.staging = append(p.staging, obj)
|
||||
}
|
||||
|
||||
sort.Slice(p.staging, func(i, j int) bool {
|
||||
return p.staging[i].Key < p.staging[j].Key
|
||||
})
|
||||
sort.Slice(p.orphans, func(i, j int) bool {
|
||||
return p.orphans[i].Key < p.orphans[j].Key
|
||||
})
|
||||
sort.Slice(p.corrupt, func(i, j int) bool {
|
||||
return p.corrupt[i].Key < p.corrupt[j].Key
|
||||
})
|
||||
sort.Slice(p.missing, func(i, j int) bool {
|
||||
return p.missing[i].ID.String() < p.missing[j].ID.String()
|
||||
})
|
||||
|
||||
return p
|
||||
}
|
||||
|
||||
func (r *Reconciler) apply(ctx context.Context, opts Options, p plan, report *Report) {
|
||||
deletes := 0
|
||||
|
||||
for _, obj := range p.orphans {
|
||||
if deletes >= opts.MaxDeletes {
|
||||
report.Errors = append(report.Errors, "max deletes reached")
|
||||
break
|
||||
}
|
||||
|
||||
if err := r.store.DeleteAdmin(ctx, obj.Key); err != nil {
|
||||
report.Errors = append(report.Errors, fmt.Sprintf("delete object %s: %v", obj.Key, err))
|
||||
continue
|
||||
}
|
||||
|
||||
deletes++
|
||||
report.DeletedObjects++
|
||||
}
|
||||
|
||||
for _, row := range p.missing {
|
||||
marked, err := r.q.MarkAttachmentFailedIfReady(ctx, db.MarkAttachmentFailedIfReadyParams{
|
||||
ID: row.ID,
|
||||
ErrorCode: ErrorCodeObjectMissing,
|
||||
})
|
||||
if err != nil {
|
||||
report.Errors = append(report.Errors, fmt.Sprintf("mark failed %s: %v", row.ID, err))
|
||||
continue
|
||||
}
|
||||
|
||||
report.MarkedFailed += int(marked)
|
||||
}
|
||||
|
||||
for _, obj := range p.staging {
|
||||
if report.DeletedObjects >= opts.MaxDeletes {
|
||||
report.Errors = append(report.Errors, "max deletes reached")
|
||||
break
|
||||
}
|
||||
|
||||
if err := r.cleanupStagingObject(ctx, obj, opts, report); err != nil {
|
||||
report.Errors = append(report.Errors, fmt.Sprintf("cleanup staging %s: %v", obj.Key, err))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package reconcile
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
s3store "mtusi2026-messenger/internal/s3"
|
||||
)
|
||||
|
||||
func (r *Reconciler) cleanupStagingObject(
|
||||
ctx context.Context,
|
||||
obj s3store.ObjectInfo,
|
||||
opts Options,
|
||||
report *Report,
|
||||
) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
tx, err := r.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
|
||||
q := r.withTx(tx)
|
||||
row, err := q.LockAttachmentByStagingKey(ctx, obj.Key)
|
||||
orphan := errors.Is(err, pgx.ErrNoRows)
|
||||
if err != nil && !orphan {
|
||||
return err
|
||||
}
|
||||
|
||||
if !orphan {
|
||||
if row.Status != db.AttachmentStatusReady && row.Status != db.AttachmentStatusFailed {
|
||||
report.SkippedInactive++
|
||||
return nil
|
||||
}
|
||||
|
||||
if r.now().Sub(row.UpdatedAt) < opts.StagingRetention {
|
||||
report.SkippedFresh++
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := r.store.DeleteAdmin(ctx, obj.Key); err != nil {
|
||||
return err
|
||||
}
|
||||
report.DeletedObjects++
|
||||
|
||||
if !orphan {
|
||||
if err := q.ClearAttachmentStagingKey(ctx, row.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package redis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func New(ctx context.Context, log *slog.Logger, url string) (*goredis.Client, error) {
|
||||
opts, err := goredis.ParseURL(url)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse redis url: %w", err)
|
||||
}
|
||||
|
||||
opts.DialTimeout = 1 * time.Second
|
||||
opts.ReadTimeout = 200 * time.Millisecond
|
||||
opts.WriteTimeout = 200 * time.Millisecond
|
||||
opts.MaxRetries = -1
|
||||
|
||||
client := goredis.NewClient(opts)
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
if err := client.Ping(pingCtx).Err(); err != nil {
|
||||
log.Warn("redis unavailable, running without cache", "error", err)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
}
|
||||
@@ -0,0 +1,295 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/aws/aws-sdk-go-v2/aws"
|
||||
awshttp "github.com/aws/aws-sdk-go-v2/aws/transport/http"
|
||||
awsconfig "github.com/aws/aws-sdk-go-v2/config"
|
||||
"github.com/aws/aws-sdk-go-v2/credentials"
|
||||
awss3 "github.com/aws/aws-sdk-go-v2/service/s3"
|
||||
"github.com/aws/smithy-go"
|
||||
|
||||
appconfig "mtusi2026-messenger/internal/config"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUnavailable = errors.New("s3 unavailable")
|
||||
ErrLoadConfig = errors.New("load aws config")
|
||||
ErrHeadBucket = errors.New("head bucket")
|
||||
ErrCreateBucket = errors.New("create bucket")
|
||||
ErrObjectNotFound = errors.New("object not found")
|
||||
ErrPutObject = errors.New("put object")
|
||||
ErrGetObject = errors.New("get object")
|
||||
ErrListObjects = errors.New("list objects")
|
||||
ErrDeleteObject = errors.New("delete object")
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
bucket string
|
||||
bootstrap *awss3.Client
|
||||
api *awss3.Client
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
type ObjectInfo struct {
|
||||
Key string
|
||||
Size int64
|
||||
LastModified time.Time
|
||||
}
|
||||
|
||||
type ListResult struct {
|
||||
Objects []ObjectInfo
|
||||
Pages int
|
||||
}
|
||||
|
||||
func New(ctx context.Context, cfg appconfig.S3Config, log *slog.Logger) (*Client, error) {
|
||||
c, err := NewAPI(ctx, cfg, log)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bootstrapAccessKey, err := cfg.BootstrapAccessKey()
|
||||
if err != nil {
|
||||
log.Error("read s3 bootstrap access key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bootstrapSecretKey, err := cfg.BootstrapSecretKey()
|
||||
if err != nil {
|
||||
log.Error("read s3 bootstrap secret key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c.bootstrap, err = newS3Client(ctx, log, cfg.Endpoint, cfg.Region, bootstrapAccessKey, bootstrapSecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c, nil
|
||||
}
|
||||
|
||||
func NewAPI(ctx context.Context, cfg appconfig.S3Config, log *slog.Logger) (*Client, error) {
|
||||
apiAccessKey, err := cfg.APIAccessKey()
|
||||
if err != nil {
|
||||
log.Error("read s3 api access key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
apiSecretKey, err := cfg.APISecretKey()
|
||||
if err != nil {
|
||||
log.Error("read s3 api secret key", "error", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
api, err := newS3Client(ctx, log, cfg.Endpoint, cfg.Region, apiAccessKey, apiSecretKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &Client{
|
||||
bucket: cfg.Bucket,
|
||||
api: api,
|
||||
log: log,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) Bucket() string {
|
||||
return c.bucket
|
||||
}
|
||||
|
||||
func (c *Client) EnsureBucket(ctx context.Context) error {
|
||||
_, err := c.bootstrap.HeadBucket(ctx, &awss3.HeadBucketInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
})
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if !isNotFound(err) {
|
||||
c.log.Error("head bucket", "bucket", c.bucket, "error", err)
|
||||
return ErrHeadBucket
|
||||
}
|
||||
|
||||
_, err = c.bootstrap.CreateBucket(ctx, &awss3.CreateBucketInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
})
|
||||
if err == nil || isAlreadyExists(err) {
|
||||
return nil
|
||||
}
|
||||
|
||||
c.log.Error("create bucket", "bucket", c.bucket, "error", err)
|
||||
return ErrCreateBucket
|
||||
}
|
||||
|
||||
func (c *Client) Ping(ctx context.Context) error {
|
||||
_, err := c.api.HeadBucket(ctx, &awss3.HeadBucketInput{Bucket: aws.String(c.bucket)})
|
||||
if err != nil {
|
||||
c.log.Error("head bucket", "bucket", c.bucket, "error", err)
|
||||
return operationError(ErrHeadBucket, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Put(
|
||||
ctx context.Context,
|
||||
key string,
|
||||
body io.ReadSeeker,
|
||||
size int64,
|
||||
contentType string,
|
||||
) error {
|
||||
_, err := c.api.PutObject(ctx, &awss3.PutObjectInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
Key: aws.String(key),
|
||||
Body: body,
|
||||
ContentLength: aws.Int64(size),
|
||||
ContentType: aws.String(contentType),
|
||||
})
|
||||
if err != nil {
|
||||
c.log.Error("put object", "bucket", c.bucket, "key", key, "error", err)
|
||||
return operationError(ErrPutObject, err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Get(ctx context.Context, key string) (*awss3.GetObjectOutput, error) {
|
||||
out, err := c.api.GetObject(ctx, &awss3.GetObjectInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if isNotFound(err) {
|
||||
c.log.Warn("get object", "bucket", c.bucket, "key", key, "error", err)
|
||||
return nil, ErrObjectNotFound
|
||||
}
|
||||
if err != nil {
|
||||
c.log.Error("get object", "bucket", c.bucket, "key", key, "error", err)
|
||||
return nil, operationError(ErrGetObject, err)
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c *Client) ListAll(ctx context.Context, prefix string) (ListResult, error) {
|
||||
result := ListResult{Objects: []ObjectInfo{}}
|
||||
|
||||
paginator := awss3.NewListObjectsV2Paginator(c.bootstrap, &awss3.ListObjectsV2Input{
|
||||
Bucket: aws.String(c.bucket),
|
||||
Prefix: aws.String(prefix),
|
||||
})
|
||||
|
||||
for paginator.HasMorePages() {
|
||||
page, err := paginator.NextPage(ctx)
|
||||
if err != nil {
|
||||
c.log.Error("list objects", "bucket", c.bucket, "prefix", prefix, "error", err)
|
||||
return ListResult{}, ErrListObjects
|
||||
}
|
||||
|
||||
result.Pages++
|
||||
|
||||
for _, obj := range page.Contents {
|
||||
result.Objects = append(result.Objects, ObjectInfo{
|
||||
Key: aws.ToString(obj.Key),
|
||||
Size: aws.ToInt64(obj.Size),
|
||||
LastModified: aws.ToTime(obj.LastModified),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (c *Client) DeleteAdmin(ctx context.Context, key string) error {
|
||||
return c.deleteObject(ctx, c.bootstrap, key)
|
||||
}
|
||||
|
||||
func (c *Client) Delete(ctx context.Context, key string) error {
|
||||
return c.deleteObject(ctx, c.api, key)
|
||||
}
|
||||
|
||||
func (c *Client) deleteObject(ctx context.Context, client *awss3.Client, key string) error {
|
||||
_, err := client.DeleteObject(ctx, &awss3.DeleteObjectInput{
|
||||
Bucket: aws.String(c.bucket),
|
||||
Key: aws.String(key),
|
||||
})
|
||||
if isNotFound(err) {
|
||||
return nil
|
||||
}
|
||||
if err != nil {
|
||||
c.log.Error("delete object", "bucket", c.bucket, "key", key, "error", err)
|
||||
return ErrDeleteObject
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func newS3Client(
|
||||
ctx context.Context,
|
||||
log *slog.Logger,
|
||||
endpoint string,
|
||||
region string,
|
||||
accessKey string,
|
||||
secretKey string,
|
||||
) (*awss3.Client, error) {
|
||||
cfg, err := awsconfig.LoadDefaultConfig(
|
||||
ctx,
|
||||
awsconfig.WithRegion(region),
|
||||
awsconfig.WithCredentialsProvider(
|
||||
credentials.NewStaticCredentialsProvider(accessKey, secretKey, ""),
|
||||
),
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("load aws config", "endpoint", endpoint, "region", region, "error", err)
|
||||
return nil, ErrLoadConfig
|
||||
}
|
||||
|
||||
return awss3.NewFromConfig(cfg, func(o *awss3.Options) {
|
||||
o.BaseEndpoint = aws.String(endpoint)
|
||||
o.UsePathStyle = true
|
||||
}), nil
|
||||
}
|
||||
|
||||
func operationError(operation, err error) error {
|
||||
var networkError net.Error
|
||||
var responseError *awshttp.ResponseError
|
||||
if errors.Is(err, context.DeadlineExceeded) || errors.As(err, &networkError) ||
|
||||
(errors.As(err, &responseError) && responseError.HTTPStatusCode() >= 500) {
|
||||
return ErrUnavailable
|
||||
}
|
||||
return operation
|
||||
}
|
||||
|
||||
func isNotFound(err error) bool {
|
||||
var responseErr *awshttp.ResponseError
|
||||
if errors.As(err, &responseErr) && responseErr.HTTPStatusCode() == http.StatusNotFound {
|
||||
return true
|
||||
}
|
||||
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "NotFound", "NoSuchBucket", "NoSuchKey":
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func isAlreadyExists(err error) bool {
|
||||
var apiErr smithy.APIError
|
||||
if errors.As(err, &apiErr) {
|
||||
switch apiErr.ErrorCode() {
|
||||
case "BucketAlreadyOwnedByYou", "BucketAlreadyExists":
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package s3
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"mtusi2026-messenger/internal/config"
|
||||
)
|
||||
|
||||
const (
|
||||
seaweedUID = 1000
|
||||
seaweedGID = 1000
|
||||
)
|
||||
|
||||
type Credential struct {
|
||||
AccessKey string `json:"accessKey"`
|
||||
SecretKey string `json:"secretKey"`
|
||||
}
|
||||
|
||||
type Identity struct {
|
||||
Name string `json:"name"`
|
||||
Credentials []Credential `json:"credentials"`
|
||||
Actions []string `json:"actions"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Identities []Identity `json:"identities"`
|
||||
}
|
||||
|
||||
func WriteConfig(output string, cfg config.S3Config) error {
|
||||
apiAccessKey, err := cfg.APIAccessKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
apiSecretKey, err := cfg.APISecretKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bootstrapAccessKey, err := cfg.BootstrapAccessKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
bootstrapSecretKey, err := cfg.BootstrapSecretKey()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
config := Config{
|
||||
Identities: []Identity{
|
||||
{
|
||||
Name: "bootstrap",
|
||||
Credentials: []Credential{
|
||||
{AccessKey: bootstrapAccessKey, SecretKey: bootstrapSecretKey},
|
||||
},
|
||||
Actions: []string{"Admin", "Read", "Write", "List", "Tagging"},
|
||||
},
|
||||
{
|
||||
Name: "api",
|
||||
Credentials: []Credential{
|
||||
{AccessKey: apiAccessKey, SecretKey: apiSecretKey},
|
||||
},
|
||||
Actions: []string{"Read", "Write", "List", "Tagging"},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
data, err := json.MarshalIndent(config, "", " ")
|
||||
if err != nil {
|
||||
return fmt.Errorf("marshal s3 config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(filepath.Dir(output), 0o755); err != nil {
|
||||
return fmt.Errorf("create config directory: %w", err)
|
||||
}
|
||||
|
||||
tmp := output + ".tmp"
|
||||
if err := os.WriteFile(tmp, append(data, '\n'), 0o600); err != nil {
|
||||
return fmt.Errorf("write config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Chown(tmp, seaweedUID, seaweedGID); err != nil {
|
||||
return fmt.Errorf("chown config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Chmod(tmp, 0o400); err != nil {
|
||||
return fmt.Errorf("chmod config: %w", err)
|
||||
}
|
||||
|
||||
if err := os.Rename(tmp, output); err != nil {
|
||||
return fmt.Errorf("replace config: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,358 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
s3store "mtusi2026-messenger/internal/s3"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
ErrAttachmentNotReady = errors.New("attachment not ready")
|
||||
ErrInvalidImage = errors.New("invalid image")
|
||||
ErrInvalidSize = errors.New("invalid size")
|
||||
ErrUploadReadTimeout = errors.New("upload body read timed out")
|
||||
)
|
||||
|
||||
type attachmentQueries interface {
|
||||
CreateAttachment(ctx context.Context, arg db.CreateAttachmentParams) (db.Attachment, error)
|
||||
CreateOutboxEntry(ctx context.Context, arg db.CreateOutboxEntryParams) (db.Outbox, error)
|
||||
GetAttachmentByID(ctx context.Context, id uuid.UUID) (db.Attachment, error)
|
||||
ListAttachmentVariants(ctx context.Context, arg db.ListAttachmentVariantsParams) ([]db.AttachmentVariant, error)
|
||||
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
|
||||
}
|
||||
|
||||
type AttachmentService struct {
|
||||
pool txBeginner
|
||||
q attachmentQueries
|
||||
withTx func(pgx.Tx) attachmentQueries
|
||||
s3 *s3store.Client
|
||||
maxBytes int
|
||||
maxPixels int
|
||||
decodeSlot chan struct{}
|
||||
}
|
||||
|
||||
func NewAttachmentService(
|
||||
pool *pgxpool.Pool,
|
||||
q *db.Queries,
|
||||
s3 *s3store.Client,
|
||||
maxBytes, maxPixels int,
|
||||
) *AttachmentService {
|
||||
return &AttachmentService{
|
||||
pool: pool,
|
||||
q: q,
|
||||
withTx: func(tx pgx.Tx) attachmentQueries {
|
||||
return q.WithTx(tx)
|
||||
},
|
||||
s3: s3,
|
||||
maxBytes: maxBytes,
|
||||
maxPixels: maxPixels,
|
||||
decodeSlot: make(chan struct{}, 1),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AttachmentService) Upload(
|
||||
ctx context.Context,
|
||||
userID, chatID uuid.UUID,
|
||||
filename string,
|
||||
body io.Reader,
|
||||
contentType string,
|
||||
) (model.Attachment, error) {
|
||||
if err := s.ensureMember(ctx, userID, chatID); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
tmp, err := os.CreateTemp("", "upload-*.tmp")
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
defer func() {
|
||||
_ = tmp.Close()
|
||||
_ = os.Remove(tmp.Name())
|
||||
}()
|
||||
|
||||
hasher := sha256.New()
|
||||
size, err := io.Copy(io.MultiWriter(tmp, hasher), body)
|
||||
if err != nil {
|
||||
var maxBytesErr *http.MaxBytesError
|
||||
if errors.As(err, &maxBytesErr) {
|
||||
return model.Attachment{}, ErrInvalidSize
|
||||
}
|
||||
var networkErr net.Error
|
||||
if errors.As(err, &networkErr) && networkErr.Timeout() {
|
||||
return model.Attachment{}, ErrUploadReadTimeout
|
||||
}
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
if size > int64(s.maxBytes) {
|
||||
return model.Attachment{}, ErrInvalidSize
|
||||
}
|
||||
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
detectedType, width, height, err := s.checkImage(ctx, tmp, contentType)
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
jobID, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
hash := hex.EncodeToString(hasher.Sum(nil))
|
||||
key := "chats/" + chatID.String() + "/attachments/" + id.String()
|
||||
stagingKey := "staging/" + id.String()
|
||||
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
if err := s.s3.Put(ctx, stagingKey, tmp, size, string(detectedType)); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
q := s.withTx(tx)
|
||||
|
||||
row, err := q.CreateAttachment(ctx, db.CreateAttachmentParams{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
OwnerID: userID,
|
||||
S3Key: key,
|
||||
StagingKey: stagingKey,
|
||||
JobID: jobID,
|
||||
Filename: filename,
|
||||
ContentType: db.AttachmentContentType(detectedType),
|
||||
SizeBytes: int(size),
|
||||
Sha256: hash,
|
||||
Width: width,
|
||||
Height: height,
|
||||
ErrorCode: "",
|
||||
})
|
||||
if postgres.IsNotFound(err) {
|
||||
return model.Attachment{}, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
if _, err := q.CreateOutboxEntry(ctx, db.CreateOutboxEntryParams{
|
||||
AttachmentID: id,
|
||||
Attempt: 1,
|
||||
AvailableAt: row.CreatedAt,
|
||||
}); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
return model.Attachment{
|
||||
ID: row.ID,
|
||||
ChatID: row.ChatID,
|
||||
OwnerID: row.OwnerID,
|
||||
Filename: row.Filename,
|
||||
ContentType: model.ContentType(row.ContentType),
|
||||
SizeBytes: row.SizeBytes,
|
||||
Sha256: row.Sha256,
|
||||
Status: model.AttachmentStatus(row.Status),
|
||||
Width: row.Width,
|
||||
Height: row.Height,
|
||||
ErrorCode: row.ErrorCode,
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) Get(
|
||||
ctx context.Context,
|
||||
userID, attachmentID uuid.UUID,
|
||||
) (model.Attachment, error) {
|
||||
row, err := s.q.GetAttachmentByID(ctx, attachmentID)
|
||||
if postgres.IsNotFound(err) {
|
||||
return model.Attachment{}, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
if err := s.ensureMember(ctx, userID, row.ChatID); err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
|
||||
rows, err := s.q.ListAttachmentVariants(ctx, db.ListAttachmentVariantsParams{AttachmentID: row.ID, PipelineVersion: row.PipelineVersion})
|
||||
if err != nil {
|
||||
return model.Attachment{}, err
|
||||
}
|
||||
variants := make([]model.AttachmentVariant, 0, len(rows))
|
||||
for _, v := range rows {
|
||||
variants = append(variants, model.AttachmentVariant{
|
||||
Name: model.AttachmentVariantName(v.Variant),
|
||||
ContentType: model.ContentType(v.ContentType),
|
||||
SizeBytes: v.SizeBytes,
|
||||
Width: v.Width,
|
||||
Height: v.Height,
|
||||
Sha256: v.Sha256,
|
||||
})
|
||||
}
|
||||
|
||||
return model.Attachment{
|
||||
Variants: variants,
|
||||
ID: row.ID,
|
||||
ChatID: row.ChatID,
|
||||
OwnerID: row.OwnerID,
|
||||
Filename: row.Filename,
|
||||
ContentType: model.ContentType(row.ContentType),
|
||||
SizeBytes: row.SizeBytes,
|
||||
Sha256: row.Sha256,
|
||||
Status: model.AttachmentStatus(row.Status),
|
||||
Width: row.Width,
|
||||
Height: row.Height,
|
||||
ErrorCode: row.ErrorCode,
|
||||
CreatedAt: row.CreatedAt,
|
||||
UpdatedAt: row.UpdatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) GetContent(
|
||||
ctx context.Context,
|
||||
userID, attachmentID uuid.UUID,
|
||||
variant string,
|
||||
) ([]byte, model.ContentType, error) {
|
||||
row, err := s.q.GetAttachmentByID(ctx, attachmentID)
|
||||
if postgres.IsNotFound(err) {
|
||||
return nil, "", ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
if err := s.ensureMember(ctx, userID, row.ChatID); err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
if model.AttachmentStatus(row.Status) != model.AttachmentStatusReady {
|
||||
return nil, "", ErrAttachmentNotReady
|
||||
}
|
||||
|
||||
key, contentType := row.S3Key, model.ContentType(row.ContentType)
|
||||
if variant != "" && variant != model.AttachmentContentOriginal {
|
||||
rows, err := s.q.ListAttachmentVariants(ctx, db.ListAttachmentVariantsParams{AttachmentID: row.ID, PipelineVersion: row.PipelineVersion})
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
found := false
|
||||
for _, v := range rows {
|
||||
if string(v.Variant) == variant {
|
||||
key, contentType, found = v.S3Key, model.ContentType(v.ContentType), true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
return nil, "", ErrAttachmentNotFound
|
||||
}
|
||||
}
|
||||
out, err := s.s3.Get(ctx, key)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
defer out.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(out.Body)
|
||||
if err != nil {
|
||||
return nil, "", err
|
||||
}
|
||||
|
||||
return data, contentType, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) checkImage(ctx context.Context, r io.ReadSeeker, contentType string) (model.ContentType, int, int, error) {
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil {
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
|
||||
if cfg.Width <= 0 || cfg.Height <= 0 {
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
if cfg.Width > s.maxPixels/cfg.Height {
|
||||
return "", 0, 0, ErrInvalidSize
|
||||
}
|
||||
|
||||
var detectedType model.ContentType
|
||||
switch format {
|
||||
case "png":
|
||||
if contentType != string(model.ContentTypePNG) {
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
detectedType = model.ContentTypePNG
|
||||
case "jpeg":
|
||||
if contentType != string(model.ContentTypeJPEG) {
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
detectedType = model.ContentTypeJPEG
|
||||
default:
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
|
||||
select {
|
||||
case s.decodeSlot <- struct{}{}:
|
||||
defer func() { <-s.decodeSlot }()
|
||||
case <-ctx.Done():
|
||||
return "", 0, 0, ctx.Err()
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", 0, 0, err
|
||||
}
|
||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||
return "", 0, 0, err
|
||||
}
|
||||
if _, _, err := image.Decode(r); err != nil {
|
||||
return "", 0, 0, ErrInvalidImage
|
||||
}
|
||||
|
||||
return detectedType, cfg.Width, cfg.Height, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentService) ensureMember(ctx context.Context, userID, chatID uuid.UUID) error {
|
||||
member, err := s.q.IsChatMember(ctx, db.IsChatMemberParams{
|
||||
ChatID: chatID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !member {
|
||||
return ErrAttachmentNotFound
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,188 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"time"
|
||||
|
||||
"mtusi2026-messenger/internal/credentials"
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
|
||||
"github.com/google/uuid"
|
||||
goredis "github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
const sessionCacheTTL = 60 * time.Second
|
||||
const cacheOpTimeout = 300 * time.Millisecond
|
||||
|
||||
var (
|
||||
ErrInvalidCredentials = errors.New("invalid credentials")
|
||||
ErrUnauthorized = errors.New("unauthorized")
|
||||
ErrSessionCacheUnavailable = errors.New("session cache unavailable")
|
||||
)
|
||||
|
||||
type authQueries interface {
|
||||
CreateSession(ctx context.Context, arg db.CreateSessionParams) error
|
||||
DeleteSession(ctx context.Context, tokenHash string) (int64, error)
|
||||
GetActiveSession(ctx context.Context, tokenHash string) (db.GetActiveSessionRow, error)
|
||||
GetUserByUsername(ctx context.Context, username string) (db.User, error)
|
||||
}
|
||||
|
||||
type AuthService struct {
|
||||
q authQueries
|
||||
cache *goredis.Client
|
||||
sessionTTL time.Duration
|
||||
log *slog.Logger
|
||||
}
|
||||
|
||||
func NewAuthService(q authQueries, cache *goredis.Client, sessionTTL time.Duration, log *slog.Logger) *AuthService {
|
||||
return &AuthService{
|
||||
q: q,
|
||||
cache: cache,
|
||||
sessionTTL: sessionTTL,
|
||||
log: log,
|
||||
}
|
||||
}
|
||||
|
||||
var dummyHash, _ = credentials.HashPassword("dummy-password-for-timing")
|
||||
|
||||
func (s *AuthService) Login(ctx context.Context, username, plain string) (model.Session, error) {
|
||||
user, err := s.q.GetUserByUsername(ctx, username)
|
||||
if postgres.IsNotFound(err) {
|
||||
_ = credentials.VerifyPassword(dummyHash, plain)
|
||||
return model.Session{}, ErrInvalidCredentials
|
||||
}
|
||||
if err != nil {
|
||||
return model.Session{}, err
|
||||
}
|
||||
|
||||
if err := credentials.VerifyPassword(user.PasswordHash, plain); err != nil {
|
||||
if errors.Is(err, credentials.ErrPasswordMismatch) {
|
||||
return model.Session{}, ErrInvalidCredentials
|
||||
}
|
||||
return model.Session{}, err
|
||||
}
|
||||
|
||||
token := credentials.NewToken()
|
||||
expiresAt := time.Now().UTC().Add(s.sessionTTL)
|
||||
|
||||
err = s.q.CreateSession(ctx, db.CreateSessionParams{
|
||||
TokenHash: credentials.HashToken(token),
|
||||
UserID: user.ID,
|
||||
ExpiresAt: expiresAt,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Session{}, err
|
||||
}
|
||||
|
||||
return model.Session{
|
||||
Token: token,
|
||||
ExpiresAt: expiresAt,
|
||||
User: model.User{
|
||||
ID: user.ID,
|
||||
Username: user.Username,
|
||||
DisplayName: user.DisplayName,
|
||||
CreatedAt: user.CreatedAt.UTC(),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) Authenticate(ctx context.Context, token string) (uuid.UUID, error) {
|
||||
tokenHash := credentials.HashToken(token)
|
||||
key := cacheKey(tokenHash)
|
||||
|
||||
if id, ok := s.cacheGet(ctx, key); ok {
|
||||
return id, nil
|
||||
}
|
||||
|
||||
row, err := s.q.GetActiveSession(ctx, tokenHash)
|
||||
if postgres.IsNotFound(err) {
|
||||
return uuid.Nil, ErrUnauthorized
|
||||
}
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
s.cacheSet(ctx, key, row.UserID, min(time.Until(row.ExpiresAt), sessionCacheTTL))
|
||||
|
||||
return row.UserID, nil
|
||||
}
|
||||
|
||||
func (s *AuthService) Logout(ctx context.Context, token string) error {
|
||||
tokenHash := credentials.HashToken(token)
|
||||
|
||||
n, err := s.q.DeleteSession(ctx, tokenHash)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := s.cacheDel(ctx, cacheKey(tokenHash)); err != nil {
|
||||
return ErrSessionCacheUnavailable
|
||||
}
|
||||
|
||||
if n == 0 {
|
||||
return ErrUnauthorized
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func cacheKey(tokenHash string) string {
|
||||
return "session:" + tokenHash
|
||||
}
|
||||
|
||||
func (s *AuthService) cacheGet(ctx context.Context, key string) (uuid.UUID, bool) {
|
||||
if s.cache == nil {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
|
||||
defer cancel()
|
||||
|
||||
val, err := s.cache.Get(cctx, key).Result()
|
||||
switch {
|
||||
case err == nil:
|
||||
id, perr := uuid.Parse(val)
|
||||
if perr != nil {
|
||||
s.log.Warn("session cache value is not a uuid", "error", perr)
|
||||
return uuid.Nil, false
|
||||
}
|
||||
s.log.Debug("session cache hit")
|
||||
return id, true
|
||||
case errors.Is(err, goredis.Nil):
|
||||
s.log.Debug("session cache miss")
|
||||
default:
|
||||
s.log.Warn("session cache get failed, falling back to database", "error", err)
|
||||
}
|
||||
return uuid.Nil, false
|
||||
}
|
||||
|
||||
func (s *AuthService) cacheSet(ctx context.Context, key string, id uuid.UUID, ttl time.Duration) {
|
||||
if s.cache == nil || ttl <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := s.cache.Set(cctx, key, id.String(), ttl).Err(); err != nil {
|
||||
s.log.Warn("session cache set failed", "error", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *AuthService) cacheDel(ctx context.Context, key string) error {
|
||||
if s.cache == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
cctx, cancel := context.WithTimeout(ctx, cacheOpTimeout)
|
||||
defer cancel()
|
||||
|
||||
if err := s.cache.Del(cctx, key).Err(); err != nil {
|
||||
s.log.Warn("session cache delete failed", "error", err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"slices"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrChatNotFound = errors.New("chat not found")
|
||||
ErrNotChatMember = errors.New("caller must be a member of member_ids")
|
||||
ErrDuplicateMember = errors.New("member_ids contains duplicates")
|
||||
ErrUnknownMember = errors.New("member_ids contains unknown user")
|
||||
)
|
||||
|
||||
type chatQueries interface {
|
||||
AddChatMembers(ctx context.Context, arg db.AddChatMembersParams) error
|
||||
CreateChat(ctx context.Context, arg db.CreateChatParams) (db.Chat, error)
|
||||
GetChat(ctx context.Context, arg db.GetChatParams) (db.GetChatRow, error)
|
||||
ListChatsAfter(ctx context.Context, arg db.ListChatsAfterParams) ([]db.ListChatsAfterRow, error)
|
||||
ListChatsFirstPage(ctx context.Context, arg db.ListChatsFirstPageParams) ([]db.ListChatsFirstPageRow, error)
|
||||
}
|
||||
|
||||
type ChatService struct {
|
||||
pool txBeginner
|
||||
q chatQueries
|
||||
withTx func(pgx.Tx) chatQueries
|
||||
}
|
||||
|
||||
func NewChatService(pool *pgxpool.Pool, q *db.Queries) *ChatService {
|
||||
return &ChatService{
|
||||
pool: pool,
|
||||
q: q,
|
||||
withTx: func(tx pgx.Tx) chatQueries {
|
||||
return q.WithTx(tx)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ChatService) Create(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
title string,
|
||||
memberIDs []uuid.UUID,
|
||||
) (model.Chat, error) {
|
||||
if !slices.Contains(memberIDs, userID) {
|
||||
return model.Chat{}, ErrNotChatMember
|
||||
}
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
q := s.withTx(tx)
|
||||
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
row, err := q.CreateChat(ctx, db.CreateChatParams{
|
||||
ID: id,
|
||||
Title: title,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
err = q.AddChatMembers(ctx, db.AddChatMembersParams{
|
||||
ChatID: row.ID,
|
||||
UserIds: memberIDs,
|
||||
})
|
||||
switch postgres.ErrCode(err) {
|
||||
case postgres.CodeUniqueViolation:
|
||||
return model.Chat{}, ErrDuplicateMember
|
||||
case postgres.CodeForeignKeyViolation:
|
||||
return model.Chat{}, ErrUnknownMember
|
||||
}
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
ids := slices.Clone(memberIDs)
|
||||
slices.SortFunc(ids, func(a, b uuid.UUID) int {
|
||||
return bytes.Compare(a[:], b[:])
|
||||
})
|
||||
|
||||
return model.Chat{
|
||||
ID: row.ID,
|
||||
Title: row.Title,
|
||||
CreatedAt: row.CreatedAt,
|
||||
MemberIDs: ids,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) Get(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
chatID uuid.UUID,
|
||||
) (model.Chat, error) {
|
||||
row, err := s.q.GetChat(ctx, db.GetChatParams{
|
||||
ID: chatID,
|
||||
UserID: userID,
|
||||
})
|
||||
if postgres.IsNotFound(err) {
|
||||
return model.Chat{}, ErrChatNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.Chat{}, err
|
||||
}
|
||||
|
||||
return model.Chat{
|
||||
ID: row.ID,
|
||||
Title: row.Title,
|
||||
CreatedAt: row.CreatedAt,
|
||||
MemberIDs: row.MemberIds,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) List(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
limit int,
|
||||
cursor *string,
|
||||
) ([]model.Chat, *string, error) {
|
||||
var chats []model.Chat
|
||||
|
||||
if cursor == nil {
|
||||
rows, err := s.q.ListChatsFirstPage(ctx, db.ListChatsFirstPageParams{
|
||||
UserID: userID,
|
||||
Lim: limit + 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
chats = make([]model.Chat, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
chats = append(chats, model.Chat{
|
||||
ID: r.ID,
|
||||
Title: r.Title,
|
||||
CreatedAt: r.CreatedAt,
|
||||
MemberIDs: r.MemberIds,
|
||||
})
|
||||
}
|
||||
} else {
|
||||
decoded, derr := decodeCursor(*cursor, uuid.Nil)
|
||||
if derr != nil {
|
||||
return nil, nil, ErrInvalidCursor
|
||||
}
|
||||
|
||||
rows, err := s.q.ListChatsAfter(ctx, db.ListChatsAfterParams{
|
||||
UserID: userID,
|
||||
AfterCreatedAt: decoded.CreatedAt,
|
||||
AfterID: decoded.ID,
|
||||
Lim: limit + 1,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
chats = make([]model.Chat, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
chats = append(chats, model.Chat{
|
||||
ID: r.ID,
|
||||
Title: r.Title,
|
||||
CreatedAt: r.CreatedAt,
|
||||
MemberIDs: r.MemberIds,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
hasMore := len(chats) > limit
|
||||
if hasMore {
|
||||
chats = chats[:limit]
|
||||
}
|
||||
|
||||
var next *string
|
||||
if hasMore {
|
||||
last := chats[len(chats)-1]
|
||||
c := encodeCursor(Cursor{
|
||||
CreatedAt: last.CreatedAt,
|
||||
ID: last.ID,
|
||||
})
|
||||
next = &c
|
||||
}
|
||||
|
||||
return chats, next, nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var ErrInvalidCursor = errors.New("invalid cursor")
|
||||
|
||||
type Cursor struct {
|
||||
ChatID uuid.UUID `json:"chat_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ID uuid.UUID `json:"id"`
|
||||
}
|
||||
|
||||
func encodeCursor(cursor Cursor) string {
|
||||
data, _ := json.Marshal(cursor)
|
||||
return base64.RawURLEncoding.EncodeToString(data)
|
||||
}
|
||||
|
||||
func decodeCursor(raw string, chatID uuid.UUID) (Cursor, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(raw)
|
||||
if err != nil {
|
||||
return Cursor{}, ErrInvalidCursor
|
||||
}
|
||||
|
||||
var cursor Cursor
|
||||
if err := json.Unmarshal(data, &cursor); err != nil {
|
||||
return Cursor{}, ErrInvalidCursor
|
||||
}
|
||||
|
||||
if cursor.ID == uuid.Nil || cursor.CreatedAt.IsZero() || cursor.ChatID != chatID {
|
||||
return Cursor{}, ErrInvalidCursor
|
||||
}
|
||||
|
||||
return cursor, nil
|
||||
}
|
||||
@@ -0,0 +1,289 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrMessageChatNotFound = errors.New("chat not found")
|
||||
ErrIdempotencyKeyConflict = errors.New("idempotency key reused with different request")
|
||||
)
|
||||
|
||||
type messageQueries interface {
|
||||
CreateIdempotencyKey(ctx context.Context, arg db.CreateIdempotencyKeyParams) (int64, error)
|
||||
CreateMessage(ctx context.Context, arg db.CreateMessageParams) (db.Message, error)
|
||||
CreateMessageAttachment(ctx context.Context, arg db.CreateMessageAttachmentParams) error
|
||||
GetIdempotentMessage(ctx context.Context, arg db.GetIdempotentMessageParams) (db.GetIdempotentMessageRow, error)
|
||||
IsChatMember(ctx context.Context, arg db.IsChatMemberParams) (bool, error)
|
||||
ListAttachmentsByIDs(ctx context.Context, arg db.ListAttachmentsByIDsParams) ([]db.AttachmentStatus, error)
|
||||
ListMessageAttachmentIDs(ctx context.Context, messageID uuid.UUID) ([]uuid.UUID, error)
|
||||
ListMessageAttachmentsByMessageIDs(ctx context.Context, messageIDs []uuid.UUID) ([]db.ListMessageAttachmentsByMessageIDsRow, error)
|
||||
ListMessagesAfter(ctx context.Context, arg db.ListMessagesAfterParams) ([]db.Message, error)
|
||||
ListMessagesFirstPage(ctx context.Context, arg db.ListMessagesFirstPageParams) ([]db.Message, error)
|
||||
}
|
||||
|
||||
type MessageService struct {
|
||||
pool txBeginner
|
||||
q *db.Queries
|
||||
withTx func(pgx.Tx) messageQueries
|
||||
}
|
||||
|
||||
func NewMessageService(pool *pgxpool.Pool, q *db.Queries) *MessageService {
|
||||
return &MessageService{
|
||||
pool: pool,
|
||||
q: q,
|
||||
withTx: func(tx pgx.Tx) messageQueries {
|
||||
return q.WithTx(tx)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (s *MessageService) Create(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
chatID uuid.UUID,
|
||||
key string,
|
||||
text string,
|
||||
attachmentIDs []uuid.UUID,
|
||||
) (model.Message, error) {
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
if len(attachmentIDs) > 0 {
|
||||
statuses, err := s.q.ListAttachmentsByIDs(ctx, db.ListAttachmentsByIDsParams{
|
||||
ChatID: chatID,
|
||||
Ids: attachmentIDs,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
if len(statuses) != len(attachmentIDs) {
|
||||
return model.Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
for _, status := range statuses {
|
||||
if status == db.AttachmentStatusFailed {
|
||||
return model.Message{}, ErrAttachmentNotReady
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
hashInput := text + "|" + normalizeAttachmentIDs(attachmentIDs)
|
||||
sum := sha256.Sum256([]byte(hashInput))
|
||||
hash := hex.EncodeToString(sum[:])
|
||||
path := "/api/v1/chats/" + chatID.String() + "/messages"
|
||||
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
|
||||
q := s.withTx(tx)
|
||||
|
||||
inserted, err := q.CreateIdempotencyKey(ctx, db.CreateIdempotencyKeyParams{
|
||||
UserID: userID,
|
||||
Method: http.MethodPost,
|
||||
Path: path,
|
||||
Key: key,
|
||||
RequestHash: hash,
|
||||
MessageID: id,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
if inserted == 0 {
|
||||
isMember, err := q.IsChatMember(ctx, db.IsChatMemberParams{
|
||||
ChatID: chatID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
if !isMember {
|
||||
return model.Message{}, ErrMessageChatNotFound
|
||||
}
|
||||
|
||||
stored, err := q.GetIdempotentMessage(ctx, db.GetIdempotentMessageParams{
|
||||
UserID: userID,
|
||||
Method: http.MethodPost,
|
||||
Path: path,
|
||||
Key: key,
|
||||
})
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
if stored.RequestHash != hash {
|
||||
return model.Message{}, ErrIdempotencyKeyConflict
|
||||
}
|
||||
|
||||
storedAttachmentIDs, err := q.ListMessageAttachmentIDs(ctx, stored.ID)
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
return model.Message{
|
||||
ID: stored.ID,
|
||||
ChatID: stored.ChatID,
|
||||
SenderID: stored.SenderID,
|
||||
Text: stored.Text,
|
||||
AttachmentIDs: storedAttachmentIDs,
|
||||
CreatedAt: stored.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
row, err := q.CreateMessage(ctx, db.CreateMessageParams{
|
||||
ID: id,
|
||||
ChatID: chatID,
|
||||
SenderID: userID,
|
||||
Text: text,
|
||||
})
|
||||
if postgres.IsNotFound(err) {
|
||||
return model.Message{}, ErrMessageChatNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
for i, attachmentID := range attachmentIDs {
|
||||
if err := q.CreateMessageAttachment(ctx, db.CreateMessageAttachmentParams{
|
||||
MessageID: id,
|
||||
AttachmentID: attachmentID,
|
||||
Position: i,
|
||||
}); err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := tx.Commit(ctx); err != nil {
|
||||
return model.Message{}, err
|
||||
}
|
||||
|
||||
return model.Message{
|
||||
ID: row.ID,
|
||||
ChatID: row.ChatID,
|
||||
SenderID: row.SenderID,
|
||||
Text: row.Text,
|
||||
AttachmentIDs: attachmentIDs,
|
||||
CreatedAt: row.CreatedAt,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *MessageService) List(
|
||||
ctx context.Context,
|
||||
userID uuid.UUID,
|
||||
chatID uuid.UUID,
|
||||
limit int,
|
||||
cursor *string,
|
||||
) ([]model.Message, *string, error) {
|
||||
isMember, err := s.q.IsChatMember(ctx, db.IsChatMemberParams{
|
||||
ChatID: chatID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if !isMember {
|
||||
return nil, nil, ErrMessageChatNotFound
|
||||
}
|
||||
|
||||
var rows []db.Message
|
||||
|
||||
if cursor == nil {
|
||||
rows, err = s.q.ListMessagesFirstPage(ctx, db.ListMessagesFirstPageParams{
|
||||
ChatID: chatID,
|
||||
Lim: limit + 1,
|
||||
})
|
||||
} else {
|
||||
decoded, derr := decodeCursor(*cursor, chatID)
|
||||
if derr != nil {
|
||||
return nil, nil, ErrInvalidCursor
|
||||
}
|
||||
|
||||
rows, err = s.q.ListMessagesAfter(ctx, db.ListMessagesAfterParams{
|
||||
ChatID: chatID,
|
||||
AfterCreatedAt: decoded.CreatedAt,
|
||||
AfterID: decoded.ID,
|
||||
Lim: limit + 1,
|
||||
})
|
||||
}
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
hasMore := len(rows) > limit
|
||||
if hasMore {
|
||||
rows = rows[:limit]
|
||||
}
|
||||
|
||||
attachmentsByMessage := make(map[uuid.UUID][]uuid.UUID, len(rows))
|
||||
if len(rows) > 0 {
|
||||
messageIDs := make([]uuid.UUID, len(rows))
|
||||
for i, r := range rows {
|
||||
messageIDs[i] = r.ID
|
||||
}
|
||||
|
||||
links, err := s.q.ListMessageAttachmentsByMessageIDs(ctx, messageIDs)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
for _, l := range links {
|
||||
attachmentsByMessage[l.MessageID] = append(attachmentsByMessage[l.MessageID], l.AttachmentID)
|
||||
}
|
||||
}
|
||||
|
||||
messages := make([]model.Message, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
messages = append(messages, model.Message{
|
||||
ID: r.ID,
|
||||
ChatID: r.ChatID,
|
||||
SenderID: r.SenderID,
|
||||
Text: r.Text,
|
||||
AttachmentIDs: attachmentsByMessage[r.ID],
|
||||
CreatedAt: r.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
var next *string
|
||||
if hasMore {
|
||||
last := messages[len(messages)-1]
|
||||
c := encodeCursor(Cursor{
|
||||
ChatID: chatID,
|
||||
CreatedAt: last.CreatedAt,
|
||||
ID: last.ID,
|
||||
})
|
||||
next = &c
|
||||
}
|
||||
|
||||
return messages, next, nil
|
||||
}
|
||||
|
||||
func normalizeAttachmentIDs(ids []uuid.UUID) string {
|
||||
if len(ids) == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
parts := make([]string, len(ids))
|
||||
for i, id := range ids {
|
||||
parts[i] = id.String()
|
||||
}
|
||||
sort.Strings(parts)
|
||||
|
||||
return strings.Join(parts, ",")
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type txBeginner interface {
|
||||
Begin(ctx context.Context) (pgx.Tx, error)
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"mtusi2026-messenger/internal/credentials"
|
||||
"mtusi2026-messenger/internal/db"
|
||||
"mtusi2026-messenger/internal/model"
|
||||
"mtusi2026-messenger/internal/postgres"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrUsernameTaken = errors.New("username already taken")
|
||||
ErrUserNotFound = errors.New("user not found")
|
||||
)
|
||||
|
||||
type userQueries interface {
|
||||
CreateUser(ctx context.Context, arg db.CreateUserParams) (db.CreateUserRow, error)
|
||||
GetUser(ctx context.Context, id uuid.UUID) (db.GetUserRow, error)
|
||||
}
|
||||
|
||||
type UserService struct {
|
||||
q userQueries
|
||||
}
|
||||
|
||||
func NewUserService(q userQueries) *UserService {
|
||||
return &UserService{q: q}
|
||||
}
|
||||
|
||||
func (s *UserService) Create(
|
||||
ctx context.Context,
|
||||
username, displayName, plain string,
|
||||
) (model.User, error) {
|
||||
hash, err := credentials.HashPassword(plain)
|
||||
if err != nil {
|
||||
return model.User{}, err
|
||||
}
|
||||
|
||||
id, err := uuid.NewV7()
|
||||
if err != nil {
|
||||
return model.User{}, err
|
||||
}
|
||||
|
||||
row, err := s.q.CreateUser(ctx, db.CreateUserParams{
|
||||
ID: id,
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
PasswordHash: hash,
|
||||
})
|
||||
if err != nil {
|
||||
if postgres.ErrCode(err) == postgres.CodeUniqueViolation {
|
||||
return model.User{}, ErrUsernameTaken
|
||||
}
|
||||
return model.User{}, err
|
||||
}
|
||||
|
||||
return model.User{
|
||||
ID: row.ID,
|
||||
Username: row.Username,
|
||||
DisplayName: row.DisplayName,
|
||||
CreatedAt: row.CreatedAt.UTC(),
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (s *UserService) GetByID(ctx context.Context, id uuid.UUID) (model.User, error) {
|
||||
row, err := s.q.GetUser(ctx, id)
|
||||
if postgres.IsNotFound(err) {
|
||||
return model.User{}, ErrUserNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return model.User{}, err
|
||||
}
|
||||
|
||||
return model.User{
|
||||
ID: row.ID,
|
||||
Username: row.Username,
|
||||
DisplayName: row.DisplayName,
|
||||
CreatedAt: row.CreatedAt.UTC(),
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image"
|
||||
"image/color"
|
||||
"image/draw"
|
||||
"image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"github.com/disintegration/imaging"
|
||||
"github.com/gen2brain/webp"
|
||||
|
||||
"mtusi2026-messenger/internal/db"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrInvalidSource = errors.New("invalid source image")
|
||||
ErrUnsupportedPipelineVersion = errors.New("unsupported pipeline version")
|
||||
)
|
||||
|
||||
type imageVariant struct {
|
||||
Name db.AttachmentVariantName
|
||||
Data []byte
|
||||
ContentType string
|
||||
Width, Height int
|
||||
}
|
||||
|
||||
func (w *Worker) process(ctx context.Context, row db.Attachment) ([]db.CreateAttachmentVariantParams, error) {
|
||||
if row.StagingKey == nil {
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
source, err := w.s3.Get(ctx, *row.StagingKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer source.Body.Close()
|
||||
|
||||
tmp, err := os.CreateTemp("", "attachment-job-*")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
_ = tmp.Close()
|
||||
_ = os.Remove(tmp.Name())
|
||||
}()
|
||||
|
||||
hash := sha256.New()
|
||||
size, err := io.Copy(io.MultiWriter(tmp, hash), io.LimitReader(source.Body, int64(w.maxBytes)+1))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if size != int64(row.SizeBytes) ||
|
||||
hex.EncodeToString(hash.Sum(nil)) != row.Sha256 {
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
select {
|
||||
case w.decodeSlot <- struct{}{}:
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
<-w.decodeSlot
|
||||
return nil, err
|
||||
}
|
||||
|
||||
images, err := runImageProcess(ctx, tmp, w.maxPixels, row.PipelineVersion)
|
||||
<-w.decodeSlot
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := ctx.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := w.s3.Put(ctx, row.S3Key, tmp, size, string(row.ContentType)); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
variants := make([]db.CreateAttachmentVariantParams, 0, len(images))
|
||||
for _, img := range images {
|
||||
key := "variants/" + row.ID.String() + "/v" + strconv.Itoa(row.PipelineVersion) + "/" + string(img.Name)
|
||||
if err := w.s3.Put(ctx, key, bytes.NewReader(img.Data), int64(len(img.Data)), img.ContentType); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
sum := sha256.Sum256(img.Data)
|
||||
variants = append(variants, db.CreateAttachmentVariantParams{
|
||||
AttachmentID: row.ID,
|
||||
PipelineVersion: row.PipelineVersion,
|
||||
Variant: img.Name,
|
||||
S3Key: key,
|
||||
ContentType: img.ContentType,
|
||||
SizeBytes: len(img.Data),
|
||||
Width: img.Width,
|
||||
Height: img.Height,
|
||||
Sha256: hex.EncodeToString(sum[:]),
|
||||
})
|
||||
}
|
||||
|
||||
return variants, nil
|
||||
}
|
||||
|
||||
func imageVariants(r io.ReadSeeker, maxPixels, pipelineVersion int) ([]imageVariant, error) {
|
||||
quality := 80
|
||||
switch pipelineVersion {
|
||||
case 1:
|
||||
case 2:
|
||||
quality = 75
|
||||
default:
|
||||
return nil, ErrUnsupportedPipelineVersion
|
||||
}
|
||||
|
||||
cfg, format, err := image.DecodeConfig(r)
|
||||
if err != nil ||
|
||||
(format != "png" && format != "jpeg") ||
|
||||
cfg.Width <= 0 ||
|
||||
cfg.Height <= 0 ||
|
||||
cfg.Width > maxPixels/cfg.Height {
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
if _, err := r.Seek(0, io.SeekStart); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
src, err := imaging.Decode(r, imaging.AutoOrientation(true))
|
||||
if err != nil {
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
variants := make([]imageVariant, 0, 3)
|
||||
for _, item := range []struct {
|
||||
name db.AttachmentVariantName
|
||||
image image.Image
|
||||
}{
|
||||
{db.AttachmentVariantNameThumbnail, fitImage(src, 256)},
|
||||
{db.AttachmentVariantNameCrop, imaging.Fill(src, 128, 128, imaging.Center, imaging.Lanczos)},
|
||||
{db.AttachmentVariantNameOptimized, fitImage(src, 1280)},
|
||||
} {
|
||||
var data []byte
|
||||
contentType := "image/jpeg"
|
||||
if item.name == db.AttachmentVariantNameOptimized {
|
||||
contentType = "image/webp"
|
||||
data, err = encodeWebP(item.image, quality)
|
||||
} else {
|
||||
data, err = encodeJPEG(item.image, 85)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
variants = append(variants, imageVariant{
|
||||
Name: item.name,
|
||||
ContentType: contentType,
|
||||
Data: data,
|
||||
Width: item.image.Bounds().Dx(),
|
||||
Height: item.image.Bounds().Dy(),
|
||||
})
|
||||
}
|
||||
|
||||
return variants, nil
|
||||
}
|
||||
|
||||
func fitImage(src image.Image, maxSize int) image.Image {
|
||||
width, height := src.Bounds().Dx(), src.Bounds().Dy()
|
||||
if width > maxSize || height > maxSize {
|
||||
if width >= height {
|
||||
height = max(1, height*maxSize/width)
|
||||
width = maxSize
|
||||
} else {
|
||||
width = max(1, width*maxSize/height)
|
||||
height = maxSize
|
||||
}
|
||||
|
||||
return imaging.Resize(src, width, height, imaging.Lanczos)
|
||||
}
|
||||
|
||||
return src
|
||||
}
|
||||
|
||||
func encodeJPEG(src image.Image, quality int) ([]byte, error) {
|
||||
dst := image.NewRGBA(image.Rect(0, 0, src.Bounds().Dx(), src.Bounds().Dy()))
|
||||
draw.Draw(dst, dst.Bounds(), image.NewUniform(color.White), image.Point{}, draw.Src)
|
||||
draw.Draw(dst, dst.Bounds(), src, src.Bounds().Min, draw.Over)
|
||||
|
||||
var out bytes.Buffer
|
||||
if err := jpeg.Encode(&out, dst, &jpeg.Options{Quality: quality}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
|
||||
func encodeWebP(src image.Image, quality int) ([]byte, error) {
|
||||
var out bytes.Buffer
|
||||
if err := webp.Encode(&out, src, webp.Options{Quality: quality, Method: 4}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return out.Bytes(), nil
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
package worker
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"errors"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func ProcessImage(maxPixels, pipelineVersion int) error {
|
||||
variants, err := imageVariants(os.Stdin, maxPixels, pipelineVersion)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return gob.NewEncoder(os.Stdout).Encode(variants)
|
||||
}
|
||||
|
||||
func runImageProcess(ctx context.Context, source *os.File, maxPixels, pipelineVersion int) ([]imageVariant, error) {
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cmd := exec.CommandContext(ctx, executable,
|
||||
"-process-image", strconv.Itoa(maxPixels),
|
||||
"-pipeline-version", strconv.Itoa(pipelineVersion),
|
||||
)
|
||||
cmd.Stdin = source
|
||||
data, err := cmd.Output()
|
||||
if ctx.Err() != nil {
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
var exitErr *exec.ExitError
|
||||
if errors.As(err, &exitErr) && exitErr.ExitCode() == 2 {
|
||||
return nil, ErrInvalidSource
|
||||
}
|
||||
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var variants []imageVariant
|
||||
if err := gob.NewDecoder(bytes.NewReader(data)).Decode(&variants); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return variants, nil
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user