Skip to content

Commit 81df3e9

Browse files
feat: 添加部署入口文件和重构main.ts
- 创建deploy.ts作为部署入口,只调用Deno.serve(handler) - 修改main.ts导出handler函数供deploy.ts使用 - 添加import.meta.main检查,确保直接运行时才启动服务器 - 支持模块化导入和独立运行两种模式
1 parent cf4d14e commit 81df3e9

2 files changed

Lines changed: 12 additions & 2 deletions

File tree

deno-proxy/src/deploy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { handler } from "./main.ts";
2+
3+
// 在 Deploy 上只调用这一处 Deno.serve
4+
Deno.serve(handler);

deno-proxy/src/main.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,8 @@ async function handleTokenCount(req: Request, requestId: string) {
247247
}
248248
}
249249

250-
serve((req) => {
250+
// 导出 handler 函数供 deploy.ts 使用
251+
export const handler = (req: Request) => {
251252
const url = new URL(req.url);
252253

253254
if (req.method === "GET" && url.pathname === "/") {
@@ -295,4 +296,9 @@ serve((req) => {
295296

296297
console.log(`404 - ${req.method} ${url.pathname}`);
297298
return new Response("Not Found", { status: 404 });
298-
}, config.autoPort ? undefined : { hostname: config.host, port: config.port });
299+
};
300+
301+
// 如果是直接运行此文件(而不是被导入),则启动服务器
302+
if (import.meta.main) {
303+
serve(handler, config.autoPort ? undefined : { hostname: config.host, port: config.port });
304+
}

0 commit comments

Comments
 (0)