feat(app) lab 3 implemented

This commit is contained in:
imadev
2026-10-30 18:11:30 +03:00
parent f731f9b460
commit 48a616e910
57 changed files with 1390 additions and 303 deletions
+96 -38
View File
@@ -1,7 +1,10 @@
# AGENT: Single local stack for lab 2 grade 4. PostgreSQL is the source of
# truth, Redis stores sessions with AOF persistence. Both have named volumes,
# so `docker compose restart` preserves data and `down -v` is never needed.
name: mtusi2026lab2
name: mtusi2026lab
x-api-build: &api-build
image: mtusi2026lab-api:local
build:
context: .
dockerfile: Dockerfile
x-api-environment: &api-environment
APP_HOST: 0.0.0.0
@@ -9,6 +12,8 @@ x-api-environment: &api-environment
DB_POOL_MAX_CONNS: ${DB_POOL_MAX_CONNS:-10}
DATABASE_URL: postgres://${POSTGRES_USER:-messenger}:${POSTGRES_PASSWORD:?set POSTGRES_PASSWORD in .env}@postgres:5432/${POSTGRES_DB:-messenger}?sslmode=disable
CACHE_URL: redis://redis:6379/0
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
IDEMPOTENCY_TTL_SECONDS: ${IDEMPOTENCY_TTL_SECONDS:-86400}
services:
postgres:
@@ -26,13 +31,25 @@ services:
timeout: 3s
retries: 20
start_period: 5s
deploy:
resources:
limits:
cpus: "${POSTGRES_CPU_LIMIT:-1.0}"
memory: "${POSTGRES_MEM_LIMIT:-512M}"
redis:
image: redis:7.4.7-alpine
restart: unless-stopped
# AOF survives a container restart and keeps absolute key TTLs, which is
# exactly what the session-persistence check needs.
command: ["redis-server", "--appendonly", "yes", "--appendfsync", "everysec", "--save", ""]
command:
[
"redis-server",
"--appendonly",
"yes",
"--appendfsync",
"everysec",
"--save",
"",
]
volumes:
- redisdata:/data
healthcheck:
@@ -41,55 +58,96 @@ services:
timeout: 3s
retries: 20
start_period: 3s
deploy:
resources:
limits:
cpus: "${REDIS_CPU_LIMIT:-0.50}"
memory: "${REDIS_MEM_LIMIT:-256M}"
migrate:
<<: *api-build
restart: "no"
depends_on:
postgres:
condition: service_healthy
environment:
<<: *api-environment
MIGRATE_ONLY: "true"
api:
build:
context: .
dockerfile: Dockerfile
<<: *api-build
restart: unless-stopped
depends_on:
postgres:
condition: service_healthy
redis:
condition: service_healthy
migrate:
condition: service_completed_successfully
environment:
<<: *api-environment
SESSION_TTL_SECONDS: ${SESSION_TTL_SECONDS:-3600}
ports:
- "${APP_PORT_PUBLISH:-8080}:8080"
RUN_MIGRATIONS: "false"
expose:
- "8080"
healthcheck:
test: ["CMD", "wget", "-q", "-O", "-", "http://127.0.0.1:8080/health/ready"]
interval: 5s
timeout: 4s
retries: 12
start_period: 15s
test:
["CMD", "wget", "-q", "-O", "-", "http://127.0.0.1:8080/health/ready"]
interval: 2s
timeout: 2s
retries: 10
start_period: 10s
stop_grace_period: 15s
deploy:
replicas: ${API_REPLICAS:-2}
resources:
limits:
cpus: "${API_CPU_LIMIT:-0.50}"
memory: "${API_MEM_LIMIT:-256M}"
# AGENT: Only for the short-TTL expiry check. It shares PostgreSQL and Redis
# with the main API, so its 2-second sessions are visible to every instance.
api-short-ttl:
profiles: ["checks"]
build:
context: .
dockerfile: Dockerfile
haproxy:
image: haproxy:3.2.6-alpine
restart: unless-stopped
depends_on:
postgres:
api:
condition: service_healthy
redis:
condition: service_healthy
environment:
<<: *api-environment
SESSION_TTL_SECONDS: "2"
ports:
- "${APP_SHORT_TTL_PORT:-8081}:8080"
- "${LB_PORT:-8080}:8080"
- "${HAPROXY_STATS_PORT:-8404}:8404"
volumes:
- ./infra/haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg:ro
healthcheck:
test: ["CMD", "wget", "-q", "-O", "-", "http://127.0.0.1:8080/health/ready"]
interval: 5s
timeout: 4s
retries: 12
start_period: 15s
stop_grace_period: 15s
test:
["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:8404/stats"]
interval: 2s
timeout: 2s
retries: 10
start_period: 5s
deploy:
resources:
limits:
cpus: "${HAPROXY_CPU_LIMIT:-0.50}"
memory: "${HAPROXY_MEM_LIMIT:-128M}"
k6:
image: grafana/k6:1.3.0
profiles: ["load"]
depends_on:
haproxy:
condition: service_healthy
user: "0:0"
environment:
BASE_URL: http://haproxy:8080
VUS: ${K6_VUS:-10}
DURATION: ${K6_DURATION:-60s}
volumes:
- ./load:/scripts:ro
- ./evidence:/results
command: ["run", "/scripts/messages.js"]
deploy:
resources:
limits:
cpus: "${K6_CPU_LIMIT:-1.0}"
memory: "${K6_MEM_LIMIT:-512M}"
volumes:
pgdata: