Fix Caddy route registration: discover server name dynamically

Caddy's Caddyfile adapter names servers 'srv0' (not 'hiy'), so
PATCHing /config/apps/http/servers/hiy/routes was a no-op. Now we
query /config/apps/http/servers/ to find the actual server name
before updating routes.

https://claude.ai/code/session_01FKCW3FDjNFj6jve4niMFXH
This commit is contained in:
Claude 2026-03-20 13:34:21 +00:00
parent c7adf84c5f
commit 6ff8c9a267
No known key found for this signature in database

View file

@ -119,6 +119,14 @@ if curl --silent --fail "${CADDY_API}/config/" >/dev/null 2>&1; then
UPSTREAM="${CONTAINER_IP}:${PORT}" UPSTREAM="${CONTAINER_IP}:${PORT}"
log "Updating Caddy: ${APP_ID}.${DOMAIN_SUFFIX}${UPSTREAM}" log "Updating Caddy: ${APP_ID}.${DOMAIN_SUFFIX}${UPSTREAM}"
# Discover actual server name (Caddyfile adapter names it 'srv0', not 'hiy').
CADDY_SERVER=$(curl --silent "${CADDY_API}/config/apps/http/servers/" 2>/dev/null | \
python3 -c "import sys,json; d=json.load(sys.stdin); print(next(iter(d)))" 2>/dev/null || true)
if [ -z "$CADDY_SERVER" ]; then
log "WARNING: Could not discover Caddy server name; skipping route update."
else
ROUTES_URL="${CADDY_API}/config/apps/http/servers/${CADDY_SERVER}/routes"
ROUTE_JSON=$(cat <<EOF ROUTE_JSON=$(cat <<EOF
{ {
"match": [{"host": ["${APP_ID}.${DOMAIN_SUFFIX}"]}], "match": [{"host": ["${APP_ID}.${DOMAIN_SUFFIX}"]}],
@ -127,7 +135,7 @@ if curl --silent --fail "${CADDY_API}/config/" >/dev/null 2>&1; then
EOF EOF
) )
# Upsert the route for this app. # Upsert the route for this app.
ROUTES=$(curl --silent --fail "${CADDY_API}/config/apps/http/servers/hiy/routes" 2>/dev/null || echo "[]") ROUTES=$(curl --silent --fail "${ROUTES_URL}" 2>/dev/null || echo "[]")
# Remove existing route for the same host, rebuild list, keep dashboard as catch-all. # Remove existing route for the same host, rebuild list, keep dashboard as catch-all.
UPDATED=$(echo "$ROUTES" | python3 -c " UPDATED=$(echo "$ROUTES" | python3 -c "
import sys, json import sys, json
@ -154,7 +162,7 @@ print(json.dumps(routes))
set +e set +e
CADDY_RESP=$(curl --silent --show-error \ CADDY_RESP=$(curl --silent --show-error \
--write-out "\nHTTP_STATUS:%{http_code}" \ --write-out "\nHTTP_STATUS:%{http_code}" \
"${CADDY_API}/config/apps/http/servers/hiy/routes" \ "${ROUTES_URL}" \
--header "Content-Type: application/json" \ --header "Content-Type: application/json" \
--request PATCH \ --request PATCH \
--data "$UPDATED" 2>&1) --data "$UPDATED" 2>&1)
@ -165,6 +173,7 @@ print(json.dumps(routes))
log "WARNING: Caddy update failed (app is running; fix routing manually)." log "WARNING: Caddy update failed (app is running; fix routing manually)."
log "Caddy response: ${CADDY_RESP}" log "Caddy response: ${CADDY_RESP}"
fi fi
fi # end: CADDY_SERVER not empty
else else
log "Caddy admin API not reachable; skipping route update." log "Caddy admin API not reachable; skipping route update."
log "Container ${CONTAINER_NAME} is running on port ${PORT} but not publicly routed." log "Container ${CONTAINER_NAME} is running on port ${PORT} but not publicly routed."