Clean up port listening implementation; more commenting.

This commit is contained in:
00Fjongl 2024-08-07 21:10:07 -05:00
parent 5b7e418007
commit a140c73537
4 changed files with 31 additions and 11 deletions

View file

@ -1,3 +1,6 @@
// This file is solely used for the automatically run GitHub job, which checks to
// see if all HU LTS code is working properly (at least on an Ubuntu machine).
const axios = require("axios"); const axios = require("axios");
const puppeteer = require("puppeteer"); const puppeteer = require("puppeteer");

View file

@ -2,10 +2,16 @@ import { readFile, writeFile, unlink, mkdir, rm } from 'node:fs/promises';
import { exec, fork } from 'node:child_process'; import { exec, fork } from 'node:child_process';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { build } from 'esbuild'; import { build } from 'esbuild';
import ecosystem from './ecosystem.config.js';
// Necessary constants are copied over from /src/server.mjs. // Some necessary constants are copied over from /src/server.mjs.
const config = Object.freeze(JSON.parse(await readFile(new URL("./src/config.json", import.meta.url)))); const config = Object.freeze(
JSON.parse(await readFile(new URL("./src/config.json", import.meta.url)))
),
ecosystemConfig = Object.freeze(
ecosystem.apps.find(app => app.name === "HolyUB") || apps[0]
);
const serverUrl = (base => { const serverUrl = (base => {
try { try {
@ -14,7 +20,7 @@ const serverUrl = (base => {
base = new URL("http://a"); base = new URL("http://a");
base.host = config.host; base.host = config.host;
} }
base.port = process.env.PORT || config.port; base.port = ecosystemConfig[ config.production ? "env_production" : "env" ].PORT;
return Object.freeze(base); return Object.freeze(base);
})(); })();
@ -28,7 +34,7 @@ for (let i = 2; i < process.argv.length; i++)
// config file. // config file.
case "start": case "start":
if (config.production) if (config.production)
exec("npx pm2 start ecosystem.config.js --env production --watch false", exec("npx pm2 start ecosystem.config.js --env production",
(error, stdout) => { (error, stdout) => {
if (error) throw error; if (error) throw error;
console.log(stdout); console.log(stdout);
@ -41,6 +47,8 @@ for (let i = 2; i < process.argv.length; i++)
if (error) throw error; if (error) throw error;
console.log(stdout); console.log(stdout);
}); });
// The following approach (and similar approaches) will not work on Windows,
// because exiting this program will also terminate backend.js on Windows.
else { else {
const server = fork( const server = fork(
fileURLToPath(new URL("./backend.js", import.meta.url)), fileURLToPath(new URL("./backend.js", import.meta.url)),
@ -106,8 +114,12 @@ for (let i = 2; i < process.argv.length; i++)
// more PM2 debugging tools. // more PM2 debugging tools.
case "kill": case "kill":
if (process.platform === "win32") if (process.platform === "win32")
exec("( npx pm2 delete ecosystem.config.js ) ; taskkill /F /IM node*", (error, stdout) => {console.log(stdout)}); exec("( npx pm2 delete ecosystem.config.js ) ; taskkill /F /IM node*",
else exec("npx pm2 delete ecosystem.config.js; pkill node", (error, stdout) => {console.log(stdout)}); (error, stdout) => {console.log(stdout)}
);
else exec("npx pm2 delete ecosystem.config.js; pkill node",
(error, stdout) => {console.log(stdout)}
);
break; break;
// No default case. // No default case.

View file

@ -1,7 +1,6 @@
{ {
"title": "HU LTS", "title": "HU LTS",
"host": "0.0.0.0", "host": "0.0.0.0",
"port": "8080",
"minifyScripts": true, "minifyScripts": true,
"production": false "production": false
} }

View file

@ -16,14 +16,20 @@ import { paintSource, tryReadFile } from './randomization.mjs';
import loadTemplates from './templates.mjs'; import loadTemplates from './templates.mjs';
import { fileURLToPath } from 'node:url'; import { fileURLToPath } from 'node:url';
import { existsSync, unlinkSync } from 'node:fs'; import { existsSync, unlinkSync } from 'node:fs';
import ecosystem from '../ecosystem.config.js';
const config = Object.freeze(JSON.parse( const config = Object.freeze(
await readFile(new URL("./config.json", import.meta.url)) JSON.parse(await readFile(new URL("./config.json", import.meta.url)))
)), ),
ecosystemConfig = Object.freeze(
ecosystem.apps.find(app => app.name === "HolyUB") || apps[0]
),
{ pages, text404 } = pkg, { pages, text404 } = pkg,
__dirname = path.resolve(); __dirname = path.resolve();
// Record the server's location as a URL object, including its host and port. // Record the server's location as a URL object, including its host and port.
// The host can be modified at /src/config.json, whereas the ports can be modified
// at /ecosystem.config.js.
const serverUrl = (base => { const serverUrl = (base => {
try { try {
base = new URL(config.host); base = new URL(config.host);
@ -31,7 +37,7 @@ const serverUrl = (base => {
base = new URL("http://a"); base = new URL("http://a");
base.host = config.host; base.host = config.host;
} }
base.port = process.env.PORT || config.port; base.port = ecosystemConfig[ config.production ? "env_production" : "env" ].PORT;
return Object.freeze(base); return Object.freeze(base);
})(); })();
console.log(serverUrl); console.log(serverUrl);