From e51dc20fce3913a1b4dfc1b216a5f138c8fc4b21 Mon Sep 17 00:00:00 2001 From: Artu-Rito Date: Wed, 18 Nov 2026 17:44:53 +0300 Subject: [PATCH] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D0=B1=D0=B0=D0=BB=D0=B0=D0=BD=D1=81=D0=B8=D1=80?= =?UTF-8?q?=D0=BE=D0=B2=D0=BA=D1=83=20=D0=B8=20=D0=BC=D0=B8=D0=B3=D1=80?= =?UTF-8?q?=D0=B0=D1=86=D0=B8=D0=B8=20=D0=BB=D0=B0=D0=B1=D1=8B=203?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/db.py | 9 ++++++++- app/main.py | 22 ++++++++++++++++++++-- app/migrate.py | 24 ++++++++++++++++++++++++ compose.yaml | 18 +++++++++++++++++- infra/nginx/nginx.conf | 4 ++-- infra/nginx/run.sh | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 6 deletions(-) create mode 100644 app/migrate.py create mode 100755 infra/nginx/run.sh diff --git a/app/db.py b/app/db.py index 7fcac4b2..ebeb9a81 100644 --- a/app/db.py +++ b/app/db.py @@ -20,7 +20,6 @@ async def init_pool(dsn: str) -> asyncpg.Pool: command_timeout=5, timeout=5, ) - await _run_migrations(_pool) return _pool @@ -51,6 +50,14 @@ async def _run_migrations(p: asyncpg.Pool) -> None: await conn.execute("SELECT pg_advisory_unlock($1)", MIGRATION_LOCK_KEY) +async def run_migrations(dsn: str) -> None: + p = await asyncpg.create_pool(dsn, min_size=1, max_size=2, command_timeout=5, timeout=5) + try: + await _run_migrations(p) + finally: + await p.close() + + async def _apply_migrations(conn: asyncpg.Connection) -> None: await conn.execute( """ diff --git a/app/main.py b/app/main.py index f4190b34..ce0332e6 100644 --- a/app/main.py +++ b/app/main.py @@ -10,6 +10,7 @@ from datetime import datetime, timezone import asyncpg from fastapi import FastAPI, Header, Request +from fastapi.exceptions import RequestValidationError from fastapi.responses import JSONResponse, Response from pydantic import BaseModel, ConfigDict, Field, ValidationError @@ -34,6 +35,8 @@ async def tagging_middleware(request: Request, call_next): request_id = uuid.uuid4().hex request.state.request_id = request_id try: + if request.url.path.startswith("/api/") and not await _dependencies_ready(): + raise unavailable("database or session store not ready") response = await call_next(request) except ApiError as exc: response = _error_response(exc, request_id) @@ -44,6 +47,10 @@ async def tagging_middleware(request: Request, call_next): return response +async def _dependencies_ready() -> bool: + return await db.ping() and await sessions.ping() + + def _error_response(exc: ApiError, request_id: str) -> JSONResponse: headers = {} if exc.status == 401: @@ -66,6 +73,14 @@ async def api_error_handler(request: Request, exc: ApiError): return _error_response(exc, getattr(request.state, "request_id", uuid.uuid4().hex)) +@app.exception_handler(RequestValidationError) +async def request_validation_error_handler(request: Request, exc: RequestValidationError): + return _error_response( + bad_request("invalid request parameters"), + getattr(request.state, "request_id", uuid.uuid4().hex), + ) + + def iso(value: datetime) -> str: return value.astimezone(timezone.utc).isoformat().replace("+00:00", "Z") @@ -111,7 +126,10 @@ def decode_cursor(value: str) -> dict: try: padded = value + "=" * (-len(value) % 4) raw = base64.urlsafe_b64decode(padded.encode()) - return json.loads(raw) + payload = json.loads(raw) + if not isinstance(payload, dict): + raise bad_request("malformed cursor") + return payload except (binascii.Error, ValueError, UnicodeDecodeError): raise bad_request("malformed cursor") @@ -210,7 +228,7 @@ async def health_ready(): @app.post("/api/v1/users", status_code=201) async def register_user(request: Request): body = await parse_body(request, CreateUser) - if not USERNAME_RE.match(body.username): + if not USERNAME_RE.fullmatch(body.username): raise bad_request("username must match ^[a-z0-9_]{3,32}$") if not body.display_name.strip(): raise bad_request("display_name must not be blank") diff --git a/app/migrate.py b/app/migrate.py new file mode 100644 index 00000000..67e3b66b --- /dev/null +++ b/app/migrate.py @@ -0,0 +1,24 @@ +"""Run database migrations as a dedicated deployment step.""" +import asyncio +import os +from pathlib import Path + +from . import db + + +def read_secret(name: str, fallback: str) -> str: + path = os.environ.get(name) + if path: + return Path(path).read_text(encoding="utf-8").strip() + value = os.environ.get(fallback) + if not value: + raise RuntimeError(f"{name} or {fallback} is required") + return value + + +async def main() -> None: + await db.run_migrations(read_secret("DATABASE_URL_FILE", "DATABASE_URL")) + + +if __name__ == "__main__": + asyncio.run(main()) diff --git a/compose.yaml b/compose.yaml index e720980e..12f2b987 100644 --- a/compose.yaml +++ b/compose.yaml @@ -6,7 +6,9 @@ services: ports: - "8080:8080" volumes: - - ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf:ro + - ./infra/nginx/nginx.conf:/etc/nginx/nginx.conf.template:ro + - ./infra/nginx/run.sh:/usr/local/bin/run-nginx.sh:ro + command: ["/bin/sh", "/usr/local/bin/run-nginx.sh"] depends_on: api1: condition: service_healthy @@ -33,6 +35,8 @@ services: - database_url - cache_url depends_on: + migrate: + condition: service_completed_successfully postgres: condition: service_healthy redis: @@ -55,6 +59,18 @@ services: <<: *api-env INSTANCE_ID: api2 + migrate: + build: . + image: mtusi2026lab3-api:local + command: ["python", "-m", "app.migrate"] + environment: + DATABASE_URL_FILE: /run/secrets/database_url + secrets: + - database_url + depends_on: + postgres: + condition: service_healthy + postgres: image: postgres:16.4 environment: diff --git a/infra/nginx/nginx.conf b/infra/nginx/nginx.conf index fe61a050..353499e4 100644 --- a/infra/nginx/nginx.conf +++ b/infra/nginx/nginx.conf @@ -8,8 +8,8 @@ http { upstream messenger_api { # Passive health check: a backend failing 1 probe is removed for 3s, # which keeps failover detection well under the 10s budget. - server api1:8080 max_fails=1 fail_timeout=3s; - server api2:8080 max_fails=1 fail_timeout=3s; + server API1_ADDRESS max_fails=1 fail_timeout=3s; + server API2_ADDRESS max_fails=1 fail_timeout=3s; } server { diff --git a/infra/nginx/run.sh b/infra/nginx/run.sh new file mode 100755 index 00000000..a9358a91 --- /dev/null +++ b/infra/nginx/run.sh @@ -0,0 +1,35 @@ +#!/bin/sh +set -eu + +template=/etc/nginx/nginx.conf.template +rendered=/tmp/nginx.conf +last_addresses= + +resolve_address() { + getent hosts "$1" | awk 'NR == 1 { print $1 ":8080" }' +} + +render() { + api1="$(resolve_address api1 || true)" + api2="$(resolve_address api2 || true)" + [ -n "$api1" ] || api1="127.0.0.1:9" + [ -n "$api2" ] || api2="127.0.0.1:9" + printf '%s\n' "$api1 $api2" + sed -e "s/API1_ADDRESS/$api1/" -e "s/API2_ADDRESS/$api2/" "$template" > "$rendered" +} + +current="$(render)" +last_addresses="$current" +nginx -c "$rendered" -g 'daemon off;' & +nginx_pid=$! +trap 'kill "$nginx_pid" 2>/dev/null || true; wait "$nginx_pid" 2>/dev/null || true' INT TERM EXIT + +while kill -0 "$nginx_pid" 2>/dev/null; do + sleep 2 + current="$(render)" + if [ "$current" != "$last_addresses" ]; then + nginx -s reload -c "$rendered" + last_addresses="$current" + fi +done +wait "$nginx_pid"