🎯 ภารกิจ — สาย Cloudflare: Workers และที่เก็บข้อมูล
ลงมือทำจริงในบัญชี AWS หรือ Cloudflare ของคุณ แล้วติ๊กรายการที่ทำได้ครบ ภารกิจที่มีป้าย “บังคับ” ต้องผ่านทุกข้อจึงจะเลื่อนระดับ อย่าลืมลบทรัพยากรที่สร้างเมื่อทำเสร็จ
ภารกิจบังคับ 0/3
API แรกบน Workers แบบมี type และ secret ถูกที่
บังคับสิ่งที่ต้องทำ
สร้าง Worker ด้วย TypeScript ที่มีอย่างน้อย 2 endpoint (หนึ่งในนั้นต้องใช้ secret ตรวจสิทธิ์) ใช้ vars สำหรับค่าไม่ลับ secret สำหรับค่าลับ รัน wrangler types และ deploy ขึ้น workers.dev ดู log สดด้วย wrangler tail แล้วลอง deploy เวอร์ชันใหม่และ rollback
คำสั่ง/โค้ดเริ่มต้น
ลองในCloudflare ↗npm create cloudflare@latest -- my-first-api --type=hello-world --lang=ts --no-deploy --git cd my-first-api echo 'ADMIN_TOKEN="local-dev-only-token"' > .dev.vars grep -q '^.dev.vars' .gitignore || echo '.dev.vars*' >> .gitignore npx wrangler types && npx wrangler dev # หลังทดสอบในเครื่องผ่าน: npx wrangler deploy openssl rand -hex 24 | npx wrangler secret put ADMIN_TOKEN npx wrangler tail --format pretty
เช็กลิสต์
แอปเต็มรูปแบบ - static assets + D1 + R2
บังคับสิ่งที่ต้องทำ
สร้างเว็บ "สมุดเยี่ยมชมพร้อมรูป" ใน Worker เดียว: หน้าเว็บอยู่ใน static assets, ข้อความเก็บใน D1 (schema ผ่าน migration), รูปเก็บใน R2 (อัปโหลดต้องมี token) API อยู่ใต้ /api/* ด้วย run_worker_first และใช้ prepared statement ทุก query
คำสั่ง/โค้ดเริ่มต้น
ลองในCloudflare ↗ช่วยเขียน Cloudflare Worker (TypeScript, wrangler.jsonc, ใช้ type Env ที่ได้จาก `wrangler types`) ที่มี - assets: directory "./public", binding "ASSETS", run_worker_first ["/api/*"] - D1 binding DB ตาราง entries(id, name, message, image_key, created_at) สร้างผ่านไฟล์ migrations/0001_*.sql - R2 binding IMAGES: PUT /api/images/<key> (ต้องมี Authorization: Bearer ตรงกับ secret ADMIN_TOKEN), GET /api/images/<key> - GET/POST /api/entries ใช้ prepare + bind เท่านั้น ตรวจความยาว name ≤ 50 และ message ≤ 500 - public/index.html ฟอร์มส่งข้อความและแสดงรายการล่าสุด 20 รายการ ให้คำสั่ง wrangler ครบ: d1 create, r2 bucket create, migrations apply --local/--remote, deploy และคำสั่งลบทุกอย่างตอนจบ ห้ามใส่ค่าความลับในโค้ดหรือ wrangler.jsonc
เช็กลิสต์
ขึ้นโดเมนจริงพร้อม staging และ cron
บังคับสิ่งที่ต้องทำ
นำ Worker จากภารกิจก่อน (หรือ status-edge จากบท 05) ขึ้น custom domain ของคุณ แยก env staging ที่ใช้ KV/D1 คนละชุด เพิ่ม Cron Trigger ที่ทำงาน idempotent และปิด workers.dev ใน production
คำสั่ง/โค้ดเริ่มต้น
ลองในCloudflare ↗npx wrangler kv namespace create STATUS_STAGING # เพิ่มใน wrangler.jsonc: "workers_dev": false, routes แบบ custom_domain, triggers.crons และ env.staging ที่ประกาศ bindings/vars ซ้ำ npx wrangler types curl -s "http://localhost:8787/cdn-cgi/local/scheduled?cron=*/5+*+*+*+*" npx wrangler deploy --env staging npx wrangler deploy
เช็กลิสต์
ย้ายโปรเจกต์ Pages เดิม (หรือ SPA) มาเป็น Workers static assets
โบนัสสิ่งที่ต้องทำ
ถ้ามีโปรเจกต์ Cloudflare Pages อยู่แล้ว ให้ย้ายมาเป็น Workers + static assets (ถ้าไม่มี ให้สร้าง React SPA ด้วย C3) ตั้ง SPA/404 handling, _headers และ .assetsignore ให้ถูก
คำสั่ง/โค้ดเริ่มต้น
ลองในCloudflare ↗ช่วยย้ายโปรเจกต์ Cloudflare Pages ของฉันไป Workers static assets ข้อมูลโปรเจกต์: framework [ชื่อ], build command [คำสั่ง], output directory [โฟลเดอร์], มี functions/ หรือ _worker.js ไหม: [มี/ไม่มี], ใช้ _headers/_redirects ไหม: [มี/ไม่มี] ขอ wrangler.jsonc ที่ครบ (assets.directory, not_found_handling, binding ถ้าจำเป็น, run_worker_first ถ้ามี API), ไฟล์ .assetsignore, สิ่งที่ต้องย้ายจาก functions/ และ checklist ทดสอบก่อนสลับโดเมน
เช็กลิสต์