#!/usr/bin/env python3 """生成 Caddy 配置:HTTP :80 → 本机 FastAPI+静态(PORT,默认 8001)。""" from __future__ import annotations import json import sys from pathlib import Path def main() -> int: alias = "live" port = "8001" out = Path("/opt/live/generated/Caddyfile") cfg = Path("/opt/live/config/system.json") if cfg.is_file(): data = json.loads(cfg.read_text(encoding="utf-8")) ident = data.get("identity") or {} web = data.get("web_console") or {} alias = str(ident.get("hostname_alias") or alias) if web.get("api_port") is not None: port = str(web["api_port"]) if len(sys.argv) >= 2: alias = sys.argv[1] if len(sys.argv) >= 3: port = sys.argv[2] if len(sys.argv) >= 4: out = Path(sys.argv[3]) host = f"{alias}.local" body = f"""# Generated by live-platform/tools/render_caddyfile.py - do not hand-edit # http://{host}/ and http:/// -> 127.0.0.1:{port} {host}:80, :80 {{ \treverse_proxy 127.0.0.1:{port} }} """ out.parent.mkdir(parents=True, exist_ok=True) out.write_text(body, encoding="utf-8") print(str(out)) return 0 if __name__ == "__main__": raise SystemExit(main())