config
This commit is contained in:
@@ -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`);
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user