Files
appfactory-tools/scripts/generate-caddyfile.sh
T

90 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
CONFIG_FILE="/opt/appfactory/config/appfactory.env"
source "$CONFIG_FILE"
CATALOG_FILE="$APPFACTORY_DIR/apps/catalog.yml"
CADDY_FILE="$APPFACTORY_DIR/gateway/Caddyfile"
mkdir -p "$(dirname "$CADDY_FILE")"
cat > "$CADDY_FILE" <<EOF_CADDY
:80 {
handle /health {
respond "AppFactory OK" 200
}
handle /portal* {
uri strip_prefix /portal
reverse_proxy appfactory-portal:9100
}
handle /apps {
root * /srv/static
rewrite * /apps.html
file_server
}
EOF_CADDY
if [ -f "$CATALOG_FILE" ]; then
python3 - "$CATALOG_FILE" "$CADDY_FILE" <<'PY'
import sys
catalog_file = sys.argv[1]
caddy_file = sys.argv[2]
apps = []
current = None
with open(catalog_file, "r", encoding="utf-8") as f:
for raw in f:
stripped = raw.strip()
if stripped.startswith("- id:"):
if current:
apps.append(current)
current = {"id": stripped.split(":", 1)[1].strip()}
elif current and ":" in stripped:
key, value = stripped.split(":", 1)
current[key.strip()] = value.strip().strip('"')
if current:
apps.append(current)
with open(caddy_file, "a", encoding="utf-8") as f:
for app in apps:
app_id = app.get("id")
port = app.get("container_port") or app.get("port") or "8000"
if not app_id:
continue
f.write(f" handle_path /apps/{app_id}/* {{\n")
f.write(f" reverse_proxy {app_id}:{port}\n")
f.write(" }\n\n")
PY
fi
cat >> "$CADDY_FILE" <<EOF_CADDY
handle_path /webhook/* {
reverse_proxy appfactory-webhook:9000
}
handle_path /registry/* {
reverse_proxy appfactory-registry:5000
}
handle {
respond "AppFactory gateway is running" 200
}
}
EOF_CADDY
echo "Generated: $CADDY_FILE"
docker exec appfactory-caddy caddy validate --config /etc/caddy/Caddyfile
docker exec appfactory-caddy caddy reload --config /etc/caddy/Caddyfile