export default {
async fetch(request, env) {
const url = new URL(request.url);
const KV = env.BLOG_KV;
// 处理 API 统一存取接口
if (url.pathname === "/api/data") {
// 允许跨域
const headers = {
"Content-Type": "application/json;charset=utf-8",
"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 });
}
if (request.method === "GET") {
const raw = await KV.get("blog_complete_dataset");
return new Response(raw || "{}", { headers });
}
if (request.method === "POST") {
const payload = await request.text();
await KV.put("blog_complete_dataset", payload);
return new Response(JSON.stringify({ status: "success" }), { headers });
}
}
// 默认直接返回单页面 HTML 网页
const rawHTML = `<!-- 这里由 CF 自动嵌入当前的 index.html 完整内容 -->`;
return new Response(rawHTML, {
headers: { "Content-Type": "text/html;charset=utf-8" }
});
}
};