diff --git a/server/server.js b/server/server.js index 5cb810f9c..967db53a7 100644 --- a/server/server.js +++ b/server/server.js @@ -1,25 +1,45 @@ -console.log("Welcome to Uptime Kuma ") -console.log("Importing libraries") -const express = require("express"); -const http = require("http"); -const { Server } = require("socket.io"); -const dayjs = require("dayjs"); -const { R } = require("redbean-node"); -const jwt = require("jsonwebtoken"); -const Monitor = require("./model/monitor"); +console.log("Welcome to Uptime Kuma") + +const { sleep, debug } = require("../src/util"); + +console.log("Importing Node libraries") const fs = require("fs"); -const { getSettings } = require("./util-server"); -const { Notification } = require("./notification") +const http = require("http"); + +console.log("Importing 3rd-party libraries") +debug("Importing express"); +const express = require("express"); +debug("Importing socket.io"); +const { Server } = require("socket.io"); +debug("Importing dayjs"); +const dayjs = require("dayjs"); +debug("Importing redbean-node"); +const { R } = require("redbean-node"); +debug("Importing jsonwebtoken"); +const jwt = require("jsonwebtoken"); +debug("Importing http-graceful-shutdown"); const gracefulShutdown = require("http-graceful-shutdown"); -const Database = require("./database"); -const { sleep } = require("../src/util"); -const args = require("args-parser")(process.argv); +debug("Importing prometheus-api-metrics"); const prometheusAPIMetrics = require("prometheus-api-metrics"); + +console.log("Importing this project modules"); +debug("Importing Monitor"); +const Monitor = require("./model/monitor"); +debug("Importing Settings"); +const { getSettings, setSettings } = require("./util-server"); +debug("Importing Notification"); +const { Notification } = require("./notification"); +debug("Importing Database"); +const Database = require("./database"); + const { basicAuth } = require("./auth"); const { login } = require("./auth"); const passwordHash = require("./password-hash"); + +const args = require("args-parser")(process.argv); + const version = require("../package.json").version; -const hostname = args.host || "0.0.0.0" +const hostname = process.env.HOST || args.host || "0.0.0.0" const port = parseInt(process.env.PORT || args.port || 3001); console.info("Version: " + version) @@ -405,13 +425,32 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); } }); - socket.on("getSettings", async (type, callback) => { + socket.on("getSettings", async (callback) => { try { checkLogin(socket) callback({ ok: true, - data: await getSettings(type), + data: await getSettings("general"), + }); + + } catch (e) { + callback({ + ok: false, + msg: e.message, + }); + } + }); + + socket.on("setSettings", async (data, callback) => { + try { + checkLogin(socket) + + await setSettings("general", data) + + callback({ + ok: true, + msg: "Saved" }); } catch (e) { diff --git a/server/util-server.js b/server/util-server.js index 43aa5ccfb..d892c1069 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -58,19 +58,44 @@ exports.setSetting = async function (key, value) { } exports.getSettings = async function (type) { - let list = await R.getAll("SELECT * FROM setting WHERE `type` = ? ", [ + let list = await R.getAll("SELECT `key`, `value` FROM setting WHERE `type` = ? ", [ type, ]) let result = {}; for (let row of list) { - result[row.key] = row.value; + result[row.key] = JSON.parse(row.value); } return result; } +exports.setSettings = async function (type, data) { + let keyList = Object.keys(data); + + let promiseList = []; + + for (let key of keyList) { + let bean = await R.findOne("setting", " `key` = ? ", [ + key + ]); + + if (bean == null) { + bean = R.dispense("setting"); + bean.type = type; + bean.key = key; + } + + if (bean.type === type) { + bean.value = JSON.stringify(data[key]); + promiseList.push(R.store(bean)) + } + } + + await Promise.all(promiseList); +} + // ssl-checker by @dyaa // param: res - response object from axios // return an object containing the certificate information diff --git a/src/pages/Settings.vue b/src/pages/Settings.vue index d1d3599ba..d11479162 100644 --- a/src/pages/Settings.vue +++ b/src/pages/Settings.vue @@ -54,10 +54,12 @@ -
Are you sure want to disable auth?
+It is for someone who have 3rd-party auth in front of Uptime Kuma such as Cloudflare Access.
+Please use it carefully.
+