Files
VisionL/packages/daemon/src/server.ts
T
2026-08-12 21:29:47 +08:00

38 lines
1.3 KiB
TypeScript

import http from 'node:http';
import { healthRoute } from './routes/health.js';
import { pageRoutes } from './routes/pages.js';
import { contentRoutes } from './routes/content.js';
import { actionRoutes } from './routes/actions.js';
import { profilesRoute } from './routes/profiles.js';
import type { BrowserManager } from './browser-manager.js';
import { createWsRelay } from './ws-relay.js';
export function startServer(port: number, browserManager?: BrowserManager): Promise<http.Server> {
const routes = [healthRoute, profilesRoute];
if (browserManager) {
routes.push(pageRoutes(browserManager));
routes.push(contentRoutes(browserManager));
routes.push(actionRoutes(browserManager));
}
return new Promise((resolve) => {
const server = http.createServer((req, res) => {
for (const route of routes) {
if (route(req, res)) return;
}
res.writeHead(404, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: false, error: { code: 'INTERNAL', message: 'not found' } }));
});
server.listen(port, '127.0.0.1', () => {
console.log(`[daemon] VisionL daemon started on http://127.0.0.1:${port}`);
createWsRelay(server);
resolve(server);
});
});
}
// Direct start when running as script
const port = parseInt(process.env.VISIONL_PORT || '9527', 10);
startServer(port);