From 97419b436c75aab273608423a534d67824a67115 Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Thu, 26 Sep 2024 15:45:54 +0800 Subject: [PATCH 1/2] Add reload settings method, reload settings when DB updated --- Server/controllers/settingsController.js | 1 + Server/service/settingsService.js | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/Server/controllers/settingsController.js b/Server/controllers/settingsController.js index 87e4edfc1..c44363bfa 100644 --- a/Server/controllers/settingsController.js +++ b/Server/controllers/settingsController.js @@ -31,6 +31,7 @@ const updateAppSettings = async (req, res, next) => { try { const settings = await req.db.updateAppSettings(req.body); + await req.settingsService.reloadSettings(); return res.status(200).json({ success: true, msg: successMessages.UPDATE_APP_SETTINGS, diff --git a/Server/service/settingsService.js b/Server/service/settingsService.js index e8abe747f..a5e7b2734 100644 --- a/Server/service/settingsService.js +++ b/Server/service/settingsService.js @@ -22,6 +22,19 @@ class SettingsService { } } + async reloadSettings() { + try { + this.settings = await AppSettings.findOne(); + if (!this.settings) { + throw new Error("Settings not found"); + } + } catch (error) { + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined ? (error.method = "reloadSettings") : null; + throw error; + } + } + getSettings() { if (!this.settings) { throw new Error("Settings have not been loaded"); From c3edbb779a690f09ab16c647a5998090cf12f8bd Mon Sep 17 00:00:00 2001 From: Alex Holliday Date: Fri, 27 Sep 2024 13:02:01 +0800 Subject: [PATCH 2/2] Modify settingsService to load from env by default --- Server/service/settingsService.js | 52 ++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/Server/service/settingsService.js b/Server/service/settingsService.js index a5e7b2734..ca2509fe0 100644 --- a/Server/service/settingsService.js +++ b/Server/service/settingsService.js @@ -1,24 +1,52 @@ const AppSettings = require("../models/AppSettings"); const SERVICE_NAME = "SettingsService"; +CLIENT_HOST = process.env.CLIENT_HOST; +JWT_SECRET = process.env.JWT_SECRET; +DB_TYPE = process.env.DB_TYPE; +DB_CONNECTION_STRING = process.env.DB_CONNECTION_STRING; +REDIS_HOST = process.env.REDIS_HOST; +REDIS_PORT = process.env.REDIS_PORT; +TOKEN_TTL = process.env.TOKEN_TTL; +PAGESPEED_API_KEY = process.env.PAGESPEED_API_KEY; +SYSTEM_EMAIL_HOST = process.env.SYSTEM_EMAIL_HOST; +SYSTEM_EMAIL_PORT = process.env.SYSTEM_EMAIL_PORT; +SYSTEM_EMAIL_ADDRESS = process.env.SYSTEM_EMAIL_ADDRESS; +SYSTEM_EMAIL_PASSWORD = process.env.SYSTEM_EMAIL_PASSWORD; class SettingsService { constructor() { - this.settings = null; + this.settings = { + clientHost: CLIENT_HOST, + jwtSecret: JWT_SECRET, + dbType: DB_TYPE, + dbConnectionString: DB_CONNECTION_STRING, + redisHost: REDIS_HOST, + redisPort: REDIS_PORT, + tokenTTL: TOKEN_TTL, + pagespeedApiKey: PAGESPEED_API_KEY, + systemEmailHost: SYSTEM_EMAIL_HOST, + systemEmailPort: SYSTEM_EMAIL_PORT, + systemEmailAddress: SYSTEM_EMAIL_ADDRESS, + systemEmailPassword: SYSTEM_EMAIL_PASSWORD, + }; } async loadSettings() { - if (!this.settings) { - try { - this.settings = await AppSettings.findOne(); - if (!this.settings) { - throw new Error("Settings not found"); - } - return this.settings; - } catch (error) { - error.service === undefined ? (error.service = SERVICE_NAME) : null; - error.method === undefined ? (error.method = "loadSettings") : null; - throw error; + try { + const dbSettings = await AppSettings.findOne(); + if (!this.settings) { + throw new Error("Settings not found"); } + for (const key in this.settings) { + if (this.settings[key] === undefined && dbSettings[key] !== undefined) { + this.settings[key] = dbSettings[key]; + } + } + return this.settings; + } catch (error) { + error.service === undefined ? (error.service = SERVICE_NAME) : null; + error.method === undefined ? (error.method = "loadSettings") : null; + throw error; } }