lichen.wiki {
	tls {
		dns cloudflare {env.CLOUDFLARE_API_TOKEN}
	}

	encode gzip

	# Immutable blob cache (only if blobs are content-addressed / versioned)
	handle /blob/* {
		header Cache-Control "public, max-age=31536000, immutable"
		reverse_proxy localhost:3000
	}

	# Static assets served from disk
	handle /public/* {
		uri strip_prefix /public
		root * /opt/lichen/public
		file_server

		header Cache-Control "public, max-age=31536000, immutable"
	}
	
	# HTML microcache
	handle {
		header Cache-Control "public, max-age=2"
		reverse_proxy localhost:3000
	}

	# Override the long immutable cache on any error so a stray 404 (missing
	# build artifact, typo'd asset path) can't get pinned at the CDN edge.
	handle_errors {
		header Cache-Control "public, max-age=60"
		respond "{err.status_code} {err.status_text}" {err.status_code}
	}
}

staging.lichen.wiki {
	tls {
		dns cloudflare {env.CLOUDFLARE_API_TOKEN}
	}

	encode gzip

	handle /blob/* {
		header Cache-Control "public, max-age=31536000, immutable"
		reverse_proxy localhost:3001
	}

	handle /public/* {
		uri strip_prefix /public
		root * /opt/lichen-staging/public
		file_server

		header Cache-Control "public, max-age=31536000, immutable"
	}

	handle {
		header Cache-Control "no-store"
		reverse_proxy localhost:3001
	}

	handle_errors {
		header Cache-Control "public, max-age=60"
		respond "{err.status_code} {err.status_text}" {err.status_code}
	}
}

stats.lichen.wiki {
	tls {
		dns cloudflare {env.CLOUDFLARE_API_TOKEN}
	}

	encode gzip
	reverse_proxy localhost:8081
}
