fix(map): fingerprint static assets on release
This commit is contained in:
@@ -18,6 +18,17 @@ from urllib.request import Request, urlopen
|
||||
|
||||
|
||||
ROOT = Path(__file__).resolve().parent
|
||||
|
||||
|
||||
def _static_asset_version():
|
||||
"""Content fingerprint used to bypass upstream static-asset caches on release."""
|
||||
digest = hashlib.sha256()
|
||||
for filename in ("app.js", "styles.css"):
|
||||
digest.update((ROOT / filename).read_bytes())
|
||||
return digest.hexdigest()[:16]
|
||||
|
||||
|
||||
STATIC_ASSET_VERSION = _static_asset_version()
|
||||
HOST = os.getenv("VEHICLE_MAP_HOST", "0.0.0.0")
|
||||
PORT = int(os.getenv("VEHICLE_MAP_PORT", "20800"))
|
||||
OPEN_PLATFORM_BASE_URL = os.getenv("OPEN_PLATFORM_BASE_URL", "https://open.d.lnoneos.com").rstrip("/")
|
||||
@@ -293,6 +304,9 @@ class VehicleMapHandler(SimpleHTTPRequestHandler):
|
||||
|
||||
def do_GET(self):
|
||||
path = urlparse(self.path).path
|
||||
if path in {"/", "/index.html"}:
|
||||
self._serve_index()
|
||||
return
|
||||
if path == "/api/health":
|
||||
self._write_json(200, {
|
||||
"status": "ok",
|
||||
@@ -380,6 +394,21 @@ class VehicleMapHandler(SimpleHTTPRequestHandler):
|
||||
})
|
||||
return False
|
||||
|
||||
def _serve_index(self):
|
||||
try:
|
||||
template = (ROOT / "index.html").read_text(encoding="utf-8")
|
||||
body = template.replace("__ASSET_VERSION__", STATIC_ASSET_VERSION).encode("utf-8")
|
||||
except OSError as exc:
|
||||
self.log_error("index template unavailable: %s", exc)
|
||||
self.send_error(500, "Map application is temporarily unavailable")
|
||||
return
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "text/html; charset=utf-8")
|
||||
self.send_header("Content-Length", str(len(body)))
|
||||
self.send_header("Cache-Control", "no-cache")
|
||||
self.end_headers()
|
||||
self.wfile.write(body)
|
||||
|
||||
def end_headers(self):
|
||||
if self.path == "/" or self.path.split("?", 1)[0].endswith((".html", ".css", ".js", ".svg")):
|
||||
self.send_header("Cache-Control", "no-cache")
|
||||
|
||||
Reference in New Issue
Block a user