Move port and hostname to the server object

api-via-socketio-client
Louis Lam 1 year ago
parent d4f9acee6a
commit 5fa2fcb0d9

@ -133,10 +133,13 @@ router.post("/api", headerAuthMiddleware, async (request, response) => {
const requestData = request.body; const requestData = request.body;
console.log(requestData); let hostname = "localhost";
if (server.hostname) {
hostname = server.hostname;
}
const protocol = (server.isHTTPS) ? "wss" : "ws";
// TODO: should not hard coded let wsURL = `${protocol}://${hostname}:${server.port}`;
let wsURL = "ws://localhost:3001";
const socket = ioClient(wsURL, { const socket = ioClient(wsURL, {
transports: [ "websocket" ], transports: [ "websocket" ],

@ -79,7 +79,7 @@ log.info("server", "Importing this project modules");
log.debug("server", "Importing Monitor"); log.debug("server", "Importing Monitor");
const Monitor = require("./model/monitor"); const Monitor = require("./model/monitor");
log.debug("server", "Importing Settings"); log.debug("server", "Importing Settings");
const { getSettings, setSettings, setting, initJWTSecret, checkLogin, FBSD, doubleCheckPassword, startE2eTests, const { getSettings, setSettings, setting, initJWTSecret, checkLogin, doubleCheckPassword, startE2eTests,
allowDevAllOrigin allowDevAllOrigin
} = require("./util-server"); } = require("./util-server");
@ -104,20 +104,6 @@ const passwordHash = require("./password-hash");
const checkVersion = require("./check-version"); const checkVersion = require("./check-version");
log.info("server", "Version: " + checkVersion.version); log.info("server", "Version: " + checkVersion.version);
// If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available and the unspecified IPv4 address (0.0.0.0) otherwise.
// Dual-stack support for (::)
// Also read HOST if not FreeBSD, as HOST is a system environment variable in FreeBSD
let hostEnv = FBSD ? null : process.env.HOST;
let hostname = args.host || process.env.UPTIME_KUMA_HOST || hostEnv;
if (hostname) {
log.info("server", "Custom hostname: " + hostname);
}
const port = [ args.port, process.env.UPTIME_KUMA_PORT, process.env.PORT, 3001 ]
.map(portValue => parseInt(portValue))
.find(portValue => !isNaN(portValue));
const disableFrameSameOrigin = !!process.env.UPTIME_KUMA_DISABLE_FRAME_SAMEORIGIN || args["disable-frame-sameorigin"] || false; const disableFrameSameOrigin = !!process.env.UPTIME_KUMA_DISABLE_FRAME_SAMEORIGIN || args["disable-frame-sameorigin"] || false;
const cloudflaredToken = args["cloudflared-token"] || process.env.UPTIME_KUMA_CLOUDFLARED_TOKEN || undefined; const cloudflaredToken = args["cloudflared-token"] || process.env.UPTIME_KUMA_CLOUDFLARED_TOKEN || undefined;
@ -182,7 +168,7 @@ let needSetup = false;
let setupDatabase = new SetupDatabase(args, server); let setupDatabase = new SetupDatabase(args, server);
if (setupDatabase.isNeedSetup()) { if (setupDatabase.isNeedSetup()) {
// Hold here and start a special setup page until user choose a database type // Hold here and start a special setup page until user choose a database type
await setupDatabase.start(hostname, port); await setupDatabase.start(server.hostname, server.port);
} }
// Connect to database // Connect to database
@ -1749,11 +1735,11 @@ let needSetup = false;
server.start(); server.start();
server.httpServer.listen(port, hostname, () => { server.httpServer.listen(server.port, server.hostname, () => {
if (hostname) { if (server.hostname) {
log.info("server", `Listening on ${hostname}:${port}`); log.info("server", `Listening on ${server.hostname}:${server.port}`);
} else { } else {
log.info("server", `Listening on ${port}`); log.info("server", `Listening on ${server.port}`);
} }
startMonitors(); startMonitors();
checkVersion.startInterval(); checkVersion.startInterval();

@ -12,6 +12,7 @@ const { Settings } = require("./settings");
const dayjs = require("dayjs"); const dayjs = require("dayjs");
const childProcess = require("child_process"); const childProcess = require("child_process");
const path = require("path"); const path = require("path");
const { FBSD } = require("./util-server");
// DO NOT IMPORT HERE IF THE MODULES USED `UptimeKumaServer.getInstance()`, put at the bottom of this file instead. // DO NOT IMPORT HERE IF THE MODULES USED `UptimeKumaServer.getInstance()`, put at the bottom of this file instead.
/** /**
@ -61,6 +62,23 @@ class UptimeKumaServer {
*/ */
jwtSecret = null; jwtSecret = null;
/**
* Port
* @type {number}
*/
port = undefined;
/**
* Hostname
* @type {string|undefined}
*/
hostname = undefined;
/**
* Is SSL enabled?
*/
isHTTPS = false;
/** /**
* Get the current instance of the server if it exists, otherwise * Get the current instance of the server if it exists, otherwise
* create a new instance. * create a new instance.
@ -78,6 +96,23 @@ class UptimeKumaServer {
* @param {object} args Arguments to initialise server with * @param {object} args Arguments to initialise server with
*/ */
constructor(args) { constructor(args) {
// Port
this.port = [ args.port, process.env.UPTIME_KUMA_PORT, process.env.PORT, 3001 ]
.map(portValue => parseInt(portValue))
.find(portValue => !isNaN(portValue));
// Hostname
// If host is omitted, the server will accept connections on the unspecified IPv6 address (::) when IPv6 is available and the unspecified IPv4 address (0.0.0.0) otherwise.
// Dual-stack support for (::)
// Also read HOST if not FreeBSD, as HOST is a system environment variable in FreeBSD
let hostEnv = FBSD ? null : process.env.HOST;
this.hostname = args.host || process.env.UPTIME_KUMA_HOST || hostEnv;
if (this.hostname) {
log.info("server", "Custom hostname: " + this.hostname);
}
// SSL // SSL
const sslKey = args["ssl-key"] || process.env.UPTIME_KUMA_SSL_KEY || process.env.SSL_KEY || undefined; const sslKey = args["ssl-key"] || process.env.UPTIME_KUMA_SSL_KEY || process.env.SSL_KEY || undefined;
const sslCert = args["ssl-cert"] || process.env.UPTIME_KUMA_SSL_CERT || process.env.SSL_CERT || undefined; const sslCert = args["ssl-cert"] || process.env.UPTIME_KUMA_SSL_CERT || process.env.SSL_CERT || undefined;
@ -87,6 +122,7 @@ class UptimeKumaServer {
this.app = express(); this.app = express();
if (sslKey && sslCert) { if (sslKey && sslCert) {
log.info("server", "Server Type: HTTPS"); log.info("server", "Server Type: HTTPS");
this.isHTTPS = true;
this.httpServer = https.createServer({ this.httpServer = https.createServer({
key: fs.readFileSync(sslKey), key: fs.readFileSync(sslKey),
cert: fs.readFileSync(sslCert), cert: fs.readFileSync(sslCert),
@ -94,6 +130,7 @@ class UptimeKumaServer {
}, this.app); }, this.app);
} else { } else {
log.info("server", "Server Type: HTTP"); log.info("server", "Server Type: HTTP");
this.isHTTPS = false;
this.httpServer = http.createServer(this.app); this.httpServer = http.createServer(this.app);
} }

Loading…
Cancel
Save