feat: extract shared infrastructure from shared_lib

Phase 1-3 of decoupling plan:
- Shared DB, models, infrastructure, browser, config, utils
- Event infrastructure (domain_events outbox, bus, processor)
- Structured logging
- Generic container concept (container_type/container_id)
- Alembic migrations for all schema changes

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
giles
2026-02-11 12:45:56 +00:00
commit ef806f8fbb
533 changed files with 276497 additions and 0 deletions

View File

@@ -0,0 +1,12 @@
from .parse import (
parse_time,
parse_cost,
parse_dt
)
from .utils import (
current_route_relative_path,
current_url_without_page,
vary,
)
from .utc import utcnow

46
browser/app/utils/htmx.py Normal file
View File

@@ -0,0 +1,46 @@
"""HTMX utilities for detecting and handling HTMX requests."""
from quart import request
def is_htmx_request() -> bool:
"""
Check if the current request is an HTMX request.
Returns:
bool: True if HX-Request header is present and true
"""
return request.headers.get("HX-Request", "").lower() == "true"
def get_htmx_target() -> str | None:
"""
Get the target element ID from HTMX request headers.
Returns:
str | None: Target element ID or None
"""
return request.headers.get("HX-Target")
def get_htmx_trigger() -> str | None:
"""
Get the trigger element ID from HTMX request headers.
Returns:
str | None: Trigger element ID or None
"""
return request.headers.get("HX-Trigger")
def should_return_fragment() -> bool:
"""
Determine if we should return a fragment vs full page.
For HTMX requests, return fragment.
For normal requests, return full page.
Returns:
bool: True if fragment should be returned
"""
return is_htmx_request()

View File

@@ -0,0 +1,36 @@
from datetime import datetime, timezone
def parse_time(val: str | None):
if not val:
return None
try:
h,m = val.split(':', 1)
from datetime import time
return time(int(h), int(m))
except Exception:
return None
def parse_cost(val: str | None):
if not val:
return None
try:
return float(val)
except Exception:
return None
if not val:
return None
dt = datetime.fromisoformat(val)
# make TZ-aware (assume local if naive; convert to UTC)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt
def parse_dt(val: str | None) -> datetime | None:
if not val:
return None
dt = datetime.fromisoformat(val)
if dt.tzinfo is None:
dt = dt.replace(tzinfo=timezone.utc)
return dt

6
browser/app/utils/utc.py Normal file
View File

@@ -0,0 +1,6 @@
from datetime import datetime, timezone
def utcnow() -> datetime:
return datetime.now(timezone.utc)

View File

@@ -0,0 +1,51 @@
from quart import (
Response,
request,
g,
)
from shared.utils import host_url
from urllib.parse import urlencode
def current_route_relative_path() -> str:
"""
Returns the current request path relative to the app's mount point (script_root).
"""
(request.script_root or "").rstrip("/")
path = request.path # excludes query string
if g.root and path.startswith(f"/{g.root}"):
rel = path[len(g.root+1):]
return rel if rel.startswith("/") else "/" + rel
return path # app at /
def current_url_without_page() -> str:
"""
Build current URL (host+path+qs) but with ?page= removed.
Used for Hx-Push-Url.
"""
base = host_url(current_route_relative_path())
params = request.args.to_dict(flat=False) # keep multivals
params.pop("page", None)
qs = urlencode(params, doseq=True)
return f"{base}?{qs}" if qs else base
def vary(resp: Response) -> Response:
"""
Ensure caches/CDNs vary on HX headers so htmx/non-htmx versions don't get mixed.
"""
v = resp.headers.get("Vary", "")
parts = [p.strip() for p in v.split(",") if p.strip()]
for h in ("HX-Request", "X-Origin"):
if h not in parts:
parts.append(h)
if parts:
resp.headers["Vary"] = ", ".join(parts)
return resp