diff --git a/server/controllers/settingsController.js b/server/controllers/settingsController.js index 54643dfbb..561ea8266 100755 --- a/server/controllers/settingsController.js +++ b/server/controllers/settingsController.js @@ -32,6 +32,7 @@ class SettingsController { } try { + console.log(req.body); await this.db.updateAppSettings(req.body); const updatedSettings = { ...(await this.settingsService.reloadSettings()) }; delete updatedSettings.jwtSecret; diff --git a/server/db/models/AppSettings.js b/server/db/models/AppSettings.js index 6415b229d..3942305e8 100755 --- a/server/db/models/AppSettings.js +++ b/server/db/models/AppSettings.js @@ -2,44 +2,9 @@ import mongoose from "mongoose"; const AppSettingsSchema = mongoose.Schema( { - apiBaseUrl: { + language: { type: String, - required: true, - default: "http://localhost:5000/api/v1", - }, - logLevel: { - type: String, - default: "debug", - enum: ["debug", "none", "error", "warn"], - }, - clientHost: { - type: String, - required: true, - default: "http://localhost:5173", - }, - jwtSecret: { - type: String, - required: true, - default: "my_secret", - }, - dbType: { - type: String, - required: true, - default: "MongoDB", - }, - dbConnectionString: { - type: String, - required: true, - default: "mongodb://localhost:27017/uptime_db", - }, - redisUrl: { - type: String, - default: "redis://127.0.0.1:6379", - }, - jwtTTL: { - type: String, - required: true, - default: "2h", + default: "gb", }, pagespeedApiKey: { type: String, @@ -47,19 +12,15 @@ const AppSettingsSchema = mongoose.Schema( }, systemEmailHost: { type: String, - default: "smtp.gmail.com", }, systemEmailPort: { type: Number, - default: 465, }, systemEmailAddress: { type: String, - default: "", }, systemEmailPassword: { type: String, - default: "", }, singleton: { type: Boolean, @@ -73,4 +34,4 @@ const AppSettingsSchema = mongoose.Schema( } ); -export default mongoose.model("AppSettings", AppSettingsSchema); \ No newline at end of file +export default mongoose.model("AppSettings", AppSettingsSchema); diff --git a/server/db/mongo/MongoDB.js b/server/db/mongo/MongoDB.js index dd9826de5..b657caec4 100755 --- a/server/db/mongo/MongoDB.js +++ b/server/db/mongo/MongoDB.js @@ -76,7 +76,8 @@ import * as diagnosticModule from "./modules/diagnosticModule.js"; class MongoDB { static SERVICE_NAME = "MongoDB"; - constructor() { + constructor({ appSettings }) { + this.appSettings = appSettings; Object.assign(this, userModule); Object.assign(this, inviteModule); Object.assign(this, recoveryModule); @@ -95,8 +96,7 @@ class MongoDB { connect = async () => { try { const connectionString = - process.env.DB_CONNECTION_STRING || "mongodb://localhost:27017/uptime_db"; - console.log("Connecting to MongoDB with connection string:", connectionString); + this.appSettings.dbConnectionString || "mongodb://localhost:27017/uptime_db"; await mongoose.connect(connectionString); // If there are no AppSettings, create one await AppSettings.findOneAndUpdate( diff --git a/server/db/mongo/modules/settingsModule.js b/server/db/mongo/modules/settingsModule.js index 3b5f68e19..b817d2a32 100755 --- a/server/db/mongo/modules/settingsModule.js +++ b/server/db/mongo/modules/settingsModule.js @@ -14,10 +14,11 @@ const getAppSettings = async () => { const updateAppSettings = async (newSettings) => { try { + console.log(newSettings); const settings = await AppSettings.findOneAndUpdate( {}, { $set: newSettings }, - { new: true } + { new: true, upsert: true } ); return settings; } catch (error) { diff --git a/server/index.js b/server/index.js index 5fa10bcab..f7cab5dbe 100755 --- a/server/index.js +++ b/server/index.js @@ -139,19 +139,21 @@ const shutdown = async () => { // Need to wrap server setup in a function to handle async nature of JobQueue const startApp = async () => { const app = express(); - const allowedOrigin = process.env.CLIENT_HOST; // Create and Register Primary services const translationService = new TranslationService(logger); const stringService = new StringService(translationService); ServiceRegistry.register(StringService.SERVICE_NAME, stringService); - // Create DB - const db = new MongoDB(); - await db.connect(); - // Create services const settingsService = new SettingsService(AppSettings); - await settingsService.loadSettings(); + const appSettings = settingsService.loadSettings(); + + // Create DB + const db = new MongoDB({ appSettings }); + await db.connect(); + + // Set allowed origin + const allowedOrigin = appSettings.clientHost; const networkService = new NetworkService( axios, @@ -237,7 +239,7 @@ const startApp = async () => { ServiceRegistry.get(SettingsService.SERVICE_NAME), ServiceRegistry.get(JobQueue.SERVICE_NAME), ServiceRegistry.get(StringService.SERVICE_NAME), - ServiceRegistry.get(EmailService.SERVICE_NAME), + ServiceRegistry.get(EmailService.SERVICE_NAME) ); const settingsController = new SettingsController( diff --git a/server/service/settingsService.js b/server/service/settingsService.js index 6c7beb456..7c522de96 100755 --- a/server/service/settingsService.js +++ b/server/service/settingsService.js @@ -1,6 +1,4 @@ const SERVICE_NAME = "SettingsService"; -import dotenv from "dotenv"; -dotenv.config(); const envConfig = { logLevel: process.env.LOG_LEVEL, clientHost: process.env.CLIENT_HOST, @@ -20,8 +18,6 @@ const envConfig = { * SettingsService * * This service is responsible for loading and managing the application settings. - * It gives priority to environment variables and will only load settings - * from the database if they are not set in the environment. */ class SettingsService { static SERVICE_NAME = SERVICE_NAME; @@ -34,40 +30,17 @@ class SettingsService { this.settings = { ...envConfig }; } /** - * Load settings from the database and merge with environment settings. - * If there are any settings that weren't set by environment variables, use user settings from the database. - * @returns {Promise} The merged settings. - * @throws Will throw an error if settings are not found in the database or if settings have not been loaded. - */ async loadSettings() { - try { - const dbSettings = await this.appSettings.findOne(); - if (!this.settings) { - throw new Error("Settings not found"); - } - - // If there are any settings that weren't set by environment variables, use user settings from DB - for (const key in envConfig) { - if ( - typeof envConfig?.[key] === "undefined" && - typeof dbSettings?.[key] !== "undefined" - ) { - this.settings[key] = dbSettings[key]; - } - } - - await this.appSettings.updateOne({}, { $set: this.settings }, { upsert: true }); - return this.settings; - } catch (error) { - error.service === undefined ? (error.service = SERVICE_NAME) : null; - error.method === undefined ? (error.method = "loadSettings") : null; - throw error; - } + * Load settings from env settings + * @returns {Object>} The settings. + */ + loadSettings() { + return this.settings; } /** * Reload settings by calling loadSettings. * @returns {Promise} The reloaded settings. */ - async reloadSettings() { + reloadSettings() { return this.loadSettings(); } /**