Перенести приложение и инфраструктуру из лабораторной 4
This commit is contained in:
+265
@@ -0,0 +1,265 @@
|
||||
name: highload-lab5
|
||||
|
||||
services:
|
||||
redis:
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
image: redis:7.4.11-bookworm
|
||||
command:
|
||||
[
|
||||
"redis-server",
|
||||
"--appendonly",
|
||||
"yes",
|
||||
"--appendfsync",
|
||||
"everysec",
|
||||
"--dir",
|
||||
"/data",
|
||||
]
|
||||
volumes:
|
||||
- redis_data:/data
|
||||
healthcheck:
|
||||
test: ["CMD", "redis-cli", "ping"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
db:
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
networks: [data]
|
||||
image: postgres:16.10-bookworm
|
||||
environment:
|
||||
POSTGRES_DB: ${POSTGRES_DB}
|
||||
POSTGRES_USER: ${POSTGRES_USER}
|
||||
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
healthcheck:
|
||||
test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"]
|
||||
interval: 3s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
migrate:
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
environment:
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
volumes:
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
command: ["./messenger", "migrate", "up"]
|
||||
depends_on:
|
||||
db:
|
||||
condition: service_healthy
|
||||
s3:
|
||||
cpus: 1.0
|
||||
mem_limit: 512m
|
||||
networks: [edge, data]
|
||||
ports:
|
||||
- "127.0.0.1:9000:9000"
|
||||
build:
|
||||
context: ./infra
|
||||
dockerfile: minio.Dockerfile
|
||||
environment:
|
||||
MINIO_ROOT_USER_FILE: /run/secrets/s3_access_key_id
|
||||
MINIO_ROOT_PASSWORD_FILE: /run/secrets/s3_secret_access_key
|
||||
MINIO_BROWSER: "off"
|
||||
MINIO_API_CORS_ALLOW_ORIGIN: ${S3_CORS_ORIGINS:-http://127.0.0.1:5173,http://localhost:5173}
|
||||
volumes:
|
||||
- s3_data:/data
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_secret_access_key:ro
|
||||
healthcheck:
|
||||
test: ["CMD", "curl", "-f", "http://127.0.0.1:9000/minio/health/ready"]
|
||||
interval: 3s
|
||||
timeout: 2s
|
||||
retries: 30
|
||||
s3-bootstrap:
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
environment: &s3-environment
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: /run/secrets/s3_root_access_key_id
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: /run/secrets/s3_root_secret_access_key
|
||||
S3_ENDPOINT: ${S3_ENDPOINT:-http://s3:9000}
|
||||
S3_PUBLIC_ENDPOINT: ${S3_PUBLIC_ENDPOINT:-http://127.0.0.1:9000}
|
||||
S3_REGION: ${S3_REGION:-us-east-1}
|
||||
S3_BUCKET: ${S3_BUCKET:-messenger-attachments}
|
||||
S3_ACCESS_KEY_ID_FILE: ${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}
|
||||
S3_SECRET_ACCESS_KEY_FILE: ${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}
|
||||
volumes:
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_root_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_root_secret_access_key:ro
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
command: ["./messenger", "s3-bootstrap"]
|
||||
depends_on:
|
||||
s3:
|
||||
condition: service_healthy
|
||||
api:
|
||||
cpus: 0.5
|
||||
mem_limit: 512m
|
||||
networks: [proxy, data]
|
||||
build: ./src
|
||||
deploy:
|
||||
replicas: ${API_REPLICAS:-2}
|
||||
stop_grace_period: 15s
|
||||
environment:
|
||||
<<: *s3-environment
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: ""
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: ""
|
||||
UPLOAD_TMP_DIR: /tmp/uploads
|
||||
UPLOAD_CLEANUP_GRACE_SECONDS: ${UPLOAD_CLEANUP_GRACE_SECONDS:-600}
|
||||
# Go 1.22 sees host CPUs; keep parallelism suitable for this 0.5 CPU limit.
|
||||
GOMAXPROCS: ${API_GOMAXPROCS:-1}
|
||||
GOMEMLIMIT: 384MiB
|
||||
APP_HOST: ${APP_HOST:-0.0.0.0}
|
||||
APP_PORT: ${APP_PORT:-8080}
|
||||
APP_ENV: ${APP_ENV:-lab}
|
||||
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
CACHE_URL_FILE: ${CACHE_URL_FILE:-/run/secrets/cache_url}
|
||||
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
|
||||
REDIS_ADDR: redis:6379
|
||||
DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
tmpfs:
|
||||
- /tmp/uploads:rw,noexec,nosuid,size=16m,mode=1777
|
||||
volumes:
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
- ./secrets/cache_url:${CACHE_URL_FILE:-/run/secrets/cache_url}:ro
|
||||
depends_on:
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
db:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test: ["CMD", "./messenger", "healthcheck"]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
upload-cleanup:
|
||||
restart: unless-stopped
|
||||
cpus: 0.25
|
||||
mem_limit: 128m
|
||||
networks: [data]
|
||||
build: ./src
|
||||
command: ["./messenger", "attachments-cleanup", "--loop"]
|
||||
environment:
|
||||
<<: *s3-environment
|
||||
S3_ROOT_ACCESS_KEY_ID_FILE: ""
|
||||
S3_ROOT_SECRET_ACCESS_KEY_FILE: ""
|
||||
DATABASE_URL_FILE: ${DATABASE_URL_FILE:-/run/secrets/database_url}
|
||||
volumes:
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/database_url:${DATABASE_URL_FILE:-/run/secrets/database_url}:ro
|
||||
healthcheck:
|
||||
disable: true
|
||||
depends_on:
|
||||
migrate:
|
||||
condition: service_completed_successfully
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
haproxy:
|
||||
environment:
|
||||
APP_PORT: ${APP_PORT:-8080}
|
||||
API_REPLICAS: ${API_REPLICAS:-2}
|
||||
cpus: 0.5
|
||||
mem_limit: 128m
|
||||
networks: [edge, proxy]
|
||||
image: haproxy:3.2.25-alpine
|
||||
ports:
|
||||
- "127.0.0.1:${API_HTTP_PORT:-8085}:8080"
|
||||
- "127.0.0.1:8404:8404"
|
||||
volumes:
|
||||
- ./infra/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
|
||||
depends_on:
|
||||
api:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD",
|
||||
"wget",
|
||||
"-q",
|
||||
"-O",
|
||||
"/dev/null",
|
||||
"http://127.0.0.1:8080/health/ready",
|
||||
]
|
||||
interval: 2s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
backend-tests:
|
||||
cpus: 2.0
|
||||
mem_limit: 1g
|
||||
networks: [data]
|
||||
profiles: ["test"]
|
||||
build:
|
||||
context: ./src
|
||||
target: builder
|
||||
environment:
|
||||
<<: *s3-environment
|
||||
S3_PUBLIC_ENDPOINT: http://s3:9000
|
||||
TEST_DATABASE_URL: postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@db:5432/${POSTGRES_DB}?sslmode=disable
|
||||
TEST_REDIS_ADDR: redis:6379
|
||||
volumes:
|
||||
- ./secrets/s3_api_access_key_id:${S3_ACCESS_KEY_ID_FILE:-/run/secrets/s3_access_key_id}:ro
|
||||
- ./secrets/s3_api_secret_access_key:${S3_SECRET_ACCESS_KEY_FILE:-/run/secrets/s3_secret_access_key}:ro
|
||||
- ./secrets/s3_access_key_id:/run/secrets/s3_root_access_key_id:ro
|
||||
- ./secrets/s3_secret_access_key:/run/secrets/s3_root_secret_access_key:ro
|
||||
depends_on:
|
||||
s3-bootstrap:
|
||||
condition: service_completed_successfully
|
||||
db:
|
||||
condition: service_healthy
|
||||
redis:
|
||||
condition: service_healthy
|
||||
command: ["go", "test", "-race", "-count=1", "-v", "./backend/..."]
|
||||
frontend:
|
||||
cpus: 0.5
|
||||
mem_limit: 512m
|
||||
networks: [edge]
|
||||
build:
|
||||
context: ./src/frontend
|
||||
environment:
|
||||
API_PROXY_TARGET: http://haproxy:8080
|
||||
ports:
|
||||
- "127.0.0.1:5173:5173"
|
||||
volumes:
|
||||
- ./src/frontend:/app
|
||||
- /app/node_modules
|
||||
depends_on:
|
||||
haproxy:
|
||||
condition: service_healthy
|
||||
healthcheck:
|
||||
test:
|
||||
[
|
||||
"CMD",
|
||||
"node",
|
||||
"-e",
|
||||
"fetch('http://127.0.0.1:5173/').then(r => process.exit(r.ok ? 0 : 1)).catch(() => process.exit(1))",
|
||||
]
|
||||
interval: 5s
|
||||
timeout: 3s
|
||||
retries: 20
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
redis_data:
|
||||
s3_data:
|
||||
|
||||
networks:
|
||||
edge:
|
||||
proxy:
|
||||
internal: true
|
||||
data:
|
||||
internal: true
|
||||
@@ -0,0 +1,49 @@
|
||||
global
|
||||
# Operator access only inside this container; no host port is published.
|
||||
stats socket ipv4@127.0.0.1:9999 level admin
|
||||
log stdout format raw local0
|
||||
maxconn 1024
|
||||
|
||||
defaults
|
||||
log global
|
||||
mode http
|
||||
option httplog
|
||||
timeout connect 1s
|
||||
timeout client 35s
|
||||
timeout server 35s
|
||||
timeout check 1s
|
||||
# Let the client decide which failed POST is safe to repeat.
|
||||
retries 0
|
||||
|
||||
# Docker returns the current addresses of all containers of the api service.
|
||||
resolvers docker
|
||||
nameserver docker_dns 127.0.0.11:53
|
||||
accepted_payload_size 8192
|
||||
resolve_retries 3
|
||||
timeout resolve 1s
|
||||
timeout retry 1s
|
||||
hold valid 1s
|
||||
hold obsolete 1s
|
||||
hold nx 1s
|
||||
hold timeout 1s
|
||||
hold refused 1s
|
||||
hold other 1s
|
||||
|
||||
frontend api_entry
|
||||
bind :8080
|
||||
default_backend api_replicas
|
||||
|
||||
backend api_replicas
|
||||
balance roundrobin
|
||||
option httpchk GET /health/ready
|
||||
http-check expect status 200
|
||||
# Separate slots receive distinct Docker DNS addresses. No sticky sessions.
|
||||
server-template api "${API_REPLICAS}" "api:${APP_PORT}" check inter 1s fall 2 rise 2 resolvers docker resolve-prefer ipv4 init-addr libc,none
|
||||
|
||||
# Local teaching aid: backend addresses, UP/DOWN state and request counters.
|
||||
listen statistics
|
||||
bind :8404
|
||||
stats enable
|
||||
stats show-legends
|
||||
stats uri /stats
|
||||
stats refresh 2s
|
||||
@@ -0,0 +1,19 @@
|
||||
FROM golang:1.24.2-bookworm AS builder
|
||||
WORKDIR /source
|
||||
RUN git init . \
|
||||
&& git remote add origin https://github.com/minio/minio.git \
|
||||
&& git fetch --depth 1 origin 0d7408fc9969caf07de6a8c3a84f9fbb10a6739e \
|
||||
&& git checkout --detach FETCH_HEAD
|
||||
RUN CGO_ENABLED=0 go build -trimpath \
|
||||
-ldflags="-s -w -X github.com/minio/minio/cmd.Version=2025-04-22T22:12:26Z -X github.com/minio/minio/cmd.ReleaseTag=RELEASE.2025-04-22T22-12-26Z -X github.com/minio/minio/cmd.CommitID=0d7408fc9969caf07de6a8c3a84f9fbb10a6739e" \
|
||||
-o /out/minio .
|
||||
|
||||
FROM debian:12.15-slim
|
||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates curl \
|
||||
&& rm -rf /var/lib/apt/lists/* \
|
||||
&& groupadd --system storage && useradd --system --gid storage storage \
|
||||
&& mkdir /data && chown storage:storage /data
|
||||
COPY --from=builder /out/minio /usr/local/bin/minio
|
||||
USER storage:storage
|
||||
ENTRYPOINT ["minio"]
|
||||
CMD ["server", "/data", "--console-address", ":9001"]
|
||||
@@ -0,0 +1,108 @@
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from urllib.parse import quote
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[1]
|
||||
|
||||
|
||||
def read_values(path):
|
||||
values = {}
|
||||
if path.exists():
|
||||
for line in path.read_text(encoding="utf-8").splitlines():
|
||||
line = line.strip()
|
||||
if line and not line.startswith("#") and "=" in line:
|
||||
key, value = line.split("=", 1)
|
||||
values[key.strip()] = value.strip().strip("\"'")
|
||||
return values
|
||||
|
||||
|
||||
def main():
|
||||
path = ROOT / ".env"
|
||||
existing = read_values(path)
|
||||
defaults = read_values(ROOT / ".env.example")
|
||||
defaults.update(
|
||||
POSTGRES_DB="messenger",
|
||||
POSTGRES_USER="messenger",
|
||||
POSTGRES_PASSWORD=secrets.token_hex(24),
|
||||
)
|
||||
defaults.update(UPLOAD_CLEANUP_GRACE_SECONDS="600", UPLOAD_TMP_DIR="/tmp/uploads",
|
||||
S3_PUBLIC_ENDPOINT="http://127.0.0.1:9000",
|
||||
S3_CORS_ORIGINS="http://127.0.0.1:5173,http://localhost:5173")
|
||||
values = defaults | existing
|
||||
for name, lower, upper in (
|
||||
("APP_PORT", 1, 65535),
|
||||
("API_REPLICAS", 2, 32),
|
||||
("SESSION_TTL_SECONDS", 1, 31536000),
|
||||
("IDEMPOTENCY_TTL_SECONDS", 1, 31536000),
|
||||
):
|
||||
try:
|
||||
valid = lower <= int(values[name]) <= upper
|
||||
except (ValueError, KeyError):
|
||||
valid = False
|
||||
if not valid:
|
||||
raise SystemExit(f"{name} must be between {lower} and {upper}")
|
||||
for name in (
|
||||
"POSTGRES_DB",
|
||||
"POSTGRES_USER",
|
||||
"POSTGRES_PASSWORD",
|
||||
"APP_HOST",
|
||||
"APP_ENV",
|
||||
):
|
||||
if not values[name]:
|
||||
raise SystemExit(f"{name} must not be empty")
|
||||
# Existing values, including credentials, are never replaced.
|
||||
missing = {key: value for key, value in values.items() if key not in existing}
|
||||
if missing:
|
||||
prefix = "\n" if path.exists() and path.stat().st_size else ""
|
||||
with path.open("a", encoding="utf-8") as file:
|
||||
file.write(
|
||||
prefix + "".join(f"{key}={value}\n" for key, value in missing.items())
|
||||
)
|
||||
os.chmod(path, 0o600)
|
||||
secret_dir = ROOT / "secrets"
|
||||
secret_dir.mkdir(mode=0o700, exist_ok=True)
|
||||
database_url = "postgres://{}:{}@db:5432/{}?sslmode=disable".format(
|
||||
quote(values["POSTGRES_USER"], safe=""),
|
||||
quote(values["POSTGRES_PASSWORD"], safe=""),
|
||||
quote(values["POSTGRES_DB"], safe=""),
|
||||
)
|
||||
for name, content in (
|
||||
("DATABASE_URL_FILE", database_url),
|
||||
("CACHE_URL_FILE", "redis://redis:6379/0"),
|
||||
("S3_ACCESS_KEY_ID_FILE", secrets.token_hex(12)),
|
||||
("S3_SECRET_ACCESS_KEY_FILE", secrets.token_hex(32)),
|
||||
):
|
||||
container_path = Path(values[name])
|
||||
if container_path.parent != Path("/run/secrets") or not container_path.name:
|
||||
raise SystemExit(f"{name} must name a file directly inside /run/secrets")
|
||||
target = (
|
||||
secret_dir
|
||||
/ {
|
||||
"DATABASE_URL_FILE": "database_url",
|
||||
"CACHE_URL_FILE": "cache_url",
|
||||
"S3_ACCESS_KEY_ID_FILE": "s3_access_key_id",
|
||||
"S3_SECRET_ACCESS_KEY_FILE": "s3_secret_access_key",
|
||||
}[name]
|
||||
)
|
||||
if not target.exists():
|
||||
# Containers run as an unprivileged user; the private host directory
|
||||
# protects the files, while read permission lets the API consume them.
|
||||
with target.open("x", encoding="utf-8") as file:
|
||||
file.write(content + "\n")
|
||||
os.chmod(target, 0o644)
|
||||
# Existing S3 files remain the root credentials; API gets a new, separate identity.
|
||||
for filename, content in (
|
||||
("s3_api_access_key_id", secrets.token_hex(12)),
|
||||
("s3_api_secret_access_key", secrets.token_hex(32)),
|
||||
):
|
||||
target = secret_dir / filename
|
||||
if not target.exists():
|
||||
with target.open("x", encoding="utf-8") as file:
|
||||
file.write(content + "\n")
|
||||
os.chmod(target, 0o644)
|
||||
print("Local .env and connection files ready; existing values and files preserved.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Executable
+7
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
docker compose --project-name highload-lab5 --env-file .env -f compose.yml restart db redis s3 >/dev/null
|
||||
docker compose --project-name highload-lab5 --env-file .env -f compose.yml up -d --wait db redis s3 >/dev/null
|
||||
docker compose --project-name highload-lab5 --env-file .env -f compose.yml restart api upload-cleanup >/dev/null
|
||||
docker compose --project-name highload-lab5 --env-file .env -f compose.yml up -d --wait >/dev/null
|
||||
Executable
+16
@@ -0,0 +1,16 @@
|
||||
#!/bin/sh
|
||||
set -eu
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
: "${TARGET_INSTANCE_ID:?Set TARGET_INSTANCE_ID to the X-Instance-Id of an API replica}"
|
||||
for container in $(docker compose --project-name highload-lab5 --env-file .env -f compose.yml ps -q api); do
|
||||
hostname=$(docker inspect --format '{{.Config.Hostname}}' "$container")
|
||||
if [ "$hostname" = "$TARGET_INSTANCE_ID" ]; then
|
||||
# Only containers from this Compose project's api service are eligible.
|
||||
docker stop --timeout 8 "$container" >/dev/null
|
||||
printf 'Stopped API instance %s\n' "$hostname"
|
||||
exit 0
|
||||
fi
|
||||
done
|
||||
printf 'No running API replica matches TARGET_INSTANCE_ID\n' >&2
|
||||
exit 1
|
||||
@@ -0,0 +1,26 @@
|
||||
.git
|
||||
.gitignore
|
||||
.github
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
secrets
|
||||
certs
|
||||
volumes
|
||||
data
|
||||
backups
|
||||
*.key
|
||||
*.pem
|
||||
*.p12
|
||||
*.pfx
|
||||
*.jks
|
||||
.venv
|
||||
__pycache__
|
||||
node_modules
|
||||
evidence
|
||||
.DS_Store
|
||||
README.md
|
||||
REPORT.md
|
||||
COURSE.md
|
||||
CONTRIBUTING.md
|
||||
MAINTAINERS.md
|
||||
@@ -0,0 +1,39 @@
|
||||
FROM golang:1.22.2-bookworm AS builder
|
||||
|
||||
WORKDIR /src
|
||||
|
||||
COPY go.mod go.sum ./
|
||||
|
||||
RUN go mod download
|
||||
|
||||
COPY backend ./backend
|
||||
|
||||
RUN CGO_ENABLED=0 \
|
||||
GOOS=linux \
|
||||
go build \
|
||||
-trimpath \
|
||||
-ldflags="-s -w" \
|
||||
-o /out/messenger \
|
||||
./backend
|
||||
|
||||
|
||||
FROM debian:12.15-slim
|
||||
|
||||
RUN groupadd --system app \
|
||||
&& useradd --system --gid app --no-create-home app
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY --from=builder /out/messenger ./messenger
|
||||
|
||||
ENV APP_HOST=0.0.0.0
|
||||
ENV APP_PORT=8080
|
||||
|
||||
EXPOSE 8080
|
||||
|
||||
HEALTHCHECK --interval=10s --timeout=3s --start-period=5s --retries=3 \
|
||||
CMD ["./messenger", "healthcheck"]
|
||||
|
||||
USER app:app
|
||||
|
||||
CMD ["./messenger"]
|
||||
@@ -0,0 +1,59 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"log"
|
||||
"messenger/src/backend/repositories"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func runUploadCleanup(args []string) error {
|
||||
flags := flag.NewFlagSet("attachments-cleanup", flag.ContinueOnError)
|
||||
loop := flags.Bool("loop", false, "run every 60 seconds")
|
||||
dryRun := flags.Bool("dry-run", false, "report without deleting anything")
|
||||
if err := flags.Parse(args); err != nil {
|
||||
return err
|
||||
}
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
init, cancel := context.WithTimeout(ctx, 20*time.Second)
|
||||
db, err := repositories.OpenDatabase(init)
|
||||
cancel()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer db.Close()
|
||||
objects, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store := repositories.NewAttachmentStore(db)
|
||||
for {
|
||||
pass, cancel := context.WithTimeout(ctx, 30*time.Second)
|
||||
result, err := store.CleanupUploads(pass, *dryRun, objects.Delete)
|
||||
direct, directErr := store.CleanupDirectUploads(pass, *dryRun, objects.Delete)
|
||||
cancel()
|
||||
if encodeErr := json.NewEncoder(os.Stdout).Encode(map[string]any{"originals": result, "direct": direct}); encodeErr != nil {
|
||||
return encodeErr
|
||||
}
|
||||
if !*loop {
|
||||
if err == nil {
|
||||
err = directErr
|
||||
}
|
||||
return err
|
||||
}
|
||||
if err != nil || directErr != nil {
|
||||
log.Print("upload cleanup pass failed; journal retained for retry")
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return nil
|
||||
case <-time.After(60 * time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"io"
|
||||
"log"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func uploadAttachmentHandler(chats *services.ChatService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
raw := strings.TrimSuffix(strings.TrimPrefix(r.URL.Path, "/api/v1/chats/"), "/attachments")
|
||||
chatID, err := uuid.Parse(raw)
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err = chats.Get(r.Context(), userID, chatID.String()); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
query, err := urlQuery(r)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
filenames := query["filename"]
|
||||
if len(filenames) != 1 {
|
||||
writeError(w, 400, "invalid_request", "exactly one filename is required", requestID)
|
||||
return
|
||||
}
|
||||
if err = services.ValidateFilename(filenames[0]); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if r.ContentLength > services.MaxUploadBytes {
|
||||
writeServiceError(w, services.ErrPayloadTooLarge, requestID)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, services.MaxUploadBytes)
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
attachment, err := attachments.Upload(ctx, userID, chatID.String(), filenames[0], r.Header.Get("Content-Type"), r.Body)
|
||||
var tooLarge *http.MaxBytesError
|
||||
if errors.As(err, &tooLarge) {
|
||||
err = services.ErrPayloadTooLarge
|
||||
}
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, attachment)
|
||||
}
|
||||
}
|
||||
|
||||
func attachmentHandler(attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
path := strings.TrimPrefix(r.URL.Path, "/api/v1/attachments/")
|
||||
parts := strings.Split(path, "/")
|
||||
if len(parts) > 2 || (len(parts) == 2 && parts[1] != "content") {
|
||||
writeServiceError(w, services.ErrAttachmentNotFound, requestID)
|
||||
return
|
||||
}
|
||||
id, err := uuid.Parse(parts[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid attachment id", requestID)
|
||||
return
|
||||
}
|
||||
a, err := attachments.Get(r.Context(), id.String(), userID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if len(parts) == 1 {
|
||||
writeJSON(w, 200, a)
|
||||
return
|
||||
}
|
||||
query, err := urlQuery(r)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if variants, exists := query["variant"]; exists {
|
||||
if len(variants) != 1 {
|
||||
writeError(w, 400, "invalid_request", "invalid variant", requestID)
|
||||
return
|
||||
}
|
||||
if variants[0] != "original" {
|
||||
writeServiceError(w, services.ErrAttachmentNotFound, requestID)
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
content, err := attachments.Content(ctx, a)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
defer content.Close()
|
||||
w.Header().Set("Content-Type", a.ContentType)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(a.SizeBytes, 10))
|
||||
w.Header().Set("X-Content-Type-Options", "nosniff")
|
||||
w.Header().Set("Cache-Control", "private, no-store")
|
||||
w.WriteHeader(200)
|
||||
if _, err = io.Copy(w, content); err != nil {
|
||||
log.Printf("attachment download interrupted, request %s", requestID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func urlQuery(r *http.Request) (url.Values, error) {
|
||||
q, err := url.ParseQuery(r.URL.RawQuery)
|
||||
if err != nil {
|
||||
return nil, services.InvalidInput{Message: "invalid query"}
|
||||
}
|
||||
return q, nil
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type createChatRequest struct {
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
}
|
||||
|
||||
func createChatHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createChatRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Create(r.Context(), userID, request.Title, request.MemberIDs)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, chat)
|
||||
}
|
||||
}
|
||||
|
||||
func getChatHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID := strings.TrimPrefix(r.URL.Path, "/api/v1/chats/")
|
||||
if chatID == "" || strings.Contains(chatID, "/") {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, chat)
|
||||
}
|
||||
}
|
||||
|
||||
func listChatsHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
limit, cursor, ok := pageParams(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := chats.List(r.Context(), userID, limit, cursor)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, page)
|
||||
}
|
||||
}
|
||||
|
||||
func chatCollectionHandler(chats *services.ChatService) http.HandlerFunc {
|
||||
create := createChatHandler(chats)
|
||||
list := listChatsHandler(chats)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method == http.MethodGet {
|
||||
list(w, r)
|
||||
} else {
|
||||
create(w, r)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func chatSubresourceHandler(chats *services.ChatService, messages *services.MessageService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
get := getChatHandler(chats)
|
||||
createMessage := createMessageHandler(chats, messages)
|
||||
listMessages := listMessagesHandler(chats, messages)
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
if attachments != nil && strings.Contains(r.URL.Path, "/attachment-uploads") {
|
||||
directUploadHandler(chats, attachments)(w, r)
|
||||
return
|
||||
}
|
||||
if attachments != nil && strings.HasSuffix(r.URL.Path, "/attachments") {
|
||||
uploadAttachmentHandler(chats, attachments)(w, r)
|
||||
return
|
||||
}
|
||||
if strings.HasSuffix(r.URL.Path, "/messages") {
|
||||
if r.Method == http.MethodGet {
|
||||
listMessages(w, r)
|
||||
} else {
|
||||
createMessage(w, r)
|
||||
}
|
||||
return
|
||||
}
|
||||
get(w, r)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func directUploadHandler(chats *services.ChatService, attachments *services.AttachmentService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
user, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
parts := strings.Split(strings.TrimPrefix(r.URL.Path, "/api/v1/chats/"), "/")
|
||||
if (len(parts) != 2 && (len(parts) != 4 || parts[3] != "complete")) || parts[1] != "attachment-uploads" {
|
||||
writeError(w, 404, "not_found", "upload endpoint not found", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := uuid.Parse(parts[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err = chats.Get(r.Context(), user, chat.String()); err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 25*time.Second)
|
||||
defer cancel()
|
||||
if len(parts) == 4 {
|
||||
id, err := uuid.Parse(parts[2])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid upload id", requestID)
|
||||
return
|
||||
}
|
||||
a, err := attachments.CompleteDirect(ctx, user, chat.String(), id.String())
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, a)
|
||||
return
|
||||
}
|
||||
r.Body = http.MaxBytesReader(w, r.Body, 4096)
|
||||
var input services.DirectUploadInput
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
decoder.DisallowUnknownFields()
|
||||
err = decoder.Decode(&input)
|
||||
if err == nil {
|
||||
var extra any
|
||||
if next := decoder.Decode(&extra); !errors.Is(next, io.EOF) {
|
||||
err = errors.New("expected one JSON object")
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid upload JSON (maximum 4 KiB)", requestID)
|
||||
return
|
||||
}
|
||||
result, err := attachments.InitDirect(ctx, user, chat.String(), input)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, result)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type healthResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
type apiError struct {
|
||||
Code string `json:"code"`
|
||||
Message string `json:"message"`
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
type errorResponse struct {
|
||||
Error apiError `json:"error"`
|
||||
}
|
||||
|
||||
func newRequestID() string {
|
||||
bytes := make([]byte, 16)
|
||||
if _, err := rand.Read(bytes); err != nil {
|
||||
return "request-id-generation-failed"
|
||||
}
|
||||
return hex.EncodeToString(bytes)
|
||||
}
|
||||
|
||||
func writeError(w http.ResponseWriter, status int, code, message, requestID string) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
response := errorResponse{Error: apiError{Code: code, Message: message, RequestID: requestID}}
|
||||
if err := json.NewEncoder(w).Encode(response); err != nil {
|
||||
log.Printf("failed to encode error response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func writeServiceError(w http.ResponseWriter, err error, requestID string) {
|
||||
var upload repositories.UploadError
|
||||
if errors.As(err, &upload) {
|
||||
writeError(w, upload.Status, upload.Code, upload.Message, requestID)
|
||||
return
|
||||
}
|
||||
var invalid services.InvalidInput
|
||||
switch {
|
||||
case errors.As(err, &invalid):
|
||||
writeError(w, 400, "invalid_request", invalid.Message, requestID)
|
||||
case errors.Is(err, services.ErrPayloadTooLarge):
|
||||
writeError(w, 413, "payload_too_large", "image exceeds upload limits", requestID)
|
||||
case errors.Is(err, services.ErrUnsupportedMedia):
|
||||
writeError(w, 415, "unsupported_media_type", "a valid PNG or JPEG matching Content-Type is required", requestID)
|
||||
case errors.Is(err, services.ErrAttachmentNotFound):
|
||||
writeError(w, 404, "not_found", "attachment not found", requestID)
|
||||
case errors.Is(err, services.ErrConflict):
|
||||
writeError(w, 409, "conflict", "request conflicts with an existing resource", requestID)
|
||||
case errors.Is(err, services.ErrUnknownUser):
|
||||
writeError(w, 401, "unauthorized", "unknown user", requestID)
|
||||
case errors.Is(err, services.ErrChatNotFound):
|
||||
writeError(w, 404, "not_found", "chat not found", requestID)
|
||||
default:
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
}
|
||||
}
|
||||
|
||||
func writeJSON(w http.ResponseWriter, status int, value any) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(status)
|
||||
if err := json.NewEncoder(w).Encode(value); err != nil {
|
||||
log.Printf("failed to encode response: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func requestUserID(w http.ResponseWriter, r *http.Request, requestID string) (string, bool) {
|
||||
id := currentUserID(r)
|
||||
if id == "" {
|
||||
writeError(w, 401, "unauthorized", "valid Bearer session is required", requestID)
|
||||
return "", false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func pageParams(w http.ResponseWriter, r *http.Request, requestID string) (int, string, bool) {
|
||||
limit := 50
|
||||
if values, exists := r.URL.Query()["limit"]; exists {
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
writeError(w, 400, "invalid_request", "invalid limit", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
parsed, err := strconv.Atoi(values[0])
|
||||
if err != nil {
|
||||
writeError(w, 400, "invalid_request", "limit must be between 1 and 100", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
limit = parsed
|
||||
}
|
||||
var cursor string
|
||||
if values, exists := r.URL.Query()["cursor"]; exists {
|
||||
if len(values) != 1 || values[0] == "" {
|
||||
writeError(w, 400, "invalid_request", "invalid cursor", requestID)
|
||||
return 0, "", false
|
||||
}
|
||||
cursor = values[0]
|
||||
}
|
||||
return limit, cursor, true
|
||||
}
|
||||
|
||||
func healthHandler(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, healthResponse{"ok"})
|
||||
}
|
||||
|
||||
func readyHandler(health *services.HealthService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
// Readiness must finish before HAProxy's one-second check read timeout.
|
||||
ctx, cancel := context.WithTimeout(r.Context(), 500*time.Millisecond)
|
||||
defer cancel()
|
||||
if err := health.Ready(ctx); err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, healthResponse{"ok"})
|
||||
}
|
||||
}
|
||||
|
||||
func NewRouter(health *services.HealthService, users *services.UserService, sessions *services.SessionService, chats *services.ChatService, messages *services.MessageService, attachmentServices ...*services.AttachmentService) http.Handler {
|
||||
var attachments *services.AttachmentService
|
||||
if len(attachmentServices) > 0 {
|
||||
attachments = attachmentServices[0]
|
||||
}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("/health/live", healthHandler)
|
||||
mux.HandleFunc("/health/ready", readyHandler(health))
|
||||
mux.Handle("/api/v1/users", createUserHandler(users))
|
||||
mux.Handle("/api/v1/auth/sessions", sessionHandler(sessions))
|
||||
mux.Handle("/api/v1/auth/sessions/current", authRequired(sessions, logoutHandler(sessions)))
|
||||
mux.Handle("/api/v1/users/me", authRequired(sessions, currentUserHandler(users)))
|
||||
mux.Handle("/api/v1/chats", authRequired(sessions, chatCollectionHandler(chats)))
|
||||
mux.Handle("/api/v1/chats/", authRequired(sessions, chatSubresourceHandler(chats, messages, attachments)))
|
||||
if attachments != nil {
|
||||
mux.Handle("/api/v1/attachments/", authRequired(sessions, attachmentHandler(attachments)))
|
||||
}
|
||||
return withInstance(mux)
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Docker's hostname lets the operator map an API response to its container.
|
||||
var instanceID = func() string {
|
||||
id, err := os.Hostname()
|
||||
if err != nil || id == "" {
|
||||
return uuid.NewString()
|
||||
}
|
||||
return id
|
||||
}()
|
||||
|
||||
var draining atomic.Bool
|
||||
|
||||
func BeginShutdown() { draining.Store(true) }
|
||||
|
||||
func withInstance(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("X-Instance-Id", instanceID)
|
||||
w.Header().Set("X-Request-Id", newRequestID())
|
||||
if draining.Load() && r.URL.Path != "/health/live" {
|
||||
writeError(w, 503, "unavailable", "instance is shutting down", w.Header().Get("X-Request-Id"))
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type createMessageRequest struct {
|
||||
Text *string `json:"text"`
|
||||
AttachmentIDs json.RawMessage `json:"attachment_ids"`
|
||||
}
|
||||
|
||||
func messageChatID(path string) (string, bool) {
|
||||
const prefix = "/api/v1/chats/"
|
||||
const suffix = "/messages"
|
||||
if !strings.HasPrefix(path, prefix) || !strings.HasSuffix(path, suffix) {
|
||||
return "", false
|
||||
}
|
||||
chatID := strings.TrimSuffix(strings.TrimPrefix(path, prefix), suffix)
|
||||
if chatID == "" || strings.Contains(chatID, "/") {
|
||||
return "", false
|
||||
}
|
||||
return chatID, true
|
||||
}
|
||||
|
||||
func createMessageHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID, ok := messageChatID(r.URL.Path)
|
||||
if !ok {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createMessageRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
ids := []string{}
|
||||
if request.Text == nil {
|
||||
writeError(w, 400, "invalid_request", "text must be a string", requestID)
|
||||
return
|
||||
}
|
||||
if len(request.AttachmentIDs) > 0 {
|
||||
if err := json.Unmarshal(request.AttachmentIDs, &ids); err != nil || ids == nil {
|
||||
writeError(w, 400, "invalid_request", "attachment_ids must be an array of UUIDs", requestID)
|
||||
return
|
||||
}
|
||||
}
|
||||
keys := r.Header.Values("Idempotency-Key")
|
||||
if len(keys) != 1 {
|
||||
writeError(w, 400, "invalid_request", "exactly one Idempotency-Key is required", requestID)
|
||||
return
|
||||
}
|
||||
message, err := messages.CreateIdempotent(r.Context(), userID, chat, *request.Text, keys[0], ids)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, message)
|
||||
}
|
||||
}
|
||||
|
||||
func listMessagesHandler(chats *services.ChatService, messages *services.MessageService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
chatID, ok := messageChatID(r.URL.Path)
|
||||
if !ok {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
if _, err := uuid.Parse(chatID); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid chat id", requestID)
|
||||
return
|
||||
}
|
||||
chat, err := chats.Get(r.Context(), userID, chatID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
limit, cursor, ok := pageParams(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
page, err := messages.List(r.Context(), userID, chat, limit, cursor)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, page)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type authenticatedUserKey struct{}
|
||||
|
||||
func currentUserID(r *http.Request) string {
|
||||
id, _ := r.Context().Value(authenticatedUserKey{}).(string)
|
||||
return id
|
||||
}
|
||||
|
||||
func authRequired(sessions *services.SessionService, next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
authorization := r.Header.Get("Authorization")
|
||||
if !strings.HasPrefix(authorization, "Bearer ") || strings.TrimSpace(strings.TrimPrefix(authorization, "Bearer ")) == "" {
|
||||
unauthorized(w, "Bearer", newRequestID())
|
||||
return
|
||||
}
|
||||
user, err := sessions.User(r.Context(), strings.TrimPrefix(authorization, "Bearer "))
|
||||
if errors.Is(err, services.ErrInvalidSession) {
|
||||
unauthorized(w, "Bearer", newRequestID())
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r.WithContext(context.WithValue(r.Context(), authenticatedUserKey{}, user.ID)))
|
||||
})
|
||||
}
|
||||
|
||||
func unauthorized(w http.ResponseWriter, scheme, requestID string) {
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if scheme == "Basic" {
|
||||
w.Header().Set("WWW-Authenticate", `Basic realm="messenger", charset="UTF-8"`)
|
||||
} else {
|
||||
w.Header().Set("WWW-Authenticate", `Bearer realm="messenger"`)
|
||||
}
|
||||
writeError(w, 401, "unauthorized", "invalid credentials", requestID)
|
||||
}
|
||||
|
||||
func sessionHandler(sessions *services.SessionService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || r.ContentLength > 0 {
|
||||
unauthorized(w, "Basic", requestID)
|
||||
return
|
||||
}
|
||||
user, token, expires, valid, err := sessions.Login(r.Context(), username, password)
|
||||
if err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
if !valid {
|
||||
unauthorized(w, "Basic", requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, struct {
|
||||
Token string `json:"token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
User any `json:"user"`
|
||||
}{token, "Bearer", expires, user})
|
||||
}
|
||||
}
|
||||
|
||||
func logoutHandler(sessions *services.SessionService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodDelete {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
token := strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
|
||||
if err := sessions.Revoke(r.Context(), token); err != nil {
|
||||
writeError(w, 503, "unavailable", "dependency unavailable", requestID)
|
||||
return
|
||||
}
|
||||
w.WriteHeader(204)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type createUserRequest struct {
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func decodeJSONBody(r *http.Request, destination any) error {
|
||||
decoder := json.NewDecoder(r.Body)
|
||||
var body json.RawMessage
|
||||
if err := decoder.Decode(&body); err != nil {
|
||||
return err
|
||||
}
|
||||
var extra any
|
||||
if err := decoder.Decode(&extra); err != io.EOF {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return fmt.Errorf("more than one JSON value")
|
||||
}
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(body, &fields); err != nil {
|
||||
return err
|
||||
}
|
||||
if fields == nil {
|
||||
return fmt.Errorf("JSON body must be an object")
|
||||
}
|
||||
// encoding/json matches struct fields case-insensitively, even with
|
||||
// DisallowUnknownFields. Check exact contract names before decoding values.
|
||||
typeOfRequest := reflect.TypeOf(destination).Elem()
|
||||
allowed := make(map[string]bool, typeOfRequest.NumField())
|
||||
for i := 0; i < typeOfRequest.NumField(); i++ {
|
||||
name := strings.Split(typeOfRequest.Field(i).Tag.Get("json"), ",")[0]
|
||||
if name != "" && name != "-" {
|
||||
allowed[name] = true
|
||||
}
|
||||
}
|
||||
for name := range fields {
|
||||
if !allowed[name] {
|
||||
return fmt.Errorf("unknown JSON field %q", name)
|
||||
}
|
||||
}
|
||||
return json.Unmarshal(body, destination)
|
||||
}
|
||||
|
||||
func createUserHandler(users *services.UserService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodPost {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
|
||||
writeError(w, 415, "unsupported_media_type", "content type must be application/json", requestID)
|
||||
return
|
||||
}
|
||||
var request createUserRequest
|
||||
if err := decodeJSONBody(r, &request); err != nil {
|
||||
writeError(w, 400, "invalid_request", "invalid JSON body", requestID)
|
||||
return
|
||||
}
|
||||
user, err := users.Create(r.Context(), request.Username, request.DisplayName, request.Password)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 201, user)
|
||||
}
|
||||
}
|
||||
|
||||
func currentUserHandler(users *services.UserService) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
requestID := newRequestID()
|
||||
w.Header().Set("X-Request-Id", requestID)
|
||||
if r.Method != http.MethodGet {
|
||||
writeError(w, 405, "method_not_allowed", "method not allowed", requestID)
|
||||
return
|
||||
}
|
||||
userID, ok := requestUserID(w, r, requestID)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
user, err := users.Get(r.Context(), userID)
|
||||
if err != nil {
|
||||
writeServiceError(w, err, requestID)
|
||||
return
|
||||
}
|
||||
writeJSON(w, 200, user)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,185 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"runtime"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
func serverAddress() string {
|
||||
host := os.Getenv("APP_HOST")
|
||||
if host == "" {
|
||||
host = "0.0.0.0"
|
||||
}
|
||||
|
||||
port := os.Getenv("APP_PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
|
||||
return net.JoinHostPort(host, port)
|
||||
}
|
||||
|
||||
func main() {
|
||||
if environment := os.Getenv("APP_ENV"); environment != "" {
|
||||
log.SetPrefix("[" + environment + "] ")
|
||||
}
|
||||
if len(os.Args) == 2 && os.Args[1] == "s3-bootstrap" {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
if err := repositories.BootstrapS3(ctx); err != nil {
|
||||
log.Fatal("S3 bootstrap failed: ", err)
|
||||
}
|
||||
log.Print("private S3 bucket ready")
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 && os.Args[1] == "attachments-cleanup" {
|
||||
if err := runUploadCleanup(os.Args[2:]); err != nil {
|
||||
log.Fatal("upload cleanup failed: ", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(os.Args) > 1 {
|
||||
if len(os.Args) == 2 && os.Args[1] == "healthcheck" {
|
||||
client := http.Client{Timeout: 2 * time.Second}
|
||||
port := os.Getenv("APP_PORT")
|
||||
if port == "" {
|
||||
port = "8080"
|
||||
}
|
||||
response, err := client.Get("http://" + net.JoinHostPort("127.0.0.1", port) + "/health/ready")
|
||||
if err != nil {
|
||||
os.Exit(1)
|
||||
}
|
||||
response.Body.Close()
|
||||
if response.StatusCode != http.StatusOK {
|
||||
os.Exit(1)
|
||||
}
|
||||
return
|
||||
}
|
||||
if len(os.Args) != 3 || os.Args[1] != "migrate" || (os.Args[2] != "up" && os.Args[2] != "down") {
|
||||
log.Fatal("usage: messenger migrate up|down")
|
||||
}
|
||||
url, err := repositories.DatabaseURL()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
if err = repositories.RunMigrations(url, os.Args[2]); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
log.Printf("migration %s complete", os.Args[2])
|
||||
return
|
||||
}
|
||||
if _, err := repositories.UploadCleanupGrace(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
|
||||
db, err := repositories.OpenDatabase(ctx)
|
||||
cancel()
|
||||
if err != nil {
|
||||
log.Fatalf("database initialization failed: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
// Opt-in experiment telemetry goes to container logs, never a public port.
|
||||
if os.Getenv("BENCH_POOL_STATS") == "1" {
|
||||
go func() {
|
||||
for range time.Tick(time.Second) {
|
||||
s := db.Stat()
|
||||
data, _ := json.Marshal(map[string]any{
|
||||
"time": time.Now().UTC(), "acquired": s.AcquiredConns(),
|
||||
"total": s.TotalConns(), "max": s.MaxConns(),
|
||||
"gomaxprocs": runtime.GOMAXPROCS(0), "goroutines": runtime.NumGoroutine(),
|
||||
"acquire_count": s.AcquireCount(), "acquire_duration_ns": s.AcquireDuration().Nanoseconds(),
|
||||
"empty_acquire_count": s.EmptyAcquireCount(), "canceled_acquire_count": s.CanceledAcquireCount(),
|
||||
})
|
||||
log.Printf("benchmark_pool %s", data)
|
||||
}
|
||||
}()
|
||||
}
|
||||
redisClient, err := repositories.RedisClientFromEnvironment()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
defer redisClient.Close()
|
||||
sessionStore := repositories.NewSessionStore(redisClient, "messenger:session:")
|
||||
userStore := repositories.NewUserStore(db)
|
||||
chatStore := repositories.NewChatStore(db)
|
||||
idempotencyTTL, err := repositories.IdempotencyTTL()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
messageStore := repositories.NewMessageStoreWithTTL(db, idempotencyTTL)
|
||||
ttl, err := services.SessionTTL()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
userService := services.NewUserService(userStore)
|
||||
sessionService := services.NewSessionService(userService, sessionStore, ttl)
|
||||
chatService := services.NewChatService(chatStore)
|
||||
messageService := services.NewMessageService(messageStore)
|
||||
objects, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
attachmentService := services.NewAttachmentService(repositories.NewAttachmentStore(db), objects)
|
||||
mux := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(db), sessionStore, objects), userService, sessionService, chatService, messageService, attachmentService)
|
||||
|
||||
server := &http.Server{
|
||||
Addr: serverAddress(),
|
||||
Handler: mux,
|
||||
ReadHeaderTimeout: 5 * time.Second,
|
||||
ReadTimeout: 30 * time.Second,
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IdleTimeout: 60 * time.Second,
|
||||
}
|
||||
|
||||
shutdownContext, stop := signal.NotifyContext(
|
||||
context.Background(),
|
||||
os.Interrupt,
|
||||
syscall.SIGTERM,
|
||||
)
|
||||
defer stop()
|
||||
|
||||
serverErrors := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
log.Printf("server is listening on %s", server.Addr)
|
||||
|
||||
if err := server.ListenAndServe(); err != nil &&
|
||||
!errors.Is(err, http.ErrServerClosed) {
|
||||
serverErrors <- err
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case err := <-serverErrors:
|
||||
log.Printf("server error: %v", err)
|
||||
|
||||
case <-shutdownContext.Done():
|
||||
log.Printf("shutdown signal received")
|
||||
handlers.BeginShutdown()
|
||||
|
||||
shutdownDeadline, cancel := context.WithTimeout(
|
||||
context.Background(),
|
||||
10*time.Second,
|
||||
)
|
||||
defer cancel()
|
||||
|
||||
if err := server.Shutdown(shutdownDeadline); err != nil {
|
||||
log.Printf("graceful shutdown failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("server stopped gracefully")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"time"
|
||||
)
|
||||
|
||||
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 []any `json:"variants"`
|
||||
ErrorCode *string `json:"error_code"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ObjectKey string `json:"-"`
|
||||
}
|
||||
|
||||
type AttachmentStore struct{ db *pgxpool.Pool }
|
||||
|
||||
func NewAttachmentStore(db *pgxpool.Pool) *AttachmentStore { return &AttachmentStore{db: db} }
|
||||
func (s *AttachmentStore) Create(ctx context.Context, a Attachment) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
_, err := s.db.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
return err
|
||||
}
|
||||
func (s *AttachmentStore) GetForMember(ctx context.Context, id, userID string) (Attachment, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
a := Attachment{Status: "ready", Variants: []any{}}
|
||||
err := s.db.QueryRow(ctx, `SELECT a.id,a.chat_id,a.owner_id,a.filename,a.object_key,a.content_type,a.size_bytes,a.sha256,a.created_at,a.updated_at FROM attachments a JOIN chat_members cm ON cm.chat_id=a.chat_id WHERE a.id=$1 AND cm.user_id=$2`, id, userID).Scan(&a.ID, &a.ChatID, &a.OwnerID, &a.Filename, &a.ObjectKey, &a.ContentType, &a.SizeBytes, &a.SHA256, &a.CreatedAt, &a.UpdatedAt)
|
||||
a.CreatedAt = a.CreatedAt.UTC()
|
||||
a.UpdatedAt = a.UpdatedAt.UTC()
|
||||
return a, err
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type Chat struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
MemberIDs []string `json:"member_ids"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type ChatStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewChatStore(db *pgxpool.Pool) *ChatStore {
|
||||
return &ChatStore{db: db}
|
||||
}
|
||||
|
||||
var ErrUnknownMember = errors.New("unknown member")
|
||||
|
||||
func (s *ChatStore) Create(ctx context.Context, title string, memberIDs []string) (Chat, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
membersCopy := append([]string(nil), memberIDs...)
|
||||
chat := Chat{
|
||||
ID: uuid.New().String(),
|
||||
Title: title,
|
||||
MemberIDs: membersCopy,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
for _, id := range membersCopy {
|
||||
var exists bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM users WHERE id=$1)", id).Scan(&exists); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
if !exists {
|
||||
return Chat{}, ErrUnknownMember
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO chats(id,title,created_at) VALUES($1,$2,$3)", chat.ID, title, chat.CreatedAt); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
for _, id := range membersCopy {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)", chat.ID, id); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func (s *ChatStore) GetByID(ctx context.Context, id string) (Chat, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var chat Chat
|
||||
err := s.db.QueryRow(ctx, "SELECT id,title,created_at FROM chats WHERE id=$1", id).Scan(&chat.ID, &chat.Title, &chat.CreatedAt)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
rows, err := s.db.Query(ctx, "SELECT user_id FROM chat_members WHERE chat_id=$1 ORDER BY user_id", id)
|
||||
if err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
chat.MemberIDs = []string{}
|
||||
for rows.Next() {
|
||||
var member string
|
||||
if err = rows.Scan(&member); err != nil {
|
||||
return Chat{}, err
|
||||
}
|
||||
chat.MemberIDs = append(chat.MemberIDs, member)
|
||||
}
|
||||
return chat, rows.Err()
|
||||
}
|
||||
|
||||
func (s *ChatStore) ListPageForUser(ctx context.Context, userID string, cursor *ChatCursor, limit int) ([]Chat, bool, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
afterTime := time.Time{}
|
||||
afterID := "00000000-0000-0000-0000-000000000000"
|
||||
if cursor != nil {
|
||||
afterTime = cursor.CreatedAt
|
||||
afterID = cursor.ChatID
|
||||
}
|
||||
rows, err := s.db.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::uuid) ORDER BY c.created_at,c.id LIMIT $4`, userID, afterTime, afterID, limit+1)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
chats := []Chat{}
|
||||
for rows.Next() {
|
||||
var c Chat
|
||||
if err = rows.Scan(&c.ID, &c.Title, &c.CreatedAt); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
chats = append(chats, c)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
more := len(chats) > limit
|
||||
if more {
|
||||
chats = chats[:limit]
|
||||
}
|
||||
for i := range chats {
|
||||
chat, err := s.GetByID(ctx, chats[i].ID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
chats[i] = chat
|
||||
}
|
||||
return chats, more, nil
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
func UploadCleanupGrace() (time.Duration, error) {
|
||||
value := os.Getenv("UPLOAD_CLEANUP_GRACE_SECONDS")
|
||||
if value == "" {
|
||||
value = "600"
|
||||
}
|
||||
seconds, err := strconv.Atoi(value)
|
||||
if err != nil || seconds < 120 || seconds > 86400 {
|
||||
return 0, errors.New("UPLOAD_CLEANUP_GRACE_SECONDS must be between 120 and 86400")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
func RedisClientFromEnvironment() (*redis.Client, error) {
|
||||
if path := os.Getenv("CACHE_URL_FILE"); path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
options, err := redis.ParseURL(strings.TrimSpace(string(data)))
|
||||
if err != nil {
|
||||
return nil, errors.New("invalid Redis URL in CACHE_URL_FILE")
|
||||
}
|
||||
options.PoolSize = 10
|
||||
options.MaxRetries = -1
|
||||
options.DialTimeout = time.Second
|
||||
options.ReadTimeout = time.Second
|
||||
options.WriteTimeout = time.Second
|
||||
return redis.NewClient(options), nil
|
||||
}
|
||||
address := os.Getenv("REDIS_ADDR")
|
||||
if address == "" {
|
||||
address = "redis:6379"
|
||||
}
|
||||
return NewRedisClient(address), nil
|
||||
}
|
||||
|
||||
func IdempotencyTTL() (time.Duration, error) {
|
||||
value := os.Getenv("IDEMPOTENCY_TTL_SECONDS")
|
||||
if value == "" {
|
||||
value = "86400"
|
||||
}
|
||||
seconds, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || seconds < 1 || seconds > 31536000 {
|
||||
return 0, errors.New("IDEMPOTENCY_TTL_SECONDS must be between 1 and 31536000")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package repositories
|
||||
|
||||
import "time"
|
||||
|
||||
type ChatCursor struct {
|
||||
UserID string `json:"user_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
ChatID string `json:"chat_id"`
|
||||
}
|
||||
|
||||
type MessageCursor struct {
|
||||
ChatID string `json:"chat_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
MessageID string `json:"message_id"`
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"embed"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/golang-migrate/migrate/v4"
|
||||
pgxmigrate "github.com/golang-migrate/migrate/v4/database/pgx/v5"
|
||||
"github.com/golang-migrate/migrate/v4/source/iofs"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
//go:embed migrations/*.sql
|
||||
var migrationFiles embed.FS
|
||||
|
||||
func DatabaseURL() (string, error) {
|
||||
if path := os.Getenv("DATABASE_URL_FILE"); path != "" {
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return strings.TrimSpace(string(data)), nil
|
||||
}
|
||||
if url := os.Getenv("DATABASE_URL"); url != "" {
|
||||
return url, nil
|
||||
}
|
||||
return "", errors.New("DATABASE_URL_FILE or DATABASE_URL is required")
|
||||
}
|
||||
|
||||
func OpenDatabase(ctx context.Context) (*pgxpool.Pool, error) {
|
||||
url, err := DatabaseURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
cfg.MaxConns = 10
|
||||
if value := os.Getenv("DATABASE_MAX_CONNS"); value != "" {
|
||||
maxConns, parseErr := strconv.Atoi(value)
|
||||
if parseErr != nil || maxConns < 1 || maxConns > 100 {
|
||||
return nil, errors.New("DATABASE_MAX_CONNS must be between 1 and 100")
|
||||
}
|
||||
cfg.MaxConns = int32(maxConns)
|
||||
}
|
||||
cfg.ConnConfig.ConnectTimeout = 5 * time.Second
|
||||
db, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err = db.Ping(ctx); err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// RunMigrations uses versioned up/down SQL embedded in the API binary.
|
||||
// "down" rolls back exactly one version and is an explicit operator command.
|
||||
func RunMigrations(url, direction string) error {
|
||||
source, err := iofs.New(migrationFiles, "migrations")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
sqlDB, err := sql.Open("pgx", url)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
driver, err := pgxmigrate.WithInstance(sqlDB, &pgxmigrate.Config{})
|
||||
if err != nil {
|
||||
sqlDB.Close()
|
||||
return err
|
||||
}
|
||||
m, err := migrate.NewWithInstance("iofs", source, "pgx5", driver)
|
||||
if err != nil {
|
||||
sqlDB.Close()
|
||||
return err
|
||||
}
|
||||
defer m.Close()
|
||||
if direction == "down" {
|
||||
err = m.Steps(-1)
|
||||
} else {
|
||||
err = m.Up()
|
||||
}
|
||||
if errors.Is(err, migrate.ErrNoChange) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
func (s *AttachmentStore) CleanupDirectUploads(ctx context.Context, dryRun bool, remove func(context.Context, string) error) (CleanupResult, error) {
|
||||
result := CleanupResult{DryRun: dryRun}
|
||||
rows, err := s.db.Query(ctx, `SELECT id::text FROM direct_uploads WHERE cleanup_after<=clock_timestamp() ORDER BY cleanup_after,id LIMIT 100`)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return result, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Candidates = len(ids)
|
||||
for _, id := range ids {
|
||||
outcome, err := s.cleanupDirectUpload(ctx, id, dryRun, remove)
|
||||
if err != nil {
|
||||
result.Failed++
|
||||
continue
|
||||
}
|
||||
switch outcome {
|
||||
case "deleted":
|
||||
result.Deleted++
|
||||
case "eligible":
|
||||
result.Eligible++
|
||||
default:
|
||||
result.Skipped++
|
||||
}
|
||||
}
|
||||
if result.Failed > 0 {
|
||||
return result, fmt.Errorf("%d direct upload cleanup operations failed", result.Failed)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) cleanupDirectUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key, state string
|
||||
var barrier, complete, purge, now time.Time
|
||||
err = tx.QueryRow(ctx, `SELECT staging_key,state,complete_before+interval '600 seconds',complete_before,purge_after,clock_timestamp() FROM direct_uploads WHERE id=$1 AND cleanup_after<=clock_timestamp() FOR UPDATE SKIP LOCKED`, id).Scan(&key, &state, &barrier, &complete, &purge, &now)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil || key != "staging/direct/"+parsed.String() {
|
||||
return "", errors.New("invalid direct staging key")
|
||||
}
|
||||
if state == "pending" && now.Before(barrier) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if dryRun {
|
||||
return "eligible", nil
|
||||
}
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if now.After(purge) && !now.Before(barrier) {
|
||||
_, err = tx.Exec(ctx, `DELETE FROM direct_uploads WHERE id=$1`, id)
|
||||
} else {
|
||||
if state == "pending" && !now.Before(complete) {
|
||||
state = "expired"
|
||||
}
|
||||
// An early cleanup is not final: a still valid POST can recreate staging.
|
||||
// Keep a second cleanup after the safety barrier, then retain the receipt 24h.
|
||||
next := barrier
|
||||
if !now.Before(barrier) {
|
||||
next = purge
|
||||
}
|
||||
_, err = tx.Exec(ctx, `UPDATE direct_uploads SET state=$2,cleanup_after=$3 WHERE id=$1`, id, state, next)
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return "deleted", nil
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// UploadError is also persisted for terminal validation failures, so retries agree.
|
||||
type UploadError struct {
|
||||
Status int
|
||||
Code, Message string
|
||||
}
|
||||
|
||||
func (e UploadError) Error() string { return e.Message }
|
||||
|
||||
func DirectError(code string) UploadError {
|
||||
switch code {
|
||||
case "upload_not_uploaded":
|
||||
return UploadError{409, code, "file has not been uploaded"}
|
||||
case "upload_expired":
|
||||
return UploadError{409, code, "upload confirmation period has expired"}
|
||||
case "upload_size_mismatch":
|
||||
return UploadError{400, code, "uploaded size differs from declared size"}
|
||||
case "upload_checksum_mismatch":
|
||||
return UploadError{400, code, "uploaded SHA-256 differs from declared checksum"}
|
||||
case "payload_too_large":
|
||||
return UploadError{413, code, "image exceeds upload limits"}
|
||||
default:
|
||||
return UploadError{415, "unsupported_media_type", "a valid PNG or JPEG matching Content-Type is required"}
|
||||
}
|
||||
}
|
||||
|
||||
type DirectUpload struct {
|
||||
ID string `json:"upload_id"`
|
||||
AttachmentID string `json:"-"`
|
||||
OwnerID string `json:"-"`
|
||||
ChatID string `json:"-"`
|
||||
Filename string `json:"-"`
|
||||
ContentType string `json:"-"`
|
||||
SizeBytes int64 `json:"-"`
|
||||
SHA256 string `json:"-"`
|
||||
StagingKey string `json:"-"`
|
||||
FinalKey string `json:"-"`
|
||||
State string `json:"-"`
|
||||
ErrorCode *string `json:"-"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
CompleteBefore time.Time `json:"complete_before"`
|
||||
}
|
||||
|
||||
const directColumns = `d.id,d.attachment_id,d.owner_id,d.chat_id,d.filename,d.content_type,d.size_bytes,d.sha256,d.staging_key,d.final_key,d.state,d.error_code,d.expires_at,d.complete_before`
|
||||
|
||||
func scanDirect(row pgx.Row) (DirectUpload, error) {
|
||||
var u DirectUpload
|
||||
err := row.Scan(&u.ID, &u.AttachmentID, &u.OwnerID, &u.ChatID, &u.Filename, &u.ContentType, &u.SizeBytes, &u.SHA256, &u.StagingKey, &u.FinalKey, &u.State, &u.ErrorCode, &u.ExpiresAt, &u.CompleteBefore)
|
||||
return u, err
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) CreateDirect(ctx context.Context, u DirectUpload) (DirectUpload, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return scanDirect(s.db.QueryRow(ctx, `INSERT INTO direct_uploads AS d(id,attachment_id,owner_id,chat_id,filename,content_type,size_bytes,sha256,staging_key,final_key) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10) RETURNING `+directColumns, u.ID, u.AttachmentID, u.OwnerID, u.ChatID, u.Filename, u.ContentType, u.SizeBytes, u.SHA256, u.StagingKey, u.FinalKey))
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) GetDirect(ctx context.Context, id, chat, user string) (DirectUpload, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return scanDirect(s.db.QueryRow(ctx, `SELECT `+directColumns+` FROM direct_uploads d JOIN chat_members cm ON cm.chat_id=d.chat_id AND cm.user_id=$3 WHERE d.id=$1 AND d.chat_id=$2 AND d.owner_id=$3`, id, chat, user))
|
||||
}
|
||||
|
||||
// The session lock serializes complete across replicas. A separately committed
|
||||
// reservation survives rollback/crash, while its row lock protects the final key.
|
||||
func (s *AttachmentStore) CompleteDirect(ctx context.Context, id, chat, user string, validate func(context.Context, DirectUpload) error) (Attachment, error) {
|
||||
var a Attachment
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
u, err := scanDirect(tx.QueryRow(ctx, `SELECT `+directColumns+` FROM direct_uploads d JOIN chat_members cm ON cm.chat_id=d.chat_id AND cm.user_id=$3 WHERE d.id=$1 AND d.chat_id=$2 AND d.owner_id=$3 FOR UPDATE OF d`, id, chat, user))
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
if u.StagingKey != "staging/direct/"+u.ID || u.FinalKey != "originals/"+u.AttachmentID {
|
||||
return a, errors.New("invalid direct upload object keys")
|
||||
}
|
||||
if u.State == "ready" {
|
||||
return s.GetForMember(ctx, u.AttachmentID, user)
|
||||
}
|
||||
if u.State == "rejected" && u.ErrorCode != nil {
|
||||
return a, DirectError(*u.ErrorCode)
|
||||
}
|
||||
var expired bool
|
||||
if err = tx.QueryRow(ctx, `SELECT clock_timestamp()>=$1`, u.CompleteBefore).Scan(&expired); err != nil {
|
||||
return a, err
|
||||
}
|
||||
if expired || u.State == "expired" {
|
||||
return a, DirectError("upload_expired")
|
||||
}
|
||||
grace, err := UploadCleanupGrace()
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
// ON CONFLICT handles retry after an interrupted COPY without losing tracking.
|
||||
_, err = s.db.Exec(ctx, `INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()+$3*interval '1 second') ON CONFLICT(id) DO UPDATE SET cleanup_after=GREATEST(upload_attempts.cleanup_after,EXCLUDED.cleanup_after) WHERE upload_attempts.object_key=EXCLUDED.object_key`, u.AttachmentID, u.FinalKey, grace.Seconds())
|
||||
if err != nil {
|
||||
return a, err
|
||||
}
|
||||
var key string
|
||||
if err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after>clock_timestamp() FOR UPDATE`, u.AttachmentID).Scan(&key); err != nil {
|
||||
return a, err
|
||||
}
|
||||
if key != u.FinalKey {
|
||||
return a, errors.New("direct upload reservation mismatch")
|
||||
}
|
||||
if err = validate(ctx, u); err != nil {
|
||||
var invalid UploadError
|
||||
if errors.As(err, &invalid) && (invalid.Status == 400 || invalid.Status == 413 || invalid.Status == 415) {
|
||||
_, saveErr := tx.Exec(ctx, `UPDATE direct_uploads SET state='rejected',error_code=$2,cleanup_after=clock_timestamp(),purge_after=clock_timestamp()+interval '24 hours' WHERE id=$1`, id, invalid.Code)
|
||||
if saveErr != nil {
|
||||
return a, saveErr
|
||||
}
|
||||
if saveErr = tx.Commit(ctx); saveErr != nil {
|
||||
return a, saveErr
|
||||
}
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
a = Attachment{ID: u.AttachmentID, ChatID: u.ChatID, OwnerID: u.OwnerID, Filename: u.Filename, ContentType: u.ContentType, SizeBytes: u.SizeBytes, SHA256: u.SHA256, ObjectKey: u.FinalKey, Status: "ready", Variants: []any{}, CreatedAt: now, UpdatedAt: now}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
if err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `UPDATE direct_uploads SET state='ready',cleanup_after=clock_timestamp(),purge_after=clock_timestamp()+interval '24 hours' WHERE id=$1`, id); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `DELETE FROM upload_attempts WHERE id=$1`, a.ID); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
)
|
||||
|
||||
func NotFound(err error) bool {
|
||||
return errors.Is(err, pgx.ErrNoRows)
|
||||
}
|
||||
|
||||
func CheckViolation(err error) bool {
|
||||
var pgerr *pgconn.PgError
|
||||
return errors.As(err, &pgerr) && pgerr.Code == "23514"
|
||||
}
|
||||
|
||||
func UniqueViolation(err error) bool {
|
||||
var pgerr *pgconn.PgError
|
||||
return errors.As(err, &pgerr) && pgerr.Code == "23505"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type HealthStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewHealthStore(db *pgxpool.Pool) *HealthStore {
|
||||
return &HealthStore{db: db}
|
||||
}
|
||||
|
||||
func (s *HealthStore) Ping(ctx context.Context) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
return s.db.Ping(ctx)
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
var ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
|
||||
var ErrIdempotencyConflict = errors.New("idempotency key belongs to a different message")
|
||||
|
||||
// CreateIdempotent commits the message and its replay record together.
|
||||
func (s *MessageStore) CreateIdempotent(ctx context.Context, chatID, senderID, text, key string, attachments ...[]string) (Message, error) {
|
||||
ids := []string{}
|
||||
if len(attachments) > 0 && attachments[0] != nil {
|
||||
ids = attachments[0]
|
||||
}
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
|
||||
// The transaction-scoped lock is shared by all API replicas. Hash collisions
|
||||
// only serialize unrelated requests; the full scope is checked by the query.
|
||||
scope := senderID + ":POST:/api/v1/chats/" + chatID + "/messages:" + key
|
||||
if _, err = tx.Exec(ctx, "SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", scope); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
// Validate attachments in this same transaction, including replay.
|
||||
for _, id := range ids {
|
||||
var exists bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM attachments WHERE id=$1 AND chat_id=$2)", id, chatID).Scan(&exists); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
if !exists {
|
||||
return Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
}
|
||||
var message Message
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT m.id, m.chat_id, m.sender_id, m.text, m.created_at, ARRAY(SELECT attachment_id::text FROM message_attachments ma WHERE ma.message_id=m.id ORDER BY position)
|
||||
FROM message_idempotency i JOIN messages m ON m.id = i.message_id
|
||||
WHERE i.sender_id=$1 AND i.chat_id=$2 AND i.key=$3
|
||||
AND i.expires_at > clock_timestamp()`, senderID, chatID, key).
|
||||
Scan(&message.ID, &message.ChatID, &message.SenderID, &message.Text, &message.CreatedAt, &message.AttachmentIDs)
|
||||
if err == nil {
|
||||
message.CreatedAt = message.CreatedAt.UTC()
|
||||
if message.Text != text || !reflect.DeepEqual(message.AttachmentIDs, ids) {
|
||||
return Message{}, ErrIdempotencyConflict
|
||||
}
|
||||
return message, tx.Commit(ctx)
|
||||
}
|
||||
if !errors.Is(err, pgx.ErrNoRows) {
|
||||
return Message{}, err
|
||||
}
|
||||
|
||||
// Expiry permits key reuse; it never deletes the original message.
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM message_idempotency WHERE sender_id=$1 AND chat_id=$2 AND key=$3", senderID, chatID, key); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
message = Message{
|
||||
AttachmentIDs: ids, ID: uuid.NewString(), ChatID: chatID, SenderID: senderID, Text: text,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)", message.ID, chatID, senderID, text, message.CreatedAt); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
for position, id := range ids {
|
||||
if _, err = tx.Exec(ctx, "INSERT INTO message_attachments(message_id,attachment_id,position) VALUES($1,$2,$3)", message.ID, id, position); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, `INSERT INTO message_idempotency(sender_id,chat_id,key,message_id,expires_at)
|
||||
VALUES($1,$2,$3,$4,clock_timestamp() + $5 * interval '1 second')`, senderID, chatID, key, message.ID, s.idempotencyTTL.Seconds()); err != nil {
|
||||
return Message{}, err
|
||||
}
|
||||
return message, tx.Commit(ctx)
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
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 MessageStore struct {
|
||||
db *pgxpool.Pool
|
||||
idempotencyTTL time.Duration
|
||||
}
|
||||
|
||||
func NewMessageStore(db *pgxpool.Pool) *MessageStore {
|
||||
return NewMessageStoreWithTTL(db, 24*time.Hour)
|
||||
}
|
||||
|
||||
func NewMessageStoreWithTTL(db *pgxpool.Pool, ttl time.Duration) *MessageStore {
|
||||
return &MessageStore{db: db, idempotencyTTL: ttl}
|
||||
}
|
||||
|
||||
func (s *MessageStore) Create(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
senderID string,
|
||||
text string,
|
||||
) (Message, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
message := Message{
|
||||
AttachmentIDs: []string{},
|
||||
ID: uuid.New().String(),
|
||||
ChatID: chatID,
|
||||
SenderID: senderID,
|
||||
Text: text,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
_, err := s.db.Exec(ctx, "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)", message.ID, message.ChatID, message.SenderID, message.Text, message.CreatedAt)
|
||||
return message, err
|
||||
}
|
||||
|
||||
func (s *MessageStore) ListPageByChat(
|
||||
ctx context.Context,
|
||||
chatID string,
|
||||
cursor *MessageCursor,
|
||||
limit int,
|
||||
) ([]Message, bool, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
afterTime := time.Time{}
|
||||
afterID := "00000000-0000-0000-0000-000000000000"
|
||||
if cursor != nil {
|
||||
afterTime = cursor.CreatedAt
|
||||
afterID = cursor.MessageID
|
||||
}
|
||||
rows, err := s.db.Query(ctx, `SELECT id,chat_id,sender_id,text,created_at,ARRAY(SELECT attachment_id::text FROM message_attachments ma WHERE ma.message_id=messages.id ORDER BY position) FROM messages WHERE chat_id=$1 AND (created_at,id)>($2,$3::uuid) ORDER BY created_at,id LIMIT $4`, chatID, afterTime, afterID, limit+1)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
defer rows.Close()
|
||||
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, false, err
|
||||
}
|
||||
items = append(items, m)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
more := len(items) > limit
|
||||
if more {
|
||||
items = items[:limit]
|
||||
}
|
||||
return items, more, nil
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
DROP TABLE IF EXISTS sessions;
|
||||
DROP TABLE IF EXISTS messages;
|
||||
DROP TABLE IF EXISTS chat_members;
|
||||
DROP TABLE IF EXISTS chats;
|
||||
DROP TABLE IF EXISTS users;
|
||||
@@ -0,0 +1,32 @@
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id uuid PRIMARY KEY,
|
||||
username varchar(32) NOT NULL UNIQUE CHECK (username ~ '^[a-z0-9_]{3,32}$'),
|
||||
display_name text NOT NULL CHECK (char_length(display_name) BETWEEN 1 AND 100),
|
||||
password_hash bytea NOT NULL,
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS chats (
|
||||
id uuid PRIMARY KEY,
|
||||
title text NOT NULL CHECK (char_length(title) BETWEEN 1 AND 100),
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
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),
|
||||
PRIMARY KEY (chat_id,user_id)
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS chat_members_user_idx ON chat_members(user_id,chat_id);
|
||||
CREATE TABLE IF NOT EXISTS 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 CHECK (char_length(text) BETWEEN 1 AND 4000),
|
||||
created_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS messages_history_idx ON messages(chat_id,created_at,id);
|
||||
CREATE TABLE IF NOT EXISTS sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS sessions_expiry_idx ON sessions(expires_at);
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE TABLE sessions (
|
||||
token_hash bytea PRIMARY KEY,
|
||||
user_id uuid NOT NULL REFERENCES users(id),
|
||||
expires_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX sessions_expiry_idx ON sessions(expires_at);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE sessions;
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE message_idempotency;
|
||||
@@ -0,0 +1,8 @@
|
||||
CREATE TABLE message_idempotency (
|
||||
sender_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
key varchar(128) NOT NULL CHECK (key ~ '^[A-Za-z0-9._:-]{1,128}$'),
|
||||
message_id uuid NOT NULL REFERENCES messages(id),
|
||||
expires_at timestamptz NOT NULL,
|
||||
PRIMARY KEY (sender_id, chat_id, key)
|
||||
);
|
||||
@@ -0,0 +1,5 @@
|
||||
-- An operator must first remove attachment-only messages before this rollback.
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (char_length(text) BETWEEN 1 AND 4000);
|
||||
DROP TABLE message_attachments;
|
||||
DROP TABLE attachments;
|
||||
@@ -0,0 +1,22 @@
|
||||
CREATE TABLE attachments (
|
||||
id uuid PRIMARY KEY,
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
filename text NOT NULL,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
content_type text NOT NULL CHECK (content_type IN ('image/png','image/jpeg')),
|
||||
size_bytes bigint NOT NULL CHECK (size_bytes BETWEEN 1 AND 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
created_at timestamptz NOT NULL,
|
||||
updated_at timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX attachments_chat_idx ON attachments(chat_id);
|
||||
CREATE TABLE message_attachments (
|
||||
message_id uuid NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||||
attachment_id uuid NOT NULL REFERENCES attachments(id),
|
||||
position smallint NOT NULL CHECK (position BETWEEN 0 AND 9),
|
||||
PRIMARY KEY (message_id, attachment_id),
|
||||
UNIQUE(message_id, position)
|
||||
);
|
||||
ALTER TABLE messages DROP CONSTRAINT messages_text_check;
|
||||
ALTER TABLE messages ADD CONSTRAINT messages_text_check CHECK (char_length(text) <= 4000);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE upload_attempts;
|
||||
@@ -0,0 +1,7 @@
|
||||
CREATE TABLE upload_attempts (
|
||||
id uuid PRIMARY KEY,
|
||||
object_key text NOT NULL UNIQUE,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
cleanup_after timestamptz NOT NULL
|
||||
);
|
||||
CREATE INDEX upload_attempts_cleanup_idx ON upload_attempts(cleanup_after,id);
|
||||
@@ -0,0 +1 @@
|
||||
DROP TABLE direct_uploads;
|
||||
@@ -0,0 +1,20 @@
|
||||
CREATE TABLE direct_uploads (
|
||||
id uuid PRIMARY KEY,
|
||||
attachment_id uuid NOT NULL UNIQUE,
|
||||
owner_id uuid NOT NULL REFERENCES users(id),
|
||||
chat_id uuid NOT NULL REFERENCES chats(id),
|
||||
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 BETWEEN 1 AND 10485760),
|
||||
sha256 text NOT NULL CHECK (sha256 ~ '^[a-f0-9]{64}$'),
|
||||
staging_key text NOT NULL UNIQUE,
|
||||
final_key text NOT NULL UNIQUE,
|
||||
state text NOT NULL DEFAULT 'pending' CHECK (state IN ('pending','ready','rejected','expired')),
|
||||
error_code text,
|
||||
created_at timestamptz NOT NULL DEFAULT clock_timestamp(),
|
||||
expires_at timestamptz NOT NULL DEFAULT clock_timestamp()+interval '120 seconds',
|
||||
complete_before timestamptz NOT NULL DEFAULT clock_timestamp()+interval '720 seconds',
|
||||
cleanup_after timestamptz NOT NULL DEFAULT clock_timestamp()+interval '1320 seconds',
|
||||
purge_after timestamptz NOT NULL DEFAULT clock_timestamp()+interval '24 hours'
|
||||
);
|
||||
CREATE INDEX direct_uploads_cleanup_idx ON direct_uploads(cleanup_after,id);
|
||||
@@ -0,0 +1,56 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
var ErrSessionNotFound = errors.New("session not found")
|
||||
|
||||
type SessionStore struct {
|
||||
client *redis.Client
|
||||
prefix string
|
||||
}
|
||||
|
||||
func NewRedisClient(address string) *redis.Client {
|
||||
return redis.NewClient(&redis.Options{
|
||||
Addr: address, PoolSize: 10, MaxRetries: -1,
|
||||
DialTimeout: time.Second, ReadTimeout: time.Second, WriteTimeout: time.Second,
|
||||
})
|
||||
}
|
||||
|
||||
func NewSessionStore(client *redis.Client, prefix string) *SessionStore {
|
||||
return &SessionStore{client: client, prefix: prefix}
|
||||
}
|
||||
|
||||
func (s *SessionStore) key(tokenHash []byte) string {
|
||||
return s.prefix + hex.EncodeToString(tokenHash)
|
||||
}
|
||||
|
||||
func (s *SessionStore) Create(ctx context.Context, tokenHash []byte, userID string, expires time.Time) error {
|
||||
ttl := time.Until(expires)
|
||||
if ttl <= 0 {
|
||||
return errors.New("session expiry must be in the future")
|
||||
}
|
||||
return s.client.Set(ctx, s.key(tokenHash), userID, ttl).Err()
|
||||
}
|
||||
|
||||
func (s *SessionStore) UserID(ctx context.Context, tokenHash []byte) (string, error) {
|
||||
id, err := s.client.Get(ctx, s.key(tokenHash)).Result()
|
||||
if errors.Is(err, redis.Nil) {
|
||||
return "", ErrSessionNotFound
|
||||
}
|
||||
return id, err
|
||||
}
|
||||
|
||||
func (s *SessionStore) Revoke(ctx context.Context, tokenHash []byte) error {
|
||||
return s.client.Del(ctx, s.key(tokenHash)).Err()
|
||||
}
|
||||
|
||||
func (s *SessionStore) Ping(ctx context.Context) error {
|
||||
return s.client.Ping(ctx).Err()
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type ObjectStore struct {
|
||||
client *minio.Client
|
||||
bucket string
|
||||
}
|
||||
|
||||
func NewObjectStoreFromEnvironment() (*ObjectStore, error) {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" || (endpoint.Scheme != "http" && endpoint.Scheme != "https") || endpoint.Path != "" {
|
||||
return nil, errors.New("invalid S3_ENDPOINT")
|
||||
}
|
||||
read := func(name string) (string, error) {
|
||||
b, e := os.ReadFile(os.Getenv(name))
|
||||
if e != nil {
|
||||
return "", errors.New("cannot read " + name)
|
||||
}
|
||||
v := strings.TrimSpace(string(b))
|
||||
if v == "" {
|
||||
return "", errors.New("empty " + name)
|
||||
}
|
||||
return v, nil
|
||||
}
|
||||
access, err := read("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
secret, err := read("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
transport := http.DefaultTransport.(*http.Transport).Clone()
|
||||
transport.ResponseHeaderTimeout = 5 * time.Second
|
||||
transport.MaxIdleConnsPerHost = 8
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION"), Transport: transport})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
if bucket == "" {
|
||||
return nil, errors.New("S3_BUCKET is required")
|
||||
}
|
||||
return &ObjectStore{client: client, bucket: bucket}, nil
|
||||
}
|
||||
func (s *ObjectStore) Ping(ctx context.Context) error {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
return errors.New("bucket missing")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (s *ObjectStore) Bootstrap(ctx context.Context) error {
|
||||
exists, err := s.client.BucketExists(ctx, s.bucket)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !exists {
|
||||
if err = s.client.MakeBucket(ctx, s.bucket, minio.MakeBucketOptions{Region: os.Getenv("S3_REGION")}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// No anonymous policy: every object operation requires signed credentials.
|
||||
return s.client.SetBucketPolicy(ctx, s.bucket, "")
|
||||
}
|
||||
func (s *ObjectStore) Put(ctx context.Context, key string, body io.Reader, size int64, contentType string) error {
|
||||
_, err := s.client.PutObject(ctx, s.bucket, key, body, size, minio.PutObjectOptions{ContentType: contentType, DisableMultipart: true})
|
||||
return err
|
||||
}
|
||||
func (s *ObjectStore) Delete(ctx context.Context, key string) error {
|
||||
return s.client.RemoveObject(ctx, s.bucket, key, minio.RemoveObjectOptions{})
|
||||
}
|
||||
func (s *ObjectStore) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
object, err := s.client.GetObject(ctx, s.bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// GetObject is lazy: force the request before writing HTTP 200 headers.
|
||||
if _, err = object.Stat(); err != nil {
|
||||
object.Close()
|
||||
return nil, err
|
||||
}
|
||||
return object, nil
|
||||
}
|
||||
@@ -0,0 +1,107 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
madmin "github.com/minio/madmin-go/v3"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
||||
)
|
||||
|
||||
func readS3Secret(name string) (string, error) {
|
||||
data, err := os.ReadFile(os.Getenv(name))
|
||||
if err != nil {
|
||||
return "", errors.New("cannot read " + name)
|
||||
}
|
||||
value := strings.TrimSpace(string(data))
|
||||
if value == "" {
|
||||
return "", errors.New("empty " + name)
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// BootstrapS3 is the only application command that needs administrative credentials.
|
||||
func BootstrapS3(ctx context.Context) error {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" {
|
||||
return errors.New("invalid S3_ENDPOINT")
|
||||
}
|
||||
rootAccess, err := readS3Secret("S3_ROOT_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rootSecret, err := readS3Secret("S3_ROOT_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
access, err := readS3Secret("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
secret, err := readS3Secret("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if access == rootAccess {
|
||||
return errors.New("API and root credentials must differ")
|
||||
}
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(rootAccess, rootSecret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION")})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
store := &ObjectStore{client: client, bucket: os.Getenv("S3_BUCKET")}
|
||||
if err = store.Bootstrap(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = bootstrapDirectLifecycle(ctx, client, store.bucket); err != nil {
|
||||
return err
|
||||
}
|
||||
admin, err := madmin.New(endpoint.Host, rootAccess, rootSecret, endpoint.Scheme == "https")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
policy := map[string]any{"Version": "2012-10-17", "Statement": []any{
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetBucketLocation", "s3:ListBucket"}, "Resource": []string{"arn:aws:s3:::" + store.bucket}},
|
||||
map[string]any{"Effect": "Allow", "Action": []string{"s3:GetObject", "s3:PutObject", "s3:DeleteObject"}, "Resource": []string{"arn:aws:s3:::" + store.bucket + "/originals/*", "arn:aws:s3:::" + store.bucket + "/staging/direct/*"}},
|
||||
}}
|
||||
data, err := json.Marshal(policy)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
const name = "messenger-attachments-api"
|
||||
if err = admin.AddCannedPolicy(ctx, name, data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = admin.SetUserReq(ctx, access, madmin.AddOrUpdateUserReq{SecretKey: secret, Status: madmin.AccountEnabled}); err != nil {
|
||||
return err
|
||||
}
|
||||
// Explicitly assign the managed policy; creating a user alone grants no S3 rights.
|
||||
// This idempotent API is supported by our pinned MinIO server release.
|
||||
return admin.SetPolicy(ctx, name, access, false)
|
||||
}
|
||||
|
||||
func bootstrapDirectLifecycle(ctx context.Context, client *minio.Client, bucket string) error {
|
||||
config, err := client.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil && minio.ToErrorResponse(err).Code != "NoSuchLifecycleConfiguration" {
|
||||
return err
|
||||
}
|
||||
if config == nil {
|
||||
config = lifecycle.NewConfiguration()
|
||||
}
|
||||
const id = "messenger-direct-staging-expiration"
|
||||
rules := make([]lifecycle.Rule, 0, len(config.Rules)+1)
|
||||
for _, rule := range config.Rules {
|
||||
if rule.ID != id {
|
||||
rules = append(rules, rule)
|
||||
}
|
||||
}
|
||||
rules = append(rules, lifecycle.Rule{ID: id, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "staging/direct/"}, Expiration: lifecycle.Expiration{Days: 1}})
|
||||
config.Rules = rules
|
||||
return client.SetBucketLifecycle(ctx, bucket, config)
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/url"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
type PostUpload struct {
|
||||
Method string `json:"method"`
|
||||
URL string `json:"url"`
|
||||
Fields map[string]string `json:"fields"`
|
||||
}
|
||||
|
||||
type StoredObject struct {
|
||||
Size int64
|
||||
ETag string
|
||||
}
|
||||
|
||||
func IsObjectMissing(err error) bool {
|
||||
code := minio.ToErrorResponse(err).Code
|
||||
return code == "NoSuchKey" || code == "NoSuchObject" || code == "NotFound"
|
||||
}
|
||||
|
||||
func (s *ObjectStore) PresignDirect(ctx context.Context, key, mime string, size int64, expires time.Time) (PostUpload, error) {
|
||||
endpoint, err := url.Parse(os.Getenv("S3_PUBLIC_ENDPOINT"))
|
||||
if err != nil || endpoint.Host == "" || (endpoint.Scheme != "http" && endpoint.Scheme != "https") || endpoint.Path != "" || endpoint.RawQuery != "" || endpoint.User != nil {
|
||||
return PostUpload{}, errors.New("invalid S3_PUBLIC_ENDPOINT")
|
||||
}
|
||||
access, err := readS3Secret("S3_ACCESS_KEY_ID_FILE")
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
secret, err := readS3Secret("S3_SECRET_ACCESS_KEY_FILE")
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
signer, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION"), BucketLookup: minio.BucketLookupPath})
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
policy := minio.NewPostPolicy()
|
||||
for _, err = range []error{policy.SetBucket(s.bucket), policy.SetKey(key), policy.SetExpires(expires), policy.SetContentType(mime), policy.SetContentLengthRange(size, size), policy.SetSuccessStatusAction("204")} {
|
||||
if err != nil {
|
||||
return PostUpload{}, err
|
||||
}
|
||||
}
|
||||
target, fields, err := signer.PresignedPostPolicy(ctx, policy)
|
||||
if err != nil {
|
||||
return PostUpload{}, errors.New("cannot sign upload policy")
|
||||
}
|
||||
return PostUpload{Method: "POST", URL: target.String(), Fields: fields}, nil
|
||||
}
|
||||
|
||||
func (s *ObjectStore) Stat(ctx context.Context, key string) (StoredObject, error) {
|
||||
object, err := s.client.StatObject(ctx, s.bucket, key, minio.StatObjectOptions{})
|
||||
return StoredObject{Size: object.Size, ETag: object.ETag}, err
|
||||
}
|
||||
|
||||
func (s *ObjectStore) Copy(ctx context.Context, source, destination, etag string) error {
|
||||
_, err := s.client.CopyObject(ctx, minio.CopyDestOptions{Bucket: s.bucket, Object: destination}, minio.CopySrcOptions{Bucket: s.bucket, Object: source, MatchETag: etag})
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
)
|
||||
|
||||
const queryTimeout = 5 * time.Second
|
||||
|
||||
func boundedQuery(ctx context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(ctx, queryTimeout)
|
||||
}
|
||||
@@ -0,0 +1,152 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
// ReserveUpload is committed before any S3 PUT, so an interrupted request is tracked.
|
||||
func (s *AttachmentStore) ReserveUpload(ctx context.Context, a Attachment, grace time.Duration) error {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
_, err := s.db.Exec(ctx, `INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()+$3*interval '1 second')`, a.ID, a.ObjectKey, grace.Seconds())
|
||||
return err
|
||||
}
|
||||
|
||||
// PublishUpload holds the journal row lock across PUT and metadata COMMIT.
|
||||
// Cleanup skips that row; a crash rolls back metadata but preserves the reservation.
|
||||
func (s *AttachmentStore) PublishUpload(ctx context.Context, a Attachment, put func(context.Context) error) error {
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key string
|
||||
err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after>clock_timestamp() FOR UPDATE`, a.ID).Scan(&key)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if key != a.ObjectKey {
|
||||
return errors.New("upload reservation mismatch")
|
||||
}
|
||||
if err = put(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = tx.Exec(ctx, `INSERT INTO attachments(id,chat_id,owner_id,filename,object_key,content_type,size_bytes,sha256,created_at,updated_at) VALUES($1,$2,$3,$4,$5,$6,$7,$8,$9,$10)`, a.ID, a.ChatID, a.OwnerID, a.Filename, a.ObjectKey, a.ContentType, a.SizeBytes, a.SHA256, a.CreatedAt, a.UpdatedAt)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM upload_attempts WHERE id=$1", a.ID); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
type CleanupResult struct {
|
||||
Candidates int `json:"candidates"`
|
||||
Deleted int `json:"deleted"`
|
||||
Eligible int `json:"eligible"`
|
||||
Protected int `json:"protected"`
|
||||
Skipped int `json:"skipped"`
|
||||
Failed int `json:"failed"`
|
||||
DryRun bool `json:"dry_run"`
|
||||
}
|
||||
|
||||
// CleanupUploads only visits journal entries; it never scans or deletes legacy data.
|
||||
func (s *AttachmentStore) CleanupUploads(ctx context.Context, dryRun bool, remove func(context.Context, string) error) (CleanupResult, error) {
|
||||
result := CleanupResult{DryRun: dryRun}
|
||||
rows, err := s.db.Query(ctx, `SELECT id::text FROM upload_attempts WHERE cleanup_after<=clock_timestamp() ORDER BY cleanup_after,id LIMIT 100`)
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
ids := []string{}
|
||||
for rows.Next() {
|
||||
var id string
|
||||
if err = rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return result, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
rows.Close()
|
||||
if err != nil {
|
||||
return result, err
|
||||
}
|
||||
result.Candidates = len(ids)
|
||||
for _, id := range ids {
|
||||
outcome, err := s.cleanupUpload(ctx, id, dryRun, remove)
|
||||
if err != nil {
|
||||
result.Failed++
|
||||
continue
|
||||
}
|
||||
switch outcome {
|
||||
case "eligible":
|
||||
result.Eligible++
|
||||
case "deleted":
|
||||
result.Deleted++
|
||||
case "protected":
|
||||
result.Protected++
|
||||
default:
|
||||
result.Skipped++
|
||||
}
|
||||
}
|
||||
if result.Failed > 0 {
|
||||
return result, fmt.Errorf("%d upload cleanup operations failed", result.Failed)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (s *AttachmentStore) cleanupUpload(ctx context.Context, id string, dryRun bool, remove func(context.Context, string) error) (string, error) {
|
||||
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
||||
defer cancel()
|
||||
tx, err := s.db.Begin(ctx)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer tx.Rollback(context.Background())
|
||||
var key string
|
||||
err = tx.QueryRow(ctx, `SELECT object_key FROM upload_attempts WHERE id=$1 AND cleanup_after<=clock_timestamp() FOR UPDATE SKIP LOCKED`, id).Scan(&key)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return "skipped", nil
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// Restrict destructive actions to server-generated keys even if a journal row is malformed.
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil || key != "originals/"+parsed.String() {
|
||||
return "", errors.New("invalid upload journal key")
|
||||
}
|
||||
var ready bool
|
||||
if err = tx.QueryRow(ctx, "SELECT EXISTS(SELECT 1 FROM attachments WHERE object_key=$1)", key).Scan(&ready); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if dryRun {
|
||||
if ready {
|
||||
return "protected", nil
|
||||
}
|
||||
return "eligible", nil
|
||||
}
|
||||
if !ready {
|
||||
// On DELETE failure retain the journal entry for the next pass.
|
||||
if err = remove(ctx, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
}
|
||||
if _, err = tx.Exec(ctx, "DELETE FROM upload_attempts WHERE id=$1", id); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err = tx.Commit(ctx); err != nil {
|
||||
return "", err
|
||||
}
|
||||
if ready {
|
||||
return "protected", nil
|
||||
}
|
||||
return "deleted", nil
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package repositories
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"display_name"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type UserStore struct {
|
||||
db *pgxpool.Pool
|
||||
}
|
||||
|
||||
func NewUserStore(db *pgxpool.Pool) *UserStore {
|
||||
return &UserStore{db: db}
|
||||
}
|
||||
|
||||
func (s *UserStore) Create(ctx context.Context, username, displayName string, passwordHash []byte) (User, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
user := User{
|
||||
ID: uuid.New().String(),
|
||||
Username: username,
|
||||
DisplayName: displayName,
|
||||
CreatedAt: time.Now().UTC().Truncate(time.Microsecond),
|
||||
}
|
||||
|
||||
_, err := s.db.Exec(ctx, "INSERT INTO users(id,username,display_name,password_hash,created_at) VALUES($1,$2,$3,$4,$5)", user.ID, user.Username, user.DisplayName, passwordHash, user.CreatedAt)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserStore) GetByID(ctx context.Context, id string) (User, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var user User
|
||||
err := s.db.QueryRow(ctx, "SELECT id,username,display_name,created_at FROM users WHERE id=$1", id).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt)
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserStore) GetCredentials(ctx context.Context, username string) (User, []byte, error) {
|
||||
ctx, cancel := boundedQuery(ctx)
|
||||
defer cancel()
|
||||
var user User
|
||||
var packedHash []byte
|
||||
err := s.db.QueryRow(ctx, "SELECT id,username,display_name,created_at,password_hash FROM users WHERE username=$1", username).Scan(&user.ID, &user.Username, &user.DisplayName, &user.CreatedAt, &packedHash)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return User{}, nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return User{}, nil, err
|
||||
}
|
||||
return user, packedHash, nil
|
||||
}
|
||||
@@ -0,0 +1,178 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"image"
|
||||
_ "image/jpeg"
|
||||
_ "image/png"
|
||||
"io"
|
||||
"messenger/src/backend/repositories"
|
||||
"mime"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
const MaxUploadBytes int64 = 10485760
|
||||
const MaxImagePixels int64 = 40000000
|
||||
|
||||
var ErrPayloadTooLarge = errors.New("payload too large")
|
||||
var ErrUnsupportedMedia = errors.New("unsupported image")
|
||||
var ErrAttachmentNotFound = errors.New("attachment not found")
|
||||
|
||||
type AttachmentService struct {
|
||||
metadata *repositories.AttachmentStore
|
||||
objects AttachmentObjects
|
||||
uploadSlot chan struct{}
|
||||
}
|
||||
|
||||
type AttachmentObjects interface {
|
||||
Put(context.Context, string, io.Reader, int64, string) error
|
||||
Get(context.Context, string) (io.ReadCloser, error)
|
||||
}
|
||||
|
||||
func NewAttachmentService(metadata *repositories.AttachmentStore, objects AttachmentObjects) *AttachmentService {
|
||||
return &AttachmentService{metadata: metadata, objects: objects, uploadSlot: make(chan struct{}, 1)}
|
||||
}
|
||||
func ValidateFilename(name string) error {
|
||||
if !utf8.ValidString(name) || strings.TrimSpace(name) == "" || utf8.RuneCountInString(name) > 255 {
|
||||
return InvalidInput{"filename must contain 1-255 characters"}
|
||||
}
|
||||
for _, r := range name {
|
||||
if r == '/' || r == '\\' || unicode.IsControl(r) {
|
||||
return InvalidInput{"invalid filename"}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func ValidateImage(data []byte, declared string) (string, error) {
|
||||
return ValidateImageReader(bytes.NewReader(data), declared)
|
||||
}
|
||||
|
||||
func ValidateImageReader(reader io.ReadSeeker, declared string) (string, error) {
|
||||
contentType, _, err := mime.ParseMediaType(declared)
|
||||
if err != nil || (contentType != "image/png" && contentType != "image/jpeg") {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
config, format, err := image.DecodeConfig(reader)
|
||||
if err != nil {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
actual := "image/" + format
|
||||
if actual != contentType {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
if config.Width <= 0 || config.Height <= 0 || int64(config.Width) > MaxImagePixels/int64(config.Height) {
|
||||
return "", ErrPayloadTooLarge
|
||||
}
|
||||
// DecodeConfig alone accepts truncated headers; decode only after bounding pixels.
|
||||
if _, err = reader.Seek(0, io.SeekStart); err != nil {
|
||||
return "", err
|
||||
}
|
||||
decoded, _, err := image.Decode(reader)
|
||||
if err != nil {
|
||||
return "", ErrUnsupportedMedia
|
||||
}
|
||||
_ = decoded
|
||||
return actual, nil
|
||||
}
|
||||
func (s *AttachmentService) Upload(ctx context.Context, userID, chatID, filename, contentType string, body io.Reader) (repositories.Attachment, error) {
|
||||
select {
|
||||
case s.uploadSlot <- struct{}{}:
|
||||
defer func() { <-s.uploadSlot }()
|
||||
case <-ctx.Done():
|
||||
return repositories.Attachment{}, ctx.Err()
|
||||
}
|
||||
grace, err := repositories.UploadCleanupGrace()
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
directory := os.Getenv("UPLOAD_TMP_DIR")
|
||||
file, err := os.CreateTemp(directory, "upload-*")
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
defer file.Close()
|
||||
// Linux keeps the open descriptor usable; no abandoned pathname survives a crash.
|
||||
if err = os.Remove(file.Name()); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
hash := sha256.New()
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(body, MaxUploadBytes+1), make([]byte, 32*1024))
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if size > MaxUploadBytes {
|
||||
return repositories.Attachment{}, ErrPayloadTooLarge
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
actual, err := ValidateImageReader(file, contentType)
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
now := time.Now().UTC().Truncate(time.Microsecond)
|
||||
a := repositories.Attachment{ID: uuid.NewString(), ChatID: chatID, OwnerID: userID, Filename: filename, ContentType: actual, SizeBytes: size, SHA256: hex.EncodeToString(hash.Sum(nil)), Status: "ready", Variants: []any{}, CreatedAt: now, UpdatedAt: now}
|
||||
a.ObjectKey = "originals/" + a.ID
|
||||
if err = s.metadata.ReserveUpload(ctx, a, grace); err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
err = s.metadata.PublishUpload(ctx, a, func(ctx context.Context) error {
|
||||
return s.objects.Put(ctx, a.ObjectKey, file, a.SizeBytes, a.ContentType)
|
||||
})
|
||||
// On any uncertainty leave the durable reservation to the cleanup worker.
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
return a, nil
|
||||
}
|
||||
func (s *AttachmentService) Get(ctx context.Context, id, userID string) (repositories.Attachment, error) {
|
||||
a, err := s.metadata.GetForMember(ctx, id, userID)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return a, ErrAttachmentNotFound
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
func (s *AttachmentService) Content(ctx context.Context, a repositories.Attachment) (io.ReadCloser, error) {
|
||||
return s.objects.Get(ctx, a.ObjectKey)
|
||||
}
|
||||
|
||||
func NormalizeAttachmentIDs(ids []string) ([]string, error) {
|
||||
if len(ids) > 10 {
|
||||
return nil, InvalidInput{"at most 10 attachments are allowed"}
|
||||
}
|
||||
result := make([]string, 0, len(ids))
|
||||
seen := map[string]bool{}
|
||||
for _, id := range ids {
|
||||
parsed, err := uuid.Parse(id)
|
||||
if err != nil {
|
||||
return nil, InvalidInput{"invalid attachment id"}
|
||||
}
|
||||
id = strings.ToLower(parsed.String())
|
||||
if seen[id] {
|
||||
return nil, InvalidInput{"duplicate attachment id"}
|
||||
}
|
||||
seen[id] = true
|
||||
result = append(result, id)
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
@@ -0,0 +1,145 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type ChatPage struct {
|
||||
Items []repositories.Chat `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type ChatService struct {
|
||||
chats *repositories.ChatStore
|
||||
}
|
||||
|
||||
func NewChatService(chats *repositories.ChatStore) *ChatService {
|
||||
return &ChatService{chats: chats}
|
||||
}
|
||||
|
||||
func ChatHasMember(chat repositories.Chat, userID string) bool {
|
||||
for _, memberID := range chat.MemberIDs {
|
||||
if memberID == userID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (s *ChatService) Create(ctx context.Context, currentUserID, title string, memberIDs []string) (repositories.Chat, error) {
|
||||
if strings.TrimSpace(title) == "" {
|
||||
return repositories.Chat{}, InvalidInput{"title must not be blank"}
|
||||
}
|
||||
if utf8.RuneCountInString(title) > 100 {
|
||||
return repositories.Chat{}, InvalidInput{"title is too long"}
|
||||
}
|
||||
if len(memberIDs) == 0 || len(memberIDs) > 100 {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must contain from 1 to 100 users"}
|
||||
}
|
||||
seen := make(map[string]struct{}, len(memberIDs))
|
||||
normalizedMembers := make([]string, len(memberIDs))
|
||||
for i, memberID := range memberIDs {
|
||||
parsed, err := uuid.Parse(memberID)
|
||||
if err != nil {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must contain valid UUIDs"}
|
||||
}
|
||||
memberID = parsed.String()
|
||||
if _, exists := seen[memberID]; exists {
|
||||
return repositories.Chat{}, InvalidInput{"member_ids must not contain duplicates"}
|
||||
}
|
||||
seen[memberID] = struct{}{}
|
||||
normalizedMembers[i] = memberID
|
||||
}
|
||||
if _, exists := seen[currentUserID]; !exists {
|
||||
return repositories.Chat{}, InvalidInput{"current user must be a chat member"}
|
||||
}
|
||||
chat, err := s.chats.Create(ctx, title, normalizedMembers)
|
||||
if errors.Is(err, repositories.ErrUnknownMember) {
|
||||
return repositories.Chat{}, InvalidInput{"all members must be existing users"}
|
||||
}
|
||||
return chat, err
|
||||
}
|
||||
|
||||
func (s *ChatService) Get(ctx context.Context, currentUserID, chatID string) (repositories.Chat, error) {
|
||||
chat, err := s.chats.GetByID(ctx, chatID)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.Chat{}, ErrChatNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.Chat{}, err
|
||||
}
|
||||
if !ChatHasMember(chat, currentUserID) {
|
||||
return repositories.Chat{}, ErrChatNotFound
|
||||
}
|
||||
return chat, nil
|
||||
}
|
||||
|
||||
func EncodeChatCursor(cursor repositories.ChatCursor) (string, error) {
|
||||
data, err := json.Marshal(cursor)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(data), nil
|
||||
}
|
||||
|
||||
func DecodeChatCursor(value string) (repositories.ChatCursor, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(value)
|
||||
if err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
var cursor repositories.ChatCursor
|
||||
if err := json.Unmarshal(data, &cursor); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
if cursor.UserID == "" || cursor.ChatID == "" || cursor.CreatedAt.IsZero() {
|
||||
return repositories.ChatCursor{}, strconv.ErrSyntax
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.UserID); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.ChatID); err != nil {
|
||||
return repositories.ChatCursor{}, err
|
||||
}
|
||||
return cursor, nil
|
||||
}
|
||||
|
||||
func (s *ChatService) List(ctx context.Context, userID string, limit int, cursorToken string) (ChatPage, error) {
|
||||
if limit < 1 || limit > 100 {
|
||||
return ChatPage{}, InvalidInput{"limit must be between 1 and 100"}
|
||||
}
|
||||
var cursor *repositories.ChatCursor
|
||||
if cursorToken != "" {
|
||||
decoded, err := DecodeChatCursor(cursorToken)
|
||||
if err != nil || decoded.UserID != userID {
|
||||
return ChatPage{}, InvalidInput{"invalid cursor"}
|
||||
}
|
||||
cursor = &decoded
|
||||
}
|
||||
items, more, err := s.chats.ListPageForUser(ctx, userID, cursor, limit)
|
||||
if err != nil {
|
||||
return ChatPage{}, err
|
||||
}
|
||||
page := ChatPage{Items: items}
|
||||
if page.Items == nil {
|
||||
page.Items = []repositories.Chat{}
|
||||
}
|
||||
if more {
|
||||
last := items[len(items)-1]
|
||||
next, err := EncodeChatCursor(repositories.ChatCursor{UserID: userID, CreatedAt: last.CreatedAt, ChatID: last.ID})
|
||||
if err != nil {
|
||||
return ChatPage{}, err
|
||||
}
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,155 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"regexp"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
)
|
||||
|
||||
type DirectUploadInput struct {
|
||||
Filename string `json:"filename"`
|
||||
ContentType string `json:"content_type"`
|
||||
SizeBytes int64 `json:"size_bytes"`
|
||||
SHA256 string `json:"sha256"`
|
||||
}
|
||||
|
||||
type DirectUploadResponse struct {
|
||||
repositories.DirectUpload
|
||||
Upload repositories.PostUpload `json:"upload"`
|
||||
}
|
||||
|
||||
type DirectObjects interface {
|
||||
AttachmentObjects
|
||||
PresignDirect(context.Context, string, string, int64, time.Time) (repositories.PostUpload, error)
|
||||
Stat(context.Context, string) (repositories.StoredObject, error)
|
||||
Copy(context.Context, string, string, string) error
|
||||
}
|
||||
|
||||
var checksumPattern = regexp.MustCompile(`^[a-f0-9]{64}$`)
|
||||
|
||||
func (s *AttachmentService) InitDirect(ctx context.Context, user, chat string, input DirectUploadInput) (DirectUploadResponse, error) {
|
||||
if err := ValidateFilename(input.Filename); err != nil {
|
||||
return DirectUploadResponse{}, err
|
||||
}
|
||||
if input.ContentType != "image/png" && input.ContentType != "image/jpeg" {
|
||||
return DirectUploadResponse{}, ErrUnsupportedMedia
|
||||
}
|
||||
if input.SizeBytes > MaxUploadBytes {
|
||||
return DirectUploadResponse{}, ErrPayloadTooLarge
|
||||
}
|
||||
if input.SizeBytes < 1 || !checksumPattern.MatchString(input.SHA256) {
|
||||
return DirectUploadResponse{}, InvalidInput{"size must be positive and sha256 must contain 64 lowercase hex characters"}
|
||||
}
|
||||
objects, ok := s.objects.(DirectObjects)
|
||||
if !ok {
|
||||
return DirectUploadResponse{}, errors.New("direct uploads unavailable")
|
||||
}
|
||||
id, attachment := uuid.NewString(), uuid.NewString()
|
||||
u, err := s.metadata.CreateDirect(ctx, repositories.DirectUpload{ID: id, AttachmentID: attachment, OwnerID: user, ChatID: chat, Filename: input.Filename, ContentType: input.ContentType, SizeBytes: input.SizeBytes, SHA256: input.SHA256, StagingKey: "staging/direct/" + id, FinalKey: "originals/" + attachment})
|
||||
if err != nil {
|
||||
return DirectUploadResponse{}, err
|
||||
}
|
||||
post, err := objects.PresignDirect(ctx, u.StagingKey, u.ContentType, u.SizeBytes, u.ExpiresAt)
|
||||
return DirectUploadResponse{DirectUpload: u, Upload: post}, err
|
||||
}
|
||||
|
||||
func (s *AttachmentService) CompleteDirect(ctx context.Context, user, chat, id string) (repositories.Attachment, error) {
|
||||
u, err := s.metadata.GetDirect(ctx, id, chat, user)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return repositories.Attachment{}, ErrAttachmentNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.Attachment{}, err
|
||||
}
|
||||
if u.State == "ready" {
|
||||
return s.Get(ctx, u.AttachmentID, user)
|
||||
}
|
||||
objects, ok := s.objects.(DirectObjects)
|
||||
if !ok {
|
||||
return repositories.Attachment{}, errors.New("direct uploads unavailable")
|
||||
}
|
||||
select {
|
||||
case s.uploadSlot <- struct{}{}:
|
||||
defer func() { <-s.uploadSlot }()
|
||||
case <-ctx.Done():
|
||||
return repositories.Attachment{}, ctx.Err()
|
||||
}
|
||||
a, err := s.metadata.CompleteDirect(ctx, id, chat, user, func(ctx context.Context, u repositories.DirectUpload) error {
|
||||
info, err := objects.Stat(ctx, u.StagingKey)
|
||||
if repositories.IsObjectMissing(err) {
|
||||
return repositories.DirectError("upload_not_uploaded")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if info.Size > MaxUploadBytes {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if info.Size != u.SizeBytes {
|
||||
return repositories.DirectError("upload_size_mismatch")
|
||||
}
|
||||
// A signed staging key remains writable until expiry. Validate the isolated
|
||||
// final object, never a staging object that can change after validation.
|
||||
if err = objects.Copy(ctx, u.StagingKey, u.FinalKey, info.ETag); err != nil {
|
||||
return err
|
||||
}
|
||||
reader, err := objects.Get(ctx, u.FinalKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer reader.Close()
|
||||
file, err := os.CreateTemp(os.Getenv("UPLOAD_TMP_DIR"), "direct-*")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer file.Close()
|
||||
if err = os.Remove(file.Name()); err != nil {
|
||||
return err
|
||||
}
|
||||
hash := sha256.New()
|
||||
size, err := io.CopyBuffer(io.MultiWriter(file, hash), io.LimitReader(reader, MaxUploadBytes+1), make([]byte, 32*1024))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if size > MaxUploadBytes {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if size != u.SizeBytes {
|
||||
return repositories.DirectError("upload_size_mismatch")
|
||||
}
|
||||
if hex.EncodeToString(hash.Sum(nil)) != u.SHA256 {
|
||||
return repositories.DirectError("upload_checksum_mismatch")
|
||||
}
|
||||
if err = ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = file.Seek(0, io.SeekStart); err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = ValidateImageReader(file, u.ContentType)
|
||||
if errors.Is(err, ErrPayloadTooLarge) {
|
||||
return repositories.DirectError("payload_too_large")
|
||||
}
|
||||
if errors.Is(err, ErrUnsupportedMedia) {
|
||||
return repositories.DirectError("unsupported_media_type")
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return ctx.Err()
|
||||
})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return a, ErrAttachmentNotFound
|
||||
}
|
||||
return a, err
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package services
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrConflict = errors.New("conflict")
|
||||
ErrUnknownUser = errors.New("unknown user")
|
||||
ErrChatNotFound = errors.New("chat not found")
|
||||
ErrInvalidSession = errors.New("invalid session")
|
||||
)
|
||||
|
||||
type InvalidInput struct {
|
||||
Message string
|
||||
}
|
||||
|
||||
func (e InvalidInput) Error() string { return e.Message }
|
||||
@@ -0,0 +1,34 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type HealthService struct {
|
||||
health *repositories.HealthStore
|
||||
sessions *repositories.SessionStore
|
||||
objects interface{ Ping(context.Context) error }
|
||||
}
|
||||
|
||||
func NewHealthService(health *repositories.HealthStore, sessions *repositories.SessionStore, objects ...interface{ Ping(context.Context) error }) *HealthService {
|
||||
service := &HealthService{health: health, sessions: sessions}
|
||||
if len(objects) > 0 {
|
||||
service.objects = objects[0]
|
||||
}
|
||||
return service
|
||||
}
|
||||
|
||||
func (s *HealthService) Ready(ctx context.Context) error {
|
||||
if err := s.health.Ping(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.sessions.Ping(ctx); err != nil {
|
||||
return err
|
||||
}
|
||||
if s.objects != nil {
|
||||
return s.objects.Ping(ctx)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
var idempotencyKeyPattern = regexp.MustCompile(`^[A-Za-z0-9._:-]{1,128}$`)
|
||||
|
||||
func (s *MessageService) CreateIdempotent(ctx context.Context, userID string, chat repositories.Chat, text, key string, attachments ...[]string) (repositories.Message, error) {
|
||||
if !ChatHasMember(chat, userID) {
|
||||
return repositories.Message{}, ErrChatNotFound
|
||||
}
|
||||
if !idempotencyKeyPattern.MatchString(key) {
|
||||
return repositories.Message{}, InvalidInput{"Idempotency-Key must contain 1-128 ASCII letters, digits or ._:-"}
|
||||
}
|
||||
var ids []string
|
||||
if len(attachments) > 0 {
|
||||
ids = attachments[0]
|
||||
}
|
||||
ids, err := NormalizeAttachmentIDs(ids)
|
||||
if err != nil {
|
||||
return repositories.Message{}, err
|
||||
}
|
||||
if (strings.TrimSpace(text) == "" && len(ids) == 0) || utf8.RuneCountInString(text) > 4000 {
|
||||
return repositories.Message{}, InvalidInput{"text must be at most 4000 characters; nonblank text or attachments required"}
|
||||
}
|
||||
message, err := s.messages.CreateIdempotent(ctx, chat.ID, userID, text, key, ids)
|
||||
if errors.Is(err, repositories.ErrAttachmentNotFound) {
|
||||
return repositories.Message{}, ErrAttachmentNotFound
|
||||
}
|
||||
if errors.Is(err, repositories.ErrIdempotencyConflict) {
|
||||
return repositories.Message{}, ErrConflict
|
||||
}
|
||||
return message, err
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strconv"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type MessagePage struct {
|
||||
Items []repositories.Message `json:"items"`
|
||||
NextCursor *string `json:"next_cursor"`
|
||||
}
|
||||
|
||||
type MessageService struct {
|
||||
messages *repositories.MessageStore
|
||||
}
|
||||
|
||||
func NewMessageService(messages *repositories.MessageStore) *MessageService {
|
||||
return &MessageService{messages: messages}
|
||||
}
|
||||
|
||||
func EncodeMessageCursor(cursor repositories.MessageCursor) (string, error) {
|
||||
data, err := json.Marshal(cursor)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(data), nil
|
||||
}
|
||||
|
||||
func DecodeMessageCursor(value string) (repositories.MessageCursor, error) {
|
||||
data, err := base64.RawURLEncoding.DecodeString(value)
|
||||
if err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
var cursor repositories.MessageCursor
|
||||
if err := json.Unmarshal(data, &cursor); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
if cursor.ChatID == "" || cursor.MessageID == "" || cursor.CreatedAt.IsZero() {
|
||||
return repositories.MessageCursor{}, strconv.ErrSyntax
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.ChatID); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
if _, err := uuid.Parse(cursor.MessageID); err != nil {
|
||||
return repositories.MessageCursor{}, err
|
||||
}
|
||||
return cursor, nil
|
||||
}
|
||||
|
||||
func (s *MessageService) List(ctx context.Context, userID string, chat repositories.Chat, limit int, cursorToken string) (MessagePage, error) {
|
||||
if !ChatHasMember(chat, userID) {
|
||||
return MessagePage{}, ErrChatNotFound
|
||||
}
|
||||
if limit < 1 || limit > 100 {
|
||||
return MessagePage{}, InvalidInput{"limit must be between 1 and 100"}
|
||||
}
|
||||
var cursor *repositories.MessageCursor
|
||||
if cursorToken != "" {
|
||||
decoded, err := DecodeMessageCursor(cursorToken)
|
||||
if err != nil || decoded.ChatID != chat.ID {
|
||||
return MessagePage{}, InvalidInput{"invalid cursor"}
|
||||
}
|
||||
cursor = &decoded
|
||||
}
|
||||
items, more, err := s.messages.ListPageByChat(ctx, chat.ID, cursor, limit)
|
||||
if err != nil {
|
||||
return MessagePage{}, err
|
||||
}
|
||||
page := MessagePage{Items: items}
|
||||
if page.Items == nil {
|
||||
page.Items = []repositories.Message{}
|
||||
}
|
||||
if more {
|
||||
last := items[len(items)-1]
|
||||
next, err := EncodeMessageCursor(repositories.MessageCursor{ChatID: chat.ID, CreatedAt: last.CreatedAt, MessageID: last.ID})
|
||||
if err != nil {
|
||||
return MessagePage{}, err
|
||||
}
|
||||
page.NextCursor = &next
|
||||
}
|
||||
return page, nil
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/subtle"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"golang.org/x/crypto/argon2"
|
||||
)
|
||||
|
||||
const (
|
||||
argonMemory uint32 = 64 * 1024
|
||||
argonIterations uint32 = 3
|
||||
argonParallelism uint8 = 2
|
||||
passwordSaltSize = 16
|
||||
passwordHashSize = 32
|
||||
)
|
||||
|
||||
// HashPassword stores the salt and Argon2id parameters in one PHC-encoded value.
|
||||
func HashPassword(password string) ([]byte, error) {
|
||||
salt := make([]byte, passwordSaltSize)
|
||||
if _, err := rand.Read(salt); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
hash := argon2.IDKey([]byte(password), salt, argonIterations, argonMemory, argonParallelism, passwordHashSize)
|
||||
encoded := fmt.Sprintf("$argon2id$v=%d$m=%d,t=%d,p=%d$%s$%s",
|
||||
argon2.Version, argonMemory, argonIterations, argonParallelism,
|
||||
base64.RawStdEncoding.EncodeToString(salt),
|
||||
base64.RawStdEncoding.EncodeToString(hash),
|
||||
)
|
||||
return []byte(encoded), nil
|
||||
}
|
||||
|
||||
func VerifyPassword(password string, encoded []byte) (bool, error) {
|
||||
parts := strings.Split(string(encoded), "$")
|
||||
if len(parts) != 6 || parts[0] != "" || parts[1] != "argon2id" || parts[2] != fmt.Sprintf("v=%d", argon2.Version) {
|
||||
return false, errors.New("invalid password hash format")
|
||||
}
|
||||
var memory, iterations uint32
|
||||
var parallelism uint8
|
||||
if _, err := fmt.Sscanf(parts[3], "m=%d,t=%d,p=%d", &memory, &iterations, ¶llelism); err != nil ||
|
||||
fmt.Sprintf("m=%d,t=%d,p=%d", memory, iterations, parallelism) != parts[3] ||
|
||||
memory < 8*1024 || memory > 128*1024 || iterations < 1 || iterations > 10 || parallelism < 1 || parallelism > 8 {
|
||||
return false, errors.New("invalid password hash parameters")
|
||||
}
|
||||
salt, err := base64.RawStdEncoding.Strict().DecodeString(parts[4])
|
||||
if err != nil || len(salt) != passwordSaltSize {
|
||||
return false, errors.New("invalid password salt")
|
||||
}
|
||||
expected, err := base64.RawStdEncoding.Strict().DecodeString(parts[5])
|
||||
if err != nil || len(expected) != passwordHashSize {
|
||||
return false, errors.New("invalid password hash")
|
||||
}
|
||||
actual := argon2.IDKey([]byte(password), salt, iterations, memory, parallelism, passwordHashSize)
|
||||
return subtle.ConstantTimeCompare(expected, actual) == 1, nil
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"os"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
type SessionService struct {
|
||||
users *UserService
|
||||
sessions *repositories.SessionStore
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
func SessionTTL() (time.Duration, error) {
|
||||
value := os.Getenv("SESSION_TTL_SECONDS")
|
||||
if value == "" {
|
||||
return time.Hour, nil
|
||||
}
|
||||
seconds, err := strconv.Atoi(value)
|
||||
if err != nil || seconds < 1 || seconds > 31536000 {
|
||||
return 0, errors.New("invalid SESSION_TTL_SECONDS")
|
||||
}
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
|
||||
func NewSessionService(users *UserService, sessions *repositories.SessionStore, ttl time.Duration) *SessionService {
|
||||
return &SessionService{users: users, sessions: sessions, ttl: ttl}
|
||||
}
|
||||
|
||||
func (s *SessionService) Login(ctx context.Context, username, password string) (repositories.User, string, time.Time, bool, error) {
|
||||
user, valid, err := s.users.Authenticate(ctx, username, password)
|
||||
if err != nil || !valid {
|
||||
return repositories.User{}, "", time.Time{}, valid, err
|
||||
}
|
||||
token, expires, err := s.CreateForUser(ctx, user.ID)
|
||||
if err != nil {
|
||||
return repositories.User{}, "", time.Time{}, false, err
|
||||
}
|
||||
return user, token, expires, true, nil
|
||||
}
|
||||
|
||||
func (s *SessionService) CreateForUser(ctx context.Context, userID string) (string, time.Time, error) {
|
||||
raw := make([]byte, 32)
|
||||
if _, err := rand.Read(raw); err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
token := hex.EncodeToString(raw)
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
expires := time.Now().UTC().Add(s.ttl).Truncate(time.Microsecond)
|
||||
if err := s.sessions.Create(ctx, hash[:], userID, expires); err != nil {
|
||||
return "", time.Time{}, err
|
||||
}
|
||||
return token, expires, nil
|
||||
}
|
||||
|
||||
func (s *SessionService) User(ctx context.Context, token string) (repositories.User, error) {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
id, err := s.sessions.UserID(ctx, hash[:])
|
||||
if errors.Is(err, repositories.ErrSessionNotFound) {
|
||||
return repositories.User{}, ErrInvalidSession
|
||||
}
|
||||
if err != nil {
|
||||
return repositories.User{}, err
|
||||
}
|
||||
user, err := s.users.Get(ctx, id)
|
||||
if errors.Is(err, ErrUnknownUser) {
|
||||
return repositories.User{}, ErrInvalidSession
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *SessionService) Revoke(ctx context.Context, token string) error {
|
||||
hash := sha256.Sum256([]byte(token))
|
||||
return s.sessions.Revoke(ctx, hash[:])
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package services
|
||||
|
||||
import (
|
||||
"context"
|
||||
"regexp"
|
||||
"strings"
|
||||
"unicode/utf8"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
var usernamePattern = regexp.MustCompile(`^[a-z0-9_]{3,32}$`)
|
||||
|
||||
type UserService struct {
|
||||
users *repositories.UserStore
|
||||
}
|
||||
|
||||
func NewUserService(users *repositories.UserStore) *UserService {
|
||||
return &UserService{users: users}
|
||||
}
|
||||
|
||||
func (s *UserService) Create(ctx context.Context, username, displayName, password string) (repositories.User, error) {
|
||||
if !usernamePattern.MatchString(username) {
|
||||
return repositories.User{}, InvalidInput{"username must contain 3-32 lowercase latin letters, digits or underscores"}
|
||||
}
|
||||
if strings.TrimSpace(displayName) == "" {
|
||||
return repositories.User{}, InvalidInput{"display_name must not be blank"}
|
||||
}
|
||||
if utf8.RuneCountInString(displayName) > 100 {
|
||||
return repositories.User{}, InvalidInput{"display_name is too long"}
|
||||
}
|
||||
if strings.TrimSpace(password) == "" || utf8.RuneCountInString(password) < 12 || utf8.RuneCountInString(password) > 128 {
|
||||
return repositories.User{}, InvalidInput{"password must contain 12-128 characters"}
|
||||
}
|
||||
hash, err := HashPassword(password)
|
||||
if err != nil {
|
||||
return repositories.User{}, err
|
||||
}
|
||||
user, err := s.users.Create(ctx, username, displayName, hash)
|
||||
if repositories.UniqueViolation(err) {
|
||||
return repositories.User{}, ErrConflict
|
||||
}
|
||||
if repositories.CheckViolation(err) {
|
||||
return repositories.User{}, InvalidInput{"invalid user fields"}
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserService) Get(ctx context.Context, id string) (repositories.User, error) {
|
||||
user, err := s.users.GetByID(ctx, id)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.User{}, ErrUnknownUser
|
||||
}
|
||||
return user, err
|
||||
}
|
||||
|
||||
func (s *UserService) Authenticate(ctx context.Context, username, password string) (repositories.User, bool, error) {
|
||||
if !usernamePattern.MatchString(username) || utf8.RuneCountInString(password) < 12 || utf8.RuneCountInString(password) > 128 {
|
||||
return repositories.User{}, false, nil
|
||||
}
|
||||
user, packedHash, err := s.users.GetCredentials(ctx, username)
|
||||
if repositories.NotFound(err) {
|
||||
return repositories.User{}, false, nil
|
||||
}
|
||||
if err != nil || packedHash == nil {
|
||||
return repositories.User{}, false, err
|
||||
}
|
||||
valid, err := VerifyPassword(password, packedHash)
|
||||
if err != nil {
|
||||
return repositories.User{}, false, err
|
||||
}
|
||||
return user, valid, nil
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestAttachmentCleanupGraceLocksProtectionAndRetry(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
makeAttempt := func(age bool, ready bool) repositories.Attachment {
|
||||
id := uuid.NewString()
|
||||
now := time.Now().UTC()
|
||||
a := repositories.Attachment{ID: id, ObjectKey: "originals/" + id, ChatID: chat, OwnerID: user, Filename: "cleanup.png", ContentType: "image/png", SizeBytes: int64(len(data)), SHA256: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", CreatedAt: now, UpdatedAt: now}
|
||||
if err := objects.Put(ctx, a.ObjectKey, bytes.NewReader(data), a.SizeBytes, a.ContentType); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { objects.Delete(context.Background(), a.ObjectKey) })
|
||||
if err := store.ReserveUpload(ctx, a, 600*time.Second); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if age {
|
||||
if _, err := f.db.Exec(ctx, "UPDATE upload_attempts SET cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1", id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if ready {
|
||||
if err := store.Create(ctx, a); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
return a
|
||||
}
|
||||
fresh := makeAttempt(false, false)
|
||||
expired := makeAttempt(true, false)
|
||||
ready := makeAttempt(true, true)
|
||||
locked := makeAttempt(true, false)
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, "SELECT id FROM upload_attempts WHERE id=$1 FOR UPDATE", locked.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dry, err := store.CleanupUploads(ctx, true, objects.Delete)
|
||||
if err != nil || dry.Deleted != 0 || dry.Candidates != 3 || dry.Protected != 1 {
|
||||
t.Fatalf("dry run: %+v %v", dry, err)
|
||||
}
|
||||
result, err := store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 1 || result.Protected != 1 || result.Skipped != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
if content, err := objects.Get(ctx, expired.ObjectKey); err == nil {
|
||||
content.Close()
|
||||
t.Fatal("expired object not removed")
|
||||
}
|
||||
for _, a := range []repositories.Attachment{fresh, ready, locked} {
|
||||
content, err := objects.Get(ctx, a.ObjectKey)
|
||||
if err != nil {
|
||||
t.Fatalf("protected object removed: %v", err)
|
||||
}
|
||||
content.Close()
|
||||
}
|
||||
tx.Rollback(ctx)
|
||||
failure, err := store.CleanupUploads(ctx, false, func(context.Context, string) error { return errors.New("injected S3 DELETE failure") })
|
||||
if err == nil || failure.Failed != 1 {
|
||||
t.Fatalf("DELETE failure: %+v %v", failure, err)
|
||||
}
|
||||
var count int
|
||||
f.db.QueryRow(ctx, "SELECT count(*) FROM upload_attempts WHERE id=$1", locked.ID).Scan(&count)
|
||||
if count != 1 {
|
||||
t.Fatal("DELETE failure lost journal")
|
||||
}
|
||||
result, err = store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 1 {
|
||||
t.Fatalf("retry: %+v %v", result, err)
|
||||
}
|
||||
result, err = store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Candidates != 0 {
|
||||
t.Fatalf("repeat not idempotent: %+v %v", result, err)
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err = store.CleanupUploads(cancelled, false, objects.Delete); err == nil {
|
||||
t.Fatal("cancelled DB operation succeeded")
|
||||
}
|
||||
f.db.QueryRow(ctx, "SELECT count(*) FROM upload_attempts WHERE id=$1", fresh.ID).Scan(&count)
|
||||
if count != 1 {
|
||||
t.Fatal("DB failure lost fresh reservation")
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentCleanupInvalidKeyIsNeverDeleted(t *testing.T) {
|
||||
f, store, _, _, _ := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
if _, err := f.db.Exec(ctx, "INSERT INTO upload_attempts(id,object_key,cleanup_after) VALUES($1,$2,clock_timestamp()-interval '1 second')", uuid.NewString(), "do-not-delete/anything"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
called := false
|
||||
result, err := store.CleanupUploads(ctx, false, func(context.Context, string) error { called = true; return nil })
|
||||
if err == nil || called || result.Failed != 1 {
|
||||
t.Fatalf("unsafe key: %+v %v delete=%v", result, err, called)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,171 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"encoding/json"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func attachmentRouter(f *testFixture, objects *repositories.ObjectStore) http.Handler {
|
||||
users := services.NewUserService(f.users)
|
||||
return handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(f.db), f.sessionStore), users, f.sessions, services.NewChatService(f.chats), services.NewMessageService(f.messages), services.NewAttachmentService(repositories.NewAttachmentStore(f.db), objects))
|
||||
}
|
||||
func attachmentCall(t *testing.T, f *testFixture, router http.Handler, method, path, user, mime string, body []byte, status int) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(method, path, bytes.NewReader(body))
|
||||
if mime != "" {
|
||||
r.Header.Set("Content-Type", mime)
|
||||
}
|
||||
if method == "POST" && strings.HasSuffix(path, "/messages") {
|
||||
r.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
}
|
||||
if user != "" {
|
||||
authorizeTestRequest(t, f, r, user)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, r)
|
||||
if w.Code != status {
|
||||
t.Fatalf("%s %s: expected %d, got %d: %s", method, path, status, w.Code, w.Body.String())
|
||||
}
|
||||
if status >= 400 {
|
||||
var error struct {
|
||||
Error struct {
|
||||
RequestID string `json:"request_id"`
|
||||
}
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &error); err != nil || error.Error.RequestID != w.Header().Get("X-Request-Id") {
|
||||
t.Fatal("invalid error envelope")
|
||||
}
|
||||
}
|
||||
return w
|
||||
}
|
||||
func TestAttachmentValidationHTTP(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachments?filename=photo.png"
|
||||
png := uploadPNG(t)
|
||||
for _, sample := range []struct {
|
||||
body []byte
|
||||
mime string
|
||||
status int
|
||||
}{{bytes.Repeat([]byte("x"), 10485761), "image/png", 413}, {png[:40], "image/png", 415}, {png, "image/jpeg", 415}, {png, "text/plain", 415}} {
|
||||
attachmentCall(t, f, router, "POST", path, user, sample.mime, sample.body, sample.status)
|
||||
}
|
||||
huge := append([]byte(nil), png...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
attachmentCall(t, f, router, "POST", path, user, "image/png", huge, 413)
|
||||
for _, name := range []string{"", " ", "../file.png", `a\b.png`, "a\x00.png", "a\n.png", strings.Repeat("x", 256)} {
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/attachments?"+url.Values{"filename": {name}}.Encode(), user, "image/png", png, 400)
|
||||
}
|
||||
// Exercise the real HTTP chunked path without Content-Length.
|
||||
server := httptest.NewServer(router)
|
||||
defer server.Close()
|
||||
token, _, err := f.sessions.CreateForUser(context.Background(), user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reader, writer := io.Pipe()
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
defer writer.Close()
|
||||
io.Copy(writer, bytes.NewReader(bytes.Repeat([]byte("x"), 10485761)))
|
||||
}()
|
||||
request, err := http.NewRequest("POST", server.URL+path, reader)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
request.Header.Set("Content-Type", "image/png")
|
||||
response, err := server.Client().Do(request)
|
||||
if err != nil {
|
||||
reader.Close()
|
||||
<-done
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
reader.Close()
|
||||
<-done
|
||||
if response.StatusCode != 413 {
|
||||
t.Fatalf("chunked limit: %d", response.StatusCode)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentAccessHTTP(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
router := attachmentRouter(f, objects)
|
||||
eve, _ := createTestUser(t, f.users, "eve", "Eve")
|
||||
other := createTestChat(t, f.chats, "Other", []string{user})
|
||||
a, err := services.NewAttachmentService(store, objects).Upload(context.Background(), user, chat, "photo.png", "image/png", bytes.NewReader(uploadPNG(t)))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { objects.Delete(context.Background(), a.ObjectKey) })
|
||||
for _, suffix := range []string{"", "/content"} {
|
||||
path := "/api/v1/attachments/" + a.ID + suffix
|
||||
attachmentCall(t, f, router, "GET", path, eve.ID, "", nil, 404)
|
||||
attachmentCall(t, f, router, "GET", path, "", "", nil, 401)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/attachments?filename=photo.png", eve.ID, "image/png", uploadPNG(t), 404)
|
||||
for _, target := range []string{uuid.NewString(), a.ID} {
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+other.ID+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+target+`"]}`), 404)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+a.ID+`","`+a.ID+`"]}`), 400)
|
||||
}
|
||||
|
||||
func TestUploadRecoveryHTTPDisconnect(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
directory := t.TempDir()
|
||||
t.Setenv("UPLOAD_TMP_DIR", directory)
|
||||
completed := make(chan struct{})
|
||||
router := attachmentRouter(f, objects)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer close(completed); router.ServeHTTP(w, r) }))
|
||||
defer server.Close()
|
||||
token, _, err := f.sessions.CreateForUser(context.Background(), user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
address := strings.TrimPrefix(server.URL, "http://")
|
||||
connection, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
raw := "POST /api/v1/chats/" + chat + "/attachments?filename=aborted.png HTTP/1.1\r\nHost: " + address + "\r\nAuthorization: Bearer " + token + "\r\nContent-Type: image/png\r\nContent-Length: 10485760\r\n\r\npartial"
|
||||
if _, err = io.WriteString(connection, raw); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
connection.Close()
|
||||
select {
|
||||
case <-completed:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("disconnected upload did not stop")
|
||||
}
|
||||
var count int
|
||||
f.db.QueryRow(context.Background(), "SELECT count(*) FROM upload_attempts").Scan(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("incomplete HTTP body reserved an object")
|
||||
}
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil || len(files) > 0 {
|
||||
t.Fatalf("temporary paths remain: %v %v", files, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"image/png"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestAttachmentImageValidation(t *testing.T) {
|
||||
img := image.NewRGBA(image.Rect(0, 0, 3, 2))
|
||||
var pngData, jpegData bytes.Buffer
|
||||
if err := png.Encode(&pngData, img); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := jpeg.Encode(&jpegData, img, nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{pngData.Bytes(), "image/png"}, {jpegData.Bytes(), "image/jpeg"}} {
|
||||
if actual, err := services.ValidateImage(sample.data, sample.mime); err != nil || actual != sample.mime {
|
||||
t.Fatalf("valid image: %s %v", actual, err)
|
||||
}
|
||||
}
|
||||
huge := append([]byte(nil), pngData.Bytes()...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
if _, err := services.ValidateImage(huge, "image/png"); !errors.Is(err, services.ErrPayloadTooLarge) {
|
||||
t.Fatalf("pixel limit: %v", err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{pngData.Bytes(), "image/jpeg"}, {jpegData.Bytes(), "image/png"}, {pngData.Bytes()[:40], "image/png"}, {[]byte("not an image"), "image/png"}, {pngData.Bytes(), "text/plain"}} {
|
||||
if _, err := services.ValidateImage(sample.data, sample.mime); !errors.Is(err, services.ErrUnsupportedMedia) {
|
||||
t.Fatalf("invalid image accepted: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentFilenameValidation(t *testing.T) {
|
||||
for _, name := range []string{"", " ", "../photo.png", `a\b.png`, "photo\x00.png", "photo\n.png"} {
|
||||
if services.ValidateFilename(name) == nil {
|
||||
t.Fatalf("accepted %q", name)
|
||||
}
|
||||
}
|
||||
if err := services.ValidateFilename("Фотография 1.png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAttachmentMessageRequiresStringText(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Images", []string{user.ID})
|
||||
id := uuid.NewString()
|
||||
// Repository-level fixture: message validation does not fetch S3 bytes.
|
||||
err := repositories.NewAttachmentStore(f.db).Create(context.Background(), repositories.Attachment{
|
||||
ID: id, ChatID: chat.ID, OwnerID: user.ID, Filename: "photo.png", ObjectKey: "test/" + id,
|
||||
ContentType: "image/png", SizeBytes: 1, SHA256: strings.Repeat("a", 64), CreatedAt: time.Now().UTC(), UpdatedAt: time.Now().UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, body := range []string{`{"attachment_ids":["` + id + `"]}`, `{"text":null,"attachment_ids":["` + id + `"]}`} {
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/chats/"+chat.ID+"/messages", strings.NewReader(body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
r.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
authorizeTestRequest(t, f, r, user.ID)
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("expected 400, got %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
||||
t.Fatal("invalid request saved a message")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,218 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"messenger/src/backend/repositories"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strconv"
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConcurrentMessageCreation(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore := fixture.users, fixture.chats
|
||||
|
||||
const clientsCount = 10
|
||||
const messagesPerClient = 10
|
||||
const expectedMessages = clientsCount * messagesPerClient
|
||||
|
||||
memberIDs := make([]string, 0, clientsCount)
|
||||
users := make([]User, 0, clientsCount)
|
||||
|
||||
for i := range clientsCount {
|
||||
user, created := createTestUser(t, userStore,
|
||||
"user"+strconv.Itoa(i),
|
||||
"Пользователь "+strconv.Itoa(i),
|
||||
)
|
||||
|
||||
if !created {
|
||||
t.Fatalf("failed to create test user %d", i)
|
||||
}
|
||||
|
||||
users = append(users, user)
|
||||
memberIDs = append(memberIDs, user.ID)
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Конкурентный чат",
|
||||
memberIDs,
|
||||
)
|
||||
|
||||
server := httptest.NewServer(fixture.router)
|
||||
defer server.Close()
|
||||
|
||||
client := server.Client()
|
||||
tokens := make([]string, clientsCount)
|
||||
for i, user := range users {
|
||||
request := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
tokens[i] = request.Header.Get("Authorization")
|
||||
}
|
||||
|
||||
var waitGroup sync.WaitGroup
|
||||
messageIDs := make(chan string, expectedMessages)
|
||||
errors := make(chan error, expectedMessages)
|
||||
|
||||
for clientIndex := range clientsCount {
|
||||
waitGroup.Add(1)
|
||||
|
||||
go func(index int) {
|
||||
defer waitGroup.Done()
|
||||
|
||||
for messageIndex := range messagesPerClient {
|
||||
body := fmt.Appendf(nil,
|
||||
`{"text":"client-%d message-%d"}`,
|
||||
index,
|
||||
messageIndex,
|
||||
)
|
||||
|
||||
request, err := http.NewRequest(
|
||||
http.MethodPost,
|
||||
server.URL+"/api/v1/chats/"+chat.ID+"/messages",
|
||||
bytes.NewReader(body),
|
||||
)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: create request: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Authorization", tokens[index])
|
||||
request.Header.Set("Idempotency-Key", fmt.Sprintf("client-%d-message-%d", index, messageIndex))
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: send request: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
responseBody, readErr := io.ReadAll(response.Body)
|
||||
response.Body.Close()
|
||||
|
||||
if readErr != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: read response: %w",
|
||||
index,
|
||||
readErr,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if response.StatusCode != http.StatusCreated {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: expected status %d, got %d; body: %s",
|
||||
index,
|
||||
http.StatusCreated,
|
||||
response.StatusCode,
|
||||
string(responseBody),
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
var message repositories.Message
|
||||
|
||||
if err := json.Unmarshal(responseBody, &message); err != nil {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: decode response: %w",
|
||||
index,
|
||||
err,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
if message.ID == "" {
|
||||
errors <- fmt.Errorf(
|
||||
"client %d: response contains empty message ID",
|
||||
index,
|
||||
)
|
||||
continue
|
||||
}
|
||||
|
||||
messageIDs <- message.ID
|
||||
}
|
||||
}(clientIndex)
|
||||
}
|
||||
|
||||
waitGroup.Wait()
|
||||
|
||||
close(errors)
|
||||
close(messageIDs)
|
||||
|
||||
for err := range errors {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
createdIDs := make(map[string]bool)
|
||||
|
||||
for messageID := range messageIDs {
|
||||
if createdIDs[messageID] {
|
||||
t.Fatalf("duplicate message ID returned: %s", messageID)
|
||||
}
|
||||
|
||||
createdIDs[messageID] = true
|
||||
}
|
||||
|
||||
if len(createdIDs) != expectedMessages {
|
||||
t.Fatalf(
|
||||
"expected %d unique created messages, got %d",
|
||||
expectedMessages,
|
||||
len(createdIDs),
|
||||
)
|
||||
}
|
||||
|
||||
// Дополнительно проверяем историю через HTTP API.
|
||||
request, err := http.NewRequest(
|
||||
http.MethodGet,
|
||||
server.URL+"/api/v1/chats/"+chat.ID+"/messages?limit=100",
|
||||
nil,
|
||||
)
|
||||
if err != nil {
|
||||
t.Fatalf("create history request: %v", err)
|
||||
}
|
||||
|
||||
request.Header.Set("Authorization", tokens[0])
|
||||
|
||||
response, err := client.Do(request)
|
||||
if err != nil {
|
||||
t.Fatalf("request message history: %v", err)
|
||||
}
|
||||
defer response.Body.Close()
|
||||
|
||||
if response.StatusCode != http.StatusOK {
|
||||
body, _ := io.ReadAll(response.Body)
|
||||
|
||||
t.Fatalf(
|
||||
"expected history status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
response.StatusCode,
|
||||
string(body),
|
||||
)
|
||||
}
|
||||
|
||||
var page messagePage
|
||||
|
||||
if err := json.NewDecoder(response.Body).Decode(&page); err != nil {
|
||||
t.Fatalf("decode message history: %v", err)
|
||||
}
|
||||
|
||||
if len(page.Items) != expectedMessages {
|
||||
t.Fatalf(
|
||||
"expected %d messages in history, got %d",
|
||||
expectedMessages,
|
||||
len(page.Items),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestConnectionFilesTakePrecedence(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
database := filepath.Join(dir, "database")
|
||||
cache := filepath.Join(dir, "cache")
|
||||
if err := os.WriteFile(database, []byte("postgres://configured/db\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(cache, []byte("redis://:test-password@configured:6380/3\n"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Setenv("DATABASE_URL_FILE", database)
|
||||
t.Setenv("DATABASE_URL", "postgres://ignored/db")
|
||||
t.Setenv("CACHE_URL_FILE", cache)
|
||||
t.Setenv("REDIS_ADDR", "ignored:6379")
|
||||
url, err := repositories.DatabaseURL()
|
||||
if err != nil || url != "postgres://configured/db" {
|
||||
t.Fatalf("database file not used: %v", err)
|
||||
}
|
||||
client, err := repositories.RedisClientFromEnvironment()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer client.Close()
|
||||
options := client.Options()
|
||||
if options.Addr != "configured:6380" || options.DB != 3 || options.Password != "test-password" {
|
||||
t.Fatal("Redis URL settings not applied")
|
||||
}
|
||||
t.Setenv("CACHE_URL_FILE", filepath.Join(dir, "missing"))
|
||||
if _, err := repositories.RedisClientFromEnvironment(); err == nil {
|
||||
t.Fatal("missing file accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfiguredIdempotencyTTL(t *testing.T) {
|
||||
t.Setenv("IDEMPOTENCY_TTL_SECONDS", "2")
|
||||
ttl, err := repositories.IdempotencyTTL()
|
||||
if err != nil || ttl != 2*time.Second {
|
||||
t.Fatalf("TTL not applied: %v", err)
|
||||
}
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{user.ID})
|
||||
store := repositories.NewMessageStoreWithTTL(f.db, ttl)
|
||||
ctx := context.Background()
|
||||
original, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var remaining float64
|
||||
if err := f.db.QueryRow(ctx, "SELECT extract(epoch FROM expires_at-clock_timestamp()) FROM message_idempotency WHERE key='ttl-test'").Scan(&remaining); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if remaining <= 0 || remaining > 2 {
|
||||
t.Fatalf("unexpected remaining TTL: %f", remaining)
|
||||
}
|
||||
replay, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil || replay.ID != original.ID {
|
||||
t.Fatal("replay before expiry changed the message")
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, "UPDATE message_idempotency SET expires_at=clock_timestamp()-interval '1 second' WHERE key='ttl-test'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reused, err := store.CreateIdempotent(ctx, chat.ID, user.ID, "hello", "ttl-test")
|
||||
if err != nil || reused.ID == original.ID {
|
||||
t.Fatal("expired key was not reusable")
|
||||
}
|
||||
if len(listTestMessages(t, store, chat.ID)) != 2 {
|
||||
t.Fatal("expiry removed an original message")
|
||||
}
|
||||
}
|
||||
|
||||
func TestInvalidIdempotencyTTL(t *testing.T) {
|
||||
for _, value := range []string{"0", "-1", "abc", "31536001"} {
|
||||
t.Setenv("IDEMPOTENCY_TTL_SECONDS", value)
|
||||
if _, err := repositories.IdempotencyTTL(); err == nil {
|
||||
t.Fatalf("accepted TTL %q", value)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
func TestCursorRejectsInvalidUUIDBeforeDatabaseAccess(t *testing.T) {
|
||||
const userID = "11111111-1111-4111-8111-111111111111"
|
||||
const chatID = "22222222-2222-4222-8222-222222222222"
|
||||
const messageID = "33333333-3333-4333-8333-333333333333"
|
||||
createdAt := time.Date(2026, 10, 10, 12, 0, 0, 0, time.UTC)
|
||||
for _, invalid := range []string{"not-a-uuid", "ffffffff-ffff-ffff-ffff-fffffffffffz"} {
|
||||
for _, field := range []string{"user_id", "chat_id"} {
|
||||
t.Run("chats/"+field+"/"+invalid, func(t *testing.T) {
|
||||
cursor := repositories.ChatCursor{UserID: userID, ChatID: chatID, CreatedAt: createdAt}
|
||||
if field == "user_id" {
|
||||
cursor.UserID = invalid
|
||||
} else {
|
||||
cursor.ChatID = invalid
|
||||
}
|
||||
token, err := services.EncodeChatCursor(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := services.DecodeChatCursor(token); err == nil {
|
||||
t.Fatal("invalid UUID accepted")
|
||||
}
|
||||
// A nil store proves that validation finishes before any SQL call.
|
||||
_, err = services.NewChatService(nil).List(context.Background(), userID, 20, token)
|
||||
var invalidInput services.InvalidInput
|
||||
if !errors.As(err, &invalidInput) {
|
||||
t.Fatalf("want InvalidInput, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
for _, field := range []string{"chat_id", "message_id"} {
|
||||
t.Run("messages/"+field+"/"+invalid, func(t *testing.T) {
|
||||
cursor := repositories.MessageCursor{ChatID: chatID, MessageID: messageID, CreatedAt: createdAt}
|
||||
if field == "chat_id" {
|
||||
cursor.ChatID = invalid
|
||||
} else {
|
||||
cursor.MessageID = invalid
|
||||
}
|
||||
token, err := services.EncodeMessageCursor(cursor)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := services.DecodeMessageCursor(token); err == nil {
|
||||
t.Fatal("invalid UUID accepted")
|
||||
}
|
||||
chat := repositories.Chat{ID: chatID, MemberIDs: []string{userID}}
|
||||
_, err = services.NewMessageService(nil).List(context.Background(), userID, chat, 20, token)
|
||||
var invalidInput services.InvalidInput
|
||||
if !errors.As(err, &invalidInput) {
|
||||
t.Fatalf("want InvalidInput, got %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatCursorPreservesPosition(t *testing.T) {
|
||||
original := repositories.ChatCursor{
|
||||
UserID: "11111111-1111-4111-8111-111111111111",
|
||||
ChatID: "22222222-2222-4222-8222-222222222222",
|
||||
CreatedAt: time.Date(2026, 10, 10, 12, 0, 0, 123000, time.UTC),
|
||||
}
|
||||
token, err := services.EncodeChatCursor(original)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := services.DecodeChatCursor(token)
|
||||
if err != nil || decoded != original {
|
||||
t.Fatalf("cursor changed: %+v, error: %v", decoded, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,144 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestDirectUploadCleanupSafetyAndReplay(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
fresh, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
expired, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
locked, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
ready, service := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
for _, u := range []string{fresh.ID, expired.ID, locked.ID, ready.ID} {
|
||||
record, err := store.GetDirect(ctx, u, chat, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = objects.Put(ctx, record.StagingKey, bytes.NewReader(data), int64(len(data)), "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
a, err := service.CompleteDirect(ctx, user, chat, ready.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=ANY($1::uuid[])`, []string{expired.ID, locked.ID}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
tx, err := f.db.Begin(ctx)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer tx.Rollback(ctx)
|
||||
if _, err = tx.Exec(ctx, `SELECT id FROM direct_uploads WHERE id=$1 FOR UPDATE`, locked.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
dry, err := store.CleanupDirectUploads(ctx, true, objects.Delete)
|
||||
if err != nil || dry.Eligible != 2 || dry.Deleted != 0 || dry.Skipped != 1 {
|
||||
t.Fatalf("dry: %+v %v", dry, err)
|
||||
}
|
||||
result, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 2 || result.Skipped != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
for _, id := range []string{fresh.ID, locked.ID} {
|
||||
u, _ := store.GetDirect(ctx, id, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); err != nil {
|
||||
t.Fatal("fresh/locked staging removed", err)
|
||||
}
|
||||
}
|
||||
for _, id := range []string{ready.ID, expired.ID} {
|
||||
u, _ := store.GetDirect(ctx, id, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); !repositories.IsObjectMissing(err) {
|
||||
t.Fatal("eligible staging remains", err)
|
||||
}
|
||||
}
|
||||
if _, err = objects.Stat(ctx, a.ObjectKey); err != nil {
|
||||
t.Fatal("ready original removed", err)
|
||||
}
|
||||
// A valid form can recreate staging after early cleanup. The receipt persists.
|
||||
if postDirect(t, ready.Upload, data, nil) != 204 {
|
||||
t.Fatal("replay failed")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, ready.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
again, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || again.Deleted != 1 {
|
||||
t.Fatalf("replay cleanup: %+v %v", again, err)
|
||||
}
|
||||
u, _ := store.GetDirect(ctx, ready.ID, chat, user)
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); !repositories.IsObjectMissing(err) {
|
||||
t.Fatal("replayed staging remains")
|
||||
}
|
||||
same, err := service.CompleteDirect(ctx, user, chat, ready.ID)
|
||||
if err != nil || same.ID != a.ID {
|
||||
t.Fatal("cleanup broke idempotent complete", err)
|
||||
}
|
||||
tx.Rollback(ctx)
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET purge_after=clock_timestamp()-interval '1 second',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, ready.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = store.CleanupDirectUploads(ctx, false, objects.Delete); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = objects.Stat(ctx, a.ObjectKey); err != nil {
|
||||
t.Fatal("receipt purge removed final", err)
|
||||
}
|
||||
if _, err = store.GetForMember(ctx, a.ID, user); err != nil {
|
||||
t.Fatal("receipt purge removed metadata", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadCleanupDependencyFailureAndMalformedKey(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
ctx := context.Background()
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
u, _ := store.GetDirect(ctx, result.ID, chat, user)
|
||||
if err := objects.Put(ctx, u.StagingKey, bytes.NewReader(data), int64(len(data)), "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '601 seconds',cleanup_after=clock_timestamp()-interval '1 second' WHERE id=$1`, result.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
failed, err := store.CleanupDirectUploads(ctx, false, func(context.Context, string) error { return errors.New("S3 unavailable") })
|
||||
if err == nil || failed.Failed != 1 {
|
||||
t.Fatal("failed DELETE was lost")
|
||||
}
|
||||
if _, err = objects.Stat(ctx, u.StagingKey); err != nil {
|
||||
t.Fatal("staging changed on failed DELETE", err)
|
||||
}
|
||||
cancelled, cancel := context.WithCancel(ctx)
|
||||
cancel()
|
||||
if _, err = store.CleanupDirectUploads(cancelled, false, objects.Delete); err == nil {
|
||||
t.Fatal("DB cancellation ignored")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET staging_key=$2 WHERE id=$1`, result.ID, "originals/"+u.AttachmentID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
called := false
|
||||
failed, err = store.CleanupDirectUploads(ctx, false, func(context.Context, string) error { called = true; return nil })
|
||||
if err == nil || failed.Failed != 1 || called {
|
||||
t.Fatal("malformed key reached S3 DELETE")
|
||||
}
|
||||
if _, err = f.db.Exec(ctx, `UPDATE direct_uploads SET staging_key=$2 WHERE id=$1`, result.ID, u.StagingKey); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
retried, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || retried.Deleted != 1 {
|
||||
t.Fatalf("retry: %+v %v", retried, err)
|
||||
}
|
||||
repeated, err := store.CleanupDirectUploads(ctx, false, objects.Delete)
|
||||
if err != nil || repeated.Deleted != 0 {
|
||||
t.Fatalf("repeat: %+v %v", repeated, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,116 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"net/http"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/lifecycle"
|
||||
)
|
||||
|
||||
func TestDirectUploadAccessPermissionsAndLifecycle(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
root := s3Client(t, true, false)
|
||||
app := s3Client(t, false, false)
|
||||
anon := s3Client(t, false, true)
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
key := "staging/direct/" + uuid.NewString()
|
||||
t.Cleanup(func() { root.RemoveObject(ctx, bucket, key, minio.RemoveObjectOptions{}) })
|
||||
if _, err := app.PutObject(ctx, bucket, key, bytes.NewReader([]byte("x")), 1, minio.PutObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
object, err := anon.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = object.Stat()
|
||||
object.Close()
|
||||
denied(t, err)
|
||||
_, err = app.PutObject(ctx, bucket, "staging/forbidden/"+uuid.NewString(), bytes.NewReader([]byte("x")), 1, minio.PutObjectOptions{})
|
||||
denied(t, err)
|
||||
config, err := root.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Add only our own harmless prefix rule, verify bootstrap merges rather than replaces.
|
||||
ruleID := "direct-test-" + uuid.NewString()
|
||||
config.Rules = append(config.Rules, lifecycle.Rule{ID: ruleID, Status: "Enabled", RuleFilter: lifecycle.Filter{Prefix: "test-lifecycle/" + ruleID + "/"}, Expiration: lifecycle.Expiration{Days: 7}})
|
||||
if err = root.SetBucketLifecycle(ctx, bucket, config); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
c, e := root.GetBucketLifecycle(ctx, bucket)
|
||||
if e != nil {
|
||||
t.Error(e)
|
||||
return
|
||||
}
|
||||
rules := []lifecycle.Rule{}
|
||||
for _, r := range c.Rules {
|
||||
if r.ID != ruleID {
|
||||
rules = append(rules, r)
|
||||
}
|
||||
}
|
||||
c.Rules = rules
|
||||
if e = root.SetBucketLifecycle(ctx, bucket, c); e != nil {
|
||||
t.Error(e)
|
||||
}
|
||||
})
|
||||
for i := 0; i < 2; i++ {
|
||||
if err = repositories.BootstrapS3(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
config, err = root.GetBucketLifecycle(ctx, bucket)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
managed, preserved := 0, false
|
||||
for _, r := range config.Rules {
|
||||
if r.ID == ruleID {
|
||||
preserved = true
|
||||
}
|
||||
if r.ID == "messenger-direct-staging-expiration" {
|
||||
managed++
|
||||
if r.RuleFilter.Prefix != "staging/direct/" || r.Expiration.Days != 1 || r.Status != "Enabled" {
|
||||
t.Fatal("unsafe staging lifecycle rule")
|
||||
}
|
||||
}
|
||||
}
|
||||
if managed != 1 || !preserved {
|
||||
t.Fatal("bootstrap is not an idempotent lifecycle merge")
|
||||
}
|
||||
denied(t, app.SetBucketLifecycle(ctx, bucket, config))
|
||||
}
|
||||
|
||||
func TestDirectUploadAccessCORS(t *testing.T) {
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("S3 integration")
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
origin string
|
||||
allowed bool
|
||||
}{{"http://127.0.0.1:5173", true}, {"http://localhost:5173", true}, {"https://untrusted.example", false}} {
|
||||
request, err := http.NewRequest("OPTIONS", os.Getenv("S3_ENDPOINT")+"/"+os.Getenv("S3_BUCKET"), nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Origin", sample.origin)
|
||||
request.Header.Set("Access-Control-Request-Method", "POST")
|
||||
request.Header.Set("Access-Control-Request-Headers", "content-type")
|
||||
response, err := http.DefaultClient.Do(request)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
response.Body.Close()
|
||||
allowed := response.Header.Get("Access-Control-Allow-Origin") == sample.origin
|
||||
if allowed != sample.allowed || response.Header.Get("Access-Control-Allow-Origin") == "*" {
|
||||
t.Fatalf("unexpected CORS allowance for %s", sample.origin)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,474 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/binary"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"hash/crc32"
|
||||
"image"
|
||||
"image/jpeg"
|
||||
"io"
|
||||
"mime/multipart"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func directInput(data []byte, mime string) services.DirectUploadInput {
|
||||
sum := sha256.Sum256(data)
|
||||
return services.DirectUploadInput{Filename: "direct.png", ContentType: mime, SizeBytes: int64(len(data)), SHA256: hex.EncodeToString(sum[:])}
|
||||
}
|
||||
|
||||
func beginDirect(t *testing.T, f *testFixture, store *repositories.AttachmentStore, objects *repositories.ObjectStore, user, chat string, data []byte, mime string) (services.DirectUploadResponse, *services.AttachmentService) {
|
||||
t.Helper()
|
||||
t.Setenv("S3_PUBLIC_ENDPOINT", os.Getenv("S3_ENDPOINT"))
|
||||
service := services.NewAttachmentService(store, objects)
|
||||
result, err := service.InitDirect(context.Background(), user, chat, directInput(data, mime))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rememberDirect(t, store, objects, result.ID, chat, user)
|
||||
return result, service
|
||||
}
|
||||
|
||||
func rememberDirect(t *testing.T, store *repositories.AttachmentStore, objects *repositories.ObjectStore, id, chat, user string) {
|
||||
t.Helper()
|
||||
u, err := store.GetDirect(context.Background(), id, chat, user)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
objects.Delete(context.Background(), u.StagingKey)
|
||||
objects.Delete(context.Background(), u.FinalKey)
|
||||
})
|
||||
}
|
||||
|
||||
func postDirect(t *testing.T, p repositories.PostUpload, data []byte, change func(map[string]string)) int {
|
||||
t.Helper()
|
||||
fields := map[string]string{}
|
||||
for k, v := range p.Fields {
|
||||
fields[k] = v
|
||||
}
|
||||
if change != nil {
|
||||
change(fields)
|
||||
}
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
for k, v := range fields {
|
||||
if err := writer.WriteField(k, v); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
file, err := writer.CreateFormFile("file", "image.png")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = file.Write(data); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err = writer.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req, err := http.NewRequest("POST", p.URL, &body)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Content-Type", writer.FormDataContentType())
|
||||
client := http.Client{Timeout: 10 * time.Second}
|
||||
response, err := client.Do(req)
|
||||
if err != nil {
|
||||
t.Fatal("S3 form request failed")
|
||||
}
|
||||
defer response.Body.Close()
|
||||
io.Copy(io.Discard, response.Body)
|
||||
return response.StatusCode
|
||||
}
|
||||
|
||||
func requireDirectError(t *testing.T, err error, status int, code string) {
|
||||
t.Helper()
|
||||
var e repositories.UploadError
|
||||
if !errors.As(err, &e) || e.Status != status || e.Code != code {
|
||||
t.Fatalf("expected %d %s, got %v", status, code, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadFlow(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := append(uploadPNG(t), []byte("aaaa")...)
|
||||
var jpg bytes.Buffer
|
||||
if err := jpeg.Encode(&jpg, image.NewRGBA(image.Rect(0, 0, 8, 8)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime string
|
||||
}{{png, "image/png"}, {jpg.Bytes(), "image/jpeg"}} {
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, sample.data, sample.mime)
|
||||
if result.ExpiresAt.Sub(time.Now()) > 121*time.Second || result.CompleteBefore.Sub(result.ExpiresAt) < 599*time.Second {
|
||||
t.Fatal("wrong upload deadlines")
|
||||
}
|
||||
if status := postDirect(t, result.Upload, sample.data, nil); status != 204 {
|
||||
t.Fatalf("POST: %d", status)
|
||||
}
|
||||
a, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
router := attachmentRouter(f, objects)
|
||||
content := attachmentCall(t, f, router, "GET", "/api/v1/attachments/"+a.ID+"/content", user, "", nil, 200)
|
||||
if !bytes.Equal(content.Body.Bytes(), sample.data) {
|
||||
t.Fatal("original bytes changed")
|
||||
}
|
||||
message := []byte(`{"text":"","attachment_ids":["` + a.ID + `"]}`)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", message, 201)
|
||||
// Lost success response is represented by ignoring a, then asking another service.
|
||||
again, err := services.NewAttachmentService(store, objects).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err != nil || again.ID != a.ID || !again.CreatedAt.Equal(a.CreatedAt) {
|
||||
t.Fatal("complete is not idempotent", err)
|
||||
}
|
||||
replacement := append([]byte(nil), sample.data...)
|
||||
replacement[len(replacement)-1] ^= 1
|
||||
if status := postDirect(t, result.Upload, replacement, nil); status != 204 {
|
||||
t.Fatal("expected valid staging replay")
|
||||
}
|
||||
final, err := objects.Get(context.Background(), a.ObjectKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
original, err := io.ReadAll(final)
|
||||
final.Close()
|
||||
if err != nil || !bytes.Equal(original, sample.data) {
|
||||
t.Fatal("staging replay modified final")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadValidationHTTP(t *testing.T) {
|
||||
f, _, objects, user, chat := uploadFixture(t)
|
||||
t.Setenv("S3_PUBLIC_ENDPOINT", os.Getenv("S3_ENDPOINT"))
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachment-uploads"
|
||||
input := directInput(uploadPNG(t), "image/png")
|
||||
valid, _ := json.Marshal(input)
|
||||
for _, sample := range []struct {
|
||||
body []byte
|
||||
status int
|
||||
}{
|
||||
{[]byte(`null`), 400}, {[]byte(`{}`), 400}, {append(append([]byte(nil), valid...), []byte(` {}`)...), 400},
|
||||
{[]byte(strings.Repeat("x", 4097)), 400}, {[]byte(`{"filename":"x","size_bytes":1.5}`), 400},
|
||||
} {
|
||||
attachmentCall(t, f, router, "POST", path, user, "application/json", sample.body, sample.status)
|
||||
}
|
||||
for _, sample := range []struct {
|
||||
mutate func(*services.DirectUploadInput)
|
||||
status int
|
||||
}{
|
||||
{func(i *services.DirectUploadInput) { i.Filename = "../image.png" }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SHA256 = "invalid" }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SizeBytes = 0 }, 400},
|
||||
{func(i *services.DirectUploadInput) { i.SizeBytes = 10485761 }, 413},
|
||||
{func(i *services.DirectUploadInput) { i.ContentType = "text/plain" }, 415},
|
||||
} {
|
||||
i := input
|
||||
sample.mutate(&i)
|
||||
body, _ := json.Marshal(i)
|
||||
attachmentCall(t, f, router, "POST", path, user, "application/json", body, sample.status)
|
||||
}
|
||||
store := repositories.NewAttachmentStore(f.db)
|
||||
w := attachmentCall(t, f, router, "POST", path, user, "application/json", valid, 201)
|
||||
var result services.DirectUploadResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rememberDirect(t, store, objects, result.ID, chat, user)
|
||||
if status := postDirect(t, result.Upload, uploadPNG(t), nil); status != 204 {
|
||||
t.Fatal("HTTP-created form failed")
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", user, "", nil, 201)
|
||||
}
|
||||
|
||||
func TestDirectUploadValidationContents(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := uploadPNG(t)
|
||||
huge := append([]byte(nil), png...)
|
||||
binary.BigEndian.PutUint32(huge[16:20], 10001)
|
||||
binary.BigEndian.PutUint32(huge[20:24], 4000)
|
||||
binary.BigEndian.PutUint32(huge[29:33], crc32.ChecksumIEEE(huge[12:29]))
|
||||
for _, sample := range []struct {
|
||||
data []byte
|
||||
mime, code string
|
||||
status int
|
||||
}{
|
||||
{png[:40], "image/png", "unsupported_media_type", 415},
|
||||
{png, "image/jpeg", "unsupported_media_type", 415},
|
||||
{huge, "image/png", "payload_too_large", 413},
|
||||
} {
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, sample.data, sample.mime)
|
||||
if postDirect(t, result.Upload, sample.data, nil) != 204 {
|
||||
t.Fatal("S3 refused matching declared policy")
|
||||
}
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, sample.status, sample.code)
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, sample.status, sample.code)
|
||||
}
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
wrong := append([]byte(nil), png...)
|
||||
wrong[len(wrong)-1] ^= 1
|
||||
if postDirect(t, result.Upload, wrong, nil) != 204 {
|
||||
t.Fatal("form POST failed")
|
||||
}
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 400, "upload_checksum_mismatch")
|
||||
result, service = beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
if postDirect(t, result.Upload, append(png, 1), nil) < 400 {
|
||||
t.Fatal("S3 did not enforce exact policy size")
|
||||
}
|
||||
u, _ := store.GetDirect(context.Background(), result.ID, chat, user)
|
||||
// Simulate a trusted-side fault that bypassed policy; complete must still check.
|
||||
if err = objects.Put(context.Background(), u.StagingKey, bytes.NewReader(png[:40]), 40, "image/png"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 400, "upload_size_mismatch")
|
||||
}
|
||||
|
||||
func TestDirectUploadAccess(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
png := uploadPNG(t)
|
||||
result, service := beginDirect(t, f, store, objects, user, chat, png, "image/png")
|
||||
eve, _ := createTestUser(t, f.users, "eve", "Eve")
|
||||
bob, _ := createTestUser(t, f.users, "bob", "Bob")
|
||||
if _, err := f.db.Exec(context.Background(), `INSERT INTO chat_members(chat_id,user_id) VALUES($1,$2)`, chat, bob.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
other := createTestChat(t, f.chats, "other", []string{user})
|
||||
router := attachmentRouter(f, objects)
|
||||
path := "/api/v1/chats/" + chat + "/attachment-uploads"
|
||||
for _, actor := range []string{eve.ID, bob.ID} {
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", actor, "", nil, 404)
|
||||
}
|
||||
attachmentCall(t, f, router, "POST", path+"/"+result.ID+"/complete", "", "", nil, 401)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+other.ID+"/attachment-uploads/"+result.ID+"/complete", user, "", nil, 404)
|
||||
body, _ := json.Marshal(directInput(png, "image/png"))
|
||||
attachmentCall(t, f, router, "POST", path, eve.ID, "application/json", body, 404)
|
||||
u, _ := store.GetDirect(context.Background(), result.ID, chat, user)
|
||||
attachmentCall(t, f, router, "POST", "/api/v1/chats/"+chat+"/messages", user, "application/json", []byte(`{"text":"","attachment_ids":["`+u.AttachmentID+`"]}`), 404)
|
||||
_, err := service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 409, "upload_not_uploaded")
|
||||
for _, change := range []func(map[string]string){
|
||||
func(fields map[string]string) { fields["key"] = "originals/" + uuid.NewString() },
|
||||
func(fields map[string]string) { fields["Content-Type"] = "image/jpeg" },
|
||||
func(fields map[string]string) { fields["x-amz-signature"] = strings.Repeat("0", 64) },
|
||||
} {
|
||||
if postDirect(t, result.Upload, png, change) < 400 {
|
||||
t.Fatal("S3 accepted tampered form")
|
||||
}
|
||||
}
|
||||
expired, err := objects.PresignDirect(context.Background(), u.StagingKey, "image/png", int64(len(png)), time.Now().Add(-time.Minute))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if postDirect(t, expired, png, nil) < 400 {
|
||||
t.Fatal("S3 accepted expired form")
|
||||
}
|
||||
if _, err = f.db.Exec(context.Background(), `UPDATE direct_uploads SET complete_before=clock_timestamp()-interval '1 second' WHERE id=$1`, result.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = service.CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
requireDirectError(t, err, 409, "upload_expired")
|
||||
}
|
||||
|
||||
type directFault struct {
|
||||
*repositories.ObjectStore
|
||||
afterCopy func() error
|
||||
failGet bool
|
||||
}
|
||||
|
||||
func (f directFault) Copy(ctx context.Context, source, dest, etag string) error {
|
||||
if err := f.ObjectStore.Copy(ctx, source, dest, etag); err != nil {
|
||||
return err
|
||||
}
|
||||
if f.afterCopy != nil {
|
||||
return f.afterCopy()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
func (f directFault) Get(ctx context.Context, key string) (io.ReadCloser, error) {
|
||||
if f.failGet {
|
||||
return nil, errors.New("injected GET failure")
|
||||
}
|
||||
return f.ObjectStore.Get(ctx, key)
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryConcurrentComplete(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
var wg sync.WaitGroup
|
||||
ids := make(chan string, 2)
|
||||
errs := make(chan error, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
a, err := services.NewAttachmentService(store, objects).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
ids <- a.ID
|
||||
errs <- err
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
close(ids)
|
||||
close(errs)
|
||||
for err := range errs {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
first := <-ids
|
||||
if second := <-ids; first == "" || first != second {
|
||||
t.Fatal("concurrent complete produced different attachments")
|
||||
}
|
||||
var n int
|
||||
if err := f.db.QueryRow(context.Background(), `SELECT count(*) FROM attachments`).Scan(&n); err != nil || n != 1 {
|
||||
t.Fatal("unexpected metadata count", n, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryFailures(t *testing.T) {
|
||||
for _, name := range []string{"copy-result-loss", "get-failure", "metadata-rejection", "database-disconnect"} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, normal := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
fault := directFault{ObjectStore: objects}
|
||||
switch name {
|
||||
case "copy-result-loss":
|
||||
fault.afterCopy = func() error { return errors.New("COPY acknowledgement lost") }
|
||||
case "get-failure":
|
||||
fault.failGet = true
|
||||
case "metadata-rejection":
|
||||
if _, err := f.db.Exec(context.Background(), `ALTER TABLE attachments ADD CONSTRAINT reject_direct CHECK (filename <> 'direct.png')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
case "database-disconnect":
|
||||
fault.afterCopy = func() error {
|
||||
_, err := f.db.Exec(context.Background(), `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name=$1 AND state='idle in transaction' AND pid<>pg_backend_pid()`, f.db.Config().ConnConfig.RuntimeParams["application_name"])
|
||||
return err
|
||||
}
|
||||
}
|
||||
_, err := services.NewAttachmentService(store, fault).CompleteDirect(context.Background(), user, chat, result.ID)
|
||||
if err == nil {
|
||||
t.Fatal("expected injected failure")
|
||||
}
|
||||
var count int
|
||||
if err = f.db.QueryRow(context.Background(), `SELECT count(*) FROM upload_attempts`).Scan(&count); err != nil || count != 1 {
|
||||
t.Fatal("durable journal missing", err)
|
||||
}
|
||||
if err = f.db.QueryRow(context.Background(), `SELECT count(*) FROM attachments`).Scan(&count); err != nil || count != 0 {
|
||||
t.Fatal("published incomplete attachment", err)
|
||||
}
|
||||
if name == "metadata-rejection" {
|
||||
if _, err = f.db.Exec(context.Background(), `ALTER TABLE attachments DROP CONSTRAINT reject_direct`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if _, err = normal.CompleteDirect(context.Background(), user, chat, result.ID); err != nil {
|
||||
t.Fatal("retry failed", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoveryProcessExit(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
child := exec.Command(os.Args[0], "-test.run=^TestDirectUploadCrashHelper$")
|
||||
child.Env = append(os.Environ(), "DIRECT_CRASH_HELPER=1", "UPLOAD_TEST_DATABASE="+f.db.Config().ConnString(), "UPLOAD_TEST_SCHEMA="+f.db.Config().ConnConfig.RuntimeParams["search_path"], "UPLOAD_TEST_USER="+user, "UPLOAD_TEST_CHAT="+chat, "DIRECT_UPLOAD_ID="+result.ID)
|
||||
output, err := child.CombinedOutput()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 23 {
|
||||
t.Fatalf("helper did not crash after COPY: %v %s", err, output)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
|
||||
func TestDirectUploadCrashHelper(t *testing.T) {
|
||||
if os.Getenv("DIRECT_CRASH_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(os.Getenv("UPLOAD_TEST_DATABASE"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
cfg.ConnConfig.RuntimeParams["application_name"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
objects := uploadObjects(t)
|
||||
fault := directFault{ObjectStore: objects, afterCopy: func() error { os.Exit(23); return nil }}
|
||||
_, err = services.NewAttachmentService(repositories.NewAttachmentStore(db), fault).CompleteDirect(context.Background(), os.Getenv("UPLOAD_TEST_USER"), os.Getenv("UPLOAD_TEST_CHAT"), os.Getenv("DIRECT_UPLOAD_ID"))
|
||||
t.Fatalf("helper failed before exit: %v", err)
|
||||
}
|
||||
|
||||
func TestDirectUploadRecoverySharedSlotAndCancellation(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
data := uploadPNG(t)
|
||||
result, _ := beginDirect(t, f, store, objects, user, chat, data, "image/png")
|
||||
if postDirect(t, result.Upload, data, nil) != 204 {
|
||||
t.Fatal("POST failed")
|
||||
}
|
||||
entered, release := make(chan struct{}), make(chan struct{})
|
||||
fault := directFault{ObjectStore: objects, afterCopy: func() error {
|
||||
close(entered)
|
||||
<-release
|
||||
return errors.New("injected operation cancellation")
|
||||
}}
|
||||
service := services.NewAttachmentService(store, fault)
|
||||
done := make(chan error, 1)
|
||||
go func() { _, err := service.CompleteDirect(context.Background(), user, chat, result.ID); done <- err }()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(5 * time.Second):
|
||||
t.Fatal("COPY not reached")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, proxyErr := service.Upload(ctx, user, chat, "waiting.png", "image/png", bytes.NewReader(data))
|
||||
_, directErr := service.CompleteDirect(ctx, user, chat, result.ID)
|
||||
close(release)
|
||||
if err := <-done; err == nil {
|
||||
t.Fatal("injected failure ignored")
|
||||
}
|
||||
if !errors.Is(proxyErr, context.Canceled) || !errors.Is(directErr, context.Canceled) {
|
||||
t.Fatalf("waiting requests ignored cancellation: proxy=%v direct=%v", proxyErr, directErr)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
@@ -0,0 +1,291 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
func TestConcurrentRegistrationSameUsername(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
const body = `{"username":"alice","display_name":"Алиса","password":"lab2-test-password"}`
|
||||
var wg sync.WaitGroup
|
||||
start := make(chan struct{})
|
||||
results := make(chan int, 2)
|
||||
for i := 0; i < 2; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
<-start
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/users", strings.NewReader(body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
results <- w.Code
|
||||
}()
|
||||
}
|
||||
close(start)
|
||||
wg.Wait()
|
||||
close(results)
|
||||
counts := map[int]int{}
|
||||
for status := range results {
|
||||
counts[status]++
|
||||
}
|
||||
if counts[201] != 1 || counts[409] != 1 {
|
||||
t.Fatalf("registration statuses: %v", counts)
|
||||
}
|
||||
var users int
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM users WHERE username='alice'").Scan(&users); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if users != 1 {
|
||||
t.Fatalf("stored %d users with the same username", users)
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatTransactionRollsBackAfterMemberInsertFailure(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
alice, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
bob, _ := createTestUser(t, f.users, "bob", "Боб")
|
||||
_, err := f.db.Exec(context.Background(), `CREATE FUNCTION reject_bob() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN
|
||||
IF NEW.user_id = '`+bob.ID+`'::uuid THEN RAISE EXCEPTION 'forced member failure'; END IF;
|
||||
RETURN NEW;
|
||||
END $$;
|
||||
CREATE TRIGGER reject_bob_insert BEFORE INSERT ON chat_members FOR EACH ROW EXECUTE FUNCTION reject_bob();`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.chats.Create(context.Background(), "rollback-check", []string{alice.ID, bob.ID}); err == nil {
|
||||
t.Fatal("expected second member insertion to fail")
|
||||
}
|
||||
var chats, members int
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chats WHERE title='rollback-check'").Scan(&chats); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chat_members").Scan(&members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 0 || members != 0 {
|
||||
t.Fatalf("transaction left chats=%d members=%d", chats, members)
|
||||
}
|
||||
}
|
||||
|
||||
func TestShortSessionTTLAndLogout(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
t.Setenv("SESSION_TTL_SECONDS", "2")
|
||||
ttl, err := services.SessionTTL()
|
||||
if err != nil || ttl != 2*time.Second {
|
||||
t.Fatalf("short TTL: %v, %v", ttl, err)
|
||||
}
|
||||
sessions := services.NewSessionService(services.NewUserService(f.users), f.sessionStore, ttl)
|
||||
router := handlers.NewRouter(nil, services.NewUserService(f.users), sessions, nil, nil)
|
||||
assertHTTPUser := func(token string, want int) {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+token)
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, r)
|
||||
if w.Code != want {
|
||||
t.Fatalf("short TTL HTTP status: got %d, want %d", w.Code, want)
|
||||
}
|
||||
t.Logf("GET /api/v1/users/me: HTTP %d", w.Code)
|
||||
}
|
||||
token, _, err := sessions.CreateForUser(context.Background(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := sessions.User(context.Background(), token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertHTTPUser(token, 200)
|
||||
expiryStarted := time.Now()
|
||||
expiryDeadline := expiryStarted.Add(5 * time.Second)
|
||||
for {
|
||||
_, err := sessions.User(context.Background(), token)
|
||||
if errors.Is(err, services.ErrInvalidSession) {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("checking expiry: %v", err)
|
||||
}
|
||||
if time.Now().After(expiryDeadline) {
|
||||
t.Fatal("session with TTL=2 seconds did not expire within 5 seconds")
|
||||
}
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
}
|
||||
t.Logf("SESSION_TTL_SECONDS=2: expiry observed after %s", time.Since(expiryStarted))
|
||||
assertHTTPUser(token, 401)
|
||||
token, _, err = f.sessions.CreateForUser(context.Background(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.sessions.Revoke(context.Background(), token); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.sessions.User(context.Background(), token); err != services.ErrInvalidSession {
|
||||
t.Fatalf("revoked token: %v", err)
|
||||
}
|
||||
assertHTTPUser(token, 401)
|
||||
}
|
||||
|
||||
func TestWrongPasswordAndLogoutHTTP(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
createTestUser(t, f.users, "alice", "Алиса")
|
||||
login := func(password string) *httptest.ResponseRecorder {
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", nil)
|
||||
r.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("alice:"+password)))
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
if w := login("wrong-password"); w.Code != 401 {
|
||||
t.Fatalf("wrong password returned %d", w.Code)
|
||||
}
|
||||
missingUser := httptest.NewRequest(http.MethodPost, "/api/v1/auth/sessions", nil)
|
||||
missingUser.SetBasicAuth("missing_user", "lab2-test-password")
|
||||
missingUserResponse := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(missingUserResponse, missingUser)
|
||||
if missingUserResponse.Code != 401 {
|
||||
t.Fatalf("unknown username returned %d", missingUserResponse.Code)
|
||||
}
|
||||
w := login("lab2-test-password")
|
||||
if w.Code != 201 {
|
||||
t.Fatalf("valid login returned %d: %s", w.Code, w.Body.String())
|
||||
}
|
||||
var result struct {
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodGet, "/api/v1/chats/not-a-uuid/messages", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+result.Token)
|
||||
w = httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 400 {
|
||||
t.Fatalf("invalid chat UUID returned %d", w.Code)
|
||||
}
|
||||
r = httptest.NewRequest(http.MethodDelete, "/api/v1/auth/sessions/current", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+result.Token)
|
||||
w = httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 204 {
|
||||
t.Fatalf("logout returned %d", w.Code)
|
||||
}
|
||||
r = httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
r.Header.Set("Authorization", "Bearer "+result.Token)
|
||||
w = httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != 401 {
|
||||
t.Fatalf("revoked token returned %d", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDependencyFailuresReturnUnavailable(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
failedRedis := repositories.NewRedisClient("127.0.0.1:1")
|
||||
defer failedRedis.Close()
|
||||
failedSessions := repositories.NewSessionStore(failedRedis, "unreachable:")
|
||||
redisRouter := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(f.db), failedSessions), services.NewUserService(f.users), services.NewSessionService(services.NewUserService(f.users), failedSessions, time.Hour), services.NewChatService(f.chats), services.NewMessageService(f.messages))
|
||||
assertStatus := func(router http.Handler, method, path, auth string, want int) {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(method, path, nil)
|
||||
if auth != "" {
|
||||
r.Header.Set("Authorization", auth)
|
||||
}
|
||||
w := httptest.NewRecorder()
|
||||
router.ServeHTTP(w, r)
|
||||
if w.Code != want {
|
||||
t.Fatalf("%s %s returned %d, want %d: %s", method, path, w.Code, want, w.Body.String())
|
||||
}
|
||||
}
|
||||
assertStatus(redisRouter, "GET", "/health/ready", "", 503)
|
||||
assertStatus(redisRouter, "GET", "/api/v1/users/me", "Bearer opaque-token", 503)
|
||||
assertStatus(redisRouter, "POST", "/api/v1/auth/sessions", "Basic "+base64.StdEncoding.EncodeToString([]byte("alice:lab2-test-password")), 503)
|
||||
|
||||
pool, err := pgxpool.New(context.Background(), os.Getenv("TEST_DATABASE_URL"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
pool.Close()
|
||||
userStore := repositories.NewUserStore(pool)
|
||||
dbRouter := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(pool), f.sessionStore), services.NewUserService(userStore), services.NewSessionService(services.NewUserService(userStore), f.sessionStore, time.Hour), services.NewChatService(repositories.NewChatStore(pool)), services.NewMessageService(repositories.NewMessageStore(pool)))
|
||||
token, _, err := f.sessions.CreateForUser(context.Background(), user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
assertStatus(dbRouter, "GET", "/health/ready", "", 503)
|
||||
assertStatus(dbRouter, "GET", "/api/v1/users/me", "Bearer "+token, 503)
|
||||
assertStatus(dbRouter, "GET", "/api/v1/users/me", "", 401)
|
||||
}
|
||||
|
||||
func TestHistoryQueryPlanAtTenThousandMessages(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
chat := createTestChat(t, f.chats, "plan-check", []string{user.ID})
|
||||
ctx := context.Background()
|
||||
_, err := f.db.Exec(ctx, `INSERT INTO messages(id,chat_id,sender_id,text,created_at)
|
||||
SELECT gen_random_uuid(),$1,$2,'plan message','2026-01-01'::timestamptz + n * interval '1 microsecond'
|
||||
FROM generate_series(1,10000) AS n`, chat.ID, user.ID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.db.Exec(ctx, "ANALYZE messages"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var lateTime time.Time
|
||||
var lateID string
|
||||
if err := f.db.QueryRow(ctx, "SELECT created_at,id FROM messages WHERE chat_id=$1 ORDER BY created_at,id OFFSET 9000 LIMIT 1", chat.ID).Scan(&lateTime, &lateID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cases := []struct {
|
||||
name string
|
||||
at time.Time
|
||||
id string
|
||||
}{
|
||||
{"first", time.Time{}, "00000000-0000-0000-0000-000000000000"},
|
||||
{"late", lateTime, lateID},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var plan []byte
|
||||
err := f.db.QueryRow(ctx, `EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
|
||||
SELECT id,chat_id,sender_id,text,created_at FROM messages
|
||||
WHERE chat_id=$1 AND (created_at,id)>($2,$3::uuid)
|
||||
ORDER BY created_at,id LIMIT $4`, chat.ID, tc.at, tc.id, 51).Scan(&plan)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.Contains(plan, []byte("messages_history_idx")) || bytes.Contains(plan, []byte(`"Node Type": "Seq Scan"`)) {
|
||||
t.Fatalf("history page did not use the index: %s", plan)
|
||||
}
|
||||
t.Logf("%s page EXPLAIN (ANALYZE, BUFFERS): %s", tc.name, plan)
|
||||
})
|
||||
}
|
||||
var count int
|
||||
if err := f.db.QueryRow(ctx, "SELECT count(*) FROM messages WHERE chat_id=$1", chat.ID).Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count < 10000 {
|
||||
t.Fatal(fmt.Errorf("history contains only %d messages", count))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"reflect"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
)
|
||||
|
||||
func TestIdempotencyConcurrentStoresAndScopes(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
alice, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
bob, _ := createTestUser(t, f.users, "bob", "Bob")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{alice.ID, bob.ID})
|
||||
otherChat := createTestChat(t, f.chats, "Other", []string{alice.ID})
|
||||
stores := []*repositories.MessageStore{f.messages, repositories.NewMessageStore(f.db)}
|
||||
const concurrency = 24
|
||||
start := make(chan struct{})
|
||||
results := make(chan repositories.Message, concurrency)
|
||||
errorsFound := make(chan error, concurrency)
|
||||
var group sync.WaitGroup
|
||||
for i := 0; i < concurrency; i++ {
|
||||
group.Add(1)
|
||||
go func(i int) {
|
||||
defer group.Done()
|
||||
<-start
|
||||
message, err := stores[i%len(stores)].CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "same-key")
|
||||
results <- message
|
||||
errorsFound <- err
|
||||
}(i)
|
||||
}
|
||||
close(start)
|
||||
group.Wait()
|
||||
close(results)
|
||||
close(errorsFound)
|
||||
for err := range errorsFound {
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
var original repositories.Message
|
||||
for message := range results {
|
||||
if original.ID == "" {
|
||||
original = message
|
||||
} else if !reflect.DeepEqual(message, original) {
|
||||
t.Fatalf("replay changed the message: %v != %v", message, original)
|
||||
}
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 1 {
|
||||
t.Fatal("concurrent repeat created duplicate messages")
|
||||
}
|
||||
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "different", "same-key"); !errors.Is(err, repositories.ErrIdempotencyConflict) {
|
||||
t.Fatalf("different body: got %v", err)
|
||||
}
|
||||
for _, scope := range [][2]string{{chat.ID, bob.ID}, {otherChat.ID, alice.ID}} {
|
||||
message, err := f.messages.CreateIdempotent(context.Background(), scope[0], scope[1], "hello", "same-key")
|
||||
if err != nil || message.ID == original.ID {
|
||||
t.Fatalf("key leaked across scopes: %v", err)
|
||||
}
|
||||
}
|
||||
if _, err := f.db.Exec(context.Background(), "UPDATE message_idempotency SET expires_at=now()-interval '1 second' WHERE message_id=$1", original.ID); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
reused, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "after expiry", "same-key")
|
||||
if err != nil || reused.ID == original.ID {
|
||||
t.Fatalf("expired key cannot be reused: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIdempotencyRecordFailureRollsBackMessage(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
alice, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{alice.ID})
|
||||
// Force a failure AFTER the message insert to verify transaction atomicity.
|
||||
_, err := f.db.Exec(context.Background(), `
|
||||
CREATE FUNCTION reject_replay() RETURNS trigger LANGUAGE plpgsql AS $$
|
||||
BEGIN RAISE EXCEPTION 'synthetic replay failure'; END $$;
|
||||
CREATE TRIGGER reject_replay BEFORE INSERT ON message_idempotency
|
||||
FOR EACH ROW EXECUTE FUNCTION reject_replay()`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "retry-key"); err == nil {
|
||||
t.Fatal("synthetic failure did not happen")
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
||||
t.Fatal("failed transaction left a message behind")
|
||||
}
|
||||
if _, err := f.db.Exec(context.Background(), "DROP TRIGGER reject_replay ON message_idempotency"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := f.messages.CreateIdempotent(context.Background(), chat.ID, alice.ID, "hello", "retry-key"); err != nil {
|
||||
t.Fatalf("failure was cached instead of permitting retry: %v", err)
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 1 {
|
||||
t.Fatal("retry did not produce exactly one message")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestMessengerWorkflow(t *testing.T) {
|
||||
router := newTestFixture(t).router
|
||||
|
||||
call := func(method, path, body, auth string, expected int) map[string]any {
|
||||
t.Helper()
|
||||
request := httptest.NewRequest(method, path, bytes.NewBufferString(body))
|
||||
if body != "" {
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if method == http.MethodPost && strings.HasSuffix(path, "/messages") {
|
||||
request.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
}
|
||||
if auth != "" {
|
||||
request.Header.Set("Authorization", auth)
|
||||
}
|
||||
response := httptest.NewRecorder()
|
||||
router.ServeHTTP(response, request)
|
||||
if response.Code != expected {
|
||||
t.Fatalf("%s %s: status %d, want %d: %s", method, path, response.Code, expected, response.Body.String())
|
||||
}
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatalf("%s %s: missing request ID", method, path)
|
||||
}
|
||||
if response.Code == http.StatusNoContent {
|
||||
return nil
|
||||
}
|
||||
var result map[string]any
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &result); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
user := call("POST", "/api/v1/users", `{"username":"alice","display_name":"Алиса","password":"lab2-test-password"}`, "", 201)
|
||||
userID := user["id"].(string)
|
||||
basic := "Basic " + base64.StdEncoding.EncodeToString([]byte("alice:lab2-test-password"))
|
||||
session := call("POST", "/api/v1/auth/sessions", "", basic, 201)
|
||||
bearer := "Bearer " + session["token"].(string)
|
||||
call("GET", "/api/v1/users/me", "", bearer, 200)
|
||||
chat := call("POST", "/api/v1/chats", `{"title":"Тестовый чат","member_ids":["`+userID+`"]}`, bearer, 201)
|
||||
chatID := chat["id"].(string)
|
||||
path := "/api/v1/chats/" + chatID + "/messages"
|
||||
for i := 0; i < 3; i++ {
|
||||
call("POST", path, `{"text":"message"}`, bearer, 201)
|
||||
}
|
||||
page1 := call("GET", path+"?limit=2", "", bearer, 200)
|
||||
first := page1["items"].([]any)
|
||||
if len(first) != 2 || page1["next_cursor"] == nil {
|
||||
t.Fatalf("unexpected first page: %v", page1)
|
||||
}
|
||||
page2 := call("GET", path+"?limit=2&cursor="+url.QueryEscape(page1["next_cursor"].(string)), "", bearer, 200)
|
||||
second := page2["items"].([]any)
|
||||
if len(second) != 1 || page2["next_cursor"] != nil || first[0].(map[string]any)["id"] == second[0].(map[string]any)["id"] {
|
||||
t.Fatalf("unexpected second page: %v", page2)
|
||||
}
|
||||
call("DELETE", "/api/v1/auth/sessions/current", "", bearer, 204)
|
||||
call("GET", "/api/v1/users/me", "", bearer, 401)
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestDecodeJSONBodyRequiresExactFieldNames(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
user, _ := createTestUser(t, fixture.users, "json_tester", "JSON tester")
|
||||
chat := createTestChat(t, fixture.chats, "JSON body tests", []string{user.ID})
|
||||
path := "/api/v1/chats/" + chat.ID + "/messages"
|
||||
for _, test := range []struct {
|
||||
name string
|
||||
body string
|
||||
valid bool
|
||||
}{
|
||||
{"canonical", `{"text":"first","attachment_ids":[]}`, true},
|
||||
{"escaped canonical name", `{"te\u0078t":"first"}`, true},
|
||||
{"uppercase extra", `{"text":"first","TEXT":"second"}`, false},
|
||||
{"uppercase extra first", `{"TEXT":"second","text":"first"}`, false},
|
||||
{"mixed case", `{"Text":"second"}`, false},
|
||||
{"uppercase attachments", `{"text":"first","ATTACHMENT_IDS":[]}`, false},
|
||||
{"escaped uppercase", `{"text":"first","\u0054EXT":"second"}`, false},
|
||||
{"unknown field", `{"text":"first","sender_id":"other"}`, false},
|
||||
{"wrong type", `{"text":123}`, false},
|
||||
{"null", `null`, false},
|
||||
{"array", `[]`, false},
|
||||
{"trailing object", `{"text":"first"}{}`, false},
|
||||
} {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
request := httptest.NewRequest(http.MethodPost, path, strings.NewReader(test.body))
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
request.Header.Set("Idempotency-Key", uuid.NewString())
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
response := httptest.NewRecorder()
|
||||
fixture.router.ServeHTTP(response, request)
|
||||
want := http.StatusBadRequest
|
||||
if test.valid {
|
||||
want = http.StatusCreated
|
||||
}
|
||||
if response.Code != want {
|
||||
t.Fatalf("want %d, got %d: %s", want, response.Code, response.Body.String())
|
||||
}
|
||||
if test.valid {
|
||||
var value map[string]any
|
||||
if err := json.Unmarshal(response.Body.Bytes(), &value); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if value["text"] != "first" {
|
||||
t.Fatal("text changed")
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,276 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"net/url"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateMessageBlankText(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
user, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected user to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Тестовый чат",
|
||||
[]string{user.ID},
|
||||
)
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/chats/"+chat.ID+"/messages",
|
||||
strings.NewReader(`{"text":" \t\n"}`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
authorizeTestRequest(t, fixture, request, user.ID)
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusBadRequest,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
response.Body.String(),
|
||||
`"code":"invalid_request"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected invalid_request error code, got body: %s",
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
messages := listTestMessages(t, messageStore, chat.ID)
|
||||
|
||||
if len(messages) != 0 {
|
||||
t.Fatalf(
|
||||
"expected no messages to be stored, got %d",
|
||||
len(messages),
|
||||
)
|
||||
}
|
||||
}
|
||||
func TestCreateMessageFromChatOutsider(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
alice, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected Alice to be created")
|
||||
}
|
||||
|
||||
bob, created := createTestUser(t, userStore, "bob", "Боб")
|
||||
if !created {
|
||||
t.Fatal("expected Bob to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Чат Алисы",
|
||||
[]string{alice.ID},
|
||||
)
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/chats/"+chat.ID+"/messages",
|
||||
strings.NewReader(`{"text":"Сообщение чужому чату"}`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
authorizeTestRequest(t, fixture, request, bob.ID)
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusNotFound {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusNotFound,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
response.Body.String(),
|
||||
`"code":"not_found"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected not_found error code, got body: %s",
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
messages := listTestMessages(t, messageStore, chat.ID)
|
||||
|
||||
if len(messages) != 0 {
|
||||
t.Fatalf(
|
||||
"expected no messages to be stored, got %d",
|
||||
len(messages),
|
||||
)
|
||||
}
|
||||
}
|
||||
func TestListMessagesPagination(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
userStore, chatStore, messageStore := fixture.users, fixture.chats, fixture.messages
|
||||
|
||||
user, created := createTestUser(t, userStore, "alice", "Алиса")
|
||||
if !created {
|
||||
t.Fatal("expected user to be created")
|
||||
}
|
||||
|
||||
chat := createTestChat(t, chatStore,
|
||||
"Тестовый чат",
|
||||
[]string{user.ID},
|
||||
)
|
||||
|
||||
first := createTestMessage(t, messageStore, chat.ID, user.ID, "Первое сообщение")
|
||||
second := createTestMessage(t, messageStore, chat.ID, user.ID, "Второе сообщение")
|
||||
third := createTestMessage(t, messageStore, chat.ID, user.ID, "Третье сообщение")
|
||||
|
||||
expectedIDs := map[string]bool{
|
||||
first.ID: true,
|
||||
second.ID: true,
|
||||
third.ID: true,
|
||||
}
|
||||
|
||||
handler := fixture.router
|
||||
|
||||
// Запрашиваем только два сообщения из трёх.
|
||||
firstRequest := httptest.NewRequest(
|
||||
http.MethodGet,
|
||||
"/api/v1/chats/"+chat.ID+"/messages?limit=2",
|
||||
nil,
|
||||
)
|
||||
authorizeTestRequest(t, fixture, firstRequest, user.ID)
|
||||
|
||||
firstResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(firstResponse, firstRequest)
|
||||
|
||||
if firstResponse.Code != http.StatusOK {
|
||||
t.Fatalf(
|
||||
"expected first page status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
firstResponse.Code,
|
||||
firstResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
var firstPage messagePage
|
||||
|
||||
if err := json.NewDecoder(firstResponse.Body).Decode(&firstPage); err != nil {
|
||||
t.Fatalf("failed to decode first page: %v", err)
|
||||
}
|
||||
|
||||
if len(firstPage.Items) != 2 {
|
||||
t.Fatalf(
|
||||
"expected 2 messages on first page, got %d",
|
||||
len(firstPage.Items),
|
||||
)
|
||||
}
|
||||
|
||||
if firstPage.NextCursor == nil || *firstPage.NextCursor == "" {
|
||||
t.Fatal("expected non-empty next cursor on first page")
|
||||
}
|
||||
|
||||
// Проверяем, что сообщения на первой странице уникальны
|
||||
// и принадлежат исходному набору.
|
||||
seenIDs := make(map[string]bool)
|
||||
|
||||
for _, message := range firstPage.Items {
|
||||
if !expectedIDs[message.ID] {
|
||||
t.Fatalf("unexpected message ID on first page: %s", message.ID)
|
||||
}
|
||||
|
||||
if seenIDs[message.ID] {
|
||||
t.Fatalf("duplicate message on first page: %s", message.ID)
|
||||
}
|
||||
|
||||
seenIDs[message.ID] = true
|
||||
}
|
||||
|
||||
// Используем cursor первой страницы для получения второй.
|
||||
secondRequest := httptest.NewRequest(
|
||||
http.MethodGet,
|
||||
"/api/v1/chats/"+chat.ID+"/messages?limit=2&cursor="+
|
||||
url.QueryEscape(*firstPage.NextCursor),
|
||||
nil,
|
||||
)
|
||||
authorizeTestRequest(t, fixture, secondRequest, user.ID)
|
||||
|
||||
secondResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(secondResponse, secondRequest)
|
||||
|
||||
if secondResponse.Code != http.StatusOK {
|
||||
t.Fatalf(
|
||||
"expected second page status %d, got %d; body: %s",
|
||||
http.StatusOK,
|
||||
secondResponse.Code,
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
var secondPage messagePage
|
||||
|
||||
if err := json.NewDecoder(secondResponse.Body).Decode(&secondPage); err != nil {
|
||||
t.Fatalf("failed to decode second page: %v", err)
|
||||
}
|
||||
|
||||
if len(secondPage.Items) != 1 {
|
||||
t.Fatalf(
|
||||
"expected 1 message on second page, got %d",
|
||||
len(secondPage.Items),
|
||||
)
|
||||
}
|
||||
|
||||
if secondPage.NextCursor != nil {
|
||||
t.Fatalf("expected second page to be final, got cursor %q", *secondPage.NextCursor)
|
||||
}
|
||||
|
||||
// Проверяем, что вторая страница не повторяет первую.
|
||||
for _, message := range secondPage.Items {
|
||||
if seenIDs[message.ID] {
|
||||
t.Fatalf("message repeated on second page: %s", message.ID)
|
||||
}
|
||||
|
||||
if !expectedIDs[message.ID] {
|
||||
t.Fatalf("unexpected message ID on second page: %s", message.ID)
|
||||
}
|
||||
|
||||
seenIDs[message.ID] = true
|
||||
}
|
||||
|
||||
if len(seenIDs) != len(expectedIDs) {
|
||||
t.Fatalf(
|
||||
"pagination lost messages: expected %d unique messages, got %d",
|
||||
len(expectedIDs),
|
||||
len(seenIDs),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestListPageByChatUsesCursorAndLimit(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Алиса")
|
||||
chat := createTestChat(t, f.chats, "Пагинация", []string{user.ID})
|
||||
other := createTestChat(t, f.chats, "Другой чат", []string{user.ID})
|
||||
createTestMessage(t, f.messages, other.ID, user.ID, "Не должно попасть в страницу")
|
||||
createdAt := time.Date(2026, 9, 18, 12, 0, 0, 0, time.UTC)
|
||||
ids := []string{
|
||||
"00000000-0000-4000-8000-000000000001", "00000000-0000-4000-8000-000000000002",
|
||||
"00000000-0000-4000-8000-000000000003", "00000000-0000-4000-8000-000000000004",
|
||||
}
|
||||
// Insert in reverse order to exercise SQL ordering, including equal timestamps.
|
||||
for i := len(ids) - 1; i >= 0; i-- {
|
||||
at := createdAt
|
||||
if i > 1 {
|
||||
at = at.Add(time.Duration(i-1) * time.Second)
|
||||
}
|
||||
_, err := f.db.Exec(context.Background(), "INSERT INTO messages(id,chat_id,sender_id,text,created_at) VALUES($1,$2,$3,$4,$5)", ids[i], chat.ID, user.ID, "message", at)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
first, more, err := f.messages.ListPageByChat(context.Background(), chat.ID, nil, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !more || len(first) != 2 || first[0].ID != ids[0] || first[1].ID != ids[1] {
|
||||
t.Fatalf("unexpected first page: %v, more=%v", first, more)
|
||||
}
|
||||
cursor := &messageCursor{ChatID: chat.ID, CreatedAt: first[1].CreatedAt, MessageID: first[1].ID}
|
||||
second, more, err := f.messages.ListPageByChat(context.Background(), chat.ID, cursor, 2)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if more || len(second) != 2 || second[0].ID != ids[2] || second[1].ID != ids[3] {
|
||||
t.Fatalf("unexpected second page: %v, more=%v", second, more)
|
||||
}
|
||||
}
|
||||
|
||||
func TestListPageByChatReturnsNonNilEmptyPage(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
page, more, err := f.messages.ListPageByChat(context.Background(), uuid.NewString(), nil, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if page == nil || len(page) != 0 || more {
|
||||
t.Fatalf("unexpected empty page: %v, more=%v", page, more)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"net/url"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
|
||||
"github.com/google/uuid"
|
||||
madmin "github.com/minio/madmin-go/v3"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
func s3Client(t *testing.T, root bool, anonymous bool) *minio.Client {
|
||||
t.Helper()
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("S3 integration: run make test-backend")
|
||||
}
|
||||
endpoint, err := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
read := func(name string) string {
|
||||
data, err := os.ReadFile(os.Getenv(name))
|
||||
if err != nil {
|
||||
t.Fatal("S3 credential file not mounted")
|
||||
}
|
||||
return strings.TrimSpace(string(data))
|
||||
}
|
||||
access, secret := "", ""
|
||||
if !anonymous {
|
||||
if root {
|
||||
access = read("S3_ROOT_ACCESS_KEY_ID_FILE")
|
||||
secret = read("S3_ROOT_SECRET_ACCESS_KEY_FILE")
|
||||
} else {
|
||||
access = read("S3_ACCESS_KEY_ID_FILE")
|
||||
secret = read("S3_SECRET_ACCESS_KEY_FILE")
|
||||
}
|
||||
}
|
||||
client, err := minio.New(endpoint.Host, &minio.Options{Creds: credentials.NewStaticV4(access, secret, ""), Secure: endpoint.Scheme == "https", Region: os.Getenv("S3_REGION")})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return client
|
||||
}
|
||||
func denied(t *testing.T, err error) {
|
||||
t.Helper()
|
||||
if err == nil || minio.ToErrorResponse(err).StatusCode != 403 {
|
||||
t.Fatalf("expected S3 HTTP 403, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestS3PermissionsAndBootstrap(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
app := s3Client(t, false, false)
|
||||
root := s3Client(t, true, false)
|
||||
anon := s3Client(t, false, true)
|
||||
bucket := os.Getenv("S3_BUCKET")
|
||||
key := "originals/" + uuid.NewString()
|
||||
data := []byte("permission fixture")
|
||||
t.Cleanup(func() { root.RemoveObject(context.Background(), bucket, key, minio.RemoveObjectOptions{}) })
|
||||
if _, err := app.PutObject(ctx, bucket, key, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for i := 0; i < 2; i++ {
|
||||
if err := repositories.BootstrapS3(ctx); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
content, err := app.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
actual, err := io.ReadAll(content)
|
||||
content.Close()
|
||||
if err != nil || !bytes.Equal(actual, data) {
|
||||
t.Fatalf("bootstrap changed existing object: %v", err)
|
||||
}
|
||||
object, err := anon.GetObject(ctx, bucket, key, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = object.Stat()
|
||||
object.Close()
|
||||
denied(t, err)
|
||||
listing, ok := <-anon.ListObjects(ctx, bucket, minio.ListObjectsOptions{})
|
||||
if !ok {
|
||||
t.Fatal("anonymous list did not return AccessDenied")
|
||||
}
|
||||
denied(t, listing.Err)
|
||||
forbidden := "forbidden/" + uuid.NewString()
|
||||
t.Cleanup(func() { root.RemoveObject(context.Background(), bucket, forbidden, minio.RemoveObjectOptions{}) })
|
||||
_, err = app.PutObject(ctx, bucket, forbidden, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{})
|
||||
denied(t, err)
|
||||
if _, err = root.PutObject(ctx, bucket, forbidden, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
outside, err := app.GetObject(ctx, bucket, forbidden, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = outside.Stat()
|
||||
outside.Close()
|
||||
denied(t, err)
|
||||
denied(t, app.RemoveObject(ctx, bucket, forbidden, minio.RemoveObjectOptions{}))
|
||||
other := "upload-permissions-" + uuid.NewString()
|
||||
if err = root.MakeBucket(ctx, other, minio.MakeBucketOptions{Region: os.Getenv("S3_REGION")}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
root.RemoveObject(context.Background(), other, key, minio.RemoveObjectOptions{})
|
||||
root.RemoveBucket(context.Background(), other)
|
||||
})
|
||||
_, err = app.PutObject(ctx, other, key, bytes.NewReader(data), int64(len(data)), minio.PutObjectOptions{})
|
||||
denied(t, err)
|
||||
denied(t, app.MakeBucket(ctx, "forbidden-"+uuid.NewString(), minio.MakeBucketOptions{}))
|
||||
denied(t, app.SetBucketPolicy(ctx, bucket, ""))
|
||||
if err = app.RemoveObject(ctx, bucket, key, minio.RemoveObjectOptions{}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
// Admin APIs have a separate JSON error type; reject exactly AccessDenied.
|
||||
endpoint, _ := url.Parse(os.Getenv("S3_ENDPOINT"))
|
||||
access, _ := os.ReadFile(os.Getenv("S3_ACCESS_KEY_ID_FILE"))
|
||||
secret, _ := os.ReadFile(os.Getenv("S3_SECRET_ACCESS_KEY_FILE"))
|
||||
admin, err := madmin.New(endpoint.Host, strings.TrimSpace(string(access)), strings.TrimSpace(string(secret)), endpoint.Scheme == "https")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_, err = admin.ListUsers(ctx)
|
||||
if err == nil || madmin.ToErrorResponse(err).Code != "AccessDenied" {
|
||||
t.Fatalf("API credentials permit admin access or wrong error: %v", err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestPasswordKDFUsesSaltAndFullUTF8Password(t *testing.T) {
|
||||
password := "длинный-пароль-с-эмодзи-🔒-0123456789"
|
||||
first, err := services.HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := services.HashPassword(password)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(first, []byte("$argon2id$v=19$m=65536,t=3,p=2$")) || bytes.Equal(first, second) {
|
||||
t.Fatal("password hashes must use Argon2id with independent salts")
|
||||
}
|
||||
valid, err := services.VerifyPassword(password, first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
invalid, err := services.VerifyPassword(password+"другое", first)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !valid || invalid {
|
||||
t.Fatal("Argon2id verification lost the full UTF-8 password")
|
||||
}
|
||||
if _, err := services.VerifyPassword(password, bytes.Repeat([]byte{1}, 64)); err == nil {
|
||||
t.Fatal("legacy scrypt hash must not be accepted")
|
||||
}
|
||||
}
|
||||
|
||||
func TestStoredPasswordUsesArgon2id(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
user, _ := createTestUser(t, fixture.users, "alice", "Алиса")
|
||||
var stored []byte
|
||||
if err := fixture.db.QueryRow(context.Background(), "SELECT password_hash FROM users WHERE id=$1", user.ID).Scan(&stored); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !bytes.HasPrefix(stored, []byte("$argon2id$")) {
|
||||
t.Fatalf("unexpected stored password hash format: %q", stored)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBearerRequiredIgnoresXUserID(t *testing.T) {
|
||||
handler := handlers.NewRouter(nil, nil, nil, nil, nil)
|
||||
request := httptest.NewRequest(http.MethodGet, "/api/v1/users/me", nil)
|
||||
request.Header.Set("X-User-Id", "11111111-1111-4111-8111-111111111111")
|
||||
response := httptest.NewRecorder()
|
||||
handler.ServeHTTP(response, request)
|
||||
if response.Code != 401 || response.Header().Get("WWW-Authenticate") != `Bearer realm="messenger"` {
|
||||
t.Fatalf("unexpected response: %d %v", response.Code, response.Header())
|
||||
}
|
||||
}
|
||||
|
||||
func TestMessageCursorPreservesPosition(t *testing.T) {
|
||||
original := messageCursor{ChatID: "11111111-1111-4111-8111-111111111111", CreatedAt: time.Date(2026, 9, 23, 10, 0, 0, 123000, time.UTC), MessageID: "22222222-2222-4222-8222-222222222222"}
|
||||
token, err := services.EncodeMessageCursor(original)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
decoded, err := services.DecodeMessageCursor(token)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if decoded != original {
|
||||
t.Fatalf("cursor changed: %+v", decoded)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type testFixture struct {
|
||||
db *pgxpool.Pool
|
||||
users *repositories.UserStore
|
||||
chats *repositories.ChatStore
|
||||
messages *repositories.MessageStore
|
||||
sessionStore *repositories.SessionStore
|
||||
sessions *services.SessionService
|
||||
router http.Handler
|
||||
}
|
||||
|
||||
// Each test owns a new schema; public application tables are never modified.
|
||||
func newTestFixture(t *testing.T) *testFixture {
|
||||
t.Helper()
|
||||
url := os.Getenv("TEST_DATABASE_URL")
|
||||
redisAddress := os.Getenv("TEST_REDIS_ADDR")
|
||||
if url == "" || redisAddress == "" {
|
||||
t.Skip("integration tests: run make test-backend, or set TEST_DATABASE_URL and TEST_REDIS_ADDR")
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
admin, err := pgxpool.New(ctx, url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(admin.Close)
|
||||
schema := "test_" + strings.ReplaceAll(uuid.NewString(), "-", "")
|
||||
identifier := pgx.Identifier{schema}.Sanitize()
|
||||
if _, err = admin.Exec(ctx, "CREATE SCHEMA "+identifier); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, err := admin.Exec(ctx, "DROP SCHEMA "+identifier+" CASCADE"); err != nil {
|
||||
t.Errorf("cleanup test schema: %v", err)
|
||||
}
|
||||
})
|
||||
cfg, err := pgxpool.ParseConfig(url)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = schema
|
||||
cfg.ConnConfig.RuntimeParams["application_name"] = schema
|
||||
db, err := pgxpool.NewWithConfig(ctx, cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(db.Close)
|
||||
ddl, err := os.ReadFile("../repositories/migrations/000001_initial.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000002_redis_sessions.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
redisClient := repositories.NewRedisClient(redisAddress)
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000003_message_idempotency.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000004_attachments.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000005_upload_attempts.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ddl, err = os.ReadFile("../repositories/migrations/000006_direct_uploads.up.sql")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err = db.Exec(ctx, string(ddl)); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { _ = redisClient.Close() })
|
||||
if err := redisClient.Ping(ctx).Err(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
users := repositories.NewUserStore(db)
|
||||
chats := repositories.NewChatStore(db)
|
||||
messages := repositories.NewMessageStore(db)
|
||||
userService := services.NewUserService(users)
|
||||
sessionStore := repositories.NewSessionStore(redisClient, "test:"+schema+":session:")
|
||||
sessions := services.NewSessionService(userService, sessionStore, time.Hour)
|
||||
router := handlers.NewRouter(services.NewHealthService(repositories.NewHealthStore(db), sessionStore), userService, sessions, services.NewChatService(chats), services.NewMessageService(messages))
|
||||
return &testFixture{db: db, users: users, chats: chats, messages: messages, sessionStore: sessionStore, sessions: sessions, router: router}
|
||||
}
|
||||
|
||||
func createTestUser(t *testing.T, store *repositories.UserStore, username, name string) (repositories.User, bool) {
|
||||
t.Helper()
|
||||
user, err := services.NewUserService(store).Create(context.Background(), username, name, "lab2-test-password")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return user, true
|
||||
}
|
||||
|
||||
func createTestChat(t *testing.T, store *repositories.ChatStore, title string, members []string) repositories.Chat {
|
||||
t.Helper()
|
||||
chat, err := store.Create(context.Background(), title, members)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return chat
|
||||
}
|
||||
|
||||
func createTestMessage(t *testing.T, store *repositories.MessageStore, chatID, userID, text string) repositories.Message {
|
||||
t.Helper()
|
||||
message, err := store.Create(context.Background(), chatID, userID, text)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
func listTestMessages(t *testing.T, store *repositories.MessageStore, chatID string) []repositories.Message {
|
||||
t.Helper()
|
||||
items, _, err := store.ListPageByChat(context.Background(), chatID, nil, 100)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return items
|
||||
}
|
||||
|
||||
func authorizeTestRequest(t *testing.T, fixture *testFixture, request *http.Request, userID string) {
|
||||
t.Helper()
|
||||
token, _, err := fixture.sessions.CreateForUser(request.Context(), userID)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
request.Header.Set("Authorization", "Bearer "+token)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
type User = repositories.User
|
||||
type Chat = repositories.Chat
|
||||
type messagePage = services.MessagePage
|
||||
type messageCursor = repositories.MessageCursor
|
||||
@@ -0,0 +1,208 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"errors"
|
||||
"image"
|
||||
"image/png"
|
||||
"io"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
type uploadFault struct {
|
||||
*repositories.ObjectStore
|
||||
afterPut func() error
|
||||
}
|
||||
|
||||
func (s uploadFault) Put(ctx context.Context, key string, reader io.Reader, size int64, mime string) error {
|
||||
if err := s.ObjectStore.Put(ctx, key, reader, size, mime); err != nil {
|
||||
return err
|
||||
}
|
||||
return s.afterPut()
|
||||
}
|
||||
|
||||
func uploadObjects(t *testing.T) *repositories.ObjectStore {
|
||||
t.Helper()
|
||||
if os.Getenv("S3_ENDPOINT") == "" {
|
||||
t.Skip("S3 integration: run make test-backend")
|
||||
}
|
||||
store, err := repositories.NewObjectStoreFromEnvironment()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return store
|
||||
}
|
||||
func uploadPNG(t *testing.T) []byte {
|
||||
t.Helper()
|
||||
var buffer bytes.Buffer
|
||||
if err := png.Encode(&buffer, image.NewRGBA(image.Rect(0, 0, 8, 8))); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return buffer.Bytes()
|
||||
}
|
||||
func uploadFixture(t *testing.T) (*testFixture, *repositories.AttachmentStore, *repositories.ObjectStore, string, string) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Upload recovery", []string{user.ID})
|
||||
return f, repositories.NewAttachmentStore(f.db), uploadObjects(t), user.ID, chat.ID
|
||||
}
|
||||
func ageAndClean(t *testing.T, f *testFixture, store *repositories.AttachmentStore, objects *repositories.ObjectStore) {
|
||||
t.Helper()
|
||||
ctx := context.Background()
|
||||
var key string
|
||||
if err := f.db.QueryRow(ctx, "SELECT object_key FROM upload_attempts").Scan(&key); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Cleanup(func() { objects.Delete(context.Background(), key) })
|
||||
if _, err := f.db.Exec(ctx, "UPDATE upload_attempts SET cleanup_after=clock_timestamp()-interval '1 second'"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
result, err := store.CleanupUploads(ctx, false, objects.Delete)
|
||||
if err != nil || result.Deleted != 1 {
|
||||
t.Fatalf("cleanup: %+v %v", result, err)
|
||||
}
|
||||
if content, err := objects.Get(ctx, key); err == nil {
|
||||
content.Close()
|
||||
t.Fatal("orphan still exists")
|
||||
}
|
||||
var count int
|
||||
if err := f.db.QueryRow(ctx, "SELECT count(*) FROM upload_attempts").Scan(&count); err != nil || count != 0 {
|
||||
t.Fatalf("journal: %d %v", count, err)
|
||||
}
|
||||
if err := f.db.QueryRow(ctx, "SELECT count(*) FROM attachments").Scan(&count); err != nil || count != 0 {
|
||||
t.Fatalf("unexpected metadata: %d %v", count, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUploadRecoveryAfterPutFailure(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
service := services.NewAttachmentService(store, uploadFault{objects, func() error { return errors.New("injected PUT result loss") }})
|
||||
if _, err := service.Upload(context.Background(), user, chat, "photo.png", "image/png", bytes.NewReader(uploadPNG(t))); err == nil {
|
||||
t.Fatal("expected failure")
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
func TestUploadRecoveryMetadataRejection(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
if _, err := f.db.Exec(context.Background(), `ALTER TABLE attachments ADD CONSTRAINT reject_fixture CHECK(filename <> 'reject.png')`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
service := services.NewAttachmentService(store, objects)
|
||||
if _, err := service.Upload(context.Background(), user, chat, "reject.png", "image/png", bytes.NewReader(uploadPNG(t))); err == nil {
|
||||
t.Fatal("expected metadata rejection")
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
func TestUploadRecoveryDatabaseConnectionLoss(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
// Kill only this fixture's idle upload transaction, after its S3 PUT.
|
||||
fault := func() error {
|
||||
_, err := f.db.Exec(context.Background(), `SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE application_name=$1 AND state='idle in transaction' AND pid<>pg_backend_pid()`, f.db.Config().ConnConfig.RuntimeParams["application_name"])
|
||||
return err
|
||||
}
|
||||
service := services.NewAttachmentService(store, uploadFault{objects, fault})
|
||||
if _, err := service.Upload(context.Background(), user, chat, "photo.png", "image/png", bytes.NewReader(uploadPNG(t))); err == nil {
|
||||
t.Fatal("expected connection loss")
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
|
||||
func TestUploadRecoveryProcessExit(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
directory := t.TempDir()
|
||||
t.Setenv("UPLOAD_TMP_DIR", directory)
|
||||
child := exec.Command(os.Args[0], "-test.run=^TestUploadCrashHelper$")
|
||||
child.Env = append(os.Environ(), "UPLOAD_CRASH_HELPER=1", "UPLOAD_TEST_DATABASE="+f.db.Config().ConnString(), "UPLOAD_TEST_SCHEMA="+f.db.Config().ConnConfig.RuntimeParams["search_path"], "UPLOAD_TEST_USER="+user, "UPLOAD_TEST_CHAT="+chat)
|
||||
output, err := child.CombinedOutput()
|
||||
var exit *exec.ExitError
|
||||
if !errors.As(err, &exit) || exit.ExitCode() != 23 {
|
||||
t.Fatalf("helper did not exit after PUT: %v %s", err, output)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil || len(files) != 0 {
|
||||
t.Fatalf("temporary files left after exit: %v %v", files, err)
|
||||
}
|
||||
}
|
||||
func TestUploadCrashHelper(t *testing.T) {
|
||||
if os.Getenv("UPLOAD_CRASH_HELPER") != "1" {
|
||||
return
|
||||
}
|
||||
cfg, err := pgxpool.ParseConfig(os.Getenv("UPLOAD_TEST_DATABASE"))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
cfg.ConnConfig.RuntimeParams["search_path"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
cfg.ConnConfig.RuntimeParams["application_name"] = os.Getenv("UPLOAD_TEST_SCHEMA")
|
||||
db, err := pgxpool.NewWithConfig(context.Background(), cfg)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
objects := uploadObjects(t)
|
||||
service := services.NewAttachmentService(repositories.NewAttachmentStore(db), uploadFault{objects, func() error { os.Exit(23); return nil }})
|
||||
_, err = service.Upload(context.Background(), os.Getenv("UPLOAD_TEST_USER"), os.Getenv("UPLOAD_TEST_CHAT"), "crash.png", "image/png", bytes.NewReader(uploadPNG(t)))
|
||||
t.Fatalf("helper failed before exit: %v", err)
|
||||
}
|
||||
|
||||
type interruptedReader struct{ first bool }
|
||||
|
||||
func (r *interruptedReader) Read(p []byte) (int, error) {
|
||||
if !r.first {
|
||||
r.first = true
|
||||
return copy(p, []byte("partial PNG")), nil
|
||||
}
|
||||
return 0, io.ErrUnexpectedEOF
|
||||
}
|
||||
func TestUploadRecoveryInterruptedBody(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
directory := t.TempDir()
|
||||
t.Setenv("UPLOAD_TMP_DIR", directory)
|
||||
service := services.NewAttachmentService(store, objects)
|
||||
if _, err := service.Upload(context.Background(), user, chat, "photo.png", "image/png", &interruptedReader{}); !errors.Is(err, io.ErrUnexpectedEOF) {
|
||||
t.Fatalf("body interruption: %v", err)
|
||||
}
|
||||
var count int
|
||||
f.db.QueryRow(context.Background(), "SELECT count(*) FROM upload_attempts").Scan(&count)
|
||||
if count != 0 {
|
||||
t.Fatal("reservation before complete body")
|
||||
}
|
||||
files, err := os.ReadDir(directory)
|
||||
if err != nil || len(files) != 0 {
|
||||
t.Fatalf("temporary paths left: %v %v", files, err)
|
||||
}
|
||||
}
|
||||
func TestUploadRecoveryWaitingCancellation(t *testing.T) {
|
||||
f, store, objects, user, chat := uploadFixture(t)
|
||||
entered := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
service := services.NewAttachmentService(store, uploadFault{objects, func() error { close(entered); <-release; return errors.New("test stop") }})
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
defer close(done)
|
||||
service.Upload(context.Background(), user, chat, "first.png", "image/png", bytes.NewReader(uploadPNG(t)))
|
||||
}()
|
||||
select {
|
||||
case <-entered:
|
||||
case <-time.After(10 * time.Second):
|
||||
t.Fatal("first upload did not reach PUT")
|
||||
}
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
_, err := service.Upload(ctx, user, chat, "second.png", "image/png", strings.NewReader("unused"))
|
||||
close(release)
|
||||
<-done
|
||||
if !errors.Is(err, context.Canceled) {
|
||||
t.Fatalf("waiting upload ignored cancellation: %v", err)
|
||||
}
|
||||
ageAndClean(t, f, store, objects)
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"messenger/src/backend/handlers"
|
||||
"messenger/src/backend/repositories"
|
||||
"messenger/src/backend/services"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCreateUserInvalidJSON(t *testing.T) {
|
||||
store := repositories.NewUserStore(nil)
|
||||
handler := handlers.NewRouter(nil, services.NewUserService(store), nil, nil, nil)
|
||||
|
||||
request := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{"username":`),
|
||||
)
|
||||
request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
response := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(response, request)
|
||||
|
||||
if response.Code != http.StatusBadRequest {
|
||||
t.Fatalf(
|
||||
"expected status %d, got %d; body: %s",
|
||||
http.StatusBadRequest,
|
||||
response.Code,
|
||||
response.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("Content-Type") != "application/json" {
|
||||
t.Fatalf(
|
||||
"expected Content-Type application/json, got %q",
|
||||
response.Header().Get("Content-Type"),
|
||||
)
|
||||
}
|
||||
|
||||
if response.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
body := response.Body.String()
|
||||
|
||||
if !strings.Contains(body, `"code":"invalid_request"`) {
|
||||
t.Fatalf(
|
||||
"expected invalid_request error code, got body: %s",
|
||||
body,
|
||||
)
|
||||
}
|
||||
|
||||
if !strings.Contains(body, "request_id") {
|
||||
t.Fatalf(
|
||||
"expected request_id in error body, got body: %s",
|
||||
body,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateUserDuplicateUsername(t *testing.T) {
|
||||
fixture := newTestFixture(t)
|
||||
handler := fixture.router
|
||||
|
||||
firstRequest := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{
|
||||
"username": "alice",
|
||||
"display_name": "Алиса", "password": "lab2-test-password"
|
||||
}`),
|
||||
)
|
||||
firstRequest.Header.Set("Content-Type", "application/json")
|
||||
|
||||
firstResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(firstResponse, firstRequest)
|
||||
|
||||
if firstResponse.Code != http.StatusCreated {
|
||||
t.Fatalf(
|
||||
"expected first request status %d, got %d; body: %s",
|
||||
http.StatusCreated,
|
||||
firstResponse.Code,
|
||||
firstResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
secondRequest := httptest.NewRequest(
|
||||
http.MethodPost,
|
||||
"/api/v1/users",
|
||||
strings.NewReader(`{
|
||||
"username": "alice",
|
||||
"display_name": "Другая Алиса", "password": "lab2-test-password"
|
||||
}`),
|
||||
)
|
||||
secondRequest.Header.Set("Content-Type", "application/json")
|
||||
|
||||
secondResponse := httptest.NewRecorder()
|
||||
|
||||
handler.ServeHTTP(secondResponse, secondRequest)
|
||||
|
||||
if secondResponse.Code != http.StatusConflict {
|
||||
t.Fatalf(
|
||||
"expected duplicate username status %d, got %d; body: %s",
|
||||
http.StatusConflict,
|
||||
secondResponse.Code,
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
if secondResponse.Header().Get("X-Request-Id") == "" {
|
||||
t.Fatal("expected non-empty X-Request-Id header")
|
||||
}
|
||||
|
||||
if !strings.Contains(
|
||||
secondResponse.Body.String(),
|
||||
`"code":"conflict"`,
|
||||
) {
|
||||
t.Fatalf(
|
||||
"expected conflict error code, got body: %s",
|
||||
secondResponse.Body.String(),
|
||||
)
|
||||
}
|
||||
|
||||
var count int
|
||||
if err := fixture.db.QueryRow(context.Background(), "SELECT count(*) FROM users").Scan(&count); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if count != 1 {
|
||||
t.Fatalf("expected one user, got %d", count)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"messenger/src/backend/services"
|
||||
)
|
||||
|
||||
func TestCaseVariantExtraFieldsReturn400WithoutSaving(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
chat := createTestChat(t, f.chats, "Chat", []string{user.ID})
|
||||
path := "/api/v1/chats/" + chat.ID + "/messages"
|
||||
key := uuid.NewString()
|
||||
send := func(path, body, key string) *httptest.ResponseRecorder {
|
||||
t.Helper()
|
||||
r := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
r.Header.Set("Idempotency-Key", key)
|
||||
authorizeTestRequest(t, f, r, user.ID)
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
return w
|
||||
}
|
||||
for _, test := range []struct{ path, body string }{
|
||||
{path, `{"text":"first","TEXT":"second"}`},
|
||||
{path, `{"TEXT":"second","text":"first"}`},
|
||||
{path, `{"Text":"second"}`},
|
||||
{path, `{"text":"first","Attachment_IDs":[]}`},
|
||||
{"/api/v1/chats", fmt.Sprintf(`{"title":"Chat","TITLE":"other","member_ids":[%q]}`, user.ID)},
|
||||
{"/api/v1/chats", fmt.Sprintf(`{"title":"Chat","MEMBER_IDS":[%q]}`, user.ID)},
|
||||
{"/api/v1/users", `{"username":"new","USERNAME":"other","display_name":"New","password":"test-password"}`},
|
||||
{"/api/v1/users", `{"username":"new","display_name":"New","Password":"test-password"}`},
|
||||
} {
|
||||
w := send(test.path, test.body, key)
|
||||
if w.Code != 400 || !strings.Contains(w.Body.String(), `"code":"invalid_request"`) {
|
||||
t.Fatalf("%s: want invalid_request 400, got %d %s", test.path, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
if len(listTestMessages(t, f.messages, chat.ID)) != 0 {
|
||||
t.Fatal("rejected request saved a message")
|
||||
}
|
||||
w := send(path, `{"text":"first"}`, key)
|
||||
if w.Code != 201 {
|
||||
t.Fatalf("corrected request with same key: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
if w = send(path, `{"text":"first","attachment_ids":[]}`, key); w.Code != 201 {
|
||||
t.Fatalf("valid replay: %d %s", w.Code, w.Body.String())
|
||||
}
|
||||
messages := listTestMessages(t, f.messages, chat.ID)
|
||||
if len(messages) != 1 || messages[0].Text != "first" {
|
||||
t.Fatal("wrong message or duplicate saved")
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatRejectsCaseVariantDuplicateMembersBeforeSQL(t *testing.T) {
|
||||
const id = "abcdef01-abcd-4abc-8abc-abcdef012345"
|
||||
for _, members := range [][]string{{id, strings.ToUpper(id)}, {strings.ToUpper(id), id}} {
|
||||
_, err := services.NewChatService(nil).Create(context.Background(), id, "Chat", members)
|
||||
var invalid services.InvalidInput
|
||||
if !errors.As(err, &invalid) {
|
||||
t.Fatalf("want InvalidInput before SQL, got %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestChatNormalizesMemberUUIDsHTTP(t *testing.T) {
|
||||
f := newTestFixture(t)
|
||||
user, _ := createTestUser(t, f.users, "alice", "Alice")
|
||||
for _, duplicate := range []bool{true, false} {
|
||||
members := fmt.Sprintf(`[%q]`, strings.ToUpper(user.ID))
|
||||
want := 201
|
||||
if duplicate {
|
||||
members = fmt.Sprintf(`[%q,%q]`, user.ID, strings.ToUpper(user.ID))
|
||||
want = 400
|
||||
}
|
||||
r := httptest.NewRequest(http.MethodPost, "/api/v1/chats", strings.NewReader(`{"title":"Chat","member_ids":`+members+`}`))
|
||||
r.Header.Set("Content-Type", "application/json")
|
||||
authorizeTestRequest(t, f, r, user.ID)
|
||||
w := httptest.NewRecorder()
|
||||
f.router.ServeHTTP(w, r)
|
||||
if w.Code != want {
|
||||
t.Fatalf("duplicate=%v: want %d, got %d %s", duplicate, want, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
var chats, members int
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chats").Scan(&chats); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := f.db.QueryRow(context.Background(), "SELECT count(*) FROM chat_members").Scan(&members); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if chats != 1 || members != 1 {
|
||||
t.Fatalf("unexpected persisted chats=%d members=%d", chats, members)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
node_modules
|
||||
dist
|
||||
@@ -0,0 +1,8 @@
|
||||
node_modules/
|
||||
dist/
|
||||
.vite/
|
||||
*.tsbuildinfo
|
||||
*.local
|
||||
.env
|
||||
.env.*
|
||||
!.env.example
|
||||
@@ -0,0 +1,12 @@
|
||||
FROM node:22-bookworm-slim
|
||||
|
||||
WORKDIR /app
|
||||
|
||||
COPY package.json package-lock.json ./
|
||||
RUN npm ci
|
||||
|
||||
COPY . .
|
||||
|
||||
EXPOSE 5173
|
||||
|
||||
CMD ["npm", "run", "dev"]
|
||||
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="ru">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>Мессенджер · проверка API</title>
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.tsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
Generated
+1288
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "messenger-test-ui",
|
||||
"private": true,
|
||||
"version": "1.0.0",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "vite",
|
||||
"build": "tsc --noEmit && vite build",
|
||||
"typecheck": "tsc --noEmit"
|
||||
},
|
||||
"dependencies": {
|
||||
"react": "19.3.0",
|
||||
"react-dom": "19.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/node": "^22.15.0",
|
||||
"@types/react": "19.3.0",
|
||||
"@types/react-dom": "19.3.0",
|
||||
"@vitejs/plugin-react": "6.1.1",
|
||||
"typescript": "7.0.2",
|
||||
"vite": "8.3.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,328 @@
|
||||
import { useCallback, useEffect, useRef, useState, type FormEvent } from "react";
|
||||
import { api, ApiError, type Attachment, type Chat, type Session, type User } from "./api";
|
||||
|
||||
import AttachmentImage from "./AttachmentImage";
|
||||
import { uploadDirect, type DirectProgress } from "./directUpload";
|
||||
import Pagination from "./Pagination";
|
||||
import { useCursorPage } from "./useCursorPage";
|
||||
|
||||
const tokenKey = "messenger_test_token";
|
||||
|
||||
function errorText(error: unknown): string {
|
||||
if (error instanceof ApiError) {
|
||||
const uploadMessages: Record<string, string> = {
|
||||
upload_not_uploaded: "Файл ещё не найден в S3. Нажмите «Повторить».",
|
||||
upload_expired: "Срок подтверждения загрузки истёк. Нажмите «Повторить».",
|
||||
upload_size_mismatch: "Размер полученного файла отличается от заявленного.",
|
||||
upload_checksum_mismatch: "Контрольная сумма полученного файла не совпала.",
|
||||
unsupported_media_type: "Файл повреждён или его содержимое не соответствует PNG/JPEG.",
|
||||
payload_too_large: "Изображение превышает 10 МиБ или 40 миллионов пикселей.",
|
||||
};
|
||||
const message = uploadMessages[error.code || ""] || error.message;
|
||||
return error.requestId
|
||||
? `${message} · запрос ${error.requestId}`
|
||||
: message;
|
||||
}
|
||||
return error instanceof Error ? error.message : "Неизвестная ошибка";
|
||||
}
|
||||
|
||||
function authErrorText(error: unknown): string {
|
||||
if (!(error instanceof ApiError)) return errorText(error);
|
||||
const message = error.status === 409
|
||||
? "Имя пользователя уже занято. Выберите другое или войдите."
|
||||
: error.status === 401
|
||||
? "Неверное имя пользователя или пароль."
|
||||
: error.status === 503
|
||||
? "Сервер временно недоступен. Попробуйте ещё раз позже."
|
||||
: error.message;
|
||||
return error.requestId ? `${message} · запрос ${error.requestId}` : message;
|
||||
}
|
||||
|
||||
function dateText(value: string): string {
|
||||
return new Date(value).toLocaleString("ru-RU", {
|
||||
dateStyle: "short",
|
||||
timeStyle: "short",
|
||||
});
|
||||
}
|
||||
|
||||
function AuthForm({ onSuccess }: { onSuccess: (session: Session) => void }) {
|
||||
const [mode, setMode] = useState<"login" | "register">("login");
|
||||
const [username, setUsername] = useState("");
|
||||
const [displayName, setDisplayName] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [error, setError] = useState("");
|
||||
|
||||
async function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setError("");
|
||||
const name = username.trim();
|
||||
if (!/^[a-z0-9_]{3,32}$/.test(name)) {
|
||||
setError("Имя пользователя: 3–32 символа, только строчные латинские буквы, цифры и _.");
|
||||
return;
|
||||
}
|
||||
const passwordLength = Array.from(password).length;
|
||||
if (passwordLength < 12 || passwordLength > 128 || (mode === "register" && !password.trim())) {
|
||||
setError("Пароль должен содержать от 12 до 128 символов.");
|
||||
return;
|
||||
}
|
||||
if (mode === "register" && (!displayName.trim() || Array.from(displayName.trim()).length > 100)) {
|
||||
setError("Отображаемое имя должно содержать от 1 до 100 символов.");
|
||||
return;
|
||||
}
|
||||
setBusy(true);
|
||||
let created = false;
|
||||
try {
|
||||
if (mode === "register") {
|
||||
await api.register(name, displayName.trim(), password);
|
||||
created = true;
|
||||
}
|
||||
const session = await api.login(name, password);
|
||||
setPassword("");
|
||||
onSuccess(session);
|
||||
} catch (cause) {
|
||||
setError(created
|
||||
? `Аккаунт создан, но войти не удалось. Повторите вход: ${authErrorText(cause)}`
|
||||
: authErrorText(cause));
|
||||
if (created) setMode("login");
|
||||
} finally {
|
||||
setBusy(false);
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<main className="auth-page">
|
||||
<div className="auth-card">
|
||||
<div className="brand-mark" aria-hidden="true"><svg viewBox="0 0 24 24"><path d="M5 4h14v12H9l-4 4V4Z" /><path d="M9 8h6M9 12h4" /></svg></div>
|
||||
<h1>Мессенджер</h1>
|
||||
<div className="tabs" aria-label="Режим входа">
|
||||
<button type="button" aria-pressed={mode === "login"} className={mode === "login" ? "active" : ""} onClick={() => { setMode("login"); setError(""); }}>Вход</button>
|
||||
<button type="button" aria-pressed={mode === "register"} className={mode === "register" ? "active" : ""} onClick={() => { setMode("register"); setError(""); }}>Регистрация</button>
|
||||
</div>
|
||||
<form onSubmit={submit} className="stack" noValidate>
|
||||
<label>
|
||||
Имя пользователя
|
||||
<input value={username} onChange={(event) => setUsername(event.target.value)} required minLength={3} maxLength={32} pattern="[a-z0-9_]+" autoComplete="username" placeholder="ivan_01" />
|
||||
</label>
|
||||
{mode === "register" && (
|
||||
<label>
|
||||
Отображаемое имя
|
||||
<input value={displayName} onChange={(event) => setDisplayName(event.target.value)} required maxLength={100} placeholder="Иван" />
|
||||
</label>
|
||||
)}
|
||||
<label>
|
||||
Пароль
|
||||
<input type="password" value={password} onChange={(event) => setPassword(event.target.value)} required minLength={12} maxLength={128} autoComplete={mode === "register" ? "new-password" : "current-password"} placeholder="Не менее 12 символов" />
|
||||
</label>
|
||||
{error && <p className="error" role="alert">{error}</p>}
|
||||
<button type="submit" className="primary" disabled={busy}>{busy ? "Подождите…" : mode === "register" ? "Создать аккаунт и войти" : "Войти"}</button>
|
||||
</form>
|
||||
</div>
|
||||
</main>
|
||||
);
|
||||
}
|
||||
|
||||
type UploadDraft = { localId: string; file: File; mode: "proxy" | "direct"; direct: DirectProgress; status: "preparing" | "uploading" | "checking" | "ready" | "error"; attachment?: Attachment; error?: string };
|
||||
|
||||
function Workspace({ token, user, onLogout }: { token: string; user: User; onLogout: (notice?: string) => void }) {
|
||||
const [selectedChat, setSelectedChat] = useState<Chat | null>(null);
|
||||
const [showCreate, setShowCreate] = useState(false);
|
||||
const [title, setTitle] = useState("");
|
||||
const [members, setMembers] = useState("");
|
||||
const [drafts, setDrafts] = useState<Record<string, string>>({});
|
||||
const [creating, setCreating] = useState(false);
|
||||
const [sending, setSending] = useState(false);
|
||||
const pendingMessages = useRef<Record<string, { body: string; key: string }>>({});
|
||||
const [uploads, setUploads] = useState<Record<string, UploadDraft[]>>({});
|
||||
const [uploading, setUploading] = useState(false);
|
||||
const [uploadMode, setUploadMode] = useState<"proxy" | "direct">("proxy");
|
||||
const uploadController = useRef(new AbortController());
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
uploadController.current = controller;
|
||||
return () => controller.abort();
|
||||
}, []);
|
||||
const [error, setError] = useState("");
|
||||
const [notice, setNotice] = useState("");
|
||||
const reportError = useCallback((cause: unknown) => {
|
||||
if (cause instanceof ApiError && cause.status === 401) onLogout("Сессия истекла. Войдите снова.");
|
||||
else setError(errorText(cause));
|
||||
}, [onLogout]);
|
||||
const fetchChats = useCallback((cursor: string | undefined, limit: number) => api.chats(token, cursor, limit), [token]);
|
||||
const chatId = selectedChat?.id || "";
|
||||
const fetchMessages = useCallback((cursor: string | undefined, limit: number) => api.messages(token, chatId, cursor, limit), [token, chatId]);
|
||||
const chats = useCursorPage(token, fetchChats, reportError);
|
||||
const messages = useCursorPage(chatId, fetchMessages, reportError);
|
||||
|
||||
async function createChat(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
if (!title.trim()) return;
|
||||
setCreating(true); setError("");
|
||||
try {
|
||||
const memberIds = Array.from(new Set([user.id, ...members.split(/[\s,;]+/).filter(Boolean)]));
|
||||
const chat = await api.createChat(token, title.trim(), memberIds);
|
||||
setSelectedChat(chat); setTitle(""); setMembers(""); setShowCreate(false); chats.refresh();
|
||||
} catch (cause) { reportError(cause); }
|
||||
finally { setCreating(false); }
|
||||
}
|
||||
|
||||
async function uploadFiles(targetChat: string, items: UploadDraft[]) {
|
||||
const signal = uploadController.current.signal;
|
||||
setUploading(true); setError("");
|
||||
const update = (id: string, patch: Partial<UploadDraft>) => setUploads(current => ({
|
||||
...current,
|
||||
[targetChat]: (current[targetChat] || []).map(item => item.localId === id ? { ...item, ...patch } : item)
|
||||
}));
|
||||
try {
|
||||
for (const item of items) {
|
||||
if (signal.aborted) break;
|
||||
update(item.localId, { status: "uploading", error: undefined });
|
||||
try {
|
||||
const attachment = item.mode === "direct"
|
||||
? await uploadDirect(token, targetChat, item.file, item.direct, signal, status => { if (!signal.aborted) update(item.localId, { status }); })
|
||||
: await api.uploadAttachment(token, targetChat, item.file, signal);
|
||||
if (signal.aborted) break;
|
||||
update(item.localId, { status: "ready", attachment });
|
||||
} catch (cause) {
|
||||
if (signal.aborted) break;
|
||||
update(item.localId, { status: "error", error: errorText(cause) });
|
||||
reportError(cause);
|
||||
if (cause instanceof ApiError && cause.status === 401) break;
|
||||
}
|
||||
}
|
||||
} finally { if (!signal.aborted) setUploading(false); }
|
||||
}
|
||||
|
||||
function selectFiles(files: FileList | null) {
|
||||
if (!files || uploading || sending) return;
|
||||
const current = uploads[chatId] || [];
|
||||
const chosen = Array.from(files);
|
||||
if (current.length + chosen.length > 10) { setError("Можно прикрепить не более 10 изображений."); return; }
|
||||
if (chosen.some(file => file.size < 1 || file.size > 10485760 || !["image/png", "image/jpeg"].includes(file.type))) {
|
||||
setError("Выберите PNG или JPEG размером до 10 МиБ каждый."); return;
|
||||
}
|
||||
const items: UploadDraft[] = chosen.map(file => ({ localId: crypto.randomUUID(), file, mode: uploadMode, direct: {}, status: "preparing" }));
|
||||
setUploads(previous => ({ ...previous, [chatId]: [...current, ...items] }));
|
||||
void uploadFiles(chatId, items);
|
||||
}
|
||||
|
||||
async function sendMessage(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
const text = (drafts[chatId] || "").trim();
|
||||
const selected = uploads[chatId] || [];
|
||||
if (uploading || selected.some(item => item.status !== "ready") || (!text && selected.length === 0)) return;
|
||||
const attachmentIds = selected.map(item => item.attachment!.id);
|
||||
const body = JSON.stringify({ text, attachment_ids: attachmentIds });
|
||||
const previous = pendingMessages.current[chatId];
|
||||
const pending = previous?.body === body ? previous : { body, key: crypto.randomUUID() };
|
||||
// A manual retry after an uncertain response keeps the original key/body.
|
||||
pendingMessages.current[chatId] = pending;
|
||||
setSending(true); setError("");
|
||||
try {
|
||||
await api.sendMessage(token, chatId, text, pending.key, attachmentIds);
|
||||
delete pendingMessages.current[chatId];
|
||||
setDrafts((previous) => ({ ...previous, [chatId]: "" }));
|
||||
setUploads(previous => ({ ...previous, [chatId]: [] }));
|
||||
messages.reload(); setNotice("Сообщение отправлено");
|
||||
} catch (cause) { reportError(cause); }
|
||||
finally { setSending(false); }
|
||||
}
|
||||
|
||||
async function logout() {
|
||||
uploadController.current.abort();
|
||||
uploadController.current = new AbortController();
|
||||
setUploading(false);
|
||||
setUploads({});
|
||||
try { await api.logout(token); onLogout(); }
|
||||
catch (cause) { reportError(cause); }
|
||||
}
|
||||
|
||||
async function copyId() {
|
||||
try { await navigator.clipboard.writeText(user.id); setNotice("ID скопирован"); }
|
||||
catch { setError("Не удалось скопировать ID. Выделите его вручную."); }
|
||||
}
|
||||
|
||||
return <div className="app-shell">
|
||||
<header className="app-header">
|
||||
<a className="brand" href="/">Мессенджер<span className="brand-dot" /></a>
|
||||
<div className="account"><span className="avatar">{user.display_name.slice(0, 1).toUpperCase()}</span><div><strong>{user.display_name}</strong><small>@{user.username}</small></div><button className="quiet" onClick={() => void logout()}>Выйти</button></div>
|
||||
</header>
|
||||
{(error || notice) && <div className={error ? "banner error" : "banner"} role={error ? "alert" : "status"}><span>{error || notice}</span><button className="quiet" aria-label="Закрыть уведомление" onClick={() => { setError(""); setNotice(""); }}>Закрыть</button></div>}
|
||||
<div className="app-content">
|
||||
<aside className="sidebar">
|
||||
<div className="sidebar-heading"><h1>Чаты</h1><button className="primary compact" aria-expanded={showCreate} onClick={() => setShowCreate(!showCreate)}>{showCreate ? "Отмена" : "Новый чат"}</button></div>
|
||||
{showCreate && <form className="create-form stack" onSubmit={createChat}>
|
||||
<label>Название<input autoFocus required maxLength={100} value={title} onChange={(event) => setTitle(event.target.value)} /></label>
|
||||
<label>Участники<textarea rows={2} placeholder="ID через запятую · вы уже добавлены" value={members} onChange={(event) => setMembers(event.target.value)} /></label>
|
||||
<button className="primary" disabled={creating}>{creating ? "Создание…" : "Создать"}</button>
|
||||
</form>}
|
||||
<div className="list-toolbar"><span>{chats.items.length} на странице</span><button className="quiet" disabled={chats.busy} onClick={chats.refresh}>Обновить</button></div>
|
||||
<div className="chat-list" aria-busy={chats.busy}>
|
||||
{chats.busy ? <p className="empty-note">Загрузка…</p> : chats.failed ? <p className="empty-note">Не удалось загрузить чаты</p> : !chats.items.length && <p className="empty-note">Нет чатов</p>}
|
||||
{chats.items.map((chat) => <button key={chat.id} className={`chat-item ${chatId === chat.id ? "selected" : ""}`} aria-pressed={chatId === chat.id} disabled={sending || uploading} onClick={() => setSelectedChat(chat)}><span className="chat-avatar">{chat.title.slice(0, 1).toUpperCase()}</span><span className="chat-copy"><strong>{chat.title}</strong><small>{chat.member_ids.length} участн.</small></span></button>)}
|
||||
</div>
|
||||
<Pagination label="Чаты" page={chats} />
|
||||
<details className="identity"><summary>Мой ID</summary><code>{user.id}</code><button className="quiet" onClick={() => void copyId()}>Копировать</button></details>
|
||||
</aside>
|
||||
<main className="conversation">
|
||||
{!selectedChat ? <div className="conversation-empty"><svg viewBox="0 0 48 48" aria-hidden="true"><path d="M8 8h32v26H18L8 42V8Z" /><path d="M17 18h14M17 25h9" /></svg><h2>Выберите чат</h2></div> : <>
|
||||
<header className="conversation-header"><div><h2>{selectedChat.title}</h2><details className="members"><summary>{selectedChat.member_ids.length} участн.</summary>{selectedChat.member_ids.map((id) => <code key={id}>{id}{id === user.id ? " · вы" : ""}</code>)}</details></div><button className="quiet" disabled={messages.busy || sending} onClick={messages.reload}>Обновить</button></header>
|
||||
<div className="messages" aria-busy={messages.busy}>
|
||||
{messages.busy ? <p className="empty-note">Загрузка…</p> : messages.failed ? <p className="empty-note">Не удалось загрузить сообщения</p> : !messages.items.length && <p className="empty-note">Нет сообщений</p>}
|
||||
{messages.items.map((message) => <article key={message.id} className={`message ${message.sender_id === user.id ? "own" : ""}`}><div className="message-meta"><strong title={message.sender_id}>{message.sender_id === user.id ? "Вы" : message.sender_id.slice(0, 8)}</strong><time dateTime={message.created_at}>{dateText(message.created_at)}</time></div>{message.text && <p>{message.text}</p>}{message.attachment_ids.map(id => <AttachmentImage key={id} id={id} token={token} onError={reportError} />)}</article>)}
|
||||
</div>
|
||||
<Pagination label="Сообщения" page={messages} />
|
||||
<form className="composer" onSubmit={sendMessage}>
|
||||
<div className="composer-fields">
|
||||
<textarea aria-label="Сообщение" value={drafts[chatId] || ""} onChange={(event) => setDrafts({ ...drafts, [chatId]: event.target.value })} disabled={sending} rows={2} maxLength={4000} placeholder="Сообщение…" />
|
||||
<label className="file-picker">Прикрепить PNG или JPEG · до 10 МиБ
|
||||
<input type="file" accept="image/png,image/jpeg" multiple disabled={sending || uploading || (uploads[chatId] || []).length >= 10} onChange={event => { selectFiles(event.target.files); event.target.value = ""; }} />
|
||||
</label>
|
||||
<label className="upload-method">
|
||||
Способ загрузки
|
||||
<select value={uploadMode} disabled={sending || uploading} onChange={event => setUploadMode(event.target.value as "proxy" | "direct")}>
|
||||
<option value="proxy">Через API</option>
|
||||
<option value="direct">Напрямую в S3</option>
|
||||
</select>
|
||||
</label>
|
||||
<ul className="upload-list" aria-live="polite">{(uploads[chatId] || []).map(item => <li key={item.localId}>
|
||||
<span><strong>{item.file.name}</strong> · {(item.file.size / 1048576).toFixed(2)} МиБ<br />
|
||||
{item.mode === "direct" ? "Напрямую в S3 · " : "Через API · "}{item.status === "preparing" ? "Подготовка…" : item.status === "uploading" ? "Загрузка…" : item.status === "checking" ? "Проверка…" : item.status === "ready" ? "Готово к отправке" : item.error}</span>
|
||||
{item.status === "error" && <button type="button" disabled={uploading || sending} onClick={() => void uploadFiles(chatId, [item])}>Повторить</button>}
|
||||
<button type="button" disabled={uploading || sending} aria-label={`Убрать ${item.file.name}`} onClick={() => setUploads(previous => ({ ...previous, [chatId]: (previous[chatId] || []).filter(upload => upload.localId !== item.localId) }))}>Убрать</button>
|
||||
</li>)}</ul>
|
||||
</div>
|
||||
<button className="primary" disabled={sending || uploading || messages.busy || (uploads[chatId] || []).some(item => item.status !== "ready") || (!(drafts[chatId] || "").trim() && !(uploads[chatId] || []).length)}>{sending ? "Отправка…" : uploading ? "Загрузка…" : "Отправить"}</button>
|
||||
</form>
|
||||
</>}
|
||||
</main>
|
||||
</div>
|
||||
</div>;
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
const [session, setSession] = useState<{ token: string; user: User } | null>(null);
|
||||
const [restoring, setRestoring] = useState(true);
|
||||
const [notice, setNotice] = useState("");
|
||||
const logout = useCallback((message = "") => {
|
||||
sessionStorage.removeItem(tokenKey); setSession(null); setNotice(message);
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
const token = sessionStorage.getItem(tokenKey);
|
||||
if (!token) { setRestoring(false); return; }
|
||||
let active = true;
|
||||
api.me(token).then((user) => {
|
||||
if (active) setSession({ token, user });
|
||||
}).catch((cause: unknown) => {
|
||||
if (!active) return;
|
||||
sessionStorage.removeItem(tokenKey);
|
||||
setNotice(cause instanceof ApiError && cause.status === 401 ? "Сессия истекла. Войдите снова." : errorText(cause));
|
||||
}).finally(() => { if (active) setRestoring(false); });
|
||||
return () => { active = false; };
|
||||
}, []);
|
||||
|
||||
if (restoring) return <main className="loading-page">Загрузка…</main>;
|
||||
if (session) return <Workspace key={session.token} {...session} onLogout={logout} />;
|
||||
return <><AuthForm onSuccess={(next) => { sessionStorage.setItem(tokenKey, next.token); setSession(next); setNotice(""); }} />{notice && <div className="toast" role="status">{notice}</div>}</>;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { api } from "./api";
|
||||
|
||||
export default function AttachmentImage({ id, token, onError }: {
|
||||
id: string; token: string; onError: (error: unknown) => void;
|
||||
}) {
|
||||
const [source, setSource] = useState("");
|
||||
const [filename, setFilename] = useState("Изображение");
|
||||
const [failed, setFailed] = useState(false);
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
let objectURL = "";
|
||||
setSource(""); setFailed(false);
|
||||
async function load() {
|
||||
try {
|
||||
const attachment = await api.attachment(token, id, controller.signal);
|
||||
const content = await api.attachmentContent(token, id, controller.signal);
|
||||
if (controller.signal.aborted) return;
|
||||
objectURL = URL.createObjectURL(content);
|
||||
setFilename(attachment.filename); setSource(objectURL);
|
||||
} catch (error) {
|
||||
if (!controller.signal.aborted) { setFailed(true); onError(error); }
|
||||
}
|
||||
}
|
||||
void load();
|
||||
return () => { controller.abort(); if (objectURL) URL.revokeObjectURL(objectURL); };
|
||||
}, [id, token, attempt, onError]);
|
||||
return <figure className="attachment-image">
|
||||
{source ? <a href={source} download={filename} aria-label={`Скачать ${filename}`}><img src={source} alt={filename} /></a>
|
||||
: failed ? <button type="button" onClick={() => setAttempt(attempt + 1)}>Повторить загрузку изображения</button>
|
||||
: <span role="status">Загрузка изображения…</span>}
|
||||
{source && <figcaption>{filename} · нажмите, чтобы скачать</figcaption>}
|
||||
</figure>;
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
interface PaginationProps {
|
||||
label: string;
|
||||
page: {
|
||||
number: number; size: number; range: string; busy: boolean;
|
||||
hasPrevious: boolean; hasNext: boolean;
|
||||
previous: () => void; next: () => void; resize: (size: number) => void;
|
||||
};
|
||||
}
|
||||
|
||||
export default function Pagination({ label, page }: PaginationProps) {
|
||||
return <nav className="pagination" aria-label={label}>
|
||||
<div className="page-settings">
|
||||
<label>По <select aria-label={`${label}: размер страницы`} value={page.size} disabled={page.busy} onChange={(event) => page.resize(Number(event.target.value))}>
|
||||
{[2, 5, 10, 25, 50].map((size) => <option key={size} value={size}>{size}</option>)}
|
||||
</select></label>
|
||||
<span className="page-range" aria-live="polite">{page.busy ? "Загрузка…" : `Записи ${page.range}`}</span>
|
||||
</div>
|
||||
<div className="page-navigation">
|
||||
<button type="button" aria-label={`${label}: назад`} onClick={page.previous} disabled={page.busy || !page.hasPrevious}>Назад</button>
|
||||
<span aria-live="polite">Стр. {page.number}</span>
|
||||
<button type="button" aria-label={`${label}: далее`} onClick={page.next} disabled={page.busy || !page.hasNext}>Далее</button>
|
||||
</div>
|
||||
</nav>;
|
||||
}
|
||||
@@ -0,0 +1,167 @@
|
||||
export interface User {
|
||||
id: string;
|
||||
username: string;
|
||||
display_name: string;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Chat {
|
||||
id: string;
|
||||
title: string;
|
||||
member_ids: string[];
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Attachment {
|
||||
id: string; chat_id: string; owner_id: string; filename: string;
|
||||
content_type: "image/png" | "image/jpeg"; size_bytes: number; sha256: string;
|
||||
status: "ready"; variants: unknown[]; error_code: null; created_at: string; updated_at: string;
|
||||
}
|
||||
|
||||
export interface DirectUpload {
|
||||
upload_id: string;
|
||||
expires_at: string;
|
||||
complete_before: string;
|
||||
upload: { method: "POST"; url: string; fields: Record<string, string> };
|
||||
}
|
||||
|
||||
export interface Message {
|
||||
id: string;
|
||||
chat_id: string;
|
||||
sender_id: string;
|
||||
text: string;
|
||||
attachment_ids: string[];
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
export interface Page<T> {
|
||||
items: T[];
|
||||
next_cursor: string | null;
|
||||
}
|
||||
|
||||
export interface Session {
|
||||
token: string;
|
||||
token_type: string;
|
||||
expires_at: string;
|
||||
user: User;
|
||||
}
|
||||
|
||||
export class ApiError extends Error {
|
||||
constructor(
|
||||
message: string,
|
||||
public readonly status: number,
|
||||
public readonly requestId?: string,
|
||||
public readonly code?: string,
|
||||
) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
|
||||
async function request<T>(path: string, init: RequestInit = {}): Promise<T> {
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(path, init);
|
||||
} catch (cause) {
|
||||
if (init.signal?.aborted) throw cause;
|
||||
throw new Error("Не удалось связаться с сервером.");
|
||||
}
|
||||
|
||||
if (response.status === 204) return undefined as T;
|
||||
|
||||
const data: unknown = await response.json().catch(() => null);
|
||||
if (!response.ok) {
|
||||
const error = data as { error?: { message?: string; request_id?: string; code?: string } } | null;
|
||||
throw new ApiError(
|
||||
error?.error?.message || `Ошибка HTTP ${response.status}`,
|
||||
response.status,
|
||||
error?.error?.request_id,
|
||||
error?.error?.code,
|
||||
);
|
||||
}
|
||||
return data as T;
|
||||
}
|
||||
|
||||
function jsonBody(value: unknown): Pick<RequestInit, "body" | "headers"> {
|
||||
return {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify(value),
|
||||
};
|
||||
}
|
||||
|
||||
function bearer(token: string): HeadersInit {
|
||||
return { Authorization: `Bearer ${token}` };
|
||||
}
|
||||
|
||||
function basic(username: string, password: string): string {
|
||||
const bytes = new TextEncoder().encode(`${username}:${password}`);
|
||||
return `Basic ${btoa(String.fromCharCode(...bytes))}`;
|
||||
}
|
||||
|
||||
export const api = {
|
||||
register: (username: string, displayName: string, password: string) =>
|
||||
request<User>("/api/v1/users", {
|
||||
method: "POST",
|
||||
...jsonBody({ username, display_name: displayName, password }),
|
||||
}),
|
||||
login: (username: string, password: string) =>
|
||||
request<Session>("/api/v1/auth/sessions", {
|
||||
method: "POST",
|
||||
headers: { Authorization: basic(username, password) },
|
||||
}),
|
||||
logout: (token: string) =>
|
||||
request<void>("/api/v1/auth/sessions/current", {
|
||||
method: "DELETE",
|
||||
headers: bearer(token),
|
||||
}),
|
||||
me: (token: string) =>
|
||||
request<User>("/api/v1/users/me", { headers: bearer(token) }),
|
||||
chats: (token: string, cursor?: string, limit = 10) => {
|
||||
const query = new URLSearchParams({ limit: String(limit) });
|
||||
if (cursor) query.set("cursor", cursor);
|
||||
return request<Page<Chat>>(`/api/v1/chats?${query}`, {
|
||||
headers: bearer(token),
|
||||
});
|
||||
},
|
||||
createChat: (token: string, title: string, memberIds: string[]) =>
|
||||
request<Chat>("/api/v1/chats", {
|
||||
method: "POST",
|
||||
headers: { ...bearer(token), "Content-Type": "application/json" },
|
||||
body: JSON.stringify({ title, member_ids: memberIds }),
|
||||
}),
|
||||
messages: (token: string, chatId: string, cursor?: string, limit = 10) => {
|
||||
const query = new URLSearchParams({ limit: String(limit) });
|
||||
if (cursor) query.set("cursor", cursor);
|
||||
return request<Page<Message>>(`/api/v1/chats/${encodeURIComponent(chatId)}/messages?${query}`, {
|
||||
headers: bearer(token),
|
||||
});
|
||||
},
|
||||
uploadAttachment: (token: string, chatId: string, file: File, signal?: AbortSignal) =>
|
||||
request<Attachment>(`/api/v1/chats/${encodeURIComponent(chatId)}/attachments?${new URLSearchParams({ filename: file.name })}`, {
|
||||
method: "POST", headers: { ...bearer(token), "Content-Type": file.type }, body: file, signal,
|
||||
}),
|
||||
initDirectUpload: (token: string, chatId: string, file: File, sha256: string, signal?: AbortSignal) =>
|
||||
request<DirectUpload>(`/api/v1/chats/${encodeURIComponent(chatId)}/attachment-uploads`, {
|
||||
method: "POST", headers: { ...bearer(token), "Content-Type": "application/json" }, signal,
|
||||
body: JSON.stringify({ filename: file.name, content_type: file.type, size_bytes: file.size, sha256 }),
|
||||
}),
|
||||
completeDirectUpload: (token: string, chatId: string, id: string, signal?: AbortSignal) =>
|
||||
request<Attachment>(`/api/v1/chats/${encodeURIComponent(chatId)}/attachment-uploads/${encodeURIComponent(id)}/complete`, {
|
||||
method: "POST", headers: bearer(token), signal,
|
||||
}),
|
||||
attachment: (token: string, id: string, signal?: AbortSignal) =>
|
||||
request<Attachment>(`/api/v1/attachments/${encodeURIComponent(id)}`, { headers: bearer(token), signal }),
|
||||
attachmentContent: async (token: string, id: string, signal?: AbortSignal) => {
|
||||
const response = await fetch(`/api/v1/attachments/${encodeURIComponent(id)}/content`, { headers: bearer(token), signal });
|
||||
if (!response.ok) {
|
||||
const data = await response.json().catch(() => null);
|
||||
throw new ApiError(data?.error?.message || `Ошибка HTTP ${response.status}`, response.status, response.headers.get("X-Request-Id") || undefined);
|
||||
}
|
||||
return response.blob();
|
||||
},
|
||||
sendMessage: (token: string, chatId: string, text: string, idempotencyKey: string, attachmentIds: string[] = []) =>
|
||||
request<Message>(`/api/v1/chats/${encodeURIComponent(chatId)}/messages`, {
|
||||
method: "POST",
|
||||
headers: { ...bearer(token), "Content-Type": "application/json", "Idempotency-Key": idempotencyKey },
|
||||
body: JSON.stringify({ text, attachment_ids: attachmentIds }),
|
||||
}),
|
||||
};
|
||||
@@ -0,0 +1,68 @@
|
||||
import { api, ApiError, type Attachment, type DirectUpload } from "./api";
|
||||
|
||||
export type UploadStage = "preparing" | "uploading" | "checking";
|
||||
export type DirectProgress = { session?: DirectUpload; sha256?: string; confirmOnly?: boolean };
|
||||
|
||||
async function complete(token: string, chat: string, progress: DirectProgress, signal: AbortSignal): Promise<Attachment> {
|
||||
try {
|
||||
return await api.completeDirectUpload(token, chat, progress.session!.upload_id, signal);
|
||||
} catch (cause) {
|
||||
if (cause instanceof ApiError && [400, 413, 415].includes(cause.status)) {
|
||||
progress.session = undefined;
|
||||
progress.confirmOnly = false;
|
||||
}
|
||||
throw cause;
|
||||
}
|
||||
}
|
||||
|
||||
export async function uploadDirect(
|
||||
token: string, chat: string, file: File, progress: DirectProgress,
|
||||
signal: AbortSignal, stage: (value: UploadStage) => void,
|
||||
): Promise<Attachment> {
|
||||
if (progress.session) {
|
||||
stage("checking");
|
||||
try { return await complete(token, chat, progress, signal); }
|
||||
catch (cause) {
|
||||
if (!(cause instanceof ApiError)) throw cause;
|
||||
if (cause.code === "upload_expired") {
|
||||
progress.session = undefined; progress.confirmOnly = false;
|
||||
} else if (cause.code === "upload_not_uploaded" && !progress.confirmOnly) {
|
||||
if (Date.now() >= Date.parse(progress.session!.expires_at)) progress.session = undefined;
|
||||
} else throw cause;
|
||||
}
|
||||
}
|
||||
if (!progress.session) {
|
||||
stage("preparing");
|
||||
if (!progress.sha256) {
|
||||
const hash = await crypto.subtle.digest("SHA-256", await file.arrayBuffer());
|
||||
progress.sha256 = Array.from(new Uint8Array(hash), byte => byte.toString(16).padStart(2, "0")).join("");
|
||||
}
|
||||
signal.throwIfAborted();
|
||||
progress.session = await api.initDirectUpload(token, chat, file, progress.sha256, signal);
|
||||
progress.confirmOnly = false;
|
||||
}
|
||||
stage("uploading");
|
||||
const form = new FormData();
|
||||
for (const [key, value] of Object.entries(progress.session.upload.fields)) form.append(key, value);
|
||||
form.append("file", file);
|
||||
let response: Response;
|
||||
try {
|
||||
response = await fetch(progress.session.upload.url, { method: "POST", body: form, credentials: "omit", signal });
|
||||
} catch (cause) {
|
||||
if (signal.aborted) throw cause;
|
||||
throw new Error("Не удалось подтвердить передачу в S3. Нажмите «Повторить»: сначала проверим, получен ли файл.");
|
||||
}
|
||||
if (!response.ok) {
|
||||
const xml = new DOMParser().parseFromString(await response.text(), "application/xml");
|
||||
const code = xml.querySelector("Code")?.textContent;
|
||||
const message = code === "EntityTooLarge" || code === "EntityTooSmall"
|
||||
? "S3 отклонил размер файла."
|
||||
: response.status === 403
|
||||
? "S3 отклонил подпись или её срок действия. Нажмите «Повторить»."
|
||||
: "Хранилище временно недоступно. Нажмите «Повторить».";
|
||||
throw new Error(message);
|
||||
}
|
||||
progress.confirmOnly = true;
|
||||
stage("checking");
|
||||
return complete(token, chat, progress, signal);
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { StrictMode } from "react";
|
||||
import { createRoot } from "react-dom/client";
|
||||
import App from "./App";
|
||||
import "./styles.css";
|
||||
|
||||
createRoot(document.getElementById("root")!).render(
|
||||
<StrictMode>
|
||||
<App />
|
||||
</StrictMode>,
|
||||
);
|
||||
@@ -0,0 +1,844 @@
|
||||
@import url("https://fonts.googleapis.com/css2?family=Manrope:wght@400;500;600;700;800&display=swap");
|
||||
|
||||
:root {
|
||||
font-family: "Manrope", "Segoe UI", sans-serif;
|
||||
color: #242925;
|
||||
background: #eef0ed;
|
||||
font-synthesis: none;
|
||||
--accent: #25634b;
|
||||
--muted: #697169;
|
||||
--line: #e4e8e1;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
}
|
||||
|
||||
body {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
button,
|
||||
input,
|
||||
textarea,
|
||||
select {
|
||||
font: inherit;
|
||||
}
|
||||
|
||||
button,
|
||||
summary,
|
||||
select {
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
button {
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 8px;
|
||||
background: white;
|
||||
padding: 9px 14px;
|
||||
color: inherit;
|
||||
font-size: 13px;
|
||||
font-weight: 650;
|
||||
transition: background .15s ease, color .15s ease;
|
||||
}
|
||||
|
||||
button:hover:not(:disabled) {
|
||||
background: #f0f3ee;
|
||||
}
|
||||
|
||||
button:disabled {
|
||||
cursor: default;
|
||||
color: #879084;
|
||||
background: #f5f6f3;
|
||||
}
|
||||
|
||||
button:focus-visible,
|
||||
input:focus-visible,
|
||||
textarea:focus-visible,
|
||||
select:focus-visible,
|
||||
summary:focus-visible,
|
||||
a:focus-visible {
|
||||
outline: 2px solid var(--accent);
|
||||
outline-offset: 3px;
|
||||
}
|
||||
|
||||
::selection {
|
||||
background: #d2e6d8;
|
||||
}
|
||||
|
||||
* {
|
||||
scrollbar-width: thin;
|
||||
scrollbar-color: #c4cec3 transparent;
|
||||
}
|
||||
|
||||
input,
|
||||
textarea {
|
||||
caret-color: var(--accent);
|
||||
}
|
||||
|
||||
h1,
|
||||
h2,
|
||||
p {
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 24px;
|
||||
letter-spacing: -.035em;
|
||||
font-weight: 800;
|
||||
}
|
||||
|
||||
h2 {
|
||||
font-size: 18px;
|
||||
letter-spacing: -.025em;
|
||||
}
|
||||
|
||||
.primary {
|
||||
background: var(--accent);
|
||||
border-color: transparent;
|
||||
color: white;
|
||||
}
|
||||
|
||||
.primary:hover:not(:disabled) {
|
||||
background: #194c38;
|
||||
}
|
||||
|
||||
.primary:disabled {
|
||||
background: #e0e6de;
|
||||
border-color: transparent;
|
||||
}
|
||||
|
||||
.quiet {
|
||||
border-color: transparent;
|
||||
background: transparent;
|
||||
color: var(--muted);
|
||||
padding: 7px 9px;
|
||||
}
|
||||
|
||||
.compact {
|
||||
font-size: 12px;
|
||||
padding: 9px 12px;
|
||||
}
|
||||
|
||||
.stack {
|
||||
display: grid;
|
||||
gap: 18px;
|
||||
}
|
||||
|
||||
label {
|
||||
display: grid;
|
||||
gap: 7px;
|
||||
font-size: 12px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
input,
|
||||
textarea {
|
||||
width: 100%;
|
||||
min-width: 0;
|
||||
padding: 12px;
|
||||
border: 1px solid #d6ded3;
|
||||
border-radius: 8px;
|
||||
background: #fff;
|
||||
color: inherit;
|
||||
font-size: 14px;
|
||||
font-weight: 400;
|
||||
}
|
||||
|
||||
input::placeholder,
|
||||
textarea::placeholder {
|
||||
color: #71796f;
|
||||
}
|
||||
|
||||
textarea {
|
||||
resize: vertical;
|
||||
}
|
||||
|
||||
.error {
|
||||
color: #a32f33;
|
||||
font-size: 13px;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.auth-page,
|
||||
.loading-page {
|
||||
min-height: 100dvh;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
padding: 24px;
|
||||
}
|
||||
|
||||
.auth-card {
|
||||
width: min(100%, 390px);
|
||||
}
|
||||
|
||||
.auth-card h1 {
|
||||
font-size: 32px;
|
||||
margin: 20px 0 28px;
|
||||
}
|
||||
|
||||
.brand-mark {
|
||||
width: 48px;
|
||||
height: 48px;
|
||||
border-radius: 14px;
|
||||
background: var(--accent);
|
||||
display: grid;
|
||||
place-items: center;
|
||||
}
|
||||
|
||||
.brand-mark svg {
|
||||
width: 28px;
|
||||
stroke: white;
|
||||
stroke-width: 1.5;
|
||||
stroke-linejoin: round;
|
||||
fill: none;
|
||||
}
|
||||
|
||||
.tabs {
|
||||
display: flex;
|
||||
gap: 4px;
|
||||
padding: 4px;
|
||||
border-radius: 10px;
|
||||
background: #e1e7de;
|
||||
margin-bottom: 28px;
|
||||
}
|
||||
|
||||
.tabs button {
|
||||
flex: 1;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
color: #596554;
|
||||
}
|
||||
|
||||
.tabs button.active {
|
||||
background: white;
|
||||
color: #242925;
|
||||
}
|
||||
|
||||
.auth-card .primary {
|
||||
padding: 13px;
|
||||
margin-top: 6px;
|
||||
}
|
||||
|
||||
.app-shell {
|
||||
width: min(1440px, 100%);
|
||||
height: calc(100dvh - 48px);
|
||||
min-height: 580px;
|
||||
margin: 24px auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: white;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 16px;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.app-header {
|
||||
min-height: 84px;
|
||||
padding: 18px 28px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 20px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.brand {
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
font-size: 21px;
|
||||
font-weight: 800;
|
||||
letter-spacing: -.035em;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.brand-dot {
|
||||
width: 7px;
|
||||
height: 7px;
|
||||
border-radius: 50%;
|
||||
background: var(--accent);
|
||||
}
|
||||
|
||||
.account {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
align-items: center;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.account strong,
|
||||
.account small {
|
||||
display: block;
|
||||
}
|
||||
|
||||
.account small {
|
||||
color: var(--muted);
|
||||
font-size: 11px;
|
||||
margin-top: 2px;
|
||||
}
|
||||
|
||||
.avatar,
|
||||
.chat-avatar {
|
||||
display: grid;
|
||||
place-items: center;
|
||||
flex-shrink: 0;
|
||||
background: #e7eee3;
|
||||
color: #435d3b;
|
||||
border-radius: 12px;
|
||||
width: 38px;
|
||||
height: 38px;
|
||||
font-size: 14px;
|
||||
font-weight: 700;
|
||||
}
|
||||
|
||||
.banner {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 16px;
|
||||
padding: 8px 24px;
|
||||
background: #edf5ee;
|
||||
color: #295735;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.banner.error {
|
||||
background: #fff0ee;
|
||||
color: #a32f33;
|
||||
}
|
||||
|
||||
.banner .quiet {
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.toast {
|
||||
position: fixed;
|
||||
bottom: 24px;
|
||||
left: 50%;
|
||||
transform: translateX(-50%);
|
||||
background: #242925;
|
||||
color: white;
|
||||
padding: 12px 18px;
|
||||
border-radius: 10px;
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.app-content {
|
||||
display: grid;
|
||||
grid-template-columns: 320px minmax(0, 1fr);
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
min-height: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
border-right: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.sidebar-heading {
|
||||
padding: 26px 22px 20px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.sidebar-heading h1 {
|
||||
font-size: 23px;
|
||||
}
|
||||
|
||||
.create-form {
|
||||
margin: 0 20px 16px;
|
||||
padding-bottom: 20px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
gap: 12px;
|
||||
overflow-y: auto;
|
||||
flex-shrink: 0;
|
||||
max-height: 38vh;
|
||||
}
|
||||
|
||||
.list-toolbar {
|
||||
padding: 0 16px 12px 24px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
color: var(--muted);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.chat-list {
|
||||
padding: 0 12px 16px;
|
||||
flex: 1;
|
||||
min-height: 60px;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.chat-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
text-align: left;
|
||||
width: 100%;
|
||||
padding: 14px 12px;
|
||||
border: 0;
|
||||
background: transparent;
|
||||
margin-bottom: 4px;
|
||||
border-radius: 10px;
|
||||
}
|
||||
|
||||
.chat-item.selected {
|
||||
background: #edf3e9;
|
||||
}
|
||||
|
||||
.chat-item.selected .chat-avatar {
|
||||
background: #dce8d4;
|
||||
color: #315c27;
|
||||
}
|
||||
|
||||
.chat-copy {
|
||||
min-width: 0;
|
||||
display: grid;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
.chat-copy strong {
|
||||
font-size: 13px;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.chat-copy small {
|
||||
font-size: 11px;
|
||||
font-weight: 400;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.identity {
|
||||
border-top: 1px solid var(--line);
|
||||
padding: 14px 24px;
|
||||
font-size: 12px;
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.identity code {
|
||||
display: block;
|
||||
font-size: 10px;
|
||||
margin-top: 12px;
|
||||
overflow-wrap: anywhere;
|
||||
user-select: all;
|
||||
}
|
||||
|
||||
.identity .quiet {
|
||||
padding-left: 0;
|
||||
}
|
||||
|
||||
.conversation {
|
||||
min-height: 0;
|
||||
min-width: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
background: #fafbf8;
|
||||
}
|
||||
|
||||
.conversation-empty {
|
||||
display: grid;
|
||||
justify-items: center;
|
||||
gap: 20px;
|
||||
margin: auto;
|
||||
color: var(--muted);
|
||||
padding: 40px;
|
||||
}
|
||||
|
||||
.conversation-empty svg {
|
||||
width: 48px;
|
||||
stroke: #95a38c;
|
||||
stroke-width: 1.5;
|
||||
fill: none;
|
||||
stroke-linejoin: round;
|
||||
}
|
||||
|
||||
.conversation-empty h2 {
|
||||
font-size: 16px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.conversation-header {
|
||||
background: white;
|
||||
padding: 24px 28px;
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: start;
|
||||
gap: 16px;
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.conversation-header h2 {
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.members {
|
||||
margin-top: 5px;
|
||||
color: var(--muted);
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.members code {
|
||||
display: block;
|
||||
overflow-wrap: anywhere;
|
||||
margin-top: 8px;
|
||||
font-size: 10px;
|
||||
}
|
||||
|
||||
.messages {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow-y: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 16px;
|
||||
padding: 28px;
|
||||
}
|
||||
|
||||
.message {
|
||||
max-width: min(85%, 620px);
|
||||
border-radius: 12px 12px 12px 3px;
|
||||
background: #eaece7;
|
||||
padding: 12px 16px;
|
||||
}
|
||||
|
||||
.message.own {
|
||||
align-self: flex-end;
|
||||
background: #e2edda;
|
||||
border-radius: 12px 12px 3px 12px;
|
||||
}
|
||||
|
||||
.message-meta {
|
||||
display: flex;
|
||||
gap: 24px;
|
||||
justify-content: space-between;
|
||||
align-items: baseline;
|
||||
color: #596453;
|
||||
font-size: 10px;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.message p {
|
||||
margin-top: 7px;
|
||||
line-height: 1.6;
|
||||
font-size: 14px;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.empty-note {
|
||||
margin: auto;
|
||||
padding: 24px;
|
||||
text-align: center;
|
||||
color: var(--muted);
|
||||
font-size: 13px;
|
||||
}
|
||||
|
||||
.pagination {
|
||||
flex-shrink: 0;
|
||||
border-top: 1px solid var(--line);
|
||||
padding: 14px 22px;
|
||||
background: white;
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
gap: 12px 24px;
|
||||
font-size: 11px;
|
||||
font-variant-numeric: tabular-nums;
|
||||
}
|
||||
|
||||
.page-settings,
|
||||
.page-navigation {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
}
|
||||
|
||||
.page-settings label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
color: var(--muted);
|
||||
font-weight: 500;
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.page-settings select {
|
||||
border: 1px solid var(--line);
|
||||
background: #fafbf8;
|
||||
border-radius: 6px;
|
||||
padding: 5px;
|
||||
color: inherit;
|
||||
}
|
||||
|
||||
.page-range {
|
||||
color: var(--muted);
|
||||
}
|
||||
|
||||
.page-navigation button {
|
||||
padding: 6px 10px;
|
||||
font-size: 11px;
|
||||
}
|
||||
|
||||
.page-navigation span {
|
||||
min-width: 44px;
|
||||
text-align: center;
|
||||
}
|
||||
|
||||
.sidebar .page-navigation {
|
||||
width: 100%;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.sidebar .page-settings {
|
||||
width: 100%;
|
||||
justify-content: space-between;
|
||||
}
|
||||
|
||||
.composer {
|
||||
display: flex;
|
||||
align-items: end;
|
||||
gap: 12px;
|
||||
padding: 20px 28px;
|
||||
background: white;
|
||||
border-top: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.composer textarea {
|
||||
background: #f7f8f5;
|
||||
border-color: transparent;
|
||||
resize: vertical;
|
||||
max-height: 180px;
|
||||
}
|
||||
|
||||
.composer .primary {
|
||||
padding: 13px 20px;
|
||||
}
|
||||
|
||||
@media (max-width: 1488px) {
|
||||
.app-shell {
|
||||
width: calc(100% - 48px);
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
.app-shell {
|
||||
width: 100%;
|
||||
height: 100dvh;
|
||||
margin: 0;
|
||||
border: 0;
|
||||
border-radius: 0;
|
||||
}
|
||||
|
||||
.app-content {
|
||||
grid-template-columns: 280px minmax(0, 1fr);
|
||||
}
|
||||
|
||||
.messages,
|
||||
.conversation-header {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.message {
|
||||
max-width: 95%;
|
||||
}
|
||||
|
||||
.message-meta {
|
||||
gap: 12px;
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.composer {
|
||||
padding: 16px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (max-width: 620px) {
|
||||
.app-shell {
|
||||
height: auto;
|
||||
min-height: 100dvh;
|
||||
}
|
||||
|
||||
.app-header {
|
||||
padding: 18px;
|
||||
}
|
||||
|
||||
.brand {
|
||||
font-size: 18px;
|
||||
}
|
||||
|
||||
.account .avatar,
|
||||
.account small {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.account {
|
||||
gap: 4px;
|
||||
}
|
||||
|
||||
.app-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
}
|
||||
|
||||
.sidebar {
|
||||
border-right: 0;
|
||||
border-bottom: 1px solid var(--line);
|
||||
}
|
||||
|
||||
.sidebar-heading {
|
||||
padding: 20px;
|
||||
}
|
||||
|
||||
.chat-list {
|
||||
max-height: 240px;
|
||||
}
|
||||
|
||||
.sidebar .pagination {
|
||||
flex-wrap: nowrap;
|
||||
}
|
||||
|
||||
.sidebar .page-settings,
|
||||
.sidebar .page-navigation {
|
||||
width: auto;
|
||||
}
|
||||
|
||||
.sidebar .page-range {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.conversation {
|
||||
min-height: 480px;
|
||||
}
|
||||
|
||||
.messages {
|
||||
max-height: 55dvh;
|
||||
min-height: 180px;
|
||||
}
|
||||
|
||||
.pagination {
|
||||
padding: 12px 16px;
|
||||
}
|
||||
|
||||
.composer {
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
.composer .primary {
|
||||
padding: 12px;
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
* {
|
||||
transition: none !important;
|
||||
}
|
||||
}
|
||||
|
||||
.composer-fields {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.composer-fields textarea {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
.file-picker {
|
||||
display: block;
|
||||
margin-top: 10px;
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.file-picker input {
|
||||
display: block;
|
||||
max-width: 100%;
|
||||
margin-top: 6px;
|
||||
}
|
||||
|
||||
.upload-list {
|
||||
list-style: none;
|
||||
margin: 8px 0 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
.upload-list li {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 8px 0;
|
||||
border-bottom: 1px solid var(--line);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.upload-list li span {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
overflow-wrap: anywhere;
|
||||
}
|
||||
|
||||
.attachment-image {
|
||||
margin: 10px 0 0;
|
||||
max-width: 100%;
|
||||
}
|
||||
|
||||
.attachment-image img {
|
||||
display: block;
|
||||
max-width: 100%;
|
||||
max-height: 320px;
|
||||
object-fit: contain;
|
||||
border-radius: 8px;
|
||||
}
|
||||
|
||||
.attachment-image figcaption {
|
||||
margin-top: 6px;
|
||||
overflow-wrap: anywhere;
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
@media (max-width: 600px) {
|
||||
.composer {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
|
||||
.composer-fields {
|
||||
flex-basis: 100%;
|
||||
}
|
||||
|
||||
.upload-list li {
|
||||
flex-wrap: wrap;
|
||||
}
|
||||
}
|
||||
|
||||
.upload-method {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
margin-top: 10px;
|
||||
color: var(--muted);
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.upload-method select {
|
||||
max-width: 100%;
|
||||
padding: 6px 8px;
|
||||
border: 1px solid var(--line);
|
||||
border-radius: 6px;
|
||||
background: #fafbf8;
|
||||
color: inherit;
|
||||
font: inherit;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import type { Page } from "./api";
|
||||
|
||||
// The API supplies only a next cursor. Remember visited cursors for Back.
|
||||
export function useCursorPage<T>(
|
||||
resource: string,
|
||||
fetchPage: (cursor: string | undefined, limit: number) => Promise<Page<T>>,
|
||||
onError: (error: unknown) => void,
|
||||
) {
|
||||
const [query, setQuery] = useState({ resource, cursors: [""], index: 0, size: 5, revision: 0 });
|
||||
const current = query.resource === resource ? query : { ...query, resource, cursors: [""], index: 0 };
|
||||
const cursor = current.cursors[current.index];
|
||||
const queryKey = JSON.stringify([resource, cursor, current.index, current.size, current.revision]);
|
||||
const [result, setResult] = useState<{ key: string; page: Page<T>; busy: boolean; failed: boolean }>({
|
||||
key: "", page: { items: [], next_cursor: null }, busy: false, failed: false,
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (!resource) return;
|
||||
let active = true;
|
||||
setResult({ key: queryKey, page: { items: [], next_cursor: null }, busy: true, failed: false });
|
||||
fetchPage(cursor || undefined, current.size).then((page) => {
|
||||
if (active) setResult({ key: queryKey, page, busy: false, failed: false });
|
||||
}).catch((error: unknown) => {
|
||||
if (!active) return;
|
||||
setResult({ key: queryKey, page: { items: [], next_cursor: null }, busy: false, failed: true });
|
||||
onError(error);
|
||||
});
|
||||
return () => { active = false; };
|
||||
}, [resource, queryKey, cursor, current.size, fetchPage, onError]);
|
||||
|
||||
const matches = result.key === queryKey;
|
||||
const items = matches ? result.page.items : [];
|
||||
const busy = Boolean(resource) && (!matches || result.busy);
|
||||
const nextCursor = matches ? result.page.next_cursor : null;
|
||||
|
||||
return {
|
||||
items, busy, failed: matches && result.failed,
|
||||
number: current.index + 1,
|
||||
size: current.size,
|
||||
hasPrevious: current.index > 0,
|
||||
hasNext: Boolean(nextCursor),
|
||||
range: items.length ? `${current.index * current.size + 1}–${current.index * current.size + items.length}` : "0",
|
||||
next() {
|
||||
if (!nextCursor || busy) return;
|
||||
setQuery({ ...current, cursors: [...current.cursors.slice(0, current.index + 1), nextCursor], index: current.index + 1 });
|
||||
},
|
||||
previous() {
|
||||
if (current.index > 0 && !busy) setQuery({ ...current, index: current.index - 1 });
|
||||
},
|
||||
resize(size: number) { setQuery({ ...current, size, index: 0, cursors: [""] }); },
|
||||
refresh() { setQuery({ ...current, index: 0, cursors: [""], revision: current.revision + 1 }); },
|
||||
reload() { setQuery({ ...current, revision: current.revision + 1 }); },
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "ES2022",
|
||||
"lib": ["ES2022", "DOM", "DOM.Iterable"],
|
||||
"module": "ESNext",
|
||||
"moduleResolution": "Bundler",
|
||||
"jsx": "react-jsx",
|
||||
"strict": true,
|
||||
"skipLibCheck": true,
|
||||
"noEmit": true,
|
||||
"types": ["vite/client", "node"]
|
||||
},
|
||||
"include": ["src", "vite.config.ts"]
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
const apiProxy = {
|
||||
target: process.env.API_PROXY_TARGET || "http://localhost:8080",
|
||||
changeOrigin: true,
|
||||
};
|
||||
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: {
|
||||
host: "0.0.0.0",
|
||||
port: 5173,
|
||||
strictPort: true,
|
||||
watch: { usePolling: true },
|
||||
proxy: {
|
||||
"/api/v1": apiProxy,
|
||||
"/health": apiProxy,
|
||||
},
|
||||
},
|
||||
});
|
||||
+58
@@ -0,0 +1,58 @@
|
||||
module messenger/src
|
||||
|
||||
go 1.22.2
|
||||
|
||||
require (
|
||||
github.com/golang-migrate/migrate/v4 v4.17.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/jackc/pgx/v5 v5.5.5
|
||||
github.com/minio/madmin-go/v3 v3.0.70
|
||||
github.com/minio/minio-go/v7 v7.0.76
|
||||
github.com/redis/go-redis/v9 v9.17.3
|
||||
golang.org/x/crypto v0.27.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-ole/go-ole v1.3.0 // indirect
|
||||
github.com/goccy/go-json v0.10.3 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
|
||||
github.com/golang/protobuf v1.5.4 // indirect
|
||||
github.com/hashicorp/errwrap v1.1.0 // indirect
|
||||
github.com/hashicorp/go-multierror v1.1.1 // indirect
|
||||
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa // 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.9 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.8 // indirect
|
||||
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 // indirect
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
|
||||
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 // indirect
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect
|
||||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.59.1 // indirect
|
||||
github.com/prometheus/procfs v0.15.1 // indirect
|
||||
github.com/prometheus/prom2json v1.4.0 // indirect
|
||||
github.com/prometheus/prometheus v0.54.1 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/safchain/ethtool v0.4.1 // indirect
|
||||
github.com/secure-io/sio-go v0.3.1 // indirect
|
||||
github.com/shirou/gopsutil/v3 v3.24.5 // indirect
|
||||
github.com/shoenig/go-m1cpu v0.1.6 // indirect
|
||||
github.com/tinylib/msgp v1.2.1 // indirect
|
||||
github.com/tklauser/go-sysconf v0.3.14 // indirect
|
||||
github.com/tklauser/numcpus v0.8.0 // indirect
|
||||
github.com/yusufpapurcu/wmi v1.2.4 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
golang.org/x/net v0.29.0 // indirect
|
||||
golang.org/x/sync v0.8.0 // indirect
|
||||
golang.org/x/sys v0.25.0 // indirect
|
||||
golang.org/x/text v0.18.0 // indirect
|
||||
google.golang.org/protobuf v1.34.2 // indirect
|
||||
)
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
|
||||
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
|
||||
github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migciow=
|
||||
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
|
||||
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/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
|
||||
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/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/dhui/dktest v0.4.1 h1:/w+IWuDXVymg3IrRJCHHOkMK10m9aNVMOyD0X12YVTg=
|
||||
github.com/dhui/dktest v0.4.1/go.mod h1:DdOqcUpL7vgyP4GlF3X3w7HbSlz8cEQzwewPveYEQbA=
|
||||
github.com/distribution/reference v0.5.0 h1:/FUIFXtfc/x2gpa5/VGfiGLuOIdYa1t65IKK2OFGvA0=
|
||||
github.com/distribution/reference v0.5.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
|
||||
github.com/docker/docker v27.0.3+incompatible h1:aBGI9TeQ4MPlhquTQKq9XbK79rKFVwXNUAYz9aXyEBE=
|
||||
github.com/docker/docker v27.0.3+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
|
||||
github.com/docker/go-connections v0.4.0 h1:El9xVISelRB7BuFusrZozjnkIM5YnzCViNKohAFqRJQ=
|
||||
github.com/docker/go-connections v0.4.0/go.mod h1:Gbd7IOopHjR8Iph03tsViu4nIes5XhDvyHbTtUxmeec=
|
||||
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
|
||||
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
|
||||
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/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
|
||||
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-logr/logr v1.4.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
|
||||
github.com/go-logr/logr v1.4.2/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
|
||||
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
|
||||
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
|
||||
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
|
||||
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
|
||||
github.com/goccy/go-json v0.10.3 h1:KZ5WoDbxAIgm2HNbYckL0se1fHD6rz5j4ywS6ebzDqA=
|
||||
github.com/goccy/go-json v0.10.3/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
|
||||
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0 h1:7cYmW1XlMY7h7ii7UhUyChSgS5wUJEnm9uZVTGqOWzg=
|
||||
github.com/golang-jwt/jwt/v4 v4.5.0/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang-migrate/migrate/v4 v4.17.1 h1:4zQ6iqL6t6AiItphxJctQb3cFqWiSpMnX7wLTPnnYO4=
|
||||
github.com/golang-migrate/migrate/v4 v4.17.1/go.mod h1:m8hinFyWBn0SA4QKHuKh175Pm9wjmxj3S2Mia7dbXzM=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
|
||||
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
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/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
|
||||
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
|
||||
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
|
||||
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
|
||||
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa h1:s+4MhCQ6YrzisK6hFJUX53drDT4UsSW3DEhKn0ifuHw=
|
||||
github.com/jackc/pgerrcode v0.0.0-20220416144525-469b46aa5efa/go.mod h1:a/s9Lp5W7n/DD0VrVoyJ00FbP2ytTPDVOivvn2bMlds=
|
||||
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.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA=
|
||||
github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM=
|
||||
github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws=
|
||||
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
|
||||
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
|
||||
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683 h1:7UMa6KCCMjZEMDtTVdcGu0B1GmmC7QJKiCCjyTAWQy0=
|
||||
github.com/lufia/plan9stats v0.0.0-20240909124753-873cd0166683/go.mod h1:ilwx/Dta8jXAgpFYFvSWEMwxmbWXyiUHkd5FwyKhb5k=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zkfA9PSy5pEvNWRP0ET0TIVo=
|
||||
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
|
||||
github.com/minio/madmin-go/v3 v3.0.70 h1:zrFCXLcV6PR74JC0yytK4Dk2qsaCV8kXQoPTvcusR2k=
|
||||
github.com/minio/madmin-go/v3 v3.0.70/go.mod h1:TOTc96ZkMknNhl+ReO/V68bQfgRGfH+8iy7YaDzHdXA=
|
||||
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.76 h1:9nxHH2XDai61cT/EFhyIw/wW4vJfpPNvl7lSFpRt+Ng=
|
||||
github.com/minio/minio-go/v7 v7.0.76/go.mod h1:AVM3IUN6WwKzmwBxVdjzhH8xq+f57JSbbvzqvUzR6eg=
|
||||
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
|
||||
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
|
||||
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
|
||||
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
|
||||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
|
||||
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
|
||||
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
|
||||
github.com/opencontainers/image-spec v1.0.2 h1:9yCKha/T5XdGtO0q9Q9a6T5NUCsTn/DrBg0D7ufOcFM=
|
||||
github.com/opencontainers/image-spec v1.0.2/go.mod h1:BtxoFyWECRxE4U/7sNtV5W15zMzWCbyJoFRP3s7yZA0=
|
||||
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986 h1:jYi87L8j62qkXzaYHAQAhEapgukhenIMZRBKTNRLHJ4=
|
||||
github.com/philhofer/fwd v1.1.3-0.20240612014219-fbbf4953d986/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
|
||||
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
|
||||
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
|
||||
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
|
||||
github.com/prometheus/client_model v0.6.1/go.mod h1:OrxVMOVHjw3lKMa8+x6HeMGkHMQyHDk9E3jmP2AmGiY=
|
||||
github.com/prometheus/common v0.59.1 h1:LXb1quJHWm1P6wq/U824uxYi4Sg0oGvNeUm1z5dJoX0=
|
||||
github.com/prometheus/common v0.59.1/go.mod h1:GpWM7dewqmVYcd7SmRaiWVe9SSqjf0UrwnYnpEZNuT0=
|
||||
github.com/prometheus/procfs v0.15.1 h1:YagwOFzUgYfKKHX6Dr+sHT7km/hxC76UB0learggepc=
|
||||
github.com/prometheus/procfs v0.15.1/go.mod h1:fB45yRUv8NstnjriLhBQLuOUt+WW4BsoGhij/e3PBqk=
|
||||
github.com/prometheus/prom2json v1.4.0 h1:2AEOsd1ebqql/p9u0IWgCpUAteAAf9Lnf/SVyieqer4=
|
||||
github.com/prometheus/prom2json v1.4.0/go.mod h1:DmcIMPspQD/fMyFCYti5qJJbuEnqDh3DGoooO0sgr4w=
|
||||
github.com/prometheus/prometheus v0.54.1 h1:vKuwQNjnYN2/mDoWfHXDhAsz/68q/dQDb+YbcEqU7MQ=
|
||||
github.com/prometheus/prometheus v0.54.1/go.mod h1:xlLByHhk2g3ycakQGrMaU8K7OySZx98BzeCR99991NY=
|
||||
github.com/redis/go-redis/v9 v9.17.3 h1:fN29NdNrE17KttK5Ndf20buqfDZwGNgoUr9qjl1DQx4=
|
||||
github.com/redis/go-redis/v9 v9.17.3/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0//kSOd3370=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/safchain/ethtool v0.4.1 h1:S6mEleTADqgynileXoiapt/nKnatyR6bmIHoF+h2ADo=
|
||||
github.com/safchain/ethtool v0.4.1/go.mod h1:XLLnZmy4OCRTkksP/UiMjij96YmIsBfmBQcs7H6tA48=
|
||||
github.com/secure-io/sio-go v0.3.1 h1:dNvY9awjabXTYGsTF1PiCySl9Ltofk9GA3VdWlo7rRc=
|
||||
github.com/secure-io/sio-go v0.3.1/go.mod h1:+xbkjDzPjwh4Axd07pRKSNriS9SCiYksWnZqdnfpQxs=
|
||||
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
|
||||
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
|
||||
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
|
||||
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
|
||||
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
|
||||
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
|
||||
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.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||
github.com/tinylib/msgp v1.2.1 h1:6ypy2qcCznxpP4hpORzhtXyTqrBs7cfM9MCCWY8zsmU=
|
||||
github.com/tinylib/msgp v1.2.1/go.mod h1:2vIGs3lcUo8izAATNobrCHevYZC/LMsJtw4JPiYPHro=
|
||||
github.com/tklauser/go-sysconf v0.3.14 h1:g5vzr9iPFFz24v2KZXs/pvpvh8/V9Fw6vQK5ZZb78yU=
|
||||
github.com/tklauser/go-sysconf v0.3.14/go.mod h1:1ym4lWMLUOhuBOPGtRcJm7tEGX4SCYNEEEtghGG/8uY=
|
||||
github.com/tklauser/numcpus v0.8.0 h1:Mx4Wwe/FjZLeQsK/6kt2EOepwwSl7SmJrK5bV/dXYgY=
|
||||
github.com/tklauser/numcpus v0.8.0/go.mod h1:ZJZlAY+dmR4eut8epnzf0u/VwodKmryxR8txiloSqBE=
|
||||
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
|
||||
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0 h1:4K4tsIXefpVJtvA/8srF4V4y0akAoPHkIslgAkjixJA=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.53.0/go.mod h1:jjdQuTGVsXV4vSs+CJ2qYDeDPf9yIJV23qlIzBm73Vg=
|
||||
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
|
||||
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
|
||||
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
|
||||
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
|
||||
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
|
||||
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
|
||||
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
|
||||
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
|
||||
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
|
||||
golang.org/x/mod v0.19.0 h1:fEdghXQSo20giMthA7cd28ZC+jts4amQ3YMXiP5oMQ8=
|
||||
golang.org/x/mod v0.19.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
|
||||
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
|
||||
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
|
||||
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
|
||||
golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20200302150141-5c8b2ff67527/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
|
||||
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
|
||||
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
|
||||
golang.org/x/tools v0.23.0 h1:SGsXPZ+2l4JsgaCKkx+FQ9YZ5XEtA1GZYuoDjenLjvg=
|
||||
golang.org/x/tools v0.23.0/go.mod h1:pnu6ufv6vQkll6szChhK3C3L/ruaIv5eBeztNG8wtsI=
|
||||
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
|
||||
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
|
||||
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=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
Reference in New Issue
Block a user