From a627b085c8fa7bb5378535dea6924226a5e74898 Mon Sep 17 00:00:00 2001 From: weishu Date: Wed, 7 Jan 2026 14:35:56 +0800 Subject: [PATCH] feat: add --host and --port CLI arguments to hapi server command. fix #37 Add configurable host binding for the Mini App HTTP server through: - New `webappHost` configuration (defaults to 127.0.0.1 for security) - CLI arguments `--host` and `--port` with flexible parsing (--host=value or --host value) - Priority order: CLI args > env vars > settings.json > defaults - Updated Bun.serve() to bind to the configured hostname Modified: - server/src/config/settings.ts - server/src/config/serverSettings.ts - server/src/configuration.ts - server/src/web/server.ts - server/src/index.ts - cli/src/commands/server.ts --- cli/src/commands/server.ts | 32 +++++++++++++++++++++++++++-- server/src/config/serverSettings.ts | 18 ++++++++++++++++ server/src/config/settings.ts | 1 + server/src/configuration.ts | 5 +++++ server/src/index.ts | 1 + server/src/web/server.ts | 3 ++- 6 files changed, 57 insertions(+), 3 deletions(-) diff --git a/cli/src/commands/server.ts b/cli/src/commands/server.ts index 296c6d79..4275ef24 100644 --- a/cli/src/commands/server.ts +++ b/cli/src/commands/server.ts @@ -1,11 +1,39 @@ import chalk from 'chalk' -import type { CommandDefinition } from './types' +import type { CommandDefinition, CommandContext } from './types' + +function parseServerArgs(args: string[]): { host?: string; port?: string } { + const result: { host?: string; port?: string } = {} + + for (let i = 0; i < args.length; i++) { + const arg = args[i] + if (arg === '--host' && i + 1 < args.length) { + result.host = args[++i] + } else if (arg === '--port' && i + 1 < args.length) { + result.port = args[++i] + } else if (arg.startsWith('--host=')) { + result.host = arg.slice('--host='.length) + } else if (arg.startsWith('--port=')) { + result.port = arg.slice('--port='.length) + } + } + + return result +} export const serverCommand: CommandDefinition = { name: 'server', requiresRuntimeAssets: false, - run: async () => { + run: async (context: CommandContext) => { try { + const { host, port } = parseServerArgs(context.commandArgs) + + if (host) { + process.env.WEBAPP_HOST = host + } + if (port) { + process.env.WEBAPP_PORT = port + } + await import('../../../server/src/index') } catch (error) { console.error(chalk.red('Error:'), error instanceof Error ? error.message : 'Unknown error') diff --git a/server/src/config/serverSettings.ts b/server/src/config/serverSettings.ts index 303aa209..cfc39253 100644 --- a/server/src/config/serverSettings.ts +++ b/server/src/config/serverSettings.ts @@ -12,6 +12,7 @@ import { getSettingsFile, readSettings, writeSettings } from './settings' export interface ServerSettings { telegramBotToken: string | null + webappHost: string webappPort: number webappUrl: string corsOrigins: string[] @@ -21,6 +22,7 @@ export interface ServerSettingsResult { settings: ServerSettings sources: { telegramBotToken: 'env' | 'file' | 'default' + webappHost: 'env' | 'file' | 'default' webappPort: 'env' | 'file' | 'default' webappUrl: 'env' | 'file' | 'default' corsOrigins: 'env' | 'file' | 'default' @@ -82,6 +84,7 @@ export async function loadServerSettings(dataDir: string): Promise file > 127.0.0.1 + let webappHost = '127.0.0.1' + if (process.env.WEBAPP_HOST) { + webappHost = process.env.WEBAPP_HOST + sources.webappHost = 'env' + if (settings.webappHost === undefined) { + settings.webappHost = webappHost + needsSave = true + } + } else if (settings.webappHost !== undefined) { + webappHost = settings.webappHost + sources.webappHost = 'file' + } + // webappPort: env > file > 3006 let webappPort = 3006 if (process.env.WEBAPP_PORT) { @@ -157,6 +174,7 @@ export async function loadServerSettings(dataDir: string): Promise