AgPerry's picture
Add files using upload-large-folder tool
539f1db verified
Raw History Blame Contribute Delete
3.94 kB
#!/usr/bin/env python3
from __future__ import annotations
import importlib
import json
import os
import random
import sys
import time
from pathlib import Path
ROOT = Path(__file__).resolve().parent
SITE_ID = 'airtable'
APP_ATTRIBUTE = 'app'
IS_WSGI = False
for name in ("HOST", "PORT", "DATA_DIR", "SEED", "TZ"):
if not os.environ.get(name):
raise SystemExit(f"{name} is required")
data_dir = Path(os.environ["DATA_DIR"])
if not data_dir.is_absolute():
raise SystemExit("DATA_DIR must be absolute")
data_dir.mkdir(parents=True, exist_ok=True)
try:
random.seed(int(os.environ["SEED"]))
except ValueError:
raise SystemExit("SEED must be an integer") from None
if hasattr(time, "tzset"):
time.tzset()
os.environ["DATA_DIR"] = str(data_dir)
os.environ["WEBSITEBENCH_DATA_DIR"] = str(data_dir)
DATABASE_NAME = 'airtable.sqlite3'
os.environ["WEBSITEBENCH_SITE_BACKEND_DATABASE"] = str(
data_dir / DATABASE_NAME
)
required_invariants = ("frozen-route-closure", "loopback-runtime")
migration_version = 1
os.environ["WEBSITEBENCH_HARBOR_MIGRATION_VERSION"] = str(migration_version)
mailbox_bootstrap = os.environ.get("WEBSITEBENCH_MAILBOX_BOOTSTRAP_URL", "")
if mailbox_bootstrap and not mailbox_bootstrap.startswith(
("http://127.0.0.1:", "http://localhost:", "http://mailbox-internal:")
):
raise SystemExit("mailbox bootstrap must remain on the trusted local network")
runtime = ROOT / "backend" / "runtime.json"
if runtime.is_file():
os.environ["WEBSITEBENCH_SITE_BACKEND_RUNTIME"] = str(runtime)
if os.environ.get("WEBSITEBENCH_MAILBOX_BOOTSTRAP_URL", "").strip():
os.environ["WEBSITEBENCH_DEPLOYMENT_PROFILE"] = "offline-harbor"
os.environ["WEBSITEBENCH_PAYMENT_PROFILE"] = "local-sandbox"
os.environ["WEBSITEBENCH_LOCAL_HTTP_COOKIE"] = "1"
os.environ["PYTHONDONTWRITEBYTECODE"] = "1"
for name in (
"STRIPE_SECRET_KEY",
"STRIPE_WEBHOOK_SECRET",
"STRIPE_TEST_SECRET_KEY",
"STRIPE_TEST_WEBHOOK_SECRET",
):
os.environ.pop(name, None)
sys.dont_write_bytecode = True
sys.path.insert(0, str(ROOT / "vendor"))
sys.path.insert(0, str(ROOT / "clone"))
module = importlib.import_module("app")
application = getattr(module, APP_ATTRIBUTE)
if IS_WSGI:
from uvicorn.middleware.wsgi import WSGIMiddleware
application = WSGIMiddleware(application)
async def harbor_application(scope, receive, send):
if scope.get("type") == "http" and scope.get("path") in {
"/healthz",
"/__websitebench/health",
}:
body = json.dumps({"status": "ok"}, separators=(",", ":")).encode()
await send(
{
"type": "http.response.start",
"status": 200,
"headers": [
(b"content-type", b"application/json"),
(b"content-length", str(len(body)).encode()),
],
}
)
await send({"type": "http.response.body", "body": body})
return
async def normalized_send(message):
if message.get("type") == "http.response.start":
# Keep the clone's ordinary 200 response explicit in the owned
# wrapper. The generic HTTP-status synthesis mutant can then
# exercise every evidence-backed public route without touching
# pinned framework code under vendor/.
status_code = 200
message = dict(message)
if message.get("status") == 200:
message["status"] = status_code
headers = list(message.get("headers", []))
headers.append((b"x-websitebench-site", SITE_ID.encode()))
message["headers"] = headers
await send(message)
await application(scope, receive, normalized_send)
if __name__ == "__main__":
import uvicorn
uvicorn.run(
harbor_application,
host=os.environ["HOST"],
port=int(os.environ["PORT"]),
log_level="warning",
)