The public endpoint accepts messages, not moderation commands
The article itself is served as static HTML. When a reader submits a comment, the browser sends a bounded form request to the public Cloudflare Worker. The Worker accepts only the configured blog origins for browser submissions, validates the fields, applies rate limits, and stores the message as pending in D1. The browser gets a receipt; it does not get a D1 credential or an administrative API.
The Worker handles the public routes for comments and contact messages. Unknown routes and methods are rejected. The optional contact form is kept separate from article discussions even though both use the same service boundary. The distinction matters: private contact text is never copied into a public article snapshot.
var sql = "INSERT OR IGNORE INTO comments (post_id, name, author_email, comment, status, reply_to, quoted_name, quoted_comment, submission_key, language, article_title, ip_address, country, region, city) " +
"SELECT ?, ?, ?, ?, 'pending', ?, ?, ?, ?, ?, ?, ?, ?, ?, ? WHERE NOT EXISTS " +
"(SELECT 1 FROM article_discussions WHERE post_id = ? AND closed = 1)";
The insert checks the article's discussion flag in the same SQL operation that creates the pending comment. A reader cannot bypass a closed discussion by keeping an old form open or sending the request directly. The submission key makes retries idempotent: if a connection fails after D1 committed, repeating the same submission returns the saved record instead of creating another comment or notification job.
Values are bound parameters. The SQL statements are fixed application source, not fragments assembled from request fields. The comment and its durable mail job are written in a D1 batch so the submission cannot be saved without its moderator alert being queued.
D1 holds workflow state; the public site remains a static snapshot
D1 stores pending, approved, and rejected comments, article-level discussion state, private contact messages, idempotency keys, publication receipts, and durable mail jobs. A scheduled Worker run drains mail jobs that are ready for delivery. Transient mail failures leave work available for retry; the submission request does not synchronously wait for an email provider to deliver a message.
The public Worker has no moderation route. The moderation interface runs on the trusted local lighttpd host. It uses its protected D1 API credential to read and change production state. That credential stays on the private host and is never sent to the browser or the public Worker.
After approval, the local application exports only approved public fields into a generated discussion snapshot. It builds a clean static site, sends the finished site to the Pi400 for its Cloudflare Pages upload, and waits for a successful deployment acknowledgment. Only then does it record the published state and queue an approval email for a reader who supplied an address. Rejection never enters the snapshot. Closing a discussion changes the D1 gate immediately and starts a rebuild so the static form also reflects the change.
Readers therefore fetch article HTML and its embedded conversation from Pages; page reading does not query D1. D1 is the mutable workflow store, while each successful Pages deployment is the public read model. If publication fails, approval and publication remain distinct states, and the local tool can retry the build without pretending the comment is already visible.
Keep each trust boundary visible
The Worker is the narrow public write boundary. D1 is the authoritative store for message and moderation state. The local application is the operator boundary. Pages serves the last confirmed public snapshot. Keeping these roles separate limits what a compromised or misconfigured component can do, and makes a failed email or deployment a recoverable phase instead of an ambiguous success.
