From 344f40882a6fc50942aed1662e8c0156c94adfa8 Mon Sep 17 00:00:00 2001 From: Il Harper Date: Mon, 4 Mar 2024 13:23:29 +0800 Subject: [PATCH] feat(shell): implement server --- packages/shell/src/server.ts | 66 ++++++++++++++++++++++++++++++++++++ packages/shell/src/types.ts | 14 +++++++- 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 packages/shell/src/server.ts diff --git a/packages/shell/src/server.ts b/packages/shell/src/server.ts new file mode 100644 index 0000000..3899cc0 --- /dev/null +++ b/packages/shell/src/server.ts @@ -0,0 +1,66 @@ +import { initSatoriServer } from './satori/server' +import { initSatoriWebHook } from './satori/webhook' +import { getAuthData } from './services/authData' +import { getConfig } from './services/config' +import type { ChronocatLogCurrentConfig } from './services/config/configEntity' +import { l } from './services/logger' +import type { DispatchMessage } from './types' + +export const initServers = async () => { + l.debug('初始化服务') + + const config = await getConfig() + const authData = await getAuthData() + + const log: ChronocatLogCurrentConfig = config.log! + // 预处理 self_url + if (!log.self_url || log.self_url === 'https://chronocat.vercel.app') + log.self_url = `http://127.0.0.1:5500` + if (log.self_url.endsWith('/')) + log.self_url = log.self_url.slice(0, log.self_url.length - 1) + + const dispatchers: ((message: DispatchMessage) => void)[] = [ + // Logger + (message) => + message + .toSatori(authData.uin, log) + .then((es) => es.forEach((e) => l.parse(e))), + ] + + // 使用独立循环可避免已启动的服务继续运行 + for (const server of config.servers!) + if (server.token === 'DEFINE_CHRONO_TOKEN') + l.error('请先修改服务密码(token)', { code: 2135, throw: true }) + + for (const server of config.servers!) { + if (!server.enable) { + l.debug('跳过不启用的服务') + continue + } + + switch (server.type) { + case 'satori': { + const { dispatcher } = await initSatoriServer(server) + dispatchers.push(dispatcher) + l.info( + `satori: 启动 Satori 服务于 http://${server.listen}:${server.port}`, + ) + break + } + + case 'satori_webhook': { + const { dispatcher } = await initSatoriWebHook(server) + dispatchers.push(dispatcher) + l.info(`satori: 启动 Satori WebHook 服务,目标 ${server.self_url}`) + break + } + } + } + + const dispatchMessage = (message: DispatchMessage) => + dispatchers.forEach((x) => x(message)) + + return { + dispatchMessage, + } +} diff --git a/packages/shell/src/types.ts b/packages/shell/src/types.ts index fcb4bd8..98ae721 100644 --- a/packages/shell/src/types.ts +++ b/packages/shell/src/types.ts @@ -1,4 +1,16 @@ -import type { Message, MessageCreatePayload } from './satori/types' +import type { O } from 'ts-toolbelt' +import type { Event, Message, MessageCreatePayload } from './satori/types' +import type { + ChronocatLogCurrentConfig, + ChronocatSatoriEventsConfig, +} from './services/config/configEntity' + +export interface DispatchMessage { + toSatori: ( + selfId: string, + config: O.Intersect, + ) => Promise +} export interface Methods { 'message.create': [[MessageCreatePayload], Message[]]