import { fetch } from "https://esm.town/v/std/fetch";
export let blocks_inner = async (req, res) => {
const [_, user, id, ...file] = req.path.split("/");
const gist = await fetch(`https://api.github.com/gists/${id}`).then((r) => r.json());
res.set("Cache-Control", "public,max-age=64800");
if (file.filter(Boolean).length) {
if (gist.files[file.filter(Boolean).join("/")].raw_url.endsWith(".js")) {
res.set("Content-Type", "application/javascript");
return res.send(
await fetch(gist.files[file.filter(Boolean).join("/")].raw_url).then((
r,
) => r.text()),
);
}
return res.redirect(gist.files[file.filter(Boolean).join("/")].raw_url);
}
console.log(gist);
const index = gist.files["index.html"];
if (!index) {
return res.end({ message: "Gist did not contain index.html" });
}
return res.status(200).send(`<html>
<body>
${index.content}
</body>
</html>
`);
};