aienos.com
Source of www.aienos.com, served from my own hardware
Brought by drakestapleton
aienos.com / scripts / serve-like-hub.py
| 1 | #!/usr/bin/env python3 |
| 2 | """Serve a built dist/ the way the hub's nginx serves the release, for the local gates. |
| 3 | |
| 4 | nginx on the hub gzips text/html only (its default gzip_types; measured 2026-11-10 from the MacBook: |
| 5 | /classic/ is sent as 24,988 gzip bytes, /assets/style.css as 50,267 plain bytes). python3 -m |
| 6 | http.server sends every file plain, so the throttled perf pass of scripts/test-site-browser.mjs |
| 7 | measured HTML at about 3.7 times its real transfer size. This server sends text/html gzip-compressed |
| 8 | when the client accepts gzip and every other type plain, like the hub. Everything else (directory |
| 9 | index.html, 404s, MIME types) is http.server's own behaviour. |
| 10 | |
| 11 | python3 scripts/serve-like-hub.py --dir dist --port 8080 [--bind 127.0.0.1] |
| 12 | """ |
| 13 | import argparse, functools, gzip, http.server, io, os |
| 14 | |
| 15 | |
| 16 | class Handler(http.server.SimpleHTTPRequestHandler): |
| 17 | def send_head(self): |
| 18 | path = self.translate_path(self.path) |
| 19 | if os.path.isdir(path) and self.path.split("?", 1)[0].endswith("/"): |
| 20 | path = os.path.join(path, "index.html") |
| 21 | accepts = "gzip" in (self.headers.get("Accept-Encoding") or "") |
| 22 | if not (accepts and os.path.isfile(path) and self.guess_type(path).startswith("text/html")): |
| 23 | return super().send_head() |
| 24 | with open(path, "rb") as fh: |
| 25 | # Hub nginx leaves gzip_comp_level commented, so it uses the default, 1. |
| 26 | # Measured 2026-11-10: live /classic/ gzip size equals Python gzip level 1, not level 6. |
| 27 | body = gzip.compress(fh.read(), compresslevel=1) |
| 28 | self.send_response(200) |
| 29 | self.send_header("Content-type", "text/html; charset=utf-8") |
| 30 | self.send_header("Content-Encoding", "gzip") |
| 31 | self.send_header("Vary", "Accept-Encoding") |
| 32 | self.send_header("Content-Length", str(len(body))) |
| 33 | self.end_headers() |
| 34 | return io.BytesIO(body) |
| 35 | |
| 36 | def log_message(self, *a): |
| 37 | pass |
| 38 | |
| 39 | |
| 40 | def main(): |
| 41 | ap = argparse.ArgumentParser() |
| 42 | ap.add_argument("--dir", required=True) |
| 43 | ap.add_argument("--port", type=int, required=True) |
| 44 | ap.add_argument("--bind", default="127.0.0.1") |
| 45 | a = ap.parse_args() |
| 46 | http.server.ThreadingHTTPServer((a.bind, a.port), functools.partial(Handler, directory=a.dir)).serve_forever() |
| 47 | |
| 48 | |
| 49 | if __name__ == "__main__": |
| 50 | main() |