Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#49): api and websocket servers optional #50

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/config/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const apiConfig: APIConfigType = {

enabled: false,
port: process.env['API_PORT'] ? parseInt(process.env['API_PORT']) : 4000,
}
3 changes: 2 additions & 1 deletion src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ export * from './general'
export * from './database'
export * from './logs'
export * from './stats'
export * from './api'
export * from './api'
export * from './websocket'
4 changes: 4 additions & 0 deletions src/config/websocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const websocketConfig: WebsocketConfigType = {

enabled: false,
}
16 changes: 10 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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()
28 changes: 18 additions & 10 deletions src/services/Logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/utils/types/configs.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,5 +87,11 @@ type StatsConfigType = {

type APIConfigType = {

enabled: boolean
port: number
}

type WebsocketConfigType = {

enabled: boolean
}