#!/usr/bin/env python3 """Serve only the contents of `src/static/` (HTML, CSS, JS, bundled stubs). Use this with **local mode** in the editor (`?local=1`): files live in IndexedDB, so no FastAPI file API is required. Maps `/` and `/editor` to `home.html` / `index.html` so links from the home page keep working. Sends ``Cross-Origin-Opener-Policy: same-origin`` and ``Cross-Origin-Embedder-Policy: credentialless`` on every response so the page can become cross-origin isolated (SharedArrayBuffer for live ADC / pins / serial), matching the full FastAPI app. Example: python scripts/serve_static_editor.py # open http://127.0.0.1:8765/ then "Use locally" → /editor?local=1 Note: Pyodide and CodeMirror still load from CDNs; you need network access. """ from __future__ import annotations import argparse from functools import partial from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer from pathlib import Path def main() -> None: parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0]) parser.add_argument("--host", default="127.0.0.1", help="Bind address (default 127.0.0.1)") parser.add_argument("--port", type=int, default=8765, help="Port (default 8765)") args = parser.parse_args() static_root = (Path(__file__).resolve().parent.parent / "src" / "static").resolve() if not static_root.is_dir(): raise SystemExit(f"Static directory not found: {static_root}") class Handler(SimpleHTTPRequestHandler): def __init__(self, *a, **kw): super().__init__(*a, directory=str(static_root), **kw) def end_headers(self): self.send_header("Cross-Origin-Opener-Policy", "same-origin") self.send_header("Cross-Origin-Embedder-Policy", "credentialless") super().end_headers() def translate_path(self, path: str) -> str: clean = path.split("?", 1)[0].split("#", 1)[0] if clean.startswith("/static/"): clean = clean[len("/static") :] # e.g. /styles.css, /bundled-lib/machine.py if clean in ("/", ""): clean = "/home.html" elif clean == "/editor" or clean.startswith("/editor/"): clean = "/index.html" elif clean == "/tutorial" or clean.startswith("/tutorial/"): clean = "/tutorial.html" elif clean == "/login" or clean.startswith("/login/"): clean = "/login.html" elif clean == "/register" or clean.startswith("/register/"): clean = "/register.html" return super().translate_path(clean) httpd = ThreadingHTTPServer((args.host, args.port), Handler) print(f"Serving {static_root} at http://{args.host}:{args.port}/") print("Open / then use “Use locally” for IndexedDB workspace.") try: httpd.serve_forever() except KeyboardInterrupt: print("\nStopped.") if __name__ == "__main__": main()