import root from "./routes/root.ts"; import user from "./routes/user.ts"; import backfill from "./backfill/index.ts"; import { clearCookies, PORT, ROOT_DOMAIN, SUBDOMAIN_REGEX } from "./utils.ts"; const db = await backfill; Deno.serve({ port: PORT, hostname: ROOT_DOMAIN }, async (req) => { const reqUrl = new URL(req.url); const subdomain = reqUrl.hostname.match(SUBDOMAIN_REGEX)?.at(0)?.split("."); // redirect ROOT_DOMAIN => www.ROOT_DOMAIN if (!subdomain) { const redirUrl = new URL(reqUrl.toString()); redirUrl.host = "www." + redirUrl.host; return new Response(`Redirecting to ${redirUrl.toString()}`, { status: 308, headers: { Location: redirUrl.toString(), }, }); } if (subdomain.length === 1 && subdomain[0] === "www") return root(req); // did:plc example: `sjkdgfjk.did-plc.ROOT_DOMAIN` // did:web example: `vielle.dev.did-web.ROOT_DOMAIN // last segment must be did-plc or did-web const isDidSubdomain = !!subdomain.at(-1)?.match(/^did-(web|plc)+$/gm); // ex: vielle.dev.ROOT_DOMAIN // cannot contain hyphen in top level domain const isHandleSubdomain = !isDidSubdomain && subdomain.length > 1; if (isDidSubdomain || isHandleSubdomain) { const res: Response = await user( db, req, isDidSubdomain ? { did: `did:${subdomain.at(-1) === "did-plc" ? "plc" : "web"}:${subdomain .slice(0, -1) .join(".")}`, } : { handle: subdomain.join(".") as `${string}.${string}`, } ); return new Response(res.body, { ...res, headers: { ...res.headers, ...clearCookies(req), }, }); } return new Response( `Could not resolve domain "${subdomain.join(".")}.${ROOT_DOMAIN}"`, { status: 404, } ); });