From c2e3b0298ebb31988a3ff0cb6c2b9db736be3c08 Mon Sep 17 00:00:00 2001 From: chahinebrini Date: Thu, 25 Jun 2026 12:33:04 +0200 Subject: [PATCH] feat(server): support --host for LAN binding and export buildApp --- src/cli/commands/server.ts | 4 ++-- src/server/index.ts | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/cli/commands/server.ts b/src/cli/commands/server.ts index 0166940..2c7229b 100644 --- a/src/cli/commands/server.ts +++ b/src/cli/commands/server.ts @@ -1,5 +1,5 @@ import { startServer } from '../../server/index.js'; -export async function serverStart(cwd: string, options: { port?: number } = {}): Promise { - await startServer(cwd, options.port ?? 3377); +export async function serverStart(cwd: string, options: { port: number; host: string }): Promise { + await startServer(cwd, options); } diff --git a/src/server/index.ts b/src/server/index.ts index f4a80d5..f85bc52 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -1,13 +1,25 @@ import Fastify from 'fastify'; import { registerRoutes } from './routes.js'; -export async function startServer(cwd: string, port = 3377): Promise { +export function buildApp(cwd: string) { const app = Fastify({ logger: false }); - await registerRoutes(app, cwd); + registerRoutes(app, cwd); + return app; +} + +export async function startServer(cwd: string, options: { port?: number; host?: string } = {}): Promise<{ app: Fastify.FastifyInstance; url: string }> { + const app = buildApp(cwd); + const port = options.port ?? 3377; + const host = options.host ?? '127.0.0.1'; try { - await app.listen({ port, host: '127.0.0.1' }); - console.log(`AgentHub server listening on http://127.0.0.1:${port}`); + await app.listen({ port, host }); + const address = app.server.address(); + const url = typeof address === 'string' + ? address + : `http://${host === '0.0.0.0' ? '127.0.0.1' : host}:${address?.port ?? port}`; + console.log(`AgentHub server listening on http://${host}:${port}`); + return { app, url }; } catch (err) { app.log.error(err); process.exit(1);