feat(shell): implement server

This commit is contained in:
Il Harper 2024-03-04 13:23:29 +08:00
parent c7decb3eeb
commit 344f40882a
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 79 additions and 1 deletions

View File

@ -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,
}
}

View File

@ -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<ChronocatLogCurrentConfig, ChronocatSatoriEventsConfig>,
) => Promise<Event[]>
}
export interface Methods {
'message.create': [[MessageCreatePayload], Message[]]