config-template / homepage.tsmain at 3b8ad1b
// homepage.ts — THE MIND'S HOMEPAGE, for anyone: GET and HEAD of `/` on every hostname that is
// no app, the domain's own too.
//
// To make the site yours, change this file. Any other path that nothing answers is not found
// (worker.ts), so a webhook to a hostname that nothing serves is not answered as taken.
import type { IterateConfigEntrypoint } from "iterate/sdk";
import { profileHtml, profileOf } from "./profile.ts";
import profileFiles from "./profile/";
/** The homepage: the mind's icon, name and description (profile/), and a link to its Dash. Null
* for anything but GET or HEAD of `/`. */
export async function homepage(
request: Request,
host: Pick<IterateConfigEntrypoint, "getItx">,
): Promise<Response | null> {
if (new URL(request.url).pathname !== "/" || !["GET", "HEAD"].includes(request.method))
return null;
using itx = host.getItx();
const zone = new URL(await itx.url()).hostname;
const safeZone = zone.replaceAll("&", "&").replaceAll('"', """);
// the mind's icon, its name as the heading and its description, as the owner wrote them
const profile = profileHtml(profileOf(profileFiles, zone));
return new Response(
[
"<!doctype html>",
'<meta charset="utf-8">',
...profile.head,
'<body style="font-family:system-ui,-apple-system,sans-serif;max-width:36rem;margin:4rem auto;padding:0 1.25rem;line-height:1.6">',
...profile.body,
`<p><a href="https://dash.${safeZone}/">Open Dash</a></p>`,
"</body>",
"",
].join("\n"),
{ headers: { "content-type": "text/html; charset=utf-8" } },
);
}