# Application-routing Caddy for the long-lived dev environment.
# Listens only on the `edge` Docker network; TLS termination and the
# real `:80`/`:443` listeners belong to the host Caddy in front of us.
#
# `/srv/galaxy-ui` is mounted from the `galaxy-dev-ui-dist` named volume,
# refreshed on every dev-deploy run.

{
    auto_https off
}

:80 {
    @frontend host www.galaxy.lan
    handle @frontend {
        root * /srv/galaxy-ui

        # SvelteKit emits hash-named JS/CSS chunks under
        # `_app/immutable/`; the file name changes whenever the
        # content changes, so the browser can cache them forever.
        # Without an explicit Cache-Control, Caddy falls back to
        # heuristic caching that revalidates on every reload —
        # measurably slow on Safari + the long-lived dev stack
        # when the cache is warm. Everything else (index.html
        # fallback, env.js, version.json, core.wasm,
        # wasm_exec.js, favicon.svg) must revalidate so a fresh
        # deploy lands without the user having to clear the
        # cache by hand.
        @immutable path /_app/immutable/*
        header @immutable Cache-Control "public, max-age=31536000, immutable"
        @dynamic not path /_app/immutable/*
        header @dynamic Cache-Control "no-cache, must-revalidate"

        try_files {path} /index.html
        file_server
        encode zstd gzip
    }

    @api host api.galaxy.lan
    handle @api {
        # Connect-Web (authenticated) lives on a separate listener
        # (`GATEWAY_AUTHENTICATED_GRPC_ADDR=:9090`). Anything else —
        # public auth, healthz — is the public REST listener on
        # `:8080`. The split mirrors the Vite dev-server proxy in
        # `ui/frontend/vite.config.ts`.
        @connect path /galaxy.gateway.v1.EdgeGateway/*
        handle @connect {
            reverse_proxy galaxy-api:9090
        }
        reverse_proxy galaxy-api:8080
    }
}
