2023-02-10 22:41:02 -08:00
|
|
|
const express = require("express");
|
|
|
|
const { log } = require("../src/util");
|
|
|
|
const expressStaticGzip = require("express-static-gzip");
|
|
|
|
const fs = require("fs");
|
|
|
|
const path = require("path");
|
|
|
|
const Database = require("./database");
|
|
|
|
const { allowDevAllOrigin } = require("./util-server");
|
2023-06-30 11:48:42 -07:00
|
|
|
const mysql = require("mysql2/promise");
|
2023-02-10 22:41:02 -08:00
|
|
|
|
|
|
|
/**
|
2023-09-07 00:42:44 -07:00
|
|
|
* A standalone express app that is used to setup a database
|
2023-02-10 22:41:02 -08:00
|
|
|
* It is used when db-config.json and kuma.db are not found or invalid
|
2023-09-07 00:42:44 -07:00
|
|
|
* Once it is configured, it will shut down and start the main server
|
2023-02-10 22:41:02 -08:00
|
|
|
*/
|
|
|
|
class SetupDatabase {
|
|
|
|
/**
|
|
|
|
* Show Setup Page
|
|
|
|
* @type {boolean}
|
|
|
|
*/
|
|
|
|
needSetup = true;
|
2023-09-07 00:42:44 -07:00
|
|
|
/**
|
|
|
|
* If the server has finished the setup
|
|
|
|
* @type {boolean}
|
|
|
|
* @private
|
|
|
|
*/
|
2023-06-30 07:17:07 -07:00
|
|
|
runningSetup = false;
|
2023-09-07 00:42:44 -07:00
|
|
|
/**
|
|
|
|
* @inheritDoc
|
|
|
|
* @type {UptimeKumaServer}
|
|
|
|
* @private
|
|
|
|
*/
|
2023-02-10 22:41:02 -08:00
|
|
|
server;
|
|
|
|
|
2023-08-11 07:29:45 -07:00
|
|
|
/**
|
2023-09-07 00:42:44 -07:00
|
|
|
* @param {object} args The arguments passed from the command line
|
|
|
|
* @param {UptimeKumaServer} server the main server instance
|
2023-08-11 07:29:45 -07:00
|
|
|
*/
|
2023-02-10 22:41:02 -08:00
|
|
|
constructor(args, server) {
|
|
|
|
this.server = server;
|
|
|
|
|
|
|
|
// Priority: env > db-config.json
|
|
|
|
// If env is provided, write it to db-config.json
|
|
|
|
// If db-config.json is found, check if it is valid
|
|
|
|
// If db-config.json is not found or invalid, check if kuma.db is found
|
|
|
|
// If kuma.db is not found, show setup page
|
|
|
|
|
|
|
|
let dbConfig;
|
|
|
|
|
|
|
|
try {
|
|
|
|
dbConfig = Database.readDBConfig();
|
2023-10-13 12:00:34 -07:00
|
|
|
log.debug("setup-database", "db-config.json is found and is valid");
|
2023-02-11 06:21:06 -08:00
|
|
|
this.needSetup = false;
|
|
|
|
|
2023-02-10 22:41:02 -08:00
|
|
|
} catch (e) {
|
|
|
|
log.info("setup-database", "db-config.json is not found or invalid: " + e.message);
|
|
|
|
|
2023-09-04 06:32:48 -07:00
|
|
|
// Check if kuma.db is found (1.X.X users), generate db-config.json
|
2023-02-10 22:41:02 -08:00
|
|
|
if (fs.existsSync(path.join(Database.dataDir, "kuma.db"))) {
|
|
|
|
this.needSetup = false;
|
2023-09-04 06:32:48 -07:00
|
|
|
|
|
|
|
log.info("setup-database", "kuma.db is found, generate db-config.json");
|
|
|
|
Database.writeDBConfig({
|
|
|
|
type: "sqlite",
|
|
|
|
});
|
2023-02-10 22:41:02 -08:00
|
|
|
} else {
|
|
|
|
this.needSetup = true;
|
|
|
|
}
|
|
|
|
dbConfig = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (process.env.UPTIME_KUMA_DB_TYPE) {
|
|
|
|
this.needSetup = false;
|
|
|
|
log.info("setup-database", "UPTIME_KUMA_DB_TYPE is provided by env, try to override db-config.json");
|
|
|
|
dbConfig.type = process.env.UPTIME_KUMA_DB_TYPE;
|
|
|
|
dbConfig.hostname = process.env.UPTIME_KUMA_DB_HOSTNAME;
|
|
|
|
dbConfig.port = process.env.UPTIME_KUMA_DB_PORT;
|
2023-12-04 13:16:55 -08:00
|
|
|
dbConfig.dbName = process.env.UPTIME_KUMA_DB_NAME;
|
2023-02-10 22:41:02 -08:00
|
|
|
dbConfig.username = process.env.UPTIME_KUMA_DB_USERNAME;
|
|
|
|
dbConfig.password = process.env.UPTIME_KUMA_DB_PASSWORD;
|
|
|
|
Database.writeDBConfig(dbConfig);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show Setup Page
|
2023-09-07 00:42:44 -07:00
|
|
|
* @returns {boolean} true if the setup page should be shown
|
2023-02-10 22:41:02 -08:00
|
|
|
*/
|
|
|
|
isNeedSetup() {
|
|
|
|
return this.needSetup;
|
|
|
|
}
|
|
|
|
|
2023-08-11 07:29:45 -07:00
|
|
|
/**
|
2023-09-07 00:42:44 -07:00
|
|
|
* Check if the embedded MariaDB is enabled
|
|
|
|
* @returns {boolean} true if the embedded MariaDB is enabled
|
2023-08-11 07:29:45 -07:00
|
|
|
*/
|
2023-02-10 22:41:02 -08:00
|
|
|
isEnabledEmbeddedMariaDB() {
|
|
|
|
return process.env.UPTIME_KUMA_ENABLE_EMBEDDED_MARIADB === "1";
|
|
|
|
}
|
|
|
|
|
2023-08-11 07:29:45 -07:00
|
|
|
/**
|
2023-09-07 00:42:44 -07:00
|
|
|
* Start the setup-database server
|
|
|
|
* @param {string} hostname where the server is listening
|
|
|
|
* @param {number} port where the server is listening
|
|
|
|
* @returns {Promise<void>}
|
2023-08-11 07:29:45 -07:00
|
|
|
*/
|
2023-02-10 22:41:02 -08:00
|
|
|
start(hostname, port) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const app = express();
|
|
|
|
let tempServer;
|
|
|
|
app.use(express.json());
|
|
|
|
|
2023-06-30 07:17:07 -07:00
|
|
|
// Disable Keep Alive, otherwise the server will not shutdown, as the client will keep the connection alive
|
|
|
|
app.use(function (req, res, next) {
|
|
|
|
res.setHeader("Connection", "close");
|
|
|
|
next();
|
|
|
|
});
|
|
|
|
|
2023-02-10 22:41:02 -08:00
|
|
|
app.get("/", async (request, response) => {
|
|
|
|
response.redirect("/setup-database");
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get("/api/entry-page", async (request, response) => {
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
response.json({
|
|
|
|
type: "setup-database",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2023-06-30 07:17:07 -07:00
|
|
|
app.get("/setup-database-info", (request, response) => {
|
2023-02-10 22:41:02 -08:00
|
|
|
allowDevAllOrigin(response);
|
2023-06-30 07:17:07 -07:00
|
|
|
console.log("Request /setup-database-info");
|
2023-02-10 22:41:02 -08:00
|
|
|
response.json({
|
2023-06-30 07:17:07 -07:00
|
|
|
runningSetup: this.runningSetup,
|
|
|
|
needSetup: this.needSetup,
|
2023-02-10 22:41:02 -08:00
|
|
|
isEnabledEmbeddedMariaDB: this.isEnabledEmbeddedMariaDB(),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post("/setup-database", async (request, response) => {
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
|
2023-06-30 07:17:07 -07:00
|
|
|
if (this.runningSetup) {
|
|
|
|
response.status(400).json("Setup is already running");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.runningSetup = true;
|
2023-02-10 22:41:02 -08:00
|
|
|
|
|
|
|
let dbConfig = request.body.dbConfig;
|
|
|
|
|
|
|
|
let supportedDBTypes = [ "mariadb", "sqlite" ];
|
|
|
|
|
|
|
|
if (this.isEnabledEmbeddedMariaDB()) {
|
|
|
|
supportedDBTypes.push("embedded-mariadb");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate input
|
|
|
|
if (typeof dbConfig !== "object") {
|
|
|
|
response.status(400).json("Invalid dbConfig");
|
2023-06-30 07:17:07 -07:00
|
|
|
this.runningSetup = false;
|
2023-02-10 22:41:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dbConfig.type) {
|
|
|
|
response.status(400).json("Database Type is required");
|
2023-06-30 07:17:07 -07:00
|
|
|
this.runningSetup = false;
|
2023-02-10 22:41:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!supportedDBTypes.includes(dbConfig.type)) {
|
|
|
|
response.status(400).json("Unsupported Database Type");
|
2023-06-30 07:17:07 -07:00
|
|
|
this.runningSetup = false;
|
2023-02-10 22:41:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-06-30 11:48:42 -07:00
|
|
|
// External MariaDB
|
2023-02-10 22:41:02 -08:00
|
|
|
if (dbConfig.type === "mariadb") {
|
|
|
|
if (!dbConfig.hostname) {
|
|
|
|
response.status(400).json("Hostname is required");
|
2023-06-30 07:17:07 -07:00
|
|
|
this.runningSetup = false;
|
2023-02-10 22:41:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dbConfig.port) {
|
|
|
|
response.status(400).json("Port is required");
|
2023-06-30 07:17:07 -07:00
|
|
|
this.runningSetup = false;
|
2023-02-10 22:41:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dbConfig.dbName) {
|
|
|
|
response.status(400).json("Database name is required");
|
2023-06-30 07:17:07 -07:00
|
|
|
this.runningSetup = false;
|
2023-02-10 22:41:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dbConfig.username) {
|
|
|
|
response.status(400).json("Username is required");
|
2023-06-30 07:17:07 -07:00
|
|
|
this.runningSetup = false;
|
2023-02-10 22:41:02 -08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dbConfig.password) {
|
|
|
|
response.status(400).json("Password is required");
|
2023-06-30 07:17:07 -07:00
|
|
|
this.runningSetup = false;
|
2023-02-10 22:41:02 -08:00
|
|
|
return;
|
|
|
|
}
|
2023-06-30 11:48:42 -07:00
|
|
|
|
|
|
|
// Test connection
|
|
|
|
try {
|
|
|
|
const connection = await mysql.createConnection({
|
|
|
|
host: dbConfig.hostname,
|
|
|
|
port: dbConfig.port,
|
|
|
|
user: dbConfig.username,
|
|
|
|
password: dbConfig.password,
|
|
|
|
});
|
|
|
|
await connection.execute("SELECT 1");
|
|
|
|
connection.end();
|
|
|
|
} catch (e) {
|
|
|
|
response.status(400).json("Cannot connect to the database: " + e.message);
|
|
|
|
this.runningSetup = false;
|
|
|
|
return;
|
|
|
|
}
|
2023-02-10 22:41:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write db-config.json
|
|
|
|
Database.writeDBConfig(dbConfig);
|
|
|
|
|
|
|
|
response.json({
|
|
|
|
ok: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Shutdown down this express and start the main server
|
2023-06-30 07:17:07 -07:00
|
|
|
log.info("setup-database", "Database is configured, close the setup-database server and start the main server now.");
|
2023-02-10 22:41:02 -08:00
|
|
|
if (tempServer) {
|
2023-06-30 07:17:07 -07:00
|
|
|
tempServer.close(() => {
|
|
|
|
log.info("setup-database", "The setup-database server is closed");
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
resolve();
|
2023-02-10 22:41:02 -08:00
|
|
|
}
|
2023-06-30 07:17:07 -07:00
|
|
|
|
2023-02-10 22:41:02 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
app.use("/", expressStaticGzip("dist", {
|
|
|
|
enableBrotli: true,
|
|
|
|
}));
|
|
|
|
|
|
|
|
app.get("*", async (_request, response) => {
|
|
|
|
response.send(this.server.indexHTML);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.options("*", async (_request, response) => {
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
response.end();
|
|
|
|
});
|
|
|
|
|
|
|
|
tempServer = app.listen(port, hostname, () => {
|
|
|
|
log.info("setup-database", `Starting Setup Database on ${port}`);
|
|
|
|
let domain = (hostname) ? hostname : "localhost";
|
|
|
|
log.info("setup-database", `Open http://${domain}:${port} in your browser`);
|
|
|
|
log.info("setup-database", "Waiting for user action...");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
SetupDatabase,
|
|
|
|
};
|