{
	{$CADDY_GLOBAL_OPTIONS}
	# Plus de logs pour diagnostiquer les handshakes TLS (utile avec PhpStorm HTTP client)
	debug

	# Compat : forcer HTTP/1.1 (désactive HTTP/2 et HTTP/3).
	# Certains clients Java/JetBrains peuvent avoir des soucis avec h2/h3 en dev.
	servers {
		protocols h1
	}

	frankenphp {
		{$FRANKENPHP_CONFIG}
	}
}

{$CADDY_EXTRA_CONFIG}

# PhpStorm/Netty peut établir une connexion TLS sans envoyer SNI (ServerName vide).
# Dans ce cas, Caddy tente de sélectionner un certificat par "LocalAddr" (IP du container),
# ici généralement 172.18.0.2. On l'ajoute donc comme fallback pour éviter l'alerte TLS "internal_error".
{$SERVER_NAME:localhost} 172.18.0.2 {
	log {
		# Redact the authorization query parameter that can be set by Mercure
		format filter {
			request>uri query {
				replace authorization REDACTED
			}
		}
	}

	# Forcer un certificat TLS RSA (meilleure compatibilité avec certains clients Java/JetBrains)
	# Conserve l'autorité locale "internal", mais évite ECDSA côté certificat serveur.
	tls internal {
		key_type rsa2048
	}

	root * /app/public
	encode zstd br gzip

	vulcain

	# Mercure désactivé pour la démo : la syntaxe du module Caddy a changé et casse le démarrage.
	# On n'en a pas besoin pour tester l'API via fichiers .http.
	# Add links to the API docs (sans Mercure)
	header ?Link `</docs.jsonld>; rel="http://www.w3.org/ns/hydra/core#apiDocumentation"`
	# Disable Topics tracking if not enabled explicitly: https://github.com/jkarlin/topics
	header ?Permissions-Policy "browsing-topics=()"

	# Matches requests for HTML documents, for static files and for Next.js files,
	# except for known API paths and paths with extensions handled by API Platform
	@pwa expression `(
			header({'Accept': '*text/html*'})
			&& !path(
				'/docs*', '/graphql*', '/bundles*', '/contexts*', '/_profiler*', '/_wdt*',
				'*.json*', '*.html', '*.csv', '*.yml', '*.yaml', '*.xml'
			)
		)
		|| path('/favicon.ico', '/manifest.json', '/robots.txt', '/sitemap*', '/_next*', '/__next*')
		|| query({'_rsc': '*'})`

	# Comment the following line if you don't want Next.js to catch requests for HTML documents.
	# In this case, they will be handled by the PHP app.
	reverse_proxy @pwa http://{$PWA_UPSTREAM}

	php_server
}
