Skip to content

Commit fe85f5a

Browse files
feat: 添加 AUTO_PORT 环境变量支持自动端口配置
- 在 config.ts 中添加 autoPort 字段和 AUTO_PORT 环境变量检测 - 当 AUTO_PORT=true 时,serve 函数不传递端口参数,让 Deno 自动分配端口 - 保持向后兼容,默认使用环境变量 PORT 或默认端口 3456
1 parent 443f649 commit fe85f5a

2 files changed

Lines changed: 9 additions & 2 deletions

File tree

deno-proxy/src/config.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface ProxyConfig {
99
aggregationIntervalMs: number;
1010
maxRequestsPerMinute: number;
1111
tokenMultiplier: number;
12+
autoPort: boolean;
1213
}
1314

1415
// 解析 TOKEN_MULTIPLIER,兼容常见字符串形式:
@@ -55,7 +56,12 @@ export function loadConfig(): ProxyConfig {
5556
throw new Error("UPSTREAM_BASE_URL must be provided");
5657
}
5758

58-
const port = Number(Deno.env.get("PORT") ?? "3456");
59+
// 检查是否启用自动端口配置
60+
const autoPort = Deno.env.get("AUTO_PORT") === "true";
61+
62+
// 如果启用自动端口,则使用 0 让系统自动分配端口
63+
// 否则使用环境变量指定的端口或默认端口 3456
64+
const port = autoPort ? 0 : Number(Deno.env.get("PORT") ?? "3456");
5965
const host = Deno.env.get("HOST") ?? "0.0.0.0";
6066
const upstreamApiKey = Deno.env.get("UPSTREAM_API_KEY");
6167
const upstreamModelOverride = Deno.env.get("UPSTREAM_MODEL");
@@ -77,5 +83,6 @@ export function loadConfig(): ProxyConfig {
7783
aggregationIntervalMs,
7884
maxRequestsPerMinute,
7985
tokenMultiplier,
86+
autoPort,
8087
};
8188
}

deno-proxy/src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,4 +277,4 @@ serve((req) => {
277277
}
278278

279279
return new Response("Not Found", { status: 404 });
280-
}, { hostname: config.host, port: config.port });
280+
}, config.autoPort ? undefined : { hostname: config.host, port: config.port });

0 commit comments

Comments
 (0)