{
	auto_https off
	admin off
}

:{$PORT} {
	root * {$MESSAGES_FRONTEND_ROOT:/app}
	header -Server

	route {
		# Health checks
		respond /__lbheartbeat__ 200
		respond /__lbheartbeat__/ 200

		reverse_proxy /__heartbeat__/* {$MESSAGES_FRONTEND_BACKEND_SERVER:localhost:8000} {
			header_up X-Forwarded-Proto https
			header_up X-Forwarded-For {remote_host}
		}

		# Django backend proxy
		reverse_proxy /api/* {$MESSAGES_FRONTEND_BACKEND_SERVER:localhost:8000} {
			header_up X-Forwarded-Proto https
			header_up X-Forwarded-For {remote_host}
		}

		redir /{$DJANGO_ADMIN_URL:admin} /{$DJANGO_ADMIN_URL:admin}/ 301

		reverse_proxy /{$DJANGO_ADMIN_URL:admin}/* {$MESSAGES_FRONTEND_BACKEND_SERVER:localhost:8000} {
			header_up X-Forwarded-Proto https
			header_up X-Forwarded-For {remote_host}
		}

		reverse_proxy /static/* {$MESSAGES_FRONTEND_BACKEND_SERVER:localhost:8000} {
			header_up X-Forwarded-Proto https
			header_up X-Forwarded-For {remote_host}
		}

		# SPA fallback: any unmatched request returns index.html so the
		# client-side router can resolve the path.
		try_files {path} /index.html
		file_server
	}

	handle_errors {
		@spa_404 expression `{err.status_code} == 404`
		handle @spa_404 {
			rewrite * /index.html
			file_server
		}
	}
}
