28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
import type { IncomingMessage, ServerResponse } from 'node:http';
|
|
|
|
import type { ConnectionService } from '../../features/connection/index.js';
|
|
|
|
interface ConnectionRuntimeRouteDependencies {
|
|
connection: Pick<ConnectionService, 'stop' | 'restart'>;
|
|
withOperation<T>(kind: string, operation: () => Promise<T>): Promise<T>;
|
|
sendState(res: ServerResponse, extra: { singboxRunning: boolean }): Promise<void>;
|
|
}
|
|
|
|
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;
|
|
},
|
|
};
|
|
}
|