web_server_static_assets.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. """Static asset lookups the SPA catch-all must not answer with ``index.html``.
  2. The sign-in page (``dashboard_auth/login_page.py``) loads its brand fonts from ``/fonts/``. Those
  3. files ship in the dashboard's ``web/public`` tree, which the dashboard build copies into its own
  4. dist, but the Webapp serves ``apps/desktop/dist-webapp`` and has no ``fonts/`` dir. A font missing
  5. from the served dist is read from the packaged dashboard dist (``HERMES_WEB_DIST`` / ``web_dist``,
  6. which is all a Nix or wheel install ships, since those exclude ``web/``), then from a source
  7. checkout's ``web/public``.
  8. """
  9. from __future__ import annotations
  10. from pathlib import Path
  11. # Requests under these prefixes are files, never client-side routes, so a miss is a 404 rather
  12. # than the SPA shell (a font answered with HTML renders in a fallback face, silently).
  13. STATIC_ASSET_PREFIXES: tuple[str, ...] = ("assets/", "ds-assets/", "fonts/", "fonts-terminal/")
  14. # Root page icons answer without sign-in, so a missing (or out-of-dist) icon must be a 404, not
  15. # the SPA shell served anonymously.
  16. STATIC_ASSET_FILES: frozenset[str] = frozenset({"apple-touch-icon.png", "favicon.ico"})
  17. _PUBLIC_FONT_DIRS: tuple[str, ...] = ("fonts", "fonts-terminal")
  18. _WEB_PUBLIC = Path(__file__).resolve().parent.parent / "web" / "public"
  19. def is_static_asset_path(full_path: str) -> bool:
  20. return full_path in STATIC_ASSET_FILES or full_path.startswith(STATIC_ASSET_PREFIXES)
  21. def public_font_file(dist: Path, dashboard_dist: Path, full_path: str) -> Path | None:
  22. """The served dist's copy of ``fonts/<name>`` (or ``fonts-terminal/<name>``), else the packaged
  23. dashboard dist's, else the ``web/public`` copy the dashboard build takes it from; ``None`` when
  24. none has it."""
  25. folder, _, name = full_path.partition("/")
  26. if folder not in _PUBLIC_FONT_DIRS or not name:
  27. return None
  28. for root in (dist / folder, dashboard_dist / folder, _WEB_PUBLIC / folder):
  29. candidate = root / name
  30. if candidate.is_file() and candidate.resolve().is_relative_to(root.resolve()):
  31. return candidate
  32. return None