diff --git a/src/controllers/custom/getConfigDataController.ts b/src/controllers/custom/getConfigDataController.ts index 8d9463845..9a8684ca9 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 1b13cc608..b7521a1a6 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 e5199a3c6..8290047bb 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 df711a5f5..d19f125a2 100644 --- a/static/webui/index.html +++ b/static/webui/index.html @@ -204,81 +204,63 @@
-
-
Change Settings
-
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
-
- - -
- -
-
+
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+ +