AutoDevReverseProxy/templates/caddy.sh
2023-01-18 15:02:13 +01:00

36 lines
993 B
Bash

#!/usr/bin/env bash
set -e
DOCKER_CADDY_NAME="${DOCKER_CADDY_NAME:-proxy}"
DOCKER_CADDY_PORT="${DOCKER_CADDY_PORT:-2020}"
WRAP_START='{\n\tadmin :2020\n}\n'
WRAP_END='\n'
TEMPLATE='${LOCAL_WEB_HOST} {\n\treverse_proxy ${LOCAL_IP}:$LOCAL_WEB_PORT\n}'
SEPARATOR='\n'
OUT='/config/Caddyfile'
label_hook() {
LOCAL_WEB_PORT="${LOCAL_WEB_PORT:-80}"
# Check if host contains spaces
if [[ "${LOCAL_WEB_HOST}" =~ \ ]]
then
LOCAL_WEB_HOST="$(sed 's/ /, /g' <<< ${LOCAL_WEB_HOST})"
fi
}
template_hook() {
if [[ "${PARTIAL_RESULT}" =~ ^([^\{,\ ]+(,\ )?)+\ \{$'\\n\\t'reverse_proxy\ (::|[.0-9a-e]+)+\:[0-9]+$'\\n'\}$ ]]
then
return 0;
fi
return 1;
}
finally_hook() {
if curl --silent "${DOCKER_CADDY_NAME}:${DOCKER_CADDY_PORT}/load" -H "Content-Type: text/caddyfile" --data-binary "@${OUT}"
then
printf '%s | Updated Caddy Config\n' "$(get_date)"
else
printf '%s | Failed to update Caddy Config\n' "$(get_date)"
fi
}