Files
wiggleverse-ecomm/backend/app/platform/deps.py
T
2026-06-10 10:34:13 -07:00

39 lines
1.2 KiB
Python

"""FastAPI dependencies — per-request wiring (SD-0001 §6.2 platform/deps).
Yields a pooled connection per request, the process-wide mailer, and the verified session
payload (from the signed cookie). The pool and mailer live on app.state, built once in
create_app(). The storefronts dependency lands in SLICE-3.
"""
from __future__ import annotations
from collections.abc import Iterator
from typing import Any
import psycopg
from fastapi import Request
from app.platform import config, session
from app.platform.mailer import Mailer
SESSION_COOKIE = "ecomm_session"
def get_conn(request: Request) -> Iterator[psycopg.Connection]:
"""A pooled connection for the duration of one request."""
pool = request.app.state.pool
with pool.connection() as conn:
yield conn
def get_mailer(request: Request) -> Mailer:
"""The process-wide mailer adapter (built from config at startup, INV-8)."""
return request.app.state.mailer
def get_session(request: Request) -> dict[str, Any] | None:
"""The verified session payload from the cookie, or None if absent/invalid."""
token = request.cookies.get(SESSION_COOKIE)
if not token:
return None
return session.verify(token, config.session_secret())