From 2e98ce957e9b7d550786a119c808344717212b4d Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 19 Mar 2026 11:17:06 +0000 Subject: [PATCH] fix: make Caddy route upsert robust against missing/invalid routes - Add --fail to the GET so a 404 (no 'hiy' server yet, stale volume) falls back to [] instead of passing error JSON to Python - Python now guards against non-list responses with try/except - Always re-append the dashboard catch-all route so it survives even when routes are rebuilt from scratch --- builder/build.sh | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/builder/build.sh b/builder/build.sh index 6568ca4..67e7b30 100755 --- a/builder/build.sh +++ b/builder/build.sh @@ -126,17 +126,25 @@ if curl --silent --fail "${CADDY_API}/config/" >/dev/null 2>&1; then EOF ) # Upsert the route for this app. - ROUTES=$(curl --silent "${CADDY_API}/config/apps/http/servers/hiy/routes" 2>/dev/null || echo "[]") - # Remove existing route for the same host, then append the new one. + ROUTES=$(curl --silent --fail "${CADDY_API}/config/apps/http/servers/hiy/routes" 2>/dev/null || echo "[]") + # Remove existing route for the same host, rebuild list, keep dashboard as catch-all. UPDATED=$(echo "$ROUTES" | python3 -c " import sys, json -routes = json.load(sys.stdin) +try: + routes = json.loads(sys.stdin.read()) + if not isinstance(routes, list): + routes = [] +except Exception: + routes = [] +DASHBOARD = {'handle': [{'handler': 'reverse_proxy', 'upstreams': [{'dial': 'server:3000'}]}]} new_host = '${APP_ID}.${DOMAIN_SUFFIX}' +# Keep host-matched routes that are NOT for this app routes = [r for r in routes - if not (isinstance(r, dict) - and any(isinstance(m, dict) and new_host in m.get('host', []) - for m in r.get('match', [])))] + if r.get('match') and not any( + isinstance(m, dict) and new_host in m.get('host', []) + for m in r.get('match', []))] routes.insert(0, json.loads(sys.argv[1])) +routes.append(DASHBOARD) print(json.dumps(routes)) " "$ROUTE_JSON")