#!/usr/bin/env python3
"""Fast static server for dist/. Mirrors the live cache headers so reloads
don't re-download the ~100MB data. `python serve.py` -> http://localhost:8777/7kaa.html"""
import http.server, socketserver, os

PORT = 8777
os.chdir(os.path.join(os.path.dirname(os.path.abspath(__file__)), "dist"))

class H(http.server.SimpleHTTPRequestHandler):
    extensions_map = {
        **http.server.SimpleHTTPRequestHandler.extensions_map,
        ".wasm": "application/wasm",
        ".js": "text/javascript",
        ".data": "application/octet-stream",
        ".json": "application/json",
    }
    def end_headers(self):
        p = self.path.split("?")[0]
        if p.endswith((".wasm", ".data", ".js")):
            self.send_header("Cache-Control", "public, max-age=31536000, immutable")
        else:
            self.send_header("Cache-Control", "no-store")
        super().end_headers()

class TS(socketserver.ThreadingMixIn, http.server.HTTPServer):
    daemon_threads = True
    block_on_close = False

with TS(("", PORT), H) as httpd:
    print(f"serving dist/ at http://localhost:{PORT}/7kaa.html")
    httpd.serve_forever()
