diff --git a/js/app.js b/js/app.js index 60ea7ad15f..496a296041 100644 --- a/js/app.js +++ b/js/app.js @@ -42,12 +42,6 @@ if (process.env.MM_CONFIG_FILE) { global.configuration_file = process.env.MM_CONFIG_FILE.replace(`${global.root_path}/`, ""); } -// FIXME: Hotfix Pull Request -// https://github.com/MagicMirrorOrg/MagicMirror/pull/673 -if (process.env.MM_PORT) { - global.mmPort = process.env.MM_PORT; -} - // The next part is here to prevent a major exception when there // is no internet connection. This could probable be solved better. process.on("uncaughtException", function (err) { diff --git a/js/defaults.js b/js/defaults.js index 4d923cd65f..64a16f10da 100644 --- a/js/defaults.js +++ b/js/defaults.js @@ -1,13 +1,6 @@ -/* global mmPort */ - -const address = "localhost"; -let port = 8080; -if (typeof mmPort !== "undefined") { - port = mmPort; -} const defaults = { - address: address, - port: port, + address: "localhost", + port: 8080, basePath: "/", useHttps: false, // Support HTTPS or not, default "false" will use HTTP httpsPrivateKey: "", // HTTPS private key path, only required when useHttps is true diff --git a/js/electron.js b/js/electron.js index 0cd902203a..68449de6f4 100644 --- a/js/electron.js +++ b/js/electron.js @@ -4,6 +4,7 @@ const electron = require("electron"); const core = require("./app"); const Log = require("./logger"); const { applyElectronSwitches } = require("./electron_helper"); +const { getServerPort } = require("#server_functions"); // Config let config = process.env.config ? JSON.parse(process.env.config) : {}; @@ -101,7 +102,7 @@ function createWindow () { } const address = (config.address === void 0) | (config.address === "") | (config.address === "0.0.0.0") | (config.address === "::") ? (config.address = "localhost") : config.address; - const port = process.env.MM_PORT || config.port; + const port = getServerPort(config); mainWindow.loadURL(`${prefix}${address}:${port}`); // Open the DevTools if run with "node --run start:dev" diff --git a/js/server.js b/js/server.js index c534b30c2c..ed1a8ae420 100644 --- a/js/server.js +++ b/js/server.js @@ -11,7 +11,7 @@ const { ipAccessControl, socketIpAccessControl } = require("./ip_access_control" const vendor = require("./vendor"); -const { getHtml, getVersion, getEnvVars, cors } = require("#server_functions"); +const { getHtml, getVersion, getEnvVars, getServerPort, cors } = require("#server_functions"); /** * Server @@ -21,7 +21,7 @@ const { getHtml, getVersion, getEnvVars, cors } = require("#server_functions"); function Server (configObj) { const config = configObj.fullConf; const app = express(); - const port = process.env.MM_PORT || config.port; + const port = getServerPort(config); const serverSockets = new Set(); let server = null; diff --git a/js/server_functions.js b/js/server_functions.js index 55f9b81ca5..2648e3317e 100644 --- a/js/server_functions.js +++ b/js/server_functions.js @@ -251,6 +251,16 @@ function getEnvVars (req, res) { res.send(obj); } +/** + * Resolves the HTTP server port. The `MM_PORT` environment variable takes + * precedence over the configured port, falling back to 8080. + * @param {object} [config] the configuration to read the port from (defaults to global.config) + * @returns {number} the port the server should listen on + */ +function getServerPort (config = global.config) { + return Number(process.env.MM_PORT || config?.port || 8080); +} + /** * Get the config file path from environment or default location * @returns {string} The absolute config file path @@ -269,4 +279,4 @@ function getConfigFilePath () { return path.resolve(global.configuration_file || `${global.root_path}/config/config.js`); } -module.exports = { cors, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getConfigFilePath, replaceSecretPlaceholder }; +module.exports = { cors, getHtml, getVersion, getStartup, getEnvVars, getEnvVarsAsObj, getUserAgent, getServerPort, getConfigFilePath, replaceSecretPlaceholder }; diff --git a/serveronly/index.js b/serveronly/index.js index 6156a0a0a5..bd1470e2a5 100644 --- a/serveronly/index.js +++ b/serveronly/index.js @@ -1,8 +1,9 @@ const app = require("../js/app"); const Log = require("../js/logger"); +const { getServerPort } = require("#server_functions"); app.start().then((config) => { const bindAddress = config.address ? config.address : "localhost"; const httpType = config.useHttps ? "https" : "http"; - Log.info(`\n>>> Ready to go! Please point your browser to: ${httpType}://${bindAddress}:${global.mmPort || config.port} <<<`); + Log.info(`\n>>> Ready to go! Please point your browser to: ${httpType}://${bindAddress}:${getServerPort(config)} <<<`); }); diff --git a/serveronly/watcher.js b/serveronly/watcher.js index ab8aa0a1e5..b65d6f9917 100644 --- a/serveronly/watcher.js +++ b/serveronly/watcher.js @@ -7,7 +7,7 @@ const path = require("node:path"); const net = require("node:net"); const http = require("node:http"); const Log = require("logger"); -const { getConfigFilePath } = require("#server_functions"); +const { getConfigFilePath, getServerPort } = require("#server_functions"); const RESTART_DELAY_MS = 500; const PORT_CHECK_MAX_ATTEMPTS = 20; @@ -32,7 +32,7 @@ function getServerConfig () { delete require.cache[require.resolve(configPath)]; const config = require(configPath); serverConfig = { - port: global.mmPort || config.port || 8080, + port: getServerPort(config), address: config.address || "localhost" }; } catch {