// رمز عبور هش شده (نمونه: "noorschool1402") - در پروژه واقعی از SHA-256 استفاده کنید const ADMIN_PASSWORD_HASH = "8c6976e5b5410415bde908bd4dee15dfb167a9c873fc4bb8a81f6f2ab448a918"; export default { async fetch(request, env) { const url = new URL(request.url); // هدرهای CORS برای ارتباط فرانت و بک const corsHeaders = { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type", }; if (request.method === "OPTIONS") return new Response(null, { headers: corsHeaders }); // مسیر دریافت لیست مصوبات if (url.pathname === "/api/posts" && request.method === "GET") { const posts = await env.NOOR_DB.get("posts") || "[]"; return new Response(posts, { headers: { ...corsHeaders, "Content-Type": "application/json" } }); } // مسیر ثبت لایک (بدون نیاز به رمز) if (url.pathname === "/api/like" && request.method === "POST") { const { id } = await request.json(); let posts = JSON.parse(await env.NOOR_DB.get("posts") || "[]"); posts = posts.map(p => p.id === id ? { ...p, likes: p.likes + 1 } : p); await env.NOOR_DB.put("posts", JSON.stringify(posts)); return new Response("OK", { headers: corsHeaders }); } // مسیر مدیریت (نیاز به چک کردن هش رمز عبور دارد) if (url.pathname === "/api/admin/add" && request.method === "POST") { const body = await request.json(); const { password, postData } = body; // بررسی هش رمز عبور ارسال شده const encoder = new TextEncoder(); const data = encoder.encode(password); const hashBuffer = await crypto.subtle.digest("SHA-256", data); const hashArray = Array.from(new Uint8Array(hashBuffer)); const submittedHash = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); if (submittedHash !== ADMIN_PASSWORD_HASH) { return new Response("Unauthorized", { status: 401, headers: corsHeaders }); } let posts = JSON.parse(await env.NOOR_DB.get("posts") || "[]"); posts.push({ ...postData, id: Date.now(), likes: 0 }); await env.NOOR_DB.put("posts", JSON.stringify(posts)); return new Response("Success", { headers: corsHeaders }); } return new Response("Not Found", { status: 404 }); } };