import { blob } from "https://esm.town/v/std/blob?v=12";
import { Hono } from "npm:hono@3";
import { html } from "https://esm.town/v/stevekrouse/html?v=5";
const COMMENTS_KEY = "comments_storage";
const template = (comments: any) => `
<html>
<head>
<title>Comment Box</title>
<style>
body { font-family: Arial, sans-serif; }
.comment { margin-bottom: 1em; }
.comment-list { margin-bottom: 2em; }
</style>
</head>
<body>
<h1>Comments</h1>
<div class="comment-list">
${comments.map((comment: string) => `<div class="comment">${comment}</div>`).join("")}
</div>
<form method="POST">
<textarea name="comment" rows="4" cols="50" required></textarea><br>
<button type="submit">Submit Comment</button>
</form>
</body>
</html>
`;
const app = new Hono();
app.get("/", async (c) => {
const comments = await blob.getJSON(COMMENTS_KEY) || [];
return c.html(template(comments));
});
app.post("/", async (c) => {
const formData = await c.req.parseBody();
const newComment = formData.comment;
const comments = await blob.getJSON(COMMENTS_KEY) || [];
comments.push(newComment);
await blob.setJSON(COMMENTS_KEY, comments);
return c.redirect("/");
});
export default app.fetch;