# Caddy plus the Cloudflare DNS and rate-limit plugins. caddy-ratelimit is
# pinned to a commit (no usable release), so Renovate leaves it; bump by hand.
# Builder and runtime tags move together.
# The last RUN validates the config for both modes, so a bad config fails the
# build before any running container is replaced.
FROM caddy:2.11.9-builder AS builder
RUN xcaddy build \
    --with github.com/caddy-dns/cloudflare@v0.2.4 \
    --with github.com/mholt/caddy-ratelimit@5625512f24f6f59d6f64fb3aafe5eecff0b286db

FROM caddy:2.11.9
COPY --from=builder /usr/bin/caddy /usr/bin/caddy
COPY Caddyfile routes.caddy /etc/caddy/
COPY sites/ /etc/caddy/sites/
# A log directory that can't be opened stops the config loading.
RUN mkdir -p /var/log/caddy /srv/www /srv/uploads
RUN set -e; \
    for site in prod dev; do \
        echo "caddy validate (CADDY_SITE=$site)"; \
        env XDG_DATA_HOME=/tmp/validate XDG_CONFIG_HOME=/tmp/validate \
            CADDY_SITE=$site DOMAIN=example.com EMAIL=validate@example.com \
            BACKEND_HOST=backend BACKEND_PORT=8080 BACKEND_ENDPOINT=/api \
            ICECAST_HOST=icecast2 ICECAST_PORT=8000 \
            GITEA_HOST=gitea GITEA_PORT=3000 \
            SILVERBULLET_PUBLIC_HOST=silverbullet-public SILVERBULLET_PUBLIC_PORT=3000 \
            PYTHON_HOST=python PYTHON_PORT=8000 \
            RECEIPT_WEB_UPSTREAM=127.0.0.1:9 RECEIPT_WEB_TOKEN=validate \
            STPPRIV_LAN_IP=192.0.2.1 \
            CLOUDFLARE_DNS_API_TOKEN=validate-placeholder-token-0000000000000 \
            caddy validate --config /etc/caddy/Caddyfile --adapter caddyfile; \
    done; \
    rm -rf /tmp/validate /var/log/caddy/*
