uptime-kuma/server/routers/api-router.js

124 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-09-14 09:55:45 +03:00
let express = require("express");
2021-09-15 09:34:30 +03:00
const { allowDevAllOrigin, getSettings, setting } = require("../util-server");
2021-09-14 09:55:45 +03:00
const { R } = require("redbean-node");
let router = express.Router();
// Status Page Config
router.get("/api/status-page/config", async (_request, response) => {
allowDevAllOrigin(response);
2021-09-15 09:34:30 +03:00
2021-09-14 09:55:45 +03:00
let config = getSettings("statusPage");
if (! config.statusPageTheme) {
config.statusPageTheme = "light";
}
2021-09-15 09:34:30 +03:00
if (! config.statusPagePublished) {
config.statusPagePublished = true;
}
2021-09-14 18:27:11 +03:00
if (! config.title) {
config.title = "Uptime Kuma";
}
2021-09-14 09:55:45 +03:00
response.json(config);
});
2021-09-15 09:34:30 +03:00
// Status Page - Get the current Incident
// Can fetch only if published
router.get("/api/status-page/incident", async (_, response) => {
allowDevAllOrigin(response);
try {
await checkPublished();
// TODO:
} catch (error) {
send403(response, error.message);
}
});
2021-09-14 09:55:45 +03:00
// Status Page - Monitor List
2021-09-15 09:34:30 +03:00
// Can fetch only if published
2021-09-14 09:55:45 +03:00
router.get("/api/status-page/monitor-list", async (_request, response) => {
allowDevAllOrigin(response);
2021-09-15 09:34:30 +03:00
try {
await checkPublished();
const monitorList = {};
let list = await R.find("monitor", " public = 1 ORDER BY weight DESC, name ", [
]);
2021-09-14 09:55:45 +03:00
2021-09-15 09:34:30 +03:00
for (let monitor of list) {
monitorList[monitor.id] = await monitor.toJSON();
}
response.json(monitorList);
2021-09-14 09:55:45 +03:00
2021-09-15 09:34:30 +03:00
} catch (error) {
send403(response, error.message);
}
2021-09-14 09:55:45 +03:00
});
// Status Page Polling Data
2021-09-15 09:34:30 +03:00
// Can fetch only if published
2021-09-14 09:55:45 +03:00
router.get("/api/status-page/heartbeat", async (_request, response) => {
allowDevAllOrigin(response);
2021-09-15 09:34:30 +03:00
try {
await checkPublished();
2021-09-14 09:55:45 +03:00
2021-09-15 09:34:30 +03:00
const monitorList = {};
let list = await R.find("", " ", [
])
2021-09-14 09:55:45 +03:00
2021-09-15 09:34:30 +03:00
for (let monitor of list) {
monitorList[monitor.id] = await monitor.toJSON();
}
response.json({
monitorList: monitorList,
});
2021-09-14 09:55:45 +03:00
2021-09-15 09:34:30 +03:00
} catch (error) {
send403(response, error.message);
}
2021-09-14 09:55:45 +03:00
});
2021-09-15 13:28:48 +03:00
router.post("/api/status-page/upload-logo", async (request, response) => {
allowDevAllOrigin(response);
// TODO: Check Bearer token
console.log(request);
response.json({});
});
2021-09-15 09:34:30 +03:00
async function checkPublished() {
if (! await isPublished()) {
throw new Error("The status page is not published");
}
}
2021-09-15 13:28:48 +03:00
/**
* Default is published
* @returns {Promise<boolean>}
*/
2021-09-15 09:34:30 +03:00
async function isPublished() {
2021-09-15 13:28:48 +03:00
const value = await setting("statusPagePublished");
if (value === null) {
return true;
}
return value;
2021-09-15 09:34:30 +03:00
}
function send403(res, msg = "") {
res.status(403).json({
"status": "fail",
"msg": msg,
})
}
2021-09-14 09:55:45 +03:00
module.exports = router;