From 554ab93df2fc14dc39ecabd91dae979b6b23bbe8 Mon Sep 17 00:00:00 2001 From: hwhung Date: Mon, 27 Apr 2026 12:27:27 +0800 Subject: [PATCH] Support deployment CLI flags --- readme.md | 61 ++++++++++++++++++++++++----------------------- src/lib/config.ts | 58 ++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 87 insertions(+), 32 deletions(-) diff --git a/readme.md b/readme.md index 9978a33..216d942 100644 --- a/readme.md +++ b/readme.md @@ -13,45 +13,46 @@ pnpm dev If plain `pnpm` is unavailable, use `corepack pnpm` for the same commands. Do not edit `package.json`. -The app runs without `config.json`: +Default startup uses: - host: `0.0.0.0` - port: `24731` - bearer token: `dev-proxy-key` -## Optional Provider Config +## Provider Wiring -Do not create `config.json` unless internal provider endpoints/tokens are available. +Prefer CLI flags. Do not create `config.json` for deployment. -If they are available, create `config.json` in one write with this shape: +If internal provider endpoints/tokens are available, start with: -```json -{ - "server": { - "host": "0.0.0.0", - "port": 24731 - }, - "proxy": { - "apiKey": "dev-proxy-key" - }, - "providers": { - "openai": { - "apiKey": "", - "baseURL": "" - }, - "anthropic": { - "apiKey": "", - "baseURL": "" - } - }, - "logging": { - "level": "info", - "pretty": true - } -} +```bash +pnpm dev -- \ + --host 0.0.0.0 \ + --port 24731 \ + --proxy-api-key dev-proxy-key \ + --openai-base-url "" \ + --openai-api-key "" \ + --anthropic-base-url "" \ + --anthropic-api-key "" ``` -Fill only `providers.openai.apiKey`, `providers.openai.baseURL`, `providers.anthropic.apiKey`, and `providers.anthropic.baseURL`. Use internal endpoints only. Never use public official provider endpoints. +Use internal endpoints only. Never use public official provider endpoints. Do not print provider tokens. + +Supported setup flags: + +- `--host` +- `--port` +- `--proxy-api-key` +- `--openai-base-url` +- `--openai-api-key` +- `--anthropic-base-url` +- `--anthropic-api-key` +- `--log-level` +- `--pretty-logs` +- `--no-pretty-logs` +- `--config` + +CLI flags override `config.json`. `config.json` is only for local/manual use. ## Verify @@ -61,4 +62,4 @@ curl -s http://localhost:24731/dashboard | head curl -s -H "Authorization: Bearer dev-proxy-key" http://localhost:24731/v1/models ``` -Expected without provider config: dashboard, health, and model list work; chat completions return `503`. +Expected without provider flags: dashboard, health, and model list work; chat completions return `503`. diff --git a/src/lib/config.ts b/src/lib/config.ts index ae2a909..1a52001 100644 --- a/src/lib/config.ts +++ b/src/lib/config.ts @@ -116,10 +116,64 @@ function configPathFromArgv(): string { return path.resolve("config.json"); } +function argValue(name: string): string | undefined { + const longName = `--${name}`; + const index = process.argv.findIndex((arg) => arg === longName); + if (index >= 0) { + const value = process.argv[index + 1]; + return value && !value.startsWith("--") ? value : undefined; + } + + const inlineArg = process.argv.find((arg) => arg.startsWith(`${longName}=`)); + return inlineArg ? inlineArg.slice(longName.length + 1) : undefined; +} + +function argFlag(name: string): boolean { + return process.argv.includes(`--${name}`); +} + +function applyArgOverrides(base: AppConfig): AppConfig { + const rawPort = argValue("port"); + const port = rawPort === undefined ? base.server.port : Number(rawPort); + if (!Number.isInteger(port) || port <= 0) { + throw new Error(`Invalid CLI value "--port": ${rawPort}`); + } + + return { + server: { + host: argValue("host") ?? base.server.host, + port, + }, + proxy: { + apiKey: argValue("proxy-api-key") ?? base.proxy.apiKey, + }, + providers: { + openai: { + ...base.providers.openai, + ...(argValue("openai-base-url") ? { baseURL: argValue("openai-base-url") } : {}), + ...(argValue("openai-api-key") ? { apiKey: argValue("openai-api-key") } : {}), + }, + anthropic: { + ...base.providers.anthropic, + ...(argValue("anthropic-base-url") ? { baseURL: argValue("anthropic-base-url") } : {}), + ...(argValue("anthropic-api-key") ? { apiKey: argValue("anthropic-api-key") } : {}), + }, + }, + logging: { + level: argValue("log-level") ?? base.logging.level, + pretty: argFlag("pretty-logs") + ? true + : argFlag("no-pretty-logs") + ? false + : base.logging.pretty, + }, + }; +} + function loadConfig(): AppConfig { const filePath = configPathFromArgv(); if (!existsSync(filePath)) { - return DEFAULT_CONFIG; + return applyArgOverrides(DEFAULT_CONFIG); } let parsed: unknown; @@ -130,7 +184,7 @@ function loadConfig(): AppConfig { throw new Error(`Could not read config file ${filePath}: ${message}`); } - return mergeConfig(parsed); + return applyArgOverrides(mergeConfig(parsed)); } export const config = loadConfig();