New shutdown script for cross-platform ease of use; implement host option in config file; minor code cleanup.

This commit is contained in:
00Fjongl 2024-08-07 10:10:19 -05:00
parent c21d775ee8
commit 92909e4ab7
7 changed files with 59 additions and 10 deletions

21
shutdown.mjs Normal file
View file

@ -0,0 +1,21 @@
import { readFile, writeFile, unlink } from 'node:fs/promises';
const config = Object.freeze(JSON.parse(await readFile(new URL("./src/config.json", import.meta.url))));
const serverUrl = (base => {
try {
base = new URL(config.host);
} catch (e) {
base = new URL("http://a");
base.host = config.host;
}
base.port = process.env.PORT || config.port;
return Object.freeze(base);
})();
const shutdown = new URL("./src/.shutdown", import.meta.url);
await writeFile(shutdown, "");
try {await fetch(new URL("/test-shutdown", serverUrl))}
catch (e) {await unlink(shutdown)}
process.exitCode = 0;