feat: attachments over S3 (grade 3)

This commit is contained in:
2026-11-20 03:55:39 +03:00
parent 083d0d80cc
commit 89e9e7b4d4
44 changed files with 1468 additions and 67 deletions
+5 -2
View File
@@ -8,11 +8,11 @@ TEST_DATABASE_URL ?= $(shell sed 's/@postgres:5432/@127.0.0.1:55432/' secrets/da
TEST_REDIS_URL ?= redis://127.0.0.1:56379/0
TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",)
.PHONY: help up down check validate test load test-persistence test-failover test-idempotency test-go
.PHONY: help up down check validate test load test-persistence test-failover test-idempotency test-attachments test-go
help:
@echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes"
@echo "up/down: start/stop the stand, volumes kept; test-idempotency: one key in parallel via LB; test-go: Go tests against the running stand"
@echo "up/down: start/stop the stand, volumes kept; test-idempotency: one key in parallel via LB; test-attachments: attachment contract checks; test-go: Go tests against the running stand"
up:
$(COMPOSE) up -d --build
@@ -43,6 +43,9 @@ test-failover:
test-idempotency:
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/idempotency-race.py
test-attachments:
BASE_URL="$(BASE_URL)" $(PYTHON) scripts/attachments-check.py
test-go:
@TEST_DATABASE_URL="$(TEST_DATABASE_URL)" TEST_REDIS_URL="$(TEST_REDIS_URL)" go test -count=1 -v ./...
+13 -2
View File
@@ -14,6 +14,7 @@ import (
"messenger/internal/handler"
"messenger/internal/repository/postgres"
"messenger/internal/repository/redis"
"messenger/internal/repository/s3"
"messenger/internal/router"
"messenger/internal/service"
)
@@ -60,9 +61,19 @@ func run(log *slog.Logger) error {
auth := service.NewAuthService(store, sessions, cfg.SessionTTL)
chats := service.NewChatService(store)
messages := service.NewMessageService(chats, store)
deps := []handler.Pinger{store.Ping, sessions.Ping}
h := handler.New(users, auth, chats, messages, deps, log)
objects, err := s3.New(cfg.S3Endpoint, cfg.S3Region, cfg.S3AccessKeyID, cfg.S3SecretAccessKey, cfg.S3Bucket)
if err != nil {
return err
}
if err := objects.EnsureBucket(context.Background()); err != nil {
log.Error("s3 bucket bootstrap failed", "err", err)
}
attachments := service.NewAttachmentService(chats, store, objects, cfg.MaxUploadBytes, cfg.MaxImagePixels)
deps := []handler.Pinger{store.Ping, sessions.Ping, objects.Ping}
h := handler.New(users, auth, chats, messages, attachments, deps, log)
srv := &http.Server{
Addr: cfg.Addr(),
Handler: router.New(h, instanceID),
+46 -3
View File
@@ -1,4 +1,4 @@
name: messenger-lab3
name: messenger-lab4
services:
postgres:
@@ -48,6 +48,33 @@ services:
memory: 128M
restart: unless-stopped
s3:
image: docker.io/rustfs/rustfs:1.0.6
environment:
RUSTFS_ACCESS_KEY_FILE: /run/secrets/s3_access_key_id
RUSTFS_SECRET_KEY_FILE: /run/secrets/s3_secret_access_key
RUSTFS_REGION: ${S3_REGION:-us-east-1}
RUSTFS_CONSOLE_ENABLE: "false"
RUSTFS_OBS_LOGGER_LEVEL: warn
secrets:
- s3_access_key_id
- s3_secret_access_key
volumes:
- s3data:/data
networks:
- backend
healthcheck:
test: ["CMD", "curl", "-f", "http://127.0.0.1:9000/health"]
interval: 2s
timeout: 3s
retries: 30
deploy:
resources:
limits:
cpus: "1.0"
memory: 512M
restart: unless-stopped
migrate:
image: docker.io/migrate/migrate:v4.20.1
entrypoint: ["/bin/sh", "-c"]
@@ -70,7 +97,7 @@ services:
api:
build: .
image: localhost/messenger-api:lab3
image: localhost/messenger-api:lab4
environment:
APP_HOST: 0.0.0.0
APP_PORT: "8080"
@@ -78,9 +105,18 @@ services:
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
DATABASE_URL_FILE: /run/secrets/database_url
CACHE_URL_FILE: /run/secrets/cache_url
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
S3_REGION: ${S3_REGION:-us-east-1}
S3_BUCKET: ${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
MAX_UPLOAD_BYTES: ${MAX_UPLOAD_BYTES:-10485760}
MAX_IMAGE_PIXELS: ${MAX_IMAGE_PIXELS:-40000000}
secrets:
- database_url
- cache_url
- s3_access_key_id
- s3_secret_access_key
networks:
- edge
- backend
@@ -96,12 +132,14 @@ services:
resources:
limits:
cpus: "1.0"
memory: 256M
memory: 512M
depends_on:
migrate:
condition: service_completed_successfully
redis:
condition: service_healthy
s3:
condition: service_healthy
restart: unless-stopped
lb:
@@ -130,7 +168,12 @@ secrets:
file: ./secrets/database_url
cache_url:
file: ./secrets/cache_url
s3_access_key_id:
file: ./secrets/s3_access_key_id
s3_secret_access_key:
file: ./secrets/s3_secret_access_key
volumes:
pgdata:
redisdata:
s3data:
+28
View File
@@ -0,0 +1,28 @@
2026-11-20T03:51:30+03:00
make test-attachments
BASE_URL="http://localhost:8080" python3 scripts/attachments-check.py
ok: upload 201 ready, sha256=99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75, size=111817
ok: jpeg upload/download byte-identical; jpeg declared as png -> 415
ok: png with 40,000,000 pixels accepted and downloaded byte-identical
ok: filename with slash -> 400
ok: filename with NUL -> 400
ok: empty filename -> 400
ok: declared jpeg with png body -> 415
ok: truncated png -> 415
ok: 10485761 bytes -> 413
ok: text/plain -> 415
ok: unknown variant -> 400
ok: variant=original -> 200
ok: unknown attachment id -> 404
ok: upload by outsider -> 404
ok: attachment from another chat -> 404
ok: unknown attachment id in message -> 404
ok: duplicate attachment_ids -> 400
ok: eleven attachment_ids -> 400
ok: empty text without attachments -> 400
ok: idempotent replay with same attachment set -> same message
ok: absent attachment_ids equals [] for idempotency
ok: same key with a different attachment set -> 409
ok: history exposes attachment_ids on every message
PASS: 23 attachment contract checks
+5
View File
@@ -0,0 +1,5 @@
2026-11-20T03:51:29+03:00
make check
python3 scripts/check_contract.py
PASS: offline skeleton structure, local refs and Python syntax; application NOT tested.
+7
View File
@@ -0,0 +1,7 @@
2026-11-20T03:53:34+03:00
users | chats | messages | attachments | links | db_size
-------+-------+----------+-------------+-------+---------
40 | 11 | 289 | 8 | 2 | 8542 kB
(1 row)
+19
View File
@@ -0,0 +1,19 @@
2026-11-20T03:53:33+03:00
Linux archlinux 7.3.5-arch1-1 #1 SMP PREEMPT_DYNAMIC Tue, 17 Nov 2026 22:28:33 +0000 x86_64 GNU/Linux
12
total used free shared buff/cache available
Mem: 15Gi 5.0Gi 1.3Gi 385Mi 9.6Gi 10Gi
Swap: 8.0Gi 58Mi 7.9Gi
podman version 6.2.0
podman version 6.2.0
podman-compose version 1.6.0
go version go1.27.4-X:nodwarf5 linux/amd64
images
docker.io/library/haproxy:3.4.7-alpine 49c6afb26f02
docker.io/library/postgres:18.6-alpine3.24 57b23a0326ee
docker.io/library/redis:8.10.3-alpine3.23 9722accf3e8e
docker.io/migrate/migrate:v4.20.1 719a6f5d0ade
docker.io/rustfs/rustfs:1.0.6 7cd1c573d76d
localhost/messenger-api:lab4 ff3adbffb20b
+7
View File
@@ -0,0 +1,7 @@
2026-11-20T03:52:00+03:00
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 86, "failed_attempts": 0, "first_recovery_seconds": 3.239}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
+7
View File
@@ -0,0 +1,7 @@
2026-11-20T03:52:32+03:00
make test-failover ACTION_SCRIPT=scripts/stop-one.sh
python3 tests/resilience.py --mode failover --action-script "scripts/stop-one.sh" --base-url "http://localhost:8080"
Executing explicit failover hook; hook output suppressed to avoid leaking secrets.
{"observed_instances_before": 2, "successful_writes": 87, "failed_attempts": 0, "first_recovery_seconds": 3.236}
PASS: failover preserved confirmed data and existing session. Hook scope needs operator evidence.
+11
View File
@@ -0,0 +1,11 @@
2026-11-20T03:51:42+03:00
make test-idempotency
BASE_URL="http://localhost:8080" python3 scripts/idempotency-race.py
parallel requests with one key: 20
successful responses (201): 20
failed requests: 0 []
distinct message ids in responses: 1
messages stored in history: 1
instances that served requests: ['939e096de71a', 'dbc0d13c9b45']
OK: exactly one message created
+23
View File
@@ -0,0 +1,23 @@
2026-11-20T03:53:27+03:00
API replicas and single entrypoint
messenger-lab4_api_1 Up About a minute (healthy)
messenger-lab4_api_2 Up 35 seconds (healthy)
messenger-lab4_lb_1 Up About a minute
messenger-lab4_postgres_1 Up About a minute (healthy)
messenger-lab4_redis_1 Up About a minute (healthy)
messenger-lab4_s3_1 Up About a minute (healthy)
published ports (only lb must publish 8080)
messenger-lab4_api_1 8080/tcp
messenger-lab4_api_2 8080/tcp
messenger-lab4_lb_1 0.0.0.0:8080->8080/tcp
messenger-lab4_postgres_1 127.0.0.1:55432->5432/tcp
messenger-lab4_redis_1 127.0.0.1:56379->6379/tcp
messenger-lab4_s3_1 9000-9001/tcp
X-Instance-Id distribution over 20 requests through LB
10 x-instance-id: 939e096de71a
10 x-instance-id: dbc0d13c9b45
+19
View File
@@ -0,0 +1,19 @@
2026-11-20T03:53:28+03:00
per-container limits, networks, secrets
messenger-lab4_api_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend messenger-lab4_edge
messenger-lab4_api_2 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend messenger-lab4_edge
messenger-lab4_lb_1 nano_cpus=500000000 memory=67108864 networks=messenger-lab4_edge
messenger-lab4_postgres_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend
messenger-lab4_redis_1 nano_cpus=500000000 memory=134217728 networks=messenger-lab4_backend
messenger-lab4_s3_1 nano_cpus=1000000000 memory=536870912 networks=messenger-lab4_backend
messenger-lab4_migrate_1 nano_cpus=500000000 memory=67108864 networks=messenger-lab4_backend
ls /run/secrets per container
messenger-lab4_api_1: cache_url database_url s3_access_key_id s3_secret_access_key
messenger-lab4_api_2: cache_url database_url s3_access_key_id s3_secret_access_key
messenger-lab4_lb_1:
messenger-lab4_postgres_1: postgres_password
messenger-lab4_redis_1:
messenger-lab4_s3_1: s3_access_key_id s3_secret_access_key
+20
View File
@@ -0,0 +1,20 @@
2026-11-20T03:53:34+03:00
migrate container logs and schema version
1/u init (12.217985ms)
2/u message_idempotency (17.93178ms)
3/u attachments (25.639306ms)
no change
no change
no change
no change
no change
no change
schema_migrations
version | dirty
---------+-------
3 | f
(1 row)
+32
View File
@@ -0,0 +1,32 @@
2026-11-20T03:51:42+03:00
make test-persistence ACTION_SCRIPT=scripts/restart.sh
python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080"
Executing explicit persistence hook; hook output suppressed to avoid leaking secrets.
PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence.
--- hook scope: containers and volumes before/after scripts/restart.sh ---
messenger-lab4_api_1 dbc0d13c9b45 Up About a minute (healthy)
messenger-lab4_api_2 939e096de71a Up 42 seconds (healthy)
messenger-lab4_lb_1 c2c6a2077627 Up About a minute
messenger-lab4_postgres_1 a0b192c17952 Up About a minute (healthy)
messenger-lab4_redis_1 f787abf8c4bf Up About a minute (healthy)
messenger-lab4_s3_1 49ff0b0c9d92 Up About a minute (healthy)
messenger-lab4_pgdata
messenger-lab4_redisdata
messenger-lab4_s3data
$ scripts/restart.sh
messenger-lab4_api_1 dbc0d13c9b45 Up 15 seconds (healthy)
messenger-lab4_api_2 939e096de71a Up 15 seconds (healthy)
messenger-lab4_lb_1 c2c6a2077627 Up 15 seconds
messenger-lab4_postgres_1 a0b192c17952 Up 16 seconds (healthy)
messenger-lab4_redis_1 f787abf8c4bf Up 16 seconds (healthy)
messenger-lab4_s3_1 49ff0b0c9d92 Up 16 seconds (healthy)
messenger-lab4_pgdata
messenger-lab4_redisdata
messenger-lab4_s3data
ready: 200
+5
View File
@@ -0,0 +1,5 @@
2026-11-20T03:51:29+03:00
make test
python3 tests/smoke.py --base-url "http://localhost:8080"
PASS: lab4 public API scenario; infrastructure and grading evidence remain separate.
+44
View File
@@ -0,0 +1,44 @@
2026-11-20T03:53:23+03:00
object bytes live in the RustFS volume, metadata in PostgreSQL
id | object_key | size_bytes | sha256
--------------------------------------+---------------------------------------------------------------------------------+------------+------------------------------------------------------------------
0df7c6fd-6ac6-488b-9803-73fee496586f | chats/70682cfc-bf33-4f15-8de6-e85972fc95e0/0df7c6fd-6ac6-488b-9803-73fee496586f | 111817 | 99ea7533c441f79cd630d15ce4d58d1ec4e06f6f0efb3b77a5c088305cd1bd75
(1 row)
S3 volume listing for that object
/data/messenger-attachments/chats/70682cfc-bf33-4f15-8de6-e85972fc95e0/0df7c6fd-6ac6-488b-9803-73fee496586f:
total 112
-rw-r--r-- 1 rustfs rustfs 112398 Nov 20 00:52 xl.meta
attachments table columns (no binary content)
column_name | data_type
--------------+--------------------------
id | uuid
chat_id | uuid
owner_id | uuid
filename | text
content_type | text
size_bytes | bigint
sha256 | text
object_key | text
status | text
error_code | text
created_at | timestamp with time zone
updated_at | timestamp with time zone
(12 rows)
anonymous S3 GET without credentials
Connecting to s3:9000 (10.89.5.10:9000)
HTTP/1.1 403 Forbidden
wget: server returned error: HTTP/1.1 403 Forbidden
admin console port (must be closed)
000
console-closed
+64
View File
@@ -0,0 +1,64 @@
2026-11-20T03:54:42+03:00
make test-go
? messenger/cmd/server [no test files]
? messenger/internal/config [no test files]
? messenger/internal/domain [no test files]
=== RUN TestSessionExpiry
--- PASS: TestSessionExpiry (3.09s)
=== RUN TestLogout
--- PASS: TestLogout (0.11s)
=== RUN TestWrongPassword
--- PASS: TestWrongPassword (0.10s)
=== RUN TestRedisUnavailable
redis: 2026/11/20 03:54:46 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:46 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:47 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:47 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:47 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
redis: 2026/11/20 03:54:48 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
--- PASS: TestRedisUnavailable (2.81s)
=== RUN TestDatabaseUnavailable
--- PASS: TestDatabaseUnavailable (0.17s)
=== RUN TestInvalidJSON
--- PASS: TestInvalidJSON (0.01s)
=== RUN TestForeignChat
--- PASS: TestForeignChat (0.18s)
=== RUN TestEmptyMessage
redis: 2026/11/20 03:54:48 pool.go:770: redis: connection pool: failed to dial after 5 attempts: dial tcp 127.0.0.1:1: connect: connection refused
--- PASS: TestEmptyMessage (0.08s)
=== RUN TestDuplicateUsername
--- PASS: TestDuplicateUsername (0.09s)
=== RUN TestPaginationMultiplePages
--- PASS: TestPaginationMultiplePages (0.09s)
=== RUN TestConcurrentMessages
--- PASS: TestConcurrentMessages (0.57s)
PASS
ok messenger/internal/handler 7.289s
=== RUN TestRegisterUsernameRace
--- PASS: TestRegisterUsernameRace (0.05s)
=== RUN TestCreateChatRollback
--- PASS: TestCreateChatRollback (0.01s)
PASS
ok messenger/internal/repository/postgres 0.072s
? messenger/internal/repository/redis [no test files]
? messenger/internal/repository/s3 [no test files]
? messenger/internal/router [no test files]
=== RUN TestCheckFilename
--- PASS: TestCheckFilename (0.00s)
=== RUN TestDetectImageType
--- PASS: TestDetectImageType (0.00s)
=== RUN TestCheckPixels
--- PASS: TestCheckPixels (0.00s)
=== RUN TestCheckIntegrity
--- PASS: TestCheckIntegrity (0.00s)
=== RUN TestCheckMessageText
--- PASS: TestCheckMessageText (0.00s)
=== RUN TestCheckAttachmentIDs
--- PASS: TestCheckAttachmentIDs (0.00s)
=== RUN TestMessageHash
--- PASS: TestMessageHash (0.00s)
=== RUN TestReadLimited
--- PASS: TestReadLimited (0.00s)
PASS
ok messenger/internal/service 0.004s
+15
View File
@@ -5,17 +5,32 @@ go 1.27.1
require (
github.com/go-chi/chi/v5 v5.3.2
github.com/jackc/pgx/v5 v5.11.0
github.com/minio/minio-go/v7 v7.4.1
github.com/redis/go-redis/v9 v9.23.0
golang.org/x/crypto v0.58.0
)
require (
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/klauspost/compress v1.19.2 // indirect
github.com/klauspost/cpuid/v2 v2.4.0 // indirect
github.com/klauspost/crc32 v1.3.0 // indirect
github.com/minio/crc64nvme v1.1.1 // indirect
github.com/minio/md5-simd v1.1.2 // indirect
github.com/philhofer/fwd v1.2.0 // indirect
github.com/rs/xid v1.6.0 // indirect
github.com/tinylib/msgp v1.6.4 // indirect
github.com/zeebo/xxh3 v1.1.0 // indirect
go.uber.org/atomic v1.12.0 // indirect
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/net v0.60.0 // indirect
golang.org/x/sync v0.24.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/text v0.43.0 // indirect
gopkg.in/ini.v1 v1.67.3 // indirect
)
+36
View File
@@ -5,8 +5,13 @@ github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/go-chi/chi/v5 v5.3.2 h1:5YQkICvTCSZ25hoRsyJazN0scjzKGiu4VAUc7H1o1nY=
github.com/go-chi/chi/v5 v5.3.2/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
@@ -15,16 +20,42 @@ github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg=
github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/klauspost/compress v1.19.2 h1:hMRETovs/pu/dVWN7zIT1PGG8t509MwT6bO7XSi26R8=
github.com/klauspost/compress v1.19.2/go.mod h1:cwPg85FWrGar70rWktvGQj8/hthj3wpl0PGDogxkrSQ=
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw=
github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU=
github.com/klauspost/crc32 v1.3.0 h1:sSmTt3gUt81RP655XGZPElI0PelVTZ6YwCRnPSupoFM=
github.com/klauspost/crc32 v1.3.0/go.mod h1:D7kQaZhnkX/Y0tstFGf8VUzv2UofNGqCjnC3zdHB0Hw=
github.com/minio/crc64nvme v1.1.1 h1:8dwx/Pz49suywbO+auHCBpCtlW1OfpcLN7wYgVR6wAI=
github.com/minio/crc64nvme v1.1.1/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
github.com/minio/minio-go/v7 v7.4.1 h1:PtU/zIOfQRWZ+QydXAZysc+xZYHdG+BBR4Z6TUvy73w=
github.com/minio/minio-go/v7 v7.4.1/go.mod h1:nQnQi7zTfXR4lREtYXZQWqo5aw0Rqv+ZqCGrTORygRk=
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s=
github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0=
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE=
github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg=
github.com/tinylib/msgp v1.6.4 h1:mOwYbyYDLPj35mkA2BjjYejgJk9BuHxDdvRnb6v2ZcQ=
github.com/tinylib/msgp v1.6.4/go.mod h1:RSp0LW9oSxFut3KzESt5Voq4GVWyS+PSulT77roAqEA=
github.com/zeebo/assert v1.3.0 h1:g7C04CbJuIDKNPFHmsk4hwZDO5O+kntRxzaUoNXj+IQ=
github.com/zeebo/assert v1.3.0/go.mod h1:Pq9JiuJQpG8JLJdtkwrJESF0Foym2/D9XMU5ciN/wJ0=
github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs=
github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s=
go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE=
@@ -33,6 +64,8 @@ go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw=
go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg=
golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54=
golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w=
golang.org/x/net v0.60.0 h1:79p50tfZlm0J9YfoDsSi639qSXNGVwEzOPLCxM2FsYU=
golang.org/x/net v0.60.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA=
golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA=
@@ -40,4 +73,7 @@ golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc=
golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/ini.v1 v1.67.3 h1:iM9Lhz5MRSGhHVGGwCuzG9KO8PoirCXj/m/qTmOJJQw=
gopkg.in/ini.v1 v1.67.3/go.mod h1:x/cyOwCgZqOkJoDIJ3c1KNHMo10+nLGAhh+kn3Zizss=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+61 -12
View File
@@ -10,12 +10,19 @@ import (
)
type Config struct {
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
Host string
Port string
Env string
SessionTTL time.Duration
DatabaseURL string
CacheURL string
S3Endpoint string
S3Region string
S3Bucket string
S3AccessKeyID string
S3SecretAccessKey string
MaxUploadBytes int64
MaxImagePixels int64
}
func Load() (Config, error) {
@@ -23,6 +30,14 @@ func Load() (Config, error) {
if err != nil || ttl <= 0 {
return Config{}, fmt.Errorf("SESSION_TTL_SECONDS must be a positive integer")
}
maxUploadBytes, err := positiveInt("MAX_UPLOAD_BYTES", "10485760")
if err != nil {
return Config{}, err
}
maxImagePixels, err := positiveInt("MAX_IMAGE_PIXELS", "40000000")
if err != nil {
return Config{}, err
}
databaseURL, err := readSecret("DATABASE_URL_FILE", "/run/secrets/database_url")
if err != nil {
return Config{}, err
@@ -31,18 +46,52 @@ func Load() (Config, error) {
if err != nil {
return Config{}, err
}
accessKeyID, err := readSecret("S3_ACCESS_KEY_ID_FILE", "/run/secrets/s3_access_key_id")
if err != nil {
return Config{}, err
}
secretAccessKey, err := readSecret("S3_SECRET_ACCESS_KEY_FILE", "/run/secrets/s3_secret_access_key")
if err != nil {
return Config{}, err
}
if accessKeyID == "" || secretAccessKey == "" {
return Config{}, fmt.Errorf("S3_ACCESS_KEY_ID_FILE and S3_SECRET_ACCESS_KEY_FILE must not be empty")
}
bucket := getenv("S3_BUCKET", "messenger-attachments")
if strings.TrimSpace(bucket) == "" {
return Config{}, fmt.Errorf("S3_BUCKET must not be empty")
}
endpoint := getenv("S3_ENDPOINT", "http://s3:9000")
if strings.TrimSpace(endpoint) == "" {
return Config{}, fmt.Errorf("S3_ENDPOINT must not be empty")
}
return Config{
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
Env: getenv("APP_ENV", "lab"),
SessionTTL: time.Duration(ttl) * time.Second,
DatabaseURL: databaseURL,
CacheURL: cacheURL,
S3Endpoint: endpoint,
S3Region: getenv("S3_REGION", "us-east-1"),
S3Bucket: bucket,
S3AccessKeyID: accessKeyID,
S3SecretAccessKey: secretAccessKey,
MaxUploadBytes: maxUploadBytes,
MaxImagePixels: maxImagePixels,
}, nil
}
func (c Config) Addr() string { return net.JoinHostPort(c.Host, c.Port) }
func positiveInt(key, def string) (int64, error) {
value, err := strconv.ParseInt(getenv(key, def), 10, 64)
if err != nil || value <= 0 {
return 0, fmt.Errorf("%s must be a positive integer", key)
}
return value, nil
}
func readSecret(key, def string) (string, error) {
path := getenv(key, def)
b, err := os.ReadFile(path)
+27 -5
View File
@@ -29,11 +29,33 @@ func (c Chat) HasMember(userID uuid.UUID) bool {
}
type Message struct {
ID uuid.UUID
ChatID uuid.UUID
SenderID uuid.UUID
Text string
CreatedAt time.Time
ID uuid.UUID
ChatID uuid.UUID
SenderID uuid.UUID
Text string
AttachmentIDs []uuid.UUID
CreatedAt time.Time
}
const (
AttachmentReady = "ready"
ContentPNG = "image/png"
ContentJPEG = "image/jpeg"
)
type Attachment struct {
ID uuid.UUID
ChatID uuid.UUID
OwnerID uuid.UUID
Filename string
ContentType string
SizeBytes int64
SHA256 string
ObjectKey string
Status string
ErrorCode *string
CreatedAt time.Time
UpdatedAt time.Time
}
type Session struct {
+7 -5
View File
@@ -3,9 +3,11 @@ package domain
import "errors"
var (
ErrInvalidInput = errors.New("invalid input")
ErrUnauthorized = errors.New("unauthorized")
ErrNotFound = errors.New("not found")
ErrConflict = errors.New("conflict")
ErrUnavailable = errors.New("dependency unavailable")
ErrInvalidInput = errors.New("invalid input")
ErrUnauthorized = errors.New("unauthorized")
ErrNotFound = errors.New("not found")
ErrConflict = errors.New("conflict")
ErrUnavailable = errors.New("dependency unavailable")
ErrTooLarge = errors.New("payload too large")
ErrUnsupportedMedia = errors.New("unsupported media type")
)
+76
View File
@@ -0,0 +1,76 @@
package handler
import (
"io"
"mime"
"net/http"
"strconv"
"uuid"
"github.com/go-chi/chi/v5"
)
func (h *Handler) UploadAttachment(w http.ResponseWriter, r *http.Request) {
chatID, err := chatIDParam(r)
if err != nil {
h.fail(w, r, err)
return
}
filename := r.URL.Query().Get("filename")
if filename == "" {
h.fail(w, r, badRequest("filename is required"))
return
}
contentType, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
if err != nil {
contentType = ""
}
attachment, err := h.attachments.Upload(r.Context(), actor(r), chatID, filename, contentType, r.Body)
if err != nil {
h.fail(w, r, err)
return
}
writeJSON(w, http.StatusCreated, toAttachmentResponse(attachment))
}
func (h *Handler) GetAttachment(w http.ResponseWriter, r *http.Request) {
id, err := attachmentIDParam(r)
if err != nil {
h.fail(w, r, err)
return
}
attachment, err := h.attachments.Get(r.Context(), actor(r), id)
if err != nil {
h.fail(w, r, err)
return
}
writeJSON(w, http.StatusOK, toAttachmentResponse(attachment))
}
func (h *Handler) DownloadAttachment(w http.ResponseWriter, r *http.Request) {
id, err := attachmentIDParam(r)
if err != nil {
h.fail(w, r, err)
return
}
attachment, content, err := h.attachments.Open(r.Context(), actor(r), id, r.URL.Query().Get("variant"))
if err != nil {
h.fail(w, r, err)
return
}
defer content.Close()
w.Header().Set("Content-Type", attachment.ContentType)
w.Header().Set("Content-Length", strconv.FormatInt(attachment.SizeBytes, 10))
w.WriteHeader(http.StatusOK)
if _, err := io.Copy(w, content); err != nil {
h.log.Error("attachment download interrupted", "request_id", w.Header().Get(headerRequestID), "err", err)
}
}
func attachmentIDParam(r *http.Request) (uuid.UUID, error) {
id, ok := parseUUID(chi.URLParam(r, "attachment_id"))
if !ok {
return uuid.UUID{}, badRequest("attachment_id must be a UUID")
}
return id, nil
}
+64 -11
View File
@@ -2,6 +2,7 @@ package handler
import (
"time"
"uuid"
"messenger/internal/domain"
)
@@ -18,7 +19,8 @@ type createChatRequest struct {
}
type createMessageRequest struct {
Text *string `json:"text"`
Text *string `json:"text"`
AttachmentIDs *[]string `json:"attachment_ids"`
}
type userResponse struct {
@@ -36,11 +38,36 @@ type chatResponse struct {
}
type messageResponse struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
SenderID string `json:"sender_id"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
ID string `json:"id"`
ChatID string `json:"chat_id"`
SenderID string `json:"sender_id"`
Text string `json:"text"`
CreatedAt string `json:"created_at"`
AttachmentIDs []string `json:"attachment_ids"`
}
type variantResponse struct {
Name string `json:"name"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
Width int `json:"width"`
Height int `json:"height"`
SHA256 string `json:"sha256"`
}
type attachmentResponse struct {
ID string `json:"id"`
ChatID string `json:"chat_id"`
OwnerID string `json:"owner_id"`
Filename string `json:"filename"`
ContentType string `json:"content_type"`
SizeBytes int64 `json:"size_bytes"`
SHA256 string `json:"sha256"`
Status string `json:"status"`
Variants []variantResponse `json:"variants"`
ErrorCode *string `json:"error_code"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
}
type pageResponse[T any] struct {
@@ -106,14 +133,40 @@ func toChatResponse(c domain.Chat) chatResponse {
func toMessageResponse(m domain.Message) messageResponse {
return messageResponse{
ID: m.ID.String(),
ChatID: m.ChatID.String(),
SenderID: m.SenderID.String(),
Text: m.Text,
CreatedAt: formatTime(m.CreatedAt),
ID: m.ID.String(),
ChatID: m.ChatID.String(),
SenderID: m.SenderID.String(),
Text: m.Text,
CreatedAt: formatTime(m.CreatedAt),
AttachmentIDs: toIDStrings(m.AttachmentIDs),
}
}
func toAttachmentResponse(a domain.Attachment) attachmentResponse {
return attachmentResponse{
ID: a.ID.String(),
ChatID: a.ChatID.String(),
OwnerID: a.OwnerID.String(),
Filename: a.Filename,
ContentType: a.ContentType,
SizeBytes: a.SizeBytes,
SHA256: a.SHA256,
Status: a.Status,
Variants: []variantResponse{},
ErrorCode: a.ErrorCode,
CreatedAt: formatTime(a.CreatedAt),
UpdatedAt: formatTime(a.UpdatedAt),
}
}
func toIDStrings(ids []uuid.UUID) []string {
result := make([]string, len(ids))
for i, id := range ids {
result[i] = id.String()
}
return result
}
func toPageResponse[D, R any](p domain.Page[D], conv func(D) R) pageResponse[R] {
items := make([]R, len(p.Items))
for i, it := range p.Items {
+4
View File
@@ -43,6 +43,10 @@ func (h *Handler) fail(w http.ResponseWriter, r *http.Request, err error) {
writeError(w, http.StatusNotFound, "not_found", "resource not found")
case errors.Is(err, domain.ErrConflict):
writeError(w, http.StatusConflict, "conflict", err.Error())
case errors.Is(err, domain.ErrTooLarge):
writeError(w, http.StatusRequestEntityTooLarge, "payload_too_large", err.Error())
case errors.Is(err, domain.ErrUnsupportedMedia):
writeError(w, http.StatusUnsupportedMediaType, "unsupported_media_type", err.Error())
case errors.Is(err, domain.ErrUnavailable):
h.log.Error("dependency unavailable", "request_id", w.Header().Get(headerRequestID),
"method", r.Method, "path", r.URL.Path, "err", err)
+12 -9
View File
@@ -12,18 +12,21 @@ import (
type Pinger func(ctx context.Context) error
type Handler struct {
users *service.UserService
auth *service.AuthService
chats *service.ChatService
messages *service.MessageService
deps []Pinger
log *slog.Logger
draining atomic.Bool
users *service.UserService
auth *service.AuthService
chats *service.ChatService
messages *service.MessageService
attachments *service.AttachmentService
deps []Pinger
log *slog.Logger
draining atomic.Bool
}
func New(users *service.UserService, auth *service.AuthService, chats *service.ChatService,
messages *service.MessageService, deps []Pinger, log *slog.Logger) *Handler {
return &Handler{users: users, auth: auth, chats: chats, messages: messages, deps: deps, log: log}
messages *service.MessageService, attachments *service.AttachmentService,
deps []Pinger, log *slog.Logger) *Handler {
return &Handler{users: users, auth: auth, chats: chats, messages: messages,
attachments: attachments, deps: deps, log: log}
}
func (h *Handler) StartDraining() {
+36 -1
View File
@@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/httptest"
@@ -15,6 +16,7 @@ import (
"time"
"uuid"
"messenger/internal/domain"
"messenger/internal/handler"
"messenger/internal/repository/postgres"
"messenger/internal/repository/redis"
@@ -22,6 +24,38 @@ import (
"messenger/internal/service"
)
type stubObjectStore struct {
objects map[string][]byte
}
func newStubObjectStore() *stubObjectStore {
return &stubObjectStore{objects: map[string][]byte{}}
}
func (s *stubObjectStore) Put(_ context.Context, key string, r io.Reader, _ int64, _ string) error {
data, err := io.ReadAll(r)
if err != nil {
return err
}
s.objects[key] = data
return nil
}
func (s *stubObjectStore) Get(_ context.Context, key string) (io.ReadCloser, error) {
data, ok := s.objects[key]
if !ok {
return nil, domain.ErrNotFound
}
return io.NopCloser(strings.NewReader(string(data))), nil
}
func (s *stubObjectStore) Delete(_ context.Context, key string) error {
delete(s.objects, key)
return nil
}
func (s *stubObjectStore) Ping(context.Context) error { return nil }
func testURLs(t *testing.T) (string, string) {
t.Helper()
dbURL, redisURL := os.Getenv("TEST_DATABASE_URL"), os.Getenv("TEST_REDIS_URL")
@@ -56,7 +90,8 @@ func newStack(t *testing.T, dbURL, redisURL string, ttl time.Duration) *httptest
auth := service.NewAuthService(store, sessions, ttl)
chats := service.NewChatService(store)
messages := service.NewMessageService(chats, store)
h := handler.New(users, auth, chats, messages, []handler.Pinger{store.Ping, sessions.Ping}, slog.New(slog.DiscardHandler))
attachments := service.NewAttachmentService(chats, store, newStubObjectStore(), 10485760, 40000000)
h := handler.New(users, auth, chats, messages, attachments, []handler.Pinger{store.Ping, sessions.Ping}, slog.New(slog.DiscardHandler))
srv := httptest.NewServer(router.New(h, "test"))
t.Cleanup(srv.Close)
return srv
+14 -1
View File
@@ -3,6 +3,7 @@ package handler
import (
"net/http"
"regexp"
"uuid"
)
const headerIdempotencyKey = "Idempotency-Key"
@@ -29,7 +30,19 @@ func (h *Handler) SendMessage(w http.ResponseWriter, r *http.Request) {
h.fail(w, r, badRequest("text is required"))
return
}
msg, err := h.messages.Send(r.Context(), actor(r), chatID, key, *req.Text)
var attachmentIDs []uuid.UUID
if req.AttachmentIDs != nil {
attachmentIDs = make([]uuid.UUID, len(*req.AttachmentIDs))
for i, raw := range *req.AttachmentIDs {
id, ok := parseUUID(raw)
if !ok {
h.fail(w, r, badRequest("attachment_ids must contain UUIDs"))
return
}
attachmentIDs[i] = id
}
}
msg, err := h.messages.Send(r.Context(), actor(r), chatID, key, *req.Text, attachmentIDs)
if err != nil {
h.fail(w, r, err)
return
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS message_attachments;
DROP TABLE IF EXISTS attachments;
@@ -0,0 +1,25 @@
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 text NOT NULL,
content_type text NOT NULL,
size_bytes bigint NOT NULL,
sha256 text NOT NULL,
object_key text NOT NULL UNIQUE,
status text NOT NULL,
error_code text,
created_at timestamptz NOT NULL,
updated_at timestamptz NOT NULL
);
CREATE INDEX attachments_chat_created_id_idx 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),
position int NOT NULL,
PRIMARY KEY (message_id, attachment_id)
);
CREATE INDEX message_attachments_attachment_id_idx ON message_attachments (attachment_id);
+80
View File
@@ -200,6 +200,22 @@ func (s *Store) CreateMessageIdempotent(ctx context.Context, m domain.Message, k
if storedHash != requestHash {
return domain.Message{}, domain.ErrConflict
}
rows, err := tx.Query(ctx,
`SELECT attachment_id FROM message_attachments WHERE message_id = $1 ORDER BY position`, stored.ID)
if err != nil {
return domain.Message{}, dbErr(err)
}
stored.AttachmentIDs, err = pgx.CollectRows(rows, func(row pgx.CollectableRow) (uuid.UUID, error) {
var id uuid.UUID
err := row.Scan(&id)
return id, err
})
if err != nil {
return domain.Message{}, dbErr(err)
}
if stored.AttachmentIDs == nil {
stored.AttachmentIDs = []uuid.UUID{}
}
return stored, nil
}
@@ -208,6 +224,20 @@ func (s *Store) CreateMessageIdempotent(ctx context.Context, m domain.Message, k
m.ID, m.ChatID, m.SenderID, m.Text, m.CreatedAt); err != nil {
return domain.Message{}, dbErr(err)
}
if len(m.AttachmentIDs) > 0 {
tag, err := tx.Exec(ctx, `
INSERT INTO message_attachments (message_id, attachment_id, position)
SELECT $1, a.id, u.ord::int
FROM unnest($2::uuid[]) WITH ORDINALITY AS u(id, ord)
JOIN attachments a ON a.id = u.id AND a.chat_id = $3 AND a.status = $4`,
m.ID, m.AttachmentIDs, m.ChatID, domain.AttachmentReady)
if err != nil {
return domain.Message{}, dbErr(err)
}
if int(tag.RowsAffected()) != len(m.AttachmentIDs) {
return domain.Message{}, domain.ErrNotFound
}
}
if err := tx.Commit(ctx); err != nil {
return domain.Message{}, dbErr(err)
}
@@ -247,9 +277,59 @@ func (s *Store) ListMessages(ctx context.Context, chatID uuid.UUID, after *domai
if more {
msgs = msgs[:limit]
}
if err := s.loadMessageAttachments(ctx, msgs); err != nil {
return nil, false, err
}
return msgs, more, nil
}
func (s *Store) CreateAttachment(ctx context.Context, a domain.Attachment) error {
_, err := s.pool.Exec(ctx, `
INSERT INTO attachments (id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, status, error_code, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
a.ID, a.ChatID, a.OwnerID, a.Filename, a.ContentType, a.SizeBytes, a.SHA256, a.ObjectKey, a.Status, a.ErrorCode, a.CreatedAt, a.UpdatedAt)
return dbErr(err)
}
func (s *Store) GetAttachment(ctx context.Context, id uuid.UUID) (domain.Attachment, error) {
var a domain.Attachment
err := s.pool.QueryRow(ctx, `
SELECT id, chat_id, owner_id, filename, content_type, size_bytes, sha256, object_key, status, error_code, created_at, updated_at
FROM attachments WHERE id = $1`, id,
).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.ObjectKey, &a.Status, &a.ErrorCode, &a.CreatedAt, &a.UpdatedAt)
return a, dbErr(err)
}
func (s *Store) loadMessageAttachments(ctx context.Context, msgs []domain.Message) error {
for i := range msgs {
msgs[i].AttachmentIDs = []uuid.UUID{}
}
if len(msgs) == 0 {
return nil
}
ids := make([]uuid.UUID, len(msgs))
index := make(map[uuid.UUID]int, len(msgs))
for i, m := range msgs {
ids[i] = m.ID
index[m.ID] = i
}
rows, err := s.pool.Query(ctx,
`SELECT message_id, attachment_id FROM message_attachments WHERE message_id = ANY($1) ORDER BY message_id, position`, ids)
if err != nil {
return dbErr(err)
}
defer rows.Close()
for rows.Next() {
var messageID, attachmentID uuid.UUID
if err := rows.Scan(&messageID, &attachmentID); err != nil {
return dbErr(err)
}
i := index[messageID]
msgs[i].AttachmentIDs = append(msgs[i].AttachmentIDs, attachmentID)
}
return dbErr(rows.Err())
}
func dbErr(err error) error {
if err == nil {
return nil
+102
View File
@@ -0,0 +1,102 @@
package s3
import (
"context"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"github.com/minio/minio-go/v7"
"github.com/minio/minio-go/v7/pkg/credentials"
"messenger/internal/domain"
)
type Store struct {
client *minio.Client
bucket string
region string
}
func New(endpoint, region, accessKeyID, secretAccessKey, bucket string) (*Store, error) {
host, secure := endpoint, false
if parsed, err := url.Parse(endpoint); err == nil && parsed.Host != "" {
host = parsed.Host
secure = parsed.Scheme == "https"
}
client, err := minio.New(strings.TrimRight(host, "/"), &minio.Options{
Creds: credentials.NewStaticV4(accessKeyID, secretAccessKey, ""),
Region: region,
Secure: secure,
})
if err != nil {
return nil, err
}
return &Store{client: client, bucket: bucket, region: region}, nil
}
func (s *Store) EnsureBucket(ctx context.Context) error {
exists, err := s.client.BucketExists(ctx, s.bucket)
if err != nil {
return s.wrap(err)
}
if exists {
return nil
}
if err := s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{Region: s.region}); err != nil {
switch minio.ToErrorResponse(err).Code {
case "BucketAlreadyOwnedByYou", "BucketAlreadyExists":
return nil
}
return s.wrap(err)
}
return nil
}
func (s *Store) Ping(ctx context.Context) error {
_, err := s.client.BucketExists(ctx, s.bucket)
return s.wrap(err)
}
func (s *Store) Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error {
_, err := s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ContentType: contentType})
if err != nil && minio.ToErrorResponse(err).Code == "NoSuchBucket" {
if ensureErr := s.EnsureBucket(ctx); ensureErr == nil {
_, err = s.client.PutObject(ctx, s.bucket, key, r, size, minio.PutObjectOptions{ContentType: contentType})
}
}
return s.wrap(err)
}
func (s *Store) Get(ctx context.Context, key string) (io.ReadCloser, error) {
object, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
if err != nil {
return nil, s.wrap(err)
}
if _, err := object.Stat(); err != nil {
object.Close()
return nil, s.wrap(err)
}
return object, nil
}
func (s *Store) Delete(ctx context.Context, key string) error {
return s.wrap(s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{}))
}
func (s *Store) wrap(err error) error {
if err == nil {
return nil
}
response := minio.ToErrorResponse(err)
switch response.Code {
case "NoSuchKey", "NotFound":
return domain.ErrNotFound
}
if response.StatusCode == http.StatusNotFound {
return domain.ErrNotFound
}
return fmt.Errorf("%w: %v", domain.ErrUnavailable, err)
}
+3
View File
@@ -31,6 +31,9 @@ func New(h *handler.Handler, instanceID string) http.Handler {
r.Get("/chats/{chat_id}", h.GetChat)
r.Post("/chats/{chat_id}/messages", h.SendMessage)
r.Get("/chats/{chat_id}/messages", h.ListMessages)
r.Post("/chats/{chat_id}/attachments", h.UploadAttachment)
r.Get("/attachments/{attachment_id}", h.GetAttachment)
r.Get("/attachments/{attachment_id}/content", h.DownloadAttachment)
})
})
return r
+186
View File
@@ -0,0 +1,186 @@
package service
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
"image"
_ "image/jpeg"
_ "image/png"
"io"
"net/http"
"time"
"unicode"
"unicode/utf8"
"uuid"
"messenger/internal/domain"
)
const (
originalVariant = "original"
maxFilenameRunes = 255
sniffBytes = 512
)
type AttachmentService struct {
chats *ChatService
repo AttachmentRepository
store ObjectStore
maxBytes int64
maxPixels int64
}
func NewAttachmentService(chats *ChatService, repo AttachmentRepository, store ObjectStore, maxBytes, maxPixels int64) *AttachmentService {
return &AttachmentService{chats: chats, repo: repo, store: store, maxBytes: maxBytes, maxPixels: maxPixels}
}
func (s *AttachmentService) Upload(ctx context.Context, actor domain.User, chatID uuid.UUID, filename, contentType string, body io.Reader) (domain.Attachment, error) {
if _, err := s.chats.Get(ctx, actor, chatID); err != nil {
return domain.Attachment{}, err
}
if err := checkFilename(filename); err != nil {
return domain.Attachment{}, err
}
if contentType != domain.ContentPNG && contentType != domain.ContentJPEG {
return domain.Attachment{}, fmt.Errorf("%w: content type must be image/png or image/jpeg", domain.ErrUnsupportedMedia)
}
raw, err := readLimited(body, s.maxBytes)
if err != nil {
return domain.Attachment{}, err
}
if detected := detectImageType(raw); detected != contentType {
return domain.Attachment{}, fmt.Errorf("%w: body does not match the declared image type", domain.ErrUnsupportedMedia)
}
if err := checkPixels(raw, s.maxPixels); err != nil {
return domain.Attachment{}, err
}
if err := checkIntegrity(raw); err != nil {
return domain.Attachment{}, err
}
id := uuid.New()
sum := sha256.Sum256(raw)
createdAt := now()
attachment := domain.Attachment{
ID: id,
ChatID: chatID,
OwnerID: actor.ID,
Filename: filename,
ContentType: contentType,
SizeBytes: int64(len(raw)),
SHA256: hex.EncodeToString(sum[:]),
ObjectKey: "chats/" + chatID.String() + "/" + id.String(),
Status: domain.AttachmentReady,
CreatedAt: createdAt,
UpdatedAt: createdAt,
}
if err := s.store.Put(ctx, attachment.ObjectKey, bytes.NewReader(raw), attachment.SizeBytes, contentType); err != nil {
return domain.Attachment{}, err
}
if err := s.repo.CreateAttachment(ctx, attachment); err != nil {
cleanupCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 5*time.Second)
defer cancel()
_ = s.store.Delete(cleanupCtx, attachment.ObjectKey)
return domain.Attachment{}, err
}
return attachment, nil
}
func (s *AttachmentService) Get(ctx context.Context, actor domain.User, id uuid.UUID) (domain.Attachment, error) {
attachment, err := s.repo.GetAttachment(ctx, id)
if err != nil {
return domain.Attachment{}, err
}
if _, err := s.chats.Get(ctx, actor, attachment.ChatID); err != nil {
return domain.Attachment{}, err
}
return attachment, nil
}
func (s *AttachmentService) Open(ctx context.Context, actor domain.User, id uuid.UUID, variant string) (domain.Attachment, io.ReadCloser, error) {
if variant == "" {
variant = originalVariant
}
if variant != originalVariant {
return domain.Attachment{}, nil, fmt.Errorf("%w: only the original variant is available", domain.ErrInvalidInput)
}
attachment, err := s.repo.GetAttachment(ctx, id)
if err != nil {
return domain.Attachment{}, nil, err
}
if _, err := s.chats.Get(ctx, actor, attachment.ChatID); err != nil {
return domain.Attachment{}, nil, err
}
if attachment.Status != domain.AttachmentReady {
return domain.Attachment{}, nil, fmt.Errorf("%w: attachment is not ready", domain.ErrConflict)
}
content, err := s.store.Get(ctx, attachment.ObjectKey)
if err != nil {
return domain.Attachment{}, nil, err
}
return attachment, content, nil
}
func checkFilename(filename string) error {
if n := utf8.RuneCountInString(filename); n < 1 || n > maxFilenameRunes {
return fmt.Errorf("%w: filename must be 1 to 255 characters", domain.ErrInvalidInput)
}
for _, r := range filename {
if unicode.IsControl(r) || r == '/' || r == '\\' {
return fmt.Errorf("%w: filename contains forbidden characters", domain.ErrInvalidInput)
}
}
return nil
}
func readLimited(body io.Reader, max int64) ([]byte, error) {
raw, err := io.ReadAll(io.LimitReader(body, max+1))
if err != nil {
return nil, fmt.Errorf("%w: cannot read the upload body", domain.ErrInvalidInput)
}
if int64(len(raw)) > max {
return nil, fmt.Errorf("%w: upload exceeds %d bytes", domain.ErrTooLarge, max)
}
if len(raw) == 0 {
return nil, fmt.Errorf("%w: upload body is empty", domain.ErrUnsupportedMedia)
}
return raw, nil
}
func detectImageType(raw []byte) string {
sniff := raw
if len(sniff) > sniffBytes {
sniff = sniff[:sniffBytes]
}
switch http.DetectContentType(sniff) {
case domain.ContentPNG:
return domain.ContentPNG
case domain.ContentJPEG:
return domain.ContentJPEG
}
return ""
}
func checkPixels(raw []byte, max int64) error {
config, _, err := image.DecodeConfig(bytes.NewReader(raw))
if err != nil {
return fmt.Errorf("%w: image header cannot be decoded", domain.ErrUnsupportedMedia)
}
if config.Width <= 0 || config.Height <= 0 {
return fmt.Errorf("%w: image has invalid dimensions", domain.ErrUnsupportedMedia)
}
if int64(config.Width) > max/int64(config.Height) {
return fmt.Errorf("%w: image exceeds %d pixels", domain.ErrTooLarge, max)
}
return nil
}
func checkIntegrity(raw []byte) error {
if _, _, err := image.Decode(bytes.NewReader(raw)); err != nil {
return fmt.Errorf("%w: image is corrupted", domain.ErrUnsupportedMedia)
}
return nil
}
+157
View File
@@ -0,0 +1,157 @@
package service
import (
"bytes"
"encoding/binary"
"errors"
"hash/crc32"
"image"
"image/png"
"strings"
"testing"
"uuid"
"messenger/internal/domain"
)
func TestCheckFilename(t *testing.T) {
valid := []string{"photo.png", "фото 1.jpg", strings.Repeat("a", 255)}
for _, name := range valid {
if err := checkFilename(name); err != nil {
t.Errorf("%q: unexpected error %v", name, err)
}
}
invalid := []string{"", strings.Repeat("a", 256), "dir/photo.png", `dir\photo.png`, "photo\x00.png", "photo\n.png", "photo\x7f.png"}
for _, name := range invalid {
if err := checkFilename(name); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("%q: got %v, want ErrInvalidInput", name, err)
}
}
}
func TestDetectImageType(t *testing.T) {
if got := detectImageType(encodePNG(t, 4, 4)); got != domain.ContentPNG {
t.Errorf("png: got %q", got)
}
if got := detectImageType([]byte("not an image at all")); got != "" {
t.Errorf("garbage: got %q", got)
}
}
func TestCheckPixels(t *testing.T) {
if err := checkPixels(encodePNG(t, 320, 200), 40000000); err != nil {
t.Errorf("small image: unexpected error %v", err)
}
if err := checkPixels(pngHeader(8000, 5000), 40000000); err != nil {
t.Errorf("exact pixel limit: unexpected error %v", err)
}
if err := checkPixels(pngHeader(20000, 2001), 40000000); !errors.Is(err, domain.ErrTooLarge) {
t.Errorf("above pixel limit: got %v, want ErrTooLarge", err)
}
if err := checkPixels(pngHeader(65536, 65536), 40000000); !errors.Is(err, domain.ErrTooLarge) {
t.Errorf("huge dimensions: got %v, want ErrTooLarge", err)
}
if err := checkPixels([]byte("garbage"), 40000000); !errors.Is(err, domain.ErrUnsupportedMedia) {
t.Errorf("garbage: got %v, want ErrUnsupportedMedia", err)
}
}
func TestCheckIntegrity(t *testing.T) {
if err := checkIntegrity(encodePNG(t, 8, 8)); err != nil {
t.Errorf("valid png: unexpected error %v", err)
}
corrupted := encodePNG(t, 8, 8)
corrupted = corrupted[:len(corrupted)-20]
if err := checkIntegrity(corrupted); !errors.Is(err, domain.ErrUnsupportedMedia) {
t.Errorf("truncated png: got %v, want ErrUnsupportedMedia", err)
}
}
func TestCheckMessageText(t *testing.T) {
if err := checkMessageText("", false); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("empty text without attachments: got %v", err)
}
if err := checkMessageText(" ", false); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("blank text without attachments: got %v", err)
}
if err := checkMessageText("", true); err != nil {
t.Errorf("empty text with attachments: unexpected error %v", err)
}
if err := checkMessageText(strings.Repeat("я", 4000), true); err != nil {
t.Errorf("4000 characters: unexpected error %v", err)
}
if err := checkMessageText(strings.Repeat("я", 4001), true); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("4001 characters: got %v", err)
}
}
func TestCheckAttachmentIDs(t *testing.T) {
ids := make([]uuid.UUID, 10)
for i := range ids {
ids[i] = uuid.New()
}
if err := checkAttachmentIDs(ids); err != nil {
t.Errorf("ten unique ids: unexpected error %v", err)
}
if err := checkAttachmentIDs(append(ids, uuid.New())); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("eleven ids: got %v", err)
}
if err := checkAttachmentIDs([]uuid.UUID{ids[0], ids[0]}); !errors.Is(err, domain.ErrInvalidInput) {
t.Errorf("duplicate ids: got %v", err)
}
}
func TestMessageHash(t *testing.T) {
first, second := uuid.New(), uuid.New()
if messageHash("", nil) != messageHash("", []uuid.UUID{}) {
t.Error("absent attachment_ids must hash like an empty list")
}
if messageHash("hi", []uuid.UUID{first, second}) != messageHash("hi", []uuid.UUID{second, first}) {
t.Error("attachment order must not change the hash")
}
if messageHash("hi", nil) == messageHash("hi!", nil) {
t.Error("different text must change the hash")
}
if messageHash("hi", []uuid.UUID{first}) == messageHash("hi", []uuid.UUID{second}) {
t.Error("different attachments must change the hash")
}
}
func TestReadLimited(t *testing.T) {
raw, err := readLimited(strings.NewReader("abcd"), 4)
if err != nil || string(raw) != "abcd" {
t.Fatalf("exact limit: got %q, %v", raw, err)
}
if _, err := readLimited(strings.NewReader("abcde"), 4); !errors.Is(err, domain.ErrTooLarge) {
t.Errorf("over limit: got %v, want ErrTooLarge", err)
}
if _, err := readLimited(strings.NewReader(""), 4); !errors.Is(err, domain.ErrUnsupportedMedia) {
t.Errorf("empty body: got %v, want ErrUnsupportedMedia", err)
}
}
func encodePNG(t *testing.T, width, height int) []byte {
t.Helper()
var buffer bytes.Buffer
if err := png.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, width, height))); err != nil {
t.Fatal(err)
}
return buffer.Bytes()
}
func pngHeader(width, height uint32) []byte {
var buffer bytes.Buffer
buffer.WriteString("\x89PNG\r\n\x1a\n")
header := make([]byte, 13)
binary.BigEndian.PutUint32(header[0:4], width)
binary.BigEndian.PutUint32(header[4:8], height)
header[8], header[9] = 8, 2
buffer.Write(binary.BigEndian.AppendUint32(nil, uint32(len(header))))
buffer.WriteString("IHDR")
buffer.Write(header)
checksum := crc32.NewIEEE()
checksum.Write([]byte("IHDR"))
checksum.Write(header)
buffer.Write(binary.BigEndian.AppendUint32(nil, checksum.Sum32()))
return buffer.Bytes()
}
+51 -9
View File
@@ -6,11 +6,16 @@ import (
"encoding/hex"
"errors"
"fmt"
"sort"
"strings"
"unicode/utf8"
"uuid"
"messenger/internal/domain"
)
const maxMessageAttachments = 10
type MessageService struct {
chats *ChatService
messages MessageRepository
@@ -20,22 +25,25 @@ func NewMessageService(chats *ChatService, messages MessageRepository) *MessageS
return &MessageService{chats: chats, messages: messages}
}
func (s *MessageService) Send(ctx context.Context, actor domain.User, chatID uuid.UUID, key, text string) (domain.Message, error) {
func (s *MessageService) Send(ctx context.Context, actor domain.User, chatID uuid.UUID, key, text string, attachmentIDs []uuid.UUID) (domain.Message, error) {
if _, err := s.chats.Get(ctx, actor, chatID); err != nil {
return domain.Message{}, err
}
if err := checkText("text", text, 4000); err != nil {
if err := checkMessageText(text, len(attachmentIDs) > 0); err != nil {
return domain.Message{}, err
}
if err := checkAttachmentIDs(attachmentIDs); err != nil {
return domain.Message{}, err
}
m := domain.Message{
ID: uuid.New(),
ChatID: chatID,
SenderID: actor.ID,
Text: text,
CreatedAt: now(),
ID: uuid.New(),
ChatID: chatID,
SenderID: actor.ID,
Text: text,
AttachmentIDs: attachmentIDs,
CreatedAt: now(),
}
sum := sha256.Sum256([]byte(text))
stored, err := s.messages.CreateMessageIdempotent(ctx, m, key, hex.EncodeToString(sum[:]))
stored, err := s.messages.CreateMessageIdempotent(ctx, m, key, messageHash(text, attachmentIDs))
if errors.Is(err, domain.ErrConflict) {
return domain.Message{}, fmt.Errorf("%w: Idempotency-Key was already used with a different body", domain.ErrConflict)
}
@@ -63,3 +71,37 @@ func (s *MessageService) History(ctx context.Context, actor domain.User, chatID
}
return page, nil
}
func checkMessageText(text string, hasAttachments bool) error {
if utf8.RuneCountInString(text) > 4000 {
return fmt.Errorf("%w: text must be at most 4000 characters", domain.ErrInvalidInput)
}
if !hasAttachments && strings.TrimSpace(text) == "" {
return fmt.Errorf("%w: text must not be empty without attachments", domain.ErrInvalidInput)
}
return nil
}
func checkAttachmentIDs(ids []uuid.UUID) error {
if len(ids) > maxMessageAttachments {
return fmt.Errorf("%w: attachment_ids must contain at most %d items", domain.ErrInvalidInput, maxMessageAttachments)
}
seen := make(map[uuid.UUID]struct{}, len(ids))
for _, id := range ids {
if _, duplicate := seen[id]; duplicate {
return fmt.Errorf("%w: attachment_ids must not contain duplicates", domain.ErrInvalidInput)
}
seen[id] = struct{}{}
}
return nil
}
func messageHash(text string, ids []uuid.UUID) string {
sorted := make([]string, len(ids))
for i, id := range ids {
sorted[i] = id.String()
}
sort.Strings(sorted)
sum := sha256.Sum256([]byte(text + "\n" + strings.Join(sorted, ",")))
return hex.EncodeToString(sum[:])
}
+13
View File
@@ -2,6 +2,7 @@ package service
import (
"context"
"io"
"time"
"uuid"
@@ -25,6 +26,18 @@ type MessageRepository interface {
ListMessages(ctx context.Context, chatID uuid.UUID, after *domain.Cursor, limit int) ([]domain.Message, bool, error)
}
type AttachmentRepository interface {
CreateAttachment(ctx context.Context, a domain.Attachment) error
GetAttachment(ctx context.Context, id uuid.UUID) (domain.Attachment, error)
}
type ObjectStore interface {
Put(ctx context.Context, key string, r io.Reader, size int64, contentType string) error
Get(ctx context.Context, key string) (io.ReadCloser, error)
Delete(ctx context.Context, key string) error
Ping(ctx context.Context) error
}
type SessionStore interface {
Create(ctx context.Context, tokenHash string, userID uuid.UUID, expiresAt time.Time) error
Get(ctx context.Context, tokenHash string) (uuid.UUID, error)
+130
View File
@@ -0,0 +1,130 @@
#!/usr/bin/env python3
"""Attachment contract checks beyond the public smoke; creates synthetic data."""
import hashlib
import sys
import uuid
from pathlib import Path
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / 'tests'))
from client import Client, check, run
ROOT = Path(__file__).resolve().parents[1]
PNG = (ROOT / 'tests/fixtures/sample.png').read_bytes()
def upload(c, actor, chat, content=PNG, filename='sample.png', content_type='image/png', expected=201):
path = '/api/v1/chats/' + chat['id'] + '/attachments?filename=' + filename
return c.call('POST', path, expected, actor=actor, raw=content, headers={'Content-Type': content_type}, schema='Attachment')
def main():
c = Client()
alice, bob, eve, chat = c.bootstrap()
other = c.call('POST', '/api/v1/chats', 201, actor=alice,
body={'title': 'Другой чат', 'member_ids': [alice['id'], bob['id']]}, schema='Chat')['json']
notes = []
att = upload(c, alice, chat)['json']
notes.append(f"upload 201 ready, sha256={att['sha256']}, size={att['size_bytes']}")
check(att['sha256'] == hashlib.sha256(PNG).hexdigest(), 'sha256 mismatch')
check(att['status'] == 'ready' and att['variants'] == [] and att['error_code'] is None, 'attachment shape')
jpeg_path = ROOT / 'scripts/fixtures/sample.jpg'
if jpeg_path.is_file():
jpeg = jpeg_path.read_bytes()
jatt = upload(c, alice, chat, content=jpeg, filename='sample.jpg', content_type='image/jpeg')['json']
check(jatt['content_type'] == 'image/jpeg' and jatt['sha256'] == hashlib.sha256(jpeg).hexdigest(),
'jpeg metadata mismatch')
got = c.call('GET', '/api/v1/attachments/' + jatt['id'] + '/content', 200, actor=bob)
check(got['bytes'] == jpeg and got['headers'].get_content_type() == 'image/jpeg', 'jpeg download mismatch')
upload(c, alice, chat, content=jpeg, filename='sample.jpg', content_type='image/png', expected=415)
notes.append('jpeg upload/download byte-identical; jpeg declared as png -> 415')
large_path = ROOT / 'scripts/fixtures/large.png'
if large_path.is_file():
large = large_path.read_bytes()
latt = upload(c, alice, chat, content=large, filename='large.png')['json']
check(latt['size_bytes'] == len(large) and latt['status'] == 'ready', 'image at the pixel limit was rejected')
got = c.call('GET', '/api/v1/attachments/' + latt['id'] + '/content', 200, actor=bob)
check(got['bytes'] == large, 'large image download mismatch')
notes.append('png with 40,000,000 pixels accepted and downloaded byte-identical')
upload(c, alice, chat, filename='bad/name.png', expected=400)
notes.append('filename with slash -> 400')
upload(c, alice, chat, filename='bad%00name.png', expected=400)
notes.append('filename with NUL -> 400')
upload(c, alice, chat, filename='', expected=400)
notes.append('empty filename -> 400')
upload(c, alice, chat, content_type='image/jpeg', expected=415)
notes.append('declared jpeg with png body -> 415')
upload(c, alice, chat, content=PNG[:-30], expected=415)
notes.append('truncated png -> 415')
upload(c, alice, chat, content=b'\x00' * (10485761), expected=413)
notes.append('10485761 bytes -> 413')
upload(c, alice, chat, content_type='text/plain', expected=415)
notes.append('text/plain -> 415')
c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=thumbnail', 400, actor=bob)
notes.append('unknown variant -> 400')
c.call('GET', '/api/v1/attachments/' + att['id'] + '/content?variant=original', 200, actor=bob)
notes.append('variant=original -> 200')
c.call('GET', '/api/v1/attachments/' + str(uuid.uuid4()), 404, actor=bob)
notes.append('unknown attachment id -> 404')
c.call('POST', '/api/v1/chats/' + chat['id'] + '/attachments?filename=x.png', 404, actor=eve,
raw=PNG, headers={'Content-Type': 'image/png'}, schema='Attachment')
notes.append('upload by outsider -> 404')
foreign = upload(c, alice, other)['json']
key = uuid.uuid4().hex
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
body={'text': '', 'attachment_ids': [foreign['id']]}, headers={'Idempotency-Key': key})
notes.append('attachment from another chat -> 404')
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 404, actor=alice,
body={'text': '', 'attachment_ids': [str(uuid.uuid4())]}, headers={'Idempotency-Key': uuid.uuid4().hex})
notes.append('unknown attachment id in message -> 404')
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
body={'text': '', 'attachment_ids': [att['id']] * 2}, headers={'Idempotency-Key': uuid.uuid4().hex})
notes.append('duplicate attachment_ids -> 400')
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
body={'text': '', 'attachment_ids': [str(uuid.uuid4()) for _ in range(11)]},
headers={'Idempotency-Key': uuid.uuid4().hex})
notes.append('eleven attachment_ids -> 400')
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 400, actor=alice,
body={'text': ''}, headers={'Idempotency-Key': uuid.uuid4().hex})
notes.append('empty text without attachments -> 400')
key = uuid.uuid4().hex
first = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
body={'text': 'вложение', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}, schema='Message')['json']
replay = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
body={'text': 'вложение', 'attachment_ids': [att['id']]}, headers={'Idempotency-Key': key}, schema='Message')['json']
check(first == replay, 'idempotent replay with attachments differs')
check(first['attachment_ids'] == [att['id']], 'message lost attachment_ids')
notes.append('idempotent replay with same attachment set -> same message')
empty_key = uuid.uuid4().hex
without = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
body={'text': 'без вложений'}, headers={'Idempotency-Key': empty_key}, schema='Message')['json']
implicit = c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 201, actor=alice,
body={'text': 'без вложений', 'attachment_ids': []}, headers={'Idempotency-Key': empty_key}, schema='Message')['json']
check(without == implicit, 'absent attachment_ids must equal an empty list')
notes.append('absent attachment_ids equals [] for idempotency')
c.call('POST', '/api/v1/chats/' + chat['id'] + '/messages', 409, actor=alice,
body={'text': 'вложение', 'attachment_ids': []}, headers={'Idempotency-Key': key})
notes.append('same key with a different attachment set -> 409')
history = c.call('GET', '/api/v1/chats/' + chat['id'] + '/messages?limit=100', 200, actor=bob,
schema='MessagePage')['json']['items']
check(all(isinstance(m['attachment_ids'], list) for m in history), 'attachment_ids must always be an array')
check(any(m['id'] == first['id'] and m['attachment_ids'] == [att['id']] for m in history),
'history lost attachments')
notes.append('history exposes attachment_ids on every message')
for line in notes:
print('ok:', line)
print(f'PASS: {len(notes)} attachment contract checks')
if __name__ == '__main__':
run(main)
Binary file not shown.

