mirror of
https://github.com/chrononeko/chronocat.git
synced 2024-11-16 12:51:39 +00:00
feat(shell): implement server
This commit is contained in:
parent
c7decb3eeb
commit
344f40882a
66
packages/shell/src/server.ts
Normal file
66
packages/shell/src/server.ts
Normal 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,
|
||||
}
|
||||
}
|
@ -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[]]
|
||||
|
Loading…
Reference in New Issue
Block a user