import { blob } from "https://esm.town/v/std/blob";
const KEY = new URL(import.meta.url).pathname;
interface Comment {
text: string;
timestamp: number;
}
export default async function main(req: Request): Promise<Response> {
if (req.method === "GET") {
const comments = await blob.getJSON<Comment[]>(KEY) || [];
return Response.json(comments);
} else if (req.method === "POST") {
const text = await req.text();
const comments = await blob.getJSON<Comment[]>(KEY) || [];
comments.push({ text, timestamp: Date.now() });
await blob.setJSON(KEY, comments);
return new Response("Comment added successfully", { status: 201 });
} else {
return new Response("Method not allowed", { status: 405 });
}
}