2022-05-30 07:45:44 +00:00
|
|
|
let express = require("express");
|
|
|
|
const apicache = require("../modules/apicache");
|
|
|
|
const { UptimeKumaServer } = require("../uptime-kuma-server");
|
|
|
|
const StatusPage = require("../model/status_page");
|
2022-07-19 22:11:15 +00:00
|
|
|
const { allowAllOrigin, allowDevAllOrigin, send403 } = require("../util-server");
|
2022-06-01 05:05:12 +00:00
|
|
|
const { R } = require("redbean-node");
|
|
|
|
const Monitor = require("../model/monitor");
|
2022-05-30 07:45:44 +00:00
|
|
|
|
|
|
|
let router = express.Router();
|
|
|
|
|
|
|
|
let cache = apicache.middleware;
|
|
|
|
const server = UptimeKumaServer.getInstance();
|
|
|
|
|
|
|
|
router.get("/status/:slug", cache("5 minutes"), async (request, response) => {
|
|
|
|
let slug = request.params.slug;
|
|
|
|
await StatusPage.handleStatusPageResponse(response, server.indexHTML, slug);
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get("/status", cache("5 minutes"), async (request, response) => {
|
|
|
|
let slug = "default";
|
|
|
|
await StatusPage.handleStatusPageResponse(response, server.indexHTML, slug);
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get("/status-page", cache("5 minutes"), async (request, response) => {
|
|
|
|
let slug = "default";
|
|
|
|
await StatusPage.handleStatusPageResponse(response, server.indexHTML, slug);
|
|
|
|
});
|
|
|
|
|
2022-07-19 22:11:15 +00:00
|
|
|
// Status page config, incident, monitor list with status ("up" or "down")
|
|
|
|
router.get("/api/status-page/:slug/summary", cache("5 minutes"), async (request, response) => {
|
|
|
|
allowAllOrigin(response);
|
|
|
|
let slug = request.params.slug;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get Status Page
|
|
|
|
let statusPage = await R.findOne("status_page", " slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!statusPage) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let statusPageData = await StatusPage.getStatusPageData(statusPage, true);
|
|
|
|
|
|
|
|
if (!statusPageData) {
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.json({
|
|
|
|
msg: "Not Found"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response
|
|
|
|
response.json(statusPageData);
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
send403(response, error.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-06-01 05:05:12 +00:00
|
|
|
// Status page config, incident, monitor list
|
|
|
|
router.get("/api/status-page/:slug", cache("5 minutes"), async (request, response) => {
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
let slug = request.params.slug;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get Status Page
|
|
|
|
let statusPage = await R.findOne("status_page", " slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!statusPage) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
let statusPageData = await StatusPage.getStatusPageData(statusPage);
|
|
|
|
|
|
|
|
if (!statusPageData) {
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.json({
|
|
|
|
msg: "Not Found"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response
|
|
|
|
response.json(statusPageData);
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
send403(response, error.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Status Page Polling Data
|
|
|
|
// Can fetch only if published
|
|
|
|
router.get("/api/status-page/heartbeat/:slug", cache("1 minutes"), async (request, response) => {
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
|
|
|
|
try {
|
|
|
|
let heartbeatList = {};
|
|
|
|
let uptimeList = {};
|
|
|
|
|
|
|
|
let slug = request.params.slug;
|
|
|
|
let statusPageID = await StatusPage.slugToID(slug);
|
|
|
|
|
|
|
|
let monitorIDList = await R.getCol(`
|
|
|
|
SELECT monitor_group.monitor_id FROM monitor_group, \`group\`
|
|
|
|
WHERE monitor_group.group_id = \`group\`.id
|
|
|
|
AND public = 1
|
|
|
|
AND \`group\`.status_page_id = ?
|
|
|
|
`, [
|
|
|
|
statusPageID
|
|
|
|
]);
|
|
|
|
|
|
|
|
for (let monitorID of monitorIDList) {
|
|
|
|
let list = await R.getAll(`
|
|
|
|
SELECT * FROM heartbeat
|
|
|
|
WHERE monitor_id = ?
|
|
|
|
ORDER BY time DESC
|
|
|
|
LIMIT 50
|
|
|
|
`, [
|
|
|
|
monitorID,
|
|
|
|
]);
|
|
|
|
|
|
|
|
list = R.convertToBeans("heartbeat", list);
|
|
|
|
heartbeatList[monitorID] = list.reverse().map(row => row.toPublicJSON());
|
|
|
|
|
|
|
|
const type = 24;
|
|
|
|
uptimeList[`${monitorID}_${type}`] = await Monitor.calcUptime(type, monitorID);
|
|
|
|
}
|
|
|
|
|
|
|
|
response.json({
|
|
|
|
heartbeatList,
|
|
|
|
uptimeList
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
send403(response, error.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-07-04 13:58:27 +00:00
|
|
|
// Status page's manifest.json
|
|
|
|
router.get("/api/status-page/:slug/manifest.json", cache("1440 minutes"), async (request, response) => {
|
|
|
|
allowDevAllOrigin(response);
|
|
|
|
let slug = request.params.slug;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get Status Page
|
|
|
|
let statusPage = await R.findOne("status_page", " slug = ? ", [
|
|
|
|
slug
|
|
|
|
]);
|
|
|
|
|
|
|
|
if (!statusPage) {
|
|
|
|
response.statusCode = 404;
|
|
|
|
response.json({
|
|
|
|
msg: "Not Found"
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Response
|
|
|
|
response.json({
|
|
|
|
"name": statusPage.title,
|
|
|
|
"start_url": "/status/" + statusPage.slug,
|
|
|
|
"display": "standalone",
|
|
|
|
"icons": [
|
|
|
|
{
|
|
|
|
"src": statusPage.icon,
|
|
|
|
"sizes": "128x128",
|
|
|
|
"type": "image/png"
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
|
|
|
} catch (error) {
|
|
|
|
send403(response, error.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-05-30 07:45:44 +00:00
|
|
|
module.exports = router;
|