Files
dj/integration-work/verify_progress_ui.py
T

23 lines
1.3 KiB
Python

"""Serve built frontend with a read-only proxy to verify progress; no DB writes."""
import http.server,json,pathlib,subprocess,threading,urllib.request,urllib.error
root=pathlib.Path(__file__).resolve().parent.parent
public=root/'huobao-source/frontend/.output/public'
upstream=json.loads((root/'huobao-data/last-launch.json').read_text())['api'].removesuffix('/api/v1')
class ReadOnly(http.server.SimpleHTTPRequestHandler):
def __init__(self,*args,**kw):super().__init__(*args,directory=str(public),**kw)
def log_message(self,*args):pass
def do_GET(self):
if self.path.startswith(('/api/','/static/')):
try:
with urllib.request.urlopen(upstream+self.path,timeout=15) as r:
data=r.read();self.send_response(r.status);self.send_header('Content-Type',r.headers.get('Content-Type','application/json'));self.end_headers();self.wfile.write(data)
except urllib.error.HTTPError as e:self.send_error(e.code)
except (ConnectionAbortedError, BrokenPipeError):pass
else:
if not pathlib.Path(self.translate_path(self.path)).exists():self.path='/index.html'
super().do_GET()
server=http.server.ThreadingHTTPServer(('127.0.0.1',18938),ReadOnly)
threading.Thread(target=server.serve_forever,daemon=True).start()
try:subprocess.run(['node',str(root/'integration-work/browser-test/verify_progress_ui.cjs')],cwd=root,check=True)
finally:server.shutdown()