Everything needed to run the app on an Intel NAS that is also doing other jobs: one published port, no host networking, memory/CPU caps and log rotation on every container, and all state in bind mounts under DATA_ROOT so mail survives image updates, project deletion in the NAS UI, and full reinstalls — and so Hyper Backup/HBS3 can actually see it. - docker/Dockerfile.backend: multi-stage, non-root (uid 10001), tini for the <10s graceful lease release. Installs the four plugins as entry-point distributions. One image, several roles via docker/entrypoint.sh (api | worker-sync | worker-jobs | migrate | reindex | plugins). - docker/Dockerfile.frontend + nginx.conf: SPA + /api reverse proxy with the WebSocket upgrade for /api/ws, and X-Forwarded-Proto preserved so the API still emits __Host-/Secure cookies when TLS is terminated by the NAS reverse proxy (identity/cookies.py keys off request scheme). - docker/docker-compose.prod.yml: project name webmail-prod, NOT webmail — docker-compose.yml (dev services) owns that name, and two compose files sharing one project name fight over each other's containers. Meilisearch is an opt-in profile; SEARCH_BACKEND=pg is the default so a small NAS pays nothing for search. - migrate runs as a one-shot (core Alembic head + every plugin branch) that everything else waits on, so a bad migration can never leave new code running against an old schema. - docker/backup.sh: nightly pg_dump verified with `pg_restore --list` before it counts as a backup, with retention and a baseline on first start. - deploy/webmail.sh: init/doctor/up/down/status/logs/update/rollback/ backup/restore/reindex/psql/shell. `update` is backup -> images -> migrate -> roll workers, api, web -> health-gate, with automatic rollback to the previous version if the gate fails. `doctor` refuses to be quiet when DATA_ROOT is on NFS/SMB (silent Postgres corruption). - deploy/build-push.sh: build images off-NAS (the frontend build wants ~2GB RAM) and push to Gitea, or --save a tar to carry over. - scripts/reindex_search.py: rebuild the search index from Postgres alone. Every SearchDoc field is already stored, so the index is derived data that never needs backing up — this closes the only real objection to running Meilisearch on a box whose volumes may be wiped. - pyproject: package-data for accounts/presets.yaml and the shared plugin Alembic env. Without it a non-editable install (i.e. any image) silently ships a package that fails at runtime. - GET /api/version, used by the deploy health gate. Verified end to end on a real stack: fresh install, all services healthy, core + 4 plugin migration branches applied, login/SPA/plugin bundles through nginx, WS upgrade returning 101, __Host- cookies under a simulated TLS proxy, backup -> restore round-trip preserving user and plugin schema, failed-update auto-rollback (both a bad migration and a boot-crashing API), and Meilisearch profile serving a live query. ruff + mypy --strict clean, 363/363 backend tests pass. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Nux3LCu4zP9nih4BRhPyHw
57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
"""FastAPI application factory (SPEC §2 ``app/main.py``)."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from collections.abc import AsyncIterator
|
|
from contextlib import asynccontextmanager
|
|
|
|
from fastapi import FastAPI
|
|
from sqlalchemy import text
|
|
|
|
from app.config import get_settings
|
|
from app.db import dispose_engine, get_engine
|
|
from app.redis_client import close_redis, get_redis
|
|
|
|
|
|
@asynccontextmanager
|
|
async def lifespan(app: FastAPI) -> AsyncIterator[None]:
|
|
"""Warm shared resources on startup, dispose them on shutdown."""
|
|
# Touch settings early so misconfiguration fails fast.
|
|
get_settings()
|
|
# Subscribe plugin hooks so events published in the API process reach them
|
|
# (worker-published events are handled by the worker's own registration).
|
|
from app.events import get_event_bus
|
|
from app.plugins import loader
|
|
|
|
loader.register_hooks(get_event_bus())
|
|
yield
|
|
await close_redis()
|
|
await dispose_engine()
|
|
|
|
|
|
def create_app() -> FastAPI:
|
|
"""Construct the FastAPI application."""
|
|
app = FastAPI(title="Webmail", version="0.1.0", lifespan=lifespan)
|
|
|
|
from app.api.errors import register_error_handlers
|
|
|
|
register_error_handlers(app)
|
|
|
|
@app.get("/api/healthz")
|
|
async def healthz() -> dict[str, str]:
|
|
"""Liveness/readiness probe: checks DB and Redis (dev-setup §6)."""
|
|
async with get_engine().connect() as conn:
|
|
await conn.execute(text("SELECT 1"))
|
|
await get_redis().ping()
|
|
return {"status": "ok"}
|
|
|
|
@app.get("/api/version")
|
|
async def version() -> dict[str, str]:
|
|
"""Report the running release, for deploy/rollback verification."""
|
|
return {"version": get_settings().app_version}
|
|
|
|
from app.api import register_routers
|
|
|
|
register_routers(app)
|
|
return app
|