项目文件夹

文件
2026-09-11 21:04:56 +08:00

48 行
1.5 KiB
JavaScript

此文件含有模棱两可的 Unicode 字符
此文件含有可能会与其他字符混淆的 Unicode 字符。 如果您是想特意这样的,可以安全地忽略该警告。 使用 Escape 按钮显示他们。
// function 测试数据:单 functionevent),入口 index.handler,路由 /api/cloud/*。
// env 用 ref: 引用 postgres_database 与 object_storage;OPENAI_* 为字面量。
const corsHeaders = {
"access-control-allow-methods": "GET,POST,OPTIONS",
"access-control-allow-headers": "content-type",
"access-control-max-age": "86400"
};
const jsonHeaders = { ...corsHeaders, "content-type": "application/json; charset=utf-8" };
function json(statusCode, body) {
return { statusCode, headers: jsonHeaders, body: JSON.stringify(body) };
}
function requestPath(event) {
return (event && (event.path || event.rawPath)) || "/api/cloud/health";
}
function requestMethod(event) {
return (event && (event.method || event.httpMethod)) || "GET";
}
async function health() {
return json(200, {
ok: true,
service: "wehub-tpl-demo-function",
bindings: {
database: !!process.env.DATABASE_URL,
object_storage: !!(process.env.OBJECT_BUCKET && process.env.OBJECT_ENDPOINT),
model: !!(process.env.OPENAI_BASE_URL && process.env.OPENAI_API_KEY && process.env.OPENAI_MODEL)
}
});
}
exports.handler = async function handler(event) {
const path = requestPath(event);
const method = requestMethod(event).toUpperCase();
if (method === "OPTIONS") {
return { statusCode: 204, headers: corsHeaders, body: "" };
}
if (method === "GET" && path.endsWith("/health")) {
return await health();
}
return json(200, {
ok: true,
routes: ["GET /api/cloud/health"]
});
};