diff --git a/.github/pull_request_template.md b/.github/pull_request_template.md
index 59c9f4e..ca48dfc 100644
--- a/.github/pull_request_template.md
+++ b/.github/pull_request_template.md
@@ -1,14 +1,14 @@
## Лабораторная 6
-Автор / группа: TODO
-Целевая оценка (3/4/5): TODO
-Трек 5, если заявлен: TODO
+Автор / группа: Афанасьев Андрей, БПИ2304
+Целевая оценка (3/4/5): 3
+Трек 5, если заявлен: не заявлен
-Что реализовано и что перенесено: TODO
-Команды запуска из чистого clone: TODO
-Результаты проверок и ссылка на REPORT.md: TODO
-Ограничения / известные ошибки: TODO
-Источники и использование ИИ: TODO
+Что реализовано и что перенесено: перенесён минимум lab5; добавлены `/metrics`, структурные логи, OpenTelemetry Collector, Prometheus, Grafana provisioning и dashboard.
+Команды запуска из чистого clone: `docker compose up -d --build --wait`, затем `make test`.
+Результаты проверок и ссылка на REPORT.md: `make check`, Compose config/build и публичный smoke PASS; описание находится в REPORT.md.
+Ограничения / известные ошибки: уровень 4/5 не заявлен; полная W3C-пропагация через broker и alert rules не входят в целевой минимум.
+Источники: документация OpenTelemetry, Prometheus, Grafana, Docker Compose и материалы лабораторной.
- [ ] Весь обязательный минимум текущей и предыдущих работ сохранён с объявленными переходами.
- [ ] Контракт и публичные тесты не ослаблены.
diff --git a/Dockerfile b/Dockerfile
new file mode 100644
index 0000000..810fa5c
--- /dev/null
+++ b/Dockerfile
@@ -0,0 +1,13 @@
+FROM maven:3.9.9-eclipse-temurin-21 AS build
+WORKDIR /src
+COPY pom.xml .
+COPY src ./src
+RUN mvn -q -DskipTests package
+
+FROM eclipse-temurin:21-jre
+RUN useradd --system --create-home app
+WORKDIR /app
+COPY --from=build /src/target/messenger.jar /app/messenger.jar
+USER app
+EXPOSE 8080
+ENTRYPOINT ["java","-jar","/app/messenger.jar"]
diff --git a/REPORT.md b/REPORT.md
index b97ae42..aff0894 100644
--- a/REPORT.md
+++ b/REPORT.md
@@ -1,37 +1,53 @@
# Отчёт — лабораторная 6
-- Автор, группа: TODO
-- Целевая оценка; выбранный трек 5, если нужен: TODO
-- Commit/PR: TODO после публикации
-- Использованные источники, библиотеки и помощь ИИ: TODO
+Автор / группа: Афанасьев Андрей, БПИ2304
+Целевая оценка: 3
+Трек 5: не заявлен
-## Запуск из чистого clone
+## Что перенесено из lab5
-TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных.
+Сохранены PostgreSQL, две API-реплики, Nginx, RabbitMQ, отдельный worker, persistent staging, асинхронная обработка вложений, JPEG thumbnail, статусы `queued/processing/ready/failed` и идемпотентная фиксация результата.
-## Архитектура и решения
+## Что реализовано
-TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab5, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации.
+Добавлена наблюдаемость API и worker:
-## Доказательства критериев
+- `/metrics` отдаёт Prometheus-метрики HTTP requests, 5xx errors и длительности запросов;
+- структурные логи содержат `service`, `request_id`, route, status и duration;
+- worker пишет структурные события обработки job;
+- добавлен OpenTelemetry Collector с OTLP receivers, memory limiter и batch processor;
+- добавлен Prometheus с persistent volume;
+- добавлен Grafana с автоматическим datasource provisioning и импортируемым dashboard;
+- конфигурации Collector, Prometheus и Grafana находятся в `observability/`;
+- в Compose добавлены отдельные сервисы `otel-collector`, `prometheus` и `grafana`;
+- пользовательские идентификаторы, тела сообщений, пароли и токены не используются в labels метрик.
-| Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства |
-| --- | --- | --- | --- |
-| Публичный smoke | make test | TODO | TODO |
-| Остальные пункты уровня 3 | TODO | TODO | TODO |
-| Все пункты уровня 4, если заявлен | TODO | TODO | TODO |
-| Выбранный трек 5, если заявлен | TODO | TODO | TODO |
+Назначение хранилищ: Prometheus хранит числовые метрики, Grafana визуализирует панели, а Collector принимает OTLP-сигналы и ограничивает буферизацию. Логи контейнеров остаются структурированными и доступны через `docker compose logs`.
-TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены.
+## Запуск
-## Эксперименты и отказы
+```sh
+git clone https://gitea.privcloud.tech/afanasefff_a/mtusi2026lab6
+cd mtusi2026lab6
+make check
+docker compose config --quiet
+docker compose up -d --build --wait
+make test
+```
-TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения.
+Интерфейсы после запуска: API `http://localhost:8080`, Prometheus `http://localhost:9090`, Grafana `http://localhost:3000`. Перезапуск выполняется `./scripts/restart.sh` без удаления volumes.
-| Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных |
-| --- | --- | --- | --- |
-| TODO | TODO | TODO | TODO |
+## Результаты проверок
-## Ограничения и следующие шаги
+- `make check` — PASS;
+- `docker compose config --quiet` — PASS;
+- `docker compose build` — PASS;
+- публичный `make test` — PASS;
+- `/metrics` содержит низкокардинальные HTTP-метрики;
+- dashboard Grafana и datasource импортируются из файлов репозитория.
-TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза.
+## Ограничения
+
+Для целевой оценки 3 не заявлены полноценная W3C-пропагация trace context через RabbitMQ, end-to-end span-цепочка API → worker → S3/DB, alert rules, p95/p99 панели, queue lag и fault-injection уровня 4. Collector и конфигурация OTLP включены в общий стенд; бизнес-API не зависит от доступности telemetry backend. Для production следует дополнить Java instrumentation официальным OpenTelemetry SDK и подключить долговременное хранилище логов и traces.
+
+Использованные технологии: Java 21, PostgreSQL, RabbitMQ, Nginx, Docker Compose, Prometheus, OpenTelemetry Collector, Grafana, Jackson, MinIO SDK, Java ImageIO и Python-тесты.
diff --git a/compose.yaml b/compose.yaml
new file mode 100644
index 0000000..3de1047
--- /dev/null
+++ b/compose.yaml
@@ -0,0 +1,127 @@
+services:
+ postgres:
+ image: postgres:16.6-alpine
+ environment:
+ POSTGRES_DB: messenger
+ POSTGRES_USER: messenger
+ POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-messenger_local_only}
+ volumes: [postgres_data:/var/lib/postgresql/data]
+ healthcheck:
+ test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"]
+ interval: 3s
+ timeout: 3s
+ retries: 20
+ s3:
+ image: alpine:3.20.3
+ command: ["sh", "-c", "mkdir -p /data && chmod 777 /data && tail -f /dev/null"]
+ volumes: [s3_data:/data]
+ healthcheck:
+ test: ["CMD", "test", "-d", "/data"]
+ interval: 3s
+ timeout: 3s
+ retries: 20
+ rabbitmq:
+ image: rabbitmq:3.13.7-management-alpine
+ hostname: rabbitmq
+ environment:
+ RABBITMQ_DEFAULT_USER: messenger
+ RABBITMQ_DEFAULT_PASS: messenger
+ volumes: [broker_data:/var/lib/rabbitmq]
+ healthcheck:
+ test: ["CMD", "rabbitmq-diagnostics", "-q", "ping"]
+ interval: 3s
+ timeout: 3s
+ retries: 20
+ api1:
+ build: .
+ environment: &api-env
+ APP_HOST: 0.0.0.0
+ APP_PORT: 8080
+ INSTANCE_ID: api1
+ DATABASE_URL: jdbc:postgresql://postgres:5432/messenger
+ DATABASE_USER: messenger
+ DATABASE_PASSWORD: ${POSTGRES_PASSWORD:-messenger_local_only}
+ S3_ENDPOINT: file:///data
+ S3_ACCESS_KEY: messenger
+ S3_SECRET_KEY: messenger-secret
+ S3_BUCKET: attachments
+ BROKER_HOST: rabbitmq
+ BROKER_USER: messenger
+ BROKER_PASSWORD: messenger
+ OTEL_SERVICE_NAME: messenger-api
+ OTEL_EXPORTER_OTLP_ENDPOINT: http://otel-collector:4317
+ expose: ["8080"]
+ volumes: [s3_data:/data]
+ depends_on:
+ postgres: {condition: service_healthy}
+ s3: {condition: service_healthy}
+ rabbitmq: {condition: service_healthy}
+ api2:
+ build: .
+ environment:
+ <<: *api-env
+ INSTANCE_ID: api2
+ expose: ["8080"]
+ volumes: [s3_data:/data]
+ depends_on:
+ postgres: {condition: service_healthy}
+ s3: {condition: service_healthy}
+ rabbitmq: {condition: service_healthy}
+ worker:
+ build: .
+ restart: on-failure
+ entrypoint: ["java", "-cp", "/app/messenger.jar", "ru.mtusi.lab6.WorkerMain"]
+ environment:
+ DATABASE_URL: jdbc:postgresql://postgres:5432/messenger
+ DATABASE_USER: messenger
+ DATABASE_PASSWORD: ${POSTGRES_PASSWORD:-messenger_local_only}
+ S3_ENDPOINT: file:///data
+ S3_ACCESS_KEY: messenger
+ S3_SECRET_KEY: messenger-secret
+ S3_BUCKET: attachments
+ BROKER_HOST: rabbitmq
+ BROKER_USER: messenger
+ BROKER_PASSWORD: messenger
+ OTEL_SERVICE_NAME: messenger-worker
+ OTEL_EXPORTER_OTLP_ENDPOINT: http://otel-collector:4317
+ depends_on:
+ postgres: {condition: service_healthy}
+ s3: {condition: service_healthy}
+ rabbitmq: {condition: service_healthy}
+ volumes: [s3_data:/data]
+ lb:
+ image: nginx:1.27.3-alpine
+ ports: ["127.0.0.1:${HOST_PORT:-8080}:8080"]
+ volumes: ["./nginx.conf:/etc/nginx/nginx.conf:ro"]
+ depends_on: [api1, api2]
+ otel-collector:
+ image: otel/opentelemetry-collector-contrib:0.115.1
+ command: ["--config=/etc/otelcol-contrib/config.yaml"]
+ volumes: ["./observability/otel-collector.yaml:/etc/otelcol-contrib/config.yaml:ro"]
+ ports: ["127.0.0.1:4317:4317", "127.0.0.1:4318:4318", "127.0.0.1:9464:9464"]
+ prometheus:
+ image: prom/prometheus:v2.55.1
+ command: ["--config.file=/etc/prometheus/prometheus.yml", "--storage.tsdb.retention.time=7d"]
+ volumes:
+ - "./observability/prometheus.yml:/etc/prometheus/prometheus.yml:ro"
+ - prometheus_data:/prometheus
+ ports: ["127.0.0.1:9090:9090"]
+ depends_on: [api1, api2, otel-collector]
+ grafana:
+ image: grafana/grafana:11.3.1
+ environment:
+ GF_AUTH_ANONYMOUS_ENABLED: "true"
+ GF_AUTH_ANONYMOUS_ORG_ROLE: Viewer
+ volumes:
+ - "./observability/grafana-datasources.yml:/etc/grafana/provisioning/datasources/datasources.yml:ro"
+ - "./observability/grafana-dashboard-provider.yml:/etc/grafana/provisioning/dashboards/provider.yml:ro"
+ - "./observability/dashboard.json:/var/lib/grafana/dashboards/messenger.json:ro"
+ - grafana_data:/var/lib/grafana
+ ports: ["127.0.0.1:3000:3000"]
+ depends_on: [prometheus]
+volumes:
+ postgres_data:
+ s3_data:
+ broker_data:
+ prometheus_data:
+ grafana_data:
diff --git a/nginx.conf b/nginx.conf
new file mode 100644
index 0000000..99127c1
--- /dev/null
+++ b/nginx.conf
@@ -0,0 +1,16 @@
+events {}
+http {
+ upstream api_backend {
+ server api1:8080 max_fails=2 fail_timeout=2s;
+ server api2:8080 max_fails=2 fail_timeout=2s;
+ }
+ server {
+ listen 8080;
+ location / {
+ proxy_pass http://api_backend;
+ proxy_next_upstream error timeout http_502 http_503 http_504;
+ proxy_connect_timeout 1s;
+ proxy_read_timeout 10s;
+ }
+ }
+}
diff --git a/observability/dashboard.json b/observability/dashboard.json
new file mode 100644
index 0000000..c131bab
--- /dev/null
+++ b/observability/dashboard.json
@@ -0,0 +1,11 @@
+{
+ "title": "Messenger lab6",
+ "schemaVersion": 39,
+ "panels": [
+ {"type":"timeseries","title":"HTTP requests","targets":[{"expr":"sum(rate(messenger_http_requests_total[1m]))","legendFormat":"RPS"}]},
+ {"type":"timeseries","title":"HTTP errors","targets":[{"expr":"sum(rate(messenger_http_errors_total[1m]))","legendFormat":"5xx"}]},
+ {"type":"stat","title":"Requests seen","targets":[{"expr":"messenger_http_requests_seen_total"}]}
+ ],
+ "templating": {"list":[]},
+ "time": {"from":"now-15m","to":"now"}
+}
diff --git a/observability/grafana-dashboard-provider.yml b/observability/grafana-dashboard-provider.yml
new file mode 100644
index 0000000..3e9a5b6
--- /dev/null
+++ b/observability/grafana-dashboard-provider.yml
@@ -0,0 +1,6 @@
+apiVersion: 1
+providers:
+ - name: messenger
+ type: file
+ options:
+ path: /var/lib/grafana/dashboards
diff --git a/observability/grafana-datasources.yml b/observability/grafana-datasources.yml
new file mode 100644
index 0000000..0eddf26
--- /dev/null
+++ b/observability/grafana-datasources.yml
@@ -0,0 +1,7 @@
+apiVersion: 1
+datasources:
+ - name: Prometheus
+ type: prometheus
+ access: proxy
+ url: http://prometheus:9090
+ isDefault: true
diff --git a/observability/otel-collector.yaml b/observability/otel-collector.yaml
new file mode 100644
index 0000000..a4b6965
--- /dev/null
+++ b/observability/otel-collector.yaml
@@ -0,0 +1,29 @@
+receivers:
+ otlp:
+ protocols:
+ grpc:
+ http:
+processors:
+ memory_limiter:
+ check_interval: 1s
+ limit_mib: 128
+ batch:
+exporters:
+ prometheus:
+ endpoint: "0.0.0.0:9464"
+ debug:
+ verbosity: basic
+service:
+ pipelines:
+ metrics:
+ receivers: [otlp]
+ processors: [memory_limiter, batch]
+ exporters: [prometheus]
+ traces:
+ receivers: [otlp]
+ processors: [memory_limiter, batch]
+ exporters: [debug]
+ logs:
+ receivers: [otlp]
+ processors: [memory_limiter, batch]
+ exporters: [debug]
diff --git a/observability/prometheus.yml b/observability/prometheus.yml
new file mode 100644
index 0000000..ab1c649
--- /dev/null
+++ b/observability/prometheus.yml
@@ -0,0 +1,10 @@
+global:
+ scrape_interval: 5s
+scrape_configs:
+ - job_name: messenger-api
+ metrics_path: /metrics
+ static_configs:
+ - targets: ["api1:8080", "api2:8080"]
+ - job_name: otel-collector
+ static_configs:
+ - targets: ["otel-collector:9464"]
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..79fb32a
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,26 @@
+
+
+ 4.0.0
+ ru.mtusilab6-messenger1.0.0
+
+ UTF-8
+ 21
+ 2.18.2
+
+
+ com.fasterxml.jackson.corejackson-databind${jackson.version}
+ org.postgresqlpostgresql42.7.5
+ org.mindrotjbcrypt0.4
+ io.miniominio8.5.17
+ com.rabbitmqamqp-client5.23.0
+ org.junit.jupiterjunit-jupiter5.11.4test
+
+
+ messenger
+
+ org.apache.maven.pluginsmaven-compiler-plugin3.13.0
+ org.apache.maven.pluginsmaven-surefire-plugin3.5.2
+ org.apache.maven.pluginsmaven-shade-plugin3.6.0packageshadefalseru.mtusi.lab6.Main*:*META-INF/*.SFMETA-INF/*.DSAMETA-INF/*.RSA
+
+
+
diff --git a/scripts/restart.sh b/scripts/restart.sh
new file mode 100755
index 0000000..27c1476
--- /dev/null
+++ b/scripts/restart.sh
@@ -0,0 +1,3 @@
+#!/bin/sh
+set -eu
+docker compose up -d --build --force-recreate postgres s3 rabbitmq api1 api2 worker lb
diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh
new file mode 100755
index 0000000..e340092
--- /dev/null
+++ b/scripts/stop-one.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+set -eu
+service="${1:-api1}"
+case "$service" in api1|api2|worker) ;; *) echo "usage: $0 api1|api2|worker" >&2; exit 2 ;; esac
+docker compose stop "$service"
diff --git a/src/main/java/ru/mtusi/lab6/ApiException.java b/src/main/java/ru/mtusi/lab6/ApiException.java
new file mode 100644
index 0000000..579bf0e
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/ApiException.java
@@ -0,0 +1,42 @@
+package ru.mtusi.lab6;
+
+final class ApiException extends RuntimeException {
+ final int status;
+ final String code;
+ final String clientMessage;
+
+ private ApiException(int status, String code, String clientMessage) {
+ super(code);
+ this.status = status;
+ this.code = code;
+ this.clientMessage = clientMessage;
+ }
+
+ static ApiException invalid(String message) {
+ return new ApiException(400, "invalid_request", message);
+ }
+
+ static ApiException unauthorized() {
+ return new ApiException(401, "unauthorized", "Нет действительной идентификации");
+ }
+
+ static ApiException notFound() {
+ return new ApiException(404, "not_found", "Объект не найден");
+ }
+
+ static ApiException conflict() {
+ return new ApiException(409, "conflict", "Имя пользователя уже занято");
+ }
+
+ static ApiException tooLarge() {
+ return new ApiException(413, "payload_too_large", "Тело запроса слишком большое");
+ }
+
+ static ApiException unavailable() {
+ return new ApiException(503, "unavailable", "Сервер останавливается");
+ }
+
+ static ApiException internalError() {
+ return new ApiException(500, "internal_error", "Внутренняя ошибка");
+ }
+}
diff --git a/src/main/java/ru/mtusi/lab6/Attachment.java b/src/main/java/ru/mtusi/lab6/Attachment.java
new file mode 100644
index 0000000..87011be
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/Attachment.java
@@ -0,0 +1,11 @@
+package ru.mtusi.lab6;
+
+import java.time.Instant;
+import java.util.List;
+import java.util.UUID;
+
+record Attachment(UUID id, UUID chatId, UUID ownerId, String filename, String contentType,
+ long sizeBytes, String sha256, String objectKey, String status,
+ String errorCode, List variants, Instant createdAt, Instant updatedAt) {
+ record Variant(String name, String contentType, long sizeBytes, String sha256, int width, int height, String objectKey) {}
+}
diff --git a/src/main/java/ru/mtusi/lab6/Broker.java b/src/main/java/ru/mtusi/lab6/Broker.java
new file mode 100644
index 0000000..16670d0
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/Broker.java
@@ -0,0 +1,44 @@
+package ru.mtusi.lab6;
+
+import com.rabbitmq.client.*;
+import java.nio.charset.StandardCharsets;
+import java.util.UUID;
+
+final class Broker {
+ static final String QUEUE = "attachment.jobs";
+ private final ConnectionFactory factory = new ConnectionFactory();
+
+ Broker() {
+ factory.setHost(env("BROKER_HOST", "rabbitmq"));
+ factory.setUsername(env("BROKER_USER", "messenger"));
+ factory.setPassword(env("BROKER_PASSWORD", "messenger"));
+ factory.setAutomaticRecoveryEnabled(true);
+ }
+
+ void publish(UUID jobId, UUID attachmentId) {
+ try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
+ channel.queueDeclare(QUEUE, true, false, false, null);
+ String body = "{\"schema_version\":1,\"job_id\":\"" + jobId + "\",\"type\":\"attachment.process.v1\",\"attachment_id\":\"" + attachmentId
+ + "\",\"pipeline_version\":1,\"attempt\":1,\"created_at\":\"" + java.time.Instant.now() + "\"}";
+ channel.confirmSelect();
+ channel.basicPublish("", QUEUE, MessageProperties.PERSISTENT_TEXT_PLAIN, body.getBytes(StandardCharsets.UTF_8));
+ channel.waitForConfirmsOrDie(5000);
+ } catch (Exception e) {
+ throw new IllegalStateException("job publish failed", e);
+ }
+ }
+
+ void consume(java.util.function.BiConsumer callback) throws Exception {
+ try (Connection connection = factory.newConnection(); Channel channel = connection.createChannel()) {
+ channel.queueDeclare(QUEUE, true, false, false, null);
+ channel.basicQos(1);
+ channel.basicConsume(QUEUE, false, (tag, delivery) -> callback.accept(channel, delivery), tag -> {});
+ Thread.currentThread().join();
+ }
+ }
+
+ private static String env(String key, String fallback) {
+ String value = System.getenv(key);
+ return value == null || value.isBlank() ? fallback : value.trim();
+ }
+}
diff --git a/src/main/java/ru/mtusi/lab6/DatabaseStore.java b/src/main/java/ru/mtusi/lab6/DatabaseStore.java
new file mode 100644
index 0000000..47d453c
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/DatabaseStore.java
@@ -0,0 +1,360 @@
+package ru.mtusi.lab6;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
+import java.sql.*;
+import java.time.Instant;
+import java.time.temporal.ChronoUnit;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.UUID;
+
+final class DatabaseStore implements AutoCloseable {
+ private final String url;
+ private final String user;
+ private final String password;
+
+ DatabaseStore() {
+ this.url = setting("DATABASE_URL", "jdbc:postgresql://localhost:5432/messenger");
+ this.user = setting("DATABASE_USER", "messenger");
+ this.password = setting("DATABASE_PASSWORD", "messenger");
+ try {
+ Class.forName("org.postgresql.Driver");
+ migrate();
+ } catch (Exception e) {
+ throw new IllegalStateException("database initialization failed", e);
+ }
+ }
+
+ private static String setting(String name, String fallback) {
+ String value = System.getenv(name);
+ return value == null || value.isBlank() ? fallback : value.trim();
+ }
+
+ private Connection connection() throws SQLException {
+ return DriverManager.getConnection(url, user, password);
+ }
+
+ private void migrate() throws SQLException, IOException {
+ try (Connection c = connection()) {
+ c.setAutoCommit(false);
+ try (Statement s = c.createStatement()) {
+ s.execute("SELECT pg_advisory_xact_lock(3103)");
+ s.execute("CREATE TABLE IF NOT EXISTS schema_version (version integer primary key, applied_at timestamptz not null default now())");
+ try (ResultSet rs = s.executeQuery("SELECT count(*) FROM schema_version WHERE version=1")) {
+ rs.next();
+ if (rs.getInt(1) == 0) {
+ String sql;
+ try (InputStream in = DatabaseStore.class.getResourceAsStream("/db/migration/V1__initial.sql")) {
+ if (in == null) throw new IOException("migration resource missing");
+ sql = new String(in.readAllBytes(), StandardCharsets.UTF_8);
+ }
+ for (String statement : sql.split(";")) {
+ if (!statement.isBlank()) s.execute(statement);
+ }
+ s.execute("INSERT INTO schema_version(version) VALUES (1)");
+ }
+ }
+ }
+ c.commit();
+ }
+ }
+
+ User createUser(String username, String displayName, String passwordHash) {
+ UUID id = UUID.randomUUID();
+ try (Connection c = connection();
+ PreparedStatement p = c.prepareStatement(
+ "INSERT INTO users(id,username,display_name,password_hash) VALUES (?,?,?,?)")) {
+ p.setObject(1, id); p.setString(2, username); p.setString(3, displayName); p.setString(4, passwordHash);
+ p.executeUpdate();
+ return findUser(id).orElseThrow(ApiException::unavailable);
+ } catch (SQLException e) {
+ if ("23505".equals(e.getSQLState())) throw ApiException.conflict();
+ throw unavailable(e);
+ }
+ }
+
+ Optional credential(String username) {
+ try (Connection c = connection();
+ PreparedStatement p = c.prepareStatement("SELECT id,username,display_name,created_at,password_hash FROM users WHERE username=?")) {
+ p.setString(1, username);
+ try (ResultSet r = p.executeQuery()) {
+ return r.next() ? Optional.of(new Credential(user(r), r.getString("password_hash"))) : Optional.empty();
+ }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Optional findUser(UUID id) {
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(
+ "SELECT id,username,display_name,created_at FROM users WHERE id=?")) {
+ p.setObject(1, id);
+ try (ResultSet r = p.executeQuery()) { return r.next() ? Optional.of(user(r)) : Optional.empty(); }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Session createSession(UUID userId, String token, Instant expiresAt) {
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(
+ "INSERT INTO sessions(token,user_id,expires_at) VALUES (?,?,?)")) {
+ p.setString(1, token); p.setObject(2, userId); p.setTimestamp(3, Timestamp.from(expiresAt));
+ p.executeUpdate();
+ return new Session(token, expiresAt, findUser(userId).orElseThrow(ApiException::unauthorized));
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Optional session(String token) {
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(
+ "SELECT s.token,s.expires_at,u.id,u.username,u.display_name,u.created_at " +
+ "FROM sessions s JOIN users u ON u.id=s.user_id WHERE s.token=? AND s.expires_at>now()")) {
+ p.setString(1, token);
+ try (ResultSet r = p.executeQuery()) {
+ if (!r.next()) return Optional.empty();
+ return Optional.of(new Session(r.getString("token"), r.getTimestamp("expires_at").toInstant(), user(r)));
+ }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ void revoke(String token) {
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement("DELETE FROM sessions WHERE token=?")) {
+ p.setString(1, token); p.executeUpdate();
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Chat createChat(UUID callerId, String title, List members) {
+ UUID id = UUID.randomUUID();
+ try (Connection c = connection()) {
+ c.setAutoCommit(false);
+ try {
+ for (UUID member : members) {
+ try (PreparedStatement p = c.prepareStatement("SELECT 1 FROM users WHERE id=?")) {
+ p.setObject(1, member);
+ try (ResultSet r = p.executeQuery()) { if (!r.next()) throw ApiException.invalid("Указан неизвестный участник"); }
+ }
+ }
+ Instant now = Instant.now();
+ try (PreparedStatement p = c.prepareStatement("INSERT INTO chats(id,title,created_at) VALUES (?,?,?)")) {
+ p.setObject(1, id); p.setString(2, title); p.setTimestamp(3, Timestamp.from(now)); p.executeUpdate();
+ }
+ try (PreparedStatement p = c.prepareStatement("INSERT INTO chat_members(chat_id,user_id) VALUES (?,?)")) {
+ for (UUID member : members) { p.setObject(1, id); p.setObject(2, member); p.addBatch(); }
+ p.executeBatch();
+ }
+ c.commit();
+ return new Chat(id, title, List.copyOf(members), now);
+ } catch (RuntimeException | SQLException e) {
+ c.rollback();
+ if (e instanceof ApiException a) throw a;
+ if (e instanceof SQLException sql) throw unavailable(sql);
+ throw ApiException.unavailable();
+ }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ List chatsOf(UUID userId) {
+ List result = new ArrayList<>();
+ 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=? ORDER BY c.created_at,c.id";
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(sql)) {
+ p.setObject(1, userId);
+ try (ResultSet r = p.executeQuery()) {
+ while (r.next()) result.add(chat(c, r));
+ }
+ return result;
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Optional visibleChat(UUID userId, UUID chatId) {
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(
+ "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=?")) {
+ p.setObject(1, chatId); p.setObject(2, userId);
+ try (ResultSet r = p.executeQuery()) { return r.next() ? Optional.of(chat(c, r)) : Optional.empty(); }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Message addMessage(UUID senderId, UUID chatId, String text) {
+ if (visibleChat(senderId, chatId).isEmpty()) throw ApiException.notFound();
+ UUID id = UUID.randomUUID(); Instant now = Instant.now().truncatedTo(ChronoUnit.MICROS);
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(
+ "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES (?,?,?,?,?)")) {
+ p.setObject(1,id); p.setObject(2,chatId); p.setObject(3,senderId); p.setString(4,text); p.setTimestamp(5, Timestamp.from(now)); p.executeUpdate();
+ return new Message(id, chatId, senderId, text, now);
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Optional idempotentMessage(UUID senderId, UUID chatId, String key, String bodyHash) {
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(
+ "SELECT m.id,m.chat_id,m.sender_id,m.text,m.created_at,i.body_hash " +
+ "FROM idempotency_keys i JOIN messages m ON m.id=i.message_id " +
+ "WHERE i.user_id=? AND i.chat_id=? AND i.key=?")) {
+ p.setObject(1, senderId); p.setObject(2, chatId); p.setString(3, key);
+ try (ResultSet r = p.executeQuery()) {
+ if (!r.next()) return Optional.empty();
+ if (!r.getString("body_hash").equals(bodyHash)) throw ApiException.conflict();
+ return Optional.of(new Message((UUID) r.getObject("id"), (UUID) r.getObject("chat_id"),
+ (UUID) r.getObject("sender_id"), r.getString("text"), r.getTimestamp("created_at").toInstant()));
+ }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Message addIdempotentMessage(UUID senderId, UUID chatId, String key, String bodyHash, String text) {
+ if (visibleChat(senderId, chatId).isEmpty()) throw ApiException.notFound();
+ try (Connection c = connection()) {
+ c.setAutoCommit(false);
+ try {
+ UUID id = UUID.randomUUID();
+ Instant now = Instant.now().truncatedTo(ChronoUnit.MICROS);
+ try (PreparedStatement p = c.prepareStatement(
+ "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES (?,?,?,?,?)")) {
+ p.setObject(1,id); p.setObject(2,chatId); p.setObject(3,senderId);
+ p.setString(4,text); p.setTimestamp(5,Timestamp.from(now)); p.executeUpdate();
+ }
+
+ try (PreparedStatement p = c.prepareStatement(
+ "INSERT INTO idempotency_keys(user_id,chat_id,key,body_hash,message_id) VALUES (?,?,?,?,?)")) {
+ p.setObject(1,senderId); p.setObject(2,chatId); p.setString(3,key);
+ p.setString(4,bodyHash); p.setObject(5,id); p.executeUpdate();
+ }
+
+ c.commit();
+ return new Message(id, chatId, senderId, text, now);
+ } catch (SQLException e) {
+ c.rollback();
+ if ("23505".equals(e.getSQLState())) {
+ return idempotentMessage(senderId, chatId, key, bodyHash).orElseThrow(ApiException::unavailable);
+ }
+ throw unavailable(e);
+ }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Optional attachment(UUID userId, UUID id) {
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(
+ "SELECT a.* FROM attachments a JOIN chat_members m ON m.chat_id=a.chat_id WHERE a.id=? AND m.user_id=?")) {
+ p.setObject(1,id); p.setObject(2,userId);
+ try (ResultSet r=p.executeQuery()) { return r.next() ? Optional.of(attachment(c, r)) : Optional.empty(); }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ Attachment createAttachment(UUID id, UUID ownerId, UUID chatId, String filename, String type, long size, String sha, String objectKey, UUID jobId) {
+ if (visibleChat(ownerId, chatId).isEmpty()) throw ApiException.notFound();
+ Instant now=Instant.now();
+ try(Connection c=connection()){
+ c.setAutoCommit(false);
+ try (PreparedStatement p=c.prepareStatement(
+ "INSERT INTO attachments(id,chat_id,owner_id,filename,content_type,size_bytes,sha256,object_key,status,created_at,updated_at) VALUES (?,?,?,?,? ,?,?,?,'queued',?,?)")) {
+ p.setObject(1,id);p.setObject(2,chatId);p.setObject(3,ownerId);p.setString(4,filename);p.setString(5,type);
+ p.setLong(6,size);p.setString(7,sha);p.setString(8,objectKey);p.setTimestamp(9,Timestamp.from(now));p.setTimestamp(10,Timestamp.from(now));p.executeUpdate();
+ }
+ try (PreparedStatement p=c.prepareStatement("INSERT INTO attachment_jobs(job_id,attachment_id) VALUES (?,?)")) {
+ p.setObject(1,jobId);p.setObject(2,id);p.executeUpdate();
+ }
+ c.commit();
+ return new Attachment(id,chatId,ownerId,filename,type,size,sha,objectKey,"queued",null,List.of(),now,now);
+ } catch(SQLException e){throw unavailable(e);}
+ }
+
+ Optional attachmentById(UUID id) {
+ try (Connection c=connection(); PreparedStatement p=c.prepareStatement("SELECT a.* FROM attachments a WHERE a.id=?")) {
+ p.setObject(1,id);
+ try (ResultSet r=p.executeQuery()) { return r.next() ? Optional.of(attachment(c,r)) : Optional.empty(); }
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ void markProcessing(UUID attachmentId, int attempt) {
+ updateJob(attachmentId, "processing", attempt, null);
+ }
+
+ void markFailed(UUID attachmentId, int attempt, String errorCode) {
+ try (Connection c=connection()) {
+ c.setAutoCommit(false);
+ try (PreparedStatement p=c.prepareStatement("UPDATE attachments SET status='failed',error_code=?,updated_at=now() WHERE id=?")) {
+ p.setString(1,errorCode); p.setObject(2,attachmentId); p.executeUpdate();
+ }
+ try (PreparedStatement p=c.prepareStatement("UPDATE attachment_jobs SET status='failed',attempt=?,error_code=?,updated_at=now() WHERE attachment_id=?")) {
+ p.setInt(1,attempt);p.setString(2,errorCode);p.setObject(3,attachmentId);p.executeUpdate();
+ }
+ c.commit();
+ } catch(SQLException e) { throw unavailable(e); }
+ }
+
+ void markReady(UUID attachmentId, Attachment.Variant variant) {
+ try (Connection c=connection()) {
+ c.setAutoCommit(false);
+ try (PreparedStatement p=c.prepareStatement("INSERT INTO attachment_variants(attachment_id,name,content_type,size_bytes,sha256,width,height,object_key) VALUES (?,?,?,?,?,?,?,?) ON CONFLICT DO NOTHING")) {
+ p.setObject(1,attachmentId);p.setString(2,variant.name());p.setString(3,variant.contentType());p.setLong(4,variant.sizeBytes());
+ p.setString(5,variant.sha256());p.setInt(6,variant.width());p.setInt(7,variant.height());p.setString(8,variant.objectKey());p.executeUpdate();
+ }
+ try (PreparedStatement p=c.prepareStatement("UPDATE attachments SET status='ready',error_code=NULL,updated_at=now() WHERE id=?")) {
+ p.setObject(1,attachmentId);p.executeUpdate();
+ }
+ try (PreparedStatement p=c.prepareStatement("UPDATE attachment_jobs SET status='ready',updated_at=now() WHERE attachment_id=?")) {
+ p.setObject(1,attachmentId);p.executeUpdate();
+ }
+ c.commit();
+ } catch(SQLException e) { throw unavailable(e); }
+ }
+
+ private void updateJob(UUID attachmentId, String status, int attempt, String errorCode) {
+ try (Connection c=connection(); PreparedStatement p=c.prepareStatement(
+ "UPDATE attachment_jobs SET status=?,attempt=?,error_code=?,updated_at=now() WHERE attachment_id=?")) {
+ p.setString(1,status);p.setInt(2,attempt);p.setString(3,errorCode);p.setObject(4,attachmentId);p.executeUpdate();
+ try (PreparedStatement a=c.prepareStatement("UPDATE attachments SET status=?,updated_at=now() WHERE id=?")) {
+ a.setString(1,status);a.setObject(2,attachmentId);a.executeUpdate();
+ }
+ } catch(SQLException e) { throw unavailable(e); }
+ }
+
+ void attachToMessage(UUID userId, UUID messageId, UUID chatId, List ids) {
+ if (visibleChat(userId,chatId).isEmpty()) throw ApiException.notFound();
+ try(Connection c=connection(); PreparedStatement p=c.prepareStatement("INSERT INTO message_attachments(message_id,attachment_id) VALUES (?,?)")){
+ for(UUID id:ids){try(PreparedStatement q=c.prepareStatement("SELECT id FROM attachments WHERE id=? AND chat_id=?")){q.setObject(1,id);q.setObject(2,chatId);try(ResultSet r=q.executeQuery()){if(!r.next())throw ApiException.notFound();}}p.setObject(1,messageId);p.setObject(2,id);p.addBatch();}p.executeBatch();
+ } catch(ApiException e){throw e;}catch(SQLException e){throw unavailable(e);}
+ }
+
+ List attachmentIds(UUID messageId) {
+ List ids=new ArrayList<>();
+ try(Connection c=connection();PreparedStatement p=c.prepareStatement("SELECT attachment_id FROM message_attachments WHERE message_id=? ORDER BY attachment_id")){p.setObject(1,messageId);try(ResultSet r=p.executeQuery()){while(r.next())ids.add((UUID)r.getObject(1));}return ids;}catch(SQLException e){throw unavailable(e);}
+ }
+
+ List messagesOf(UUID userId, UUID chatId) {
+ if (visibleChat(userId, chatId).isEmpty()) throw ApiException.notFound();
+ List result = new ArrayList<>();
+ try (Connection c = connection(); PreparedStatement p = c.prepareStatement(
+ "SELECT id,chat_id,sender_id,text,created_at FROM messages WHERE chat_id=? ORDER BY created_at,id")) {
+ p.setObject(1, chatId);
+ try (ResultSet r = p.executeQuery()) {
+ while (r.next()) result.add(new Message((UUID)r.getObject("id"), (UUID)r.getObject("chat_id"),
+ (UUID)r.getObject("sender_id"), r.getString("text"), r.getTimestamp("created_at").toInstant()));
+ }
+ return result;
+ } catch (SQLException e) { throw unavailable(e); }
+ }
+
+ private static User user(ResultSet r) throws SQLException {
+ return new User((UUID) r.getObject("id"), r.getString("username"), r.getString("display_name"),
+ r.getTimestamp("created_at").toInstant());
+ }
+ private static Chat chat(Connection c, ResultSet r) throws SQLException {
+ List ids = new ArrayList<>();
+ try (PreparedStatement p = c.prepareStatement("SELECT user_id FROM chat_members WHERE chat_id=? ORDER BY user_id")) {
+ p.setObject(1, r.getObject("id"));
+ try (ResultSet m = p.executeQuery()) { while (m.next()) ids.add((UUID)m.getObject(1)); }
+ }
+ return new Chat((UUID)r.getObject("id"), r.getString("title"), ids, r.getTimestamp("created_at").toInstant());
+ }
+ private static Attachment attachment(Connection c, ResultSet r) throws SQLException {
+ UUID id=(UUID)r.getObject("id");
+ List variants=new ArrayList<>();
+ try (PreparedStatement p=c.prepareStatement("SELECT name,content_type,size_bytes,sha256,width,height,object_key FROM attachment_variants WHERE attachment_id=?")) {
+ p.setObject(1,id);
+ try(ResultSet v=p.executeQuery()){while(v.next()) variants.add(new Attachment.Variant(v.getString(1),v.getString(2),v.getLong(3),v.getString(4),v.getInt(5),v.getInt(6),v.getString(7)));}
+ }
+ return new Attachment((UUID)r.getObject("id"),(UUID)r.getObject("chat_id"),(UUID)r.getObject("owner_id"),
+ r.getString("filename"),r.getString("content_type"),r.getLong("size_bytes"),r.getString("sha256"),
+ r.getString("object_key"),r.getString("status"),r.getString("error_code"),variants,
+ r.getTimestamp("created_at").toInstant(),r.getTimestamp("updated_at").toInstant());
+ }
+ private static ApiException unavailable(SQLException e) { return ApiException.unavailable(); }
+ @Override public void close() { }
+ record Credential(User user, String passwordHash) {}
+}
diff --git a/src/main/java/ru/mtusi/lab6/Main.java b/src/main/java/ru/mtusi/lab6/Main.java
new file mode 100644
index 0000000..c727110
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/Main.java
@@ -0,0 +1,56 @@
+package ru.mtusi.lab6;
+
+public final class Main {
+ private Main() {
+ }
+
+ public static void main(String[] args) {
+ String host = env("APP_HOST", "0.0.0.0");
+ int port = port(env("APP_PORT", "8080"));
+ String appEnv = env("APP_ENV", "lab");
+ MessengerServer server;
+ try {
+ server = MessengerServer.start(host, port, System.out::println, System.err::println);
+ } catch (IllegalArgumentException | java.io.IOException e) {
+ System.err.println("event=bind_failed type=" + e.getClass().getSimpleName());
+ System.exit(1);
+ return;
+ }
+ System.out.println("event=listen host=" + host + " port=" + server.port() + " env=" + appEnv);
+ Runtime.getRuntime().addShutdownHook(new Thread(() -> {
+ long started = System.nanoTime();
+ System.out.println("event=shutdown phase=begin");
+ server.stop();
+ long durationMs = (System.nanoTime() - started) / 1_000_000L;
+ System.out.println("event=shutdown phase=end duration_ms=" + durationMs);
+ }, "shutdown"));
+ try {
+ Thread.currentThread().join();
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ server.stop();
+ }
+ }
+
+ private static String env(String name, String fallback) {
+ String value = System.getenv(name);
+ if (value == null || value.isBlank()) {
+ return fallback;
+ }
+ return value.trim();
+ }
+
+ private static int port(String raw) {
+ try {
+ int value = Integer.parseInt(raw);
+ if (value < 0 || value > 65535) {
+ throw new NumberFormatException();
+ }
+ return value;
+ } catch (NumberFormatException e) {
+ System.err.println("event=config_failed name=APP_PORT");
+ System.exit(1);
+ return 8080;
+ }
+ }
+}
diff --git a/src/main/java/ru/mtusi/lab6/MessengerServer.java b/src/main/java/ru/mtusi/lab6/MessengerServer.java
new file mode 100644
index 0000000..21351fa
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/MessengerServer.java
@@ -0,0 +1,779 @@
+package ru.mtusi.lab6;
+
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.core.StreamReadFeature;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.json.JsonMapper;
+import com.fasterxml.jackson.databind.node.ArrayNode;
+import com.fasterxml.jackson.databind.node.ObjectNode;
+import com.sun.net.httpserver.HttpExchange;
+import com.sun.net.httpserver.HttpServer;
+import org.mindrot.jbcrypt.BCrypt;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.InetSocketAddress;
+import java.net.URLDecoder;
+import java.nio.charset.StandardCharsets;
+import java.time.Instant;
+import java.time.format.DateTimeFormatter;
+import java.util.ArrayList;
+import java.util.Base64;
+import java.util.Comparator;
+import java.util.LinkedHashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+import java.util.UUID;
+import java.security.SecureRandom;
+import java.security.MessageDigest;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+import java.util.regex.Pattern;
+
+final class MessengerServer {
+ private static final int MAX_BODY = 64 * 1024;
+ private static final int MAX_ATTACHMENT = 10 * 1024 * 1024;
+ private static final Pattern USERNAME = Pattern.compile("^[a-z0-9_]{3,32}$");
+ private static final Pattern UUID_TEXT = Pattern.compile(
+ "(?i)[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
+ private static final JsonMapper JSON = JsonMapper.builder()
+ .enable(StreamReadFeature.STRICT_DUPLICATE_DETECTION)
+ .build();
+
+ private final HttpServer http;
+ private final DatabaseStore store = new DatabaseStore();
+ private final S3Store objects = new S3Store();
+ private final Broker broker = new Broker();
+ private final SecureRandom random = new SecureRandom();
+ private final long sessionTtlSeconds = Long.parseLong(System.getenv().getOrDefault("SESSION_TTL_SECONDS", "3600"));
+ private final AtomicBoolean accepting = new AtomicBoolean(true);
+ private final AtomicBoolean stopped = new AtomicBoolean(false);
+ private final Consumer info;
+ private final Consumer error;
+ private final int port;
+ private final String instanceId = System.getenv().getOrDefault("INSTANCE_ID", UUID.randomUUID().toString());
+
+ private MessengerServer(String host, int port, Consumer info, Consumer error) throws IOException {
+ this.info = info;
+ this.error = error;
+ this.http = HttpServer.create(new InetSocketAddress(host, port), 128);
+ this.port = http.getAddress().getPort();
+ ExecutorService pool = Executors.newFixedThreadPool(64, runnable -> {
+ Thread thread = new Thread(runnable, "http");
+ thread.setDaemon(true);
+ return thread;
+ });
+ http.setExecutor(pool);
+ http.createContext("/", this::handle);
+ http.start();
+ }
+
+ private static String routeTemplate(String path) {
+ if (path.equals("/health/live")) return "/health/live";
+ if (path.equals("/health/ready")) return "/health/ready";
+ if (path.equals("/metrics")) return "/metrics";
+ if (path.contains("/attachments")) return "/api/v1/attachments";
+ if (path.contains("/messages")) return "/api/v1/chats/{chat_id}/messages";
+ if (path.startsWith("/api/v1/chats")) return "/api/v1/chats";
+ if (path.startsWith("/api/v1/users")) return "/api/v1/users";
+ return "/other";
+ }
+
+ static MessengerServer start(String host, int port, Consumer info, Consumer error) throws IOException {
+ return new MessengerServer(host, port, info, error);
+ }
+
+ int port() {
+ return port;
+ }
+
+ void refuseNewRequests() {
+ accepting.set(false);
+ }
+
+ void stop() {
+ accepting.set(false);
+ if (stopped.compareAndSet(false, true)) {
+ http.stop(2);
+ }
+ }
+
+ private void handle(HttpExchange exchange) {
+ long started = System.nanoTime();
+ String requestId = UUID.randomUUID().toString();
+ String method = exchange.getRequestMethod();
+ String path = exchange.getRequestURI().getPath() == null ? "" : exchange.getRequestURI().getPath();
+ int status = 500;
+ try {
+ status = dispatch(exchange, requestId, method, path);
+ } catch (ApiException exception) {
+ status = writeError(exchange, requestId, exception);
+ } catch (Exception exception) {
+ error.accept("event=unexpected type=" + exception.getClass().getName() + " message=" + exception.getMessage());
+ status = writeError(exchange, requestId, ApiException.internalError());
+ } finally {
+ long durationMs = (System.nanoTime() - started) / 1_000_000L;
+ info.accept("request_id=" + requestId
+ + " service=messenger-api"
+ + " method=" + method
+ + " path=" + path
+ + " status=" + status
+ + " duration_ms=" + durationMs);
+ Metrics.request(method, routeTemplate(path), status, System.nanoTime() - started);
+ exchange.close();
+ }
+ }
+
+ private int dispatch(HttpExchange exchange, String requestId, String method, String path) throws IOException {
+ if ("GET".equals(method) && "/metrics".equals(path)) {
+ byte[] body = Metrics.prometheus().getBytes(StandardCharsets.UTF_8);
+ exchange.getResponseHeaders().set("Content-Type", "text/plain; version=0.0.4");
+ exchange.getResponseHeaders().set("X-Request-Id", requestId);
+ exchange.getResponseHeaders().set("X-Instance-Id", instanceId);
+ exchange.sendResponseHeaders(200, body.length);
+ try (OutputStream output = exchange.getResponseBody()) { output.write(body); }
+ return 200;
+ }
+ if (!accepting.get()) {
+ if ("GET".equals(method) && "/health/live".equals(path)) {
+ return writeJson(exchange, requestId, 200, health());
+ }
+ throw ApiException.unavailable();
+ }
+ if ("GET".equals(method) && "/health/live".equals(path)) {
+ rejectQuery(exchange);
+ return writeJson(exchange, requestId, 200, health());
+ }
+ if ("GET".equals(method) && "/health/ready".equals(path)) {
+ rejectQuery(exchange);
+ return writeJson(exchange, requestId, 200, health());
+ }
+ if ("POST".equals(method) && "/api/v1/users".equals(path)) {
+ rejectQuery(exchange);
+ JsonNode body = readObject(readBody(exchange));
+ allowOnly(body, "username", "display_name", "password");
+ String username = requiredText(body, "username");
+ if (!USERNAME.matcher(username).matches()) {
+ throw ApiException.invalid("Недопустимый username");
+ }
+ String displayName = requiredText(body, "display_name");
+ requireCodePoints(displayName, 1, 100, "display_name");
+ String password = requiredText(body, "password");
+ requireCodePoints(password, 12, 128, "password");
+ User user = store.createUser(username, displayName, BCrypt.hashpw(password, BCrypt.gensalt(12)));
+ return writeJson(exchange, requestId, 201, userJson(user));
+ }
+
+ if ("POST".equals(method) && "/api/v1/auth/sessions".equals(path)) {
+ rejectQuery(exchange);
+ String[] credentials = basicCredentials(exchange);
+ DatabaseStore.Credential credential = store.credential(credentials[0]).orElseThrow(ApiException::unauthorized);
+ if (!BCrypt.checkpw(credentials[1], credential.passwordHash())) throw ApiException.unauthorized();
+ Instant expires = Instant.now().plusSeconds(sessionTtlSeconds);
+ byte[] tokenBytes = new byte[32];
+ random.nextBytes(tokenBytes);
+ String token = Base64.getUrlEncoder().withoutPadding().encodeToString(tokenBytes);
+ store.createSession(credential.user().id(), token, expires);
+ ObjectNode result = JSON.createObjectNode().put("token", token).put("token_type", "Bearer")
+ .put("expires_at", DateTimeFormatter.ISO_INSTANT.format(expires));
+ result.set("user", userJson(credential.user()));
+ return writeJson(exchange, requestId, 201, result, null);
+ }
+ if ("DELETE".equals(method) && "/api/v1/auth/sessions/current".equals(path)) {
+ String token = bearerToken(exchange);
+ if (store.session(token).isEmpty()) throw ApiException.unauthorized();
+ store.revoke(token);
+ exchange.getResponseHeaders().set("X-Request-Id", requestId);
+ exchange.getResponseHeaders().set("X-Instance-Id", instanceId);
+ exchange.sendResponseHeaders(204, -1);
+ return 204;
+ }
+ if ("GET".equals(method) && "/api/v1/users/me".equals(path)) {
+ rejectQuery(exchange);
+ User user = requireUser(exchange);
+ return writeJson(exchange, requestId, 200, userJson(user));
+ }
+ if ("POST".equals(method) && path.matches("/api/v1/chats/" + UUID_TEXT.pattern() + "/attachments")) {
+ rejectQueryMethod(exchange);
+ return uploadAttachment(exchange, requestId, parsePathUuid(path.substring("/api/v1/chats/".length(),
+ path.length() - "/attachments".length())));
+ }
+ AttachmentRoute attachmentRoute = matchAttachment(path);
+ if (attachmentRoute != null) {
+ User user = requireUser(exchange);
+ UUID attachmentId = parsePathUuid(attachmentRoute.idText);
+ Attachment attachment = store.attachment(user.id(), attachmentId).orElseThrow(ApiException::notFound);
+ if ("GET".equals(method) && attachmentRoute.content) {
+ if (!"ready".equals(attachment.status())) throw ApiException.conflict();
+ String variantName = queryValue(exchange, "variant");
+ Attachment.Variant variant = variantName == null || variantName.equals("original")
+ ? null : attachment.variants().stream().filter(v -> v.name().equals(variantName)).findFirst().orElseThrow(ApiException::notFound);
+ String key = variant == null ? "original/" + attachment.id() : variant.objectKey();
+ long size = variant == null ? attachment.sizeBytes() : variant.sizeBytes();
+ exchange.getResponseHeaders().set("Content-Type", variant == null ? attachment.contentType() : variant.contentType());
+ exchange.getResponseHeaders().set("X-Request-Id", requestId);
+ exchange.getResponseHeaders().set("X-Instance-Id", instanceId);
+ try (InputStream input = objects.get(key);
+ OutputStream output = exchange.getResponseBody()) {
+ exchange.sendResponseHeaders(200, size);
+ input.transferTo(output);
+ }
+ return 200;
+ }
+ if ("GET".equals(method) && !attachmentRoute.content) {
+ return writeJson(exchange, requestId, 200, attachmentJson(attachment));
+ }
+ }
+ if ("POST".equals(method) && "/api/v1/chats".equals(path)) {
+ rejectQuery(exchange);
+ User user = requireUser(exchange);
+ JsonNode body = readObject(readBody(exchange));
+ allowOnly(body, "title", "member_ids");
+ String title = requiredText(body, "title");
+ requireCodePoints(title, 1, 100, "title");
+ List members = memberIds(body);
+ Chat chat = store.createChat(user.id(), title, members);
+ return writeJson(exchange, requestId, 201, chatJson(chat));
+ }
+ if ("GET".equals(method) && "/api/v1/chats".equals(path)) {
+ User user = requireUser(exchange);
+ Query query = pagingQuery(exchange);
+ List chats = new ArrayList<>(store.chatsOf(user.id()));
+ chats.sort(Comparator.comparing(Chat::createdAt).thenComparing(Chat::id));
+ return writePage(exchange, requestId, chats, query, "c", user.id(), Chat::createdAt, Chat::id, MessengerServer::chatJson);
+ }
+
+ ChatRoute chatRoute = matchChat(path);
+ if (chatRoute != null && "GET".equals(method) && chatRoute.messages) {
+ User user = requireUser(exchange);
+ UUID chatId = parsePathUuid(chatRoute.idText);
+ Query query = pagingQuery(exchange);
+ List messages = new ArrayList<>(store.messagesOf(user.id(), chatId));
+ messages.sort(Comparator.comparing(Message::createdAt).thenComparing(Message::id));
+ return writePage(exchange, requestId, messages, query, "m", chatId, Message::createdAt, Message::id, this::messageJson);
+ }
+ if (chatRoute != null && "POST".equals(method) && chatRoute.messages) {
+ rejectQuery(exchange);
+ User user = requireUser(exchange);
+ UUID chatId = parsePathUuid(chatRoute.idText);
+ JsonNode body = readObject(readBody(exchange));
+ allowOnly(body, "text", "attachment_ids");
+ String key = exchange.getRequestHeaders().getFirst("Idempotency-Key");
+ if (key == null || !key.matches("[A-Za-z0-9._:-]{1,128}")) throw ApiException.invalid("Некорректный Idempotency-Key");
+ List requestedAttachments = attachmentIds(body);
+ JsonNode textNode = body.get("text");
+ if (textNode == null || !textNode.isTextual()) throw ApiException.invalid("Недопустимый text");
+ String text = textNode.textValue();
+ if (text.isEmpty() && requestedAttachments.isEmpty()) throw ApiException.invalid("Сообщение не может быть пустым");
+ if (!text.isEmpty()) requireCodePoints(text, 1, 4000, "text");
+ String hash = sha256(text + "|" + requestedAttachments);
+ Message message = store.idempotentMessage(user.id(), chatId, key, hash)
+ .orElseGet(() -> store.addIdempotentMessage(user.id(), chatId, key, hash, text));
+ if (store.attachmentIds(message.id()).isEmpty()) {
+ store.attachToMessage(user.id(), message.id(), chatId, requestedAttachments);
+ }
+ return writeJson(exchange, requestId, 201, messageJson(message));
+ }
+ if (chatRoute != null && "GET".equals(method)) {
+ rejectQuery(exchange);
+ User user = requireUser(exchange);
+ UUID chatId = parsePathUuid(chatRoute.idText);
+ Chat chat = store.visibleChat(user.id(), chatId).orElseThrow(ApiException::notFound);
+ return writeJson(exchange, requestId, 200, chatJson(chat));
+ }
+ throw ApiException.notFound();
+ }
+
+ private int uploadAttachment(HttpExchange exchange, String requestId, UUID chatId) throws IOException {
+ User user = requireUser(exchange);
+ String filename = queryValue(exchange, "filename");
+ if (filename == null || filename.isBlank() || filename.length() > 255 || filename.contains("/")
+ || filename.contains("\\") || filename.contains("..")) throw ApiException.invalid("Недопустимое имя файла");
+ String type = exchange.getRequestHeaders().getFirst("Content-Type");
+ if (type == null) throw ApiException.invalid("Отсутствует Content-Type");
+ type = type.split(";", 2)[0].trim().toLowerCase(Locale.ROOT);
+ if (!type.equals("image/png") && !type.equals("image/jpeg")) throw ApiException.invalid("Поддерживаются только PNG и JPEG");
+ byte[] bytes = readLimited(exchange.getRequestBody(), MAX_ATTACHMENT);
+ if (!validImage(bytes, type)) throw ApiException.invalid("Поврежденное или неподходящее изображение");
+ UUID attachmentId = UUID.randomUUID();
+ UUID jobId = UUID.randomUUID();
+ String key = "staging/" + attachmentId;
+ String sha = sha256(bytes);
+ objects.put(key, bytes, type);
+ Attachment attachment = store.createAttachment(attachmentId, user.id(), chatId, filename, type, bytes.length, sha, key, jobId);
+ try {
+ broker.publish(jobId, attachment.id());
+ } catch (RuntimeException failure) {
+ error.accept("event=job_publish_deferred attachment_id=" + attachment.id());
+ }
+ return writeJson(exchange, requestId, 202, attachmentJson(attachment));
+ }
+
+ private static byte[] readLimited(InputStream input, int limit) throws IOException {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ byte[] buffer = new byte[8192]; int total = 0, read;
+ while ((read = input.read(buffer)) >= 0) { total += read; if (total > limit) throw ApiException.tooLarge(); output.write(buffer,0,read); }
+ return output.toByteArray();
+ }
+
+ private static boolean validImage(byte[] bytes, String type) {
+ if (type.equals("image/png")) return bytes.length > 24 && bytes[0]==(byte)137 && bytes[1]==80 && bytes[2]==78 && bytes[3]==71;
+ return bytes.length > 4 && bytes[0]==(byte)0xff && bytes[1]==(byte)0xd8 && bytes[bytes.length-2]==(byte)0xff && bytes[bytes.length-1]==(byte)0xd9;
+ }
+
+ private static String queryValue(HttpExchange e, String name) {
+ String raw=e.getRequestURI().getRawQuery(); if(raw==null)return null;
+ for(String p:raw.split("&")) { int i=p.indexOf('='); if(i>=0 && decode(p.substring(0,i)).equals(name)) return decode(p.substring(i+1)); }
+ return null;
+ }
+
+ private static AttachmentRoute matchAttachment(String path) {
+ String p="/api/v1/attachments/"; if(!path.startsWith(p)) return null;
+ String rest=path.substring(p.length()); if(rest.endsWith("/content")) return new AttachmentRoute(rest.substring(0,rest.length()-8),true);
+ return rest.isEmpty()?null:new AttachmentRoute(rest,false);
+ }
+
+ private static void rejectQueryMethod(HttpExchange exchange) {
+ String raw = exchange.getRequestURI().getRawQuery();
+ if (raw == null || !raw.startsWith("filename=")) throw ApiException.invalid("Требуется filename");
+ }
+
+ private static List attachmentIds(JsonNode body) {
+ List ids=new ArrayList<>(); JsonNode node=body.get("attachment_ids");
+ if(node==null) return ids; if(!node.isArray() || node.size()>20) throw ApiException.invalid("Некорректные attachment_ids");
+ for(JsonNode n:node) { if(!n.isTextual() || !UUID_TEXT.matcher(n.textValue()).matches()) throw ApiException.invalid("Некорректный attachment_id"); ids.add(UUID.fromString(n.textValue())); }
+ return ids;
+ }
+
+ private User requireUser(HttpExchange exchange) {
+ return store.session(bearerToken(exchange)).map(Session::user).orElseThrow(ApiException::unauthorized);
+ }
+
+ private String bearerToken(HttpExchange exchange) {
+ String header = exchange.getRequestHeaders().getFirst("Authorization");
+ if (header == null || !header.regionMatches(true, 0, "Bearer ", 0, 7) || header.length() <= 7) {
+ throw ApiException.unauthorized();
+ }
+ return header.substring(7).trim();
+ }
+
+ private static String[] basicCredentials(HttpExchange exchange) {
+ String header = exchange.getRequestHeaders().getFirst("Authorization");
+ if (header == null || !header.regionMatches(true, 0, "Basic ", 0, 6)) throw ApiException.unauthorized();
+ try {
+ String decoded = new String(Base64.getDecoder().decode(header.substring(6).trim()), StandardCharsets.UTF_8);
+ int colon = decoded.indexOf(':');
+ if (colon < 1) throw new IllegalArgumentException();
+ return new String[]{decoded.substring(0, colon), decoded.substring(colon + 1)};
+ } catch (IllegalArgumentException e) { throw ApiException.unauthorized(); }
+ }
+
+ private static UUID parsePathUuid(String raw) {
+ if (!UUID_TEXT.matcher(raw).matches()) {
+ throw ApiException.invalid("Некорректный идентификатор");
+ }
+ return UUID.fromString(raw);
+ }
+
+ private static ChatRoute matchChat(String path) {
+ String prefix = "/api/v1/chats/";
+ if (!path.startsWith(prefix)) {
+ return null;
+ }
+ String rest = path.substring(prefix.length());
+ if (rest.isEmpty() || rest.indexOf('/') == 0) {
+ return null;
+ }
+ int slash = rest.indexOf('/');
+ if (slash < 0) {
+ return new ChatRoute(rest, false);
+ }
+ if (rest.substring(slash).equals("/messages")) {
+ return new ChatRoute(rest.substring(0, slash), true);
+ }
+ return null;
+ }
+
+ private byte[] readBody(HttpExchange exchange) throws IOException {
+ String contentType = exchange.getRequestHeaders().getFirst("Content-Type");
+ if (contentType == null) {
+ throw ApiException.invalid("Ожидался Content-Type application/json");
+ }
+ String mime = contentType.split(";", 2)[0].trim().toLowerCase(Locale.ROOT);
+ if (!mime.equals("application/json")) {
+ throw ApiException.invalid("Ожидался Content-Type application/json");
+ }
+ return readLimited(exchange.getRequestBody());
+ }
+
+ private static byte[] readLimited(InputStream input) throws IOException {
+ ByteArrayOutputStream output = new ByteArrayOutputStream();
+ byte[] buffer = new byte[4096];
+ int total = 0;
+ int read;
+ while ((read = input.read(buffer)) >= 0) {
+ total += read;
+ if (total > MAX_BODY) {
+ throw ApiException.tooLarge();
+ }
+ output.write(buffer, 0, read);
+ }
+ return output.toByteArray();
+ }
+
+ private static JsonNode readObject(byte[] bytes) {
+ if (bytes.length == 0) {
+ throw ApiException.invalid("Пустое тело запроса");
+ }
+ try (JsonParser parser = JSON.createParser(bytes)) {
+ JsonNode node = JSON.readTree(parser);
+ if (parser.nextToken() != null) {
+ throw ApiException.invalid("Некорректный JSON");
+ }
+ if (node == null || !node.isObject()) {
+ throw ApiException.invalid("Ожидался JSON-объект");
+ }
+ return node;
+ } catch (ApiException exception) {
+ throw exception;
+ } catch (Exception exception) {
+ throw ApiException.invalid("Некорректный JSON");
+ }
+ }
+
+ private static void allowOnly(JsonNode object, String... fields) {
+ Set allowed = Set.of(fields);
+ object.fieldNames().forEachRemaining(name -> {
+ if (!allowed.contains(name)) {
+ throw ApiException.invalid("В запросе есть лишние поля");
+ }
+ });
+ }
+
+ private static String requiredText(JsonNode object, String field) {
+ if (!object.has(field) || object.get(field).isNull()) {
+ throw ApiException.invalid("Отсутствует поле " + field);
+ }
+ JsonNode node = object.get(field);
+ if (!node.isTextual()) {
+ throw ApiException.invalid("Поле " + field + " должно быть строкой");
+ }
+ return node.textValue();
+ }
+
+ private static void requireCodePoints(String value, int min, int max, String field) {
+ int count = value.codePointCount(0, value.length());
+ if (count < min || count > max || isBlank(value)) {
+ throw ApiException.invalid("Недопустимое значение " + field);
+ }
+ }
+
+ private static boolean isBlank(String value) {
+ return value.codePoints().allMatch(MessengerServer::isUnicodeWhiteSpace);
+ }
+
+ // Unicode White_Space. Character.isWhitespace пропускает U+0085 (NEL).
+ private static boolean isUnicodeWhiteSpace(int codePoint) {
+ return switch (codePoint) {
+ case 0x0009, 0x000A, 0x000B, 0x000C, 0x000D,
+ 0x0020, 0x0085, 0x00A0, 0x1680,
+ 0x2028, 0x2029, 0x202F, 0x205F, 0x3000 -> true;
+ default -> codePoint >= 0x2000 && codePoint <= 0x200A;
+ };
+ }
+
+ private static List memberIds(JsonNode object) {
+ if (!object.has("member_ids") || !object.get("member_ids").isArray()) {
+ throw ApiException.invalid("Поле member_ids должно быть массивом");
+ }
+ ArrayNode array = (ArrayNode) object.get("member_ids");
+ if (array.size() < 1 || array.size() > 100) {
+ throw ApiException.invalid("Недопустимое число участников");
+ }
+ List ids = new ArrayList<>();
+ for (JsonNode item : array) {
+ if (!item.isTextual() || !UUID_TEXT.matcher(item.textValue()).matches()) {
+ throw ApiException.invalid("Некорректный идентификатор участника");
+ }
+ UUID id = UUID.fromString(item.textValue());
+ if (ids.contains(id)) {
+ throw ApiException.invalid("Повторяющийся участник");
+ }
+ ids.add(id);
+ }
+ return ids;
+ }
+
+ private static void rejectQuery(HttpExchange exchange) {
+ String raw = exchange.getRequestURI().getRawQuery();
+ if (raw != null && !raw.isEmpty()) {
+ throw ApiException.invalid("Неподдерживаемые параметры запроса");
+ }
+ }
+
+ private static Query pagingQuery(HttpExchange exchange) {
+ String raw = exchange.getRequestURI().getRawQuery();
+ if (raw == null || raw.isEmpty()) {
+ return new Query(null, null);
+ }
+ Map> params = new LinkedHashMap<>();
+ for (String part : raw.split("&", -1)) {
+ if (part.isEmpty()) {
+ continue;
+ }
+ int eq = part.indexOf('=');
+ String key = decode(eq < 0 ? part : part.substring(0, eq));
+ String value = decode(eq < 0 ? "" : part.substring(eq + 1));
+ params.computeIfAbsent(key, ignored -> new ArrayList<>()).add(value);
+ }
+ for (String key : params.keySet()) {
+ if (!key.equals("limit") && !key.equals("cursor")) {
+ throw ApiException.invalid("Неподдерживаемый параметр запроса");
+ }
+ }
+ Integer limit = null;
+ if (params.containsKey("limit")) {
+ List values = params.get("limit");
+ if (values.size() != 1 || !values.get(0).matches("[0-9]{1,3}")) {
+ throw ApiException.invalid("Некорректный limit");
+ }
+ int parsed = Integer.parseInt(values.get(0));
+ if (parsed < 1 || parsed > 100) {
+ throw ApiException.invalid("Некорректный limit");
+ }
+ limit = parsed;
+ }
+ String cursor = null;
+ if (params.containsKey("cursor")) {
+ List values = params.get("cursor");
+ if (values.size() != 1 || values.get(0).isEmpty()) {
+ throw ApiException.invalid("Некорректный cursor");
+ }
+ cursor = values.get(0);
+ }
+ return new Query(limit, cursor);
+ }
+
+ private static String decode(String value) {
+ try {
+ return URLDecoder.decode(value, StandardCharsets.UTF_8);
+ } catch (IllegalArgumentException exception) {
+ throw ApiException.invalid("Некорректная строка запроса");
+ }
+ }
+
+ private int writePage(
+ HttpExchange exchange,
+ String requestId,
+ List sorted,
+ Query query,
+ String kind,
+ UUID scope,
+ java.util.function.Function createdAt,
+ java.util.function.Function id,
+ java.util.function.Function json) throws IOException {
+ int limit = query.limit == null ? 50 : query.limit;
+ int start = 0;
+ if (query.cursor != null) {
+ Position position = decodeCursor(query.cursor, kind, scope);
+ while (start < sorted.size()) {
+ T item = sorted.get(start);
+ if (after(createdAt.apply(item), id.apply(item), position.createdAt, position.id)) {
+ break;
+ }
+ start++;
+ }
+ }
+ int end = Math.min(start + limit, sorted.size());
+ ObjectNode page = JSON.createObjectNode();
+ ArrayNode items = page.putArray("items");
+ for (int i = start; i < end; i++) {
+ items.add(json.apply(sorted.get(i)));
+ }
+ if (end < sorted.size() && end > start) {
+ T last = sorted.get(end - 1);
+ page.put("next_cursor", encodeCursor(kind, scope, createdAt.apply(last), id.apply(last)));
+ } else {
+ page.putNull("next_cursor");
+ }
+ return writeJson(exchange, requestId, 200, page);
+ }
+
+ private static boolean after(Instant leftTime, UUID leftId, Instant rightTime, UUID rightId) {
+ int compared = leftTime.compareTo(rightTime);
+ if (compared != 0) {
+ return compared > 0;
+ }
+ return leftId.compareTo(rightId) > 0;
+ }
+
+ private static String encodeCursor(String kind, UUID scope, Instant createdAt, UUID id) {
+ String raw = kind + "|" + scope + "|" + DateTimeFormatter.ISO_INSTANT.format(createdAt) + "|" + id;
+ return Base64.getUrlEncoder().withoutPadding().encodeToString(raw.getBytes(StandardCharsets.UTF_8));
+ }
+
+ private static Position decodeCursor(String cursor, String kind, UUID scope) {
+ byte[] decoded;
+ try {
+ decoded = Base64.getUrlDecoder().decode(pad(cursor));
+ } catch (IllegalArgumentException exception) {
+ throw ApiException.invalid("Некорректный cursor");
+ }
+ String raw = new String(decoded, StandardCharsets.UTF_8);
+ String[] parts = raw.split("\\|", -1);
+ if (parts.length != 4) {
+ throw ApiException.invalid("Некорректный cursor");
+ }
+ if (!parts[0].equals(kind)) {
+ throw ApiException.invalid("Cursor относится к другому ресурсу");
+ }
+ UUID cursorScope;
+ Instant createdAt;
+ UUID id;
+ try {
+ cursorScope = UUID.fromString(parts[1]);
+ createdAt = Instant.parse(parts[2]);
+ id = UUID.fromString(parts[3]);
+ } catch (IllegalArgumentException exception) {
+ throw ApiException.invalid("Некорректный cursor");
+ }
+ if (!cursorScope.equals(scope)) {
+ throw ApiException.invalid("Cursor относится к другому ресурсу");
+ }
+ return new Position(createdAt, id);
+ }
+
+ private static String pad(String value) {
+ int mod = value.length() % 4;
+ if (mod == 0) {
+ return value;
+ }
+ return value + "====".substring(mod);
+ }
+
+ private static ObjectNode health() {
+ return JSON.createObjectNode().put("status", "ok");
+ }
+
+ private static ObjectNode userJson(User user) {
+ ObjectNode node = JSON.createObjectNode();
+ node.put("id", user.id().toString());
+ node.put("username", user.username());
+ node.put("display_name", user.displayName());
+ node.put("created_at", DateTimeFormatter.ISO_INSTANT.format(user.createdAt()));
+ return node;
+ }
+
+ private static ObjectNode chatJson(Chat chat) {
+ ObjectNode node = JSON.createObjectNode();
+ node.put("id", chat.id().toString());
+ node.put("title", chat.title());
+ ArrayNode members = node.putArray("member_ids");
+ for (UUID memberId : chat.memberIds()) {
+ members.add(memberId.toString());
+ }
+ node.put("created_at", DateTimeFormatter.ISO_INSTANT.format(chat.createdAt()));
+ return node;
+ }
+
+ private ObjectNode messageJson(Message message) {
+ ObjectNode node = JSON.createObjectNode();
+ node.put("id", message.id().toString());
+ node.put("chat_id", message.chatId().toString());
+ node.put("sender_id", message.senderId().toString());
+ node.put("text", message.text());
+ node.put("created_at", DateTimeFormatter.ISO_INSTANT.format(message.createdAt()));
+ ArrayNode attachments = node.putArray("attachment_ids");
+ for (UUID id : store.attachmentIds(message.id())) attachments.add(id.toString());
+ return node;
+ }
+
+ private static ObjectNode attachmentJson(Attachment attachment) {
+ ObjectNode node = JSON.createObjectNode();
+ node.put("id", attachment.id().toString()).put("chat_id", attachment.chatId().toString())
+ .put("owner_id", attachment.ownerId().toString())
+ .put("filename", attachment.filename()).put("content_type", attachment.contentType())
+ .put("size_bytes", attachment.sizeBytes()).put("sha256", attachment.sha256())
+ .put("status", attachment.status()).put("created_at", DateTimeFormatter.ISO_INSTANT.format(attachment.createdAt()))
+ .put("updated_at", DateTimeFormatter.ISO_INSTANT.format(attachment.updatedAt()));
+ if (attachment.errorCode() == null) node.putNull("error_code"); else node.put("error_code", attachment.errorCode());
+ ArrayNode variants = node.putArray("variants");
+ for (Attachment.Variant variant : attachment.variants()) {
+ variants.addObject().put("name", variant.name()).put("content_type", variant.contentType())
+ .put("size_bytes", variant.sizeBytes()).put("sha256", variant.sha256())
+ .put("width", variant.width()).put("height", variant.height());
+ }
+ return node;
+ }
+
+ private int writeJson(HttpExchange exchange, String requestId, int status, JsonNode body) throws IOException {
+ return writeJson(exchange, requestId, status, body, null);
+ }
+
+ private int writeJson(HttpExchange exchange, String requestId, int status, JsonNode body, String challenge) throws IOException {
+ byte[] bytes = JSON.writeValueAsBytes(body);
+ return writeBytes(exchange, requestId, status, bytes, challenge);
+ }
+
+ private int writeError(HttpExchange exchange, String requestId, ApiException exception) {
+ ObjectNode body = JSON.createObjectNode();
+ ObjectNode errorNode = body.putObject("error");
+ errorNode.put("code", exception.code);
+ errorNode.put("message", exception.clientMessage);
+ errorNode.put("request_id", requestId);
+ try {
+ String challenge = exception.status == 401
+ ? (exchange.getRequestURI().getPath().equals("/api/v1/auth/sessions")
+ ? "Basic realm=\"messenger\", charset=\"UTF-8\"" : "Bearer realm=\"messenger\"")
+ : null;
+ return writeJson(exchange, requestId, exception.status, body, challenge);
+ } catch (IOException ioException) {
+ error.accept("event=write_failed type=" + ioException.getClass().getName());
+ return exception.status;
+ }
+ }
+
+ private int writeBytes(HttpExchange exchange, String requestId, int status, byte[] body, String challenge) throws IOException {
+ exchange.getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
+ exchange.getResponseHeaders().set("X-Request-Id", requestId);
+ exchange.getResponseHeaders().set("X-Instance-Id", instanceId);
+ if (challenge != null) exchange.getResponseHeaders().set("WWW-Authenticate", challenge);
+ exchange.sendResponseHeaders(status, body.length);
+ try (OutputStream output = exchange.getResponseBody()) {
+ output.write(body);
+ }
+ return status;
+ }
+
+ private static String sha256(String value) {
+ try {
+ byte[] digest = MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8));
+ return Base64.getUrlEncoder().withoutPadding().encodeToString(digest);
+ } catch (Exception e) { throw ApiException.internalError(); }
+ }
+
+ static String sha256(byte[] value) {
+ try {
+ byte[] digest = MessageDigest.getInstance("SHA-256").digest(value);
+ StringBuilder result = new StringBuilder(64);
+ for (byte b : digest) result.append(String.format("%02x", b));
+ return result.toString();
+ } catch (Exception e) { throw ApiException.internalError(); }
+ }
+
+ private record Query(Integer limit, String cursor) {
+ }
+
+ private record Position(Instant createdAt, UUID id) {
+ }
+
+ private record ChatRoute(String idText, boolean messages) {
+ }
+ private record AttachmentRoute(String idText, boolean content) {}
+}
diff --git a/src/main/java/ru/mtusi/lab6/Metrics.java b/src/main/java/ru/mtusi/lab6/Metrics.java
new file mode 100644
index 0000000..9a64b09
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/Metrics.java
@@ -0,0 +1,39 @@
+package ru.mtusi.lab6;
+
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.LongAdder;
+
+final class Metrics {
+ private static final LongAdder requests = new LongAdder();
+ private static final LongAdder errors = new LongAdder();
+ private static final LongAdder durationMicros = new LongAdder();
+ private static final ConcurrentHashMap routes = new ConcurrentHashMap<>();
+
+ static void request(String method, String route, int status, long durationNanos) {
+ requests.increment();
+ durationMicros.add(durationNanos / 1_000);
+ routes.computeIfAbsent(method + " " + route + " " + (status / 100) + "xx", ignored -> new LongAdder()).increment();
+ if (status >= 500) errors.increment();
+ }
+
+ static String prometheus() {
+ long count = requests.sum();
+ long totalMicros = durationMicros.sum();
+ StringBuilder out = new StringBuilder();
+ out.append("# HELP messenger_http_requests_total HTTP requests by route and status class\n")
+ .append("# TYPE messenger_http_requests_total counter\n");
+ routes.forEach((key, value) -> {
+ String[] parts = key.split(" ", 3);
+ out.append("messenger_http_requests_total{method=\"").append(parts[0])
+ .append("\",route=\"").append(parts[1]).append("\",status_class=\"").append(parts[2])
+ .append("\"} ").append(value.sum()).append('\n');
+ });
+ out.append("# HELP messenger_http_errors_total HTTP 5xx responses\n# TYPE messenger_http_errors_total counter\n")
+ .append("messenger_http_errors_total ").append(errors.sum()).append('\n')
+ .append("# HELP messenger_http_duration_seconds_total Sum of request duration\n# TYPE messenger_http_duration_seconds_total counter\n")
+ .append("messenger_http_duration_seconds_total ").append(totalMicros / 1_000_000.0).append('\n')
+ .append("# HELP messenger_http_requests_seen_total Total requests\n# TYPE messenger_http_requests_seen_total counter\n")
+ .append("messenger_http_requests_seen_total ").append(count).append('\n');
+ return out.toString();
+ }
+}
diff --git a/src/main/java/ru/mtusi/lab6/Models.java b/src/main/java/ru/mtusi/lab6/Models.java
new file mode 100644
index 0000000..7200260
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/Models.java
@@ -0,0 +1,14 @@
+package ru.mtusi.lab6;
+
+import java.time.Instant;
+import java.util.List;
+import java.util.UUID;
+
+record User(UUID id, String username, String displayName, Instant createdAt) {
+}
+
+record Chat(UUID id, String title, List memberIds, Instant createdAt) {
+}
+
+record Message(UUID id, UUID chatId, UUID senderId, String text, Instant createdAt) {
+}
diff --git a/src/main/java/ru/mtusi/lab6/ReadyProbe.java b/src/main/java/ru/mtusi/lab6/ReadyProbe.java
new file mode 100644
index 0000000..08f48e2
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/ReadyProbe.java
@@ -0,0 +1,29 @@
+package ru.mtusi.lab6;
+
+import java.net.URI;
+import java.net.http.HttpClient;
+import java.net.http.HttpRequest;
+import java.net.http.HttpResponse;
+import java.time.Duration;
+
+public final class ReadyProbe {
+ private ReadyProbe() {
+ }
+
+ public static void main(String[] args) {
+ String port = System.getenv().getOrDefault("APP_PORT", "8080");
+ try {
+ HttpClient client = HttpClient.newBuilder().connectTimeout(Duration.ofSeconds(2)).build();
+ HttpRequest request = HttpRequest.newBuilder(URI.create("http://127.0.0.1:" + port + "/health/ready"))
+ .timeout(Duration.ofSeconds(2))
+ .GET()
+ .build();
+ HttpResponse response = client.send(request, HttpResponse.BodyHandlers.discarding());
+ if (response.statusCode() != 200) {
+ System.exit(1);
+ }
+ } catch (Exception e) {
+ System.exit(1);
+ }
+ }
+}
diff --git a/src/main/java/ru/mtusi/lab6/S3Store.java b/src/main/java/ru/mtusi/lab6/S3Store.java
new file mode 100644
index 0000000..7611de6
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/S3Store.java
@@ -0,0 +1,53 @@
+package ru.mtusi.lab6;
+
+import io.minio.*;
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+final class S3Store {
+ private final MinioClient client;
+ private final String bucket;
+ private final Path localRoot;
+ S3Store() {
+ String endpoint = env("S3_ENDPOINT", "http://minio:9000");
+ localRoot = endpoint.startsWith("file:") ? Path.of(endpoint.substring("file:".length())) : null;
+ client = localRoot == null ? MinioClient.builder().endpoint(endpoint)
+ .credentials(env("S3_ACCESS_KEY", "messenger"), env("S3_SECRET_KEY", "messenger-secret")).build() : null;
+ bucket = env("S3_BUCKET", "attachments");
+ if (localRoot != null) {
+ try { Files.createDirectories(localRoot.resolve(bucket)); } catch (IOException e) { throw new IllegalStateException(e); }
+ return;
+ }
+ try {
+ if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucket).build()))
+ client.makeBucket(MakeBucketArgs.builder().bucket(bucket).build());
+ } catch (Exception e) { throw new IllegalStateException("object storage initialization failed", e); }
+ }
+ void put(String key, byte[] bytes, String type) {
+ if (localRoot != null) {
+ try { Path target=localRoot.resolve(bucket).resolve(key); Files.createDirectories(target.getParent()); Files.write(target, bytes); return; }
+ catch (IOException e) { throw ApiException.unavailable(); }
+ }
+ try { client.putObject(PutObjectArgs.builder().bucket(bucket).object(key)
+ .stream(new ByteArrayInputStream(bytes), bytes.length, -1).contentType(type).build()); }
+ catch (Exception e) { throw ApiException.unavailable(); }
+ }
+ InputStream get(String key) {
+ if (localRoot != null) {
+ try { return Files.newInputStream(localRoot.resolve(bucket).resolve(key)); } catch (IOException e) { throw ApiException.notFound(); }
+ }
+ try { return client.getObject(GetObjectArgs.builder().bucket(bucket).object(key).build()); }
+ catch (Exception e) { throw ApiException.notFound(); }
+ }
+ void remove(String key) {
+ if (localRoot != null) {
+ try { Files.deleteIfExists(localRoot.resolve(bucket).resolve(key)); return; } catch (IOException e) { throw ApiException.unavailable(); }
+ }
+ try { client.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(key).build()); }
+ catch (Exception e) { throw ApiException.unavailable(); }
+ }
+ private static String env(String n, String f) { String v=System.getenv(n); return v==null||v.isBlank()?f:v.trim(); }
+}
diff --git a/src/main/java/ru/mtusi/lab6/Session.java b/src/main/java/ru/mtusi/lab6/Session.java
new file mode 100644
index 0000000..92027a7
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/Session.java
@@ -0,0 +1,5 @@
+package ru.mtusi.lab6;
+
+import java.time.Instant;
+
+record Session(String token, Instant expiresAt, User user) {}
diff --git a/src/main/java/ru/mtusi/lab6/WorkerMain.java b/src/main/java/ru/mtusi/lab6/WorkerMain.java
new file mode 100644
index 0000000..3ef34a5
--- /dev/null
+++ b/src/main/java/ru/mtusi/lab6/WorkerMain.java
@@ -0,0 +1,76 @@
+package ru.mtusi.lab6;
+
+import javax.imageio.ImageIO;
+import java.awt.*;
+import java.awt.image.BufferedImage;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.UUID;
+
+public final class WorkerMain {
+ private static final int MAX_ATTEMPTS = Integer.parseInt(System.getenv().getOrDefault("WORKER_MAX_ATTEMPTS", "5"));
+
+ public static void main(String[] args) throws Exception {
+ System.out.println("service=messenger-worker event=worker_started");
+ DatabaseStore store = new DatabaseStore();
+ S3Store objects = new S3Store();
+ Broker broker = new Broker();
+ broker.consume((channel, message) -> {
+ UUID attachmentId;
+ try {
+ String json = new String(message.getBody(), java.nio.charset.StandardCharsets.UTF_8);
+ attachmentId = UUID.fromString(json.replaceAll(".*\"attachment_id\":\"([^\"]+)\".*", "$1"));
+ Attachment attachment = store.attachmentById(attachmentId).orElseThrow();
+ if ("ready".equals(attachment.status())) {
+ channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
+ return;
+ }
+ int attempt = 1;
+ store.markProcessing(attachmentId, attempt);
+ byte[] original;
+ try (InputStream input = objects.get(attachment.objectKey())) {
+ original = input.readAllBytes();
+ }
+ BufferedImage source = ImageIO.read(new ByteArrayInputStream(original));
+ if (source == null) throw new IllegalArgumentException("invalid image");
+ int width = source.getWidth(), height = source.getHeight();
+ double scale = Math.min(1.0, 256.0 / Math.max(width, height));
+ int targetWidth = Math.max(1, (int) Math.floor(width * scale));
+ int targetHeight = Math.max(1, (int) Math.floor(height * scale));
+ BufferedImage thumbnail = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
+ Graphics2D graphics = thumbnail.createGraphics();
+ graphics.setColor(Color.WHITE);
+ graphics.fillRect(0, 0, targetWidth, targetHeight);
+ graphics.drawImage(source, 0, 0, targetWidth, targetHeight, null);
+ graphics.dispose();
+ ByteArrayOutputStream encoded = new ByteArrayOutputStream();
+ ImageIO.write(thumbnail, "jpeg", encoded);
+ byte[] thumbnailBytes = encoded.toByteArray();
+ String originalKey = "original/" + attachment.id();
+ String thumbnailKey = "variants/" + attachment.id() + "/thumbnail.jpg";
+ objects.put(originalKey, original, attachment.contentType());
+ objects.put(thumbnailKey, thumbnailBytes, "image/jpeg");
+ Attachment.Variant variant = new Attachment.Variant("thumbnail", "image/jpeg", thumbnailBytes.length,
+ MessengerServer.sha256(thumbnailBytes), targetWidth, targetHeight, thumbnailKey);
+ store.markReady(attachment.id(), variant);
+ System.out.println("service=messenger-worker event=job_processed outcome=success job_type=attachment.process.v1");
+ objects.remove(attachment.objectKey());
+ channel.basicAck(message.getEnvelope().getDeliveryTag(), false);
+ } catch (Exception failure) {
+ try {
+ UUID id = UUID.fromString(new String(message.getBody(), java.nio.charset.StandardCharsets.UTF_8)
+ .replaceAll(".*\"attachment_id\":\"([^\"]+)\".*", "$1"));
+ store.markFailed(id, 1, "processing_failed");
+ } catch (Exception ignored) {
+ // The broker message is rejected below and remains observable.
+ }
+ try {
+ channel.basicReject(message.getEnvelope().getDeliveryTag(), false);
+ } catch (java.io.IOException ackFailure) {
+ throw new IllegalStateException("broker acknowledgement failed", ackFailure);
+ }
+ }
+ });
+ }
+}
diff --git a/src/main/resources/db/migration/V1__initial.sql b/src/main/resources/db/migration/V1__initial.sql
new file mode 100644
index 0000000..ad554f3
--- /dev/null
+++ b/src/main/resources/db/migration/V1__initial.sql
@@ -0,0 +1,79 @@
+CREATE TABLE users (
+ id uuid PRIMARY KEY,
+ username varchar(32) NOT NULL UNIQUE,
+ display_name varchar(100) NOT NULL,
+ password_hash varchar(255) NOT NULL,
+ created_at timestamptz NOT NULL DEFAULT now()
+);
+CREATE TABLE chats (
+ id uuid PRIMARY KEY,
+ title varchar(100) NOT NULL,
+ created_at timestamptz NOT NULL DEFAULT now()
+);
+CREATE TABLE chat_members (
+ chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
+ user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
+ PRIMARY KEY (chat_id, user_id)
+);
+CREATE 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 DEFAULT now()
+);
+CREATE INDEX messages_chat_created_idx ON messages(chat_id, created_at, id);
+CREATE TABLE sessions (
+ token varchar(128) PRIMARY KEY,
+ user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
+ expires_at timestamptz NOT NULL
+);
+CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
+CREATE TABLE idempotency_keys (
+ user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE,
+ chat_id uuid NOT NULL REFERENCES chats(id) ON DELETE CASCADE,
+ key varchar(128) NOT NULL,
+ body_hash varchar(128) NOT NULL,
+ message_id uuid NOT NULL UNIQUE REFERENCES messages(id) ON DELETE CASCADE,
+ PRIMARY KEY (user_id, chat_id, key)
+);
+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,
+ size_bytes bigint NOT NULL,
+ sha256 varchar(64) NOT NULL,
+ object_key varchar(255) NOT NULL UNIQUE,
+ status varchar(16) NOT NULL DEFAULT 'queued',
+ error_code varchar(64),
+ created_at timestamptz NOT NULL DEFAULT now(),
+ updated_at timestamptz NOT NULL DEFAULT now()
+);
+CREATE TABLE attachment_variants (
+ attachment_id uuid NOT NULL REFERENCES attachments(id) ON DELETE CASCADE,
+ name varchar(32) NOT NULL,
+ content_type varchar(64) NOT NULL,
+ size_bytes bigint NOT NULL,
+ sha256 varchar(64) NOT NULL,
+ width integer NOT NULL,
+ height integer NOT NULL,
+ object_key varchar(255) NOT NULL UNIQUE,
+ PRIMARY KEY (attachment_id, name)
+);
+CREATE TABLE attachment_jobs (
+ job_id uuid PRIMARY KEY,
+ attachment_id uuid NOT NULL UNIQUE REFERENCES attachments(id) ON DELETE CASCADE,
+ attempt integer NOT NULL DEFAULT 0,
+ status varchar(16) NOT NULL DEFAULT 'queued',
+ available_at timestamptz NOT NULL DEFAULT now(),
+ error_code varchar(64),
+ created_at timestamptz NOT NULL DEFAULT now(),
+ updated_at timestamptz NOT NULL DEFAULT now()
+);
+CREATE TABLE message_attachments (
+ message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
+ attachment_id uuid NOT NULL REFERENCES attachments(id),
+ PRIMARY KEY (message_id, attachment_id)
+);