feat(failover): route multiple hostnames through one Worker

Add an allowlisted multi-host entrypoint that delegates each hostname to the
existing origin selector with its own required health status. Hostnames share
the same origin pair and readiness secret, so one Worker can serve several
independent Routes without duplicating the deployment.

Validation: 21 Worker tests and independent review passed.

Disclosure: I'm an AI assistant helping the author; I'm not the author.
This commit is contained in:
deytenit committed 2026-10-11 20:36:50 +03:00
1 parent 30760bb4f8
commit 31938681dc
3 files changed
+91 -1

No files matched your search

@@ -0,0 +1,23 @@
import singleHost from './worker.mjs';
function unavailable(status) {
return new Response(status === 421 ? 'Misdirected request' : 'Service unavailable', {
status, headers: { 'Cache-Control': 'private, no-store' },
});
}
export default {
async fetch(request, env) {
let hosts;
try { hosts = JSON.parse(env?.APEX_PUBLIC_HOSTS); } catch { return unavailable(503); }
if (!hosts || Array.isArray(hosts) || typeof hosts !== 'object') return unavailable(503);
const entries = Object.entries(hosts);
if (!entries.length || entries.length > 64 || entries.some(([name, status]) =>
name !== name.toLowerCase() || name.length > 253 || !name.includes('.')
|| !name.split('.').every((label) => /^[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?$/.test(label))
|| !['200', '204'].includes(status))) return unavailable(503);
const host = new URL(request.url).hostname;
if (!Object.hasOwn(hosts, host)) return unavailable(421);
return singleHost.fetch(request, { ...env, APEX_PUBLIC_HOST: host, APEX_READY_STATUS: hosts[host] });
},
};