"""Health, session bootstrap and freight quoting.""" import os from fastapi import APIRouter, HTTPException, Request, Response from ..core import db from ..core.auth import client_ip, owner, new_session, rate_limit from ..core.models import Freight from ..runtime import ENVIRONMENT, GUEST_SESSION_LIMIT, PART_BYTES, freight, storage router = APIRouter() @router.get('/health') @router.get('/api/health') def health(): try: with db.connect() as c: c.execute('SELECT 1') storage.health() except Exception: raise HTTPException(503, 'Database or storage unavailable') return {'status': 'ok', 'environment': ENVIRONMENT, 'storage': 'minio' if ENVIRONMENT == 'local' else 'r2', 'integrations': 'fake'} @router.get('/api/session') def session(request: Request, response: Response): try: session_id = owner(request) except HTTPException: # Per source, not per deployment: keyed on the environment name this was a # single global bucket, so ~8 new visitors a minute exhausted it site-wide. rate_limit('guest-sessions', client_ip(request), GUEST_SESSION_LIMIT, 900) with db.connect() as c: session_id = new_session(c, response) return {'environment': ENVIRONMENT, 'cart_scope': str(session_id), 'part_bytes': PART_BYTES, 'max_upload_bytes': int(os.environ.get('MAX_UPLOAD_BYTES', '5368709120'))} @router.post('/api/freight') def quote_freight(body: Freight): try: return freight.quote(body.service, body.postal_code) except ValueError as exc: raise HTTPException(422, str(exc))