function json(data, status = 200) { return new Response(JSON.stringify(data), { status, headers: { "Content-Type": "application/json; charset=utf-8", "Cache-Control": "no-store" } }); } function authorised(request, env) { const supplied = request.headers.get("X-Admin-Password"); return Boolean( env.ADMIN_PASSWORD && supplied && supplied === env.ADMIN_PASSWORD ); } // Display a readable article title in the administration panel. function articleTitle(id) { const titles = { landscape: "The Landscape We Have Learned Not to See" }; return titles[id] || id; } function adminPage() { return new Response(`
Enter your administration password to review comments.
`, { headers: { "Content-Type": "text/html; charset=utf-8", "Cache-Control": "no-store", "X-Frame-Options": "DENY" } }); } export default { async fetch(request, env) { const url = new URL(request.url); const path = url.pathname; // Administration page if (path === "/admin" || path === "/admin/") { if (request.method !== "GET") { return new Response("Method not allowed", { status: 405 }); } return adminPage(); } // Submit a comment if (path === "/api/comments" && request.method === "POST") { try { const data = await request.json(); const name = String(data.name || "").trim(); const comment = String(data.comment || "").trim(); const article_id = String( data.article_id || "landscape" ).trim(); if (!name || !comment || !article_id) { return json({ error: "Please enter your name and comment." }, 400); } if ( name.length > 100 || comment.length > 2000 || article_id.length > 100 || !/^[a-z0-9-]+$/.test(article_id) ) { return json({ error: "Invalid or excessively long submission." }, 400); } await env.DB.prepare( `INSERT INTO comments (name, comment, status, article_id) VALUES (?, ?, 'pending', ?)` ).bind(name, comment, article_id).run(); return json({ success: true, message: "Thank you. Your comment is awaiting approval." }); } catch (error) { return json({ error: "Unable to submit your comment." }, 500); } } // Retrieve approved comments. // Ten per page, newest first. if (path === "/api/comments" && request.method === "GET") { try { const article_id = url.searchParams.get("article_id") || "landscape"; const rawPage = url.searchParams.get("page") || "1"; const pageNumber = Number(rawPage); if ( !Number.isSafeInteger(pageNumber) || pageNumber < 1 || pageNumber > 100000 ) { return json({ error: "Invalid page number." }, 400); } const pageSize = 10; const offset = (pageNumber - 1) * pageSize; const result = await env.DB.prepare( `SELECT id, name, comment, created_at FROM comments WHERE status = 'approved' AND article_id = ? ORDER BY id DESC LIMIT ? OFFSET ?` ).bind( article_id, pageSize, offset ).all(); return json(result.results); } catch (error) { return json({ error: "Unable to load comments." }, 500); } } // Retrieve comments awaiting moderation. if ( path === "/api/admin/comments" && request.method === "GET" ) { if (!authorised(request, env)) { return json({ error: "Incorrect password." }, 401); } try { const result = await env.DB.prepare( `SELECT id, name, comment, article_id, created_at FROM comments WHERE status = 'pending' ORDER BY id ASC` ).all(); return json(result.results); } catch (error) { return json({ error: "Unable to load comments." }, 500); } } // Approve or reject a comment. const match = path.match(/^\/api\/admin\/comments\/(\d+)$/); if (match && request.method === "POST") { if (!authorised(request, env)) { return json({ error: "Incorrect password." }, 401); } try { const data = await request.json(); const status = data.status; if (!["approved", "rejected"].includes(status)) { return json({ error: "Invalid status." }, 400); } await env.DB.prepare( `UPDATE comments SET status = ? WHERE id = ? AND status = 'pending'` ).bind( status, Number(match[1]) ).run(); return json({ success: true }); } catch (error) { return json({ error: "Unable to update comment." }, 500); } } // Protect administration API routes. if (path.startsWith("/api/admin/")) { return new Response("Not found", { status: 404 }); } // Serve existing website. return env.ASSETS.fetch(request); } };