From 30b75228227c8fbe64eff45ceaa579e858f1542d Mon Sep 17 00:00:00 2001 From: deptyped Date: Tue, 23 Jul 2024 03:53:55 +0300 Subject: [PATCH] Decouple bot and server --- src/main.ts | 13 +++++++------ src/server/index.ts | 46 ++++++++++++++++++++++----------------------- 2 files changed, 30 insertions(+), 29 deletions(-) diff --git a/src/main.ts b/src/main.ts index e1a4116c..6faac0ae 100644 --- a/src/main.ts +++ b/src/main.ts @@ -46,11 +46,15 @@ async function startWebhook(config: WebhookConfig) { config, logger, }) - const server = createServer(bot, { + const server = createServer({ + bot, config, logger, }) - const serverManager = createServerManager(server) + const serverManager = createServerManager(server, { + host: config.serverHost, + port: config.serverPort, + }) // graceful shutdown onShutdown(async () => { @@ -62,10 +66,7 @@ async function startWebhook(config: WebhookConfig) { await bot.init() // start server - const info = await serverManager.start( - config.serverHost, - config.serverPort, - ) + const info = await serverManager.start() logger.info({ msg: 'Server started', url: info.url, diff --git a/src/server/index.ts b/src/server/index.ts index 81d70730..715c04ae 100644 --- a/src/server/index.ts +++ b/src/server/index.ts @@ -12,27 +12,23 @@ import type { Logger } from '#root/logger.js' import type { Config } from '#root/config.js' interface Dependencies { + bot: Bot config: Config logger: Logger } -export function createServer(bot: Bot, dependencies: Dependencies) { +export function createServer(dependencies: Dependencies) { const { + bot, config, logger, } = dependencies - if (!config.isWebhookMode) { - throw new Error('Bot is not in webhook mode') - } - const server = new Hono() server.use(requestId()) server.use(setLogger(logger)) - - if (config.isDebug) - server.use(requestLogger()) + config.isDebug && server.use(requestLogger()) server.onError(async (error, c) => { if (error instanceof HTTPException) { @@ -60,28 +56,30 @@ export function createServer(bot: Bot, dependencies: Dependencies) { server.get('/', c => c.json({ status: true })) - server.post( - '/webhook', - webhookCallback(bot, 'hono', { - secretToken: config.botWebhookSecret, - }), - ) + if (config.isWebhookMode) { + server.post( + '/webhook', + webhookCallback(bot, 'hono', { + secretToken: config.botWebhookSecret, + }), + ) + } return server } export type Server = Awaited> -export function createServerManager(server: Server) { +export function createServerManager(server: Server, options: { host: string, port: number }) { let handle: undefined | ReturnType return { - start: (host: string, port: number) => - new Promise<{ url: string }>((resolve) => { + start() { + return new Promise<{ url: string } >((resolve) => { handle = serve( { fetch: server.fetch, - hostname: host, - port, + hostname: options.host, + port: options.port, }, info => resolve({ url: info.family === 'IPv6' @@ -89,13 +87,15 @@ export function createServerManager(server: Server) { : `http://${info.address}:${info.port}`, }), ) - }), - stop: () => - new Promise((resolve) => { + }) + }, + stop() { + return new Promise((resolve) => { if (handle) handle.close(() => resolve()) else resolve() - }), + }) + }, } }