Refactor route handling in panelctl.sh and update caddy configuration for improved route management

Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
Jakub Dorfman 2026-04-26 20:41:56 +02:00
parent c1aa44c04e
commit 5c67fecd2d
2 changed files with 33 additions and 9 deletions

View file

@ -76,7 +76,7 @@ app_volume_dir() {
app_route_file() {
local name="$1"
echo "${ROUTES_DIR}/${name}.caddy"
echo "${ROUTES_DIR}/routes.caddy"
}
load_app() {
@ -248,12 +248,29 @@ cmd_render_route() {
auth_block="${FORWARD_AUTH_BLOCK}"
fi
cat >"${APP_ROUTE_FILE}" <<EOF
${APP_DOMAIN} {
${auth_block} reverse_proxy ${APP_UPSTREAM}
}
EOF
# Append to aggregate routes file (idempotent: remove stale block first).
local tmp
tmp="$(mktemp)"
# Strip any existing block for this app from the aggregate file.
if [[ -f "${APP_ROUTE_FILE}" ]]; then
grep -v "^# route:${name}:" "${APP_ROUTE_FILE}" >"${tmp}"
else
printf "" >"${tmp}"
fi
{
printf "# route:%s:start\n" "${name}"
printf "%s {\n" "${APP_DOMAIN}"
if [[ -n "${auth_block}" ]]; then
printf "%s\n" "${auth_block}"
fi
printf " reverse_proxy %s\n" "${APP_UPSTREAM}"
printf "}\n"
printf "# route:%s:end\n" "${name}"
} >>"${tmp}"
mv "${tmp}" "${APP_ROUTE_FILE}"
echo "rendered route ${APP_ROUTE_FILE}"
}
@ -306,7 +323,15 @@ cmd_remove() {
run_compose -f "${APP_COMPOSE_FILE}" down || true
rm -f "${APP_ROUTE_FILE}" "$(app_manifest "${name}")"
# Remove this app's block from the aggregate routes file.
if [[ -f "${APP_ROUTE_FILE}" ]]; then
local tmp
tmp="$(mktemp)"
grep -v "^# route:${name}:" "${APP_ROUTE_FILE}" >"${tmp}"
mv "${tmp}" "${APP_ROUTE_FILE}"
fi
rm -f "$(app_manifest "${name}")"
rm -rf "${APP_STACK_DIR}"
if [[ "${keep_volumes}" != "--keep-volumes" ]]; then