import { blob } from "https://esm.town/v/std/blob";
import { Hono } from "npm:hono@3";
import { html } from "https://esm.town/v/stevekrouse/html";
const BLOB_KEY = "comments_storage";
const app = new Hono();
app.get("/", async (c) => {
const comments = await blob.getJSON(BLOB_KEY) ?? [];
return c.html(html`
<html>
<body>
<h1>Comments</h1>
<ul>
${comments.map(comment => `<li>${comment}</li>`).join('')}
</ul>
<form action="/" method="POST">
<input name="comment" type="text" placeholder="Enter your comment here" required />
<button type="submit">Submit</button>
</form>
</body>
</html>
`);
});
app.post("/", async (c) => {
const form = await c.req.parseBody();
const newComment = form.comment;
if (newComment) {
const comments = await blob.getJSON(BLOB_KEY) ?? [];
comments.push(newComment);
await blob.setJSON(BLOB_KEY, comments);
}
return c.redirect("/");
});
export default app.fetch;