[Status Page] SSR

This commit is contained in:
Louis Lam 2022-05-30 15:45:44 +08:00
parent ffb7ba176c
commit c095767f4a
6 changed files with 92 additions and 29 deletions

View file

@ -1,11 +1,34 @@
const { BeanModel } = require("redbean-node/dist/bean-model");
const { R } = require("redbean-node");
const cheerio = require("cheerio");
const { UptimeKumaServer } = require("../uptime-kuma-server");
class StatusPage extends BeanModel {
/**
* Like this: { "test-uptime.kuma.pet": "default" }
* @type {{}}
*/
static domainMappingList = { };
/**
*
* @param {Response} response
* @param {string} indexHTML
* @param {string} slug
*/
static async handleStatusPageResponse(response, indexHTML, slug) {
let statusPage = await R.findOne("status_page", " slug = ? ", [
slug
]);
if (statusPage) {
response.send(StatusPage.renderHTML(indexHTML, statusPage));
} else {
response.status(404).send(UptimeKumaServer.getInstance().indexHTML);
}
}
/**
* SSR for status pages
* @param {string} indexHTML
@ -14,6 +37,17 @@ class StatusPage extends BeanModel {
static renderHTML(indexHTML, statusPage) {
const $ = cheerio.load(indexHTML);
$("title").text(statusPage.title);
$("meta[name=description]").attr("content", statusPage.description.substring(0, 155));
if (statusPage.icon) {
$("link[rel=icon]")
.attr("href", statusPage.icon)
.removeAttr("type");
}
const head = $("head");
return $.root().html();
}