wehub-cloud-tpl-function
35766be779
Co-authored-by: Cursor <cursoragent@cursor.com>
48 行
1.5 KiB
JavaScript
48 行
1.5 KiB
JavaScript
// function 测试数据:单 function(event),入口 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"]
|
||
});
|
||
};
|