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)
This commit is contained in:
ZTatman 2026-04-24 14:30:49 -04:00
parent 13ac417aa5
commit c983a9a480
6 changed files with 33 additions and 17 deletions

6
.gitignore vendored
View file

@ -1,3 +1,4 @@
# General
node_modules/ node_modules/
.DS_Store .DS_Store
.tmp/ .tmp/
@ -7,3 +8,8 @@ coverage/
.env.* .env.*
!.env.example !.env.example
*.tsbuildinfo *.tsbuildinfo
# Python
web/.venv/
web/__pycache__/
web/*.py[cod]

View file

@ -20,7 +20,7 @@ services:
volumes: volumes:
- postgres_data:/var/lib/postgresql/data - postgres_data:/var/lib/postgresql/data
healthcheck: healthcheck:
test: ["CMD-SHELL", "pg_isready -U myst -d myst"] test: ["CMD-SHELL", "pg_isready -U myst -d myst_db"]
interval: 5s interval: 5s
timeout: 5s timeout: 5s
retries: 5 retries: 5

3
web/.gitignore vendored
View file

@ -1,3 +0,0 @@
.venv/
__pycache__/
*.py[cod]

View file

@ -1,13 +1,20 @@
FROM python:3.13-slim 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 WORKDIR /app
COPY pyproject.toml uv.lock ./ ENV PYTHONUNBUFFERED=1
RUN UV_NO_DEV=1 uv sync --frozen --no-cache 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 EXPOSE 8000

View file

@ -1,6 +1,7 @@
from functools import lru_cache from functools import lru_cache
from pathlib import Path from pathlib import Path
from pydantic import SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, SettingsConfigDict
env_file = Path(__file__).parent.parent / ".env" env_file = Path(__file__).parent.parent / ".env"
@ -10,9 +11,9 @@ class Settings(BaseSettings):
"""Application settings loaded from environment variables.""" """Application settings loaded from environment variables."""
app_name: str = "Myst" 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_base_url: str = ""
forgejo_pat: str = "" forgejo_pat: SecretStr = SecretStr("")
model_config = SettingsConfigDict(env_file=str(env_file)) model_config = SettingsConfigDict(env_file=str(env_file))

View file

@ -1,7 +1,8 @@
import uuid
from datetime import datetime 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 sqlalchemy.orm import Mapped, mapped_column
from app.database import Base from app.database import Base
@ -12,16 +13,20 @@ GrantType = Literal["public", "private"]
class Grant(Base): class Grant(Base):
__tablename__ = "grants" __tablename__ = "grants"
id: Mapped[str] = mapped_column( id: Mapped[uuid.UUID] = mapped_column(
Uuid, primary_key=True, server_default=func.gen_random_uuid() 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) token: Mapped[str] = mapped_column(String, unique=True, nullable=False, index=True)
created_at: Mapped[datetime] = mapped_column(DateTime, server_default=func.now()) created_at: Mapped[datetime] = mapped_column(server_default=func.now())
revoked_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True) revoked_at: Mapped[datetime | None] = mapped_column(nullable=True)
repo_owner: Mapped[str] = mapped_column(String, nullable=False) repo_owner: Mapped[str] = mapped_column(String, nullable=False)
repo_name: 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) commit_sha: Mapped[str] = mapped_column(String, nullable=False)
grant_type: Mapped[GrantType] = mapped_column( 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) recipient_email: Mapped[str | None] = mapped_column(String, nullable=True)