feat(server): support --host for LAN binding and export buildApp

This commit is contained in:
chahinebrini 2026-06-25 12:33:04 +02:00
parent 407202f092
commit c2e3b0298e
2 changed files with 18 additions and 6 deletions

View File

@ -1,5 +1,5 @@
import { startServer } from '../../server/index.js'; import { startServer } from '../../server/index.js';
export async function serverStart(cwd: string, options: { port?: number } = {}): Promise<void> { export async function serverStart(cwd: string, options: { port: number; host: string }): Promise<void> {
await startServer(cwd, options.port ?? 3377); await startServer(cwd, options);
} }

View File

@ -1,13 +1,25 @@
import Fastify from 'fastify'; import Fastify from 'fastify';
import { registerRoutes } from './routes.js'; import { registerRoutes } from './routes.js';
export async function startServer(cwd: string, port = 3377): Promise<void> { export function buildApp(cwd: string) {
const app = Fastify({ logger: false }); 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 { try {
await app.listen({ port, host: '127.0.0.1' }); await app.listen({ port, host });
console.log(`AgentHub server listening on http://127.0.0.1:${port}`); 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) { } catch (err) {
app.log.error(err); app.log.error(err);
process.exit(1); process.exit(1);