27 lines
1.4 KiB
Python
27 lines
1.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Preview the public static release on an OS-assigned local port."""
|
|
from http.server import ThreadingHTTPServer, SimpleHTTPRequestHandler
|
|
from pathlib import Path
|
|
import json, os
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
OUT=ROOT/'web-front/out'
|
|
class Handler(SimpleHTTPRequestHandler):
|
|
def __init__(self,*args,**kwargs):super().__init__(*args,directory=str(OUT),**kwargs)
|
|
def do_GET(self):
|
|
path=self.path.split('?',1)[0]
|
|
if path=='/auto.shtml':
|
|
self.send_response(308);self.send_header('Location','/guides/choose');self.end_headers();return
|
|
if path.startswith('/api/') or path.rstrip('/').endswith('/showcase'):
|
|
self.send_error(404);return
|
|
target=OUT/path.lstrip('/')
|
|
if path.startswith('/health/') and target.is_file():
|
|
self.send_response(200);self.send_header('Content-Type','application/json');self.end_headers();self.wfile.write(target.read_bytes());return
|
|
if target.with_suffix('.html').is_file():self.path=path+'.html'
|
|
super().do_GET()
|
|
httpd=ThreadingHTTPServer(('127.0.0.1',0),Handler)
|
|
runtime=ROOT/'.local-run';runtime.mkdir(exist_ok=True)
|
|
state=dict(port=httpd.server_port,pid=os.getpid(),url=f'http://127.0.0.1:{httpd.server_port}')
|
|
(runtime/'server.json').write_text(json.dumps(state,indent=2)+'\n')
|
|
print(json.dumps(state),flush=True)
|
|
httpd.serve_forever()
|