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,84 +204,66 @@
</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>
<form class="card-body" onsubmit="doChangeSettings();return false;">
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="skipStoryModeChoice" /> <input class="form-check-input" type="checkbox" id="skipStoryModeChoice" />
<label label class="form-check-label" for="skipStoryModeChoice" <label class="form-check-label" for="skipStoryModeChoice">Skip Story Mode Choice</label>
>Skip Story Mode Choice?</label </div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="skipTutorial" />
<label class="form-check-label" for="skipTutorial">Skip Tutorial</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="unlockAllScans" />
<label class="form-check-label" for="unlockAllScans">Unlock All Scans</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="unlockAllMissions" />
<label class="form-check-label" for="unlockAllMissions">Unlock All Missions</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="unlockAllQuests" />
<label class="form-check-label" for="unlockAllQuests">Unlock All Quests</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="completeAllQuests" />
<label class="form-check-label" for="completeAllQuests">Complete All Quests</label>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" id="infiniteResources" />
<label class="form-check-label" for="infiniteResources"
>Infinite Credits and Platinum</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="unlockAllShipFeatures" />
<label label class="form-check-label" for="skipTutorial">Skip Tutorial?</label> <label class="form-check-label" for="unlockAllShipFeatures">Unlock All Ship Features</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="unlockAllShipDecorations" />
<label label class="form-check-label" for="unlockAllScans">Unlock All Scans?</label> <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="unlockAllMissions" />
<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="unlockAllQuests" /> <input class="form-check-input" type="checkbox" id="unlockAllFlavourItems" />
<label label class="form-check-label" for="unlockAllQuests">Unlock All Quests?</label> <label class="form-check-label" for="unlockAllFlavourItems">Unlock All Accessories</label>
</div> </div>
<div class="form-check"> <div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="completeAllQuests" /> <input class="form-check-input" type="checkbox" id="unlockAllSkins" />
<label label class="form-check-label" for="completeAllQuests" <label class="form-check-label" for="unlockAllSkins">Unlock All Skins</label>
>Complete All Quests?</label
>
</div> </div>
<div class="form-check"> <div class="form-group">
<input class="form-check-input" type="checkbox" value="" id="infiniteResources" /> <label class="form-label" for="spoofMasteryRank"
<label label class="form-check-label" for="infiniteResources"
>Infinite Credits and Platinum?</label
>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="unlockallShipFeatures" />
<label label class="form-check-label" for="unlockallShipFeatures"
>Unlock All Ship Features?</label
>
</div>
<div class="form-check">
<input
class="form-check-input"
type="checkbox"
value=""
id="unlockAllShipDecorations"
/>
<label label class="form-check-label" for="unlockAllShipDecorations"
>Unlock All Ship Decorations?</label
>
</div>
<div class="form-check">
<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 >Spoofed Mastery Rank (-1 to disable)</label
> >
<input class="form-control" id="spoofMasteryRank" type="number" min="-1" value="" /> <input class="form-control" id="spoofMasteryRank" type="number" min="-1" />
</div> </div>
<button class="btn btn-primary" type="submit">Save Settings</button> <button class="btn btn-primary mt-3" type="submit">Save Settings</button>
</form> </form>
</div> </div>
</div> </div>
</div> </div>
</div>
<datalist id="datalist-warframes"></datalist> <datalist id="datalist-warframes"></datalist>
<datalist id="datalist-weapons"></datalist> <datalist id="datalist-weapons"></datalist>
<datalist id="datalist-miscitems"></datalist> <datalist id="datalist-miscitems"></datalist>