Size: 2140 bytes.


 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
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python3
# cs/devtools/generate_routing.gpt.py
"""
Generate routing.json from system.gpt.yml for load balancer.

Reads the routing section from system.gpt.yml and generates a JSON file
that maps domains to service names.
"""

import json
import sys
import yaml
from pathlib import Path
from typing import Any, Dict


def load_system_spec(path: Path) -> Dict[str, Any]:
    """Load and parse the system specification YAML."""
    with open(path) as f:
        return yaml.safe_load(f)


def generate_routing_json(spec: Dict[str, Any]) -> Dict[str, Any]:
    """Generate routing JSON from system spec with both prod and dev routes."""
    ngrok_service = spec.get("services", {}).get("ngrok", {})
    expose_config = ngrok_service.get("expose", {})
    load_balancer_config = expose_config.get("load-balancer", {})
    mapping = load_balancer_config.get("mapping", {})

    prod_domains = load_balancer_config.get("prod", [])
    prod_routes = {}
    for domain in prod_domains:
        domain_clean = domain.replace("dev.", "", 1)
        service_name = mapping.get(domain_clean) or domain_clean.replace(".", "-")
        prod_routes[domain] = service_name

    dev_domains = load_balancer_config.get("dev", [])
    dev_routes = {}
    for domain in dev_domains:
        domain_clean = domain.replace("dev.", "", 1)
        service_name = mapping.get(domain_clean) or domain_clean.replace(".", "-")
        dev_routes[domain] = service_name

    return {"prod": {"routes": prod_routes}, "dev": {"routes": dev_routes}}


def main():
    if len(sys.argv) < 3:
        print("Usage: generate_routing.gpt.py <system.gpt.yml> <output.json>")
        sys.exit(1)

    system_file = Path(sys.argv[1])
    output_file = Path(sys.argv[2])

    if not system_file.exists():
        print(f"Error: {system_file} not found", file=sys.stderr)
        sys.exit(1)

    spec = load_system_spec(system_file)
    routing_data = generate_routing_json(spec)

    with open(output_file, "w") as f:
        json.dump(routing_data, f, indent=2)

    print(f"Generated {output_file} with prod and dev routes")


if __name__ == "__main__":
    main()
v0 (commit) © 2025 @p13i.io | Load balancer proxied to: cs-code-viewer-3:8080 in 4ms.