# TLS termination for Vallus.
#
# The runner speaks plain HTTP by design — no TLS stack inside the binary, so
# certificates, renewal and cipher policy stay a deployment concern rather than
# an application one. 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 `:3000` 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}:3000, localhost:3000, 127.0.0.1:3000 {
	import runner
	reverse_proxy vallus-rs-be:3000
}

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

{$RUNNER_HOST:localhost}:3002, localhost:3002, 127.0.0.1:3002 {
	import runner
	reverse_proxy vallus-rs-be:3002
}

{$RUNNER_HOST:localhost}:3003, localhost:3003, 127.0.0.1:3003 {
	import runner
	reverse_proxy vallus-rs-be:3003
}

{$RUNNER_HOST:localhost}:3004, localhost:3004, 127.0.0.1:3004 {
	import runner
	reverse_proxy vallus-rs-be:3004
}
