conditionally load message broker

This commit is contained in:
Erik 2023-05-15 18:05:30 +03:00
parent 8078e07b9a
commit 4ac0e9a084
Signed by: Navy.gif
GPG Key ID: 2532FBBB61C65A68
8 changed files with 35 additions and 32 deletions

View File

@ -1,4 +1,5 @@
import { LoggerClientOptions } from '@navy.gif/logger'; import { LoggerClientOptions } from '@navy.gif/logger';
import { BrokerOptions } from '@navy.gif/wrappers';
export type FileExtensions = 'mp4' | 'png' | 'mkv' | 'mov' | 'wmv' | 'm4v' | 'webm' | 'gif' | 'jpg' | 'exe' | 'avi' | 'webp' | 'jpeg' | 'htm' | 'zip' | 'txt'; export type FileExtensions = 'mp4' | 'png' | 'mkv' | 'mov' | 'wmv' | 'm4v' | 'webm' | 'gif' | 'jpg' | 'exe' | 'avi' | 'webp' | 'jpeg' | 'htm' | 'zip' | 'txt';
export type FileType = 'video' | 'image' | 'audio' | 'text' | 'application' | 'message' | 'multipart'; export type FileType = 'video' | 'image' | 'audio' | 'text' | 'application' | 'message' | 'multipart';
@ -34,7 +35,8 @@ type MongoDBOptions = {
export type DatabaseOptions = { export type DatabaseOptions = {
mariadb: MariaDBOptions, mariadb: MariaDBOptions,
mongodb: MongoDBOptions mongodb: MongoDBOptions,
broker: BrokerOptions
} }
export type SignupCode = { export type SignupCode = {

View File

@ -47,7 +47,8 @@ export type ServerOptions = {
http: HttpOptions, http: HttpOptions,
databases: { databases: {
mariadb?: MariaOptions, mariadb?: MariaOptions,
mongodb?: MongoOptions mongodb?: MongoOptions,
broker?: BrokerOptions
}, },
logger?: LoggerClientOptions, logger?: LoggerClientOptions,
domain: string, domain: string,
@ -59,8 +60,7 @@ export type ServerOptions = {
discord: { discord: {
scope?: string[], scope?: string[],
version?: number version?: number
}, }
rabbitConfig: BrokerOptions
} }
export type Permissions = { export type Permissions = {

View File

@ -61,6 +61,9 @@
"broadcastLevel": 3 "broadcastLevel": 3
}, },
"databases": { "databases": {
"messageBroker": {
"load": false
},
"mariadb": { "mariadb": {
"load": false, "load": false,
"client": { "client": {

View File

@ -15,7 +15,7 @@
"@navy.gif/commandparser": "^1.5.2", "@navy.gif/commandparser": "^1.5.2",
"@navy.gif/logger": "^2.3.3", "@navy.gif/logger": "^2.3.3",
"@navy.gif/passport-discord": "^0.2.2-b", "@navy.gif/passport-discord": "^0.2.2-b",
"@navy.gif/wrappers": "^1.3.18", "@navy.gif/wrappers": "^1.3.20",
"@types/cors": "^2.8.13", "@types/cors": "^2.8.13",
"@types/express-fileupload": "^1.4.1", "@types/express-fileupload": "^1.4.1",
"@types/express-session": "^1.17.7", "@types/express-session": "^1.17.7",
@ -51,7 +51,6 @@
"nodemon": "^2.0.20" "nodemon": "^2.0.20"
}, },
"scripts": { "scripts": {
"setup": "yarn install --registry https://registry.corgi.wtf",
"start": "yarn build && node build/index.js", "start": "yarn build && node build/index.js",
"build": "tsc", "build": "tsc",
"lint": "eslint src/ --fix", "lint": "eslint src/ --fix",

View File

@ -18,7 +18,6 @@ import { ControllerOptions } from '../../@types/Controller.js';
import BaseCommand from './BaseCommand.js'; import BaseCommand from './BaseCommand.js';
import { IPCMessage } from '../../@types/Other.js'; import { IPCMessage } from '../../@types/Other.js';
import { ServerOptions } from '../../@types/Server.js'; import { ServerOptions } from '../../@types/Server.js';
import { BrokerOptions } from '@navy.gif/wrappers';
const CLIOptions: CommandOption[] = [ const CLIOptions: CommandOption[] = [
new CommandOption({ new CommandOption({
@ -191,12 +190,11 @@ class Controller extends EventEmitter {
serverOptions = {} as ServerOptions, serverOptions = {} as ServerOptions,
logger = {}, logger = {},
discord = {}, discord = {},
databases = {}, databases = {}
rabbitConfig = {} as BrokerOptions
} = this.#_options; } = this.#_options;
return { return {
serverOptions: { serverOptions: {
...serverOptions, logger, discord, databases, rabbitConfig ...serverOptions, logger, discord, databases
}, },
...shardOptions, ...shardOptions,
env: this.#_options.env, env: this.#_options.env,

View File

@ -57,7 +57,7 @@ class Server extends EventEmitter {
#_mongodb: MongoDB; #_mongodb: MongoDB;
#_memoryStoreProvider: MongoMemory; #_memoryStoreProvider: MongoMemory;
#_userDatabase: UserDatabaseInterface; #_userDatabase: UserDatabaseInterface;
#_messageBroker: MessageBroker; #_messageBroker?: MessageBroker;
#_rateLimiter: RateLimiter; #_rateLimiter: RateLimiter;
#_authenticator: Authenticator; #_authenticator: Authenticator;
@ -79,7 +79,7 @@ class Server extends EventEmitter {
MONGO_MEMORY_PASS, MONGO_MEMORY_DB, MONGO_MEMORY_AUTH_DB, MONGO_MEMORY_PASS, MONGO_MEMORY_DB, MONGO_MEMORY_AUTH_DB,
RABBIT_HOST, RABBIT_USER, RABBIT_PASS, RABBIT_VHOST, RABBIT_PORT RABBIT_HOST, RABBIT_USER, RABBIT_PASS, RABBIT_VHOST, RABBIT_PORT
} = process.env as { [key: string]: string }; } = process.env as { [key: string]: string };
const { http: httpOpts, databases, name, rabbitConfig } = options; const { http: httpOpts, databases, name } = options;
// This key never leaves memory and is exclusively used on the server, the salt can stay static // This key never leaves memory and is exclusively used on the server, the salt can stay static
const encryption = Util.createEncryptionKey(CRYPTO_SECRET as string, CRYPTO_SALT as string); const encryption = Util.createEncryptionKey(CRYPTO_SECRET as string, CRYPTO_SALT as string);
@ -151,8 +151,9 @@ class Server extends EventEmitter {
}); });
this.#_userDatabase = new UserDatabase(this, this.#_mongodb); this.#_userDatabase = new UserDatabase(this, this.#_mongodb);
if (databases.broker?.load)
this.#_messageBroker = new MessageBroker(this, { this.#_messageBroker = new MessageBroker(this, {
...rabbitConfig, ...databases.broker,
host: RABBIT_HOST, host: RABBIT_HOST,
user: RABBIT_USER, user: RABBIT_USER,
pass: RABBIT_PASS, pass: RABBIT_PASS,
@ -239,7 +240,7 @@ class Server extends EventEmitter {
await this.#_userDatabase.init(); await this.#_userDatabase.init();
await this.#_messageBroker.init(); await this.#_messageBroker?.init();
await this.#_flagManager.init(); await this.#_flagManager.init();
@ -299,7 +300,7 @@ class Server extends EventEmitter {
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.');
this.#logger.status('Server shutdown complete.'); this.#logger.status('Server shutdown complete.');

View File

@ -1,4 +1,4 @@
import { MongoDB, ObjectId } from "@navy.gif/wrappers"; import { MessageBroker, MongoDB, ObjectId } from "@navy.gif/wrappers";
import Server from "../Server.js"; import Server from "../Server.js";
import Flag from "../structures/Flag.js"; import Flag from "../structures/Flag.js";
import { FlagConsumer, FlagData, FlagEnv } from "../../../@types/Flags.js"; import { FlagConsumer, FlagData, FlagEnv } from "../../../@types/Flags.js";
@ -20,7 +20,7 @@ class FlagManager {
#mongo: MongoDB; #mongo: MongoDB;
#collectionName = 'flags'; #collectionName = 'flags';
#flags: Collection<string, Flag>; #flags: Collection<string, Flag>;
#broker; #broker?: MessageBroker;
#logger: LoggerClient; #logger: LoggerClient;
#_tags: string[]; #_tags: string[];
@ -50,7 +50,7 @@ class FlagManager {
} }
this.#_tags = [ ...tags ]; this.#_tags = [ ...tags ];
this.#broker.subscribe('flagUpdates', this.#flagUpdate.bind(this)); this.#broker?.subscribe('flagUpdates', this.#flagUpdate.bind(this));
} }
@ -117,7 +117,7 @@ class FlagManager {
const flag = new Flag(this, data); const flag = new Flag(this, data);
await this.#mongo.insertOne(this.#collectionName, flag.json); await this.#mongo.insertOne(this.#collectionName, flag.json);
this.#flags.set(flag.id, flag); this.#flags.set(flag.id, flag);
this.#broker.publish('flagUpdates', { origin: this.#server.shardId, flag: flag.json }); this.#broker?.publish('flagUpdates', { origin: this.#server.shardId, flag: flag.json });
return flag; return flag;
} }
@ -127,7 +127,7 @@ class FlagManager {
const json = flag.json as { _id?: string }; const json = flag.json as { _id?: string };
delete json._id; delete json._id;
await this.#mongo.updateOne(this.#collectionName, { _id: flag.id }, json, true); await this.#mongo.updateOne(this.#collectionName, { _id: flag.id }, json, true);
this.#broker.publish('flagUpdates', { origin: this.#server.shardId, flag: flag.json }); this.#broker?.publish('flagUpdates', { origin: this.#server.shardId, flag: flag.json });
} }

View File

@ -1773,15 +1773,15 @@ __metadata:
languageName: node languageName: node
linkType: hard linkType: hard
"@navy.gif/wrappers@npm:^1.3.18": "@navy.gif/wrappers@npm:^1.3.20":
version: 1.3.18 version: 1.3.20
resolution: "@navy.gif/wrappers@npm:1.3.18" resolution: "@navy.gif/wrappers@npm:1.3.20"
dependencies: dependencies:
amqp-connection-manager: ^4.1.12 amqp-connection-manager: ^4.1.12
amqplib: ^0.10.3 amqplib: ^0.10.3
mongodb: ^5.2.0 mongodb: ^5.2.0
mysql: ^2.18.1 mysql: ^2.18.1
checksum: fadee6eb5b17da6c876aa2e34cedd8048e0993eb409835f106365d8eccd0645d79593b018da29e61567a0b70c93824439148de4bc4e9ae84a068fae96a4e641f checksum: 5fa76d7e9fc84cf9b2790905a8703bedeb541f3b5f0cb43de002ea4e531eae05d1d31a5c0f674692cc2ad0ec96b8ee6994ef91bda50d4dc45dc2375304886e24
languageName: node languageName: node
linkType: hard linkType: hard
@ -7038,7 +7038,7 @@ __metadata:
"@navy.gif/commandparser": ^1.5.2 "@navy.gif/commandparser": ^1.5.2
"@navy.gif/logger": ^2.3.3 "@navy.gif/logger": ^2.3.3
"@navy.gif/passport-discord": ^0.2.2-b "@navy.gif/passport-discord": ^0.2.2-b
"@navy.gif/wrappers": ^1.3.18 "@navy.gif/wrappers": ^1.3.20
"@types/cors": ^2.8.13 "@types/cors": ^2.8.13
"@types/express-fileupload": ^1.4.1 "@types/express-fileupload": ^1.4.1
"@types/express-session": ^1.17.7 "@types/express-session": ^1.17.7