import { Hono } from "npm:hono@3.0.0";
import { sqlite } from "https://esm.town/v/std/sqlite";
import { html } from "https://esm.town/v/stevekrouse/html";
await sqlite.execute(`
CREATE TABLE IF NOT EXISTS comments (
id INTEGER PRIMARY KEY AUTOINCREMENT,
content TEXT NOT NULL
)
`);
export default async function main(req: Request): Promise<Response> {
if (req.method === "POST") {
const formData = await req.formData();
const content = formData.get("content");
if (typeof content === "string" && content.trim() !== "") {
await sqlite.execute("INSERT INTO comments (content) VALUES (?)", [content]);
}
return Response.redirect(new URL(req.url).origin);
}
const { values: comments } = await sqlite.execute("SELECT content FROM comments");
const commentsHTML = comments.map(({ content }) => `<p>${content}</p>`).join("");
const responseHTML = `
<html>
<head>
<title>Comment Box</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; }
form { margin-bottom: 20px; }
</style>
</head>
<body>
<h1>Comment Box</h1>
<form method="POST">
<textarea name="content" rows="4" cols="50" placeholder="Type your comment here..." required></textarea>
<br>
<button type="submit">Submit</button>
</form>
<h2>Comments:</h2>
${commentsHTML}
</body>
</html>
`;
return html(responseHTML);
}