From 3e25f0e9d9470ef26219a48549704092109f4fd0 Mon Sep 17 00:00:00 2001 From: LouisLam Date: Sat, 11 Sep 2021 19:40:03 +0800 Subject: [PATCH] [Status Page] WIP: Checkpoint --- server/server.js | 29 ++++++++++-- server/util-server.js | 12 +++++ src/icon.js | 4 +- src/layouts/Layout.vue | 11 +++++ src/mixins/socket.js | 8 ++-- src/mixins/theme.js | 16 +++++-- src/pages/StatusPage.vue | 97 +++++++++++++++++++++++++++++++++++++--- 7 files changed, 159 insertions(+), 18 deletions(-) diff --git a/server/server.js b/server/server.js index 2949c4be7..aec4528da 100644 --- a/server/server.js +++ b/server/server.js @@ -26,7 +26,7 @@ console.log("Importing this project modules"); debug("Importing Monitor"); const Monitor = require("./model/monitor"); debug("Importing Settings"); -const { getSettings, setSettings, setting, initJWTSecret } = require("./util-server"); +const { getSettings, setSettings, setting, initJWTSecret, allowDevAllOrigin } = require("./util-server"); debug("Importing Notification"); const { Notification } = require("./notification"); @@ -127,7 +127,9 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); console.log("Adding route") + // *************************** // Normal Router here + // *************************** // Robots.txt app.get("/robots.txt", async (_request, response) => { @@ -147,7 +149,28 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); app.use("/", express.static("dist")); - // Universal Route Handler, must be at the end + // *************************** + // Public API + // *************************** + + // Status Page Config + app.get("/api/status-page/config", async (_request, response) => { + allowDevAllOrigin(response); + let config = getSettings("statusPage"); + + if (! config.statusPageTheme) { + config.statusPageTheme = "light"; + } + + response.json(config); + }); + + // Status Page Polling Data + app.get("/api/status-page", async (_request, response) => { + allowDevAllOrigin(response); + }); + + // Universal Route Handler, must be at the end of all express route. app.get("*", async (_request, response) => { response.send(indexHTML); }); @@ -172,7 +195,7 @@ let indexHTML = fs.readFileSync("./dist/index.html").toString(); }); // *************************** - // Public API + // Public Socket API // *************************** socket.on("loginByToken", async (token, callback) => { diff --git a/server/util-server.js b/server/util-server.js index a2fef0656..67bf13020 100644 --- a/server/util-server.js +++ b/server/util-server.js @@ -5,6 +5,7 @@ const { debug } = require("../src/util"); const passwordHash = require("./password-hash"); const dayjs = require("dayjs"); const { Resolver } = require("dns"); +const { allowAllOrigin } = require("./util-server"); /** * Init or reset JWT secret @@ -271,3 +272,14 @@ exports.getTotalClientInRoom = (io, roomName) => { return 0; } } + +exports.allowDevAllOrigin = (res) => { + if (process.env.NODE_ENV === "development") { + exports.allowAllOrigin(res); + } +} + +exports.allowAllOrigin = (res) => { + res.header("Access-Control-Allow-Origin", "*"); + res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); +} diff --git a/src/icon.js b/src/icon.js index 58583f0f8..7e7c7dd67 100644 --- a/src/icon.js +++ b/src/icon.js @@ -1,10 +1,10 @@ import { library } from "@fortawesome/fontawesome-svg-core" -import { faCog, faEdit, faPlus, faPause, faPlay, faTachometerAlt, faTrash, faList, faArrowAltCircleUp, faEye, faEyeSlash } from "@fortawesome/free-solid-svg-icons" +import { faCog, faEdit, faPlus, faPause, faPlay, faTachometerAlt, faTrash, faList, faArrowAltCircleUp, faEye, faEyeSlash, faCheckCircle, faStream } from "@fortawesome/free-solid-svg-icons" //import { fa } from '@fortawesome/free-regular-svg-icons' import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome" // Add Free Font Awesome Icons here // https://fontawesome.com/v5.15/icons?d=gallery&p=2&s=solid&m=free -library.add(faCog, faEdit, faPlus, faPause, faPlay, faTachometerAlt, faTrash, faList, faArrowAltCircleUp, faEye, faEyeSlash); +library.add(faCog, faEdit, faPlus, faPause, faPlay, faTachometerAlt, faTrash, faList, faArrowAltCircleUp, faEye, faEyeSlash, faCheckCircle, faStream); export { FontAwesomeIcon } diff --git a/src/layouts/Layout.vue b/src/layouts/Layout.vue index 467ae53a2..b7b8688b3 100644 --- a/src/layouts/Layout.vue +++ b/src/layouts/Layout.vue @@ -18,6 +18,11 @@