# TLS termination for Vallus (TypeScript backend).
#
# Ports start at 3500 so this stack can run alongside the Rust one (3000–3004)
# on the same machine without either claiming the other's ports.
#
# The runner can terminate TLS itself (VALLUS_PROTOCOL=https, the images'
# default), but not in this stack: docker-compose.yml sets VALLUS_PROTOCOL=http,
# so certificates, renewal and cipher policy stay a deployment concern. Caddy
# sits in front and terminates TLS, forwarding to the runner over the container
# network.
#
# It sets X-Forwarded-Proto, which the app reads when it has to build an
# absolute URL back to itself (the OIDC post-logout redirect).
#
# ── Local / IP-only deployments ───────────────────────────────────────────────
# `tls internal` makes Caddy issue certificates from its own local CA. That
# works instantly with no domain and no public exposure, at the cost of a
# browser warning until the CA is trusted. To trust it:
#
#   docker compose cp caddy:/data/caddy/pki/authorities/local/root.crt ./caddy-root.crt
#   macOS:  sudo security add-trusted-cert -d -r trustRoot \
#             -k /Library/Keychains/System.keychain ./caddy-root.crt
#
# ── Real domain ───────────────────────────────────────────────────────────────
# Replace `:3500` with the hostname (e.g. `runner.example.com`) and delete the
# `tls internal` line. Caddy then obtains and renews a public certificate
# automatically. That needs ports 80 and 443 published and the DNS pointing here.

{
	# No admin API - nothing needs it, and it is one less listener.
	admin off
	# An operator address for the local CA; never used for a public cert.
	email admin@localhost
	# A client reaching the runner by raw IP sends no SNI (it may only carry a
	# hostname), leaving Caddy with no site to match and the handshake failing
	# outright. Assume the primary name instead: the connection succeeds, at the
	# cost of a name-mismatch warning for IP access. Browsers always send SNI for
	# a hostname, so this only affects the IP case.
	default_sni localhost
}

(runner) {
	tls internal
	encode zstd gzip

	# Playwright HTML reports stream large trace and attachment files, and a run
	# can hold a request open for a while.
	request_body {
		max_size 512MB
	}
}

# The names each site answers to. A certificate is issued per name, so a host
# that isn't listed here fails the TLS handshake rather than being served - name
# every address the runner is reached by.
#
# Override for a LAN address or a DNS name by setting RUNNER_HOST in the
# environment (see docker-compose.yml):
#   RUNNER_HOST=runner.lan docker compose up -d
#
# The site address needs a literal port, so each block repeats the host names.

# Launcher (home page).
{$RUNNER_HOST:localhost}:3500, localhost:3500, 127.0.0.1:3500 {
	import runner
	reverse_proxy vallus-ts-be:3500
}

# Per-project dashboards. The port numbers must match the ones assigned in
# setup, because the launcher links to projects by port.
{$RUNNER_HOST:localhost}:3501, localhost:3501, 127.0.0.1:3501 {
	import runner
	reverse_proxy vallus-ts-be:3501
}

{$RUNNER_HOST:localhost}:3502, localhost:3502, 127.0.0.1:3502 {
	import runner
	reverse_proxy vallus-ts-be:3502
}

{$RUNNER_HOST:localhost}:3503, localhost:3503, 127.0.0.1:3503 {
	import runner
	reverse_proxy vallus-ts-be:3503
}

{$RUNNER_HOST:localhost}:3504, localhost:3504, 127.0.0.1:3504 {
	import runner
	reverse_proxy vallus-ts-be:3504
}
