From c4e74c99430a19a4f1e36f7af9e6bd908548573c Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Wed, 6 Apr 2022 22:43:22 +0800 Subject: [PATCH] Render if domain matched --- server/model/status_page.js | 27 +++++++++++++++++++++++++++ server/routers/api-router.js | 14 ++++++++++++-- server/server.js | 10 ++++++++-- src/icon.js | 2 ++ src/pages/Entry.vue | 33 +++++++++++++++++++++++++++------ src/pages/StatusPage.vue | 21 +++++++++++++++------ 6 files changed, 91 insertions(+), 16 deletions(-) diff --git a/server/model/status_page.js b/server/model/status_page.js index 6f763f586..7f03fe6d7 100644 --- a/server/model/status_page.js +++ b/server/model/status_page.js @@ -3,6 +3,20 @@ const { R } = require("redbean-node"); class StatusPage extends BeanModel { + static domainMappingList = { }; + + /** + * Return object like this: { "test-uptime.kuma.pet": "default" } + * @returns {Promise} + */ + static async loadDomainMappingList() { + this.domainMappingList = await R.getAssoc(` + SELECT domain, slug + FROM status_page, status_page_cname + WHERE status_page.id = status_page_cname.status_page_id + `); + } + static async sendStatusPageList(io, socket) { let result = {}; @@ -16,6 +30,18 @@ class StatusPage extends BeanModel { return list; } + getDomainList() { + let domainList = []; + for (let domain in StatusPage.domainMappingList) { + let s = StatusPage.domainMappingList[domain]; + + if (this.slug === s) { + domainList.push(domain); + } + } + return domainList; + } + async toJSON() { return { id: this.id, @@ -26,6 +52,7 @@ class StatusPage extends BeanModel { theme: this.theme, published: !!this.published, showTags: !!this.show_tags, + domainList: this.getDomainList(), }; } diff --git a/server/routers/api-router.js b/server/routers/api-router.js index ad8870847..6f463b6b0 100644 --- a/server/routers/api-router.js +++ b/server/routers/api-router.js @@ -12,9 +12,19 @@ let router = express.Router(); let cache = apicache.middleware; let io = server.io; -router.get("/api/entry-page", async (_, response) => { +router.get("/api/entry-page", async (request, response) => { allowDevAllOrigin(response); - response.json(server.entryPage); + + let result = { }; + + if (request.hostname in StatusPage.domainMappingList) { + result.type = "statusPageMatchedDomain"; + result.statusPageSlug = StatusPage.domainMappingList[request.hostname]; + } else { + result.type = "entryPage"; + result.entryPage = server.entryPage; + } + response.json(result); }); router.get("/api/push/:pushToken", async (request, response) => { diff --git a/server/server.js b/server/server.js index dc97d742d..3bd724d2c 100644 --- a/server/server.js +++ b/server/server.js @@ -197,6 +197,7 @@ exports.entryPage = "dashboard"; await initDatabase(testMode); exports.entryPage = await setting("entryPage"); + await StatusPage.loadDomainMappingList(); console.log("Adding route"); @@ -205,8 +206,13 @@ exports.entryPage = "dashboard"; // *************************** // Entry Page - app.get("/", async (_request, response) => { - if (exports.entryPage && exports.entryPage.startsWith("statusPage-")) { + app.get("/", async (request, response) => { + debug(`Request Domain: ${request.hostname}`); + + if (request.hostname in StatusPage.domainMappingList) { + debug("This is a status page domain"); + response.send(indexHTML); + } else if (exports.entryPage && exports.entryPage.startsWith("statusPage-")) { response.redirect("/status/" + exports.entryPage.replace("statusPage-", "")); } else { response.redirect("/dashboard"); diff --git a/src/icon.js b/src/icon.js index bbd816ea0..c03db7555 100644 --- a/src/icon.js +++ b/src/icon.js @@ -37,6 +37,7 @@ import { faPen, faExternalLinkSquareAlt, faSpinner, + faUndo, } from "@fortawesome/free-solid-svg-icons"; library.add( @@ -73,6 +74,7 @@ library.add( faPen, faExternalLinkSquareAlt, faSpinner, + faUndo, ); export { FontAwesomeIcon }; diff --git a/src/pages/Entry.vue b/src/pages/Entry.vue index 90f039162..fd24a3520 100644 --- a/src/pages/Entry.vue +++ b/src/pages/Entry.vue @@ -1,23 +1,44 @@