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
This commit is contained in:
Claude 2026-03-19 11:17:06 +00:00
parent a8b22d8e2d
commit 2e98ce957e
No known key found for this signature in database

View file

@ -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")