After

Width:  |  Height:  |  Size: 124 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 653 B

+7 -2
View File
@@ -13,6 +13,11 @@ password=$(cat secrets/postgres_password)
printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$password" > secrets/database_url
[ -f secrets/cache_url ] ||
printf 'redis://redis:6379/0\n' > secrets/cache_url
[ -f secrets/s3_access_key_id ] ||
head -c 10 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_access_key_id
[ -f secrets/s3_secret_access_key ] ||
head -c 24 /dev/urandom | od -An -tx1 | tr -d ' \n' > secrets/s3_secret_access_key
chmod 644 secrets/postgres_password secrets/database_url secrets/cache_url
echo "secrets ready in ./secrets"
chmod 644 secrets/postgres_password secrets/database_url secrets/cache_url \
secrets/s3_access_key_id secrets/s3_secret_access_key
echo "secrets ready in ./secrets"
+2 -2
View File
@@ -4,5 +4,5 @@ cd "$(dirname "$0")/.."
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
$COMPOSE stop lb api redis postgres
$COMPOSE start postgres redis api lb
$COMPOSE stop lb api redis postgres s3
$COMPOSE start postgres redis s3 api lb
+2 -2
View File
@@ -3,7 +3,7 @@ set -eu
cd "$(dirname "$0")/.."
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
COMPOSE=${COMPOSE:-$([ "$ENGINE" = podman ] && echo podman-compose || echo "docker compose")}
PROJECT=${PROJECT:-messenger-lab3}
PROJECT=${PROJECT:-messenger-lab4}
replicas() {
$ENGINE ps -aq --no-trunc --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api" | sort
@@ -20,7 +20,7 @@ wait_healthy() {
exit 1
}
$ENGINE build --build-arg "BUILD_ID=${BUILD_ID:-$(date +%s)}" -t localhost/messenger-api:lab3 .
$ENGINE build --build-arg "BUILD_ID=${BUILD_ID:-$(date +%s)}" -t localhost/messenger-api:lab4 .
$COMPOSE run --rm migrate
for old in $(replicas); do
+1 -1
View File
@@ -1,7 +1,7 @@
#!/bin/sh
set -eu
ENGINE=${ENGINE:-$(command -v podman >/dev/null 2>&1 && echo podman || echo docker)}
PROJECT=${PROJECT:-messenger-lab3}
PROJECT=${PROJECT:-messenger-lab4}
: "${TARGET_INSTANCE_ID:?TARGET_INSTANCE_ID is required}"
for id in $($ENGINE ps -q --filter "label=com.docker.compose.project=$PROJECT" --filter "label=com.docker.compose.service=api"); do