52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
/**
|
||
* 平台服务入口(架构设计第 2 节)。
|
||
*
|
||
* 组装:应用服务 + HTTP / SSE 入口。控制面(core)与引擎适配(agent)在后续阶段接入。
|
||
*/
|
||
import type { Server } from 'node:http';
|
||
import type { AddressInfo } from 'node:net';
|
||
|
||
import { ApplicationService } from './app/service.js';
|
||
import { createHttpServer } from './entry/http.js';
|
||
|
||
export { ApplicationService } from './app/service.js';
|
||
export type { Subject } from './app/subject.js';
|
||
export { createHttpServer } from './entry/http.js';
|
||
|
||
export interface StartOptions {
|
||
host?: string;
|
||
port?: number;
|
||
version?: string;
|
||
}
|
||
|
||
export interface RunningServer {
|
||
url: string;
|
||
port: number;
|
||
service: ApplicationService;
|
||
close(): Promise<void>;
|
||
}
|
||
|
||
export async function startServer(options: StartOptions = {}): Promise<RunningServer> {
|
||
const host = options.host ?? '127.0.0.1';
|
||
const port = options.port ?? 5199;
|
||
const service = new ApplicationService(options.version === undefined ? {} : { version: options.version });
|
||
const server: Server = createHttpServer(service);
|
||
|
||
await new Promise<void>((resolve, reject) => {
|
||
server.once('error', reject);
|
||
server.listen(port, host, () => resolve());
|
||
});
|
||
|
||
const address = server.address() as AddressInfo | null;
|
||
const actualPort = address ? address.port : port;
|
||
|
||
return {
|
||
url: `http://${host}:${actualPort}`,
|
||
port: actualPort,
|
||
service,
|
||
close: () =>
|
||
new Promise<void>((resolve, reject) => {
|
||
server.close((error) => (error ? reject(error) : resolve()));
|
||
}),
|
||
};
|
||
} |