import { blob } from "https://esm.town/v/std/blob?v=11";
import { sqlite } from "https://esm.town/v/std/sqlite?v=11";
import { Hono } from "npm:hono";
await sqlite.execute(`CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)`);
const app = new Hono();
app.get("/", async (c) => {
const comments = await sqlite.query("SELECT text, created_at FROM comments ORDER BY created_at DESC");
const commentsHtml = comments.map(({ text, created_at }) =>
`<div><strong>${new Date(created_at).toLocaleString()}:</strong> ${text}</div>`).join("");
return c.html(`
<!DOCTYPE html>
<html>
<head>
<title>Comments</title>
</head>
<body>
<form action="/submit" method="POST">
<textarea name="comment" required></textarea><br>
<button type="submit">Submit</button>
</form>
<hr>
${commentsHtml}
</body>
</html>
`);
});
app.post("/submit", async (c) => {
const formData = await c.req.parseBody();
const comment = formData.comment;
if (comment) {
await sqlite.execute("INSERT INTO comments (text) VALUES (?)", [comment]);
}
return c.redirect("/");
});
export default app.fetch;