File size: 2,047 Bytes
2fcdd6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""WebsiteBench Harbor foreground entrypoint for the frozen clone."""

from __future__ import annotations

import json
import os
from pathlib import Path

import uvicorn


ROOT = Path(__file__).resolve().parent
SITE_ROOT = ROOT.parent
RUNTIME = json.loads((SITE_ROOT / "backend" / "runtime.json").read_text(encoding="utf-8"))
SITE_ID = str(RUNTIME["site"]["id"])
DATA_DIR = Path(os.environ["DATA_DIR"])
if not DATA_DIR.is_absolute():
    raise RuntimeError("DATA_DIR must be absolute")
DATA_DIR = DATA_DIR.resolve()
DATA_DIR.mkdir(parents=True, exist_ok=True)
os.environ["WEBSITEBENCH_DATA_DIR"] = str(DATA_DIR)
os.environ["WEBSITEBENCH_SITE_BACKEND_RUNTIME"] = str(SITE_ROOT / "backend" / "runtime.json")
os.environ["WEBSITEBENCH_SITE_BACKEND_DATABASE"] = str(DATA_DIR / RUNTIME["database"]["filename"])
if os.environ.get("WEBSITEBENCH_MAILBOX_BOOTSTRAP_URL", "").strip():
    os.environ["WEBSITEBENCH_DEPLOYMENT_PROFILE"] = "offline-harbor"
os.environ["WEBSITEBENCH_PAYMENT_PROFILE"] = "local-sandbox"

from app import app as clone_app  # noqa: E402

HEALTH_BODY = b'{"status":"ok"}'
# /healthz is the contract (ADR-0010); the private path stays mounted
# so bundles frozen before the migration keep answering.
HEALTH_PATHS = ("/healthz", "/__websitebench/health")


async def application(scope, receive, send):
    if scope.get("type") == "http" and scope.get("path") in HEALTH_PATHS:
        await send({
            "type": "http.response.start",
            "status": 200,
            "headers": [
                (b"content-type", b"application/json"),
                (b"content-length", str(len(HEALTH_BODY)).encode("ascii")),
                (b"cache-control", b"no-store"),
            ],
        })
        await send({"type": "http.response.body", "body": HEALTH_BODY})
        return
    await clone_app(scope, receive, send)


if __name__ == "__main__":
    uvicorn.run(
        application,
        host=os.environ["HOST"],
        port=int(os.environ["PORT"]),
        access_log=False,
        timeout_graceful_shutdown=2,
    )