Move port and hostname to the server object

api-via-socketio-client
Louis Lam 7 months ago
parent d4f9acee6a
commit 5fa2fcb0d9

@ -133,10 +133,13 @@ router.post("/api", headerAuthMiddleware, async (request, response) => {
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 = "ws://localhost:3001";
let wsURL = `${protocol}://${hostname}:${server.port}`;
const socket = ioClient(wsURL, {
transports: [ "websocket" ],

@ -79,7 +79,7 @@ log.info("server", "Importing this project modules");
log.debug("server", "Importing Monitor");
const Monitor = require("./model/monitor");
log.debug("server", "Importing Settings");
const { getSettings, setSettings, setting, initJWTSecret, checkLogin, FBSD, doubleCheckPassword, startE2eTests,
const { getSettings, setSettings, setting, initJWTSecret, checkLogin, doubleCheckPassword, startE2eTests,
allowDevAllOrigin
} = require("./util-server");
@ -104,20 +104,6 @@ const passwordHash = require("./password-hash");
const checkVersion = require("./check-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 cloudflaredToken = args["cloudflared-token"] || process.env.UPTIME_KUMA_CLOUDFLARED_TOKEN || undefined;
@ -182,7 +168,7 @@ let needSetup = false;
let setupDatabase = new SetupDatabase(args, server);
if (setupDatabase.isNeedSetup()) {
// 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
@ -1749,11 +1735,11 @@ let needSetup = false;
server.start();
server.httpServer.listen(port, hostname, () => {
if (hostname) {
log.info("server", `Listening on ${hostname}:${port}`);
server.httpServer.listen(server.port, server.hostname, () => {
if (server.hostname) {
log.info("server", `Listening on ${server.hostname}:${server.port}`);
} else {
log.info("server", `Listening on ${port}`);
log.info("server", `Listening on ${server.port}`);
}
startMonitors();
checkVersion.startInterval();

@ -12,6 +12,7 @@ const { Settings } = require("./settings");
const dayjs = require("dayjs");
const childProcess = require("child_process");
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.
/**
@ -61,6 +62,23 @@ class UptimeKumaServer {
*/
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
* create a new instance.
@ -78,6 +96,23 @@ class UptimeKumaServer {
* @param {object} args Arguments to initialise server with
*/
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
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;
@ -87,6 +122,7 @@ class UptimeKumaServer {
this.app = express();
if (sslKey && sslCert) {
log.info("server", "Server Type: HTTPS");
this.isHTTPS = true;
this.httpServer = https.createServer({
key: fs.readFileSync(sslKey),
cert: fs.readFileSync(sslCert),
@ -94,6 +130,7 @@ class UptimeKumaServer {
}, this.app);
} else {
log.info("server", "Server Type: HTTP");
this.isHTTPS = false;
this.httpServer = http.createServer(this.app);
}

Loading…
Cancel
Save