From 0a361367c7b31de619c8db20c34f93c7f309ff03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9E=D0=BB=D1=8F=20=D0=9C=D0=BE=D1=80=D0=BE=D0=B7?= <145702378+MiniLynx13@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:46:03 +0300 Subject: [PATCH] feat:lab4-catchup --- Dockerfile | 31 ++ cmd/messenger/main.go | 107 +++++ compose.yaml | 130 ++++++ go.mod | 30 ++ go.sum | 62 +++ infra/minio/bootstrap.sh | 52 +++ infra/nginx/nginx.conf | 29 ++ migrations/0001_init.sql | 31 ++ migrations/0002_sessions.sql | 9 + migrations/0003_messages_alt_index.sql | 5 + migrations/0004_idempotency.sql | 14 + migrations/0005_attachments.sql | 30 ++ scripts/migrate.sh | 13 + scripts/restart.sh | 26 ++ scripts/restore-api.sh | 36 ++ scripts/stop-one.sh | 20 + src/api/auth.go | 79 ++++ src/api/errors.go | 28 ++ src/api/handlers.go | 537 ++++++++++++++++++++++ src/api/middleware.go | 46 ++ src/api/router.go | 24 + src/config/config.go | 136 ++++++ src/domain/attachments.go | 189 ++++++++ src/domain/cache.go | 91 ++++ src/domain/idempotency.go | 75 +++ src/domain/models.go | 92 ++++ src/domain/postgres.go | 608 +++++++++++++++++++++++++ src/domain/s3.go | 213 +++++++++ src/domain/sessions.go | 108 +++++ 29 files changed, 2851 insertions(+) create mode 100644 Dockerfile create mode 100644 cmd/messenger/main.go create mode 100644 compose.yaml create mode 100644 go.mod create mode 100644 go.sum create mode 100644 infra/minio/bootstrap.sh create mode 100644 infra/nginx/nginx.conf create mode 100644 migrations/0001_init.sql create mode 100644 migrations/0002_sessions.sql create mode 100644 migrations/0003_messages_alt_index.sql create mode 100644 migrations/0004_idempotency.sql create mode 100644 migrations/0005_attachments.sql create mode 100644 scripts/migrate.sh create mode 100644 scripts/restart.sh create mode 100644 scripts/restore-api.sh create mode 100644 scripts/stop-one.sh create mode 100644 src/api/auth.go create mode 100644 src/api/errors.go create mode 100644 src/api/handlers.go create mode 100644 src/api/middleware.go create mode 100644 src/api/router.go create mode 100644 src/config/config.go create mode 100644 src/domain/attachments.go create mode 100644 src/domain/cache.go create mode 100644 src/domain/idempotency.go create mode 100644 src/domain/models.go create mode 100644 src/domain/postgres.go create mode 100644 src/domain/s3.go create mode 100644 src/domain/sessions.go diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..040241e0 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,31 @@ +# --- build stage --- +FROM golang:1.22-alpine AS build + +WORKDIR /src +RUN apk add --no-cache ca-certificates + +COPY go.mod go.sum ./ +RUN go mod download + +COPY cmd ./cmd +COPY src ./src +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/messenger ./cmd/messenger + +# --- runtime stage --- +FROM alpine:3.20 +RUN apk add --no-cache ca-certificates postgresql-client && \ + addgroup -S app && adduser -S -G app app +WORKDIR /app + +COPY --from=build /out/messenger /app/messenger +COPY migrations /app/migrations +COPY scripts/migrate.sh /app/migrate.sh +RUN chmod +x /app/migrate.sh + +USER app +EXPOSE 8080 + +HEALTHCHECK --interval=10s --timeout=3s --start-period=20s --retries=5 \ + CMD wget -qO- http://127.0.0.1:8080/health/ready >/dev/null || exit 1 + +ENTRYPOINT ["/app/messenger"] \ No newline at end of file diff --git a/cmd/messenger/main.go b/cmd/messenger/main.go new file mode 100644 index 00000000..0f0cdce1 --- /dev/null +++ b/cmd/messenger/main.go @@ -0,0 +1,107 @@ +package main + +import ( + "context" + "errors" + "fmt" + "log" + "net/http" + "os" + "os/signal" + "syscall" + "time" + + "mtusi-messenger/src/api" + "mtusi-messenger/src/config" + "mtusi-messenger/src/domain" +) + +func main() { + cfg := config.Load() + if cfg.DatabaseURL == "" { + log.Fatal("DATABASE_URL is required (env DATABASE_URL or DATABASE_URL_FILE)") + } + + rootCtx, cancelRoot := context.WithCancel(context.Background()) + defer cancelRoot() + + store, err := domain.NewStore(rootCtx, cfg.DatabaseURL) + if err != nil { + log.Fatalf("postgres: %v", err) + } + defer store.Close() + + var s3store *domain.S3Store + if cfg.S3Endpoint != "" { + s3store, err = domain.NewS3Store(rootCtx, cfg.S3Endpoint, cfg.S3Region, cfg.S3Bucket, + cfg.S3AccessKeyID, cfg.S3SecretKey, cfg.MaxUploadBytes, cfg.MaxImagePixels) + if err != nil { + log.Fatalf("s3: %v", err) + } + } else { + log.Fatal("S3_ENDPOINT is required for lab4") + } + + // Кэш сессий в Redis/Valkey. Если CACHE_URL не задан — работаем без кэша. + var cache *domain.SessionCache + if cfg.CacheURL != "" { + cache, err = domain.NewSessionCache(rootCtx, cfg.CacheURL) + if err != nil { + if cfg.RedisRequired { + log.Fatalf("redis required but unavailable: %v", err) + } + log.Printf("redis unavailable, continuing without session cache: %v", err) + cache = nil + } else { + defer cache.Close() + } + } else if cfg.RedisRequired { + log.Fatal("SESSION_STORE=redis but CACHE_URL is not configured") + } + + sessions := domain.NewSessionStore(store, cache, cfg.SessionTTL) + handler := &api.Handler{Store: store, Sessions: sessions, S3: s3store, MaxUpload: cfg.MaxUploadBytes} + router := api.NewRouter(handler, cfg.InstanceID) + + go func() { + ticker := time.NewTicker(2 * time.Minute) + defer ticker.Stop() + for { + select { + case <-rootCtx.Done(): + return + case <-ticker.C: + if err := store.ReconcileOrphans(rootCtx, s3store, cfg.S3GCGrace); err != nil { + log.Printf("reconcile: %v", err) + } + } + } + }() + + srv := &http.Server{ + Addr: fmt.Sprintf("%s:%d", cfg.Host, cfg.Port), + Handler: router, + ReadHeaderTimeout: 5 * time.Second, + ReadTimeout: 15 * time.Second, + WriteTimeout: 15 * time.Second, + IdleTimeout: 60 * time.Second, + } + + stop := make(chan os.Signal, 1) + signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT) + + go func() { + log.Printf("messenger listening on %s (env=%s, session_ttl=%s)", srv.Addr, cfg.Env, cfg.SessionTTL) + log.Printf("instance_id=%s", cfg.InstanceID) + if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) { + log.Fatalf("server error: %v", err) + } + }() + + <-stop + log.Println("shutting down...") + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + _ = srv.Shutdown(ctx) + log.Println("stopped") +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..eb616c89 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,130 @@ +x-api: &api + build: + context: . + dockerfile: Dockerfile + env_file: + - .env + secrets: [database_url, cache_url, s3_access_key_id, s3_secret_access_key] + depends_on: + postgres: { condition: service_healthy } + redis: { condition: service_healthy } + s3: { condition: service_healthy } + s3-init: { condition: service_completed_successfully } + networks: [backend] + +services: + postgres: + image: postgres:16.3-alpine + environment: + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + POSTGRES_DB: messenger + secrets: [postgres_password] + volumes: [pgdata:/var/lib/postgresql/data] + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 20 + networks: [backend] + deploy: + resources: + limits: { cpus: "${PG_CPUS:-1.0}", memory: "${PG_MEM:-512M}" } + + redis: + image: redis:7.2.5-alpine + command: ["redis-server", "--appendonly", "yes", "--save"] + volumes: [redisdata:/data] + healthcheck: + test: ["CMD", "redis-cli", "ping"] + interval: 5s + timeout: 3s + retries: 20 + networks: [backend] + + s3: + image: pgsty/silo:RELEASE.2026-10-26T00-00-00Z + command: ["server", "/data", "--console-address", ":9001"] + environment: + MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user + MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password + secrets: [minio_root_user, minio_root_password] + volumes: [s3data:/data] + healthcheck: + test: ["CMD", "mc", "ready", "local"] + interval: 5s + timeout: 3s + retries: 20 + networks: [backend] + deploy: + resources: + limits: { cpus: "${S3_CPUS:-1.0}", memory: "${S3_MEM:-512M}" } + + s3-init: + image: pgsty/silo:RELEASE.2026-10-26T00-00-00Z + depends_on: + s3: { condition: service_healthy } + environment: + MINIO_ROOT_USER_FILE: /run/secrets/minio_root_user + MINIO_ROOT_PASSWORD_FILE: /run/secrets/minio_root_password + S3_ACCESS_KEY_ID_FILE: /run/secrets/s3_access_key_id + S3_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_secret_access_key + entrypoint: ["/bin/sh", "/bootstrap/bootstrap.sh"] + secrets: + - minio_root_user + - minio_root_password + - s3_access_key_id + - s3_secret_access_key + volumes: + - ./infra/minio/bootstrap.sh:/bootstrap/bootstrap.sh:ro + - /tmp/silo-init-data:/data + networks: [backend] + deploy: + replicas: 1 + restart_policy: { condition: "no" } + + api: + <<: *api + deploy: + replicas: ${API_REPLICAS:-2} + resources: + limits: + cpus: "${API_CPU_PER_REPLICA:-0.5}" + memory: "${API_MEM_PER_REPLICA:-128M}" + + migrate: + <<: *api + entrypoint: ["/app/migrate.sh"] + depends_on: + postgres: { condition: service_healthy } + deploy: + replicas: 1 + restart_policy: + condition: "no" + + lb: + image: nginx:1.27.0-alpine + volumes: + - ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro + ports: + - "8080:8080" + depends_on: [api] + networks: [backend] + +volumes: + pgdata: + redisdata: + s3data: + +networks: + backend: + driver: bridge + +secrets: + postgres_password: { file: ./secrets/postgres_password.txt } + database_url: { file: ./secrets/database_url.txt } + cache_url: { file: ./secrets/cache_url.txt } + s3_access_key_id: { file: ./secrets/s3_access_key_id.txt } + s3_secret_access_key: { file: ./secrets/s3_secret_access_key.txt } + minio_root_user: { file: ./secrets/minio_root_user.txt } + minio_root_password: { file: ./secrets/minio_root_password.txt } \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..ce04daa2 --- /dev/null +++ b/go.mod @@ -0,0 +1,30 @@ +module mtusi-messenger + +go 1.22 + +require ( + github.com/google/uuid v1.6.0 + github.com/jackc/pgx/v5 v5.5.5 + github.com/minio/minio-go/v7 v7.0.72 + github.com/redis/go-redis/v9 v9.5.1 + golang.org/x/crypto v0.23.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.2.0 // indirect + github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect + github.com/dustin/go-humanize v1.0.1 // indirect + github.com/goccy/go-json v0.10.2 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/puddle/v2 v2.2.1 // indirect + github.com/klauspost/compress v1.17.6 // indirect + github.com/klauspost/cpuid/v2 v2.2.6 // indirect + github.com/minio/md5-simd v1.1.2 // indirect + github.com/rs/xid v1.5.0 // indirect + golang.org/x/net v0.23.0 // indirect + golang.org/x/sync v0.1.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + gopkg.in/ini.v1 v1.67.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..9a1cd55c --- /dev/null +++ b/go.sum @@ -0,0 +1,62 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44= +github.com/cespare/xxhash/v2 v2.2.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 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78= +github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= +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/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU= +github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= +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-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= +github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= +github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/klauspost/compress v1.17.6 h1:60eq2E/jlfwQXtvZEeBUYADs+BwKBWURIY+Gj2eRGjI= +github.com/klauspost/compress v1.17.6/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= +github.com/klauspost/cpuid/v2 v2.2.6 h1:ndNyv040zDGIDh8thGkXYjnFtiN02M1PVVF+JE/48xc= +github.com/klauspost/cpuid/v2 v2.2.6/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +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.0.72 h1:ZSbxs2BfJensLyHdVOgHv+pfmvxYraaUy07ER04dWnA= +github.com/minio/minio-go/v7 v7.0.72/go.mod h1:4yBA8v80xGA30cfM3fz0DKYMXunWl/AV/6tWEs9ryzo= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8= +github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M= +github.com/rs/xid v1.5.0 h1:mKX4bl4iPYJtEIxp6CYiUuLQ/8DYMoz0PUdtGgMFRVc= +github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= +github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA= +gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/infra/minio/bootstrap.sh b/infra/minio/bootstrap.sh new file mode 100644 index 00000000..45aed49e --- /dev/null +++ b/infra/minio/bootstrap.sh @@ -0,0 +1,52 @@ +#!/bin/sh +set -eu + +MINIO_ROOT_USER_FILE="${MINIO_ROOT_USER_FILE:-/run/secrets/minio_root_user}" +MINIO_ROOT_PASSWORD_FILE="${MINIO_ROOT_PASSWORD_FILE:-/run/secrets/minio_root_password}" +S3_ENDPOINT="${S3_ENDPOINT:-http://s3:9000}" +S3_BUCKET="${S3_BUCKET:-messenger-attachments}" +API_USER_FILE="${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}" +API_PASS_FILE="${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}" + +for f in "$MINIO_ROOT_USER_FILE" "$MINIO_ROOT_PASSWORD_FILE"; do + [ -f "$f" ] || { echo "ERROR: missing $f" >&2; exit 1; } +done + +ROOT_USER="$(cat "$MINIO_ROOT_USER_FILE")" +ROOT_PASS="$(cat "$MINIO_ROOT_PASSWORD_FILE")" + +mc alias set local "$S3_ENDPOINT" "$ROOT_USER" "$ROOT_PASS" >/dev/null + +# 1) bucket: приватный, создаётся один раз +mc mb --ignore-existing "local/$S3_BUCKET" >/dev/null +mc anonymous set none "local/$S3_BUCKET" >/dev/null + +# 2) ограниченный пользователь API (если креды заданы заранее) +if [ -s "$API_USER_FILE" ] && [ -s "$API_PASS_FILE" ]; then + API_USER="$(cat "$API_USER_FILE")" + API_PASS="$(cat "$API_PASS_FILE")" + if ! mc admin user info local "$API_USER" >/dev/null 2>&1; then + mc admin user add local "$API_USER" "$API_PASS" >/dev/null + fi + # Политика: только PutObject/GetObject/DeleteObject в bucket. + cat >/tmp/api-policy.json </dev/null 2>&1 || true + mc admin policy attach local api-attachments --user "$API_USER" >/dev/null 2>&1 || true +else + echo "WARN: API S3 credentials not pre-created; generate and place them in secrets/" >&2 +fi + +echo "s3 bootstrap done (bucket=$S3_BUCKET)" \ No newline at end of file diff --git a/infra/nginx/nginx.conf b/infra/nginx/nginx.conf new file mode 100644 index 00000000..9d5c3ede --- /dev/null +++ b/infra/nginx/nginx.conf @@ -0,0 +1,29 @@ +worker_processes auto; +events { worker_connections 4096; } + +http { + # Docker's embedded DNS. nginx будет обращаться сюда за IP реплик. + resolver 127.0.0.11 valid=2s ipv6=off; + resolver_timeout 5s; + + server { + listen 8080; + client_max_body_size 12m; + + location / { + set $api_backend http://api:8080; + + proxy_pass $api_backend; + proxy_http_version 1.1; + proxy_set_header Connection ""; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + + proxy_connect_timeout 2s; + proxy_send_timeout 15s; + proxy_read_timeout 15s; + + proxy_next_upstream error timeout http_502 http_503 http_504; + } + } +} \ No newline at end of file diff --git a/migrations/0001_init.sql b/migrations/0001_init.sql new file mode 100644 index 00000000..3925e4ac --- /dev/null +++ b/migrations/0001_init.sql @@ -0,0 +1,31 @@ +CREATE TABLE IF NOT EXISTS users ( + id UUID PRIMARY KEY, + username TEXT NOT NULL UNIQUE, + display_name TEXT NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE TABLE IF NOT EXISTS chats ( + id UUID PRIMARY KEY, + title TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE TABLE IF NOT EXISTS 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 IF NOT EXISTS 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 TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +-- Индекс истории: ровно под ORDER BY (created_at, id) и keyset-курсор. +CREATE INDEX IF NOT EXISTS idx_messages_chat_created_id + ON messages (chat_id, created_at, id); \ No newline at end of file diff --git a/migrations/0002_sessions.sql b/migrations/0002_sessions.sql new file mode 100644 index 00000000..f77b30d7 --- /dev/null +++ b/migrations/0002_sessions.sql @@ -0,0 +1,9 @@ +CREATE TABLE IF NOT EXISTS sessions ( + token TEXT PRIMARY KEY, + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + expires_at TIMESTAMPTZ NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS idx_sessions_user ON sessions (user_id); +CREATE INDEX IF NOT EXISTS idx_sessions_expires ON sessions (expires_at); \ No newline at end of file diff --git a/migrations/0003_messages_alt_index.sql b/migrations/0003_messages_alt_index.sql new file mode 100644 index 00000000..12d58f11 --- /dev/null +++ b/migrations/0003_messages_alt_index.sql @@ -0,0 +1,5 @@ +-- Индекс B для «стоимости записи»: +-- покрывающий индекс, включающий text — растёт быстрее, помогает только +-- покрывающим запросам. +CREATE INDEX IF NOT EXISTS idx_messages_chat_covering + ON messages (chat_id, created_at, id) INCLUDE (sender_id, text); \ No newline at end of file diff --git a/migrations/0004_idempotency.sql b/migrations/0004_idempotency.sql new file mode 100644 index 00000000..da621207 --- /dev/null +++ b/migrations/0004_idempotency.sql @@ -0,0 +1,14 @@ +CREATE TABLE IF NOT EXISTS idempotency_keys ( + user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + method TEXT NOT NULL, + path TEXT NOT NULL, + key TEXT NOT NULL, + request_hash TEXT NOT NULL, + status_code INTEGER NOT NULL, + response JSONB NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + PRIMARY KEY (user_id, method, path, key) +); + +CREATE INDEX IF NOT EXISTS idx_idempotency_created + ON idempotency_keys (created_at); \ No newline at end of file diff --git a/migrations/0005_attachments.sql b/migrations/0005_attachments.sql new file mode 100644 index 00000000..d8765167 --- /dev/null +++ b/migrations/0005_attachments.sql @@ -0,0 +1,30 @@ +CREATE TABLE IF NOT EXISTS attachments ( + id UUID PRIMARY KEY, + chat_id UUID NOT NULL REFERENCES chats(id) ON DELETE CASCADE, + owner_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE, + object_key TEXT NOT NULL UNIQUE, + filename TEXT NOT NULL, + content_type TEXT NOT NULL CHECK (content_type IN ('image/png','image/jpeg')), + size_bytes BIGINT NOT NULL CHECK (size_bytes > 0 AND size_bytes <= 10485760), + sha256 TEXT NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'), + width INTEGER NOT NULL CHECK (width > 0), + height INTEGER NOT NULL CHECK (height > 0), + status TEXT NOT NULL DEFAULT 'ready' CHECK (status IN ('pending','ready','failed')), + error_code TEXT, + created_at TIMESTAMPTZ NOT NULL DEFAULT now(), + updated_at TIMESTAMPTZ NOT NULL DEFAULT now() +); + +CREATE INDEX IF NOT EXISTS idx_attachments_chat ON attachments (chat_id, created_at, id); +CREATE INDEX IF NOT EXISTS idx_attachments_owner ON attachments (owner_id); +-- Для reconciler/поиска «зависших» pending-записей: +CREATE INDEX IF NOT EXISTS idx_attachments_pending ON attachments (created_at) WHERE status <> 'ready'; + +ALTER TABLE messages ADD COLUMN IF NOT EXISTS attachment_ids UUID[] NOT NULL DEFAULT '{}'; + +-- Журнал компенсации: объекты S3, которые могли остаться без метаданных. +CREATE TABLE IF NOT EXISTS attachment_gc ( + object_key TEXT PRIMARY KEY, + reason TEXT NOT NULL, + created_at TIMESTAMPTZ NOT NULL DEFAULT now() +); \ No newline at end of file diff --git a/scripts/migrate.sh b/scripts/migrate.sh new file mode 100644 index 00000000..5d3457a3 --- /dev/null +++ b/scripts/migrate.sh @@ -0,0 +1,13 @@ +#!/bin/sh +set -eu +# Применяет миграции к БД, читая DATABASE_URL из файла секрета. +# Устойчиво к повторному запуску (CREATE ... IF NOT EXISTS). +URL_FILE="${DATABASE_URL_FILE:-/run/secrets/database_url}" +DATABASE_URL="$(cat "$URL_FILE")" +PSQL="psql $DATABASE_URL -v ON_ERROR_STOP=1 -q" + +for f in migrations/*.sql; do + echo "applying $f" + $PSQL -f "$f" +done +echo "migrations done" \ No newline at end of file diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100644 index 00000000..742babeb --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,26 @@ +#!/bin/sh +set -eu + +# ./scripts/restart.sh # 2 реплики (по умолчанию) +# ./scripts/restart.sh 4 # 4 реплики +REPLICAS="${1:-2}" + +# Останов с сохранением volumes. +docker compose down --remove-orphans + +# Пересоздание сети + контейнеров с нужным числом реплик. +API_REPLICAS="$REPLICAS" \ + docker compose --env-file .env -f compose.yaml up -d --build + +# Ждём готовности с хоста. +i=0 +while [ "$i" -lt 90 ]; do + if curl -sf http://localhost:8080/health/ready >/dev/null 2>&1; then + echo "ready after ${i}s (replicas=$REPLICAS)" + exit 0 + fi + i=$((i + 1)) + sleep 1 +done +echo "stack did not become ready in 90s" >&2 +exit 1 \ No newline at end of file diff --git a/scripts/restore-api.sh b/scripts/restore-api.sh new file mode 100644 index 00000000..266c2840 --- /dev/null +++ b/scripts/restore-api.sh @@ -0,0 +1,36 @@ +#!/bin/sh +set -eu + +# Все контейнеры сервиса api (включая Exited). +# `docker compose ps -a -q api` возвращает их ID. +STOPPED="" +for id in $(docker compose ps -a -q api); do + state=$(docker inspect --format '{{.State.Status}}' "$id") + if [ "$state" = "exited" ] || [ "$state" = "created" ]; then + STOPPED="$STOPPED $id" + fi +done + +if [ -z "$STOPPED" ]; then + echo "all api containers already running" + exit 0 +fi + +for id in $STOPPED; do + hn=$(docker inspect --format '{{.Config.Hostname}}' "$id") + docker start "$id" >/dev/null + echo "started $id ($hn)" +done + +# Дождаться, пока LB увидит их снова (curl с хоста). +i=0 +while [ "$i" -lt 30 ]; do + if curl -sf http://localhost:8080/health/ready >/dev/null 2>&1; then + echo "ready after ${i}s" + exit 0 + fi + i=$((i + 1)) + sleep 1 +done +echo "stack did not become ready in 30s" >&2 +exit 1 \ No newline at end of file diff --git a/scripts/stop-one.sh b/scripts/stop-one.sh new file mode 100644 index 00000000..370c3476 --- /dev/null +++ b/scripts/stop-one.sh @@ -0,0 +1,20 @@ +#!/bin/sh +set -eu +: "${TARGET_INSTANCE_ID:?test provides target API instance alias}" + +# Находим ID контейнера, чьё имя (HOSTNAME) совпадает с TARGET_INSTANCE_ID. +CID=$(docker compose ps -q api | while read -r id; do + name=$(docker inspect --format '{{.Config.Hostname}}' "$id") + if [ "$name" = "$TARGET_INSTANCE_ID" ]; then + echo "$id" + break + fi +done) + +if [ -z "$CID" ]; then + echo "container with hostname $TARGET_INSTANCE_ID not found" >&2 + exit 2 +fi + +docker stop "$CID" +echo "stopped $CID ($TARGET_INSTANCE_ID)" \ No newline at end of file diff --git a/src/api/auth.go b/src/api/auth.go new file mode 100644 index 00000000..be30c192 --- /dev/null +++ b/src/api/auth.go @@ -0,0 +1,79 @@ +package api + +import ( + "context" + "encoding/base64" + "errors" + "net/http" + "strings" + + "mtusi-messenger/src/domain" +) + +// bearerAuth извлекает токен, проверяет сессию и кладёт пользователя в контекст. +// Basic на бизнес-эндпоинтах не принимается. X-User-Id игнорируется. +func (h *Handler) bearerAuth(next http.HandlerFunc) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + header := r.Header.Get("Authorization") + if !strings.HasPrefix(header, "Bearer ") { + w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`) + writeError(w, r, http.StatusUnauthorized, "unauthorized", "missing bearer token") + return + } + token := strings.TrimSpace(strings.TrimPrefix(header, "Bearer ")) + if token == "" { + w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`) + writeError(w, r, http.StatusUnauthorized, "unauthorized", "missing bearer token") + return + } + + u, err := h.Sessions.Lookup(r.Context(), token) + switch { + case err == nil: + // ok + case errors.Is(err, domain.ErrUnauthorized): + w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`) + writeError(w, r, http.StatusUnauthorized, "unauthorized", "invalid session") + return + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "session store timeout") + return + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "session store unavailable") + return + } + + ctx := context.WithValue(r.Context(), userKey, *u) + ctx = context.WithValue(ctx, tokenKey, token) + next(w, r.WithContext(ctx)) + } +} + +func userFrom(r *http.Request) (domain.User, bool) { + u, ok := r.Context().Value(userKey).(domain.User) + return u, ok +} + +func tokenFrom(r *http.Request) string { + if v, ok := r.Context().Value(tokenKey).(string); ok { + return v + } + return "" +} + +// parseBasicCredentials разбирает Authorization: Basic base64(username:password). +func parseBasicCredentials(header string) (string, string, error) { + if !strings.HasPrefix(header, "Basic ") { + return "", "", errors.New("not basic") + } + raw := strings.TrimSpace(strings.TrimPrefix(header, "Basic ")) + decoded, err := base64.StdEncoding.DecodeString(raw) + if err != nil { + return "", "", errors.New("invalid base64") + } + parts := strings.SplitN(string(decoded), ":", 2) + if len(parts) != 2 { + return "", "", errors.New("missing colon") + } + return parts[0], parts[1], nil +} diff --git a/src/api/errors.go b/src/api/errors.go new file mode 100644 index 00000000..7c5b5df8 --- /dev/null +++ b/src/api/errors.go @@ -0,0 +1,28 @@ +package api + +import ( + "encoding/json" + "net/http" + + "mtusi-messenger/src/domain" +) + +// writeError пишет ошибку в формате контракта. +func writeError(w http.ResponseWriter, r *http.Request, status int, code, message string) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(domain.ErrorBody{ + Error: domain.ErrorDetail{ + Code: code, + Message: message, + RequestID: requestIDFrom(r), + }, + }) +} + +// writeJSON пишет успешный JSON-ответ. +func writeJSON(w http.ResponseWriter, status int, v any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + _ = json.NewEncoder(w).Encode(v) +} diff --git a/src/api/handlers.go b/src/api/handlers.go new file mode 100644 index 00000000..eee49f6d --- /dev/null +++ b/src/api/handlers.go @@ -0,0 +1,537 @@ +package api + +import ( + "bytes" + "encoding/json" + "errors" + "io" + "log" + "net/http" + "regexp" + "strconv" + "strings" + + "mtusi-messenger/src/domain" + + "github.com/google/uuid" +) + +var usernameRe = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) +var idempotencyKeyRe = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`) + +type Handler struct { + Store *domain.Store + Sessions *domain.SessionStore + S3 *domain.S3Store + MaxUpload int64 +} + +// --- Health --- + +func (h *Handler) Live(w http.ResponseWriter, r *http.Request) { + writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) +} + +// Ready проверяет БД (обязательная зависимость). Redis — кэш, его недоступность +// не должна ронять readiness: API может работать, обходя кэш. +func (h *Handler) Ready(w http.ResponseWriter, r *http.Request) { + if err := h.Store.Ping(r.Context()); err != nil { + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + writeJSON(w, http.StatusOK, map[string]string{"status": "ok"}) +} + +// --- Users --- + +type createUserReq struct { + Username string `json:"username"` + DisplayName string `json:"display_name"` + Password string `json:"password"` +} + +func (h *Handler) CreateUser(w http.ResponseWriter, r *http.Request) { + var req createUserReq + if err := decodeSingleJSON(r.Body, &req); err != nil { + writeError(w, r, http.StatusBadRequest, "invalid_request", "malformed JSON") + return + } + if !usernameRe.MatchString(req.Username) { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid username") + return + } + if n := len([]rune(req.DisplayName)); n < 1 || n > 100 || strings.TrimSpace(req.DisplayName) == "" { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid display_name") + return + } + // Пароль: 12–128 code points, не состоит только из пробелов. + if n := len([]rune(req.Password)); n < 12 || n > 128 || strings.TrimSpace(req.Password) == "" { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid password") + return + } + u, err := h.Store.CreateUser(r.Context(), req.Username, req.DisplayName, req.Password) + switch { + case err == nil: + // ok + case errors.Is(err, domain.ErrConflict): + writeError(w, r, http.StatusConflict, "conflict", "username taken") + return + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout") + return + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + writeJSON(w, http.StatusCreated, u) +} + +func (h *Handler) CurrentUser(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + u, _ := userFrom(r) + writeJSON(w, http.StatusOK, u) + })(w, r) +} + +// --- Auth --- + +func (h *Handler) CreateSession(w http.ResponseWriter, r *http.Request) { + username, password, err := parseBasicCredentials(r.Header.Get("Authorization")) + if err != nil { + w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`) + writeError(w, r, http.StatusUnauthorized, "unauthorized", "invalid credentials") + return + } + u, err := h.Store.VerifyPassword(r.Context(), username, password) + switch { + case err == nil: + // ok + case errors.Is(err, domain.ErrUnauthorized): + w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`) + writeError(w, r, http.StatusUnauthorized, "unauthorized", "invalid credentials") + return + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout") + return + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + s, err := h.Sessions.Create(r.Context(), *u) + if err != nil { + if errors.Is(err, domain.ErrTimeout) { + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "session store timeout") + return + } + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "session store unavailable") + return + } + writeJSON(w, http.StatusCreated, s) +} + +func (h *Handler) RevokeSession(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + token := tokenFrom(r) + if err := h.Sessions.Revoke(r.Context(), token); err != nil { + if errors.Is(err, domain.ErrTimeout) { + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "session store timeout") + return + } + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "session store unavailable") + return + } + w.WriteHeader(http.StatusNoContent) + })(w, r) +} + +// --- Chats --- + +type createChatReq struct { + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` +} + +func (h *Handler) CreateChat(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + me, _ := userFrom(r) + var req createChatReq + if err := decodeSingleJSON(r.Body, &req); err != nil { + writeError(w, r, http.StatusBadRequest, "invalid_request", "malformed JSON") + return + } + if strings.TrimSpace(req.Title) == "" || len([]rune(req.Title)) > 100 { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid title") + return + } + if len(req.MemberIDs) < 1 || len(req.MemberIDs) > 100 { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid member_ids") + return + } + c, err := h.Store.CreateChat(r.Context(), req.Title, req.MemberIDs, me.ID) + switch { + case err == nil: + // ok + case errors.Is(err, domain.ErrInvalid): + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid members") + return + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout") + return + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + writeJSON(w, http.StatusCreated, c) + })(w, r) +} + +func (h *Handler) ListChats(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + me, _ := userFrom(r) + limit := 50 + if v := r.URL.Query().Get("limit"); v != "" { + n, err := strconv.Atoi(v) + if err != nil || n < 1 || n > 100 { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid limit") + return + } + limit = n + } + cursor := r.URL.Query().Get("cursor") + chats, next, err := h.Store.ListChats(r.Context(), me.ID, limit, cursor) + switch { + case err == nil: + case errors.Is(err, domain.ErrInvalid): + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid cursor") + return + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout") + return + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + writeJSON(w, http.StatusOK, domain.ChatPage{Items: chats, NextCursor: next}) + })(w, r) +} + +func (h *Handler) GetChat(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + me, _ := userFrom(r) + chatID := r.PathValue("chat_id") + c, err := h.Store.GetChat(r.Context(), chatID, me.ID) + switch { + case err == nil: + case errors.Is(err, domain.ErrInvalid): + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat_id") + return + case errors.Is(err, domain.ErrNotFound): + writeError(w, r, http.StatusNotFound, "not_found", "chat not found") + return + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout") + return + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + writeJSON(w, http.StatusOK, c) + })(w, r) +} + +// --- Messages --- + +type createMessageReq struct { + Text string `json:"text"` + AttachmentIDs []string `json:"attachment_ids"` +} + +func (h *Handler) CreateMessage(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + me, _ := userFrom(r) + chatID := r.PathValue("chat_id") + + // Idempotency-Key обязателен. + key := r.Header.Get("Idempotency-Key") + if key == "" { + writeError(w, r, http.StatusBadRequest, "invalid_request", "Idempotency-Key required") + return + } + if !idempotencyKeyRe.MatchString(key) { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid Idempotency-Key") + return + } + + if _, err := h.Store.GetChat(r.Context(), chatID, me.ID); err != nil { + switch { + case errors.Is(err, domain.ErrInvalid): + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat_id") + case errors.Is(err, domain.ErrNotFound): + writeError(w, r, http.StatusNotFound, "not_found", "chat not found") + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout") + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + } + return + } + + // Читаем тело один раз и хешируем нормализованный JSON. + rawBody, err := io.ReadAll(io.LimitReader(r.Body, 64*1024)) + if err != nil { + writeError(w, r, http.StatusBadRequest, "invalid_request", "cannot read body") + return + } + var req createMessageReq + if err := decodeSingleJSON(bytes.NewReader(rawBody), &req); err != nil { + writeError(w, r, http.StatusBadRequest, "invalid_request", "malformed JSON") + return + } + if len([]rune(req.Text)) > 4000 { + writeError(w, r, http.StatusBadRequest, "invalid_request", "message too long") + return + } + if strings.TrimSpace(req.Text) == "" && len(req.AttachmentIDs) == 0 { + writeError(w, r, http.StatusBadRequest, "invalid_request", "empty message") + return + } + bodyHash, err := domain.HashBody(rawBody) + if err != nil { + writeError(w, r, http.StatusBadRequest, "invalid_request", "malformed JSON") + return + } + if len(req.Text) > 0 && len([]rune(req.Text)) == 0 { + // всё пробелы, но вложения есть — допустимо + } + + attachmentIDs := req.AttachmentIDs + if attachmentIDs == nil { + attachmentIDs = []string{} + } + msg, rec, replay, err := h.Store.AddMessageIdempotent( + r.Context(), chatID, me.ID, req.Text, attachmentIDs, key, bodyHash) + switch { + case err == nil: + // ok + case errors.Is(err, domain.ErrNotFound): + writeError(w, r, http.StatusNotFound, "not_found", "chat not found") + return + case errors.Is(err, domain.ErrConflict): + writeError(w, r, http.StatusConflict, "conflict", "idempotency key reused with different body") + return + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout") + return + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + + if replay { + // Возвращаем сохранённый 201 и то же Message. + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(rec.StatusCode) + _, _ = w.Write(rec.Response) + return + } + writeJSON(w, http.StatusCreated, msg) + })(w, r) +} + +func (h *Handler) ListMessages(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + me, _ := userFrom(r) + chatID := r.PathValue("chat_id") + if _, err := h.Store.GetChat(r.Context(), chatID, me.ID); err != nil { + switch { + case errors.Is(err, domain.ErrInvalid): + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat_id") + case errors.Is(err, domain.ErrNotFound): + writeError(w, r, http.StatusNotFound, "not_found", "chat not found") + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database timeout") + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + } + return + } + limit := 50 + if v := r.URL.Query().Get("limit"); v != "" { + n, err := strconv.Atoi(v) + if err != nil || n < 1 || n > 100 { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid limit") + return + } + limit = n + } + cursor := r.URL.Query().Get("cursor") + items, next, err := h.Store.ListMessages(r.Context(), chatID, limit, cursor) + switch { + case err == nil: + // ok + case errors.Is(err, domain.ErrInvalid): + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid cursor") + return + case errors.Is(err, domain.ErrTimeout): + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "query timeout") + return + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + writeJSON(w, http.StatusOK, domain.MessagePage{Items: items, NextCursor: next}) + })(w, r) +} + +func decodeSingleJSON(r io.Reader, v any) error { + dec := json.NewDecoder(r) + dec.DisallowUnknownFields() + if err := dec.Decode(v); err != nil { + return err + } + if dec.More() { + return errors.New("trailing data after JSON value") + } + // Пробуем прочитать ещё один токен — если это не EOF, значит есть мусор. + var extra any + if err := dec.Decode(&extra); err != io.EOF { + return errors.New("trailing data after JSON value") + } + return nil +} + +// --- Attachments --- + +// safeFilename отбрасывает пути и опасные имена; используется только для отображения. +func safeFilename(name string) (string, bool) { + name = strings.TrimSpace(name) + if name == "" || len(name) > 255 { + return "", false + } + if strings.ContainsAny(name, "/\\") || strings.Contains(name, "..") { + return "", false + } + for _, r := range name { + if r < 0x20 || r == 0x7f { + return "", false + } + } + return name, true +} + +func (h *Handler) UploadAttachment(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + me, _ := userFrom(r) + chatID := r.PathValue("chat_id") + + if _, err := h.Store.GetChat(r.Context(), chatID, me.ID); err != nil { + switch { + case errors.Is(err, domain.ErrInvalid): + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid chat_id") + case errors.Is(err, domain.ErrNotFound): + writeError(w, r, http.StatusNotFound, "not_found", "chat not found") + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + } + return + } + + filename, ok := safeFilename(r.URL.Query().Get("filename")) + if !ok { + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid filename") + return + } + + objectKey := "attachments/" + chatID + "/" + uuid.NewString() + pendingID, err := h.Store.CreateAttachmentPending(r.Context(), chatID, me.ID, objectKey, filename) + if err != nil { + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + + res, err := h.S3.PutValidatedImage(r.Context(), objectKey, r.Body) + if err != nil { + _ = h.Store.MarkAttachmentFailed(r.Context(), pendingID, err.Error()) + _ = h.Store.ScheduleObjectGC(r.Context(), objectKey, "upload_failed") + switch { + case errors.Is(err, domain.ErrTooLarge): + writeError(w, r, http.StatusRequestEntityTooLarge, "payload_too_large", "file too large") + case errors.Is(err, domain.ErrUnsupported): + writeError(w, r, http.StatusUnsupportedMediaType, "unsupported_media_type", "only PNG/JPEG") + case errors.Is(err, domain.ErrNotImage): + writeError(w, r, http.StatusBadRequest, "invalid_request", "invalid image") + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "storage unavailable") + } + return + } + + att, err := h.Store.FinalizeAttachment(r.Context(), pendingID, res.ContentType, res.SizeBytes, res.SHA256, res.Width, res.Height) + if err != nil { + // S3 PUT прошёл, COMMIT — нет: объект в GC. + _ = h.Store.ScheduleObjectGC(r.Context(), objectKey, "finalize_failed") + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + writeJSON(w, http.StatusCreated, att) + })(w, r) +} + +func (h *Handler) GetAttachment(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + me, _ := userFrom(r) + att, err := h.Store.GetAttachmentForUser(r.Context(), r.PathValue("attachment_id"), me.ID) + switch { + case err == nil: + writeJSON(w, http.StatusOK, att) + case errors.Is(err, domain.ErrNotFound): + writeError(w, r, http.StatusNotFound, "not_found", "attachment not found") + default: + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + } + })(w, r) +} + +func (h *Handler) DownloadAttachment(w http.ResponseWriter, r *http.Request) { + h.bearerAuth(func(w http.ResponseWriter, r *http.Request) { + me, _ := userFrom(r) + id := r.PathValue("attachment_id") + att, err := h.Store.GetAttachmentForUser(r.Context(), id, me.ID) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + writeError(w, r, http.StatusNotFound, "not_found", "attachment not found") + return + } + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "database unavailable") + return + } + key, err := h.Store.GetAttachmentObjectKey(r.Context(), att.ID) + if err != nil { + writeError(w, r, http.StatusNotFound, "not_found", "attachment not found") + return + } + + rc, contentType, size, err := h.S3.GetObject(r.Context(), key) + if err != nil { + if errors.Is(err, domain.ErrNotFound) { + writeError(w, r, http.StatusNotFound, "not_found", "file missing in storage") + return + } + writeError(w, r, http.StatusServiceUnavailable, "unavailable", "storage unavailable") + return + } + defer rc.Close() + + w.Header().Set("Content-Type", contentType) + w.Header().Set("Content-Length", strconv.FormatInt(size, 10)) + w.Header().Set("Content-Disposition", `inline; filename="`+strings.ReplaceAll(att.Filename, `"`, "")+`"`) + w.WriteHeader(http.StatusOK) + + written, err := io.Copy(w, rc) + if err != nil || written != size { + // Соединение уже частично отправлено, но клиент получит обрезанный файл. + // Логируем это для отладки. + log.Printf("download truncated: written=%d expected=%d err=%v", written, size, err) + } + })(w, r) +} diff --git a/src/api/middleware.go b/src/api/middleware.go new file mode 100644 index 00000000..7cce92e1 --- /dev/null +++ b/src/api/middleware.go @@ -0,0 +1,46 @@ +package api + +import ( + "context" + "net/http" + + "github.com/google/uuid" +) + +type ctxKey string + +const ( + requestIDKey ctxKey = "request_id" + userKey ctxKey = "user" + tokenKey ctxKey = "token" +) + +func requestIDFrom(r *http.Request) string { + if v, ok := r.Context().Value(requestIDKey).(string); ok { + return v + } + return "" +} + +func withRequestID(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + id := r.Header.Get("X-Request-Id") + if id == "" { + id = uuid.NewString() + } + w.Header().Set("X-Request-Id", id) + ctx := context.WithValue(r.Context(), requestIDKey, id) + next.ServeHTTP(w, r.WithContext(ctx)) + }) +} + +// withInstanceID ставит X-Instance-Id на КАЖДЫЙ ответ (включая ошибки и health). +// ID генерируется один раз при старте процесса — стабилен в пределах жизни. +func withInstanceID(instanceID string) func(http.Handler) http.Handler { + return func(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("X-Instance-Id", instanceID) + next.ServeHTTP(w, r) + }) + } +} diff --git a/src/api/router.go b/src/api/router.go new file mode 100644 index 00000000..d5f9b317 --- /dev/null +++ b/src/api/router.go @@ -0,0 +1,24 @@ +package api + +import "net/http" + +func NewRouter(h *Handler, instanceID string) http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("GET /health/live", h.Live) + mux.HandleFunc("GET /health/ready", h.Ready) + mux.HandleFunc("POST /api/v1/users", h.CreateUser) + mux.HandleFunc("GET /api/v1/users/me", h.CurrentUser) + mux.HandleFunc("POST /api/v1/auth/sessions", h.CreateSession) + mux.HandleFunc("DELETE /api/v1/auth/sessions/current", h.RevokeSession) + mux.HandleFunc("POST /api/v1/chats", h.CreateChat) + mux.HandleFunc("GET /api/v1/chats", h.ListChats) + mux.HandleFunc("GET /api/v1/chats/{chat_id}", h.GetChat) + mux.HandleFunc("POST /api/v1/chats/{chat_id}/messages", h.CreateMessage) + mux.HandleFunc("GET /api/v1/chats/{chat_id}/messages", h.ListMessages) + mux.HandleFunc("POST /api/v1/chats/{chat_id}/attachments", h.UploadAttachment) + mux.HandleFunc("GET /api/v1/attachments/{attachment_id}", h.GetAttachment) + mux.HandleFunc("GET /api/v1/attachments/{attachment_id}/content", h.DownloadAttachment) + + // Порядок: instanceID снаружи, requestID внутри. + return withInstanceID(instanceID)(withRequestID(mux)) +} diff --git a/src/config/config.go b/src/config/config.go new file mode 100644 index 00000000..d9fa3e8c --- /dev/null +++ b/src/config/config.go @@ -0,0 +1,136 @@ +package config + +import ( + "crypto/rand" + "encoding/hex" + "os" + "strconv" + "time" +) + +type Config struct { + Host string + Port int + Env string + DatabaseURL string + CacheURL string + SessionTTL time.Duration + SessionStore string + RedisRequired bool + InstanceID string + S3Endpoint string + S3Region string + S3Bucket string + S3AccessKeyID string + S3SecretKey string + MaxUploadBytes int64 + MaxImagePixels int64 + S3GCGrace time.Duration +} + +func getenv(key, def string) string { + if v := os.Getenv(key); v != "" { + return v + } + return def +} + +func readSecretFromFile(path string) string { + if path == "" { + return "" + } + b, err := os.ReadFile(path) + if err != nil { + return "" + } + return string(b) +} + +func Load() Config { + port := 8080 + if v := os.Getenv("APP_PORT"); v != "" { + if p, err := strconv.Atoi(v); err == nil { + port = p + } + } + ttl := 3600 + if v := os.Getenv("SESSION_TTL_SECONDS"); v != "" { + if n, err := strconv.Atoi(v); err == nil && n > 0 { + ttl = n + } + } + + // DATABASE_URL и CACHE_URL могут прийти либо напрямую, либо через *_FILE. + dbURL := os.Getenv("DATABASE_URL") + if dbURL == "" { + dbURL = readSecretFromFile(os.Getenv("DATABASE_URL_FILE")) + } + cacheURL := os.Getenv("CACHE_URL") + if cacheURL == "" { + cacheURL = readSecretFromFile(os.Getenv("CACHE_URL_FILE")) + } + + instanceID := os.Getenv("INSTANCE_ID") + if instanceID == "" { + instanceID = os.Getenv("HOSTNAME") + } + if instanceID == "" { + instanceID = "api-" + randomShortID() + } + + store := getenv("SESSION_STORE", "postgres") + + s3Access := os.Getenv("S3_ACCESS_KEY_ID") + if s3Access == "" { + s3Access = readSecretFromFile(os.Getenv("S3_ACCESS_KEY_ID_FILE")) + } + s3Secret := os.Getenv("S3_SECRET_ACCESS_KEY") + if s3Secret == "" { + s3Secret = readSecretFromFile(os.Getenv("S3_SECRET_ACCESS_KEY_FILE")) + } + maxUpload := int64(10485760) + if v := os.Getenv("MAX_UPLOAD_BYTES"); v != "" { + if n, err := strconv.ParseInt(v, 10, 64); err == nil && n > 0 { + maxUpload = n + } + } + maxPixels := int64(40000000) + if v := os.Getenv("MAX_IMAGE_PIXELS"); v != "" { + if n, err := strconv.ParseInt(v, 10, 64); err == nil && n > 0 { + maxPixels = n + } + } + gcGrace := 600 * time.Second + if v := os.Getenv("S3_GC_GRACE_SECONDS"); v != "" { + if n, err := strconv.Atoi(v); err == nil && n > 0 { + gcGrace = time.Duration(n) * time.Second + } + } + return Config{ + Host: getenv("APP_HOST", "0.0.0.0"), + Port: port, + Env: getenv("APP_ENV", "lab"), + DatabaseURL: dbURL, + CacheURL: cacheURL, + SessionTTL: time.Duration(ttl) * time.Second, + SessionStore: store, + RedisRequired: store == "redis", + InstanceID: instanceID, + S3Endpoint: getenv("S3_ENDPOINT", ""), + S3Region: getenv("S3_REGION", "us-east-1"), + S3Bucket: getenv("S3_BUCKET", "messenger-attachments"), + S3AccessKeyID: s3Access, + S3SecretKey: s3Secret, + MaxUploadBytes: maxUpload, + MaxImagePixels: maxPixels, + S3GCGrace: gcGrace, + } +} + +func randomShortID() string { + b := make([]byte, 6) + if _, err := rand.Read(b); err != nil { + return "unknown" + } + return hex.EncodeToString(b) +} diff --git a/src/domain/attachments.go b/src/domain/attachments.go new file mode 100644 index 00000000..0a724bda --- /dev/null +++ b/src/domain/attachments.go @@ -0,0 +1,189 @@ +package domain + +import ( + "context" + "errors" + "time" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" +) + +// CreateAttachmentPending резервирует строку ДО записи в S3. +// Это даёт точку компенсации: если S3 PUT упал, строка снимается/чистится. +func (s *Store) CreateAttachmentPending(ctx context.Context, chatID, ownerID, objectKey, filename string) (string, error) { + id := uuid.NewString() + _, err := s.pool.Exec(ctx, + `INSERT INTO attachments (id, chat_id, owner_id, object_key, filename, content_type, size_bytes, sha256, width, height, status) + VALUES ($1,$2,$3,$4,$5,'image/png',1,repeat('0',64),1,1,'pending')`, + id, chatID, ownerID, objectKey, filename) + if err != nil { + return "", classifyDBError(err) + } + return id, nil +} + +// FinalizeAttachment атомарно переводит pending→ready с реальными метаданными. +func (s *Store) FinalizeAttachment(ctx context.Context, id, contentType string, size int64, sha string, w, h int) (*Attachment, error) { + tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{}) + if err != nil { + return nil, classifyDBError(err) + } + defer tx.Rollback(ctx) + + var a Attachment + var errCode *string + err = tx.QueryRow(ctx, + `UPDATE attachments + SET content_type=$2, size_bytes=$3, sha256=$4, width=$5, height=$6, + status='ready', error_code=NULL, updated_at=now() + WHERE id=$1 AND status='pending' + RETURNING id, chat_id, owner_id, filename, content_type, size_bytes, sha256, + status, error_code, created_at, updated_at`, + id, contentType, size, sha, w, h, + ).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256, + &a.Status, &errCode, &a.CreatedAt, &a.UpdatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + if err != nil { + return nil, classifyDBError(err) + } + a.ErrorCode = errCode + a.Variants = []Variant{} + if err := tx.Commit(ctx); err != nil { + return nil, classifyDBError(err) + } + return &a, nil +} + +// MarkAttachmentFailed помечает pending как failed (компенсация). +func (s *Store) MarkAttachmentFailed(ctx context.Context, id, code string) error { + _, err := s.pool.Exec(ctx, + `UPDATE attachments SET status='failed', error_code=$2, updated_at=now() + WHERE id=$1 AND status='pending'`, id, code) + return classifyDBError(err) +} + +// ScheduleObjectGC ставит object_key в очередь на удаление (grace period). +func (s *Store) ScheduleObjectGC(ctx context.Context, objectKey, reason string) error { + _, err := s.pool.Exec(ctx, + `INSERT INTO attachment_gc (object_key, reason) VALUES ($1,$2) + ON CONFLICT (object_key) DO NOTHING`, objectKey, reason) + return classifyDBError(err) +} + +// GetAttachmentForUser возвращает вложение, только если пользователь — участник чата. +func (s *Store) GetAttachmentForUser(ctx context.Context, id, userID string) (*Attachment, error) { + var a Attachment + var errCode *string + err := s.pool.QueryRow(ctx, ` + SELECT a.id, a.chat_id, a.owner_id, a.filename, a.content_type, a.size_bytes, a.sha256, + a.status, a.error_code, a.created_at, a.updated_at + FROM attachments a + JOIN chat_members m ON m.chat_id = a.chat_id + WHERE a.id=$1 AND m.user_id=$2 AND a.status='ready'`, id, userID, + ).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ContentType, &a.SizeBytes, &a.SHA256, + &a.Status, &errCode, &a.CreatedAt, &a.UpdatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + if err != nil { + return nil, classifyDBError(err) + } + a.ErrorCode = errCode + a.Variants = []Variant{} + return &a, nil +} + +// GetAttachmentObjectKey — только для внутреннего download после проверки ACL. +func (s *Store) GetAttachmentObjectKey(ctx context.Context, id string) (string, error) { + var key string + err := s.pool.QueryRow(ctx, `SELECT object_key FROM attachments WHERE id=$1`, id).Scan(&key) + if errors.Is(err, pgx.ErrNoRows) { + return "", ErrNotFound + } + if err != nil { + return "", classifyDBError(err) + } + return key, nil +} + +// ValidateAttachmentIDsForChat проверяет, что все вложения принадлежат чату, +// имеют статус ready и лимит 10. Возвращает ErrInvalid/ErrNotFound. +func (s *Store) ValidateAttachmentIDsForChat(ctx context.Context, chatID string, ids []string) error { + if len(ids) > 10 { + return ErrInvalid + } + seen := map[string]bool{} + for _, id := range ids { + if seen[id] { + return ErrInvalid + } + seen[id] = true + var ok bool + err := s.pool.QueryRow(ctx, + `SELECT true FROM attachments WHERE id=$1 AND chat_id=$2 AND status='ready'`, + id, chatID).Scan(&ok) + if errors.Is(err, pgx.ErrNoRows) { + return ErrNotFound + } + if err != nil { + return classifyDBError(err) + } + } + return nil +} + +// ReconcileOrphans: удаляет объекты из attachment_gc старше grace, которых нет в БД, +// и «призрачные» объекты pending старше grace. Вызывается из фонового тикера. +func (s *Store) ReconcileOrphans(ctx context.Context, s3 *S3Store, grace time.Duration) error { + // 1) GC-очередь: удалить объекты в S3, затем строку. + rows, err := s.pool.Query(ctx, + `SELECT object_key FROM attachment_gc WHERE created_at < now() - $1::interval LIMIT 100`, + grace.String()) + if err != nil { + return classifyDBError(err) + } + var keys []string + for rows.Next() { + var k string + if err := rows.Scan(&k); err != nil { + rows.Close() + return classifyDBError(err) + } + keys = append(keys, k) + } + rows.Close() + for _, k := range keys { + if err := s3.DeleteObject(ctx, k); err != nil { + continue + } + _, _ = s.pool.Exec(ctx, `DELETE FROM attachment_gc WHERE object_key=$1`, k) + } + + // 2) pending-записи старше grace: объект мог попасть в S3, а commit — нет. + prow, err := s.pool.Query(ctx, + `SELECT id, object_key FROM attachments + WHERE status='pending' AND created_at < now() - $1::interval LIMIT 100`, + grace.String()) + if err != nil { + return classifyDBError(err) + } + type pend struct{ id, key string } + var pendings []pend + for prow.Next() { + var p pend + if err := prow.Scan(&p.id, &p.key); err != nil { + prow.Close() + return classifyDBError(err) + } + pendings = append(pendings, p) + } + prow.Close() + for _, p := range pendings { + _ = s3.DeleteObject(ctx, p.key) + _, _ = s.pool.Exec(ctx, `DELETE FROM attachments WHERE id=$1 AND status='pending'`, p.id) + } + return nil +} diff --git a/src/domain/cache.go b/src/domain/cache.go new file mode 100644 index 00000000..45032295 --- /dev/null +++ b/src/domain/cache.go @@ -0,0 +1,91 @@ +package domain + +import ( + "context" + "encoding/json" + "time" + + "github.com/redis/go-redis/v9" +) + +// SessionCache — необязательный кэш сессий поверх Redis. +// Источник истины — PostgreSQL. Кэш ускоряет проверку токена. +type SessionCache struct { + rdb *redis.Client +} + +func NewSessionCache(ctx context.Context, url string) (*SessionCache, error) { + opt, err := redis.ParseURL(url) + if err != nil { + return nil, err + } + // Жёсткие короткие таймауты: Redis — кэш, а не критический путь. + opt.DialTimeout = 1 * time.Second + opt.ReadTimeout = 500 * time.Millisecond + opt.WriteTimeout = 500 * time.Millisecond + opt.MaxRetries = 0 // без автоматических ретраев: одна попытка — и уходим в БД + opt.PoolTimeout = 1 * time.Second + + rdb := redis.NewClient(opt) + + // Проверка связи при старте — короткая. + c, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + if err := rdb.Ping(c).Err(); err != nil { + _ = rdb.Close() + return nil, err + } + return &SessionCache{rdb: rdb}, nil +} + +func (c *SessionCache) userKey(userID string) string { return "user:" + userID } + +// opCtx возвращает контекст с коротким таймаутом для каждой операции. +// Не наследуем HTTP-контекст напрямую, чтобы не блокировать ответ клиенту. +func (c *SessionCache) opCtx() (context.Context, context.CancelFunc) { + return context.WithTimeout(context.Background(), 500*time.Millisecond) +} + +// SetUser кладёт публичные данные пользователя в кэш с TTL до конца сессии. +func (c *SessionCache) SetUser(ctx context.Context, userID string, u User, expires time.Time) { + ttl := time.Until(expires) + if ttl <= 0 { + return + } + b, err := json.Marshal(u) + if err != nil { + return + } + opCtx, cancel := c.opCtx() + defer cancel() + _ = c.rdb.Set(opCtx, c.userKey(userID), b, ttl).Err() +} + +func (c *SessionCache) GetUser(ctx context.Context, userID string) (User, bool) { + opCtx, cancel := c.opCtx() + defer cancel() + b, err := c.rdb.Get(opCtx, c.userKey(userID)).Bytes() + if err != nil { + return User{}, false + } + var u User + if err := json.Unmarshal(b, &u); err != nil { + return User{}, false + } + return u, true +} + +// DelUser — на случай удаления пользователя (в lab2 не вызывается). +func (c *SessionCache) DelUser(ctx context.Context, userID string) error { + opCtx, cancel := c.opCtx() + defer cancel() + return c.rdb.Del(opCtx, c.userKey(userID)).Err() +} + +func (c *SessionCache) Ping(ctx context.Context) error { + c2, cancel := context.WithTimeout(ctx, 1*time.Second) + defer cancel() + return c.rdb.Ping(c2).Err() +} + +func (c *SessionCache) Close() error { return c.rdb.Close() } diff --git a/src/domain/idempotency.go b/src/domain/idempotency.go new file mode 100644 index 00000000..f340f77d --- /dev/null +++ b/src/domain/idempotency.go @@ -0,0 +1,75 @@ +package domain + +import ( + "context" + "crypto/sha256" + "encoding/hex" + "encoding/json" + "errors" + "time" + + "github.com/jackc/pgx/v5" +) + +// IdempotencyRecord — сохранённый результат POST. +type IdempotencyRecord struct { + StatusCode int + Response json.RawMessage +} + +// LookupIdempotency возвращает запись, если ключ уже использован. +// Второй аргумент — совпадает ли хеш тела с сохранённым. +func (s *Store) LookupIdempotency(ctx context.Context, userID, method, path, key, bodyHash string) (*IdempotencyRecord, bool, error) { + var ( + storedHash string + rec IdempotencyRecord + ) + err := s.pool.QueryRow(ctx, + `SELECT request_hash, status_code, response + FROM idempotency_keys + WHERE user_id=$1 AND method=$2 AND path=$3 AND key=$4`, + userID, method, path, key, + ).Scan(&storedHash, &rec.StatusCode, &rec.Response) + if errors.Is(err, pgx.ErrNoRows) { + return nil, false, nil + } + if err != nil { + return nil, false, classifyDBError(err) + } + if storedHash != bodyHash { + return nil, true, ErrConflict + } + return &rec, true, nil +} + +// SaveIdempotency пишет результат. Конфликт PK означает, что параллельный +// запрос успел раньше — вызывающий должен перечитать через LookupIdempotency. +func (s *Store) SaveIdempotency(ctx context.Context, userID, method, path, key, bodyHash string, status int, resp json.RawMessage) error { + _, err := s.pool.Exec(ctx, + `INSERT INTO idempotency_keys (user_id, method, path, key, request_hash, status_code, response) + VALUES ($1,$2,$3,$4,$5,$6,$7)`, + userID, method, path, key, bodyHash, status, resp) + return classifyDBError(err) +} + +// HashBody — стабильный хеш JSON независимо от порядка ключей. +func HashBody(raw []byte) (string, error) { + var v any + if err := json.Unmarshal(raw, &v); err != nil { + return "", err + } + normalized, err := json.Marshal(v) // map keys сортируются + if err != nil { + return "", err + } + sum := sha256.Sum256(normalized) + return hex.EncodeToString(sum[:]), nil +} + +// CleanupIdempotency — можно звать из фонового тикера (опционально). +func (s *Store) CleanupIdempotency(ctx context.Context, olderThan time.Duration) error { + _, err := s.pool.Exec(ctx, + `DELETE FROM idempotency_keys WHERE created_at < now() - $1::interval`, + olderThan.String()) + return err +} diff --git a/src/domain/models.go b/src/domain/models.go new file mode 100644 index 00000000..9f427722 --- /dev/null +++ b/src/domain/models.go @@ -0,0 +1,92 @@ +package domain + +import ( + "errors" + "time" +) + +type User struct { + ID string `json:"id"` + Username string `json:"username"` + DisplayName string `json:"display_name"` + CreatedAt time.Time `json:"created_at"` +} + +type Chat struct { + ID string `json:"id"` + Title string `json:"title"` + MemberIDs []string `json:"member_ids"` + CreatedAt time.Time `json:"created_at"` +} + +type Message struct { + ID string `json:"id"` + ChatID string `json:"chat_id"` + SenderID string `json:"sender_id"` + Text string `json:"text"` + CreatedAt time.Time `json:"created_at"` + AttachmentIDs []string `json:"attachment_ids"` +} + +type MessagePage struct { + Items []Message `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +type ChatPage struct { + Items []Chat `json:"items"` + NextCursor *string `json:"next_cursor"` +} + +type Session struct { + Token string `json:"token"` + TokenType string `json:"token_type"` + ExpiresAt time.Time `json:"expires_at"` + User User `json:"user"` +} + +type ErrorBody struct { + Error ErrorDetail `json:"error"` +} + +type ErrorDetail struct { + Code string `json:"code"` + Message string `json:"message"` + RequestID string `json:"request_id"` +} + +type Variant 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 Attachment 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 []Variant `json:"variants"` + ErrorCode *string `json:"error_code"` + CreatedAt time.Time `json:"created_at"` + UpdatedAt time.Time `json:"updated_at"` +} + +var ( + ErrNotFound = errors.New("not found") + ErrConflict = errors.New("conflict") + ErrInvalid = errors.New("invalid request") + ErrUnauthorized = errors.New("unauthorized") + ErrTimeout = errors.New("timeout") + ErrUnavailable = errors.New("unavailable") + ErrTooLarge = errors.New("payload too large") + ErrUnsupported = errors.New("unsupported media type") + ErrNotImage = errors.New("not a valid image") +) diff --git a/src/domain/postgres.go b/src/domain/postgres.go new file mode 100644 index 00000000..3497f4c6 --- /dev/null +++ b/src/domain/postgres.go @@ -0,0 +1,608 @@ +package domain + +import ( + "context" + "errors" + "fmt" + "strings" + "time" + + "crypto/rand" + "crypto/subtle" + "encoding/base64" + "encoding/json" + + "github.com/google/uuid" + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" + "golang.org/x/crypto/argon2" +) + +type Store struct { + pool *pgxpool.Pool +} + +func NewStore(ctx context.Context, dsn string) (*Store, error) { + cfg, err := pgxpool.ParseConfig(dsn) + if err != nil { + return nil, classifyDBError(err) + } + cfg.MaxConns = 10 + cfg.MinConns = 1 + cfg.MaxConnLifetime = time.Hour + cfg.MaxConnIdleTime = 5 * time.Minute + cfg.ConnConfig.ConnectTimeout = 5 * time.Second + + cfg.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { + _, err := conn.Exec(ctx, ` + SET statement_timeout = '5s'; + SET lock_timeout = '2s'; + SET idle_in_transaction_session_timeout = '5s'; + `) + return err + } + + pool, err := pgxpool.NewWithConfig(ctx, cfg) + if err != nil { + return nil, classifyDBError(err) + } + ctxPing, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + if err := pool.Ping(ctxPing); err != nil { + pool.Close() + return nil, classifyDBError(err) + } + return &Store{pool: pool}, nil +} + +func (s *Store) Close() { s.pool.Close() } + +func (s *Store) Ping(ctx context.Context) error { + c, cancel := context.WithTimeout(ctx, 2*time.Second) + defer cancel() + return s.pool.Ping(c) +} + +// Argon2id параметры — OWASP baseline: m=19456 KiB (19 MiB), t=2, p=1. +// Хеш кодируется в формате PHC: $argon2id$v=19$m=19456,t=2,p=1$$ +const ( + argonMemory uint32 = 19456 + argonTime uint32 = 2 + argonThreads uint8 = 1 + argonKeyLen uint32 = 32 + argonSaltLen = 16 +) + +func hashPassword(password string) (string, error) { + salt := make([]byte, argonSaltLen) + if _, err := rand.Read(salt); err != nil { + return "", err + } + key := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen) + b64Salt := base64.RawStdEncoding.EncodeToString(salt) + b64Key := base64.RawStdEncoding.EncodeToString(key) + // PHC string; параметры можно будет менять в будущем, старые хеши останутся валидными. + return fmt.Sprintf("$argon2id$v=19$m=%d,t=%d,p=%d$%s$%s", + argonMemory, argonTime, argonThreads, b64Salt, b64Key), nil +} + +func verifyPassword(password, encoded string) bool { + // Минимальный парсер PHC-строки. + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false + } + var mem, time uint32 + var threads uint8 + if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &mem, &time, &threads); err != nil { + return false + } + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil { + return false + } + wantKey, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil { + return false + } + gotKey := argon2.IDKey([]byte(password), salt, time, mem, threads, uint32(len(wantKey))) + return subtle.ConstantTimeCompare(gotKey, wantKey) == 1 +} + +// --- Users --- + +func (s *Store) CreateUser(ctx context.Context, username, displayName, password string) (*User, error) { + hash, err := hashPassword(password) + if err != nil { + return nil, classifyDBError(err) + } + id := uuid.NewString() + var u User + err = s.pool.QueryRow(ctx, + `INSERT INTO users (id, username, display_name, password_hash) + VALUES ($1,$2,$3,$4) + RETURNING id, username, display_name, created_at`, + id, username, displayName, string(hash), + ).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt) + if err != nil { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) && pgErr.Code == "23505" { + return nil, ErrConflict + } + return nil, classifyDBError(err) + } + return &u, nil +} + +func (s *Store) GetUserByID(ctx context.Context, id string) (*User, error) { + var u User + err := s.pool.QueryRow(ctx, + `SELECT id, username, display_name, created_at FROM users WHERE id=$1`, id, + ).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + if err != nil { + return nil, classifyDBError(err) + } + return &u, nil +} + +// VerifyPassword возвращает пользователя, если пароль верный; иначе ErrUnauthorized. +func (s *Store) VerifyPassword(ctx context.Context, username, password string) (*User, error) { + var u User + var hash string + err := s.pool.QueryRow(ctx, + `SELECT id, username, display_name, created_at, password_hash FROM users WHERE username=$1`, + username, + ).Scan(&u.ID, &u.Username, &u.DisplayName, &u.CreatedAt, &hash) + if errors.Is(err, pgx.ErrNoRows) { + // Тратим сопоставимое время на проверку фиктивного хеша, + // чтобы не различать "нет пользователя" и "неверный пароль" по таймингу. + _ = verifyPassword(password, "$argon2id$v=19$m=19456,t=2,p=1$AAAAAAAAAAAAAAAAAAAAAA$AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA") + return nil, ErrUnauthorized + } + if err != nil { + return nil, classifyDBError(err) + } + if !verifyPassword(password, hash) { + return nil, ErrUnauthorized + } + return &u, nil +} + +// --- Chats --- + +func (s *Store) CreateChat(ctx context.Context, title string, memberIDs []string, creatorID string) (*Chat, error) { + // Проверка входных данных. + seen := map[string]bool{} + for _, id := range memberIDs { + if seen[id] { + return nil, ErrInvalid + } + seen[id] = true + } + if !seen[creatorID] { + return nil, ErrInvalid + } + + tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.ReadCommitted}) + if err != nil { + return nil, classifyDBError(err) + } + defer tx.Rollback(ctx) + + // Проверяем, что все участники существуют. + for id := range seen { + var exists bool + if err := tx.QueryRow(ctx, `SELECT true FROM users WHERE id=$1`, id).Scan(&exists); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrInvalid + } + return nil, classifyDBError(err) + } + } + + chatID := uuid.NewString() + var c Chat + err = tx.QueryRow(ctx, + `INSERT INTO chats (id, title) VALUES ($1,$2) RETURNING id, title, created_at`, + chatID, title, + ).Scan(&c.ID, &c.Title, &c.CreatedAt) + if err != nil { + return nil, classifyDBError(err) + } + + for id := range seen { + if _, err := tx.Exec(ctx, + `INSERT INTO chat_members (chat_id, user_id) VALUES ($1,$2)`, + c.ID, id, + ); err != nil { + return nil, classifyDBError(err) + } + } + + if err := tx.Commit(ctx); err != nil { + return nil, classifyDBError(err) + } + + // Порядок member_ids — как в запросе. + c.MemberIDs = append([]string(nil), memberIDs...) + return &c, nil +} + +func (s *Store) GetChat(ctx context.Context, chatID, userID string) (*Chat, error) { + var exists bool + err := s.pool.QueryRow(ctx, + `SELECT true FROM chat_members WHERE chat_id=$1 AND user_id=$2`, + chatID, userID, + ).Scan(&exists) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + if err != nil { + return nil, classifyDBError(err) + } + + var c Chat + err = s.pool.QueryRow(ctx, + `SELECT id, title, created_at FROM chats WHERE id=$1`, chatID, + ).Scan(&c.ID, &c.Title, &c.CreatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + if err != nil { + return nil, classifyDBError(err) + } + + rows, err := s.pool.Query(ctx, + `SELECT user_id FROM chat_members WHERE chat_id=$1 ORDER BY user_id`, chatID) + if err != nil { + return nil, classifyDBError(err) + } + defer rows.Close() + for rows.Next() { + var uid string + if err := rows.Scan(&uid); err != nil { + return nil, classifyDBError(err) + } + c.MemberIDs = append(c.MemberIDs, uid) + } + return &c, rows.Err() +} + +// ListChats возвращает чаты пользователя с keyset-пагинацией по (created_at, id). +func (s *Store) ListChats(ctx context.Context, userID string, limit int, cursor string) ([]Chat, *string, error) { + var rows pgx.Rows + var err error + + if cursor == "" { + rows, err = s.pool.Query(ctx, + `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 = $1 + ORDER BY c.created_at, c.id + LIMIT $2`, userID, limit+1) + } else { + var cCreated time.Time + if err := s.pool.QueryRow(ctx, + `SELECT created_at FROM chats WHERE id=$1`, cursor, + ).Scan(&cCreated); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, nil, ErrInvalid + } + return nil, nil, classifyDBError(err) + } + rows, err = s.pool.Query(ctx, + `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 = $1 + AND (c.created_at, c.id) > ($2, $3) + ORDER BY c.created_at, c.id + LIMIT $4`, userID, cCreated, cursor, limit+1) + } + if err != nil { + return nil, nil, classifyDBError(err) + } + defer rows.Close() + + var chats []Chat + for rows.Next() { + var c Chat + if err := rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil { + return nil, nil, classifyDBError(err) + } + chats = append(chats, c) + } + if err := rows.Err(); err != nil { + return nil, nil, classifyDBError(err) + } + + var next *string + if len(chats) > limit { + chats = chats[:limit] + last := chats[len(chats)-1].ID + next = &last + } + if chats == nil { + chats = []Chat{} + } + + // Догружаем участников. + for i := range chats { + mrows, err := s.pool.Query(ctx, + `SELECT user_id FROM chat_members WHERE chat_id=$1 ORDER BY user_id`, chats[i].ID) + if err != nil { + return nil, nil, classifyDBError(err) + } + for mrows.Next() { + var uid string + if err := mrows.Scan(&uid); err != nil { + mrows.Close() + return nil, nil, classifyDBError(err) + } + chats[i].MemberIDs = append(chats[i].MemberIDs, uid) + } + mrows.Close() + } + return chats, next, nil +} + +// --- Messages --- + +func (s *Store) AddMessage(ctx context.Context, chatID, senderID, text string) (*Message, error) { + // Проверка членства + вставка в одной транзакции. + tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.ReadCommitted}) + if err != nil { + return nil, classifyDBError(err) + } + defer tx.Rollback(ctx) + + var ok bool + if err := tx.QueryRow(ctx, + `SELECT true FROM chat_members WHERE chat_id=$1 AND user_id=$2`, + chatID, senderID, + ).Scan(&ok); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrNotFound + } + return nil, classifyDBError(err) + } + + var m Message + var attach []string + err = tx.QueryRow(ctx, + `INSERT INTO messages (id, chat_id, sender_id, text, attachment_ids) + VALUES ($1,$2,$3,$4,$5) + RETURNING id, chat_id, sender_id, text, created_at, attachment_ids`, + uuid.NewString(), chatID, senderID, text, []string{}, + ).Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &attach) + if err != nil { + return nil, classifyDBError(err) + } + if err := tx.Commit(ctx); err != nil { + return nil, classifyDBError(err) + } + m.AttachmentIDs = attach + if m.AttachmentIDs == nil { + m.AttachmentIDs = []string{} + } + return &m, nil +} + +// ListMessages — keyset-пагинация по (created_at, id). +// Курсор — это id последнего элемента предыдущей страницы; сам id уникален, +// поэтому используем его для поиска (created_at, id) в БД. +func (s *Store) ListMessages(ctx context.Context, chatID string, limit int, cursor string) ([]Message, *string, error) { + var rows pgx.Rows + var err error + + if cursor == "" { + rows, err = s.pool.Query(ctx, + `SELECT id, chat_id, sender_id, text, created_at, attachment_ids + FROM messages + WHERE chat_id=$1 + ORDER BY created_at, id + LIMIT $2`, chatID, limit+1) + } else { + // Определяем позицию курсора. + var cCreated time.Time + if err := s.pool.QueryRow(ctx, + `SELECT created_at FROM messages WHERE id=$1 AND chat_id=$2`, + cursor, chatID, + ).Scan(&cCreated); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, nil, ErrInvalid + } + return nil, nil, classifyDBError(err) + } + rows, err = s.pool.Query(ctx, + `SELECT id, chat_id, sender_id, text, created_at, attachment_ids + FROM messages + WHERE chat_id=$1 + AND (created_at, id) > ($2, $3) + ORDER BY created_at, id + LIMIT $4`, chatID, cCreated, cursor, limit+1) + } + if err != nil { + return nil, nil, classifyDBError(err) + } + defer rows.Close() + + var items []Message + for rows.Next() { + var m Message + if err := rows.Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &m.AttachmentIDs); err != nil { + return nil, nil, classifyDBError(err) + } + if m.AttachmentIDs == nil { + m.AttachmentIDs = []string{} + } + items = append(items, m) + } + if err := rows.Err(); err != nil { + return nil, nil, classifyDBError(err) + } + + var next *string + if len(items) > limit { + items = items[:limit] + last := items[len(items)-1].ID + next = &last + } + if items == nil { + items = []Message{} + } + return items, next, nil +} + +func classifyDBError(err error) error { + if err == nil { + return nil + } + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) { + switch pgErr.Code { + case "57014": // query_canceled (statement_timeout) + return ErrTimeout + case "55P03": // lock_not_available (lock_timeout) + return ErrTimeout + case "40001": // serialization_failure + return ErrUnavailable + case "53300": // too_many_connections + return ErrUnavailable + case "22P02", "22007", "22023": // invalid_text_representation / invalid_datetime_format / invalid_parameter_value + return ErrInvalid + } + } + if errors.Is(err, context.DeadlineExceeded) || errors.Is(err, context.Canceled) { + return ErrTimeout + } + if errors.Is(err, pgx.ErrNoRows) { + return ErrNotFound + } + return err +} + +// AddMessageIdempotent — атомарно проверяет членство, вставляет сообщение +// и сохраняет результат идемпотентности в одной транзакции. +// Если ключ уже есть с тем же body — возвращает сохранённый ответ и replay=true. +func (s *Store) AddMessageIdempotent( + ctx context.Context, + chatID, senderID, text string, + attachmentIDs []string, + key, bodyHash string, +) (msg *Message, rec *IdempotencyRecord, replay bool, err error) { + path := "/api/v1/chats/" + chatID + "/messages" + + tx, err := s.pool.BeginTx(ctx, pgx.TxOptions{IsoLevel: pgx.Serializable}) + if err != nil { + return nil, nil, false, classifyDBError(err) + } + defer tx.Rollback(ctx) + + // 1) Членство. + var ok bool + if err := tx.QueryRow(ctx, + `SELECT true FROM chat_members WHERE chat_id=$1 AND user_id=$2`, + chatID, senderID, + ).Scan(&ok); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, nil, false, ErrNotFound + } + return nil, nil, false, classifyDBError(err) + } + + // Вложения должны принадлежать этому чату и быть ready. + if len(attachmentIDs) > 10 { + return nil, nil, false, ErrInvalid + } + seen := map[string]bool{} + for _, aid := range attachmentIDs { + if seen[aid] { + return nil, nil, false, ErrInvalid + } + seen[aid] = true + var ok bool + if err := tx.QueryRow(ctx, + `SELECT true FROM attachments WHERE id=$1 AND chat_id=$2 AND status='ready'`, + aid, chatID).Scan(&ok); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, nil, false, ErrNotFound + } + return nil, nil, false, classifyDBError(err) + } + } + + // 2) Уже был такой ключ? + var storedHash string + var storedStatus int + var storedResp json.RawMessage + err = tx.QueryRow(ctx, + `SELECT request_hash, status_code, response + FROM idempotency_keys + WHERE user_id=$1 AND method='POST' AND path=$2 AND key=$3`, + senderID, path, key, + ).Scan(&storedHash, &storedStatus, &storedResp) + switch { + case err == nil: + if storedHash != bodyHash { + return nil, nil, false, ErrConflict + } + return nil, &IdempotencyRecord{StatusCode: storedStatus, Response: storedResp}, true, nil + case !errors.Is(err, pgx.ErrNoRows): + return nil, nil, false, classifyDBError(err) + } + + // 3) Вставка сообщения. + var m Message + var attach []string + err = tx.QueryRow(ctx, + `INSERT INTO messages (id, chat_id, sender_id, text, attachment_ids) + VALUES ($1,$2,$3,$4,$5) + RETURNING id, chat_id, sender_id, text, created_at, attachment_ids`, + uuid.NewString(), chatID, senderID, text, attachmentIDs, + ).Scan(&m.ID, &m.ChatID, &m.SenderID, &m.Text, &m.CreatedAt, &attach) + if err != nil { + // Гонка на одном ключе в разных репликах: unique violation в idempotency_keys + // маловероятен здесь, но serialization failure возможен. + return nil, nil, false, classifyDBError(err) + } + m.AttachmentIDs = attach + if m.AttachmentIDs == nil { + m.AttachmentIDs = []string{} + } + + // 4) Сохранение ответа. + respJSON, _ := json.Marshal(m) + _, err = tx.Exec(ctx, + `INSERT INTO idempotency_keys (user_id, method, path, key, request_hash, status_code, response) + VALUES ($1,'POST',$2,$3,$4,201,$5)`, + senderID, path, key, bodyHash, respJSON) + if err != nil { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) && pgErr.Code == "23505" { + // Кто-то параллельно успел вставить: перечитываем в этой же tx. + if err2 := tx.QueryRow(ctx, + `SELECT request_hash, status_code, response + FROM idempotency_keys + WHERE user_id=$1 AND method='POST' AND path=$2 AND key=$3`, + senderID, path, key, + ).Scan(&storedHash, &storedStatus, &storedResp); err2 == nil { + if storedHash != bodyHash { + return nil, nil, false, ErrConflict + } + return nil, &IdempotencyRecord{StatusCode: storedStatus, Response: storedResp}, true, nil + } + } + return nil, nil, false, classifyDBError(err) + } + + if err := tx.Commit(ctx); err != nil { + return nil, nil, false, classifyDBError(err) + } + return &m, nil, false, nil +} diff --git a/src/domain/s3.go b/src/domain/s3.go new file mode 100644 index 00000000..461c21fd --- /dev/null +++ b/src/domain/s3.go @@ -0,0 +1,213 @@ +package domain + +import ( + "bytes" + "context" + "crypto/sha256" + "encoding/hex" + "errors" + "fmt" + "hash" + "image" + _ "image/jpeg" + _ "image/png" + "io" + "net" + "net/http" + "os" + "strings" + "time" + + "github.com/minio/minio-go/v7" + "github.com/minio/minio-go/v7/pkg/credentials" +) + +type S3Store struct { + client *minio.Client + bucket string + maxBytes int64 + maxPixels int64 + tr *http.Transport +} + +func NewS3Store(ctx context.Context, endpoint, region, bucket, access, secret string, maxBytes, maxPixels int64) (*S3Store, error) { + if endpoint == "" || access == "" || secret == "" || bucket == "" { + return nil, errors.New("s3 configuration incomplete") + } + + tr := &http.Transport{ + MaxIdleConns: 100, + MaxIdleConnsPerHost: 100, + IdleConnTimeout: 30 * time.Second, + DialContext: (&net.Dialer{ + Timeout: 3 * time.Second, // Быстрый отказ при недоступности + KeepAlive: 30 * time.Second, + }).DialContext, + TLSHandshakeTimeout: 3 * time.Second, + ResponseHeaderTimeout: 5 * time.Second, + } + + cli, err := minio.New(endpoint, &minio.Options{ + Creds: credentials.NewStaticV4(access, secret, ""), + Secure: strings.HasPrefix(endpoint, "https://"), + Region: region, + Transport: tr, + }) + if err != nil { + return nil, err + } + + pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + if _, err := cli.BucketExists(pingCtx, bucket); err != nil { + return nil, fmt.Errorf("s3 bucket check: %w", err) + } + return &S3Store{client: cli, bucket: bucket, maxBytes: maxBytes, maxPixels: maxPixels, tr: tr}, nil +} + +type UploadResult struct { + ObjectKey string + ContentType string + SizeBytes int64 + SHA256 string + Width int + Height int +} + +// PutValidatedImage читает тело потоково во временный файл на диске, +// проверяет размер, MIME, пиксели и затем стримит в S3. +// Это гарантирует, что память не растёт с размером файла. +func (s *S3Store) PutValidatedImage(ctx context.Context, objectKey string, body io.Reader) (*UploadResult, error) { + // 1. Создаём временный файл. + tmpFile, err := os.CreateTemp("", "upload-*") + if err != nil { + return nil, err + } + tmpPath := tmpFile.Name() + defer func() { + tmpFile.Close() + os.Remove(tmpPath) + }() + + // 2. Копируем тело в файл с ограничением. + limited := io.LimitReader(body, s.maxBytes+1) + size, err := io.Copy(tmpFile, limited) + if err != nil { + return nil, err + } + if size > s.maxBytes { + return nil, ErrTooLarge + } + if size == 0 { + return nil, ErrUnsupported + } + + // 3. Проверяем MIME по первым 512 байтам. + if _, err := tmpFile.Seek(0, io.SeekStart); err != nil { + return nil, err + } + header := make([]byte, 512) + n, err := io.ReadFull(tmpFile, header) + if err != nil && err != io.ErrUnexpectedEOF && err != io.EOF { + return nil, err + } + header = header[:n] + ct := http.DetectContentType(header) + if ct != "image/png" && ct != "image/jpeg" { + return nil, ErrUnsupported + } + + // 4. Проверяем заголовок изображения (размеры). + // DecodeConfig читает только начало, но мы используем файл, поэтому это безопасно. + if _, err := tmpFile.Seek(0, io.SeekStart); err != nil { + return nil, err + } + cfg, _, err := image.DecodeConfig(tmpFile) + if err != nil { + return nil, ErrNotImage + } + if int64(cfg.Width)*int64(cfg.Height) > s.maxPixels { + return nil, ErrNotImage + } + + // 5. Считаем SHA-256 и стримим файл в S3. + if _, err := tmpFile.Seek(0, io.SeekStart); err != nil { + return nil, err + } + var hasher hash.Hash = sha256.New() + tee := io.TeeReader(tmpFile, hasher) + + putCtx, cancelPut := context.WithTimeout(ctx, 5*time.Second) + defer cancelPut() + + _, err = s.client.PutObject(putCtx, s.bucket, objectKey, tee, size, + minio.PutObjectOptions{ContentType: ct}) + if err != nil { + return nil, err + } + + sha := hex.EncodeToString(hasher.Sum(nil)) + + return &UploadResult{ + ObjectKey: objectKey, + ContentType: ct, + SizeBytes: size, + SHA256: sha, + Width: cfg.Width, + Height: cfg.Height, + }, nil +} + +func (s *S3Store) GetObject(ctx context.Context, objectKey string) (io.ReadCloser, string, int64, error) { + // Общий дедлайн на все попытки — 6 секунд. + overallCtx, overallCancel := context.WithTimeout(ctx, 6*time.Second) + defer overallCancel() + + var lastErr error + for attempt := 0; attempt < 2; attempt++ { + if attempt > 0 { + time.Sleep(500 * time.Millisecond) + s.tr.CloseIdleConnections() + } + + attemptCtx, cancel := context.WithTimeout(overallCtx, 3*time.Second) + obj, err := s.client.GetObject(attemptCtx, s.bucket, objectKey, minio.GetObjectOptions{}) + if err != nil { + cancel() + lastErr = err + continue + } + + info, err := obj.Stat() + if err != nil { + obj.Close() + cancel() + if minio.ToErrorResponse(err).Code == "NoSuchKey" { + return nil, "", 0, ErrNotFound + } + lastErr = err + continue + } + + data, err := io.ReadAll(obj) + obj.Close() + cancel() + if err != nil { + lastErr = err + continue + } + if int64(len(data)) != info.Size { + lastErr = fmt.Errorf("size mismatch: got %d, expected %d", len(data), info.Size) + continue + } + + return io.NopCloser(bytes.NewReader(data)), info.ContentType, info.Size, nil + } + + s.tr.CloseIdleConnections() + return nil, "", 0, fmt.Errorf("s3 get failed after retries: %w", lastErr) +} + +func (s *S3Store) DeleteObject(ctx context.Context, objectKey string) error { + return s.client.RemoveObject(ctx, s.bucket, objectKey, minio.RemoveObjectOptions{}) +} diff --git a/src/domain/sessions.go b/src/domain/sessions.go new file mode 100644 index 00000000..5eaf1786 --- /dev/null +++ b/src/domain/sessions.go @@ -0,0 +1,108 @@ +package domain + +import ( + "context" + "crypto/rand" + "encoding/base64" + "errors" + "time" + + "github.com/jackc/pgx/v5" +) + +type SessionStore struct { + store *Store + cache *SessionCache + ttl time.Duration +} + +func NewSessionStore(store *Store, cache *SessionCache, ttl time.Duration) *SessionStore { + return &SessionStore{store: store, cache: cache, ttl: ttl} +} + +func newToken() (string, error) { + b := make([]byte, 32) // 256 бит + if _, err := rand.Read(b); err != nil { + return "", classifyDBError(err) + } + return base64.RawURLEncoding.EncodeToString(b), nil +} + +// Create — пишем сессию в БД (источник истины) и кэшируем публичные данные User. +func (s *SessionStore) Create(ctx context.Context, user User) (*Session, error) { + token, err := newToken() + if err != nil { + return nil, classifyDBError(err) + } + expires := time.Now().UTC().Add(s.ttl) + if _, err := s.store.pool.Exec(ctx, + `INSERT INTO sessions (token, user_id, expires_at) VALUES ($1,$2,$3)`, + token, user.ID, expires); err != nil { + return nil, classifyDBError(err) + } + if s.cache != nil { + s.cache.SetUser(ctx, user.ID, user, expires) + } + return &Session{ + Token: token, + TokenType: "Bearer", + ExpiresAt: expires, + User: user, + }, nil +} + +// Lookup ВСЕГДА обращается к PostgreSQL за фактом существования сессии. +// Кэш используется только для публичных данных User. +func (s *SessionStore) Lookup(ctx context.Context, token string) (*User, error) { + var ( + userID string + expiresAt time.Time + ) + err := s.store.pool.QueryRow(ctx, + `SELECT user_id, expires_at FROM sessions WHERE token=$1`, token, + ).Scan(&userID, &expiresAt) + if errors.Is(err, pgx.ErrNoRows) { + return nil, ErrUnauthorized + } + if err != nil { + return nil, classifyDBError(err) + } + if time.Now().UTC().After(expiresAt) { + _, _ = s.store.pool.Exec(ctx, `DELETE FROM sessions WHERE token=$1`, token) + return nil, ErrUnauthorized + } + + if s.cache != nil { + if u, ok := s.cache.GetUser(ctx, userID); ok { + return &u, nil + } + } + + u, err := s.store.GetUserByID(ctx, userID) + if err != nil { + return nil, ErrUnauthorized + } + if s.cache != nil { + s.cache.SetUser(ctx, userID, *u, expiresAt) + } + return u, nil +} + +func (s *SessionStore) Revoke(ctx context.Context, token string) error { + var userID string + err := s.store.pool.QueryRow(ctx, + `DELETE FROM sessions WHERE token=$1 RETURNING user_id`, token, + ).Scan(&userID) + if errors.Is(err, pgx.ErrNoRows) { + // Сессии уже нет — с точки зрения клиента токен отозван. + return nil + } + if err != nil { + return classifyDBError(err) + } + if s.cache != nil { + // Кэш User не критичен, но полезно сбросить. + _ = s.cache.DelUser(ctx, userID) + } + return nil +}