{$PUBLIC_WEB_HOST:localhost} {
    # A remote module's app link goes through nexus-web: the API redirects it
    # to the app's grant URL, which nexus-web points at the API host. A remote
    # app never runs on this host.
    handle /app-html/remote/* {
        reverse_proxy nexus-web:3000 {
            header_up X-Forwarded-Proto {scheme}
            header_up X-Forwarded-Host {host}
        }
    }
    # Same-origin /app-html/ → ABI without forging ABI_API_KEY (anonymous
    # browsers must present a scoped JWT / cookie / user Bearer).
    handle /app-html/* {
        reverse_proxy abi:9879 {
            header_up X-Forwarded-Proto {scheme}
            header_up X-Forwarded-Host {host}
        }
    }
    handle {
        reverse_proxy nexus-web:3000 {
            header_up X-Forwarded-Proto {scheme}
            header_up X-Forwarded-Host {host}
        }
    }
}

{$PUBLIC_API_HOST:api.localhost} {
    # The API's own routes read bodies up to 64 MiB (the API refuses past it
    # too); remote apps (/app-remote/) set their own limits.
    @api_routes path /api/*
    request_body @api_routes {
        max_size 64MiB
    }
    reverse_proxy abi:9879 {
        header_up X-Forwarded-Proto {scheme}
        header_up X-Forwarded-Host {host}
    }
}

{% if INCLUDE_HEADSCALE %}
{$HEADSCALE_SERVER_URL:headscale.localhost} {
    reverse_proxy headscale:8080 {
        header_up X-Forwarded-Proto {scheme}
    }
}
{% endif %}

{% if INCLUDE_CODING %}
# Coder control plane + per-workspace app subdomains (the browser IDE). Same
# registrable domain as the web UI so the editor's session cookie is first-party
# inside the Nexus iframe. Caddy preserves the Host header (Coder needs it for
# subdomain app routing) and proxies WebSockets automatically. The wildcard
# label needs a DNS-challenge cert in production; locally Caddy's internal CA
# covers it.
coder.{$PUBLIC_WEB_HOST:localhost}, *.coder.{$PUBLIC_WEB_HOST:localhost} {
    reverse_proxy coder:7080 {
        header_up X-Forwarded-Proto {scheme}
    }
}

# Forgejo - git host + in-app review backend. Proxied so git clone/push and the
# REST API the Nexus review UI calls work; users review inside Nexus, not here.
git.{$PUBLIC_WEB_HOST:localhost} {
    reverse_proxy forgejo:3000 {
        header_up X-Forwarded-Proto {scheme}
    }
}
{% endif %}
