43 lines
1.6 KiB
Python
43 lines
1.6 KiB
Python
"""Health, session bootstrap and freight quoting."""
|
|
from fastapi import APIRouter, HTTPException, Request, Response
|
|
|
|
from ..core import db
|
|
from ..core.auth import client_ip, owner, new_session, rate_limit
|
|
from ..core.limits import upload_limit_bytes
|
|
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': upload_limit_bytes()}
|
|
|
|
@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))
|