From 4ac0e9a084e2a342ccb51daf6a40244bb238f2b7 Mon Sep 17 00:00:00 2001 From: "Navy.gif" Date: Mon, 15 May 2023 18:05:30 +0300 Subject: [PATCH] conditionally load message broker --- @types/Other.ts | 4 +++- @types/Server.ts | 6 +++--- options.json | 3 +++ package.json | 3 +-- src/controller/Controller.ts | 6 ++---- src/server/Server.ts | 25 +++++++++++++------------ src/server/components/FlagManager.ts | 10 +++++----- yarn.lock | 10 +++++----- 8 files changed, 35 insertions(+), 32 deletions(-) diff --git a/@types/Other.ts b/@types/Other.ts index b8b0677..d325e80 100644 --- a/@types/Other.ts +++ b/@types/Other.ts @@ -1,4 +1,5 @@ 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 FileType = 'video' | 'image' | 'audio' | 'text' | 'application' | 'message' | 'multipart'; @@ -34,7 +35,8 @@ type MongoDBOptions = { export type DatabaseOptions = { mariadb: MariaDBOptions, - mongodb: MongoDBOptions + mongodb: MongoDBOptions, + broker: BrokerOptions } export type SignupCode = { diff --git a/@types/Server.ts b/@types/Server.ts index 5d06540..1df46ae 100644 --- a/@types/Server.ts +++ b/@types/Server.ts @@ -47,7 +47,8 @@ export type ServerOptions = { http: HttpOptions, databases: { mariadb?: MariaOptions, - mongodb?: MongoOptions + mongodb?: MongoOptions, + broker?: BrokerOptions }, logger?: LoggerClientOptions, domain: string, @@ -59,8 +60,7 @@ export type ServerOptions = { discord: { scope?: string[], version?: number - }, - rabbitConfig: BrokerOptions + } } export type Permissions = { diff --git a/options.json b/options.json index 2f51455..d145f7e 100644 --- a/options.json +++ b/options.json @@ -61,6 +61,9 @@ "broadcastLevel": 3 }, "databases": { + "messageBroker": { + "load": false + }, "mariadb": { "load": false, "client": { diff --git a/package.json b/package.json index 00b2c8b..f3822ee 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "@navy.gif/commandparser": "^1.5.2", "@navy.gif/logger": "^2.3.3", "@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/express-fileupload": "^1.4.1", "@types/express-session": "^1.17.7", @@ -51,7 +51,6 @@ "nodemon": "^2.0.20" }, "scripts": { - "setup": "yarn install --registry https://registry.corgi.wtf", "start": "yarn build && node build/index.js", "build": "tsc", "lint": "eslint src/ --fix", diff --git a/src/controller/Controller.ts b/src/controller/Controller.ts index 0f67721..7497c55 100644 --- a/src/controller/Controller.ts +++ b/src/controller/Controller.ts @@ -18,7 +18,6 @@ import { ControllerOptions } from '../../@types/Controller.js'; import BaseCommand from './BaseCommand.js'; import { IPCMessage } from '../../@types/Other.js'; import { ServerOptions } from '../../@types/Server.js'; -import { BrokerOptions } from '@navy.gif/wrappers'; const CLIOptions: CommandOption[] = [ new CommandOption({ @@ -191,12 +190,11 @@ class Controller extends EventEmitter { serverOptions = {} as ServerOptions, logger = {}, discord = {}, - databases = {}, - rabbitConfig = {} as BrokerOptions + databases = {} } = this.#_options; return { serverOptions: { - ...serverOptions, logger, discord, databases, rabbitConfig + ...serverOptions, logger, discord, databases }, ...shardOptions, env: this.#_options.env, diff --git a/src/server/Server.ts b/src/server/Server.ts index 80d5283..d0974f9 100644 --- a/src/server/Server.ts +++ b/src/server/Server.ts @@ -57,7 +57,7 @@ class Server extends EventEmitter { #_mongodb: MongoDB; #_memoryStoreProvider: MongoMemory; #_userDatabase: UserDatabaseInterface; - #_messageBroker: MessageBroker; + #_messageBroker?: MessageBroker; #_rateLimiter: RateLimiter; #_authenticator: Authenticator; @@ -79,7 +79,7 @@ class Server extends EventEmitter { MONGO_MEMORY_PASS, MONGO_MEMORY_DB, MONGO_MEMORY_AUTH_DB, RABBIT_HOST, RABBIT_USER, RABBIT_PASS, RABBIT_VHOST, RABBIT_PORT } = 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 const encryption = Util.createEncryptionKey(CRYPTO_SECRET as string, CRYPTO_SALT as string); @@ -151,14 +151,15 @@ class Server extends EventEmitter { }); this.#_userDatabase = new UserDatabase(this, this.#_mongodb); - this.#_messageBroker = new MessageBroker(this, { - ...rabbitConfig, - host: RABBIT_HOST, - user: RABBIT_USER, - pass: RABBIT_PASS, - vhost: RABBIT_VHOST, - port: parseInt(RABBIT_PORT) - }); + if (databases.broker?.load) + this.#_messageBroker = new MessageBroker(this, { + ...databases.broker, + host: RABBIT_HOST, + user: RABBIT_USER, + pass: RABBIT_PASS, + vhost: RABBIT_VHOST, + port: parseInt(RABBIT_PORT) + }); // Provider needs to implement getKey(key) and setKey(key, value) // Distributed memory storage, using mongo in this case, but this could be redis or whatever @@ -239,7 +240,7 @@ class Server extends EventEmitter { await this.#_userDatabase.init(); - await this.#_messageBroker.init(); + await this.#_messageBroker?.init(); await this.#_flagManager.init(); @@ -299,7 +300,7 @@ class Server extends EventEmitter { await this.#_mongodb.close(); await this.#_mariadb.close(); await this.#_memoryStoreProvider.close(); - await this.#_messageBroker.close(); + await this.#_messageBroker?.close(); this.#logger.status('DB shutdowns complete.'); this.#logger.status('Server shutdown complete.'); diff --git a/src/server/components/FlagManager.ts b/src/server/components/FlagManager.ts index daef3ae..6014be0 100644 --- a/src/server/components/FlagManager.ts +++ b/src/server/components/FlagManager.ts @@ -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 Flag from "../structures/Flag.js"; import { FlagConsumer, FlagData, FlagEnv } from "../../../@types/Flags.js"; @@ -20,7 +20,7 @@ class FlagManager { #mongo: MongoDB; #collectionName = 'flags'; #flags: Collection; - #broker; + #broker?: MessageBroker; #logger: LoggerClient; #_tags: string[]; @@ -50,7 +50,7 @@ class FlagManager { } 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); await this.#mongo.insertOne(this.#collectionName, flag.json); 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; } @@ -127,7 +127,7 @@ class FlagManager { const json = flag.json as { _id?: string }; delete json._id; 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 }); } diff --git a/yarn.lock b/yarn.lock index aec1a57..52d3a14 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1773,15 +1773,15 @@ __metadata: languageName: node linkType: hard -"@navy.gif/wrappers@npm:^1.3.18": - version: 1.3.18 - resolution: "@navy.gif/wrappers@npm:1.3.18" +"@navy.gif/wrappers@npm:^1.3.20": + version: 1.3.20 + resolution: "@navy.gif/wrappers@npm:1.3.20" dependencies: amqp-connection-manager: ^4.1.12 amqplib: ^0.10.3 mongodb: ^5.2.0 mysql: ^2.18.1 - checksum: fadee6eb5b17da6c876aa2e34cedd8048e0993eb409835f106365d8eccd0645d79593b018da29e61567a0b70c93824439148de4bc4e9ae84a068fae96a4e641f + checksum: 5fa76d7e9fc84cf9b2790905a8703bedeb541f3b5f0cb43de002ea4e531eae05d1d31a5c0f674692cc2ad0ec96b8ee6994ef91bda50d4dc45dc2375304886e24 languageName: node linkType: hard @@ -7038,7 +7038,7 @@ __metadata: "@navy.gif/commandparser": ^1.5.2 "@navy.gif/logger": ^2.3.3 "@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/express-fileupload": ^1.4.1 "@types/express-session": ^1.17.7