fix: config change on WebUI causing hot reload in dev mode (#297)

This commit is contained in:
Sainan 2024-06-15 02:52:45 +02:00 committed by GitHub
parent b08fff1906
commit e5451d5227
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 86 additions and 90 deletions

View File

@ -1,8 +1,8 @@
import { RequestHandler } from "express"; import { RequestHandler } from "express";
import configFile from "@/config.json"; import { config } from "@/src/services/configService";
const getConfigDataController: RequestHandler = (_req, res) => { const getConfigDataController: RequestHandler = (_req, res) => {
res.json(configFile); res.json(config);
}; };
export { getConfigDataController }; export { getConfigDataController };

View File

@ -1,16 +1,9 @@
import { RequestHandler } from "express"; import { RequestHandler } from "express";
import path from "path"; import { updateConfig } from "@/src/services/configService";
import fs from "fs";
const rootDir = path.join(__dirname, "../../..");
const updateConfigDataController: RequestHandler = req => { const updateConfigDataController: RequestHandler = async (req, res) => {
const updateSettingsData = req.body; await updateConfig(req.body.toString());
res.end();
fs.writeFile(path.join(rootDir, "config.json"), updateSettingsData, function (err: any) {
if (err) {
return console.log(err);
}
});
}; };
export { updateConfigDataController }; export { updateConfigDataController };

View File

@ -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 { interface IConfig {
mongodbUrl: string; mongodbUrl: string;
@ -26,4 +44,7 @@ interface ILoggerConfig {
level: string; // "fatal" | "error" | "warn" | "info" | "http" | "debug" | "trace"; 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);
};

View File

@ -204,81 +204,63 @@
</div> </div>
</div> </div>
<div data-route="/webui/settings" data-title="Settings | OpenWF WebUI"> <div data-route="/webui/settings" data-title="Settings | OpenWF WebUI">
<div class="card mb-4"> <form onsubmit="doChangeSettings();return false;">
<h5 class="card-header">Change Settings</h5> <div class="form-check">
<form class="card-body" onsubmit="doChangeSettings();return false;"> <input class="form-check-input" type="checkbox" id="skipStoryModeChoice" />
<div class="form-check"> <label class="form-check-label" for="skipStoryModeChoice">Skip Story Mode Choice</label>
<input class="form-check-input" type="checkbox" value="" id="skipStoryModeChoice" /> </div>
<label label class="form-check-label" for="skipStoryModeChoice" <div class="form-check">
>Skip Story Mode Choice?</label <input class="form-check-input" type="checkbox" id="skipTutorial" />
> <label class="form-check-label" for="skipTutorial">Skip Tutorial</label>
</div> </div>
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="skipTutorial" /> <input class="form-check-input" type="checkbox" id="unlockAllScans" />
<label label class="form-check-label" for="skipTutorial">Skip Tutorial?</label> <label class="form-check-label" for="unlockAllScans">Unlock All Scans</label>
</div> </div>
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="unlockAllScans" /> <input class="form-check-input" type="checkbox" id="unlockAllMissions" />
<label label class="form-check-label" for="unlockAllScans">Unlock All Scans?</label> <label class="form-check-label" for="unlockAllMissions">Unlock All Missions</label>
</div> </div>
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="unlockAllMissions" /> <input class="form-check-input" type="checkbox" id="unlockAllQuests" />
<label label class="form-check-label" for="unlockAllMissions" <label class="form-check-label" for="unlockAllQuests">Unlock All Quests</label>
>Unlock All Missions?</label </div>
> <div class="form-check">
</div> <input class="form-check-input" type="checkbox" id="completeAllQuests" />
<div class="form-check"> <label class="form-check-label" for="completeAllQuests">Complete All Quests</label>
<input class="form-check-input" type="checkbox" value="" id="unlockAllQuests" /> </div>
<label label class="form-check-label" for="unlockAllQuests">Unlock All Quests?</label> <div class="form-check">
</div> <input class="form-check-input" type="checkbox" id="infiniteResources" />
<div class="form-check"> <label class="form-check-label" for="infiniteResources"
<input class="form-check-input" type="checkbox" value="" id="completeAllQuests" /> >Infinite Credits and Platinum</label
<label label class="form-check-label" for="completeAllQuests" >
>Complete All Quests?</label </div>
> <div class="form-check">
</div> <input class="form-check-input" type="checkbox" id="unlockAllShipFeatures" />
<div class="form-check"> <label class="form-check-label" for="unlockAllShipFeatures">Unlock All Ship Features</label>
<input class="form-check-input" type="checkbox" value="" id="infiniteResources" /> </div>
<label label class="form-check-label" for="infiniteResources" <div class="form-check">
>Infinite Credits and Platinum?</label <input class="form-check-input" type="checkbox" id="unlockAllShipDecorations" />
> <label class="form-check-label" for="unlockAllShipDecorations"
</div> >Unlock All Ship Decorations</label
<div class="form-check"> >
<input class="form-check-input" type="checkbox" value="" id="unlockallShipFeatures" /> </div>
<label label class="form-check-label" for="unlockallShipFeatures" <div class="form-check">
>Unlock All Ship Features?</label <input class="form-check-input" type="checkbox" id="unlockAllFlavourItems" />
> <label class="form-check-label" for="unlockAllFlavourItems">Unlock All Accessories</label>
</div> </div>
<div class="form-check"> <div class="form-check">
<input <input class="form-check-input" type="checkbox" id="unlockAllSkins" />
class="form-check-input" <label class="form-check-label" for="unlockAllSkins">Unlock All Skins</label>
type="checkbox" </div>
value="" <div class="form-group">
id="unlockAllShipDecorations" <label class="form-label" for="spoofMasteryRank"
/> >Spoofed Mastery Rank (-1 to disable)</label
<label label class="form-check-label" for="unlockAllShipDecorations" >
>Unlock All Ship Decorations?</label <input class="form-control" id="spoofMasteryRank" type="number" min="-1" />
> </div>
</div> <button class="btn btn-primary mt-3" type="submit">Save Settings</button>
<div class="form-check"> </form>
<input class="form-check-input" type="checkbox" value="" id="unlockAllFlavourItems" />
<label label class="form-check-label" for="unlockAllFlavourItems"
>Unlock All Accessories?</label
>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="unlockAllSkins" />
<label label class="form-check-label" for="unlockAllSkins">Unlock All Skins?</label>
</div>
<div class="form-check">
<label label class="form-check-label" for="spoofMasteryRank"
>Spoofed Mastery Rank (-1 to disable)</label
>
<input class="form-control" id="spoofMasteryRank" type="number" min="-1" value="" />
</div>
<button class="btn btn-primary" type="submit">Save Settings</button>
</form>
</div>
</div> </div>
</div> </div>
</div> </div>