import { blob } from "https://esm.town/v/std/blob";
const COMMENTS_KEY = "comments_storage";
const getHtml = (comments: string[]) => `
<html>
<head>
<title>Comment Box</title>
</head>
<body>
<h1>Comment Box</h1>
<form method="POST">
<textarea name="comment" rows="4" cols="50"></textarea><br />
<button type="submit">Submit</button>
</form>
<h2>Previous Comments:</h2>
<ul>
${comments.map(comment => `<li>${comment}</li>`).join('')}
</ul>
</body>
</html>
`;
export default async function main(req: Request): Promise<Response> {
let comments: string[] = await blob.getJSON(COMMENTS_KEY) || [];
if (req.method === "POST") {
const formData = await req.formData();
const newComment = formData.get("comment")?.toString().trim();
if (newComment) {
comments.push(newComment);
await blob.setJSON(COMMENTS_KEY, comments);
return Response.redirect(new URL(req.url).origin);
}
}
return new Response(getHtml(comments), {
headers: { "Content-Type": "text/html" }
});
}