diff --git a/proxyServiceValidator.js b/proxyServiceValidator.js index c14d5cab..80ca2375 100644 --- a/proxyServiceValidator.js +++ b/proxyServiceValidator.js @@ -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 puppeteer = require("puppeteer"); diff --git a/run-command.mjs b/run-command.mjs index 1f07b67c..ba7611d6 100644 --- a/run-command.mjs +++ b/run-command.mjs @@ -2,10 +2,16 @@ import { readFile, writeFile, unlink, mkdir, rm } from 'node:fs/promises'; import { exec, fork } from 'node:child_process'; import { fileURLToPath } from 'node:url'; 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 => { try { @@ -14,7 +20,7 @@ const serverUrl = (base => { base = new URL("http://a"); base.host = config.host; } - base.port = process.env.PORT || config.port; + base.port = ecosystemConfig[ config.production ? "env_production" : "env" ].PORT; return Object.freeze(base); })(); @@ -28,7 +34,7 @@ for (let i = 2; i < process.argv.length; i++) // config file. case "start": 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) => { if (error) throw error; console.log(stdout); @@ -41,6 +47,8 @@ for (let i = 2; i < process.argv.length; i++) if (error) throw error; 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 { const server = fork( 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. case "kill": if (process.platform === "win32") - exec("( npx pm2 delete ecosystem.config.js ) ; taskkill /F /IM node*", (error, stdout) => {console.log(stdout)}); - else exec("npx pm2 delete ecosystem.config.js; pkill node", (error, stdout) => {console.log(stdout)}); + exec("( npx pm2 delete ecosystem.config.js ) ; taskkill /F /IM node*", + (error, stdout) => {console.log(stdout)} + ); + else exec("npx pm2 delete ecosystem.config.js; pkill node", + (error, stdout) => {console.log(stdout)} + ); break; // No default case. diff --git a/src/config.json b/src/config.json index 438a7924..5ac433c5 100644 --- a/src/config.json +++ b/src/config.json @@ -1,7 +1,6 @@ { "title": "HU LTS", "host": "0.0.0.0", - "port": "8080", "minifyScripts": true, "production": false } diff --git a/src/server.mjs b/src/server.mjs index 3f6d21de..00e1972a 100644 --- a/src/server.mjs +++ b/src/server.mjs @@ -16,14 +16,20 @@ import { paintSource, tryReadFile } from './randomization.mjs'; import loadTemplates from './templates.mjs'; import { fileURLToPath } from 'node:url'; import { existsSync, unlinkSync } from 'node:fs'; +import ecosystem from '../ecosystem.config.js'; -const config = Object.freeze(JSON.parse( - await readFile(new URL("./config.json", import.meta.url)) - )), +const config = Object.freeze( + 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, __dirname = path.resolve(); // 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 => { try { base = new URL(config.host); @@ -31,7 +37,7 @@ const serverUrl = (base => { base = new URL("http://a"); base.host = config.host; } - base.port = process.env.PORT || config.port; + base.port = ecosystemConfig[ config.production ? "env_production" : "env" ].PORT; return Object.freeze(base); })(); console.log(serverUrl);