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
This commit is contained in:
weishu
2026-01-07 14:39:54 +08:00
parent 89236d1d2e
commit a627b085c8
6 changed files with 57 additions and 3 deletions
+30 -2
View File
@@ -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')