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 configFile from "@/config.json";
import { config } from "@/src/services/configService";
const getConfigDataController: RequestHandler = (_req, res) => {
res.json(configFile);
res.json(config);
};
export { getConfigDataController };

View File

@ -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 };

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 {
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);
};

View File

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