File size: 732 Bytes
ddebc3e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
"""Helpers for serving static HTML with lightweight version injection."""

from pathlib import Path

from fastapi import HTTPException
from fastapi.responses import HTMLResponse

from app.platform.meta import get_project_version


_VERSION_TOKEN = "{{APP_VERSION}}"


def serve_static_html(path: Path) -> HTMLResponse:
    """Serve an HTML file, replacing the version token if present."""
    if not path.exists():
        raise HTTPException(status_code=404, detail="Page not found")

    body = path.read_text(encoding="utf-8")
    if _VERSION_TOKEN in body:
        body = body.replace(_VERSION_TOKEN, get_project_version())

    return HTMLResponse(body, headers={"Cache-Control": "no-store"})


__all__ = ["serve_static_html"]