2022-04-19 08:38:59 +01:00
|
|
|
const express = require("express");
|
|
|
|
const https = require("https");
|
|
|
|
const fs = require("fs");
|
|
|
|
const http = require("http");
|
|
|
|
const { Server } = require("socket.io");
|
|
|
|
const { R } = require("redbean-node");
|
2022-04-19 09:46:45 +01:00
|
|
|
const { log } = require("../src/util");
|
2022-05-06 07:41:34 +01:00
|
|
|
const Database = require("./database");
|
|
|
|
const util = require("util");
|
2022-04-19 08:38:59 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* `module.exports` (alias: `server`) should be inside this class, in order to avoid circular dependency issue.
|
|
|
|
* @type {UptimeKumaServer}
|
|
|
|
*/
|
|
|
|
class UptimeKumaServer {
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @type {UptimeKumaServer}
|
|
|
|
*/
|
|
|
|
static instance = null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Main monitor list
|
|
|
|
* @type {{}}
|
|
|
|
*/
|
|
|
|
monitorList = {};
|
|
|
|
entryPage = "dashboard";
|
|
|
|
app = undefined;
|
|
|
|
httpServer = undefined;
|
|
|
|
io = undefined;
|
|
|
|
|
2022-05-30 08:45:44 +01:00
|
|
|
/**
|
|
|
|
* Cache Index HTML
|
|
|
|
* @type {string}
|
|
|
|
*/
|
|
|
|
indexHTML = "";
|
|
|
|
|
2022-04-19 08:38:59 +01:00
|
|
|
static getInstance(args) {
|
|
|
|
if (UptimeKumaServer.instance == null) {
|
|
|
|
UptimeKumaServer.instance = new UptimeKumaServer(args);
|
|
|
|
}
|
|
|
|
return UptimeKumaServer.instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(args) {
|
|
|
|
// SSL
|
2022-04-19 09:46:45 +01:00
|
|
|
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;
|
2022-04-19 08:38:59 +01:00
|
|
|
|
2022-04-19 09:46:45 +01:00
|
|
|
log.info("server", "Creating express and socket.io instance");
|
2022-04-19 08:38:59 +01:00
|
|
|
this.app = express();
|
|
|
|
|
|
|
|
if (sslKey && sslCert) {
|
2022-04-19 09:46:45 +01:00
|
|
|
log.info("server", "Server Type: HTTPS");
|
2022-04-19 08:38:59 +01:00
|
|
|
this.httpServer = https.createServer({
|
|
|
|
key: fs.readFileSync(sslKey),
|
|
|
|
cert: fs.readFileSync(sslCert)
|
|
|
|
}, this.app);
|
|
|
|
} else {
|
2022-04-19 09:46:45 +01:00
|
|
|
log.info("server", "Server Type: HTTP");
|
2022-04-19 08:38:59 +01:00
|
|
|
this.httpServer = http.createServer(this.app);
|
|
|
|
}
|
|
|
|
|
2022-05-30 08:45:44 +01:00
|
|
|
try {
|
|
|
|
this.indexHTML = fs.readFileSync("./dist/index.html").toString();
|
|
|
|
} catch (e) {
|
|
|
|
// "dist/index.html" is not necessary for development
|
|
|
|
if (process.env.NODE_ENV !== "development") {
|
|
|
|
log.error("server", "Error: Cannot find 'dist/index.html', did you install correctly?");
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-19 08:38:59 +01:00
|
|
|
this.io = new Server(this.httpServer);
|
|
|
|
}
|
|
|
|
|
|
|
|
async sendMonitorList(socket) {
|
|
|
|
let list = await this.getMonitorJSONList(socket.userID);
|
|
|
|
this.io.to(socket.userID).emit("monitorList", list);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2022-04-19 09:46:45 +01:00
|
|
|
/**
|
|
|
|
* Get a list of monitors for the given user.
|
|
|
|
* @param {string} userID - The ID of the user to get monitors for.
|
|
|
|
* @returns {Promise<Object>} A promise that resolves to an object with monitor IDs as keys and monitor objects as values.
|
|
|
|
*
|
|
|
|
* Generated by Trelent
|
|
|
|
*/
|
2022-04-19 08:38:59 +01:00
|
|
|
async getMonitorJSONList(userID) {
|
|
|
|
let result = {};
|
|
|
|
|
|
|
|
let monitorList = await R.find("monitor", " user_id = ? ORDER BY weight DESC, name", [
|
|
|
|
userID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let monitor of monitorList) {
|
|
|
|
result[monitor.id] = await monitor.toJSON();
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2022-05-06 07:41:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Write error to log file
|
|
|
|
* @param {any} error The error to write
|
|
|
|
* @param {boolean} outputToConsole Should the error also be output to console?
|
|
|
|
*/
|
|
|
|
static errorLog(error, outputToConsole = true) {
|
|
|
|
const errorLogStream = fs.createWriteStream(Database.dataDir + "/error.log", {
|
|
|
|
flags: "a"
|
|
|
|
});
|
|
|
|
|
|
|
|
errorLogStream.on("error", () => {
|
|
|
|
log.info("", "Cannot write to error.log");
|
|
|
|
});
|
|
|
|
|
|
|
|
if (errorLogStream) {
|
|
|
|
const dateTime = R.isoDateTime();
|
|
|
|
errorLogStream.write(`[${dateTime}] ` + util.format(error) + "\n");
|
|
|
|
|
|
|
|
if (outputToConsole) {
|
|
|
|
console.error(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
errorLogStream.end();
|
|
|
|
}
|
2022-04-19 08:38:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
UptimeKumaServer
|
|
|
|
};
|