import { blob } from "https://esm.town/v/std/blob";
const KEY = new URL(import.meta.url).pathname;
export default async function main(req: Request): Promise<Response> {
const url = new URL(req.url);
if (url.pathname === "/api/entries") {
if (req.method === "GET") {
const entries = await blob.getJSON(KEY).catch(() => []) || [];
return Response.json(entries);
} else if (req.method === "POST") {
const entry = await req.json();
const entries = await blob.getJSON(KEY).catch(() => []) || [];
entries.push(entry);
await blob.setJSON(KEY, entries);
return Response.json({ success: true });
}
}
if (req.method === "POST") {
const formData = await req.formData();
const entry = Object.fromEntries(formData);
const entries = await blob.getJSON(KEY).catch(() => []) || [];
entries.push(entry);
await blob.setJSON(KEY, entries);
return Response.redirect(req.url);
}
const entries = await blob.getJSON(KEY).catch(() => []) || [];
const html = `
<!DOCTYPE html>
<html>
<body>
<form method="POST">
<input name="title" placeholder="Video Title" required><br>
<input name="url" type="url" placeholder="Video URL" required><br>
<input name="date" type="date" required><br>
<input name="speaker" placeholder="Speaker" required><br>
<textarea name="description" placeholder="Description" required></textarea><br>
<button type="submit">Submit</button>
</form>
<h2>Entries:</h2>
<ul>
${entries.map(entry => `
<li>${Object.entries(entry).map(([k, v]) => `${k}: ${v}`).join(', ')}</li>
`).join('')}
</ul>
</body>
</html>
`;
return new Response(html, {
headers: { "Content-Type": "text/html" },
});
}