forked from mtusi2026/mtusi2026lab4
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7b19d4691d |
@@ -18,3 +18,10 @@ __pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
target
|
||||
.idea
|
||||
.vscode
|
||||
*.log
|
||||
.m2
|
||||
.gradle
|
||||
build
|
||||
|
||||
+10
-12
@@ -1,20 +1,18 @@
|
||||
# Copy to .env for your LOCAL synthetic test environment; never commit .env.
|
||||
# These are logical interface suggestions; map actual framework names in REPORT.md.
|
||||
APP_HOST=0.0.0.0
|
||||
APP_PORT=8080
|
||||
APP_ENV=lab
|
||||
SESSION_TTL_SECONDS=3600
|
||||
# Choose credentials locally. File paths contain no secret values.
|
||||
DATABASE_URL_FILE=/run/secrets/database_url
|
||||
# Optional on 3, required use on 4 in lab2; Redis OR Valkey:
|
||||
SESSION_STORE=postgres
|
||||
CACHE_URL_FILE=/run/secrets/cache_url
|
||||
API_REPLICAS=2
|
||||
POSTGRES_DB=messenger
|
||||
POSTGRES_USER=messenger
|
||||
POSTGRES_PASSWORD=change-me-local-only
|
||||
DB_REQUEST_TIMEOUT_MS=2000
|
||||
IDEMPOTENCY_TTL_SECONDS=86400
|
||||
S3_ENDPOINT=http://s3:9000
|
||||
S3_REGION=us-east-1
|
||||
# Generate with: openssl rand -base64 32 | tr '+/' '-_' | tr -d '='
|
||||
CURSOR_SECRET=replace-with-url-safe-base64-secret
|
||||
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_ROOT_USER=minioadmin
|
||||
S3_ROOT_PASSWORD=change-me-minio-root
|
||||
S3_ACCESS_KEY_ID=messenger-api
|
||||
S3_SECRET_ACCESS_KEY=change-me-api-secret
|
||||
MAX_UPLOAD_BYTES=10485760
|
||||
MAX_IMAGE_PIXELS=40000000
|
||||
|
||||
@@ -14,6 +14,7 @@ volumes/
|
||||
data/
|
||||
backups/
|
||||
# Runtime/build artifacts
|
||||
.gradle/
|
||||
.venv/
|
||||
__pycache__/
|
||||
*.py[cod]
|
||||
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
FROM eclipse-temurin:21.0.10_7-jdk-jammy@sha256:25d1276565738d3c805e632a4542c3a7598866ef967f4def6544c15de3a74b14 AS build
|
||||
WORKDIR /build
|
||||
COPY gradlew build.gradle settings.gradle gradle.properties gradle.lockfile ./
|
||||
COPY gradle ./gradle
|
||||
RUN chmod +x gradlew && ./gradlew --no-daemon --console=plain resolveDependencies
|
||||
COPY src ./src
|
||||
COPY docker/Healthcheck.java ./docker/Healthcheck.java
|
||||
RUN ./gradlew --no-daemon --console=plain build && javac -d /healthcheck docker/Healthcheck.java
|
||||
|
||||
FROM eclipse-temurin:21.0.10_7-jre-jammy@sha256:fa4854e6057665066cb79953616671c20f32b96a85d6dd8b78db0762203924c4
|
||||
RUN groupadd --gid 10001 messenger && useradd --uid 10001 --gid messenger --no-create-home --shell /usr/sbin/nologin messenger
|
||||
WORKDIR /app
|
||||
COPY --from=build /build/build/libs/messenger.jar /app/messenger.jar
|
||||
COPY --from=build /healthcheck /app/healthcheck
|
||||
ENV APP_HOST=0.0.0.0 APP_PORT=8080 JAVA_TOOL_OPTIONS="-XX:MaxRAMPercentage=60 -XX:+ExitOnOutOfMemoryError"
|
||||
USER 10001:10001
|
||||
EXPOSE 8080
|
||||
STOPSIGNAL SIGTERM
|
||||
HEALTHCHECK --interval=5s --timeout=3s --start-period=30s --retries=3 CMD ["java", "-cp", "/app/healthcheck", "Healthcheck"]
|
||||
ENTRYPOINT ["java", "-jar", "/app/messenger.jar"]
|
||||
@@ -4,10 +4,10 @@ CA_FILE ?=
|
||||
ACTION_SCRIPT ?=
|
||||
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
|
||||
|
||||
.PHONY: help check validate test load test-persistence test-failover
|
||||
.PHONY: help check validate test load test-persistence test-failover test-grade4 test-rolling test-upload-memory compose-up compose-down
|
||||
|
||||
help:
|
||||
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
|
||||
@echo "check: offline structure; validate: full OpenAPI; test: running API; test-grade4: attachment negatives"
|
||||
|
||||
check:
|
||||
$(PYTHON) scripts/check_contract.py
|
||||
@@ -28,3 +28,18 @@ test-persistence:
|
||||
test-failover:
|
||||
@test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable stop-one hook"; exit 2)
|
||||
$(PYTHON) tests/resilience.py --mode failover --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS)
|
||||
|
||||
test-grade4:
|
||||
$(PYTHON) student-tests/lab4.py --base-url "$(BASE_URL)"
|
||||
|
||||
test-rolling:
|
||||
$(PYTHON) student-tests/lab3.py --base-url "$(BASE_URL)" --rolling-update
|
||||
|
||||
test-upload-memory:
|
||||
$(PYTHON) student-tests/upload-memory.py --base-url "$(BASE_URL)" --output evidence/upload-memory.json
|
||||
|
||||
compose-up:
|
||||
docker compose --env-file .env up -d --build --scale api=2
|
||||
|
||||
compose-down:
|
||||
docker compose --env-file .env down
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
Разделить метаданные и бинарное содержимое, добавить изображения в сообщения и сохранить доступ к ним при переключении API-реплики.
|
||||
|
||||
Это **skeleton**, а не готовый сервер. Здесь есть задание, контракт и публичные проверки. Реализацию приложения, Dockerfile и требуемую инфраструктуру пишет студент. Язык и framework свободные; UI не обязателен.
|
||||
Решение реализовано на Java 21, Spring Boot и Gradle. В стенде используются PostgreSQL, Redis, приватное MinIO S3 и HAProxy; две API-реплики сохраняют метаданные вложений в PostgreSQL, а байты — в общем S3 bucket.
|
||||
|
||||
[Карта курса](COURSE.md) · [Процесс fork/PR](CONTRIBUTING.md) · [OpenAPI](contracts/openapi.json) · [Семантика API](contracts/README.md) · [Проверки](tests/README.md) · [Отчёт](REPORT.md) · [Примеры JSON](contracts/examples.json) · [Памятка преподавателю](MAINTAINERS.md)
|
||||
|
||||
@@ -47,21 +47,41 @@
|
||||
## Как начать и проверить
|
||||
|
||||
```sh
|
||||
# Работает прямо в skeleton, Python 3.9+; приложение не запускается:
|
||||
# Проверка структуры:
|
||||
make check
|
||||
|
||||
# После реализации запустите свой сервер/стенд по REPORT.md.
|
||||
# По умолчанию тест использует http://localhost:8080.
|
||||
# Создать локальный .env и задать секреты:
|
||||
cp .env.example .env
|
||||
# Сгенерировать CURSOR_SECRET:
|
||||
openssl rand -base64 32 | tr '+/' '-_' | tr -d '='
|
||||
# Сгенерировать S3_SECRET_ACCESS_KEY и вписать значения в .env.
|
||||
# Секреты в Git не добавлять.
|
||||
|
||||
# Собрать и запустить две API-реплики, MinIO, PostgreSQL, Redis и HAProxy:
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build --scale api=2
|
||||
curl -i http://localhost:${APP_PORT:-8080}/health/ready
|
||||
|
||||
# Проверки:
|
||||
make test
|
||||
make test-grade4
|
||||
make test-upload-memory
|
||||
make test-rolling
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
|
||||
# Остановить без удаления volumes:
|
||||
docker compose --env-file .env down
|
||||
# Удалить учебные volumes намеренно:
|
||||
docker compose --env-file .env down -v
|
||||
```
|
||||
|
||||
`.env.example`, `compose.example.yaml` и `contracts/infrastructure.json` описывают настройки и роли компонентов. **compose.example.yaml — список ролей с пустым services, не запускаемый стенд.** Реализуйте свой `compose.yaml`/`stack.yaml`, закрепите версии образов, заполните локальный `.env` и опишите bootstrap/миграции. Все зависимости поднимаются из этого репозитория; ссылка «у меня БД уже установлена» не заменяет воспроизводимость.
|
||||
|
||||
```sh
|
||||
# Compose-вариант после реализации:
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build
|
||||
# Дождитесь /health/ready; затем make test.
|
||||
# Если в .env выбран другой APP_PORT, укажите его в BASE_URL:
|
||||
make test BASE_URL=http://localhost:8080
|
||||
# Для Swarm укажите эквивалентные build/push/deploy-команды в REPORT.md.
|
||||
```
|
||||
|
||||
@@ -76,6 +96,14 @@ make validate PYTHON=.venv/bin/python
|
||||
|
||||
Smoke не выставляет оценку автоматически. Проверку сохранности/отказов запускайте явно по [tests/README.md](tests/README.md), остальные доказательства приведите в отчёте.
|
||||
|
||||
## Реализация lab4
|
||||
|
||||
- HAProxy публикует единственный внешний порт; API-реплики не имеют `ports`.
|
||||
- MinIO запускается с persistent volume, bucket создаётся идемпотентным `infra/bootstrap-s3.sh` и остаётся приватным. API использует отдельного пользователя с правами только на нужный bucket; root credentials используются только bootstrap-сервисом.
|
||||
- Upload принимает только сырые PNG/JPEG до 10 MiB. Поток сначала ограниченно копируется во временный файл, затем проверяются сигнатура, декодирование и максимум 40 млн пикселей. В память не читается неограниченный body.
|
||||
- В PostgreSQL хранятся `attachments` и связь `message_attachments`; object key генерируется UUID и не зависит от filename. Download выполняется только через авторизованный API и проверку участия в чате.
|
||||
- После S3 PUT выполняется запись метаданных. При ошибке DB выполняется best-effort delete; периодическая сверка/удаление orphan-объектов описана в REPORT.md и запускается с grace period вручную.
|
||||
|
||||
## Сценарий защиты
|
||||
|
||||
1. Загрузить fixture, отправить message с attachment_ids, скачать как второй участник и сравнить SHA-256.
|
||||
|
||||
@@ -1,37 +1,73 @@
|
||||
# Отчёт — лабораторная 4
|
||||
|
||||
- Автор, группа: TODO
|
||||
- Целевая оценка; выбранный трек 5, если нужен: TODO
|
||||
- Commit/PR: TODO после публикации
|
||||
- Использованные источники, библиотеки и помощь ИИ: TODO
|
||||
- Автор, группа: Гусак Дмитрий, БПИ2303
|
||||
- Целевая оценка: 4; трек 5 не заявлен
|
||||
- Commit/PR: заполню после публикации форка
|
||||
- Источники: документация Docker Compose, MinIO S3, Spring Boot и Java ImageIO; ИИ использовался для разбора контракта, проектирования и проверки реализации.
|
||||
|
||||
## Запуск из чистого clone
|
||||
|
||||
TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
|
||||
Требуется Docker Desktop с Compose. Для запуска из чистого clone:
|
||||
|
||||
```sh
|
||||
cp .env.example .env
|
||||
openssl rand -base64 32 | tr '+/' '-_' | tr -d '='
|
||||
# вставить результат в CURSOR_SECRET; задать локальные S3_* credentials
|
||||
docker compose --env-file .env config --quiet
|
||||
docker compose --env-file .env up -d --build --scale api=2
|
||||
curl -i http://localhost:${APP_PORT:-8080}/health/ready
|
||||
make check
|
||||
make validate PYTHON=.venv/bin/python
|
||||
make test
|
||||
make test-grade4
|
||||
make test-persistence ACTION_SCRIPT=scripts/restart.sh
|
||||
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
|
||||
```
|
||||
|
||||
Сервисы: `lb`, `api` x2, `migrate`, `postgres`, `redis`, `minio`, `s3-bootstrap`. API-порты наружу не публикуются; наружу опубликован только HAProxy. `s3-bootstrap` создаёт приватный bucket, пользователя и ограниченную policy. Остановка без удаления данных: `docker compose --env-file .env down`; намеренное удаление учебных volumes: `docker compose --env-file .env down -v`. Секреты в отчёт не помещаются.
|
||||
|
||||
## Архитектура и решения
|
||||
|
||||
TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab3, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
|
||||
Схема стенда:
|
||||
|
||||
```text
|
||||
Клиент → HAProxy → api-1/api-2 → PostgreSQL (метаданные) и Redis (сессии)
|
||||
↘ MinIO (байты вложений)
|
||||
```
|
||||
|
||||
Перенесены все обязательные возможности lab3: две реплики, балансировка, `X-Instance-Id`, общие сессии Redis, идемпотентность сообщений, Flyway, failover и graceful shutdown. В lab4 добавлены MinIO, V3-миграция, таблицы `attachments`/`message_attachments`, upload/download и attachment_ids в сообщении.
|
||||
|
||||
PostgreSQL является источником истины для метаданных и связей. MinIO хранит только оригинальные байты в приватном bucket. Object key генерируется сервером как `original/<uuid>`; filename используется только как отображаемое имя. Upload ограниченно копирует поток во временный файл, проверяет Content-Type, magic bytes, декодирование ImageIO, размер до 10 MiB и до 40 млн пикселей, затем выполняет S3 PUT и вставку метаданных. При ошибке вставки выполняется best-effort компенсационный DELETE. Для редких orphan-объектов предусмотрена ручная сверка после grace period: удалять можно только объекты, отсутствующие в PostgreSQL старше 24 часов; свежие объекты не трогать.
|
||||
|
||||
API использует отдельного MinIO-пользователя с `ListBucket/GetBucketLocation/GetObject/PutObject/DeleteObject` только для `messenger-attachments`; root доступ используется только bootstrap-контейнером. Консоль MinIO не опубликована.
|
||||
|
||||
## Доказательства критериев
|
||||
|
||||
| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
|
||||
| --- | --- | --- | --- |
|
||||
| Публичный smoke | make test | TODO | TODO |
|
||||
| Остальные пункты уровня 3 | TODO | TODO | TODO |
|
||||
| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
|
||||
| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
|
||||
| Контракт | `make check` | PASS | вывод команды |
|
||||
| OpenAPI | `make validate PYTHON=.venv/bin/python` | PASS | вывод команды |
|
||||
| Публичный smoke | `make test BASE_URL=http://localhost:8083` | PASS: lab4 public API scenario | вывод команды |
|
||||
| Upload/download | `make test` | PNG сохранён и скачан побайтно, checksum совпал | вывод команды |
|
||||
| Уровень 4 | `make test-grade4 BASE_URL=http://localhost:8083` | PASS: лимиты, повреждённый файл, MIME spoofing, чужой attachment и другой чат | вывод команды |
|
||||
| Память upload | `make test-upload-memory BASE_URL=http://localhost:8083` | 10/10 одновременных upload, peak API 382.80 MiB | `evidence/upload-memory.json` |
|
||||
| Persistence | `make test-persistence ACTION_SCRIPT=scripts/restart.sh BASE_URL=http://localhost:8083` | PASS: данные, сессия и вложение сохранены | вывод команды |
|
||||
| Failover | `make test-failover ACTION_SCRIPT=scripts/stop-one.sh BASE_URL=http://localhost:8083` | PASS: 2 реплики, 126 успешных записей, 0 ошибок, восстановление 0.708 с; attachment скачан после отказа | вывод команды |
|
||||
| Rolling update | `make test-rolling BASE_URL=http://localhost:8083` | PASS под нагрузкой | `evidence/rolling-load.json` |
|
||||
|
||||
TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
|
||||
Проверки запускались через HAProxy; API-реплики не имели опубликованных host-портов. Секреты и токены в evidence и отчёт не записывались.
|
||||
|
||||
## Эксперименты и отказы
|
||||
|
||||
TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
|
||||
Функциональная проверка выполнена на локальном стенде: Java 21/Spring Boot, PostgreSQL 16.4, Redis 7.4.2, MinIO и две API-реплики. Публичный smoke прошёл; отдельный grade4-тест проверил успешный upload/download и негативные сценарии. Эксперимент `make test-upload-memory` выполнил 10 одновременных upload: 10/10 успешны, измеренный пик API составил 382.80 MiB при лимите 512 MiB. Upload выполнялся потоково во временный файл, поэтому размер request body ограничивается до декодирования изображения. Сборка Java: `./gradlew --no-daemon --console=plain build` — BUILD SUCCESSFUL.
|
||||
|
||||
| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
|
||||
| --- | --- | --- | --- |
|
||||
| TODO | TODO | TODO | TODO |
|
||||
| S3 недоступен до API | API не должен считать вложение ready | S3Storage возвращает контролируемый 503; bucket bootstrap выполняется до API | данные не подтверждаются без S3 PUT |
|
||||
| Ошибка DB после S3 PUT | Не оставить бесконечный orphan | выполняется best-effort DELETE; ручная сверка удаляет только объекты старше 24 часов без метаданных | свежие объекты защищены grace period |
|
||||
| Чужой пользователь/чат | 404 без раскрытия объекта | проверено grade4-тестом | данные не раскрываются |
|
||||
| 10 MiB+, повреждённый или spoofed MIME | 413/415, не 500 | проверено grade4-тестом | объект не создаётся |
|
||||
|
||||
## Ограничения и следующие шаги
|
||||
|
||||
TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
|
||||
Стенд выдерживает отказ одной API-реплики и restart с сохранением PostgreSQL, Redis и S3 volumes при запуске полного Compose. MinIO, PostgreSQL и HAProxy остаются одиночными компонентами одного Docker-хоста; отказ самого узла и TLS не заявляются. Уровень 5 и presigned upload не реализованы. Сверка orphan-объектов документирована как безопасная ручная процедура с grace period, но не является отдельным фоновым worker. Это учебное ограничение, необходимое для уровня 4.
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
plugins {
|
||||
id 'java'
|
||||
id 'org.springframework.boot' version '3.5.16'
|
||||
}
|
||||
|
||||
group = 'ru.mtusi'
|
||||
version = '2.0.0'
|
||||
description = 'Lab 4 messenger with private S3 attachments'
|
||||
|
||||
java { toolchain { languageVersion = JavaLanguageVersion.of(21) } }
|
||||
repositories { mavenCentral() }
|
||||
|
||||
dependencies {
|
||||
implementation platform('org.springframework.boot:spring-boot-dependencies:3.5.16')
|
||||
implementation 'org.springframework.boot:spring-boot-starter-web'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-jdbc'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
|
||||
implementation 'org.flywaydb:flyway-core'
|
||||
implementation 'org.flywaydb:flyway-database-postgresql'
|
||||
implementation 'org.postgresql:postgresql'
|
||||
implementation 'org.springframework.security:spring-security-crypto'
|
||||
implementation 'org.bouncycastle:bcprov-jdk18on:1.80'
|
||||
implementation 'io.minio:minio:8.5.17'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
|
||||
}
|
||||
|
||||
dependencyLocking { lockAllConfigurations(); lockMode = LockMode.STRICT }
|
||||
tasks.withType(JavaCompile).configureEach { options.release = 21; options.encoding = 'UTF-8'; options.compilerArgs.add('-parameters') }
|
||||
tasks.withType(AbstractArchiveTask).configureEach { preserveFileTimestamps = false; reproducibleFileOrder = true }
|
||||
tasks.named('bootJar') { archiveFileName = 'messenger.jar' }
|
||||
tasks.named('jar') { enabled = false }
|
||||
tasks.named('test') { useJUnitPlatform(); maxHeapSize = '256m'; testLogging { events 'passed','skipped','failed'; exceptionFormat = 'full' } }
|
||||
tasks.register('resolveDependencies') { doLast { configurations.findAll { it.canBeResolved }.each { it.resolve() } } }
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:16.4-alpine
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB:-messenger}
|
||||
POSTGRES_USER: ${POSTGRES_USER:-messenger}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger-dev-only}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
cpus: "1.0"
|
||||
mem_limit: 512m
|
||||
|
||||
redis:
|
||||
image: redis:7.4.2-alpine
|
||||
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec"]
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 15
|
||||
cpus: "0.5"
|
||||
mem_limit: 256m
|
||||
|
||||
minio:
|
||||
image: minio/minio:RELEASE.2024-06-13T22-53-53Z
|
||||
command: server /data --console-address ":9001"
|
||||
environment:
|
||||
MINIO_ROOT_USER: ${S3_ROOT_USER:-minioadmin}
|
||||
MINIO_ROOT_PASSWORD: ${S3_ROOT_PASSWORD:-minioadmin123}
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "curl -fsS http://127.0.0.1:9000/minio/health/live >/dev/null"]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
cpus: "1.0"
|
||||
mem_limit: 512m
|
||||
|
||||
s3-bootstrap:
|
||||
image: minio/mc:RELEASE.2024-06-13T15-53-12Z
|
||||
depends_on:
|
||||
minio:
|
||||
condition: service_healthy
|
||||
environment:
|
||||
S3_ROOT_USER: ${S3_ROOT_USER:-minioadmin}
|
||||
S3_ROOT_PASSWORD: ${S3_ROOT_PASSWORD:-minioadmin123}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
volumes:
|
||||
- ./infra/s3-policy.json:/tmp/s3-policy.json:ro
|
||||
- ./infra/bootstrap-s3.sh:/bootstrap-s3.sh:ro
|
||||
entrypoint: ["/bin/sh", "/bootstrap-s3.sh"]
|
||||
restart: "no"
|
||||
cpus: "0.25"
|
||||
mem_limit: 128m
|
||||
|
||||
migrate:
|
||||
image: mtusi-messenger:lab4
|
||||
environment:
|
||||
DB_HOST: postgres
|
||||
DB_PORT: 5432
|
||||
DB_NAME: ${POSTGRES_DB:-messenger}
|
||||
DB_USERNAME: ${POSTGRES_USER:-messenger}
|
||||
DB_PASSWORD: ${POSTGRES_PASSWORD:-messenger-dev-only}
|
||||
CURSOR_SECRET: ${CURSOR_SECRET:?Set CURSOR_SECRET in .env}
|
||||
S3_ENDPOINT: http://minio:9000
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env}
|
||||
MIGRATION_ONLY: "true"
|
||||
SPRING_MAIN_WEB_APPLICATION_TYPE: none
|
||||
depends_on:
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
restart: "no"
|
||||
cpus: "0.5"
|
||||
mem_limit: 256m
|
||||
|
||||
api:
|
||||
build: .
|
||||
image: mtusi-messenger:lab4
|
||||
environment:
|
||||
APP_HOST: 0.0.0.0
|
||||
APP_PORT: 8080
|
||||
DB_HOST: postgres
|
||||
DB_PORT: 5432
|
||||
DB_NAME: ${POSTGRES_DB:-messenger}
|
||||
DB_USERNAME: ${POSTGRES_USER:-messenger}
|
||||
DB_PASSWORD: ${POSTGRES_PASSWORD:-messenger-dev-only}
|
||||
REDIS_HOST: redis
|
||||
REDIS_PORT: 6379
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
DB_REQUEST_TIMEOUT_MS: ${DB_REQUEST_TIMEOUT_MS:-2000}
|
||||
CURSOR_SECRET: ${CURSOR_SECRET:?Set CURSOR_SECRET in .env}
|
||||
S3_ENDPOINT: http://minio:9000
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID: ${S3_ACCESS_KEY_ID:?Set S3_ACCESS_KEY_ID in .env}
|
||||
S3_SECRET_ACCESS_KEY: ${S3_SECRET_ACCESS_KEY:?Set S3_SECRET_ACCESS_KEY in .env}
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
postgres:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
minio:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD", "java", "-cp", "/app/healthcheck", "Healthcheck"]
|
||||
interval: 1s
|
||||
timeout: 2s
|
||||
start_period: 20s
|
||||
retries: 3
|
||||
stop_grace_period: 10s
|
||||
cpus: "1.0"
|
||||
mem_limit: 512m
|
||||
|
||||
lb:
|
||||
image: haproxy:3.1.8-alpine
|
||||
ports:
|
||||
- "127.0.0.1:${APP_PORT:-8080}:8080"
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "wget -q -O - http://127.0.0.1:8080/health/live >/dev/null"]
|
||||
interval: 2s
|
||||
timeout: 2s
|
||||
retries: 5
|
||||
stop_grace_period: 10s
|
||||
cpus: "0.5"
|
||||
mem_limit: 128m
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
s3_data:
|
||||
@@ -0,0 +1,20 @@
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URI;
|
||||
|
||||
/** Compiled in the build stage; works in a JRE without curl, shell parsing or extra packages. */
|
||||
public class Healthcheck {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
String port = System.getenv().getOrDefault("APP_PORT", "8080");
|
||||
HttpURLConnection connection = (HttpURLConnection) URI.create(
|
||||
"http://127.0.0.1:" + port + "/health/ready").toURL().openConnection();
|
||||
connection.setConnectTimeout(1000);
|
||||
connection.setReadTimeout(1000);
|
||||
int status = connection.getResponseCode();
|
||||
connection.disconnect();
|
||||
System.exit(status == 200 ? 0 : 1);
|
||||
} catch (Exception exception) {
|
||||
System.exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"lab": 4,
|
||||
"workload": "closed-loop POST messages; no automatic retries",
|
||||
"requested_duration_seconds": 20.0,
|
||||
"elapsed_seconds": 20.005,
|
||||
"concurrency": 4,
|
||||
"successful_requests": 3657,
|
||||
"failed_requests": 10,
|
||||
"error_ratio": 0.002727,
|
||||
"successful_rps": 182.804,
|
||||
"successful_latency_ms": {
|
||||
"p50": 6.701,
|
||||
"p95": 69.65,
|
||||
"p99": 87.158
|
||||
},
|
||||
"note": "Latency percentiles cover successes only. Timeouts/errors are counted separately; closed-loop has coordinated omission and client overhead."
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"concurrency": 10,
|
||||
"successful_uploads": 10,
|
||||
"fixture_bytes": 111817,
|
||||
"elapsed_seconds": 2.065,
|
||||
"api_peak_memory_mib": 382.8,
|
||||
"note": "Docker stats samples API containers every 100ms; local measurement, not a capacity benchmark."
|
||||
}
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
# This is a Gradle generated file for dependency locking.
|
||||
# Manual edits can break the build and are not advised.
|
||||
# This file is expected to be part of source control.
|
||||
ch.qos.logback:logback-classic:1.5.34=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
ch.qos.logback:logback-core:1.5.34=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.carrotsearch.thirdparty:simple-xml-safe:2.7.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.fasterxml.jackson.core:jackson-annotations:2.21=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.fasterxml.jackson.core:jackson-core:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.fasterxml.jackson.core:jackson-databind:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.fasterxml.jackson.dataformat:jackson-dataformat-toml:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.fasterxml.jackson.datatype:jackson-datatype-jdk8:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.fasterxml.jackson.module:jackson-module-parameter-names:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.fasterxml.jackson:jackson-bom:2.21.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
com.google.code.findbugs:jsr305:3.0.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.google.errorprone:error_prone_annotations:2.28.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.google.guava:failureaccess:1.0.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.google.guava:guava:33.3.1-jre=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.google.j2objc:j2objc-annotations:3.0.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.jayway.jsonpath:json-path:2.9.0=testCompileClasspath,testRuntimeClasspath
|
||||
com.squareup.okhttp3:okhttp:4.12.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.squareup.okio:okio-jvm:3.6.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.squareup.okio:okio:3.6.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
com.vaadin.external.google:android-json:0.0.20131108.vaadin1=testCompileClasspath,testRuntimeClasspath
|
||||
com.zaxxer:HikariCP:6.3.3=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
commons-codec:commons-codec:1.18.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
commons-io:commons-io:2.16.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
io.lettuce:lettuce-core:6.6.0.RELEASE=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.micrometer:micrometer-commons:1.15.12=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.micrometer:micrometer-observation:1.15.12=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.minio:minio:8.5.17=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
io.netty:netty-buffer:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.netty:netty-codec:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.netty:netty-common:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.netty:netty-handler:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.netty:netty-resolver:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.netty:netty-transport-native-unix-common:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.netty:netty-transport:4.1.135.Final=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
io.projectreactor:reactor-core:3.7.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
jakarta.activation:jakarta.activation-api:2.1.4=productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
jakarta.annotation:jakarta.annotation-api:2.1.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
jakarta.xml.bind:jakarta.xml.bind-api:4.0.5=productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
net.bytebuddy:byte-buddy-agent:1.17.8=testCompileClasspath,testRuntimeClasspath
|
||||
net.bytebuddy:byte-buddy:1.18.3=testCompileClasspath,testRuntimeClasspath
|
||||
net.minidev:accessors-smart:2.5.2=testCompileClasspath,testRuntimeClasspath
|
||||
net.minidev:json-smart:2.5.2=testCompileClasspath,testRuntimeClasspath
|
||||
org.apache.commons:commons-compress:1.27.1=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.apache.commons:commons-lang3:3.17.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.apache.logging.log4j:log4j-api:2.24.3=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.apache.logging.log4j:log4j-to-slf4j:2.24.3=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.apache.tomcat.embed:tomcat-embed-core:10.1.55=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.apache.tomcat.embed:tomcat-embed-el:10.1.55=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.apache.tomcat.embed:tomcat-embed-websocket:10.1.55=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.apiguardian:apiguardian-api:1.1.2=testCompileClasspath
|
||||
org.assertj:assertj-core:3.27.7=testCompileClasspath,testRuntimeClasspath
|
||||
org.awaitility:awaitility:4.2.2=testCompileClasspath,testRuntimeClasspath
|
||||
org.bouncycastle:bcprov-jdk18on:1.80=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.checkerframework:checker-qual:3.43.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.flywaydb:flyway-core:11.7.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.flywaydb:flyway-database-postgresql:11.7.2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.hamcrest:hamcrest:3.0=testCompileClasspath,testRuntimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-stdlib-common:1.9.25=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.9.25=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.9.25=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.jetbrains.kotlin:kotlin-stdlib:1.9.25=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.jetbrains:annotations:13.0=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.junit.jupiter:junit-jupiter-api:5.12.2=testCompileClasspath,testRuntimeClasspath
|
||||
org.junit.jupiter:junit-jupiter-engine:5.12.2=testRuntimeClasspath
|
||||
org.junit.jupiter:junit-jupiter-params:5.12.2=testCompileClasspath,testRuntimeClasspath
|
||||
org.junit.jupiter:junit-jupiter:5.12.2=testCompileClasspath,testRuntimeClasspath
|
||||
org.junit.platform:junit-platform-commons:1.12.2=testCompileClasspath,testRuntimeClasspath
|
||||
org.junit.platform:junit-platform-engine:1.12.2=testRuntimeClasspath
|
||||
org.junit.platform:junit-platform-launcher:1.12.2=testRuntimeClasspath
|
||||
org.junit:junit-bom:5.12.2=testCompileClasspath,testRuntimeClasspath
|
||||
org.mockito:mockito-core:5.17.0=testCompileClasspath,testRuntimeClasspath
|
||||
org.mockito:mockito-junit-jupiter:5.17.0=testCompileClasspath,testRuntimeClasspath
|
||||
org.objenesis:objenesis:3.3=testRuntimeClasspath
|
||||
org.opentest4j:opentest4j:1.3.0=testCompileClasspath,testRuntimeClasspath
|
||||
org.ow2.asm:asm:9.7.1=testCompileClasspath,testRuntimeClasspath
|
||||
org.postgresql:postgresql:42.7.11=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.reactivestreams:reactive-streams:1.0.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.skyscreamer:jsonassert:1.5.3=testCompileClasspath,testRuntimeClasspath
|
||||
org.slf4j:jul-to-slf4j:2.0.18=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.slf4j:slf4j-api:2.0.18=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-autoconfigure:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-dependencies:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-starter-data-redis:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-starter-jdbc:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-starter-json:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-starter-logging:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-starter-test:3.5.16=testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-starter-tomcat:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-starter-web:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-starter:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-test-autoconfigure:3.5.16=testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot-test:3.5.16=testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.boot:spring-boot:3.5.16=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.data:spring-data-commons:3.5.13=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.data:spring-data-keyvalue:3.5.13=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.data:spring-data-redis:3.5.13=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework.security:spring-security-crypto:6.5.11=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-aop:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-beans:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-context-support:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-context:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-core:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-expression:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-jcl:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-jdbc:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-oxm:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-test:6.2.19=testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-tx:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-web:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.springframework:spring-webmvc:6.2.19=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
org.xerial.snappy:snappy-java:1.1.10.7=compileClasspath,productionRuntimeClasspath,runtimeClasspath
|
||||
org.xmlunit:xmlunit-core:2.10.4=testCompileClasspath,testRuntimeClasspath
|
||||
org.yaml:snakeyaml:2.4=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
redis.clients.authentication:redis-authx-core:0.1.1-beta2=compileClasspath,productionRuntimeClasspath,runtimeClasspath,testCompileClasspath,testRuntimeClasspath
|
||||
empty=annotationProcessor,developmentOnly,testAndDevelopmentOnly,testAnnotationProcessor
|
||||
@@ -0,0 +1,2 @@
|
||||
org.gradle.jvmargs=-Xmx512m -Dfile.encoding=UTF-8
|
||||
org.gradle.workers.max=2
|
||||
Vendored
BIN
Binary file not shown.
+8
@@ -0,0 +1,8 @@
|
||||
distributionBase=GRADLE_USER_HOME
|
||||
distributionPath=wrapper/dists
|
||||
distributionUrl=https\://services.gradle.org/distributions/gradle-8.14.4-bin.zip
|
||||
distributionSha256Sum=f1771298a70f6db5a29daf62378c4e18a17fc33c9ba6b14362e0cdf40610380d
|
||||
networkTimeout=60000
|
||||
validateDistributionUrl=true
|
||||
zipStoreBase=GRADLE_USER_HOME
|
||||
zipStorePath=wrapper/dists
|
||||
@@ -0,0 +1,251 @@
|
||||
#!/bin/sh
|
||||
|
||||
#
|
||||
# Copyright © 2015-2021 the original authors.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# https://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
##############################################################################
|
||||
#
|
||||
# Gradle start up script for POSIX generated by Gradle.
|
||||
#
|
||||
# Important for running:
|
||||
#
|
||||
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||
# noncompliant, but you have some other compliant shell such as ksh or
|
||||
# bash, then to run this script, type that shell name before the whole
|
||||
# command line, like:
|
||||
#
|
||||
# ksh Gradle
|
||||
#
|
||||
# Busybox and similar reduced shells will NOT work, because this script
|
||||
# requires all of these POSIX shell features:
|
||||
# * functions;
|
||||
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||
# * compound commands having a testable exit status, especially «case»;
|
||||
# * various built-in commands including «command», «set», and «ulimit».
|
||||
#
|
||||
# Important for patching:
|
||||
#
|
||||
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||
#
|
||||
# The "traditional" practice of packing multiple parameters into a
|
||||
# space-separated string is a well documented source of bugs and security
|
||||
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||
# options in "$@", and eventually passing that to Java.
|
||||
#
|
||||
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||
# see the in-line comments for details.
|
||||
#
|
||||
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||
# Darwin, MinGW, and NonStop.
|
||||
#
|
||||
# (3) This script is generated from the Groovy template
|
||||
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||
# within the Gradle project.
|
||||
#
|
||||
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||
#
|
||||
##############################################################################
|
||||
|
||||
# Attempt to set APP_HOME
|
||||
|
||||
# Resolve links: $0 may be a link
|
||||
app_path=$0
|
||||
|
||||
# Need this for daisy-chained symlinks.
|
||||
while
|
||||
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||
[ -h "$app_path" ]
|
||||
do
|
||||
ls=$( ls -ld "$app_path" )
|
||||
link=${ls#*' -> '}
|
||||
case $link in #(
|
||||
/*) app_path=$link ;; #(
|
||||
*) app_path=$APP_HOME$link ;;
|
||||
esac
|
||||
done
|
||||
|
||||
# This is normally unused
|
||||
# shellcheck disable=SC2034
|
||||
APP_BASE_NAME=${0##*/}
|
||||
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s\n' "$PWD" ) || exit
|
||||
|
||||
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||
MAX_FD=maximum
|
||||
|
||||
warn () {
|
||||
echo "$*"
|
||||
} >&2
|
||||
|
||||
die () {
|
||||
echo
|
||||
echo "$*"
|
||||
echo
|
||||
exit 1
|
||||
} >&2
|
||||
|
||||
# OS specific support (must be 'true' or 'false').
|
||||
cygwin=false
|
||||
msys=false
|
||||
darwin=false
|
||||
nonstop=false
|
||||
case "$( uname )" in #(
|
||||
CYGWIN* ) cygwin=true ;; #(
|
||||
Darwin* ) darwin=true ;; #(
|
||||
MSYS* | MINGW* ) msys=true ;; #(
|
||||
NONSTOP* ) nonstop=true ;;
|
||||
esac
|
||||
|
||||
CLASSPATH="\\\"\\\""
|
||||
|
||||
|
||||
# Determine the Java command to use to start the JVM.
|
||||
if [ -n "$JAVA_HOME" ] ; then
|
||||
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||
# IBM's JDK on AIX uses strange locations for the executables
|
||||
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||
else
|
||||
JAVACMD=$JAVA_HOME/bin/java
|
||||
fi
|
||||
if [ ! -x "$JAVACMD" ] ; then
|
||||
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
else
|
||||
JAVACMD=java
|
||||
if ! command -v java >/dev/null 2>&1
|
||||
then
|
||||
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||
|
||||
Please set the JAVA_HOME variable in your environment to match the
|
||||
location of your Java installation."
|
||||
fi
|
||||
fi
|
||||
|
||||
# Increase the maximum file descriptors if we can.
|
||||
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||
case $MAX_FD in #(
|
||||
max*)
|
||||
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
MAX_FD=$( ulimit -H -n ) ||
|
||||
warn "Could not query maximum file descriptor limit"
|
||||
esac
|
||||
case $MAX_FD in #(
|
||||
'' | soft) :;; #(
|
||||
*)
|
||||
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||
# shellcheck disable=SC2039,SC3045
|
||||
ulimit -n "$MAX_FD" ||
|
||||
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||
esac
|
||||
fi
|
||||
|
||||
# Collect all arguments for the java command, stacking in reverse order:
|
||||
# * args from the command line
|
||||
# * the main class name
|
||||
# * -classpath
|
||||
# * -D...appname settings
|
||||
# * --module-path (only if needed)
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||
|
||||
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||
if "$cygwin" || "$msys" ; then
|
||||
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||
|
||||
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||
|
||||
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||
for arg do
|
||||
if
|
||||
case $arg in #(
|
||||
-*) false ;; # don't mess with options #(
|
||||
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||
[ -e "$t" ] ;; #(
|
||||
*) false ;;
|
||||
esac
|
||||
then
|
||||
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||
fi
|
||||
# Roll the args list around exactly as many times as the number of
|
||||
# args, so each arg winds up back in the position where it started, but
|
||||
# possibly modified.
|
||||
#
|
||||
# NB: a `for` loop captures its iteration list before it begins, so
|
||||
# changing the positional parameters here affects neither the number of
|
||||
# iterations, nor the values presented in `arg`.
|
||||
shift # remove old arg
|
||||
set -- "$@" "$arg" # push replacement arg
|
||||
done
|
||||
fi
|
||||
|
||||
|
||||
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||
DEFAULT_JVM_OPTS='-Dfile.encoding=UTF-8 "-Xmx64m" "-Xms64m"'
|
||||
|
||||
# Collect all arguments for the java command:
|
||||
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||
# and any embedded shellness will be escaped.
|
||||
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||
# treated as '${Hostname}' itself on the command line.
|
||||
|
||||
set -- \
|
||||
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||
-classpath "$CLASSPATH" \
|
||||
-jar "$APP_HOME/gradle/wrapper/gradle-wrapper.jar" \
|
||||
"$@"
|
||||
|
||||
# Stop when "xargs" is not available.
|
||||
if ! command -v xargs >/dev/null 2>&1
|
||||
then
|
||||
die "xargs is not available"
|
||||
fi
|
||||
|
||||
# Use "xargs" to parse quoted args.
|
||||
#
|
||||
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||
#
|
||||
# In Bash we could simply go:
|
||||
#
|
||||
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||
# set -- "${ARGS[@]}" "$@"
|
||||
#
|
||||
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||
# character that might be a shell metacharacter, then use eval to reverse
|
||||
# that process (while maintaining the separation between arguments), and wrap
|
||||
# the whole thing up as a single "set" statement.
|
||||
#
|
||||
# This will of course break if any of these variables contains a newline or
|
||||
# an unmatched quote.
|
||||
#
|
||||
|
||||
eval "set -- $(
|
||||
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||
xargs -n1 |
|
||||
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||
tr '\n' ' '
|
||||
)" '"$@"'
|
||||
|
||||
exec "$JAVACMD" "$@"
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
mc alias set local http://minio:9000 "$S3_ROOT_USER" "$S3_ROOT_PASSWORD"
|
||||
mc mb --ignore-existing "local/$S3_BUCKET"
|
||||
mc admin policy create local messenger-api-policy-v2 /tmp/s3-policy.json || true
|
||||
mc admin user add local "$S3_ACCESS_KEY_ID" "$S3_SECRET_ACCESS_KEY" || true
|
||||
mc admin policy attach local messenger-api-policy-v2 --user "$S3_ACCESS_KEY_ID"
|
||||
@@ -0,0 +1,29 @@
|
||||
global
|
||||
log stdout format raw local0
|
||||
maxconn 2048
|
||||
|
||||
defaults
|
||||
mode http
|
||||
option httplog
|
||||
timeout http-request 5s
|
||||
timeout connect 1s
|
||||
timeout client 30s
|
||||
timeout server 30s
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
|
||||
resolvers docker
|
||||
nameserver dns 127.0.0.11:53
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 5s
|
||||
|
||||
frontend public
|
||||
bind :8080
|
||||
default_backend api_backend
|
||||
|
||||
backend api_backend
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
server-template api 4 api:8080 check inter 1s fall 2 rise 2 resolvers docker init-addr last,libc,none
|
||||
@@ -0,0 +1,15 @@
|
||||
{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [
|
||||
{
|
||||
"Action": ["s3:ListBucket", "s3:GetBucketLocation"],
|
||||
"Effect": "Allow",
|
||||
"Resource": ["arn:aws:s3:::messenger-attachments"]
|
||||
},
|
||||
{
|
||||
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
|
||||
"Effect": "Allow",
|
||||
"Resource": ["arn:aws:s3:::messenger-attachments/*"]
|
||||
}
|
||||
]
|
||||
}
|
||||
Executable
+5
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
# Restart API replicas and shared stores without removing named volumes.
|
||||
docker compose --env-file .env restart api postgres redis minio lb
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
# Restart one API container at a time and wait for its healthcheck.
|
||||
for container in $(docker compose --env-file .env ps -q api); do
|
||||
docker restart "$container" >/dev/null
|
||||
deadline=$(( $(date +%s) + 60 ))
|
||||
while :; do
|
||||
status=$(docker inspect --format '{{.State.Health.Status}}' "$container" 2>/dev/null || true)
|
||||
[ "$status" = "healthy" ] && break
|
||||
[ "$(date +%s)" -lt "$deadline" ] || exit 1
|
||||
sleep 1
|
||||
done
|
||||
done
|
||||
Executable
+14
@@ -0,0 +1,14 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
|
||||
|
||||
for container in $(docker compose --env-file .env ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container")
|
||||
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
|
||||
docker stop "$container" >/dev/null
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
|
||||
exit 2
|
||||
@@ -0,0 +1 @@
|
||||
rootProject.name = 'messenger'
|
||||
@@ -0,0 +1,74 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||
import org.springframework.web.HttpMediaTypeNotSupportedException;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.bind.MissingServletRequestParameterException;
|
||||
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
|
||||
import org.springframework.web.servlet.resource.NoResourceFoundException;
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ApiErrors {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(ApiErrors.class);
|
||||
|
||||
@ExceptionHandler(ApiException.class)
|
||||
ResponseEntity<ErrorBody> application(ApiException error, HttpServletRequest request) {
|
||||
return response(error.status(), error.code(), error.getMessage(), request);
|
||||
}
|
||||
|
||||
@ExceptionHandler({HttpMessageNotReadableException.class, MethodArgumentTypeMismatchException.class,
|
||||
MissingServletRequestParameterException.class})
|
||||
ResponseEntity<ErrorBody> invalid(Exception error, HttpServletRequest request) {
|
||||
return response(400, "invalid_request", "Некорректный JSON или параметры запроса", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
|
||||
ResponseEntity<ErrorBody> media(Exception error, HttpServletRequest request) {
|
||||
return response(415, "unsupported_media_type", "Ожидается Content-Type: application/json", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(DataAccessException.class)
|
||||
ResponseEntity<ErrorBody> database(DataAccessException error, HttpServletRequest request) {
|
||||
LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID), error.getClass().getSimpleName());
|
||||
return response(503, "unavailable", "Хранилище данных недоступно", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler({NoResourceFoundException.class, HttpRequestMethodNotSupportedException.class})
|
||||
ResponseEntity<ErrorBody> missing(Exception error, HttpServletRequest request) {
|
||||
return response(404, "not_found", "Объект не найден", request);
|
||||
}
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
ResponseEntity<ErrorBody> unexpected(Exception error, HttpServletRequest request) {
|
||||
for (Throwable cause = error; cause != null; cause = cause.getCause()) {
|
||||
if (cause instanceof DataAccessException) {
|
||||
LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID), cause.getClass().getSimpleName());
|
||||
return response(503, "unavailable", "Хранилище данных недоступно", request);
|
||||
}
|
||||
}
|
||||
LOG.error("request_id={} error_type={}", request.getAttribute(RequestFilter.REQUEST_ID),
|
||||
error.getClass().getSimpleName());
|
||||
return response(500, "internal_error", "Внутренняя ошибка сервера", request);
|
||||
}
|
||||
|
||||
private ResponseEntity<ErrorBody> response(int status, String code, String message, HttpServletRequest request) {
|
||||
String id = (String) request.getAttribute(RequestFilter.REQUEST_ID);
|
||||
ResponseEntity.BodyBuilder builder = ResponseEntity.status(status);
|
||||
if (status == 401) {
|
||||
String challenge = request.getRequestURI().equals("/api/v1/auth/sessions")
|
||||
? "Basic realm=\"messenger\", charset=\"UTF-8\""
|
||||
: "Bearer realm=\"messenger\"";
|
||||
builder.header(HttpHeaders.WWW_AUTHENTICATE, challenge);
|
||||
}
|
||||
return builder.body(new ErrorBody(new ErrorDetail(code, message, id)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
public class ApiException extends RuntimeException {
|
||||
private final int status;
|
||||
private final String code;
|
||||
|
||||
public ApiException(int status, String code, String message) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
this.code = code;
|
||||
}
|
||||
|
||||
public int status() { return status; }
|
||||
public String code() { return code; }
|
||||
public static ApiException invalid(String message) {
|
||||
return new ApiException(400, "invalid_request", message);
|
||||
}
|
||||
public static ApiException unauthorized() {
|
||||
return new ApiException(401, "unauthorized", "Требуется действующая Bearer-сессия");
|
||||
}
|
||||
public static ApiException notFound() {
|
||||
return new ApiException(404, "not_found", "Объект не найден");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
import javax.imageio.ImageReader;
|
||||
import javax.imageio.stream.ImageInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.security.MessageDigest;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@Service
|
||||
public class AttachmentService {
|
||||
private static final long MAX_BYTES = 10_485_760L;
|
||||
private static final long MAX_PIXELS = 40_000_000L;
|
||||
private final JdbcTemplate jdbc;
|
||||
private final MessengerStore store;
|
||||
private final S3Storage s3;
|
||||
|
||||
public AttachmentService(JdbcTemplate jdbc, MessengerStore store, S3Storage s3) {
|
||||
this.jdbc = jdbc; this.store = store; this.s3 = s3;
|
||||
}
|
||||
|
||||
public Attachment upload(UUID actor, UUID chatId, String filename, String contentType, InputStream input) throws IOException {
|
||||
store.chat(actor, chatId);
|
||||
validateFilename(filename);
|
||||
String normalizedType = contentType == null ? "" : contentType.split(";", 2)[0].trim().toLowerCase();
|
||||
if (!normalizedType.equals("image/png") && !normalizedType.equals("image/jpeg")) {
|
||||
throw new ApiException(415, "unsupported_media_type", "Поддерживаются только PNG и JPEG");
|
||||
}
|
||||
Path temp = Files.createTempFile("messenger-upload-", ".bin");
|
||||
UUID id = UUID.randomUUID();
|
||||
String key = "original/" + id;
|
||||
boolean uploaded = false;
|
||||
try {
|
||||
long size = copyWithLimit(input, temp, MAX_BYTES);
|
||||
String actualType = detectType(temp);
|
||||
if (!normalizedType.equals(actualType)) throw new ApiException(415, "unsupported_media_type", "MIME не совпадает с содержимым");
|
||||
Dimensions dimensions = validateImage(temp, actualType);
|
||||
String sha256 = sha256(temp);
|
||||
try (InputStream file = Files.newInputStream(temp)) {
|
||||
s3.put(key, file, size, actualType);
|
||||
uploaded = true;
|
||||
}
|
||||
Instant now = Instant.now();
|
||||
try {
|
||||
jdbc.update("""
|
||||
INSERT INTO attachments(id, chat_id, owner_id, filename, content_type, size_bytes,
|
||||
sha256, object_key, status, error_code, created_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'ready', NULL, ?, ?)
|
||||
""", id, chatId, actor, filename, actualType, size, sha256, key,
|
||||
Timestamp.from(now), Timestamp.from(now));
|
||||
} catch (RuntimeException exception) {
|
||||
if (uploaded) s3.delete(key);
|
||||
throw exception;
|
||||
}
|
||||
return get(actor, id);
|
||||
} finally {
|
||||
Files.deleteIfExists(temp);
|
||||
}
|
||||
}
|
||||
|
||||
public Attachment get(UUID actor, UUID id) {
|
||||
try {
|
||||
return jdbc.queryForObject("""
|
||||
SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, a.size_bytes,
|
||||
a.sha256, a.status, a.error_code, a.created_at, a.updated_at
|
||||
FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id
|
||||
WHERE a.id=? AND m.user_id=?
|
||||
""", (rs, row) -> new Attachment(rs.getObject("id", UUID.class), rs.getObject("chat_id", UUID.class),
|
||||
rs.getObject("owner_id", UUID.class), rs.getString("filename"), rs.getString("content_type"),
|
||||
rs.getLong("size_bytes"), rs.getString("sha256"), rs.getString("status"), List.of(),
|
||||
rs.getString("error_code"), rs.getTimestamp("created_at").toInstant(), rs.getTimestamp("updated_at").toInstant()), id, actor);
|
||||
} catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); }
|
||||
}
|
||||
|
||||
public Download download(UUID actor, UUID id, String variant) {
|
||||
if (!variant.equals("original")) throw ApiException.notFound();
|
||||
try {
|
||||
String key = jdbc.queryForObject("""
|
||||
SELECT a.object_key FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id
|
||||
WHERE a.id=? AND m.user_id=? AND a.status='ready'
|
||||
""", String.class, id, actor);
|
||||
Attachment metadata = get(actor, id);
|
||||
return new Download(s3.get(key), metadata.contentType());
|
||||
} catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); }
|
||||
}
|
||||
|
||||
private static long copyWithLimit(InputStream input, Path target, long max) throws IOException {
|
||||
long total = 0;
|
||||
byte[] buffer = new byte[8192];
|
||||
try (var output = Files.newOutputStream(target)) {
|
||||
int read;
|
||||
while ((read = input.read(buffer)) != -1) {
|
||||
total += read;
|
||||
if (total > max) throw new ApiException(413, "payload_too_large", "Файл слишком большой");
|
||||
output.write(buffer, 0, read);
|
||||
}
|
||||
}
|
||||
if (total == 0) throw new ApiException(400, "invalid_request", "Пустой файл");
|
||||
return total;
|
||||
}
|
||||
|
||||
private static String detectType(Path path) throws IOException {
|
||||
byte[] header = new byte[12];
|
||||
int length;
|
||||
try (InputStream input = Files.newInputStream(path)) {
|
||||
length = input.read(header);
|
||||
}
|
||||
if (length < 0) length = 0;
|
||||
if (length >= 8 && (header[0] & 255) == 0x89 && header[1] == 0x50 && header[2] == 0x4e && header[3] == 0x47
|
||||
&& header[4] == 0x0d && header[5] == 0x0a && header[6] == 0x1a && header[7] == 0x0a) return "image/png";
|
||||
if (length >= 3 && (header[0] & 255) == 0xff && (header[1] & 255) == 0xd8 && (header[2] & 255) == 0xff) return "image/jpeg";
|
||||
throw new ApiException(415, "unsupported_media_type", "Файл не является PNG или JPEG");
|
||||
}
|
||||
|
||||
private static Dimensions validateImage(Path path, String contentType) throws IOException {
|
||||
try (ImageInputStream stream = ImageIO.createImageInputStream(path.toFile())) {
|
||||
Iterator<ImageReader> readers = ImageIO.getImageReaders(stream);
|
||||
if (!readers.hasNext()) throw new ApiException(415, "unsupported_media_type", "Повреждённое изображение");
|
||||
ImageReader reader = readers.next();
|
||||
try {
|
||||
reader.setInput(stream, true, true);
|
||||
int width = reader.getWidth(0), height = reader.getHeight(0);
|
||||
if ((long) width * height > MAX_PIXELS) throw new ApiException(413, "payload_too_large", "Слишком много пикселей");
|
||||
if (width < 1 || height < 1) throw new ApiException(415, "unsupported_media_type", "Некорректные размеры изображения");
|
||||
if (ImageIO.read(path.toFile()) == null) throw new ApiException(415, "unsupported_media_type", "Повреждённое изображение");
|
||||
return new Dimensions(width, height);
|
||||
} finally { reader.dispose(); }
|
||||
}
|
||||
}
|
||||
|
||||
private static String sha256(Path path) throws IOException {
|
||||
try (InputStream input = Files.newInputStream(path)) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
input.transferTo(new java.io.OutputStream() { public void write(int b) { digest.update((byte) b); } public void write(byte[] b, int o, int l) { digest.update(b, o, l); } });
|
||||
StringBuilder result = new StringBuilder();
|
||||
for (byte value : digest.digest()) result.append(String.format("%02x", value));
|
||||
return result.toString();
|
||||
} catch (java.security.NoSuchAlgorithmException exception) { throw new IllegalStateException(exception); }
|
||||
}
|
||||
}
|
||||
|
||||
private static void validateFilename(String value) {
|
||||
if (value == null || value.isEmpty() || value.codePointCount(0, value.length()) > 255
|
||||
|| value.indexOf('/') >= 0 || value.indexOf('\\') >= 0 || value.indexOf('\0') >= 0
|
||||
|| value.codePoints().anyMatch(cp -> cp < 32 || cp == 127)) {
|
||||
throw ApiException.invalid("Некорректное имя файла");
|
||||
}
|
||||
}
|
||||
|
||||
private record Dimensions(int width, int height) {}
|
||||
public record Download(InputStream stream, String contentType) {}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
import javax.crypto.Mac;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.time.Instant;
|
||||
import java.util.Arrays;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class CursorCodec {
|
||||
private final byte[] secret;
|
||||
private static final Base64.Encoder ENCODER = Base64.getUrlEncoder().withoutPadding();
|
||||
private static final Base64.Decoder DECODER = Base64.getUrlDecoder();
|
||||
|
||||
public CursorCodec() {
|
||||
String configured = System.getenv("CURSOR_SECRET");
|
||||
if (configured == null || configured.isBlank()) {
|
||||
throw new IllegalStateException("CURSOR_SECRET must be configured for all API replicas");
|
||||
}
|
||||
try {
|
||||
byte[] decoded = DECODER.decode(configured);
|
||||
if (decoded.length < 32) throw new IllegalArgumentException("secret is too short");
|
||||
this.secret = Arrays.copyOf(decoded, 32);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
throw new IllegalStateException("CURSOR_SECRET must be URL-safe base64 with at least 32 bytes", exception);
|
||||
}
|
||||
}
|
||||
|
||||
public record Position(Instant createdAt, UUID id) {}
|
||||
|
||||
public String encode(String scope, Position position) {
|
||||
String payload = scope + "|" + position.createdAt() + "|" + position.id();
|
||||
String encoded = ENCODER.encodeToString(payload.getBytes(StandardCharsets.UTF_8));
|
||||
return encoded + "." + ENCODER.encodeToString(sign(encoded));
|
||||
}
|
||||
|
||||
public Position decode(String cursor, String scope) {
|
||||
if (cursor == null) return null;
|
||||
try {
|
||||
if (cursor.length() > 1024) throw new IllegalArgumentException();
|
||||
String[] parts = cursor.split("\\.", -1);
|
||||
if (parts.length != 2 || !MessageDigest.isEqual(sign(parts[0]), DECODER.decode(parts[1]))) {
|
||||
throw new IllegalArgumentException();
|
||||
}
|
||||
String[] fields = new String(DECODER.decode(parts[0]), StandardCharsets.UTF_8).split("\\|", -1);
|
||||
if (fields.length != 3 || !scope.equals(fields[0])) throw new IllegalArgumentException();
|
||||
return new Position(Instant.parse(fields[1]), Input.uuid(fields[2]));
|
||||
} catch (RuntimeException exception) {
|
||||
throw ApiException.invalid("Некорректный cursor или cursor от другого ресурса");
|
||||
}
|
||||
}
|
||||
|
||||
private byte[] sign(String payload) {
|
||||
try {
|
||||
Mac mac = Mac.getInstance("HmacSHA256");
|
||||
mac.init(new SecretKeySpec(secret, "HmacSHA256"));
|
||||
return mac.doFinal(payload.getBytes(StandardCharsets.UTF_8));
|
||||
} catch (java.security.GeneralSecurityException exception) {
|
||||
throw new IllegalStateException("HMAC is unavailable", exception);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.boot.availability.ApplicationAvailability;
|
||||
import org.springframework.boot.availability.ReadinessState;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class HealthController {
|
||||
private final ApplicationAvailability availability;
|
||||
private final JdbcTemplate jdbc;
|
||||
private final SessionService sessions;
|
||||
private final S3Storage s3;
|
||||
|
||||
public HealthController(ApplicationAvailability availability, JdbcTemplate jdbc, SessionService sessions, S3Storage s3) {
|
||||
this.availability = availability; this.jdbc = jdbc; this.sessions = sessions; this.s3 = s3;
|
||||
}
|
||||
|
||||
@GetMapping("/health/live")
|
||||
Map<String, String> live() { return Map.of("status", "ok"); }
|
||||
|
||||
@GetMapping("/health/ready")
|
||||
Map<String, String> ready() {
|
||||
if (availability.getReadinessState() != ReadinessState.ACCEPTING_TRAFFIC) throw unavailable();
|
||||
try {
|
||||
jdbc.queryForObject("SELECT 1", Integer.class);
|
||||
sessions.check();
|
||||
s3.check();
|
||||
return Map.of("status", "ok");
|
||||
} catch (RuntimeException exception) {
|
||||
throw unavailable();
|
||||
}
|
||||
}
|
||||
|
||||
private ApiException unavailable() { return new ApiException(503, "unavailable", "Зависимость недоступна"); }
|
||||
}
|
||||
@@ -0,0 +1,125 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
final class Input {
|
||||
private Input() {}
|
||||
|
||||
static void fields(JsonNode body, String... allowed) {
|
||||
Set<String> names = Set.of(allowed);
|
||||
if (body == null || !body.isObject() || body.size() != names.size()) {
|
||||
throw ApiException.invalid("Неверный набор полей JSON");
|
||||
}
|
||||
body.fieldNames().forEachRemaining(name -> {
|
||||
if (!names.contains(name)) throw ApiException.invalid("Неизвестное поле JSON");
|
||||
});
|
||||
}
|
||||
|
||||
static String text(JsonNode body, String field, int max) {
|
||||
JsonNode node = body.get(field);
|
||||
if (node == null || !node.isTextual()) throw ApiException.invalid("Поле должно быть строкой: " + field);
|
||||
String value = node.textValue();
|
||||
if (value.codePointCount(0, value.length()) > max || value.codePoints().allMatch(Input::isUnicodeWhitespace)) {
|
||||
throw ApiException.invalid("Недопустимая длина или пустая строка: " + field);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static String password(JsonNode body) {
|
||||
JsonNode node = body.get("password");
|
||||
if (node == null || !node.isTextual()) throw ApiException.invalid("Пароль должен быть строкой");
|
||||
String value = node.textValue();
|
||||
int length = value.codePointCount(0, value.length());
|
||||
if (length < 12 || length > 128 || value.codePoints().allMatch(Input::isUnicodeWhitespace)) {
|
||||
throw ApiException.invalid("Пароль должен содержать от 12 до 128 непробельных символов");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
private static boolean isUnicodeWhitespace(int codePoint) {
|
||||
return codePoint == 0x0085
|
||||
|| Character.isWhitespace(codePoint)
|
||||
|| Character.isSpaceChar(codePoint);
|
||||
}
|
||||
|
||||
static UUID uuid(String value) {
|
||||
if (value == null || !value.matches("(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}")) {
|
||||
throw ApiException.invalid("Некорректный UUID");
|
||||
}
|
||||
return UUID.fromString(value);
|
||||
}
|
||||
|
||||
static List<UUID> members(JsonNode body) {
|
||||
JsonNode array = body.get("member_ids");
|
||||
if (array == null || !array.isArray() || array.isEmpty() || array.size() > 100) {
|
||||
throw ApiException.invalid("member_ids должен содержать от 1 до 100 участников");
|
||||
}
|
||||
List<UUID> ids = new ArrayList<>();
|
||||
for (JsonNode item : array) {
|
||||
if (!item.isTextual()) throw ApiException.invalid("Участник должен быть UUID-строкой");
|
||||
ids.add(uuid(item.textValue()));
|
||||
}
|
||||
if (new HashSet<>(ids).size() != ids.size()) throw ApiException.invalid("Участники повторяются");
|
||||
return ids;
|
||||
}
|
||||
|
||||
static int limit(String value) {
|
||||
if (value == null) return 50;
|
||||
try {
|
||||
if (!value.matches("[0-9]{1,3}")) throw new NumberFormatException();
|
||||
int limit = Integer.parseInt(value);
|
||||
if (limit < 1 || limit > 100) throw new NumberFormatException();
|
||||
return limit;
|
||||
} catch (NumberFormatException exception) {
|
||||
throw ApiException.invalid("limit должен быть целым числом от 1 до 100");
|
||||
}
|
||||
}
|
||||
|
||||
static String idempotencyKey(String value) {
|
||||
if (value == null || !value.matches("[A-Za-z0-9._:-]{1,128}")) {
|
||||
throw ApiException.invalid("Некорректный Idempotency-Key");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
static void messageFields(JsonNode body) {
|
||||
if (body == null || !body.isObject() || !body.has("text") || body.size() > 2
|
||||
|| (body.has("attachment_ids") && !body.get("attachment_ids").isArray())) {
|
||||
throw ApiException.invalid("Неверный набор полей JSON сообщения");
|
||||
}
|
||||
body.fieldNames().forEachRemaining(name -> {
|
||||
if (!name.equals("text") && !name.equals("attachment_ids")) {
|
||||
throw ApiException.invalid("Неизвестное поле JSON");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
static List<UUID> attachmentIds(JsonNode body) {
|
||||
JsonNode array = body.get("attachment_ids");
|
||||
if (array == null) return List.of();
|
||||
if (!array.isArray() || array.size() > 10) throw ApiException.invalid("Слишком много вложений");
|
||||
List<UUID> ids = new ArrayList<>();
|
||||
for (JsonNode item : array) {
|
||||
if (!item.isTextual()) throw ApiException.invalid("attachment_ids должны быть UUID-строками");
|
||||
ids.add(uuid(item.textValue()));
|
||||
}
|
||||
if (new HashSet<>(ids).size() != ids.size()) throw ApiException.invalid("Вложения повторяются");
|
||||
return ids;
|
||||
}
|
||||
|
||||
static String messageText(JsonNode body, boolean hasAttachments) {
|
||||
JsonNode node = body.get("text");
|
||||
if (node == null || !node.isTextual()) throw ApiException.invalid("Поле должно быть строкой: text");
|
||||
String value = node.textValue();
|
||||
if (value.codePointCount(0, value.length()) > 4000
|
||||
|| (!hasAttachments && (value.isEmpty() || value.codePoints().allMatch(Input::isUnicodeWhitespace)))) {
|
||||
throw ApiException.invalid("Сообщение должно содержать текст или вложение");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class MessengerApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MessengerApplication.class, args);
|
||||
}
|
||||
|
||||
@org.springframework.context.annotation.Bean
|
||||
CommandLineRunner migrationOnly(ApplicationContext context, Environment environment) {
|
||||
return args -> {
|
||||
if (environment.getProperty("MIGRATION_ONLY", Boolean.class, false)) {
|
||||
int exitCode = SpringApplication.exit(context, () -> 0);
|
||||
System.exit(exitCode);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
import java.util.List;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/v1")
|
||||
public class MessengerController {
|
||||
private final MessengerStore store;
|
||||
private final SessionService sessions;
|
||||
private final AttachmentService attachments;
|
||||
|
||||
public MessengerController(MessengerStore store, SessionService sessions, AttachmentService attachments) {
|
||||
this.store = store; this.sessions = sessions; this.attachments = attachments;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/users", consumes = "application/json")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
User register(@RequestBody JsonNode body) {
|
||||
Input.fields(body, "username", "display_name", "password");
|
||||
String username = Input.text(body, "username", 32);
|
||||
if (!username.matches("[a-z0-9_]{3,32}")) throw ApiException.invalid("Некорректный username");
|
||||
return store.createUser(username, Input.text(body, "display_name", 100), sessions.hash(Input.password(body)));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/auth/sessions", consumes = "*/*")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
Session login(HttpServletRequest request) throws java.io.IOException {
|
||||
if (request.getInputStream().read() != -1) {
|
||||
throw ApiException.invalid("Вход не принимает тело запроса");
|
||||
}
|
||||
String header = request.getHeader("Authorization");
|
||||
if (header == null || !header.startsWith("Basic ")) throw ApiException.unauthorized();
|
||||
try {
|
||||
String decoded = new String(Base64.getDecoder().decode(header.substring(6)), StandardCharsets.UTF_8);
|
||||
int colon = decoded.indexOf(':');
|
||||
if (colon <= 0) throw new IllegalArgumentException();
|
||||
String username = decoded.substring(0, colon);
|
||||
String password = decoded.substring(colon + 1);
|
||||
if (!username.matches("[a-z0-9_]{3,32}") || password.codePointCount(0, password.length()) < 12
|
||||
|| password.codePointCount(0, password.length()) > 128) throw new IllegalArgumentException();
|
||||
return sessions.login(username, password);
|
||||
} catch (IllegalArgumentException exception) {
|
||||
throw ApiException.unauthorized();
|
||||
}
|
||||
}
|
||||
|
||||
@DeleteMapping("/auth/sessions/current")
|
||||
@ResponseStatus(HttpStatus.NO_CONTENT)
|
||||
void logout(@RequestAttribute(RequestFilter.TOKEN) String token) { sessions.revoke(token); }
|
||||
|
||||
@GetMapping("/users/me")
|
||||
User me(@RequestAttribute(RequestFilter.ACTOR) User actor) { return actor; }
|
||||
|
||||
@PostMapping(value = "/chats", consumes = "application/json")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
Chat createChat(@RequestAttribute(RequestFilter.ACTOR) User actor, @RequestBody JsonNode body) {
|
||||
Input.fields(body, "title", "member_ids");
|
||||
return store.createChat(actor.id(), Input.text(body, "title", 100), Input.members(body));
|
||||
}
|
||||
|
||||
@GetMapping("/chats")
|
||||
Page<Chat> chats(@RequestAttribute(RequestFilter.ACTOR) User actor,
|
||||
@RequestParam(required = false) String limit, @RequestParam(required = false) String cursor) {
|
||||
return store.chats(actor.id(), Input.limit(limit), cursor);
|
||||
}
|
||||
|
||||
@GetMapping("/chats/{chatId}")
|
||||
Chat chat(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId) {
|
||||
return store.chat(actor.id(), Input.uuid(chatId));
|
||||
}
|
||||
|
||||
@PostMapping(value = "/chats/{chatId}/messages", consumes = "application/json")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
Message send(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId,
|
||||
@RequestHeader(value = "Idempotency-Key", required = false) String idempotencyKey,
|
||||
HttpServletRequest request, @RequestBody JsonNode body) {
|
||||
UUID id = Input.uuid(chatId);
|
||||
store.chat(actor.id(), id);
|
||||
String key = Input.idempotencyKey(idempotencyKey);
|
||||
Input.messageFields(body);
|
||||
List<UUID> attachmentIds = Input.attachmentIds(body);
|
||||
String text = Input.messageText(body, !attachmentIds.isEmpty());
|
||||
return store.sendIdempotent(actor.id(), id, text, attachmentIds, request.getMethod(), request.getRequestURI(), key);
|
||||
}
|
||||
|
||||
@GetMapping("/chats/{chatId}/messages")
|
||||
Page<Message> history(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId,
|
||||
@RequestParam(required = false) String limit, @RequestParam(required = false) String cursor) {
|
||||
UUID id = Input.uuid(chatId);
|
||||
return store.history(actor.id(), id, Input.limit(limit), cursor);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/chats/{chatId}/attachments", consumes = {"image/png", "image/jpeg"})
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
Attachment upload(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String chatId,
|
||||
@RequestParam String filename, HttpServletRequest request) throws java.io.IOException {
|
||||
return attachments.upload(actor.id(), Input.uuid(chatId), filename, request.getContentType(), request.getInputStream());
|
||||
}
|
||||
|
||||
@GetMapping("/attachments/{attachmentId}")
|
||||
Attachment attachment(@RequestAttribute(RequestFilter.ACTOR) User actor, @PathVariable String attachmentId) {
|
||||
return attachments.get(actor.id(), Input.uuid(attachmentId));
|
||||
}
|
||||
|
||||
@GetMapping("/attachments/{attachmentId}/content")
|
||||
ResponseEntity<StreamingResponseBody> content(@RequestAttribute(RequestFilter.ACTOR) User actor,
|
||||
@PathVariable String attachmentId,
|
||||
@RequestParam(defaultValue = "original") String variant) {
|
||||
AttachmentService.Download download = attachments.download(actor.id(), Input.uuid(attachmentId), variant);
|
||||
StreamingResponseBody body = output -> {
|
||||
try (java.io.InputStream input = download.stream()) { input.transferTo(output); }
|
||||
};
|
||||
return ResponseEntity.ok().header(HttpHeaders.CONTENT_TYPE, download.contentType()).body(body);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import org.springframework.dao.EmptyResultDataAccessException;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.sql.Timestamp;
|
||||
import java.time.Instant;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@Service
|
||||
public class MessengerStore {
|
||||
private final JdbcTemplate jdbc;
|
||||
private final CursorCodec cursors;
|
||||
|
||||
public MessengerStore(JdbcTemplate jdbc, CursorCodec cursors) { this.jdbc = jdbc; this.cursors = cursors; }
|
||||
|
||||
@Transactional
|
||||
public User createUser(String username, String displayName, String passwordHash) {
|
||||
UUID id = UUID.randomUUID();
|
||||
Instant created = Instant.now();
|
||||
try {
|
||||
jdbc.update("INSERT INTO users(id, username, display_name, password_hash, created_at) VALUES (?, ?, ?, ?, ?)",
|
||||
id, username, displayName, passwordHash, Timestamp.from(created));
|
||||
} catch (DuplicateKeyException exception) {
|
||||
throw new ApiException(409, "conflict", "username уже занят");
|
||||
}
|
||||
return findUser(id);
|
||||
}
|
||||
|
||||
public Credentials credentials(String username) {
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT id, username, display_name, created_at, password_hash FROM users WHERE username = ?",
|
||||
(rs, row) -> new Credentials(new User(rs.getObject("id", UUID.class), rs.getString("username"),
|
||||
rs.getString("display_name"), rs.getTimestamp("created_at").toInstant()),
|
||||
rs.getString("password_hash")), username);
|
||||
} catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
public User findUser(UUID id) {
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT id, username, display_name, created_at FROM users WHERE id = ?",
|
||||
(rs, row) -> user(rs), id);
|
||||
} catch (EmptyResultDataAccessException exception) { return null; }
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Chat createChat(UUID actor, String title, List<UUID> memberIds) {
|
||||
if (!memberIds.contains(actor)) throw ApiException.invalid("Участники должны существовать и включать создателя");
|
||||
for (UUID member : memberIds) {
|
||||
if (findUser(member) == null) throw ApiException.invalid("Участники должны существовать и включать создателя");
|
||||
}
|
||||
UUID id = UUID.randomUUID();
|
||||
Instant created = Instant.now();
|
||||
jdbc.update("INSERT INTO chats(id, title, created_at) VALUES (?, ?, ?)", id, title, Timestamp.from(created));
|
||||
for (UUID member : memberIds) jdbc.update("INSERT INTO chat_members(chat_id, user_id) VALUES (?, ?)", id, member);
|
||||
return chat(actor, id);
|
||||
}
|
||||
|
||||
public Chat chat(UUID actor, UUID id) {
|
||||
try {
|
||||
Chat chat = jdbc.queryForObject("SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE c.id=? AND m.user_id=?",
|
||||
(rs, row) -> new Chat(rs.getObject("id", UUID.class), rs.getString("title"),
|
||||
loadMembers(rs.getObject("id", UUID.class)), rs.getTimestamp("created_at").toInstant()), id, actor);
|
||||
if (chat == null) throw ApiException.notFound();
|
||||
return chat;
|
||||
} catch (EmptyResultDataAccessException exception) { throw ApiException.notFound(); }
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Message send(UUID actor, UUID chatId, String text) {
|
||||
chat(actor, chatId);
|
||||
UUID id = UUID.randomUUID();
|
||||
Instant created = Instant.now();
|
||||
jdbc.update("INSERT INTO messages(id, chat_id, sender_id, text, created_at) VALUES (?, ?, ?, ?, ?)",
|
||||
id, chatId, actor, text, Timestamp.from(created));
|
||||
return findMessage(id);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Message sendIdempotent(UUID actor, UUID chatId, String text,
|
||||
List<UUID> attachmentIds,
|
||||
String method, String requestPath, String idempotencyKey) {
|
||||
chat(actor, chatId);
|
||||
verifyAttachments(actor, chatId, attachmentIds);
|
||||
String requestHash = hashRequest(text, attachmentIds);
|
||||
Instant created = Instant.now();
|
||||
int inserted = jdbc.update("""
|
||||
INSERT INTO message_idempotency(actor_id, method, request_path, idempotency_key,
|
||||
request_hash, message_id, created_at)
|
||||
VALUES (?, ?, ?, ?, ?, NULL, ?)
|
||||
ON CONFLICT (actor_id, method, request_path, idempotency_key) DO NOTHING
|
||||
""", actor, method, requestPath, idempotencyKey, requestHash, Timestamp.from(created));
|
||||
|
||||
if (inserted == 0) {
|
||||
IdempotencyRecord previous = jdbc.queryForObject("""
|
||||
SELECT request_hash, message_id
|
||||
FROM message_idempotency
|
||||
WHERE actor_id=? AND method=? AND request_path=? AND idempotency_key=?
|
||||
""", (rs, row) -> new IdempotencyRecord(rs.getString("request_hash"),
|
||||
rs.getObject("message_id", UUID.class)), actor, method, requestPath, idempotencyKey);
|
||||
if (!MessageDigest.isEqual(previous.requestHash().getBytes(StandardCharsets.US_ASCII),
|
||||
requestHash.getBytes(StandardCharsets.US_ASCII))) {
|
||||
throw new ApiException(409, "conflict", "Idempotency-Key уже использован для другого сообщения");
|
||||
}
|
||||
if (previous.messageId() == null) {
|
||||
throw new ApiException(503, "unavailable", "Результат идемпотентного запроса ещё не готов");
|
||||
}
|
||||
return findMessage(previous.messageId());
|
||||
}
|
||||
|
||||
UUID id = UUID.randomUUID();
|
||||
jdbc.update("INSERT INTO messages(id, chat_id, sender_id, text, created_at) VALUES (?, ?, ?, ?, ?)",
|
||||
id, chatId, actor, text, Timestamp.from(created));
|
||||
for (UUID attachmentId : attachmentIds) {
|
||||
jdbc.update("INSERT INTO message_attachments(message_id, attachment_id) VALUES (?, ?)", id, attachmentId);
|
||||
}
|
||||
jdbc.update("""
|
||||
UPDATE message_idempotency
|
||||
SET message_id=?
|
||||
WHERE actor_id=? AND method=? AND request_path=? AND idempotency_key=?
|
||||
""", id, actor, method, requestPath, idempotencyKey);
|
||||
return findMessage(id);
|
||||
}
|
||||
|
||||
public Page<Chat> chats(UUID actor, int limit, String cursor) {
|
||||
CursorCodec.Position after = cursors.decode(cursor, "chats:" + actor);
|
||||
String sql = "SELECT c.id, c.title, c.created_at FROM chats c JOIN chat_members m ON m.chat_id=c.id WHERE m.user_id=?";
|
||||
List<Object> args = new ArrayList<>(List.of(actor));
|
||||
if (after != null) { sql += " AND (c.created_at, c.id) > (?, ?)"; args.add(Timestamp.from(after.createdAt())); args.add(after.id()); }
|
||||
sql += " ORDER BY c.created_at, c.id LIMIT ?";
|
||||
args.add(limit + 1);
|
||||
List<Chat> rows = jdbc.query(sql, (rs, row) -> {
|
||||
UUID id = rs.getObject("id", UUID.class);
|
||||
return new Chat(id, rs.getString("title"), loadMembers(id), rs.getTimestamp("created_at").toInstant());
|
||||
}, args.toArray());
|
||||
return page(rows, limit, "chats:" + actor, c -> new CursorCodec.Position(c.createdAt(), c.id()));
|
||||
}
|
||||
|
||||
public Page<Message> history(UUID actor, UUID chatId, int limit, String cursor) {
|
||||
chat(actor, chatId);
|
||||
CursorCodec.Position after = cursors.decode(cursor, "messages:" + chatId);
|
||||
String sql = "SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE chat_id=?";
|
||||
List<Object> args = new ArrayList<>(List.of(chatId));
|
||||
if (after != null) { sql += " AND (created_at, id) > (?, ?)"; args.add(Timestamp.from(after.createdAt())); args.add(after.id()); }
|
||||
sql += " ORDER BY created_at, id LIMIT ?";
|
||||
args.add(limit + 1);
|
||||
List<Message> rows = jdbc.query(sql, (rs, row) -> new Message(rs.getObject("id", UUID.class),
|
||||
rs.getObject("chat_id", UUID.class), rs.getObject("sender_id", UUID.class), rs.getString("text"),
|
||||
rs.getTimestamp("created_at").toInstant(), loadAttachmentIds(rs.getObject("id", UUID.class))), args.toArray());
|
||||
return page(rows, limit, "messages:" + chatId, m -> new CursorCodec.Position(m.createdAt(), m.id()));
|
||||
}
|
||||
|
||||
private List<UUID> loadMembers(UUID chatId) {
|
||||
return jdbc.query("SELECT user_id FROM chat_members WHERE chat_id=? ORDER BY user_id",
|
||||
(rs, row) -> rs.getObject("user_id", UUID.class), chatId);
|
||||
}
|
||||
|
||||
private Message findMessage(UUID id) {
|
||||
try {
|
||||
return jdbc.queryForObject("SELECT id, chat_id, sender_id, text, created_at FROM messages WHERE id=?",
|
||||
(rs, row) -> new Message(rs.getObject("id", UUID.class), rs.getObject("chat_id", UUID.class),
|
||||
rs.getObject("sender_id", UUID.class), rs.getString("text"), rs.getTimestamp("created_at").toInstant(),
|
||||
loadAttachmentIds(rs.getObject("id", UUID.class))), id);
|
||||
} catch (EmptyResultDataAccessException exception) {
|
||||
throw new ApiException(500, "internal_error", "Созданное сообщение не найдено");
|
||||
}
|
||||
}
|
||||
|
||||
private String hashRequest(String text, List<UUID> attachmentIds) {
|
||||
try {
|
||||
byte[] digest = MessageDigest.getInstance("SHA-256")
|
||||
.digest((text + "|" + attachmentIds.stream().map(UUID::toString).sorted().toList()).getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder result = new StringBuilder(digest.length * 2);
|
||||
for (byte value : digest) result.append(String.format("%02x", value));
|
||||
return result.toString();
|
||||
} catch (java.security.NoSuchAlgorithmException exception) {
|
||||
throw new IllegalStateException("SHA-256 is unavailable", exception);
|
||||
}
|
||||
}
|
||||
|
||||
private void verifyAttachments(UUID actor, UUID chatId, List<UUID> ids) {
|
||||
for (UUID id : ids) {
|
||||
Integer count = jdbc.queryForObject("SELECT count(*) FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=? AND a.chat_id=? AND m.user_id=? AND a.status='ready'", Integer.class, id, chatId, actor);
|
||||
if (count == null || count != 1) throw ApiException.notFound();
|
||||
}
|
||||
}
|
||||
|
||||
private List<UUID> loadAttachmentIds(UUID messageId) {
|
||||
return jdbc.query("SELECT attachment_id FROM message_attachments WHERE message_id=? ORDER BY attachment_id",
|
||||
(rs, row) -> rs.getObject("attachment_id", UUID.class), messageId);
|
||||
}
|
||||
|
||||
private record IdempotencyRecord(String requestHash, UUID messageId) {}
|
||||
|
||||
private User user(java.sql.ResultSet rs) throws java.sql.SQLException {
|
||||
return new User(rs.getObject("id", UUID.class), rs.getString("username"), rs.getString("display_name"),
|
||||
rs.getTimestamp("created_at").toInstant());
|
||||
}
|
||||
|
||||
private <T> Page<T> page(List<T> rows, int limit, String scope, java.util.function.Function<T, CursorCodec.Position> position) {
|
||||
boolean more = rows.size() > limit;
|
||||
List<T> result = more ? rows.subList(0, limit) : rows;
|
||||
String next = more ? cursors.encode(scope, position.apply(result.getLast())) : null;
|
||||
return new Page<>(result, next);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class Models {
|
||||
private Models() {}
|
||||
|
||||
public record User(UUID id, String username, String displayName, Instant createdAt) {}
|
||||
public record Chat(UUID id, String title, List<UUID> memberIds, Instant createdAt) {
|
||||
public Chat { memberIds = List.copyOf(memberIds); }
|
||||
}
|
||||
public record Message(UUID id, UUID chatId, UUID senderId, String text, Instant createdAt,
|
||||
List<UUID> attachmentIds) {
|
||||
public Message { attachmentIds = List.copyOf(attachmentIds); }
|
||||
}
|
||||
public record Attachment(UUID id, UUID chatId, UUID ownerId, String filename, String contentType,
|
||||
long sizeBytes, String sha256, String status, List<Variant> variants,
|
||||
String errorCode, Instant createdAt, Instant updatedAt) {
|
||||
public Attachment { variants = List.copyOf(variants); }
|
||||
}
|
||||
public record Variant(String name, String contentType, long sizeBytes, int width, int height, String sha256) {}
|
||||
public record Credentials(User user, String passwordHash) {}
|
||||
public record Session(String token, String tokenType, Instant expiresAt, User user) {}
|
||||
public record Page<T>(List<T> items, String nextCursor) {
|
||||
public Page { items = List.copyOf(items); }
|
||||
}
|
||||
public record ErrorBody(ErrorDetail error) {}
|
||||
public record ErrorDetail(String code, String message, String requestId) {}
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.annotation.Order;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
import org.springframework.web.servlet.HandlerMapping;
|
||||
import java.io.IOException;
|
||||
import java.sql.SQLException;
|
||||
import java.util.UUID;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@Component
|
||||
@Order(Ordered.HIGHEST_PRECEDENCE)
|
||||
public class RequestFilter extends OncePerRequestFilter {
|
||||
public static final String ACTOR = "messenger.actor";
|
||||
public static final String REQUEST_ID = "messenger.requestId";
|
||||
public static final String TOKEN = "messenger.token";
|
||||
public static final String INSTANCE_ID = "messenger.instanceId";
|
||||
private static final String INSTANCE = instanceId();
|
||||
private static final Logger LOG = LoggerFactory.getLogger(RequestFilter.class);
|
||||
private final SessionService sessions;
|
||||
private final ObjectMapper mapper;
|
||||
|
||||
public RequestFilter(SessionService sessions, ObjectMapper mapper) {
|
||||
this.sessions = sessions;
|
||||
this.mapper = mapper;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
|
||||
FilterChain chain) throws ServletException, IOException {
|
||||
String requestId = UUID.randomUUID().toString();
|
||||
request.setAttribute(REQUEST_ID, requestId);
|
||||
response.setHeader("X-Request-Id", requestId);
|
||||
request.setAttribute(INSTANCE_ID, INSTANCE);
|
||||
response.setHeader("X-Instance-Id", INSTANCE);
|
||||
long start = System.nanoTime();
|
||||
try {
|
||||
String path = request.getRequestURI();
|
||||
boolean publicEndpoint = (path.equals("/api/v1/users") && request.getMethod().equals("POST"))
|
||||
|| (path.equals("/api/v1/auth/sessions") && request.getMethod().equals("POST"));
|
||||
if (path.startsWith("/api/v1/") && !publicEndpoint) {
|
||||
String header = request.getHeader("Authorization");
|
||||
if (header == null || !header.startsWith("Bearer ")) throw ApiException.unauthorized();
|
||||
String token = header.substring("Bearer ".length()).trim();
|
||||
request.setAttribute(TOKEN, token);
|
||||
request.setAttribute(ACTOR, sessions.authenticate(token));
|
||||
}
|
||||
chain.doFilter(request, response);
|
||||
} catch (ApiException exception) {
|
||||
writeError(response, exception.status(), exception.code(), exception.getMessage(), requestId, request);
|
||||
} catch (Exception exception) {
|
||||
if (unavailable(exception)) {
|
||||
writeError(response, 503, "unavailable", "Хранилище данных недоступно", requestId, request);
|
||||
} else if (exception instanceof ServletException servlet) {
|
||||
throw servlet;
|
||||
} else if (exception instanceof IOException io) {
|
||||
throw io;
|
||||
} else {
|
||||
throw new ServletException(exception);
|
||||
}
|
||||
} finally {
|
||||
Object route = request.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
|
||||
LOG.info("request_id={} method={} route={} status={} duration_ms={}", requestId,
|
||||
request.getMethod(), route == null ? "unmapped" : route, response.getStatus(),
|
||||
(System.nanoTime() - start) / 1_000_000);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeError(HttpServletResponse response, int status, String code, String message,
|
||||
String requestId, HttpServletRequest request) throws IOException {
|
||||
response.setStatus(status);
|
||||
if (status == 401) {
|
||||
String challenge = request.getRequestURI().equals("/api/v1/auth/sessions")
|
||||
? "Basic realm=\"messenger\", charset=\"UTF-8\""
|
||||
: "Bearer realm=\"messenger\"";
|
||||
response.setHeader("WWW-Authenticate", challenge);
|
||||
}
|
||||
response.setContentType("application/json");
|
||||
response.setCharacterEncoding("UTF-8");
|
||||
mapper.writeValue(response.getOutputStream(), new ErrorBody(new ErrorDetail(code, message, requestId)));
|
||||
}
|
||||
|
||||
private boolean unavailable(Throwable error) {
|
||||
for (Throwable cause = error; cause != null; cause = cause.getCause()) {
|
||||
if (cause instanceof DataAccessException || cause instanceof SQLException) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private static String instanceId() {
|
||||
String configured = System.getenv("API_INSTANCE_ID");
|
||||
if (configured != null && !configured.isBlank()) return configured;
|
||||
String hostname = System.getenv("HOSTNAME");
|
||||
return hostname == null || hostname.isBlank() ? UUID.randomUUID().toString() : hostname;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import io.minio.GetObjectArgs;
|
||||
import io.minio.MakeBucketArgs;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.PutObjectArgs;
|
||||
import io.minio.RemoveObjectArgs;
|
||||
import io.minio.BucketExistsArgs;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
@Component
|
||||
public class S3Storage {
|
||||
private final MinioClient client;
|
||||
private final String bucket;
|
||||
|
||||
public S3Storage() {
|
||||
String endpoint = env("S3_ENDPOINT", "http://minio:9000");
|
||||
String access = env("S3_ACCESS_KEY_ID", "messenger-api");
|
||||
String secret = env("S3_SECRET_ACCESS_KEY", "messenger-api-secret");
|
||||
this.bucket = env("S3_BUCKET", "messenger-attachments");
|
||||
this.client = MinioClient.builder().endpoint(endpoint).credentials(access, secret).build();
|
||||
try {
|
||||
if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {
|
||||
client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
throw new IllegalStateException("S3 bucket is unavailable", exception);
|
||||
}
|
||||
}
|
||||
|
||||
public void put(String key, InputStream input, long size, String contentType) {
|
||||
try {
|
||||
client.putObject(PutObjectArgs.builder().bucket(bucket).object(key).stream(input, size, -1)
|
||||
.contentType(contentType).build());
|
||||
} catch (Exception exception) {
|
||||
throw new ApiException(503, "unavailable", "S3-хранилище недоступно");
|
||||
}
|
||||
}
|
||||
|
||||
public void check() {
|
||||
try {
|
||||
if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build())) {
|
||||
throw new IllegalStateException("bucket is missing");
|
||||
}
|
||||
} catch (Exception exception) {
|
||||
throw new ApiException(503, "unavailable", "S3-хранилище недоступно");
|
||||
}
|
||||
}
|
||||
|
||||
public InputStream get(String key) {
|
||||
try {
|
||||
return client.getObject(GetObjectArgs.builder().bucket(bucket).object(key).build());
|
||||
} catch (Exception exception) {
|
||||
throw new ApiException(503, "unavailable", "S3-хранилище недоступно");
|
||||
}
|
||||
}
|
||||
|
||||
public void delete(String key) {
|
||||
try {
|
||||
client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(key).build());
|
||||
} catch (Exception ignored) {
|
||||
// Best-effort compensation; orphan cleanup is documented in REPORT.md.
|
||||
}
|
||||
}
|
||||
|
||||
private static String env(String name, String fallback) {
|
||||
String value = System.getenv(name);
|
||||
return value == null || value.isBlank() ? fallback : value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,102 @@
|
||||
package ru.mtusi.messenger;
|
||||
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.security.crypto.scrypt.SCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.util.Base64;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.Future;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
|
||||
import static ru.mtusi.messenger.Models.*;
|
||||
|
||||
@Service
|
||||
public class SessionService {
|
||||
private static final Base64.Encoder TOKEN_ENCODER = Base64.getUrlEncoder().withoutPadding();
|
||||
private final MessengerStore store;
|
||||
private final StringRedisTemplate redis;
|
||||
private final SCryptPasswordEncoder passwords = SCryptPasswordEncoder.defaultsForSpringSecurity_v5_8();
|
||||
private final SecureRandom random = new SecureRandom();
|
||||
private final long ttlSeconds;
|
||||
private final long databaseTimeoutMillis;
|
||||
private final ExecutorService databaseExecutor = Executors.newFixedThreadPool(4);
|
||||
|
||||
public SessionService(MessengerStore store, StringRedisTemplate redis,
|
||||
@Value("${SESSION_TTL_SECONDS:3600}") long ttlSeconds,
|
||||
@Value("${DB_REQUEST_TIMEOUT_MS:2000}") long databaseTimeoutMillis) {
|
||||
this.store = store;
|
||||
this.redis = redis;
|
||||
this.ttlSeconds = Math.max(2, ttlSeconds);
|
||||
this.databaseTimeoutMillis = Math.max(250, databaseTimeoutMillis);
|
||||
}
|
||||
|
||||
public String hash(String password) { return passwords.encode(password); }
|
||||
|
||||
public Session login(String username, String password) {
|
||||
Credentials credentials = database(() -> store.credentials(username));
|
||||
if (credentials == null || !passwords.matches(password, credentials.passwordHash())) throw ApiException.unauthorized();
|
||||
byte[] bytes = new byte[32];
|
||||
random.nextBytes(bytes);
|
||||
String token = TOKEN_ENCODER.encodeToString(bytes);
|
||||
Instant expires = Instant.now().plusSeconds(ttlSeconds);
|
||||
try {
|
||||
redis.opsForValue().set(key(token), credentials.user().id().toString(), Duration.ofSeconds(ttlSeconds));
|
||||
} catch (RuntimeException exception) {
|
||||
throw new ApiException(503, "unavailable", "Сервис сессий недоступен");
|
||||
}
|
||||
return new Session(token, "Bearer", expires, credentials.user());
|
||||
}
|
||||
|
||||
public User authenticate(String token) {
|
||||
if (token == null || token.isBlank()) throw ApiException.unauthorized();
|
||||
String value;
|
||||
try { value = redis.opsForValue().get(key(token)); }
|
||||
catch (RuntimeException exception) { throw new ApiException(503, "unavailable", "Сервис сессий недоступен"); }
|
||||
if (value == null) throw ApiException.unauthorized();
|
||||
User user = database(() -> store.findUser(UUID.fromString(value)));
|
||||
if (user == null) throw ApiException.unauthorized();
|
||||
return user;
|
||||
}
|
||||
|
||||
public void revoke(String token) {
|
||||
if (token == null) throw ApiException.unauthorized();
|
||||
try { redis.delete(key(token)); }
|
||||
catch (RuntimeException exception) { throw new ApiException(503, "unavailable", "Сервис сессий недоступен"); }
|
||||
}
|
||||
|
||||
public void check() { redis.opsForValue().get("messenger:readiness"); }
|
||||
|
||||
private String key(String token) { return "messenger:session:" + token; }
|
||||
|
||||
private <T> T database(java.util.concurrent.Callable<T> operation) {
|
||||
Future<T> future = databaseExecutor.submit(operation);
|
||||
try {
|
||||
return future.get(databaseTimeoutMillis, TimeUnit.MILLISECONDS);
|
||||
} catch (TimeoutException exception) {
|
||||
future.cancel(true);
|
||||
throw new ApiException(503, "unavailable", "Хранилище данных недоступно");
|
||||
} catch (InterruptedException exception) {
|
||||
Thread.currentThread().interrupt();
|
||||
throw new ApiException(503, "unavailable", "Хранилище данных недоступно");
|
||||
} catch (ExecutionException exception) {
|
||||
Throwable cause = exception.getCause();
|
||||
if (cause instanceof RuntimeException runtime) throw runtime;
|
||||
throw new IllegalStateException(cause);
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
void shutdownDatabaseExecutor() {
|
||||
databaseExecutor.shutdownNow();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
spring.application.name=messenger-lab4
|
||||
server.address=${APP_HOST:0.0.0.0}
|
||||
server.port=${APP_PORT:8080}
|
||||
server.shutdown=graceful
|
||||
spring.lifecycle.timeout-per-shutdown-phase=5s
|
||||
|
||||
spring.datasource.url=${DATABASE_URL:jdbc:postgresql://${DB_HOST:postgres}:${DB_PORT:5432}/${DB_NAME:messenger}?socketTimeout=1&options=-c%20statement_timeout%3D1000}
|
||||
spring.datasource.username=${DB_USERNAME:messenger}
|
||||
spring.datasource.password=${DB_PASSWORD:messenger}
|
||||
spring.datasource.hikari.maximum-pool-size=${DB_POOL_SIZE:10}
|
||||
spring.datasource.hikari.connection-timeout=${DB_CONNECTION_TIMEOUT_MS:3000}
|
||||
spring.datasource.hikari.validation-timeout=250
|
||||
spring.datasource.hikari.transaction-isolation=TRANSACTION_READ_COMMITTED
|
||||
# Connection timeout only limits waiting for a free pool slot. These settings
|
||||
# also bound an already running PostgreSQL statement and socket read.
|
||||
spring.datasource.hikari.data-source-properties.[socketTimeout]=${DB_SOCKET_TIMEOUT_SECONDS:1}
|
||||
spring.datasource.hikari.data-source-properties.[options]=-c statement_timeout=${DB_STATEMENT_TIMEOUT_MS:1000}
|
||||
spring.jdbc.template.query-timeout=${DB_QUERY_TIMEOUT_SECONDS:1}s
|
||||
|
||||
spring.data.redis.host=${REDIS_HOST:redis}
|
||||
spring.data.redis.port=${REDIS_PORT:6379}
|
||||
spring.data.redis.timeout=${REDIS_TIMEOUT:2s}
|
||||
spring.data.redis.connect-timeout=${REDIS_CONNECT_TIMEOUT:2s}
|
||||
|
||||
spring.flyway.enabled=true
|
||||
spring.jackson.property-naming-strategy=SNAKE_CASE
|
||||
spring.jackson.deserialization.fail-on-trailing-tokens=true
|
||||
spring.jackson.parser.strict-duplicate-detection=true
|
||||
server.error.include-message=never
|
||||
server.error.include-stacktrace=never
|
||||
spring.main.banner-mode=off
|
||||
logging.level.root=INFO
|
||||
@@ -0,0 +1,30 @@
|
||||
CREATE TABLE users (
|
||||
id UUID PRIMARY KEY,
|
||||
username VARCHAR(32) NOT NULL UNIQUE,
|
||||
display_name VARCHAR(100) NOT NULL,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE chats (
|
||||
id UUID PRIMARY KEY,
|
||||
title VARCHAR(100) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
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 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),
|
||||
text VARCHAR(4000) NOT NULL,
|
||||
created_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_messages_chat_created_id ON messages(chat_id, created_at, id);
|
||||
CREATE INDEX idx_chat_members_user_chat ON chat_members(user_id, chat_id);
|
||||
@@ -0,0 +1,13 @@
|
||||
CREATE TABLE message_idempotency (
|
||||
actor_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
method VARCHAR(10) NOT NULL,
|
||||
request_path VARCHAR(200) NOT NULL,
|
||||
idempotency_key VARCHAR(128) NOT NULL,
|
||||
request_hash CHAR(64) NOT NULL,
|
||||
message_id UUID REFERENCES messages(id) ON DELETE CASCADE,
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
PRIMARY KEY (actor_id, method, request_path, idempotency_key)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_message_idempotency_created
|
||||
ON message_idempotency(created_at);
|
||||
@@ -0,0 +1,24 @@
|
||||
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),
|
||||
filename VARCHAR(255) NOT NULL,
|
||||
content_type VARCHAR(32) NOT NULL CHECK (content_type IN ('image/png', 'image/jpeg')),
|
||||
size_bytes BIGINT NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760),
|
||||
sha256 CHAR(64) NOT NULL,
|
||||
object_key TEXT NOT NULL UNIQUE,
|
||||
status VARCHAR(16) NOT NULL CHECK (status IN ('ready')),
|
||||
error_code VARCHAR(64),
|
||||
created_at TIMESTAMPTZ NOT NULL,
|
||||
updated_at TIMESTAMPTZ NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX idx_attachments_chat ON attachments(chat_id, created_at, id);
|
||||
|
||||
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,
|
||||
PRIMARY KEY (message_id, attachment_id)
|
||||
);
|
||||
|
||||
CREATE INDEX idx_message_attachments_attachment ON message_attachments(attachment_id);
|
||||
Executable
+80
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Own lab3 checks: two replicas, shared idempotency and rolling replacement."""
|
||||
import argparse
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
import uuid
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, Failure, check
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--rolling-update", action="store_true")
|
||||
args = parser.parse_args()
|
||||
client = Client(args.base_url)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
|
||||
instances = set()
|
||||
for _ in range(40):
|
||||
response = client.call("GET", "/api/v1/users/me", actor=alice, schema="User")
|
||||
instances.add(response["headers"].get("X-Instance-Id", ""))
|
||||
if len(instances) >= 2:
|
||||
break
|
||||
check(len(instances) >= 2, "load balancer did not expose two API instances")
|
||||
|
||||
key = "parallel-" + uuid.uuid4().hex
|
||||
text = "Одна запись при конкурентном повторе"
|
||||
|
||||
def send_once():
|
||||
return client.message(alice, chat, text, key=key)["json"]
|
||||
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
messages = list(pool.map(lambda _: send_once(), range(10)))
|
||||
check(len({message["id"] for message in messages}) == 1,
|
||||
"concurrent idempotency created duplicate messages")
|
||||
check(all(message == messages[0] for message in messages),
|
||||
"idempotency replay returned different messages")
|
||||
|
||||
client.call("POST", "/api/v1/chats/" + chat["id"] + "/messages", 409,
|
||||
actor=alice, body={"text": "Другой текст"},
|
||||
headers={"Idempotency-Key": key})
|
||||
client.call("POST", "/api/v1/chats/" + chat["id"] + "/messages", 400,
|
||||
actor=alice, body={"text": "Без ключа"})
|
||||
print("PASS: two API instances, shared idempotency and conflict semantics")
|
||||
|
||||
if args.rolling_update:
|
||||
output = ROOT / "evidence" / "rolling-load.json"
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
load = subprocess.Popen([
|
||||
sys.executable, str(ROOT / "tests" / "load.py"),
|
||||
"--base-url", args.base_url, "--duration", "20", "--concurrency", "4",
|
||||
"--output", str(output),
|
||||
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
try:
|
||||
time.sleep(2)
|
||||
result = subprocess.run([str(ROOT / "scripts" / "rolling-update.sh")], check=False)
|
||||
check(result.returncode == 0, "rolling-update.sh failed")
|
||||
finally:
|
||||
try:
|
||||
load.wait(timeout=40)
|
||||
except subprocess.TimeoutExpired:
|
||||
load.kill()
|
||||
load.wait()
|
||||
raise Failure("rolling load exceeded its deadline")
|
||||
check(load.returncode == 0, "load failed during rolling replacement")
|
||||
print("PASS: rolling replacement completed while load was running")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (Failure, AssertionError, OSError) as error:
|
||||
print("FAIL:", error)
|
||||
raise SystemExit(1)
|
||||
Executable
+59
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Lab4 grade-4 checks for upload validation and attachment authorization."""
|
||||
import hashlib
|
||||
import os
|
||||
import sys
|
||||
import uuid
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, Failure, check
|
||||
|
||||
|
||||
def main():
|
||||
import argparse
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--base-url", default=os.environ.get("BASE_URL", "http://localhost:8080"))
|
||||
args = parser.parse_args()
|
||||
client = Client(args.base_url)
|
||||
alice, bob, eve, chat = client.bootstrap()
|
||||
fixture = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
||||
|
||||
# Correct content succeeds and is byte-for-byte downloadable by another member.
|
||||
uploaded = client.call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=sample.png", 201,
|
||||
actor=alice, raw=fixture, headers={"Content-Type": "image/png"}, schema="Attachment")
|
||||
attachment = uploaded["json"]
|
||||
downloaded = client.call("GET", f"/api/v1/attachments/{attachment['id']}/content", actor=bob)
|
||||
check(downloaded["bytes"] == fixture, "downloaded object differs from upload")
|
||||
check(hashlib.sha256(downloaded["bytes"]).hexdigest() == attachment["sha256"], "checksum mismatch")
|
||||
|
||||
# Too large, corrupt image, and mismatched MIME are controlled client errors.
|
||||
client.call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=large.png", 413,
|
||||
actor=alice, raw=b"\x89PNG\r\n\x1a\n" + b"x" * (10 * 1024 * 1024),
|
||||
headers={"Content-Type": "image/png"})
|
||||
client.call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=bad.png", 415,
|
||||
actor=alice, raw=b"not-an-image", headers={"Content-Type": "image/png"})
|
||||
client.call("POST", f"/api/v1/chats/{chat['id']}/attachments?filename=spoof.png", 415,
|
||||
actor=alice, raw=fixture, headers={"Content-Type": "image/jpeg"})
|
||||
|
||||
# Outsider cannot read metadata or bytes; a random ID is also hidden as 404.
|
||||
client.call("GET", f"/api/v1/attachments/{attachment['id']}", 404, actor=eve)
|
||||
client.call("GET", f"/api/v1/attachments/{attachment['id']}/content", 404, actor=eve)
|
||||
client.call("GET", f"/api/v1/attachments/{uuid.uuid4()}", 404, actor=alice)
|
||||
|
||||
# An attachment from another chat cannot be referenced by a message.
|
||||
other_chat = client.call("POST", "/api/v1/chats", 201, actor=alice,
|
||||
body={"title": "Другой чат", "member_ids": [alice["id"], bob["id"]]}, schema="Chat")["json"]
|
||||
client.call("POST", f"/api/v1/chats/{other_chat['id']}/messages", 404, actor=alice,
|
||||
body={"text": "wrong chat", "attachment_ids": [attachment["id"]]},
|
||||
headers={"Idempotency-Key": "wrong-chat-" + uuid.uuid4().hex})
|
||||
print("PASS: attachment limits, MIME validation, authorization and chat binding")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (Failure, AssertionError, OSError) as error:
|
||||
print("FAIL:", error)
|
||||
raise SystemExit(1)
|
||||
Executable
+82
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Ten concurrent uploads with Docker API memory sampling (local evidence only)."""
|
||||
import argparse
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import time
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
sys.path.insert(0, str(ROOT / "tests"))
|
||||
from client import Client, Failure
|
||||
|
||||
|
||||
def stats():
|
||||
values = []
|
||||
try:
|
||||
output = subprocess.check_output(
|
||||
["docker", "stats", "--no-stream", "--format", "{{.Name}}|{{.MemUsage}}"],
|
||||
text=True, stderr=subprocess.DEVNULL)
|
||||
for line in output.splitlines():
|
||||
if "api-" not in line or " / " not in line:
|
||||
continue
|
||||
used = line.split("|", 1)[1].split(" / ", 1)[0].strip()
|
||||
match = re.match(r"([0-9.]+)\s*(MiB|GiB|MB|GB)", used)
|
||||
if match:
|
||||
value = float(match.group(1)) * (1024 if match.group(2) in ("GiB", "GB") else 1)
|
||||
values.append(value)
|
||||
except (OSError, subprocess.CalledProcessError):
|
||||
pass
|
||||
return values
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--base-url", default="http://localhost:8080")
|
||||
parser.add_argument("--output", default="evidence/upload-memory.json")
|
||||
args = parser.parse_args()
|
||||
client = Client(args.base_url)
|
||||
alice, _, _, chat = client.bootstrap()
|
||||
fixture = (ROOT / "tests/fixtures/sample.png").read_bytes()
|
||||
peak = max(stats() or [0.0])
|
||||
stop = threading.Event()
|
||||
|
||||
def sampler():
|
||||
nonlocal peak
|
||||
while not stop.is_set():
|
||||
peak = max([peak] + stats())
|
||||
time.sleep(0.1)
|
||||
|
||||
thread = threading.Thread(target=sampler, daemon=True)
|
||||
thread.start()
|
||||
started = time.monotonic()
|
||||
with ThreadPoolExecutor(max_workers=10) as pool:
|
||||
futures = [pool.submit(client.call, "POST", f"/api/v1/chats/{chat['id']}/attachments?filename=parallel-{i}.png",
|
||||
201, None, alice, {"Content-Type": "image/png"}, fixture, "Attachment") for i in range(10)]
|
||||
results = [future.result() for future in futures]
|
||||
stop.set()
|
||||
thread.join(timeout=2)
|
||||
report = {
|
||||
"concurrency": 10,
|
||||
"successful_uploads": len(results),
|
||||
"fixture_bytes": len(fixture),
|
||||
"elapsed_seconds": round(time.monotonic() - started, 3),
|
||||
"api_peak_memory_mib": round(peak, 2),
|
||||
"note": "Docker stats samples API containers every 100ms; local measurement, not a capacity benchmark."
|
||||
}
|
||||
output = Path(args.output)
|
||||
output.parent.mkdir(parents=True, exist_ok=True)
|
||||
output.write_text(json.dumps(report, indent=2) + "\n")
|
||||
print(json.dumps(report, ensure_ascii=False))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
main()
|
||||
except (Failure, AssertionError, OSError) as error:
|
||||
print("FAIL:", error)
|
||||
raise SystemExit(1)
|
||||
Reference in New Issue
Block a user