This commit is contained in:
Clove 2026-06-18 02:03:04 +01:00
parent 6d9769459b
commit e740b8ee6b
2 changed files with 21 additions and 14 deletions

View File

@ -32,6 +32,6 @@ pnpx wrangler secret put TURNSTILE_SECRET
# Skip this step to run with just honeypot + rate limiting. # Skip this step to run with just honeypot + rate limiting.
# 3. Run locally / deploy # 3. Run locally / deploy
pnpm run dev pnpm dev
pnpm run deploy pnpm deploy
``` ```

View File

@ -26,7 +26,7 @@ interface PostBody {
const ENTRIES_KEY = "entries"; const ENTRIES_KEY = "entries";
const MAX_ENTRIES = 1000; // keep the JSON blob bounded const MAX_ENTRIES = 1000; // keep the JSON blob bounded
const RATE_LIMIT_SECONDS = 30; // min seconds between posts from one IP const RATE_LIMIT_SECONDS = 60; // min seconds between posts from one IP (also KV's minimum expirationTtl)
const LIMITS = { const LIMITS = {
name: 40, name: 40,
@ -198,17 +198,24 @@ async function handlePost(request: Request, env: Env): Promise<Response> {
export default { export default {
async fetch(request: Request, env: Env): Promise<Response> { async fetch(request: Request, env: Env): Promise<Response> {
try {
const url = new URL(request.url); const url = new URL(request.url);
if (request.method === "OPTIONS") { if (request.method === "OPTIONS") {
return new Response(null, { status: 204, headers: corsHeaders(env) }); return new Response(null, { status: 204, headers: corsHeaders(env) });
} }
if (request.method === "GET") { if (request.method === "GET") {
return handleGet(url, env); return await handleGet(url, env);
} }
if (request.method === "POST") { if (request.method === "POST") {
return handlePost(request, env); return await handlePost(request, env);
} }
return json({ error: "Method not allowed." }, 405, env); return json({ error: "Method not allowed." }, 405, env);
} catch (err) {
// Always attach CORS headers, even on unexpected errors, so the browser
// surfaces a real message instead of a masked CORS/network error.
console.error("[guestbook] unhandled error", err);
return json({ error: "Internal error." }, 500, env);
}
}, },
} satisfies ExportedHandler<Env>; } satisfies ExportedHandler<Env>;