From f612fbef775b94a0f642c840b20061b81d2f0b79 Mon Sep 17 00:00:00 2001 From: gera Date: Fri, 16 Oct 2026 00:50:33 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=A02:=20PostgreSQL,=20=D1=81=D0=B5?= =?UTF-8?q?=D1=81=D1=81=D0=B8=D0=B8=20=D0=B8=20Valkey=20=D0=BD=D0=B0=20?= =?UTF-8?q?=D0=BE=D1=86=D0=B5=D0=BD=D0=BA=D1=83=204?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .dockerignore | 17 +- .env.example | 6 +- Dockerfile | 19 + Makefile | 23 +- REPORT.md | 79 ++- auth.go | 131 +++++ cache.go | 104 ++++ compose.yaml | 75 +++ db.go | 57 +++ evidence/deps-down.txt | 16 + evidence/expiry.txt | 15 + evidence/explain.txt | 49 ++ evidence/own-tests.txt | 16 + evidence/persistence.txt | 22 + evidence/race-register.txt | 3 + evidence/rollback.txt | 2 + evidence/smoke.txt | 6 + go.mod | 20 + go.sum | 41 ++ handlers.go | 928 ++++++++++++++++++++++++++++++++++++ main.go | 76 +++ migrate.go | 74 +++ migrations/0001_init.sql | 38 ++ models.go | 33 ++ scripts/explain.sh | 59 +++ scripts/mksecrets.sh | 13 + scripts/restart.sh | 7 + scripts/test-deps-down.sh | 54 +++ scripts/test-expiry.sh | 46 ++ tests/test_own.py | 210 ++++++++ tests/test_race_register.py | 50 ++ tests/test_rollback.py | 71 +++ 32 files changed, 2339 insertions(+), 21 deletions(-) create mode 100644 Dockerfile create mode 100644 auth.go create mode 100644 cache.go create mode 100644 compose.yaml create mode 100644 db.go create mode 100644 evidence/deps-down.txt create mode 100644 evidence/expiry.txt create mode 100644 evidence/explain.txt create mode 100644 evidence/own-tests.txt create mode 100644 evidence/persistence.txt create mode 100644 evidence/race-register.txt create mode 100644 evidence/rollback.txt create mode 100644 evidence/smoke.txt create mode 100644 go.mod create mode 100644 go.sum create mode 100644 handlers.go create mode 100644 main.go create mode 100644 migrate.go create mode 100644 migrations/0001_init.sql create mode 100644 models.go create mode 100755 scripts/explain.sh create mode 100755 scripts/mksecrets.sh create mode 100755 scripts/restart.sh create mode 100755 scripts/test-deps-down.sh create mode 100755 scripts/test-expiry.sh create mode 100644 tests/test_own.py create mode 100644 tests/test_race_register.py create mode 100644 tests/test_rollback.py diff --git a/.dockerignore b/.dockerignore index bc16e556..53f2ff97 100644 --- a/.dockerignore +++ b/.dockerignore @@ -14,7 +14,22 @@ backups *.pfx *.jks .venv -__pycache__ +__pycache__/ node_modules evidence .DS_Store +tests +scripts +examples +contracts +*.md +Makefile +Dockerfile* +!Dockerfile +.go-cache +bin +dist +target +*.log +.idea +.vscode diff --git a/.env.example b/.env.example index 714c8076..7ffa9924 100644 --- a/.env.example +++ b/.env.example @@ -1,11 +1,9 @@ # Copy to .env for your LOCAL synthetic test environment; never commit .env. -# These are logical interface suggestions; map actual framework names in REPORT.md. APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab SESSION_TTL_SECONDS=3600 -# Choose credentials locally. File paths contain no secret values. -DATABASE_URL_FILE=/run/secrets/database_url -# Optional on 3, required use on 4 in lab2; Redis OR Valkey: +SHUTDOWN_TIMEOUT_SECONDS=5 SESSION_STORE=postgres +DATABASE_URL_FILE=/run/secrets/database_url CACHE_URL_FILE=/run/secrets/cache_url diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 00000000..acb0d63b --- /dev/null +++ b/Dockerfile @@ -0,0 +1,19 @@ +FROM golang:1.26-alpine@sha256:c95332c2af86b6d89b91bd0500f4b9529ccbd090a0d1855c6d1ceaa142ae8615 AS build +WORKDIR /src +COPY go.mod go.sum ./ +RUN go mod download +COPY *.go ./ +COPY migrations ./migrations +RUN CGO_ENABLED=0 GOOS=linux go build -trimpath -ldflags="-s -w" -o /out/messenger . + +FROM alpine:3.21@sha256:ce64758a109eb420d874a118f87920e625e12d3634e03b4a5573fd9f6e5d3507 +RUN adduser -D -u 10001 app +WORKDIR /app +COPY --from=build /out/messenger /app/messenger +USER 10001 +EXPOSE 8080 +ENV APP_HOST=0.0.0.0 APP_PORT=8080 APP_ENV=lab +STOPSIGNAL SIGTERM +HEALTHCHECK --interval=5s --timeout=3s --start-period=10s --retries=3 \ + CMD wget -qO- http://127.0.0.1:8080/health/ready >/dev/null || exit 1 +ENTRYPOINT ["/app/messenger"] diff --git a/Makefile b/Makefile index 9ecb223e..9df68b98 100644 --- a/Makefile +++ b/Makefile @@ -4,10 +4,11 @@ CA_FILE ?= ACTION_SCRIPT ?= TLS_ARGS = $(if $(CA_FILE),--ca-file "$(CA_FILE)",) -.PHONY: help check validate test load test-persistence +.PHONY: help check validate test load test-persistence test-own test-race test-rollback test-expiry test-deps explain help: - @echo "check: offline structure; validate: full OpenAPI (requirements-dev.txt); test: running API; load: synthetic writes" + @echo "check: offline structure; validate: full OpenAPI; test: running API" + @echo "test-own/race/rollback/expiry/deps/explain: grade-4 student checks" check: $(PYTHON) scripts/check_contract.py @@ -24,3 +25,21 @@ load: test-persistence: @test -n "$(ACTION_SCRIPT)" || (echo "Set ACTION_SCRIPT to your implemented executable restart hook"; exit 2) $(PYTHON) tests/resilience.py --mode persistence --action-script "$(ACTION_SCRIPT)" --base-url "$(BASE_URL)" $(TLS_ARGS) + +test-own: + BASE_URL="$(BASE_URL)" $(PYTHON) tests/test_own.py -v + +test-race: + BASE_URL="$(BASE_URL)" $(PYTHON) tests/test_race_register.py + +test-rollback: + BASE_URL="$(BASE_URL)" $(PYTHON) tests/test_rollback.py + +test-expiry: + ./scripts/test-expiry.sh + +test-deps: + ./scripts/test-deps-down.sh + +explain: + ./scripts/explain.sh evidence/explain.txt diff --git a/REPORT.md b/REPORT.md index e42f2b05..26d1e0eb 100644 --- a/REPORT.md +++ b/REPORT.md @@ -1,37 +1,88 @@ # Отчёт — лабораторная 2 -- Автор, группа: TODO -- Целевая оценка; выбранный трек 5, если нужен: TODO +- Автор, группа: Березин Герман / БВТ2304 +- Целевая оценка; выбранный трек 5, если нужен: 4 - Commit/PR: TODO после публикации -- Использованные источники, библиотеки и помощь ИИ: TODO +- Использованные источники, библиотеки и помощь ИИ: Go stdlib, jackc/pgx/v5, redis/go-redis/v9, golang.org/x/crypto/argon2, Docker Compose, PostgreSQL docs (transactions/EXPLAIN), Valkey persistence; contracts/openapi.json; ИИ — помощь в реализации по заданию; база — собственный lab1 (mtusi2026lab1-fork) ## Запуск из чистого clone -TODO: версии runtime/образов, prerequisites, команды создания локальных secrets/.env, build, миграций, запуска, ожидания готовности, тестов и остановки. Указать необходимые действия оператора. Не вставлять секреты. Отдельно указать безопасную остановку и команду намеренного удаления учебных данных. +Нужны: Docker + Compose, Python 3.9+, make, openssl. Go на хосте не обязателен. + +```sh +cp .env.example .env +./scripts/mksecrets.sh +docker compose --env-file .env up -d --build +# дождаться /health/ready +make test +make test-own test-race test-rollback +make test-persistence ACTION_SCRIPT=scripts/restart.sh +make test-expiry test-deps explain +``` + +Остановка: `docker compose --env-file .env down` (volumes сохраняются). +Намеренное удаление учебных данных: `docker compose --env-file .env down -v`. + +Образы (pinned tag+digest): +- `golang:1.26-alpine@sha256:c95332c2af86b6d89b91bd0500f4b9529ccbd090a0d1855c6d1ceaa142ae8615` +- `alpine:3.21@sha256:ce64758a109eb420d874a118f87920e625e12d3634e03b4a5573fd9f6e5d3507` +- `postgres:17-alpine@sha256:b0f9560a2de083e2cc7382e75f808c7381a32852a7ec49117deedb300e552b24` +- `valkey/valkey:8.1-alpine@sha256:081c2f5cb575efc901aa80ff9cdbd1ec6a301682fd35e1ebb4b0990a4a4a8507` + +Конфиг: `APP_HOST`, `APP_PORT`, `APP_ENV`, `SESSION_TTL_SECONDS`, `SHUTDOWN_TIMEOUT_SECONDS`, `SESSION_STORE=postgres`, `DATABASE_URL_FILE`, `CACHE_URL_FILE`. Секреты в `./secrets/` (не в git): `postgres_password`, `database_url`, `cache_url`. + +Миграции применяются API при старте (`embed` + таблица `schema_migrations`); повторный запуск не дублирует схему/данные. ## Архитектура и решения -TODO: схема процессов/хранилищ, источник истины, транзакционные границы, ограничения. Что перенесено из lab1, что изменено в этой работе. Соответствие переменных из .env.example реальной конфигурации. +``` +Клиент → API (Go) → PostgreSQL (источник истины: users/chats/members/messages/sessions) + ↘ Valkey (кэш сессий sess:, TTL = остаток жизни) +``` + +Перенесено из lab1: формат ошибок/`X-Request-Id`, валидация, keyset-cursor, graceful shutdown, multi-stage Dockerfile. +Изменено: RAM-store → PostgreSQL; `X-User-Id` → Basic login + opaque Bearer; добавлен Valkey как кэш. + +Источник истины сессий — Postgres (`sessions.token_hash = sha256(token)`, `expires_at`). Valkey — кэш; при miss/ошибке кэша читаем Postgres и (при успехе) пишем кэш. Logout: `DELETE` в Postgres + `DEL` в Valkey. TTL не продлевается рестартом. + +Транзакции: +- создание чата: одна tx `insert chats` + `insert chat_members`; FK/unknown member → rollback → 400; +- регистрация: `UNIQUE(username)`, гонка → SQLSTATE 23505 → 409; +- сообщение: один `INSERT`. + +Пул: MaxConns=10, MinConns=2, ConnectTimeout=3s, query timeout 3s; только параметризованные запросы. +Пароли: argon2id (`m=65536,t=2,p=1`). Токен: 32 random bytes (256 bit), base64url. +`/health/ready` проверяет только Postgres (Valkey некритичен). + +Индекс истории: `messages_history_idx (chat_id, created_at, id)` под keyset +`(created_at, id) > ($cursor)` + `ORDER BY created_at, id LIMIT n`. +EXPLAIN на 10001 сообщении: Index Cond по `messages_history_idx`; OFFSET 5000 сортирует ≥5050 строк (evidence/explain.txt). ## Доказательства критериев | Критерий README | Команда/сценарий | Наблюдаемый результат | Файл доказательства | | --- | --- | --- | --- | -| Публичный smoke | make test | TODO | TODO | -| Остальные пункты уровня 3 | TODO | TODO | TODO | -| Все пункты уровня 4, если заявлен | TODO | TODO | TODO | -| Выбранный трек 5, если заявлен | TODO | TODO | TODO | - -TODO: добавить отдельную строку для каждого заявленного критерия. PASS без команды и наблюдения не является доказательством. Проверка инфраструктурного hook требует подтверждения, какие реальные процессы/хранилища перезапущены. +| Публичный smoke | `make test` | PASS lab2 public API | evidence/smoke.txt | +| Offline check | `make check` | PASS skeleton | evidence/smoke.txt | +| Persistence + session | `make test-persistence ACTION_SCRIPT=scripts/restart.sh` | PASS; StartedAt api/postgres/valkey сменились, volumes сохранены | evidence/persistence.txt | +| Own negative/auth | `make test-own` | 10 tests OK | evidence/own-tests.txt | +| Race username | `make test-race` | created=1 conflict=19 | evidence/race-register.txt | +| Rollback chat | `make test-rollback` | PASS, чат не в списке | evidence/rollback.txt | +| Session expiry | `make test-expiry` | 200 → sleep → 401 при TTL=2 | evidence/expiry.txt | +| Deps down | `make test-deps` | valkey down → 200; postgres down → 503/503 | evidence/deps-down.txt | +| EXPLAIN index | `make explain` | Index Scan на messages_history_idx, 10001 msgs | evidence/explain.txt | ## Эксперименты и отказы -TODO: гипотеза, hardware/лимиты, версии, объём данных, профиль нагрузки, concurrency/arrival rate, длительность/прогрев, повторы, успешные RPS, ошибки, p50/p95/p99, CPU/RAM. Сохранить raw summary без tokens и объяснить ограничения измерения. +Уровень 5 не заявлен. | Воздействие | Что ожидаем | Что наблюдали | Время восстановления / потеря данных | | --- | --- | --- | --- | -| TODO | TODO | TODO | TODO | +| `scripts/restart.sh` (stop+start всех сервисов) | тот же Bearer читает те же данные | PASS persistence | ~несколько секунд; данные в volumes | +| Valkey stop | обход кэша, сессия из Postgres | `/users/me` 200 | мгновенно; без потери | +| Postgres stop | ready/бизнес → 503 | 503/503 | после start — 200, данные на месте | +| SESSION_TTL_SECONDS=2 | после expiry 401 | 200 затем 401 | сессия отозвана по времени | ## Ограничения и следующие шаги -TODO: какие отказы выдерживаются и какие нет; что не реализовано; где учебное упрощение; какое улучшение подтверждено, а какое пока гипотеза. +Один API-процесс, нет LB/идемпотентности (lab3+). Valkey с AOF, но для оценки 4 он кэш, не единственное хранилище сессий. Нет delete API — тестовые данные копятся в volume. Следующий шаг курса — балансировка и idempotency. diff --git a/auth.go b/auth.go new file mode 100644 index 00000000..cacc42d0 --- /dev/null +++ b/auth.go @@ -0,0 +1,131 @@ +package main + +import ( + "crypto/rand" + "crypto/sha256" + "crypto/subtle" + "encoding/base64" + "encoding/hex" + "errors" + "fmt" + "strconv" + "strings" + "unicode/utf8" + + "golang.org/x/crypto/argon2" +) + +const ( + argonTime = 2 + argonMemory = 19456 + argonThreads = 1 + argonKeyLen = 32 + argonSaltLen = 16 +) + +func hashPassword(password string) (string, error) { + salt := make([]byte, argonSaltLen) + if _, err := rand.Read(salt); err != nil { + return "", err + } + hash := argon2.IDKey([]byte(password), salt, argonTime, argonMemory, argonThreads, argonKeyLen) + b64Salt := base64.RawStdEncoding.EncodeToString(salt) + b64Hash := base64.RawStdEncoding.EncodeToString(hash) + return fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s", + argon2.Version, argonMemory, argonTime, argonThreads, b64Salt, b64Hash), nil +} + +func checkPassword(encoded, password string) (bool, error) { + parts := strings.Split(encoded, "$") + if len(parts) != 6 || parts[1] != "argon2id" { + return false, errors.New("bad hash") + } + var version int + if _, err := fmt.Sscanf(parts[2], "v=%d", &version); err != nil { + return false, err + } + var memory, timeCost, threads int + for _, p := range strings.Split(parts[3], ",") { + kv := strings.SplitN(p, "=", 2) + if len(kv) != 2 { + return false, errors.New("bad params") + } + n, err := strconv.Atoi(kv[1]) + if err != nil { + return false, err + } + switch kv[0] { + case "m": + memory = n + case "t": + timeCost = n + case "p": + threads = n + } + } + salt, err := base64.RawStdEncoding.DecodeString(parts[4]) + if err != nil { + return false, err + } + want, err := base64.RawStdEncoding.DecodeString(parts[5]) + if err != nil { + return false, err + } + got := argon2.IDKey([]byte(password), salt, uint32(timeCost), uint32(memory), uint8(threads), uint32(len(want))) + return subtle.ConstantTimeCompare(got, want) == 1, nil +} + +func newToken() (string, []byte, string, error) { + raw := make([]byte, 32) + if _, err := rand.Read(raw); err != nil { + return "", nil, "", err + } + token := base64.RawURLEncoding.EncodeToString(raw) + sum := sha256.Sum256([]byte(token)) + hash := make([]byte, len(sum)) + copy(hash, sum[:]) + return token, hash, hex.EncodeToString(hash), nil +} + +func tokenHash(token string) ([]byte, string) { + sum := sha256.Sum256([]byte(token)) + hash := make([]byte, len(sum)) + copy(hash, sum[:]) + return hash, hex.EncodeToString(hash) +} + +func parseBasic(header string) (username, password string, ok bool) { + const prefix = "Basic " + if !strings.HasPrefix(header, prefix) { + return "", "", false + } + decoded, err := base64.StdEncoding.DecodeString(strings.TrimSpace(header[len(prefix):])) + if err != nil { + return "", "", false + } + s := string(decoded) + i := strings.IndexByte(s, ':') + if i < 0 { + return "", "", false + } + username = s[:i] + password = s[i+1:] + return username, password, true +} + +func parseBearer(header string) (string, bool) { + const prefix = "Bearer " + if !strings.HasPrefix(header, prefix) { + return "", false + } + tok := strings.TrimSpace(header[len(prefix):]) + if tok == "" { + return "", false + } + return tok, true +} + +func validPassword(p string) bool { + n := utf8.RuneCountInString(p) + return n >= 12 && n <= 128 +} diff --git a/cache.go b/cache.go new file mode 100644 index 00000000..9afc5e3f --- /dev/null +++ b/cache.go @@ -0,0 +1,104 @@ +package main + +import ( + "context" + "encoding/json" + "log/slog" + "time" + + "github.com/redis/go-redis/v9" +) + +type cachedSession struct { + UserID string `json:"user_id"` + ExpiresAt time.Time `json:"expires_at"` +} + +type Cache struct { + rdb *redis.Client + log *slog.Logger +} + +func openCache(log *slog.Logger) *Cache { + url, err := readSecret("CACHE_URL", "CACHE_URL_FILE") + if err != nil { + log.Warn("cache disabled", "err", err.Error()) + return &Cache{log: log} + } + opt, err := redis.ParseURL(url) + if err != nil { + log.Warn("cache parse failed", "err", err.Error()) + return &Cache{log: log} + } + opt.DialTimeout = 200 * time.Millisecond + opt.ReadTimeout = 200 * time.Millisecond + opt.WriteTimeout = 200 * time.Millisecond + opt.PoolTimeout = 200 * time.Millisecond + rdb := redis.NewClient(opt) + ctx, cancel := context.WithTimeout(context.Background(), 500*time.Millisecond) + defer cancel() + if err := rdb.Ping(ctx).Err(); err != nil { + log.Warn("cache ping failed, will bypass", "err", err.Error()) + } + return &Cache{rdb: rdb, log: log} +} + +func (c *Cache) Close() { + if c.rdb != nil { + _ = c.rdb.Close() + } +} + +func sessKey(hashHex string) string { + return "sess:" + hashHex +} + +func (c *Cache) GetSession(ctx context.Context, hashHex string) (*cachedSession, bool) { + if c.rdb == nil { + return nil, false + } + ctx, cancel := context.WithTimeout(ctx, 200*time.Millisecond) + defer cancel() + val, err := c.rdb.Get(ctx, sessKey(hashHex)).Result() + if err != nil { + return nil, false + } + var s cachedSession + if err := json.Unmarshal([]byte(val), &s); err != nil { + return nil, false + } + if time.Now().UTC().After(s.ExpiresAt) { + return nil, false + } + return &s, true +} + +func (c *Cache) SetSession(ctx context.Context, hashHex string, userID string, expiresAt time.Time) { + if c.rdb == nil { + return + } + ttl := time.Until(expiresAt) + if ttl <= 0 { + return + } + body, err := json.Marshal(cachedSession{UserID: userID, ExpiresAt: expiresAt.UTC()}) + if err != nil { + return + } + ctx, cancel := context.WithTimeout(ctx, 200*time.Millisecond) + defer cancel() + if err := c.rdb.Set(ctx, sessKey(hashHex), body, ttl).Err(); err != nil { + c.log.Warn("cache set failed", "err", err.Error()) + } +} + +func (c *Cache) DelSession(ctx context.Context, hashHex string) { + if c.rdb == nil { + return + } + ctx, cancel := context.WithTimeout(ctx, 200*time.Millisecond) + defer cancel() + if err := c.rdb.Del(ctx, sessKey(hashHex)).Err(); err != nil { + c.log.Warn("cache del failed", "err", err.Error()) + } +} diff --git a/compose.yaml b/compose.yaml new file mode 100644 index 00000000..8f344bc6 --- /dev/null +++ b/compose.yaml @@ -0,0 +1,75 @@ +services: + postgres: + image: postgres:17-alpine@sha256:b0f9560a2de083e2cc7382e75f808c7381a32852a7ec49117deedb300e552b24 + environment: + POSTGRES_USER: messenger + POSTGRES_PASSWORD_FILE: /run/secrets/postgres_password + POSTGRES_DB: messenger + secrets: + - postgres_password + volumes: + - pg_data:/var/lib/postgresql/data + healthcheck: + test: ["CMD-SHELL", "pg_isready -U messenger -d messenger"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + networks: + - labnet + restart: unless-stopped + + valkey: + image: valkey/valkey:8.1-alpine@sha256:081c2f5cb575efc901aa80ff9cdbd1ec6a301682fd35e1ebb4b0990a4a4a8507 + command: ["valkey-server", "--appendonly", "yes", "--save", "60", "1"] + volumes: + - valkey_data:/data + healthcheck: + test: ["CMD", "valkey-cli", "ping"] + interval: 5s + timeout: 3s + retries: 10 + start_period: 5s + networks: + - labnet + restart: unless-stopped + + api: + build: . + image: mtusi-messenger:lab2 + ports: + - "127.0.0.1:8080:8080" + env_file: + - .env + environment: + DATABASE_URL_FILE: /run/secrets/database_url + CACHE_URL_FILE: /run/secrets/cache_url + SESSION_STORE: postgres + SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600} + secrets: + - database_url + - cache_url + depends_on: + postgres: + condition: service_healthy + valkey: + condition: service_healthy + networks: + - labnet + restart: unless-stopped + +secrets: + postgres_password: + file: ./secrets/postgres_password + database_url: + file: ./secrets/database_url + cache_url: + file: ./secrets/cache_url + +volumes: + pg_data: + valkey_data: + +networks: + labnet: + driver: bridge diff --git a/db.go b/db.go new file mode 100644 index 00000000..5b051b33 --- /dev/null +++ b/db.go @@ -0,0 +1,57 @@ +package main + +import ( + "context" + "fmt" + "os" + "strings" + "time" + + "github.com/jackc/pgx/v5/pgxpool" +) + +func readSecret(envKey, fileKey string) (string, error) { + if v := os.Getenv(envKey); v != "" { + return strings.TrimSpace(v), nil + } + path := os.Getenv(fileKey) + if path == "" { + return "", fmt.Errorf("%s or %s required", envKey, fileKey) + } + b, err := os.ReadFile(path) + if err != nil { + return "", err + } + return strings.TrimSpace(string(b)), nil +} + +func openDB(ctx context.Context) (*pgxpool.Pool, error) { + dsn, err := readSecret("DATABASE_URL", "DATABASE_URL_FILE") + if err != nil { + return nil, err + } + cfg, err := pgxpool.ParseConfig(dsn) + if err != nil { + return nil, err + } + cfg.MaxConns = 10 + cfg.MinConns = 2 + cfg.MaxConnLifetime = 5 * time.Minute + cfg.ConnConfig.ConnectTimeout = 3 * time.Second + + pool, err := pgxpool.NewWithConfig(ctx, cfg) + if err != nil { + return nil, err + } + pingCtx, cancel := context.WithTimeout(ctx, 5*time.Second) + defer cancel() + if err := pool.Ping(pingCtx); err != nil { + pool.Close() + return nil, err + } + return pool, nil +} + +func dbCtx(parent context.Context) (context.Context, context.CancelFunc) { + return context.WithTimeout(parent, 3*time.Second) +} diff --git a/evidence/deps-down.txt b/evidence/deps-down.txt new file mode 100644 index 00000000..bd1599e0 --- /dev/null +++ b/evidence/deps-down.txt @@ -0,0 +1,16 @@ +./scripts/test-deps-down.sh +=== stop valkey (cache bypass) === + Container mtusi2026lab2-valkey-1 Stopping + Container mtusi2026lab2-valkey-1 Stopped +with valkey down /users/me -> 200 (expect 200) +=== start valkey, stop postgres === + Container mtusi2026lab2-valkey-1 Starting + Container mtusi2026lab2-valkey-1 Started + Container mtusi2026lab2-postgres-1 Stopping + Container mtusi2026lab2-postgres-1 Stopped +postgres down ready=503 me=503 (expect 503/503) +=== restore postgres === + Container mtusi2026lab2-postgres-1 Starting + Container mtusi2026lab2-postgres-1 Started +restored /users/me -> 200 (expect 200) +PASS: dependency failures diff --git a/evidence/expiry.txt b/evidence/expiry.txt new file mode 100644 index 00000000..5607109c --- /dev/null +++ b/evidence/expiry.txt @@ -0,0 +1,15 @@ +./scripts/test-expiry.sh +=== restart api with SESSION_TTL_SECONDS=2 === + Container mtusi2026lab2-api-1 Recreate + Container mtusi2026lab2-api-1 Recreated + Container mtusi2026lab2-api-1 Starting + Container mtusi2026lab2-api-1 Started +immediate /users/me -> 200 (expect 200) +sleep 3 for expiry +after ttl /users/me -> 401 (expect 401) +=== restore SESSION_TTL_SECONDS=3600 === + Container mtusi2026lab2-api-1 Recreate + Container mtusi2026lab2-api-1 Recreated + Container mtusi2026lab2-api-1 Starting + Container mtusi2026lab2-api-1 Started +PASS: session expiry diff --git a/evidence/explain.txt b/evidence/explain.txt new file mode 100644 index 00000000..812d7c5c --- /dev/null +++ b/evidence/explain.txt @@ -0,0 +1,49 @@ +date=2026-10-15T21:41:08Z +chat_id=a9b8867c-4828-4847-ab1d-39f3cd4dcdbb +count=10001 + +=== keyset with index messages_history_idx === + QUERY PLAN +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ + Limit (cost=46.77..46.81 rows=14 width=88) (actual time=3.557..3.567 rows=50 loops=1) + Buffers: shared hit=258 + -> Sort (cost=46.77..46.81 rows=14 width=88) (actual time=3.556..3.560 rows=50 loops=1) + Sort Key: created_at, id + Sort Method: top-N heapsort Memory: 31kB + Buffers: shared hit=258 + -> Bitmap Heap Scan on messages (cost=4.43..46.50 rows=14 width=88) (actual time=1.233..2.307 rows=10001 loops=1) + Recheck Cond: ((chat_id = 'a9b8867c-4828-4847-ab1d-39f3cd4dcdbb'::uuid) AND (ROW(created_at, id) > ROW('1970-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-4000-8000-000000000000'::uuid))) + Heap Blocks: exact=123 + Buffers: shared hit=252 + -> Bitmap Index Scan on messages_history_idx (cost=0.00..4.42 rows=14 width=0) (actual time=1.194..1.195 rows=10001 loops=1) + Index Cond: ((chat_id = 'a9b8867c-4828-4847-ab1d-39f3cd4dcdbb'::uuid) AND (ROW(created_at, id) > ROW('1970-01-01 00:00:00+00'::timestamp with time zone, '00000000-0000-4000-8000-000000000000'::uuid))) + Buffers: shared hit=129 + Planning: + Buffers: shared hit=101 + Planning Time: 1.515 ms + Execution Time: 3.739 ms +(17 rows) + + +=== OFFSET page ~200 (full scan risk) === + QUERY PLAN +---------------------------------------------------------------------------------------------------------------------------------------------- + Limit (cost=93.55..93.55 rows=1 width=88) (actual time=5.617..5.627 rows=50 loops=1) + Buffers: shared hit=258 + -> Sort (cost=93.44..93.55 rows=43 width=88) (actual time=5.054..5.347 rows=5050 loops=1) + Sort Key: created_at, id + Sort Method: quicksort Memory: 1713kB + Buffers: shared hit=258 + -> Bitmap Heap Scan on messages (cost=4.62..92.28 rows=43 width=88) (actual time=0.847..2.033 rows=10001 loops=1) + Recheck Cond: (chat_id = 'a9b8867c-4828-4847-ab1d-39f3cd4dcdbb'::uuid) + Heap Blocks: exact=123 + Buffers: shared hit=252 + -> Bitmap Index Scan on messages_history_idx (cost=0.00..4.61 rows=43 width=0) (actual time=0.818..0.818 rows=10001 loops=1) + Index Cond: (chat_id = 'a9b8867c-4828-4847-ab1d-39f3cd4dcdbb'::uuid) + Buffers: shared hit=129 + Planning: + Buffers: shared hit=102 + Planning Time: 1.926 ms + Execution Time: 5.951 ms +(17 rows) + diff --git a/evidence/own-tests.txt b/evidence/own-tests.txt new file mode 100644 index 00000000..98f4d9f7 --- /dev/null +++ b/evidence/own-tests.txt @@ -0,0 +1,16 @@ +BASE_URL="http://localhost:8080" python3 tests/test_own.py -v +test_bad_json (__main__.OwnTests.test_bad_json) ... ok +test_basic_on_business (__main__.OwnTests.test_basic_on_business) ... ok +test_cursor_other_chat (__main__.OwnTests.test_cursor_other_chat) ... ok +test_duplicate_username (__main__.OwnTests.test_duplicate_username) ... ok +test_empty_message (__main__.OwnTests.test_empty_message) ... ok +test_foreign_chat (__main__.OwnTests.test_foreign_chat) ... ok +test_logout (__main__.OwnTests.test_logout) ... ok +test_pagination_pages (__main__.OwnTests.test_pagination_pages) ... ok +test_wrong_password (__main__.OwnTests.test_wrong_password) ... ok +test_x_user_id_ignored (__main__.OwnTests.test_x_user_id_ignored) ... ok + +---------------------------------------------------------------------- +Ran 10 tests in 4.147s + +OK diff --git a/evidence/persistence.txt b/evidence/persistence.txt new file mode 100644 index 00000000..f2738f75 --- /dev/null +++ b/evidence/persistence.txt @@ -0,0 +1,22 @@ +date=2026-10-15T21:41:21Z +=== before restart === +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab2-api-1 mtusi-messenger:lab2 "/app/messenger" api 21 seconds ago Up 20 seconds (healthy) 127.0.0.1:8080->8080/tcp +mtusi2026lab2-postgres-1 postgres:17-alpine@sha256:b0f9560a2de083e2cc7382e75f808c7381a32852a7ec49117deedb300e552b24 "docker-entrypoint.s…" postgres 2 minutes ago Up 15 seconds (healthy) 5432/tcp +mtusi2026lab2-valkey-1 valkey/valkey:8.1-alpine@sha256:081c2f5cb575efc901aa80ff9cdbd1ec6a301682fd35e1ebb4b0990a4a4a8507 "tini -- docker-entr…" valkey 2 minutes ago Up 17 seconds (healthy) 6379/tcp +=== StartedAt before === +/mtusi2026lab2-api-1 2026-10-15T21:41:01.004288114Z +/mtusi2026lab2-postgres-1 2026-10-15T21:41:06.039417376Z +/mtusi2026lab2-valkey-1 2026-10-15T21:41:04.187505543Z +python3 tests/resilience.py --mode persistence --action-script "scripts/restart.sh" --base-url "http://localhost:8080" +Executing explicit persistence hook; hook output suppressed to avoid leaking secrets. +PASS: persistence preserved confirmed data and existing session. Hook scope needs operator evidence. +=== after restart === +NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS +mtusi2026lab2-api-1 mtusi-messenger:lab2 "/app/messenger" api 29 seconds ago Up Less than a second (health: starting) 127.0.0.1:8080->8080/tcp +mtusi2026lab2-postgres-1 postgres:17-alpine@sha256:b0f9560a2de083e2cc7382e75f808c7381a32852a7ec49117deedb300e552b24 "docker-entrypoint.s…" postgres 3 minutes ago Up 5 seconds (healthy) 5432/tcp +mtusi2026lab2-valkey-1 valkey/valkey:8.1-alpine@sha256:081c2f5cb575efc901aa80ff9cdbd1ec6a301682fd35e1ebb4b0990a4a4a8507 "tini -- docker-entr…" valkey 3 minutes ago Up 5 seconds (healthy) 6379/tcp +=== StartedAt after === +/mtusi2026lab2-api-1 2026-10-15T21:41:28.660588193Z +/mtusi2026lab2-postgres-1 2026-10-15T21:41:23.448021809Z +/mtusi2026lab2-valkey-1 2026-10-15T21:41:23.246159861Z diff --git a/evidence/race-register.txt b/evidence/race-register.txt new file mode 100644 index 00000000..805017bf --- /dev/null +++ b/evidence/race-register.txt @@ -0,0 +1,3 @@ +BASE_URL="http://localhost:8080" python3 tests/test_race_register.py +codes=[201, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409, 409] created=1 conflict=19 +PASS: race register handled by unique username diff --git a/evidence/rollback.txt b/evidence/rollback.txt new file mode 100644 index 00000000..5099e8ae --- /dev/null +++ b/evidence/rollback.txt @@ -0,0 +1,2 @@ +BASE_URL="http://localhost:8080" python3 tests/test_rollback.py +PASS: unknown member rolls back chat create diff --git a/evidence/smoke.txt b/evidence/smoke.txt new file mode 100644 index 00000000..0570f0df --- /dev/null +++ b/evidence/smoke.txt @@ -0,0 +1,6 @@ +date=2026-10-15T21:41:19Z +commit=65003b6 +python3 scripts/check_contract.py +PASS: offline skeleton structure, local refs and Python syntax; application NOT tested. +python3 tests/smoke.py --base-url "http://localhost:8080" +PASS: lab2 public API scenario; infrastructure and grading evidence remain separate. diff --git a/go.mod b/go.mod new file mode 100644 index 00000000..ed78357c --- /dev/null +++ b/go.mod @@ -0,0 +1,20 @@ +module mtusi-messenger + +go 1.26.0 + +require ( + github.com/jackc/pgx/v5 v5.11.0 + github.com/redis/go-redis/v9 v9.23.0 + golang.org/x/crypto v0.58.0 +) + +require ( + github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect + github.com/jackc/puddle/v2 v2.2.2 // indirect + go.uber.org/atomic v1.12.0 // indirect + golang.org/x/sync v0.24.0 // indirect + golang.org/x/sys v0.49.0 // indirect + golang.org/x/text v0.43.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 00000000..e82c5df0 --- /dev/null +++ b/go.sum @@ -0,0 +1,41 @@ +github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs= +github.com/bsm/ginkgo/v2 v2.12.0/go.mod h1:SwYbGRRDovPVboqFv0tPTcG1sN61LM1Z4ARdbAV9g4c= +github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA= +github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0= +github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= +github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= +github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= +github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= +github.com/jackc/pgx/v5 v5.11.0 h1:IzBBtyK9AHqf98cctWFifYSci2hgQR/cd56wB4p+ogg= +github.com/jackc/pgx/v5 v5.11.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4= +github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo= +github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= +github.com/klauspost/cpuid/v2 v2.4.0 h1:S6Hrbc7+ywsr0r+RLapfGBHfyefhCTwEh3A0tV913Dw= +github.com/klauspost/cpuid/v2 v2.4.0/go.mod h1:19jmZ9mjzoF//ddRSUsv0zfBTJWh3QJh9FNxZTMrGxU= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/redis/go-redis/v9 v9.23.0 h1:/TTVdJa8BRNsybwwGGnmiYktHNlUXtbOQxlWpHBPL3s= +github.com/redis/go-redis/v9 v9.23.0/go.mod h1:EWP2UUk+XDaI1s5nQB5mablZN6vccXlmBQTyfnF87D0= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= +github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +github.com/zeebo/xxh3 v1.1.0 h1:s7DLGDK45Dyfg7++yxI0khrfwq9661w9EN78eP/UZVs= +github.com/zeebo/xxh3 v1.1.0/go.mod h1:IisAie1LELR4xhVinxWS5+zf1lA4p0MW4T+w+W07F5s= +go.uber.org/atomic v1.12.0 h1:BvcXdFKuviU4fTL/f+SxdQ5qJX/Jix8pAkgdUcb3XOE= +go.uber.org/atomic v1.12.0/go.mod h1:I6c4cg+6HCxRjfjSsYtApoFILnpc0CGUdGkXVqbYVNk= +go.yaml.in/yaml/v3 v3.0.5 h1:N6y/pJk8buWs9NY5ERU2HSMfm+IuD/OtfdAnq6kESPw= +go.yaml.in/yaml/v3 v3.0.5/go.mod h1:HVTZu1O7/Vkt2N+BFy8Zza+lnLsABggaTM2ZpNIGuKg= +golang.org/x/crypto v0.58.0 h1:COkYLr4k7nDI4r6QtMML2AfzeIswWHAJthLS6K/M/54= +golang.org/x/crypto v0.58.0/go.mod h1:Xh+kl5A+M0gMiWDytH2Hhr5Z7WbNCpv/eB/EFgfSk+w= +golang.org/x/sync v0.24.0 h1:iqiDrGoDgyuvAKrhLQthUEzU/R378N78gXbBjxed7oA= +golang.org/x/sync v0.24.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.49.0 h1:XbzkgYJHdqh/8m2Uu0W/dQv8nktxx4BFHp1M0gROTXA= +golang.org/x/sys v0.49.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/text v0.43.0 h1:1QivrlhwAsnMOcqgOdl9my7s9OyZl8O5/UGUTJVXoWc= +golang.org/x/text v0.43.0/go.mod h1:hbSIYA/amXcRXTFSZqh8tWHEoqRvvbKPJ2uz/KU+a+s= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/handlers.go b/handlers.go new file mode 100644 index 00000000..6e307a28 --- /dev/null +++ b/handlers.go @@ -0,0 +1,928 @@ +package main + +import ( + "context" + "crypto/rand" + "encoding/base64" + "encoding/hex" + "encoding/json" + "errors" + "fmt" + "io" + "log/slog" + "net/http" + "os" + "regexp" + "strconv" + "strings" + "sync/atomic" + "time" + "unicode/utf8" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" +) + +var usernameRe = regexp.MustCompile(`^[a-z0-9_]{3,32}$`) + +type apiError struct { + Code string `json:"code"` + Message string `json:"message"` + ReqID string `json:"request_id"` +} + +type errorBody struct { + Error apiError `json:"error"` +} + +type App struct { + db *pgxpool.Pool + cache *Cache + log *slog.Logger + ready atomic.Bool + sessionTTL time.Duration +} + +func NewApp(db *pgxpool.Pool, cache *Cache, log *slog.Logger, sessionTTL time.Duration) *App { + a := &App{db: db, cache: cache, log: log, sessionTTL: sessionTTL} + a.ready.Store(true) + return a +} + +func (a *App) SetReady(v bool) { + a.ready.Store(v) +} + +func newUUID() string { + var b [16]byte + _, _ = rand.Read(b[:]) + b[6] = (b[6] & 0x0f) | 0x40 + b[8] = (b[8] & 0x3f) | 0x80 + return fmt.Sprintf("%x-%x-%x-%x-%x", b[0:4], b[4:6], b[6:8], b[8:10], b[10:16]) +} + +func nowUTC() time.Time { + return time.Now().UTC().Truncate(time.Microsecond) +} + +func formatTime(t time.Time) string { + return t.UTC().Format("2006-01-02T15:04:05.000000Z") +} + +func parseTime(s string) (time.Time, error) { + return time.Parse("2006-01-02T15:04:05.000000Z", s) +} + +type statusWriter struct { + http.ResponseWriter + status int +} + +func (w *statusWriter) WriteHeader(code int) { + w.status = code + w.ResponseWriter.WriteHeader(code) +} + +type ctxKey int + +const reqIDKey ctxKey = 1 + +func (a *App) Middleware(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + start := time.Now() + reqID := newUUID() + w.Header().Set("X-Request-Id", reqID) + sw := &statusWriter{ResponseWriter: w, status: 200} + defer func() { + if rec := recover(); rec != nil { + a.writeErr(sw, reqID, 500, "internal_error", "internal error") + a.log.Error("panic", "request_id", reqID, "err", fmt.Sprint(rec)) + } + a.log.Info("request", + "method", r.Method, + "path", r.URL.Path, + "status", sw.status, + "duration_ms", time.Since(start).Milliseconds(), + "request_id", reqID, + ) + }() + next.ServeHTTP(sw, r.WithContext(context.WithValue(r.Context(), reqIDKey, reqID))) + }) +} + +func reqIDFrom(r *http.Request) string { + if v, ok := r.Context().Value(reqIDKey).(string); ok { + return v + } + return "" +} + +func (a *App) writeJSON(w http.ResponseWriter, status int, v any) { + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(status) + enc := json.NewEncoder(w) + enc.SetEscapeHTML(false) + _ = enc.Encode(v) +} + +func (a *App) writeErr(w http.ResponseWriter, reqID string, status int, code, msg string) { + a.writeJSON(w, status, errorBody{Error: apiError{Code: code, Message: msg, ReqID: reqID}}) +} + +func (a *App) writeUnauthorized(w http.ResponseWriter, r *http.Request, realm string) { + reqID := reqIDFrom(r) + if realm == "Basic" { + w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`) + } else { + w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`) + } + a.writeErr(w, reqID, 401, "unauthorized", "unauthorized") +} + +func (a *App) Routes() http.Handler { + mux := http.NewServeMux() + mux.HandleFunc("GET /health/live", a.handleLive) + mux.HandleFunc("GET /health/ready", a.handleReady) + mux.HandleFunc("POST /api/v1/users", a.handleCreateUser) + mux.HandleFunc("GET /api/v1/users/me", a.handleMe) + mux.HandleFunc("POST /api/v1/chats", a.handleCreateChat) + mux.HandleFunc("GET /api/v1/chats", a.handleListChats) + mux.HandleFunc("GET /api/v1/chats/{chat_id}", a.handleGetChat) + mux.HandleFunc("POST /api/v1/chats/{chat_id}/messages", a.handleCreateMessage) + mux.HandleFunc("GET /api/v1/chats/{chat_id}/messages", a.handleListMessages) + mux.HandleFunc("POST /api/v1/auth/sessions", a.handleCreateSession) + mux.HandleFunc("DELETE /api/v1/auth/sessions/current", a.handleRevokeSession) + return a.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, pattern := mux.Handler(r) + if pattern == "" { + a.writeErr(w, reqIDFrom(r), 404, "not_found", "not found") + return + } + mux.ServeHTTP(w, r) + })) +} + +func (a *App) handleLive(w http.ResponseWriter, r *http.Request) { + a.writeJSON(w, 200, map[string]string{"status": "ok"}) +} + +func (a *App) handleReady(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + if !a.ready.Load() { + a.writeErr(w, reqID, 503, "unavailable", "shutting down") + return + } + ctx, cancel := dbCtx(r.Context()) + defer cancel() + if err := a.db.Ping(ctx); err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + a.writeJSON(w, 200, map[string]string{"status": "ok"}) +} + +func (a *App) readJSON(w http.ResponseWriter, r *http.Request, dst any) bool { + reqID := reqIDFrom(r) + ct := r.Header.Get("Content-Type") + media := strings.ToLower(strings.TrimSpace(strings.Split(ct, ";")[0])) + if media != "application/json" { + a.writeErr(w, reqID, 415, "unsupported_media_type", "Content-Type must be application/json") + return false + } + r.Body = http.MaxBytesReader(w, r.Body, 1<<20) + dec := json.NewDecoder(r.Body) + dec.DisallowUnknownFields() + if err := dec.Decode(dst); err != nil { + if _, ok := err.(*http.MaxBytesError); ok { + a.writeErr(w, reqID, 413, "payload_too_large", "body too large") + return false + } + a.writeErr(w, reqID, 400, "invalid_request", "invalid json") + return false + } + var extra json.RawMessage + if err := dec.Decode(&extra); err != io.EOF { + a.writeErr(w, reqID, 400, "invalid_request", "invalid json") + return false + } + return true +} + +func isBlank(s string) bool { + return strings.TrimSpace(s) == "" +} + +func isUUID(s string) bool { + if len(s) != 36 { + return false + } + for i, c := range s { + switch i { + case 8, 13, 18, 23: + if c != '-' { + return false + } + default: + if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { + return false + } + } + } + raw := strings.ReplaceAll(s, "-", "") + _, err := hex.DecodeString(raw) + return err == nil +} + +func isPgCode(err error, code string) bool { + var pgErr *pgconn.PgError + return errors.As(err, &pgErr) && pgErr.Code == code +} + +func userJSON(u *User) map[string]any { + return map[string]any{ + "id": u.ID, + "username": u.Username, + "display_name": u.DisplayName, + "created_at": formatTime(u.CreatedAt), + } +} + +func (a *App) getUserByID(ctx context.Context, id string) (*User, error) { + ctx, cancel := dbCtx(ctx) + defer cancel() + var u User + err := a.db.QueryRow(ctx, ` + select id::text, username, display_name, password_hash, created_at + from users where id = $1::uuid`, id).Scan( + &u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt) + if errors.Is(err, pgx.ErrNoRows) { + return nil, nil + } + if err != nil { + return nil, err + } + return &u, nil +} + +func (a *App) authUser(w http.ResponseWriter, r *http.Request) (*User, string) { + token, ok := parseBearer(r.Header.Get("Authorization")) + if !ok { + a.writeUnauthorized(w, r, "Bearer") + return nil, "" + } + hash, hashHex := tokenHash(token) + + if cs, hit := a.cache.GetSession(r.Context(), hashHex); hit { + u, err := a.getUserByID(r.Context(), cs.UserID) + if err != nil { + a.writeErr(w, reqIDFrom(r), 503, "unavailable", "database unavailable") + return nil, "" + } + if u == nil { + a.writeUnauthorized(w, r, "Bearer") + return nil, "" + } + return u, hashHex + } + + ctx, cancel := dbCtx(r.Context()) + defer cancel() + var userID string + var expiresAt time.Time + err := a.db.QueryRow(ctx, ` + select user_id::text, expires_at from sessions + where token_hash = $1 and expires_at > now()`, hash).Scan(&userID, &expiresAt) + if errors.Is(err, pgx.ErrNoRows) { + a.writeUnauthorized(w, r, "Bearer") + return nil, "" + } + if err != nil { + a.writeErr(w, reqIDFrom(r), 503, "unavailable", "database unavailable") + return nil, "" + } + u, err := a.getUserByID(r.Context(), userID) + if err != nil { + a.writeErr(w, reqIDFrom(r), 503, "unavailable", "database unavailable") + return nil, "" + } + if u == nil { + a.writeUnauthorized(w, r, "Bearer") + return nil, "" + } + a.cache.SetSession(r.Context(), hashHex, userID, expiresAt) + return u, hashHex +} + +func (a *App) handleCreateUser(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + var body struct { + Username *string `json:"username"` + DisplayName *string `json:"display_name"` + Password *string `json:"password"` + } + if !a.readJSON(w, r, &body) { + return + } + if body.Username == nil || body.DisplayName == nil || body.Password == nil { + a.writeErr(w, reqID, 400, "invalid_request", "username, display_name and password required") + return + } + username := *body.Username + display := *body.DisplayName + password := *body.Password + if !usernameRe.MatchString(username) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid username") + return + } + n := utf8.RuneCountInString(display) + if n < 1 || n > 100 || isBlank(display) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid display_name") + return + } + if !validPassword(password) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid password") + return + } + ph, err := hashPassword(password) + if err != nil { + a.writeErr(w, reqID, 500, "internal_error", "internal error") + return + } + id := newUUID() + createdAt := nowUTC() + ctx, cancel := dbCtx(r.Context()) + defer cancel() + _, err = a.db.Exec(ctx, ` + insert into users(id, username, display_name, password_hash, created_at) + values($1::uuid, $2, $3, $4, $5)`, id, username, display, ph, createdAt) + if isPgCode(err, "23505") { + a.writeErr(w, reqID, 409, "conflict", "username taken") + return + } + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + a.writeJSON(w, 201, map[string]any{ + "id": id, + "username": username, + "display_name": display, + "created_at": formatTime(createdAt), + }) +} + +func (a *App) handleCreateSession(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + username, password, ok := parseBasic(r.Header.Get("Authorization")) + if !ok { + a.writeUnauthorized(w, r, "Basic") + return + } + if !usernameRe.MatchString(username) || !validPassword(password) { + a.writeUnauthorized(w, r, "Basic") + return + } + ctx, cancel := dbCtx(r.Context()) + defer cancel() + var u User + err := a.db.QueryRow(ctx, ` + select id::text, username, display_name, password_hash, created_at + from users where username = $1`, username).Scan( + &u.ID, &u.Username, &u.DisplayName, &u.PasswordHash, &u.CreatedAt) + if errors.Is(err, pgx.ErrNoRows) { + a.writeUnauthorized(w, r, "Basic") + return + } + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + match, err := checkPassword(u.PasswordHash, password) + if err != nil || !match { + a.writeUnauthorized(w, r, "Basic") + return + } + token, hash, hashHex, err := newToken() + if err != nil { + a.writeErr(w, reqID, 500, "internal_error", "internal error") + return + } + createdAt := nowUTC() + expiresAt := createdAt.Add(a.sessionTTL) + _, err = a.db.Exec(ctx, ` + insert into sessions(token_hash, user_id, created_at, expires_at) + values($1, $2::uuid, $3, $4)`, hash, u.ID, createdAt, expiresAt) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + a.cache.SetSession(r.Context(), hashHex, u.ID, expiresAt) + a.writeJSON(w, 201, map[string]any{ + "token": token, + "token_type": "Bearer", + "expires_at": formatTime(expiresAt), + "user": userJSON(&u), + }) +} + +func (a *App) handleRevokeSession(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + token, ok := parseBearer(r.Header.Get("Authorization")) + if !ok { + a.writeUnauthorized(w, r, "Bearer") + return + } + hash, hashHex := tokenHash(token) + ctx, cancel := dbCtx(r.Context()) + defer cancel() + tag, err := a.db.Exec(ctx, ` + delete from sessions where token_hash = $1 and expires_at > now()`, hash) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + a.cache.DelSession(r.Context(), hashHex) + if tag.RowsAffected() == 0 { + a.writeUnauthorized(w, r, "Bearer") + return + } + w.Header().Set("X-Request-Id", reqID) + w.WriteHeader(204) +} + +func (a *App) handleMe(w http.ResponseWriter, r *http.Request) { + u, _ := a.authUser(w, r) + if u == nil { + return + } + a.writeJSON(w, 200, userJSON(u)) +} + +func (a *App) handleCreateChat(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + u, _ := a.authUser(w, r) + if u == nil { + return + } + var body struct { + Title *string `json:"title"` + MemberIDs *[]string `json:"member_ids"` + } + if !a.readJSON(w, r, &body) { + return + } + if body.Title == nil || body.MemberIDs == nil { + a.writeErr(w, reqID, 400, "invalid_request", "title and member_ids required") + return + } + title := *body.Title + n := utf8.RuneCountInString(title) + if n < 1 || n > 100 || isBlank(title) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid title") + return + } + members := *body.MemberIDs + if len(members) < 1 || len(members) > 100 { + a.writeErr(w, reqID, 400, "invalid_request", "invalid member_ids") + return + } + seen := make(map[string]struct{}, len(members)) + hasCaller := false + for _, id := range members { + if !isUUID(id) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid member id") + return + } + if _, dup := seen[id]; dup { + a.writeErr(w, reqID, 400, "invalid_request", "duplicate member") + return + } + seen[id] = struct{}{} + if id == u.ID { + hasCaller = true + } + } + if !hasCaller { + a.writeErr(w, reqID, 400, "invalid_request", "caller must be member") + return + } + + chatID := newUUID() + createdAt := nowUTC() + ctx, cancel := dbCtx(r.Context()) + defer cancel() + tx, err := a.db.Begin(ctx) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + defer tx.Rollback(ctx) + + _, err = tx.Exec(ctx, `insert into chats(id, title, created_at) values($1::uuid, $2, $3)`, + chatID, title, createdAt) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + for _, mid := range members { + _, err = tx.Exec(ctx, `insert into chat_members(chat_id, user_id) values($1::uuid, $2::uuid)`, + chatID, mid) + if isPgCode(err, "23503") { + a.writeErr(w, reqID, 400, "invalid_request", "unknown member") + return + } + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + } + if err := tx.Commit(ctx); err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + a.writeJSON(w, 201, map[string]any{ + "id": chatID, + "title": title, + "member_ids": members, + "created_at": formatTime(createdAt), + }) +} + +func encodeCursor(parts ...string) string { + return base64.RawURLEncoding.EncodeToString([]byte(strings.Join(parts, "|"))) +} + +func decodeCursor(s string) ([]string, error) { + b, err := base64.RawURLEncoding.DecodeString(s) + if err != nil { + return nil, err + } + return strings.Split(string(b), "|"), nil +} + +func parseLimit(r *http.Request) (int, error) { + q := r.URL.Query().Get("limit") + if q == "" { + return 50, nil + } + n, err := strconv.Atoi(q) + if err != nil || n < 1 || n > 100 { + return 0, fmt.Errorf("bad limit") + } + return n, nil +} + +func (a *App) loadChatMembers(ctx context.Context, chatID string) ([]string, error) { + ctx, cancel := dbCtx(ctx) + defer cancel() + rows, err := a.db.Query(ctx, ` + select user_id::text from chat_members where chat_id = $1::uuid order by user_id`, chatID) + if err != nil { + return nil, err + } + defer rows.Close() + var ids []string + for rows.Next() { + var id string + if err := rows.Scan(&id); err != nil { + return nil, err + } + ids = append(ids, id) + } + return ids, rows.Err() +} + +func (a *App) isMember(ctx context.Context, chatID, userID string) (bool, error) { + ctx, cancel := dbCtx(ctx) + defer cancel() + var ok bool + err := a.db.QueryRow(ctx, ` + select exists( + select 1 from chat_members where chat_id = $1::uuid and user_id = $2::uuid + )`, chatID, userID).Scan(&ok) + return ok, err +} + +func (a *App) handleListChats(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + u, _ := a.authUser(w, r) + if u == nil { + return + } + limit, err := parseLimit(r) + if err != nil { + a.writeErr(w, reqID, 400, "invalid_request", "invalid limit") + return + } + var afterTS *time.Time + var afterID string + if cur := r.URL.Query().Get("cursor"); cur != "" { + parts, err := decodeCursor(cur) + if err != nil || len(parts) != 3 { + a.writeErr(w, reqID, 400, "invalid_request", "invalid cursor") + return + } + if parts[0] != u.ID { + a.writeErr(w, reqID, 400, "invalid_request", "invalid cursor") + return + } + ts, err := parseTime(parts[1]) + if err != nil || !isUUID(parts[2]) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid cursor") + return + } + afterTS = &ts + afterID = parts[2] + } + + ctx, cancel := dbCtx(r.Context()) + defer cancel() + var rows pgx.Rows + if afterTS == nil { + rows, err = a.db.Query(ctx, ` + select c.id::text, c.title, c.created_at + from chats c + join chat_members m on m.chat_id = c.id + where m.user_id = $1::uuid + order by c.created_at, c.id + limit $2`, u.ID, limit+1) + } else { + rows, err = a.db.Query(ctx, ` + select c.id::text, c.title, c.created_at + from chats c + join chat_members m on m.chat_id = c.id + where m.user_id = $1::uuid + and (c.created_at, c.id) > ($2, $3::uuid) + order by c.created_at, c.id + limit $4`, u.ID, *afterTS, afterID, limit+1) + } + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + defer rows.Close() + + type row struct { + id, title string + created time.Time + } + var list []row + for rows.Next() { + var x row + if err := rows.Scan(&x.id, &x.title, &x.created); err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + list = append(list, x) + } + if err := rows.Err(); err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + hasMore := len(list) > limit + if hasMore { + list = list[:limit] + } + out := make([]map[string]any, 0, len(list)) + for _, c := range list { + members, err := a.loadChatMembers(r.Context(), c.id) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + out = append(out, map[string]any{ + "id": c.id, + "title": c.title, + "member_ids": members, + "created_at": formatTime(c.created), + }) + } + var next any = nil + if hasMore && len(list) > 0 { + last := list[len(list)-1] + next = encodeCursor(u.ID, formatTime(last.created), last.id) + } + a.writeJSON(w, 200, map[string]any{"items": out, "next_cursor": next}) +} + +func (a *App) handleGetChat(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + u, _ := a.authUser(w, r) + if u == nil { + return + } + chatID := r.PathValue("chat_id") + if !isUUID(chatID) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid chat_id") + return + } + ok, err := a.isMember(r.Context(), chatID, u.ID) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + if !ok { + a.writeErr(w, reqID, 404, "not_found", "chat not found") + return + } + ctx, cancel := dbCtx(r.Context()) + defer cancel() + var title string + var createdAt time.Time + err = a.db.QueryRow(ctx, `select title, created_at from chats where id = $1::uuid`, chatID). + Scan(&title, &createdAt) + if errors.Is(err, pgx.ErrNoRows) { + a.writeErr(w, reqID, 404, "not_found", "chat not found") + return + } + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + members, err := a.loadChatMembers(r.Context(), chatID) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + a.writeJSON(w, 200, map[string]any{ + "id": chatID, + "title": title, + "member_ids": members, + "created_at": formatTime(createdAt), + }) +} + +func (a *App) handleCreateMessage(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + u, _ := a.authUser(w, r) + if u == nil { + return + } + chatID := r.PathValue("chat_id") + if !isUUID(chatID) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid chat_id") + return + } + ok, err := a.isMember(r.Context(), chatID, u.ID) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + if !ok { + a.writeErr(w, reqID, 404, "not_found", "chat not found") + return + } + var body struct { + Text *string `json:"text"` + } + if !a.readJSON(w, r, &body) { + return + } + if body.Text == nil { + a.writeErr(w, reqID, 400, "invalid_request", "text required") + return + } + text := *body.Text + n := utf8.RuneCountInString(text) + if n < 1 || n > 4000 || isBlank(text) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid text") + return + } + msgID := newUUID() + createdAt := nowUTC() + ctx, cancel := dbCtx(r.Context()) + defer cancel() + _, err = a.db.Exec(ctx, ` + insert into messages(id, chat_id, sender_id, text, created_at) + values($1::uuid, $2::uuid, $3::uuid, $4, $5)`, + msgID, chatID, u.ID, text, createdAt) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + a.writeJSON(w, 201, map[string]any{ + "id": msgID, + "chat_id": chatID, + "sender_id": u.ID, + "text": text, + "created_at": formatTime(createdAt), + }) +} + +func (a *App) handleListMessages(w http.ResponseWriter, r *http.Request) { + reqID := reqIDFrom(r) + u, _ := a.authUser(w, r) + if u == nil { + return + } + chatID := r.PathValue("chat_id") + if !isUUID(chatID) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid chat_id") + return + } + ok, err := a.isMember(r.Context(), chatID, u.ID) + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + if !ok { + a.writeErr(w, reqID, 404, "not_found", "chat not found") + return + } + limit, err := parseLimit(r) + if err != nil { + a.writeErr(w, reqID, 400, "invalid_request", "invalid limit") + return + } + var afterTS *time.Time + var afterID string + if cur := r.URL.Query().Get("cursor"); cur != "" { + parts, err := decodeCursor(cur) + if err != nil || len(parts) != 3 { + a.writeErr(w, reqID, 400, "invalid_request", "invalid cursor") + return + } + if parts[0] != chatID { + a.writeErr(w, reqID, 400, "invalid_request", "invalid cursor") + return + } + ts, err := parseTime(parts[1]) + if err != nil || !isUUID(parts[2]) { + a.writeErr(w, reqID, 400, "invalid_request", "invalid cursor") + return + } + afterTS = &ts + afterID = parts[2] + } + + ctx, cancel := dbCtx(r.Context()) + defer cancel() + var rows pgx.Rows + if afterTS == nil { + rows, err = a.db.Query(ctx, ` + select id::text, chat_id::text, sender_id::text, text, created_at + from messages + where chat_id = $1::uuid + order by created_at, id + limit $2`, chatID, limit+1) + } else { + rows, err = a.db.Query(ctx, ` + select id::text, chat_id::text, sender_id::text, text, created_at + from messages + where chat_id = $1::uuid + and (created_at, id) > ($2, $3::uuid) + order by created_at, id + limit $4`, chatID, *afterTS, afterID, limit+1) + } + if err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + defer rows.Close() + + type row struct { + id, chatID, senderID, text string + created time.Time + } + var list []row + for rows.Next() { + var x row + if err := rows.Scan(&x.id, &x.chatID, &x.senderID, &x.text, &x.created); err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + list = append(list, x) + } + if err := rows.Err(); err != nil { + a.writeErr(w, reqID, 503, "unavailable", "database unavailable") + return + } + hasMore := len(list) > limit + if hasMore { + list = list[:limit] + } + out := make([]map[string]any, 0, len(list)) + for _, m := range list { + out = append(out, map[string]any{ + "id": m.id, + "chat_id": m.chatID, + "sender_id": m.senderID, + "text": m.text, + "created_at": formatTime(m.created), + }) + } + var next any = nil + if hasMore && len(list) > 0 { + last := list[len(list)-1] + next = encodeCursor(chatID, formatTime(last.created), last.id) + } + a.writeJSON(w, 200, map[string]any{"items": out, "next_cursor": next}) +} + +func envOr(key, def string) string { + if v := os.Getenv(key); v != "" { + return v + } + return def +} diff --git a/main.go b/main.go new file mode 100644 index 00000000..ffab3478 --- /dev/null +++ b/main.go @@ -0,0 +1,76 @@ +package main + +import ( + "context" + "log/slog" + "net" + "net/http" + "os" + "os/signal" + "strconv" + "syscall" + "time" +) + +func main() { + log := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{Level: slog.LevelInfo})) + + host := envOr("APP_HOST", "0.0.0.0") + port := envOr("APP_PORT", "8080") + shutdownSec, _ := strconv.Atoi(envOr("SHUTDOWN_TIMEOUT_SECONDS", "5")) + if shutdownSec < 1 { + shutdownSec = 5 + } + ttlSec, _ := strconv.Atoi(envOr("SESSION_TTL_SECONDS", "3600")) + if ttlSec < 1 { + ttlSec = 3600 + } + + ctx := context.Background() + db, err := openDB(ctx) + if err != nil { + log.Error("database open failed", "err", err.Error()) + os.Exit(1) + } + defer db.Close() + + if err := migrate(ctx, db); err != nil { + log.Error("migrate failed", "err", err.Error()) + os.Exit(1) + } + log.Info("migrations applied") + + cache := openCache(log) + defer cache.Close() + + app := NewApp(db, cache, log, time.Duration(ttlSec)*time.Second) + + addr := net.JoinHostPort(host, port) + srv := &http.Server{ + Addr: addr, + Handler: app.Routes(), + ReadHeaderTimeout: 5 * time.Second, + } + + go func() { + log.Info("listening", "addr", addr, "env", envOr("APP_ENV", "lab"), "session_ttl_s", ttlSec) + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + log.Error("server error", "err", err.Error()) + os.Exit(1) + } + }() + + stop := make(chan os.Signal, 1) + signal.Notify(stop, syscall.SIGTERM, syscall.SIGINT) + sig := <-stop + log.Info("shutdown started", "signal", sig.String()) + app.SetReady(false) + + shCtx, cancel := context.WithTimeout(context.Background(), time.Duration(shutdownSec)*time.Second) + defer cancel() + if err := srv.Shutdown(shCtx); err != nil { + log.Error("shutdown error", "err", err.Error()) + os.Exit(1) + } + log.Info("shutdown finished") +} diff --git a/migrate.go b/migrate.go new file mode 100644 index 00000000..dcfbc6a8 --- /dev/null +++ b/migrate.go @@ -0,0 +1,74 @@ +package main + +import ( + "context" + "embed" + "fmt" + "sort" + "strings" + "time" + + "github.com/jackc/pgx/v5/pgxpool" +) + +//go:embed migrations/*.sql +var migrationFS embed.FS + +func migrate(ctx context.Context, pool *pgxpool.Pool) error { + ctx, cancel := context.WithTimeout(ctx, 30*time.Second) + defer cancel() + + _, err := pool.Exec(ctx, ` + create table if not exists schema_migrations ( + version text primary key, + applied_at timestamptz not null + )`) + if err != nil { + return fmt.Errorf("schema_migrations: %w", err) + } + + entries, err := migrationFS.ReadDir("migrations") + if err != nil { + return err + } + var names []string + for _, e := range entries { + if !e.IsDir() && strings.HasSuffix(e.Name(), ".sql") { + names = append(names, e.Name()) + } + } + sort.Strings(names) + + for _, name := range names { + var exists bool + err := pool.QueryRow(ctx, `select exists(select 1 from schema_migrations where version=$1)`, name).Scan(&exists) + if err != nil { + return err + } + if exists { + continue + } + + body, err := migrationFS.ReadFile("migrations/" + name) + if err != nil { + return err + } + + tx, err := pool.Begin(ctx) + if err != nil { + return err + } + if _, err := tx.Exec(ctx, string(body)); err != nil { + _ = tx.Rollback(ctx) + return fmt.Errorf("migration %s: %w", name, err) + } + if _, err := tx.Exec(ctx, `insert into schema_migrations(version, applied_at) values($1, now())`, name); err != nil { + _ = tx.Rollback(ctx) + return err + } + if err := tx.Commit(ctx); err != nil { + return err + } + } + return nil +} diff --git a/migrations/0001_init.sql b/migrations/0001_init.sql new file mode 100644 index 00000000..a50290e5 --- /dev/null +++ b/migrations/0001_init.sql @@ -0,0 +1,38 @@ +create table 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 +); + +create table chats ( + id uuid primary key, + title text not null, + created_at timestamptz not null +); + +create table chat_members ( + chat_id uuid not null references chats(id), + user_id uuid not null references users(id), + primary key (chat_id, user_id) +); + +create index chat_members_user_idx on chat_members (user_id); + +create table messages ( + id uuid primary key, + chat_id uuid not null references chats(id), + sender_id uuid not null references users(id), + text text not null, + created_at timestamptz not null +); + +create index messages_history_idx on messages (chat_id, created_at, id); + +create table sessions ( + token_hash bytea primary key, + user_id uuid not null references users(id) on delete cascade, + created_at timestamptz not null, + expires_at timestamptz not null +); diff --git a/models.go b/models.go new file mode 100644 index 00000000..c6fd8042 --- /dev/null +++ b/models.go @@ -0,0 +1,33 @@ +package main + +import "time" + +type User struct { + ID string + Username string + DisplayName string + PasswordHash string + CreatedAt time.Time +} + +type Chat struct { + ID string + Title string + MemberIDs []string + CreatedAt time.Time +} + +type Message struct { + ID string + ChatID string + SenderID string + Text string + CreatedAt time.Time +} + +type Session struct { + TokenHash []byte + UserID string + CreatedAt time.Time + ExpiresAt time.Time +} diff --git a/scripts/explain.sh b/scripts/explain.sh new file mode 100755 index 00000000..329a07ae --- /dev/null +++ b/scripts/explain.sh @@ -0,0 +1,59 @@ +#!/bin/sh +set -eu +ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)" +cd "$ROOT" +OUT="${1:-evidence/explain.txt}" +mkdir -p "$(dirname "$OUT")" + +echo "=== seed >=10000 messages ===" +docker compose --env-file .env exec -T postgres psql -U messenger -d messenger <<'SQL' +do $$ +declare + uid uuid := gen_random_uuid(); + cid uuid := gen_random_uuid(); +begin + insert into users(id, username, display_name, password_hash, created_at) + values (uid, 'explain_seed_' || substr(replace(uid::text,'-',''),1,8), 'Explain', 'x', now()) + on conflict do nothing; + insert into chats(id, title, created_at) values (cid, 'explain chat', now()); + insert into chat_members(chat_id, user_id) values (cid, uid); + insert into messages(id, chat_id, sender_id, text, created_at) + select gen_random_uuid(), cid, uid, 'msg-' || g, now() - ((10001 - g) * interval '1 millisecond') + from generate_series(1, 10001) g; + raise notice 'chat_id=%', cid; +end $$; +SQL + +CHAT=$(docker compose --env-file .env exec -T postgres psql -U messenger -d messenger -Atc \ + "select id from chats where title='explain chat' order by created_at desc limit 1;") +echo "chat_id=$CHAT" + +{ + echo "date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" + echo "chat_id=$CHAT" + echo "count=$(docker compose --env-file .env exec -T postgres psql -U messenger -d messenger -Atc "select count(*) from messages where chat_id='$CHAT';")" + echo + echo "=== keyset with index messages_history_idx ===" + docker compose --env-file .env exec -T postgres psql -U messenger -d messenger < ('1970-01-01'::timestamptz, '00000000-0000-4000-8000-000000000000'::uuid) +ORDER BY created_at, id +LIMIT 50; +SQL + echo + echo "=== OFFSET page ~200 (full scan risk) ===" + docker compose --env-file .env exec -T postgres psql -U messenger -d messenger < "$OUT" +echo "wrote $OUT" +cat "$OUT" +echo "PASS: explain captured" diff --git a/scripts/mksecrets.sh b/scripts/mksecrets.sh new file mode 100755 index 00000000..5d503ca0 --- /dev/null +++ b/scripts/mksecrets.sh @@ -0,0 +1,13 @@ +#!/bin/sh +set -eu +ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)" +mkdir -p "$ROOT/secrets" +if [ ! -f "$ROOT/secrets/postgres_password" ]; then + openssl rand -base64 24 | tr -d '\n' > "$ROOT/secrets/postgres_password" +fi +PASS=$(cat "$ROOT/secrets/postgres_password") +ENC=$(printf '%s' "$PASS" | python3 -c 'import sys,urllib.parse; print(urllib.parse.quote(sys.stdin.read(), safe=""))') +printf 'postgres://messenger:%s@postgres:5432/messenger?sslmode=disable\n' "$ENC" > "$ROOT/secrets/database_url" +printf 'redis://valkey:6379/0\n' > "$ROOT/secrets/cache_url" +chmod 644 "$ROOT/secrets/"* +echo "secrets ready in $ROOT/secrets" diff --git a/scripts/restart.sh b/scripts/restart.sh new file mode 100755 index 00000000..6ffe2c7e --- /dev/null +++ b/scripts/restart.sh @@ -0,0 +1,7 @@ +#!/bin/sh +set -eu +ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)" +cd "$ROOT" +docker compose --env-file .env stop +docker compose --env-file .env start +exit 0 diff --git a/scripts/test-deps-down.sh b/scripts/test-deps-down.sh new file mode 100755 index 00000000..b00e4d7d --- /dev/null +++ b/scripts/test-deps-down.sh @@ -0,0 +1,54 @@ +#!/bin/sh +set -eu +ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)" +cd "$ROOT" +BASE="${BASE_URL:-http://127.0.0.1:8080}" + +USER="dep_$(date +%s)" +PASS="T3st-$(openssl rand -hex 8)" +curl -sf -X POST "$BASE/api/v1/users" \ + -H 'Content-Type: application/json' \ + -d "{\"username\":\"$USER\",\"display_name\":\"Deps\",\"password\":\"$PASS\"}" >/tmp/dep_user.json +CRED=$(printf '%s:%s' "$USER" "$PASS" | base64 -w0 2>/dev/null || printf '%s:%s' "$USER" "$PASS" | base64) +SESS=$(curl -sf -X POST "$BASE/api/v1/auth/sessions" -H "Authorization: Basic $CRED") +TOKEN=$(printf '%s' "$SESS" | python3 -c 'import sys,json; print(json.load(sys.stdin)["token"])') + +echo "=== stop valkey (cache bypass) ===" +docker compose --env-file .env stop valkey >/dev/null +sleep 1 +code=$(curl -sS -o /tmp/dep_vk.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me") +echo "with valkey down /users/me -> $code (expect 200)" +test "$code" = "200" + +echo "=== start valkey, stop postgres ===" +docker compose --env-file .env start valkey >/dev/null +i=0 +while [ "$i" -lt 20 ]; do + if docker compose --env-file .env exec -T valkey valkey-cli ping 2>/dev/null | grep -q PONG; then + break + fi + i=$((i + 1)) + sleep 0.5 +done +docker compose --env-file .env stop postgres >/dev/null +sleep 1 +ready=$(curl -sS -o /tmp/dep_ready.json -w '%{http_code}' "$BASE/health/ready" || true) +me=$(curl -sS -o /tmp/dep_me.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me" || true) +echo "postgres down ready=$ready me=$me (expect 503/503)" +test "$ready" = "503" +test "$me" = "503" + +echo "=== restore postgres ===" +docker compose --env-file .env start postgres >/dev/null +i=0 +while [ "$i" -lt 40 ]; do + if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then + break + fi + i=$((i + 1)) + sleep 0.5 +done +code=$(curl -sS -o /tmp/dep_ok.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me") +echo "restored /users/me -> $code (expect 200)" +test "$code" = "200" +echo "PASS: dependency failures" diff --git a/scripts/test-expiry.sh b/scripts/test-expiry.sh new file mode 100755 index 00000000..9e2bce55 --- /dev/null +++ b/scripts/test-expiry.sh @@ -0,0 +1,46 @@ +#!/bin/sh +set -eu +ROOT="$(CDPATH= cd -- "$(dirname "$0")/.." && pwd)" +cd "$ROOT" +BASE="${BASE_URL:-http://127.0.0.1:8080}" + +echo "=== restart api with SESSION_TTL_SECONDS=2 ===" +SESSION_TTL_SECONDS=2 docker compose --env-file .env up -d --no-deps --force-recreate api >/dev/null +i=0 +while [ "$i" -lt 40 ]; do + if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then + break + fi + i=$((i + 1)) + sleep 0.5 +done + +USER="exp_$(date +%s)" +PASS="T3st-$(openssl rand -hex 8)" +curl -sf -X POST "$BASE/api/v1/users" \ + -H 'Content-Type: application/json' \ + -d "{\"username\":\"$USER\",\"display_name\":\"Expiry\",\"password\":\"$PASS\"}" >/tmp/exp_user.json +CRED=$(printf '%s:%s' "$USER" "$PASS" | base64 -w0 2>/dev/null || printf '%s:%s' "$USER" "$PASS" | base64) +SESS=$(curl -sf -X POST "$BASE/api/v1/auth/sessions" -H "Authorization: Basic $CRED") +TOKEN=$(printf '%s' "$SESS" | python3 -c 'import sys,json; print(json.load(sys.stdin)["token"])') +code=$(curl -sS -o /tmp/exp_me1.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me") +echo "immediate /users/me -> $code (expect 200)" +test "$code" = "200" + +echo "sleep 3 for expiry" +sleep 3 +code=$(curl -sS -o /tmp/exp_me2.json -w '%{http_code}' -H "Authorization: Bearer $TOKEN" "$BASE/api/v1/users/me" || true) +echo "after ttl /users/me -> $code (expect 401)" +test "$code" = "401" + +echo "=== restore SESSION_TTL_SECONDS=3600 ===" +SESSION_TTL_SECONDS=3600 docker compose --env-file .env up -d --no-deps --force-recreate api >/dev/null +i=0 +while [ "$i" -lt 40 ]; do + if curl -sf "$BASE/health/ready" >/dev/null 2>&1; then + break + fi + i=$((i + 1)) + sleep 0.5 +done +echo "PASS: session expiry" diff --git a/tests/test_own.py b/tests/test_own.py new file mode 100644 index 00000000..9ef2bd75 --- /dev/null +++ b/tests/test_own.py @@ -0,0 +1,210 @@ +#!/usr/bin/env python3 +import base64 +import json +import os +import unittest +import urllib.error +import urllib.request +import uuid +from urllib.parse import quote + +BASE = os.environ.get('BASE_URL', 'http://localhost:8080').rstrip('/') + + +def call(method, path, body=None, headers=None, raw=None): + h = {'Accept': 'application/json'} + data = None + if body is not None: + data = json.dumps(body, ensure_ascii=False).encode() + h['Content-Type'] = 'application/json' + if raw is not None: + data = raw + h.update(headers or {}) + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + resp = urllib.request.urlopen(req, timeout=10) + except urllib.error.HTTPError as e: + resp = e + raw_body = resp.read() + payload = None + if raw_body: + try: + payload = json.loads(raw_body) + except Exception: + payload = None + hdrs = {k.lower(): v for k, v in resp.headers.items()} + return resp.code, hdrs, payload + + +def make_user(name=None): + username = name or ('u_' + uuid.uuid4().hex[:10]) + password = 'T3st-' + uuid.uuid4().hex + code, _, user = call('POST', '/api/v1/users', { + 'username': username, + 'display_name': 'User ' + username, + 'password': password, + }) + assert code == 201, code + cred = base64.b64encode((username + ':' + password).encode()).decode() + code, _, session = call('POST', '/api/v1/auth/sessions', headers={ + 'Authorization': 'Basic ' + cred, + }) + assert code == 201, code + return {'user': user, 'token': session['token'], 'password': password, 'username': username} + + +def auth(actor): + return {'Authorization': 'Bearer ' + actor['token']} + + +class OwnTests(unittest.TestCase): + def test_bad_json(self): + code, hdrs, body = call( + 'POST', '/api/v1/users', + raw=b'{not json', + headers={'Content-Type': 'application/json'}, + ) + self.assertEqual(code, 400) + self.assertTrue(hdrs.get('x-request-id')) + self.assertEqual(body['error']['code'], 'invalid_request') + + def test_duplicate_username(self): + name = 'dup_' + uuid.uuid4().hex[:8] + make_user(name) + code, _, body = call('POST', '/api/v1/users', { + 'username': name, + 'display_name': 'Other', + 'password': 'T3st-' + uuid.uuid4().hex, + }) + self.assertEqual(code, 409) + self.assertEqual(body['error']['code'], 'conflict') + + def test_wrong_password(self): + a = make_user() + bad = base64.b64encode((a['username'] + ':wrong-password-xx').encode()).decode() + code, hdrs, body = call('POST', '/api/v1/auth/sessions', headers={ + 'Authorization': 'Basic ' + bad, + }) + self.assertEqual(code, 401) + self.assertTrue((hdrs.get('www-authenticate') or '').lower().startswith('basic')) + self.assertEqual(body['error']['code'], 'unauthorized') + + def test_basic_on_business(self): + a = make_user() + cred = base64.b64encode((a['username'] + ':' + a['password']).encode()).decode() + code, hdrs, _ = call('GET', '/api/v1/users/me', headers={ + 'Authorization': 'Basic ' + cred, + }) + self.assertEqual(code, 401) + self.assertTrue((hdrs.get('www-authenticate') or '').lower().startswith('bearer')) + + def test_x_user_id_ignored(self): + a = make_user() + b = make_user() + code, _, me = call('GET', '/api/v1/users/me', headers={ + 'Authorization': 'Bearer ' + a['token'], + 'X-User-Id': b['user']['id'], + }) + self.assertEqual(code, 200) + self.assertEqual(me['id'], a['user']['id']) + code, _, _ = call('GET', '/api/v1/users/me', headers={ + 'X-User-Id': a['user']['id'], + }) + self.assertEqual(code, 401) + + def test_logout(self): + a = make_user() + code, _, _ = call('DELETE', '/api/v1/auth/sessions/current', headers=auth(a)) + self.assertEqual(code, 204) + code, _, _ = call('GET', '/api/v1/users/me', headers=auth(a)) + self.assertEqual(code, 401) + code, _, _ = call('DELETE', '/api/v1/auth/sessions/current', headers=auth(a)) + self.assertEqual(code, 401) + + def test_empty_message(self): + a = make_user() + b = make_user() + code, _, chat = call('POST', '/api/v1/chats', { + 'title': 't', + 'member_ids': [a['user']['id'], b['user']['id']], + }, headers=auth(a)) + self.assertEqual(code, 201) + path = '/api/v1/chats/' + chat['id'] + '/messages' + code, _, _ = call('POST', path, {'text': ''}, headers=auth(a)) + self.assertEqual(code, 400) + code, _, _ = call('POST', path, {'text': ' '}, headers=auth(a)) + self.assertEqual(code, 400) + + def test_foreign_chat(self): + a = make_user() + b = make_user() + eve = make_user() + code, _, chat = call('POST', '/api/v1/chats', { + 'title': 'private', + 'member_ids': [a['user']['id'], b['user']['id']], + }, headers=auth(a)) + self.assertEqual(code, 201) + path = '/api/v1/chats/' + chat['id'] + code, _, _ = call('GET', path, headers=auth(eve)) + self.assertEqual(code, 404) + code, _, _ = call('POST', path + '/messages', {'text': 'hi'}, headers=auth(eve)) + self.assertEqual(code, 404) + + def test_pagination_pages(self): + a = make_user() + code, _, chat = call('POST', '/api/v1/chats', { + 'title': 'pages', + 'member_ids': [a['user']['id']], + }, headers=auth(a)) + self.assertEqual(code, 201) + path = '/api/v1/chats/' + chat['id'] + '/messages' + ids = [] + for i in range(7): + code, _, msg = call('POST', path, {'text': 'm%d' % i}, headers=auth(a)) + self.assertEqual(code, 201) + ids.append(msg['id']) + seen = [] + cursor = None + pages = 0 + while True: + url = path + '?limit=2' + if cursor: + url += '&cursor=' + quote(cursor, safe='') + code, _, page = call('GET', url, headers=auth(a)) + self.assertEqual(code, 200) + pages += 1 + for item in page['items']: + seen.append(item['id']) + cursor = page['next_cursor'] + if cursor is None: + break + self.assertEqual(pages, 4) + self.assertEqual(seen, ids) + self.assertEqual(len(set(seen)), 7) + + def test_cursor_other_chat(self): + a = make_user() + _, _, c1 = call('POST', '/api/v1/chats', { + 'title': 'c1', 'member_ids': [a['user']['id']], + }, headers=auth(a)) + _, _, c2 = call('POST', '/api/v1/chats', { + 'title': 'c2', 'member_ids': [a['user']['id']], + }, headers=auth(a)) + p1 = '/api/v1/chats/' + c1['id'] + '/messages' + p2 = '/api/v1/chats/' + c2['id'] + '/messages' + call('POST', p1, {'text': 'one'}, headers=auth(a)) + call('POST', p1, {'text': 'two'}, headers=auth(a)) + code, _, page = call('GET', p1 + '?limit=1', headers=auth(a)) + self.assertEqual(code, 200) + cur = page['next_cursor'] + self.assertTrue(cur) + code, _, body = call( + 'GET', p2 + '?limit=1&cursor=' + quote(cur, safe=''), + headers=auth(a), + ) + self.assertEqual(code, 400) + self.assertEqual(body['error']['code'], 'invalid_request') + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_race_register.py b/tests/test_race_register.py new file mode 100644 index 00000000..9435c261 --- /dev/null +++ b/tests/test_race_register.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 +import json +import os +import sys +import urllib.error +import urllib.request +import uuid +from concurrent.futures import ThreadPoolExecutor, as_completed + +BASE = os.environ.get('BASE_URL', 'http://localhost:8080').rstrip('/') + + +def register(username, password): + body = json.dumps({ + 'username': username, + 'display_name': 'Race ' + username, + 'password': password, + }).encode() + req = urllib.request.Request( + BASE + '/api/v1/users', + data=body, + headers={'Content-Type': 'application/json', 'Accept': 'application/json'}, + method='POST', + ) + try: + resp = urllib.request.urlopen(req, timeout=15) + except urllib.error.HTTPError as e: + resp = e + return resp.code + + +def main(): + username = 'race_' + uuid.uuid4().hex[:12] + password = 'T3st-' + uuid.uuid4().hex + codes = [] + with ThreadPoolExecutor(max_workers=20) as pool: + futs = [pool.submit(register, username, password) for _ in range(20)] + for f in as_completed(futs): + codes.append(f.result()) + ok = codes.count(201) + conflict = codes.count(409) + print('codes=%s created=%d conflict=%d' % (sorted(codes), ok, conflict)) + if ok != 1 or conflict != 19: + print('FAIL: expected exactly one 201 and nineteen 409') + sys.exit(1) + print('PASS: race register handled by unique username') + + +if __name__ == '__main__': + main() diff --git a/tests/test_rollback.py b/tests/test_rollback.py new file mode 100644 index 00000000..2db93765 --- /dev/null +++ b/tests/test_rollback.py @@ -0,0 +1,71 @@ +#!/usr/bin/env python3 +import base64 +import json +import os +import sys +import urllib.error +import urllib.request +import uuid + +BASE = os.environ.get('BASE_URL', 'http://localhost:8080').rstrip('/') + + +def call(method, path, body=None, headers=None): + h = {'Accept': 'application/json'} + data = None + if body is not None: + data = json.dumps(body, ensure_ascii=False).encode() + h['Content-Type'] = 'application/json' + h.update(headers or {}) + req = urllib.request.Request(BASE + path, data=data, headers=h, method=method) + try: + resp = urllib.request.urlopen(req, timeout=10) + except urllib.error.HTTPError as e: + resp = e + raw = resp.read() + payload = json.loads(raw) if raw else None + return resp.code, payload + + +def make_actor(): + username = 'rb_' + uuid.uuid4().hex[:10] + password = 'T3st-' + uuid.uuid4().hex + code, user = call('POST', '/api/v1/users', { + 'username': username, + 'display_name': 'Rollback', + 'password': password, + }) + assert code == 201, code + cred = base64.b64encode((username + ':' + password).encode()).decode() + code, session = call('POST', '/api/v1/auth/sessions', headers={ + 'Authorization': 'Basic ' + cred, + }) + assert code == 201, code + return user, session['token'] + + +def main(): + user, token = make_actor() + fake = str(uuid.uuid4()) + code, body = call('POST', '/api/v1/chats', { + 'title': 'should rollback', + 'member_ids': [user['id'], fake], + }, headers={'Authorization': 'Bearer ' + token}) + if code != 400: + print('FAIL: expected 400, got', code, body) + sys.exit(1) + code, page = call('GET', '/api/v1/chats?limit=100', headers={ + 'Authorization': 'Bearer ' + token, + }) + if code != 200: + print('FAIL: list chats', code) + sys.exit(1) + titles = [c['title'] for c in page['items']] + if 'should rollback' in titles: + print('FAIL: rolled-back chat still listed') + sys.exit(1) + print('PASS: unknown member rolls back chat create') + + +if __name__ == '__main__': + main() -- 2.54.0