From 0138d52093d9ed3bcfcf2fdc23e3170cdbf11a3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartholom=C3=A9=20Gili?= <66025667+barthofu@users.noreply.github.com> Date: Sun, 4 Sep 2022 18:53:51 +0000 Subject: [PATCH] feat(#49): api and websocket servers optional --- src/config/api.ts | 1 + src/config/index.ts | 3 ++- src/config/websocket.ts | 4 ++++ src/main.ts | 16 ++++++++++------ src/services/Logger.ts | 28 ++++++++++++++++++---------- src/utils/types/configs.d.ts | 6 ++++++ 6 files changed, 41 insertions(+), 17 deletions(-) create mode 100644 src/config/websocket.ts diff --git a/src/config/api.ts b/src/config/api.ts index bd7cd3a9..c9bda5d1 100644 --- a/src/config/api.ts +++ b/src/config/api.ts @@ -1,4 +1,5 @@ export const apiConfig: APIConfigType = { + enabled: false, port: process.env['API_PORT'] ? parseInt(process.env['API_PORT']) : 4000, } \ No newline at end of file diff --git a/src/config/index.ts b/src/config/index.ts index 6b9099e9..30597a1b 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -2,4 +2,5 @@ export * from './general' export * from './database' export * from './logs' export * from './stats' -export * from './api' \ No newline at end of file +export * from './api' +export * from './websocket' \ No newline at end of file diff --git a/src/config/websocket.ts b/src/config/websocket.ts new file mode 100644 index 00000000..73037bea --- /dev/null +++ b/src/config/websocket.ts @@ -0,0 +1,4 @@ +export const websocketConfig: WebsocketConfigType = { + + enabled: false, +} \ No newline at end of file diff --git a/src/main.ts b/src/main.ts index 7e117266..fd4275b8 100644 --- a/src/main.ts +++ b/src/main.ts @@ -10,7 +10,7 @@ import { initDataTable, waitForDependency } from '@utils/functions' import { Server } from '@api/server' import { clientConfig } from './client' -import { generalConfig } from '@config' +import { apiConfig, generalConfig, websocketConfig } from '@config' import { NoBotTokenError } from '@errors' async function run() { @@ -43,18 +43,22 @@ async function run() { await client.login(process.env.BOT_TOKEN) // start the api server - const server = await waitForDependency(Server) - await server.start() + if (apiConfig.enabled) { + const server = await waitForDependency(Server) + await server.start() + } // connect to the dashboard websocket - const webSocket = await waitForDependency(WebSocket) - await webSocket.init(client.user?.id || null) + if (websocketConfig.enabled) { + const webSocket = await waitForDependency(WebSocket) + await webSocket.init(client.user?.id || null) + } // upload images to imgur if configured if (process.env.IMGUR_CLIENT_ID && generalConfig.automaticUploadImagesToImgur) { const imagesUpload = await waitForDependency(ImagesUpload) await imagesUpload.syncWithDatabase() - } + } } run() \ No newline at end of file diff --git a/src/services/Logger.ts b/src/services/Logger.ts index 67e44eeb..6de0c565 100644 --- a/src/services/Logger.ts +++ b/src/services/Logger.ts @@ -327,22 +327,30 @@ export class Logger { this.console('info', chalk.green(`${symbol} ${numberAlign(scheduledJobs)} ${chalk.bold('scheduled jobs')} loaded`), true) // connected - this.console('info', chalk.gray(boxen( - ` API Server listening on port ${chalk.bold(apiConfig.port)} `, - { - padding: 0, - margin: 1, - borderStyle: 'round', - dimBorder: true - } - )), true) + if (apiConfig.enabled) { + + this.console('info', chalk.gray(boxen( + ` API Server listening on port ${chalk.bold(apiConfig.port)} `, + { + padding: 0, + margin: { + top: 1, + bottom: 0, + left: 1, + right: 1 + }, + borderStyle: 'round', + dimBorder: true + } + )), true) + } this.console('info', chalk.hex('7289DA')(boxen( ` ${this.client.user ? `${chalk.bold(this.client.user.tag)}` : 'Bot'} is ${chalk.green('connected')}! `, { padding: 0, margin: { - top: 0, + top: 1, bottom: 1, left: 1 * 3, right: 1 * 3 diff --git a/src/utils/types/configs.d.ts b/src/utils/types/configs.d.ts index 08f36a0d..ff86066e 100644 --- a/src/utils/types/configs.d.ts +++ b/src/utils/types/configs.d.ts @@ -87,5 +87,11 @@ type StatsConfigType = { type APIConfigType = { + enabled: boolean port: number +} + +type WebsocketConfigType = { + + enabled: boolean } \ No newline at end of file