From c983a9a48034da163d16ea3cdbad707da63ea598 Mon Sep 17 00:00:00 2001 From: ZTatman Date: Fri, 24 Apr 2026 14:30:49 -0400 Subject: [PATCH] feat(web): add FastAPI service with async SQLAlchemy and Docker setup - Add Grant model with proper typing (uuid.UUID, timezone-aware datetime) - Add DB enum for grant_type to enforce valid values at database level - Configure SQLAlchemy Base with datetime timezone mapping - Harden Dockerfile: pin uv version, non-root user, security env vars - Update docker-compose with correct healthcheck database name - Add default database_url for Docker Compose deployment - Add PYTHONUNBUFFERED and PYTHONDONTWRITEBYTECODE env vars - Clean up .gitignore (merged into root) --- .gitignore | 6 ++++++ docker-compose.yml | 2 +- web/.gitignore | 3 --- web/Dockerfile | 15 +++++++++++---- web/app/config.py | 5 +++-- web/app/models/grant.py | 19 ++++++++++++------- 6 files changed, 33 insertions(+), 17 deletions(-) delete mode 100644 web/.gitignore diff --git a/.gitignore b/.gitignore index 0e9aa1a..38a9d60 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +# General node_modules/ .DS_Store .tmp/ @@ -7,3 +8,8 @@ coverage/ .env.* !.env.example *.tsbuildinfo + +# Python +web/.venv/ +web/__pycache__/ +web/*.py[cod] \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 759c4e0..7444a00 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,7 +20,7 @@ services: volumes: - postgres_data:/var/lib/postgresql/data healthcheck: - test: ["CMD-SHELL", "pg_isready -U myst -d myst"] + test: ["CMD-SHELL", "pg_isready -U myst -d myst_db"] interval: 5s timeout: 5s retries: 5 diff --git a/web/.gitignore b/web/.gitignore deleted file mode 100644 index f0ccc32..0000000 --- a/web/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -.venv/ -__pycache__/ -*.py[cod] diff --git a/web/Dockerfile b/web/Dockerfile index 308424a..9b6a89b 100644 --- a/web/Dockerfile +++ b/web/Dockerfile @@ -1,13 +1,20 @@ FROM python:3.13-slim -COPY --from=ghcr.io/astral-sh/uv:latest /uv /uvx /bin/ +COPY --from=ghcr.io/astral-sh/uv:0.7.4 /uv /uvx /bin/ WORKDIR /app -COPY pyproject.toml uv.lock ./ -RUN UV_NO_DEV=1 uv sync --frozen --no-cache +ENV PYTHONUNBUFFERED=1 +ENV PYTHONDONTWRITEBYTECODE=1 -COPY . . +RUN useradd --create-home --shell /bin/bash appuser + +COPY --chown=appuser:appuser pyproject.toml uv.lock ./ +RUN UV_NO_DEV=1 uv sync --frozen --no-cache && chown -R appuser:appuser /app + +COPY --chown=appuser:appuser . . + +USER appuser EXPOSE 8000 diff --git a/web/app/config.py b/web/app/config.py index 39f35f4..74ca59d 100644 --- a/web/app/config.py +++ b/web/app/config.py @@ -1,6 +1,7 @@ from functools import lru_cache from pathlib import Path +from pydantic import SecretStr from pydantic_settings import BaseSettings, SettingsConfigDict env_file = Path(__file__).parent.parent / ".env" @@ -10,9 +11,9 @@ class Settings(BaseSettings): """Application settings loaded from environment variables.""" app_name: str = "Myst" - database_url: str = "postgresql+asyncpg://myst:myst@db:5432/myst_db" + database_url: str = "postgresql+asyncpg://myst:myst@db:5432/myst_db" # Default for Docker Compose; override with DATABASE_URL env var in production forgejo_base_url: str = "" - forgejo_pat: str = "" + forgejo_pat: SecretStr = SecretStr("") model_config = SettingsConfigDict(env_file=str(env_file)) diff --git a/web/app/models/grant.py b/web/app/models/grant.py index a62170c..763c76d 100644 --- a/web/app/models/grant.py +++ b/web/app/models/grant.py @@ -1,7 +1,8 @@ +import uuid from datetime import datetime -from typing import Literal +from typing import Literal, get_args -from sqlalchemy import DateTime, String, Uuid, func +from sqlalchemy import Enum, String, Uuid, func from sqlalchemy.orm import Mapped, mapped_column from app.database import Base @@ -12,16 +13,20 @@ GrantType = Literal["public", "private"] class Grant(Base): __tablename__ = "grants" - id: Mapped[str] = mapped_column( - Uuid, primary_key=True, server_default=func.gen_random_uuid() + id: Mapped[uuid.UUID] = mapped_column( + Uuid, + primary_key=True, + server_default=func.gen_random_uuid(), # Postgres-specific (native on 13+) ) token: Mapped[str] = mapped_column(String, unique=True, nullable=False, index=True) - created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) - revoked_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) + created_at: Mapped[datetime] = mapped_column(server_default=func.now()) + revoked_at: Mapped[datetime | None] = mapped_column(nullable=True) repo_owner: Mapped[str] = mapped_column(String, nullable=False) repo_name: Mapped[str] = mapped_column(String, nullable=False) commit_sha: Mapped[str] = mapped_column(String, nullable=False) grant_type: Mapped[GrantType] = mapped_column( - String, nullable=False, default="public" + Enum(*get_args(GrantType), name="grant_type_enum"), + nullable=False, + default="public", ) recipient_email: Mapped[str | None] = mapped_column(String, nullable=True)