optional loading of mariadb

This commit is contained in:
Erik 2023-07-14 19:39:05 +03:00
parent 2f11ffa08a
commit 3c8688ee34
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68

View File

@ -9,6 +9,7 @@ import express, { Express, NextFunction } from 'express';
import helmet from 'helmet'; import helmet from 'helmet';
import cors from 'cors'; import cors from 'cors';
import MongoStore from 'connect-mongo'; import MongoStore from 'connect-mongo';
import { Collection } from '@discordjs/collection';
// Own // Own
import { LogFunction, LoggerClient } from '@navy.gif/logger'; import { LogFunction, LoggerClient } from '@navy.gif/logger';
@ -25,7 +26,6 @@ import { MongoMemory } from './database/index.js';
import { IPCMessage, SignupCode } from '../../@types/Other.js'; import { IPCMessage, SignupCode } from '../../@types/Other.js';
import { DoneCallback } from 'passport'; import { DoneCallback } from 'passport';
import FlagManager from './components/FlagManager.js'; import FlagManager from './components/FlagManager.js';
import { Collection } from '@discordjs/collection';
// const pkg = JSON.parse(readFileSync('../../package.json', { encoding: 'utf8' })); // const pkg = JSON.parse(readFileSync('../../package.json', { encoding: 'utf8' }));
@ -57,7 +57,7 @@ class Server extends EventEmitter
#server: http.Server | null; #server: http.Server | null;
#app: Express; #app: Express;
#mariadb: MariaDB; #mariadb?: MariaDB;
#mongodb: MongoDB; #mongodb: MongoDB;
#memoryStoreProvider: MongoMemory; #memoryStoreProvider: MongoMemory;
#userDatabase: UserDatabaseInterface; #userDatabase: UserDatabaseInterface;
@ -137,16 +137,19 @@ class Server extends EventEmitter
// TODO: Database definitions should probably be elsewhere through injection // TODO: Database definitions should probably be elsewhere through injection
// Mariadb isn't strictly necessary here for anything, it's just here pre-emptively // Mariadb isn't strictly necessary here for anything, it's just here pre-emptively
this.#mariadb = new MariaDB(this, { if (databases.mariadb?.load)
...databases.mariadb, {
credentials: { this.#mariadb = new MariaDB(this, {
host: MARIA_HOST, ...databases.mariadb,
user: MARIA_USER, credentials: {
port: parseInt(MARIA_PORT), host: MARIA_HOST,
password: MARIA_PASS, user: MARIA_USER,
database: MARIA_DB port: parseInt(MARIA_PORT),
} password: MARIA_PASS,
}); database: MARIA_DB
}
});
}
// Mongo is used for session and user storage // Mongo is used for session and user storage
this.#mongodb = new MongoDB(this, { this.#mongodb = new MongoDB(this, {
...databases.mongodb, ...databases.mongodb,
@ -243,8 +246,11 @@ class Server extends EventEmitter
const start = Date.now(); const start = Date.now();
this.#logger.status('Starting server'); this.#logger.status('Starting server');
this.#logger.info('Initialising MariaDB'); if (this.#mariadb)
this.#mariadb.init(); {
this.#logger.info('Initialising MariaDB');
this.#mariadb.init();
}
this.#logger.info('Initialising MongoDB'); this.#logger.info('Initialising MongoDB');
await this.#mongodb.init(); await this.#mongodb.init();
@ -360,7 +366,7 @@ class Server extends EventEmitter
this.#server.close(async () => this.#server.close(async () =>
{ {
await this.#mongodb.close(); await this.#mongodb.close();
await this.#mariadb.close(); await this.#mariadb?.close();
await this.#memoryStoreProvider.close(); await this.#memoryStoreProvider.close();
await this.#messageBroker?.close(); await this.#messageBroker?.close();
this.#logger.status('DB shutdowns complete.'); this.#logger.status('DB shutdowns complete.');