import type { IncomingMessage, ServerResponse } from 'node:http'; import type { ConnectionService } from '../../features/connection/index.js'; interface ConnectionRuntimeRouteDependencies { connection: Pick; withOperation(kind: string, operation: () => Promise): Promise; sendState(res: ServerResponse, extra: { singboxRunning: boolean }): Promise; } export function createConnectionRuntimeRoute(dependencies: ConnectionRuntimeRouteDependencies) { return { async handle(req: IncomingMessage, res: ServerResponse) { if (req.method === 'POST' && req.url === '/api/singbox/stop') { await dependencies.withOperation('stop', () => dependencies.connection.stop()); await dependencies.sendState(res, { singboxRunning: false }); return true; } if (req.method === 'POST' && req.url === '/api/singbox/restart') { await dependencies.withOperation('start', () => dependencies.connection.restart()); await dependencies.sendState(res, { singboxRunning: true }); return true; } return false; }, }; }