Add coop fragment middleware and env vars
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Some checks failed
Build and Deploy / build-and-deploy (push) Has been cancelled
Fetch nav-tree, auth-menu, cart-mini from coop apps for unified header. Add INTERNAL_URL env vars for Docker networking. Update base.html to render fragment blocks. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -10,6 +10,7 @@ from fastapi import FastAPI, Request
|
|||||||
from fastapi.responses import JSONResponse, HTMLResponse
|
from fastapi.responses import JSONResponse, HTMLResponse
|
||||||
|
|
||||||
from artdag_common import create_jinja_env
|
from artdag_common import create_jinja_env
|
||||||
|
from artdag_common.middleware.auth import get_user_from_cookie
|
||||||
|
|
||||||
from .config import settings
|
from .config import settings
|
||||||
|
|
||||||
@@ -37,6 +38,44 @@ def create_app() -> FastAPI:
|
|||||||
lifespan=lifespan,
|
lifespan=lifespan,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Coop fragment pre-fetch — inject nav-tree, auth-menu, cart-mini
|
||||||
|
_FRAG_SKIP = ("/auth/", "/.well-known/", "/health",
|
||||||
|
"/internal/", "/static/", "/inbox")
|
||||||
|
|
||||||
|
@app.middleware("http")
|
||||||
|
async def coop_fragments_middleware(request: Request, call_next):
|
||||||
|
path = request.url.path
|
||||||
|
if (
|
||||||
|
request.method != "GET"
|
||||||
|
or any(path.startswith(p) for p in _FRAG_SKIP)
|
||||||
|
or request.headers.get("hx-request")
|
||||||
|
):
|
||||||
|
request.state.nav_tree_html = ""
|
||||||
|
request.state.auth_menu_html = ""
|
||||||
|
request.state.cart_mini_html = ""
|
||||||
|
return await call_next(request)
|
||||||
|
|
||||||
|
from artdag_common.fragments import fetch_fragments as _fetch_frags
|
||||||
|
|
||||||
|
user = get_user_from_cookie(request)
|
||||||
|
auth_params = {"email": user.email} if user and user.email else {}
|
||||||
|
nav_params = {"app_name": "artdag", "path": path}
|
||||||
|
|
||||||
|
try:
|
||||||
|
nav_tree_html, auth_menu_html, cart_mini_html = await _fetch_frags([
|
||||||
|
("blog", "nav-tree", nav_params),
|
||||||
|
("account", "auth-menu", auth_params or None),
|
||||||
|
("cart", "cart-mini", None),
|
||||||
|
])
|
||||||
|
except Exception:
|
||||||
|
nav_tree_html = auth_menu_html = cart_mini_html = ""
|
||||||
|
|
||||||
|
request.state.nav_tree_html = nav_tree_html
|
||||||
|
request.state.auth_menu_html = auth_menu_html
|
||||||
|
request.state.cart_mini_html = cart_mini_html
|
||||||
|
|
||||||
|
return await call_next(request)
|
||||||
|
|
||||||
# Initialize Jinja2 templates
|
# Initialize Jinja2 templates
|
||||||
template_dir = Path(__file__).parent / "templates"
|
template_dir = Path(__file__).parent / "templates"
|
||||||
app.state.templates = create_jinja_env(template_dir)
|
app.state.templates = create_jinja_env(template_dir)
|
||||||
|
|||||||
@@ -8,6 +8,30 @@
|
|||||||
<span class="text-stone-600 text-3xl">L2</span>
|
<span class="text-stone-600 text-3xl">L2</span>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block cart_mini %}
|
||||||
|
{% if request and request.state.cart_mini_html %}
|
||||||
|
{{ request.state.cart_mini_html | safe }}
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block nav_tree %}
|
||||||
|
{% if request and request.state.nav_tree_html %}
|
||||||
|
{{ request.state.nav_tree_html | safe }}
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block auth_menu %}
|
||||||
|
{% if request and request.state.auth_menu_html %}
|
||||||
|
{{ request.state.auth_menu_html | safe }}
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
|
{% block auth_menu_mobile %}
|
||||||
|
{% if request and request.state.auth_menu_html %}
|
||||||
|
{{ request.state.auth_menu_html | safe }}
|
||||||
|
{% endif %}
|
||||||
|
{% endblock %}
|
||||||
|
|
||||||
{% block sub_nav %}
|
{% block sub_nav %}
|
||||||
<div class="bg-stone-200 border-b border-stone-300">
|
<div class="bg-stone-200 border-b border-stone-300">
|
||||||
<div class="max-w-screen-2xl mx-auto px-4">
|
<div class="max-w-screen-2xl mx-auto px-4">
|
||||||
|
|||||||
Reference in New Issue
Block a user