add datastar and render shim

This commit is contained in:
Abel Luck 2026-03-30 12:27:45 +02:00
parent 9ce576e7e8
commit 2accb26546
6 changed files with 173 additions and 42 deletions

View file

@ -1,15 +1,43 @@
from __future__ import annotations
from quart import Quart, url_for
import hashlib
from repub.pages import admin_page
import htpy as h
from quart import Quart, Response, request, url_for
from repub.pages import admin_component, shim_page
def _render_shim_page(*, stylesheet_href: str, datastar_src: str) -> tuple[str, str]:
head = (
h.title["Republisher Admin UI"],
h.link(rel="stylesheet", href=stylesheet_href),
)
body = str(shim_page(datastar_src=datastar_src, head=head))
etag = hashlib.sha256(body.encode("utf-8")).hexdigest()
return body, etag
def create_app() -> Quart:
app = Quart(__name__)
@app.get("/")
async def index() -> str:
return str(admin_page(stylesheet_href=url_for("static", filename="app.css")))
async def index() -> Response:
body, etag = _render_shim_page(
stylesheet_href=url_for("static", filename="app.css"),
datastar_src=url_for("static", filename="datastar@1.0.0-RC.8.js"),
)
if request.if_none_match.contains(etag):
response = Response(status=304)
response.set_etag(etag)
return response
response = Response(body, mimetype="text/html")
response.set_etag(etag)
return response
@app.post("/")
async def index_patch() -> Response:
return Response(str(admin_component()), mimetype="text/html")
return app