feat(slice-2): deps — mailer + session dependencies; build mailer at startup

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-06-10 10:34:13 -07:00
parent 95680c9960
commit d2eceac272
2 changed files with 133 additions and 8 deletions
+22 -2
View File
@@ -1,18 +1,38 @@
"""FastAPI dependencies — per-request wiring (SD-0001 §6.2 platform/deps).
Yields a pooled connection per request. The pool lives on app.state, built once in
create_app(). Later slices add the current-session and mailer dependencies here.
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())