From e5451d52270d7d11bc51ec8f72b9f63c4fa622ff Mon Sep 17 00:00:00 2001 From: Sainan Date: Sat, 15 Jun 2024 02:52:45 +0200 Subject: [PATCH] fix: config change on WebUI causing hot reload in dev mode (#297) --- .../custom/getConfigDataController.ts | 4 +- .../custom/updateConfigDataController.ts | 15 +- src/services/configService.ts | 25 +++- static/webui/index.html | 132 ++++++++---------- 4 files changed, 86 insertions(+), 90 deletions(-) diff --git a/src/controllers/custom/getConfigDataController.ts b/src/controllers/custom/getConfigDataController.ts index 8d946384..9a8684ca 100644 --- a/src/controllers/custom/getConfigDataController.ts +++ b/src/controllers/custom/getConfigDataController.ts @@ -1,8 +1,8 @@ import { RequestHandler } from "express"; -import configFile from "@/config.json"; +import { config } from "@/src/services/configService"; const getConfigDataController: RequestHandler = (_req, res) => { - res.json(configFile); + res.json(config); }; export { getConfigDataController }; diff --git a/src/controllers/custom/updateConfigDataController.ts b/src/controllers/custom/updateConfigDataController.ts index 1b13cc60..b7521a1a 100644 --- a/src/controllers/custom/updateConfigDataController.ts +++ b/src/controllers/custom/updateConfigDataController.ts @@ -1,16 +1,9 @@ import { RequestHandler } from "express"; -import path from "path"; -import fs from "fs"; -const rootDir = path.join(__dirname, "../../.."); +import { updateConfig } from "@/src/services/configService"; -const updateConfigDataController: RequestHandler = req => { - const updateSettingsData = req.body; - - fs.writeFile(path.join(rootDir, "config.json"), updateSettingsData, function (err: any) { - if (err) { - return console.log(err); - } - }); +const updateConfigDataController: RequestHandler = async (req, res) => { + await updateConfig(req.body.toString()); + res.end(); }; export { updateConfigDataController }; diff --git a/src/services/configService.ts b/src/services/configService.ts index e5199a3c..8290047b 100644 --- a/src/services/configService.ts +++ b/src/services/configService.ts @@ -1,4 +1,22 @@ -import rawConfig from "@/config.json"; +import path from "path"; +import fs from "fs"; +import fsPromises from "fs/promises"; +import { logger } from "@/src/utils/logger"; + +const rootDir = path.join(__dirname, "../.."); +const repoDir = path.basename(rootDir) == "build" ? path.join(rootDir, "..") : rootDir; +const configPath = path.join(repoDir, "config.json"); +export const config: IConfig = JSON.parse(fs.readFileSync(configPath, "utf-8")); + +let amnesia = false; +fs.watchFile(configPath, () => { + if (amnesia) { + amnesia = false; + } else { + logger.info("Detected a change to config.json, reloading its contents."); + Object.assign(config, JSON.parse(fs.readFileSync(configPath, "utf-8"))); + } +}); interface IConfig { mongodbUrl: string; @@ -26,4 +44,7 @@ interface ILoggerConfig { level: string; // "fatal" | "error" | "warn" | "info" | "http" | "debug" | "trace"; } -export const config: IConfig = rawConfig; +export const updateConfig = async (data: string) => { + amnesia = true; + return await fsPromises.writeFile(configPath, data); +}; diff --git a/static/webui/index.html b/static/webui/index.html index df711a5f..d19f125a 100644 --- a/static/webui/index.html +++ b/static/webui/index.html @@ -204,81 +204,63 @@
-
-
Change Settings
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- -
-
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +