myst/web/app/config.py
ZTatman 8b7f2664d0 refactor(web): expose app settings as required/optional FastAPI deps
Mirror the session.py dependency trio: current_app_settings (honest
optional accessor), get_app_settings (required; raises 500 if reached
unconfigured), and set_app_settings (single write path). Middleware reads
through the accessor instead of poking app.state directly.

get_app_settings takes request and calls current_app_settings internally
rather than declaring it as a sub-Depends — a Depends param typed with a
TYPE_CHECKING-only forward ref makes FastAPI drop the marker and treat it
as a query param.
2026-06-10 17:03:10 -04:00

88 lines
3 KiB
Python

from __future__ import annotations
from functools import lru_cache
from pathlib import Path
from typing import TYPE_CHECKING
from cryptography.fernet import Fernet
from fastapi import FastAPI, HTTPException, Request, status
from pydantic import SecretStr
from pydantic_settings import BaseSettings, SettingsConfigDict
from app.auth.crypto import fernet_from_encryption_key
if TYPE_CHECKING:
from app.models.app_settings import AppSettings
_env_file = Path(__file__).parent.parent / ".env"
class Settings(BaseSettings):
"""Bootstrap settings loaded from environment variables.
Only contains fields needed before the database is available.
Forgejo config lives in the AppSettings DB table after setup.
"""
app_name: str = "Firebreak"
database_url: str = "postgresql+asyncpg://firebreak:firebreak@db:5432/firebreak_db"
token_encryption_key: str = ""
debug: bool = False
model_config = SettingsConfigDict(env_file=str(_env_file))
@lru_cache
def get_settings() -> Settings:
"""Cached settings instance."""
return Settings()
@lru_cache
def get_fernet() -> Fernet:
"""Return a cached Fernet cipher from the TOKEN_ENCRYPTION_KEY env var."""
key = get_settings().token_encryption_key
if not key:
raise ValueError(
"TOKEN_ENCRYPTION_KEY is not set. Add it to your .env file or deployment "
"environment. Generate one with: uv run python -c "
"'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())'"
)
fernet = fernet_from_encryption_key(SecretStr(key))
if fernet is None:
raise ValueError(
"TOKEN_ENCRYPTION_KEY is invalid. Generate a valid key with: uv run python -c "
"'from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())'"
)
return fernet
def current_app_settings(request: Request) -> AppSettings | None:
"""Get AppSettings row, or None if not available.
This is the source of truth for the live AppSettings row
"""
return request.app.state.app_settings
def get_app_settings(request: Request) -> AppSettings:
"""FastAPI dependency that requires configured app settings.
Calls `current_app_settings` directly (rather than depending on it) so the
only injected parameter is the runtime-resolvable `request` — mirroring how
`get_current_session` calls `lookup_session`. Raises HTTP 500 when settings
are unavailable, which should only happen if a route that requires settings
is reached before setup completes (i.e. not gated by setup middleware).
"""
settings = current_app_settings(request)
if settings is None:
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail="App settings unavailable — route not gated by setup middleware.",
)
return settings
def set_app_settings(app: FastAPI, row: AppSettings | None) -> None:
"""Set application settings on the FastAPI app state."""
app.state.app_settings = row