scripts: new web server script

This provides a new script for serving the output of `yarn build`. This
is needed since the app now requires special http headers to be defined
to use certain web APIs.
This commit is contained in:
David Lechner
2022-09-09 11:37:37 -05:00
parent f24db4a89b
commit 37527630cf
2 changed files with 36 additions and 2 deletions
+34
View File
@@ -0,0 +1,34 @@
#!/usr/bin/env python3
"""
Serve the build directory using builtin Python web server.
"""
import http.server
import pathlib
import socketserver
BUILD_DIR = (pathlib.Path(__file__).parent.parent / "build").resolve()
PORT = 8000
class Handler(http.server.SimpleHTTPRequestHandler):
def __init__(
self,
request: bytes,
client_address: tuple[str, int],
server: socketserver.BaseServer,
directory: str | None = ...
) -> None:
super().__init__(request, client_address, server, directory=BUILD_DIR)
def end_headers(self) -> None:
# custom headers needed for some web API features
self.send_header("Cross-Origin-Opener-Policy", "same-origin")
self.send_header("Cross-Origin-Embedder-Policy", "require-corp")
return super().end_headers()
with socketserver.TCPServer(("", PORT), Handler) as httpd:
print(f"serving at http://0.0.0.0:{PORT}")
httpd.serve_forever()