This commit is contained in:
2025-03-14 01:41:53 +08:00
parent efef48a1b0
commit d947043a16
12 changed files with 284 additions and 65 deletions

View File

@@ -1,3 +1,31 @@
import { SimpleRouter } from '@kevisual/router/simple';
import { router } from '@/app.ts';
import http from 'http';
import { useContextKey } from '@kevisual/use-config/context';
export const router = useContextKey('router', () => new SimpleRouter());
import { checkAuth, error } from './middleware/auth.ts';
export { router, checkAuth, error };
/**
* 事件客户端
*/
const eventClientsInit = () => {
const clients = new Map<string, { client?: http.ServerResponse; [key: string]: any }>();
return clients;
};
export const clients = useContextKey('event-clients', () => eventClientsInit());
/**
* 获取 task-id
* @param req
* @returns
*/
export const getTaskId = (req: http.IncomingMessage) => {
return req.headers['task-id'] as string;
};
/**
* 写入事件
* @param req
* @param data
*/
export const writeEvents = (req: http.IncomingMessage, data: any) => {
const taskId = getTaskId(req);
taskId && clients.get(taskId)?.client?.write?.(`${JSON.stringify(data)}\n`);
};