#=============================================================================
# ExecuteR - Caddy site (single self-contained Caddyfile)
#=============================================================================
#
# Fronts the three stack services on one public TLS edge:
#   /                  -> WebUI            (host port 13080)
#   /api/              -> Backend API      (host port 19080)
#   /messaging/agent   -> NATS WebSocket   (host port 19222)
#   /messaging/browser -> NATS WebSocket   (host port 19222)
#
# BEFORE USE, replace:
#   control.example.com      your public hostname
#   10.0.0.10                the Docker host running the ExecuteR stack
#
# Caddy terminates TLS itself: certificates are obtained and renewed
# automatically via ACME - no certificate paths to manage. The :80 listener
# (redirect + ACME challenge) is automatic as well. Runs on Windows and
# Linux alike: https://caddyserver.com/docs/install
#
# Standalone server instead of the compose stack? Point all three handles
# at the single binary (default https://10.0.0.10:9080 with the
# skip-verify transport shown at the bottom).
#=============================================================================

control.example.com {

	# --- Forwarded headers ---------------------------------------------------
	# Caddy sends X-Forwarded-For / -Proto / -Host on its own. The backend
	# only honors them from trusted proxy addresses (private networks by
	# default), so make sure THIS host is covered by TRUSTED_PROXIES /
	# TRUSTED_NETWORKS. Change the prefix only when mounting under a
	# sub-path instead of the site root.
	request_header X-Forwarded-Prefix /

	# Security headers (the app is same-origin; adjust if you embed it).
	header {
		X-Content-Type-Options    "nosniff"
		X-Frame-Options           "SAMEORIGIN"
		Referrer-Policy           "same-origin"
		Strict-Transport-Security "max-age=31536000"
	}

	# ----------------------------------------------------------------------
	# Optional: restrict the whole site to known networks.
	# Uncomment and edit, then reload Caddy.
	# ----------------------------------------------------------------------
	# @outside {
	#	not remote_ip 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16 203.0.113.0/24
	# }
	# respond @outside 403

	# ── NATS WebSocket: agents and browsers ───────────────────────────────
	# WebSocket upgrades pass through reverse_proxy automatically and are
	# not timed out mid-stream.
	handle /messaging/* {
		reverse_proxy 10.0.0.10:19222
	}

	# ── Backend API (enrollment, sign-in, OIDC callback included) ─────────
	# Edge rate limiting needs the caddy-ratelimit plugin
	# (github.com/mholt/caddy-ratelimit); the backend rate limits
	# enrollment and sign-in itself, so the edge layer is optional here.
	handle /api/* {
		reverse_proxy 10.0.0.10:19080
	}

	# ── WebUI (everything else) ───────────────────────────────────────────
	handle {
		reverse_proxy 10.0.0.10:13080
	}

	# ----------------------------------------------------------------------
	# Backend running with TLS_SELF_SIGNED=1? Proxy the API upstream over
	# https instead - the hop stays encrypted; the self-signed roots simply
	# are not distributed. Either skip verification:
	#
	#   handle /api/* {
	#       reverse_proxy https://10.0.0.10:19080 {
	#           transport http {
	#               tls
	#               tls_insecure_skip_verify
	#           }
	#       }
	#   }
	#
	# ...or import root.crt + intermediate.crt from <data>/tls and pin them
	# (transport http > tls_trust_pool file ...), re-importing when the
	# chain rotates at expiry. Follow your organization's security
	# standards.
	# ----------------------------------------------------------------------
}
