Switch from Caddy templates to runtime envsubst for API and Keycloak URLs, adjust Dockerfile and entrypoint script
All checks were successful
Build and Publish Docker Images / build-and-push (., backend/infrastructure/gateway/Dockerfile, api-gateway, api-gateway) (push) Successful in 8m31s
Build and Publish Docker Images / build-and-push (., backend/services/ping/Dockerfile, ping-service, ping-service) (push) Successful in 7m20s
Build and Publish Docker Images / build-and-push (., config/docker/caddy/web-app/Dockerfile, web-app, web-app) (push) Successful in 2m1s
Build and Publish Docker Images / build-and-push (., config/docker/keycloak/Dockerfile, keycloak, keycloak) (push) Successful in 1m39s

This commit is contained in:
Stefan Mogeritsch 2026-03-14 22:14:21 +01:00
parent a202d3d817
commit a1bc7039cb
4 changed files with 27 additions and 13 deletions

View File

@ -17,10 +17,6 @@
encode gzip zstd
templates {
mime text/html application/json
}
handle /api/* {
reverse_proxy api-gateway:8081
}

View File

@ -13,6 +13,9 @@ ARG BUILD_DATE
# ===================================================================
FROM caddy:${CADDY_VERSION}
# envsubst für Env-Var-Substitution zur Laufzeit
RUN apk add --no-cache gettext
ARG VERSION
ARG BUILD_DATE
@ -21,13 +24,17 @@ LABEL service="web-app" \
maintainer="Meldestelle Development Team" \
build.date="${BUILD_DATE}"
# Copy Caddy Config & Templates
# Copy Caddy Config & Entrypoint
COPY config/docker/caddy/web-app/Caddyfile /etc/caddy/Caddyfile
COPY config/docker/caddy/web-app/config.json /usr/share/caddy/config.json
COPY config/docker/caddy/web-app/entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh
# Copy Pre-built Static Assets from Host
# NOTE: You must run `./gradlew :frontend:shells:meldestelle-portal:jsBrowserDistribution -Pproduction=true` locally first!
COPY frontend/shells/meldestelle-portal/build/dist/js/productionExecutable/ /usr/share/caddy/
# index.html wird als Template abgelegt; der Entrypoint erzeugt daraus zur Laufzeit die finale index.html
RUN mv /usr/share/caddy/index.html /usr/share/caddy/index.html.tmpl
# Ensure favicon exists (fallback)
# Using the shared asset from existing config structure
@ -38,4 +45,5 @@ EXPOSE 4000
HEALTHCHECK --interval=30s --timeout=5s --start-period=10s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:4000/health || exit 1
ENTRYPOINT ["/entrypoint.sh"]
CMD ["caddy", "run", "--config", "/etc/caddy/Caddyfile", "--adapter", "caddyfile"]

View File

@ -0,0 +1,10 @@
#!/bin/sh
set -e
# Ersetze ${API_BASE_URL} und ${KEYCLOAK_URL} in index.html zur Container-Startzeit.
# Caddy bekommt eine fertige, statische HTML-Datei — kein Template-Parsing mehr nötig.
envsubst '${API_BASE_URL} ${KEYCLOAK_URL}' \
< /usr/share/caddy/index.html.tmpl \
> /usr/share/caddy/index.html
exec "$@"

View File

@ -14,13 +14,13 @@
</div>
<!--
Schritt 1: Caddy füllt dieses JSON-Template aus.
Der Inhalt ist reines JSON, was Syntaxkonflikte mit JS vermeidet.
Schritt 1: Der Container-Entrypoint ersetzt ${API_BASE_URL} und ${KEYCLOAK_URL}
via envsubst beim Start. Caddy bekommt eine fertige statische Datei ohne Template-Parsing.
-->
<script id="app-config" type="application/json">
{
"apiBaseUrl": "{{.Env.API_BASE_URL}}",
"keycloakUrl": "{{.Env.KEYCLOAK_URL}}"
"apiBaseUrl": "${API_BASE_URL}",
"keycloakUrl": "${KEYCLOAK_URL}"
}
</script>
@ -33,20 +33,20 @@
const config = JSON.parse(configJson);
const params = new URLSearchParams(window.location.search);
const tmplPrefix = '{' + '{'; // Verhindert, dass Caddy '{{' als Template-Action interpretiert
const notReplaced = '${'; // Fallback-Erkennung: envsubst hat den Wert NICHT ersetzt
const apiFromCaddy = config.apiBaseUrl;
const apiOverride = params.get('apiBaseUrl');
globalThis.API_BASE_URL = apiOverride
? apiOverride.replace(/\/$/, '')
: (apiFromCaddy && !apiFromCaddy.startsWith(tmplPrefix)
: (apiFromCaddy && !apiFromCaddy.startsWith(notReplaced)
? apiFromCaddy.replace(/\/$/, '')
: 'http://' + window.location.origin.replace(/\/$/, ''));
: 'http://' + window.location.hostname + ':8081');
const kcFromCaddy = config.keycloakUrl;
const kcOverride = params.get('keycloakUrl');
globalThis.KEYCLOAK_URL = kcOverride
? kcOverride.replace(/\/$/, '')
: (kcFromCaddy && !kcFromCaddy.startsWith(tmplPrefix)
: (kcFromCaddy && !kcFromCaddy.startsWith(notReplaced)
? kcFromCaddy.replace(/\/$/, '')
: 'http://' + window.location.hostname + ':8180');