From 0a7ec11510ad0b7b3a518efeef2d67eaf2f77aaa Mon Sep 17 00:00:00 2001 From: Chris Troutner Date: Sun, 9 Jun 2024 08:28:40 -0700 Subject: [PATCH] feat(Mongo): Config variable can disable MongoDB usage and dependency in Docker --- bin/server.js | 30 +++++++++++++++++------------- config/env/common.js | 3 +++ src/controllers/rest-api/index.js | 18 +++++++++++------- 3 files changed, 31 insertions(+), 20 deletions(-) diff --git a/bin/server.js b/bin/server.js index 535c238..defd5e9 100644 --- a/bin/server.js +++ b/bin/server.js @@ -45,16 +45,18 @@ class Server { const app = new Koa() app.keys = [this.config.session] - // Connect to the Mongo Database. - this.mongoose.Promise = global.Promise - this.mongoose.set('useCreateIndex', true) // Stop deprecation warning. - console.log( - `Connecting to MongoDB with this connection string: ${this.config.database}` - ) - await this.mongoose.connect(this.config.database, { - useUnifiedTopology: true, - useNewUrlParser: true - }) + if (!this.config.noMongo) { + // Connect to the Mongo Database. + this.mongoose.Promise = global.Promise + this.mongoose.set('useCreateIndex', true) // Stop deprecation warning. + console.log( + `Connecting to MongoDB with this connection string: ${this.config.database}` + ) + await this.mongoose.connect(this.config.database, { + useUnifiedTopology: true, + useNewUrlParser: true + }) + } console.log(`Starting environment: ${this.config.env}`) console.log(`Debug level: ${this.config.debugLevel}`) @@ -102,9 +104,11 @@ class Server { this.server = await app.listen(this.config.port) console.log(`Server started on ${this.config.port}`) - // Create the system admin user. - const success = await this.adminLib.createSystemUser() - if (success) console.log('System admin user created.') + if (!this.config.noMongo) { + // Create the system admin user. + const success = await this.adminLib.createSystemUser() + if (success) console.log('System admin user created.') + } // Attach the other IPFS controllers. // Skip if this is a test environment. diff --git a/config/env/common.js b/config/env/common.js index 2fac04c..26dde16 100644 --- a/config/env/common.js +++ b/config/env/common.js @@ -43,6 +43,9 @@ export default { ? process.env.EMAILPASS : 'emailpassword', + // Enable or Disable the usage of Mongo DB. + noMongo: process.env.NO_MONGO ? true : false, + // BEGIN WALLET CONFIGURATION // BCH Mnemonic for generating encryption keys and payment address diff --git a/src/controllers/rest-api/index.js b/src/controllers/rest-api/index.js index 63e18a6..64b2f35 100644 --- a/src/controllers/rest-api/index.js +++ b/src/controllers/rest-api/index.js @@ -12,6 +12,7 @@ import UserRouter from './users/index.js' import ContactRESTController from './contact/index.js' import LogsRESTController from './logs/index.js' import IpfsRESTController from './ipfs/index.js' +import config from '../../../config/index.js' class RESTControllers { constructor (localConfig = {}) { @@ -29,7 +30,8 @@ class RESTControllers { ) } - // console.log('Controllers localConfig: ', localConfig) + // Encapsulate dependencies + this.config = config } attachRESTControllers (app) { @@ -38,13 +40,15 @@ class RESTControllers { useCases: this.useCases } - // Attach the REST API Controllers associated with the /auth route - const authRESTController = new AuthRESTController(dependencies) - authRESTController.attach(app) + if (!this.config.noMongo) { + // Attach the REST API Controllers associated with the /auth route + const authRESTController = new AuthRESTController(dependencies) + authRESTController.attach(app) - // Attach the REST API Controllers associated with the /user route - const userRouter = new UserRouter(dependencies) - userRouter.attach(app) + // Attach the REST API Controllers associated with the /user route + const userRouter = new UserRouter(dependencies) + userRouter.attach(app) + } // Attach the REST API Controllers associated with the /contact route const contactRESTController = new ContactRESTController(dependencies)