scripts/serve: Optionally serve over https.

Allows local testing on mobile devices with Bluetooth, USB, and camera suppport.
This commit is contained in:
Laurens Valk
2026-04-02 10:25:34 +02:00
parent 38a71238f9
commit c49df2b81f
+16 -6
View File
@@ -6,9 +6,11 @@ Serve the build directory using builtin Python web server.
import http.server import http.server
import pathlib import pathlib
import socketserver import ssl
BUILD_DIR = (pathlib.Path(__file__).parent.parent / "build").resolve() BUILD_DIR = (pathlib.Path(__file__).parent.parent / "build").resolve()
CERT_FILE = BUILD_DIR / "cert.pem"
KEY_FILE = BUILD_DIR / "key.pem"
PORT = 8000 PORT = 8000
@@ -17,10 +19,10 @@ class Handler(http.server.SimpleHTTPRequestHandler):
self, self,
request: bytes, request: bytes,
client_address: tuple[str, int], client_address: tuple[str, int],
server: socketserver.BaseServer, server: http.server.HTTPServer,
directory: str | None = ... directory: str | None = ...
) -> None: ) -> None:
super().__init__(request, client_address, server, directory=BUILD_DIR) super().__init__(request, client_address, server, directory=str(BUILD_DIR))
def end_headers(self) -> None: def end_headers(self) -> None:
# custom headers needed for some web API features # custom headers needed for some web API features
@@ -29,6 +31,14 @@ class Handler(http.server.SimpleHTTPRequestHandler):
return super().end_headers() return super().end_headers()
with socketserver.TCPServer(("", PORT), Handler) as httpd: httpd = http.server.HTTPServer(("", PORT), Handler)
print(f"serving at http://0.0.0.0:{PORT}")
httpd.serve_forever() if CERT_FILE.exists() and KEY_FILE.exists():
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
ctx.load_cert_chain(certfile=CERT_FILE, keyfile=KEY_FILE)
httpd.socket = ctx.wrap_socket(httpd.socket, server_side=True)
print(f"serving at https://0.0.0.0:{PORT}")
else:
print(f"cert/key not found, serving without TLS at http://0.0.0.0:{PORT}")
httpd.serve_forever()