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

130 lines
2.9 KiB
JavaScript
Raw Normal View History

2021-09-14 02:55:45 -04:00
let express = require("express");
2021-09-15 02:34:30 -04:00
const { allowDevAllOrigin, getSettings, setting } = require("../util-server");
2021-09-14 02:55:45 -04:00
const { R } = require("redbean-node");
2021-09-15 08:40:26 -04:00
const server = require("../server");
2021-09-14 02:55:45 -04:00
let router = express.Router();
2021-09-15 08:40:26 -04:00
router.get("/api/entry-page", async (_, response) => {
allowDevAllOrigin(response);
response.json(server.entryPage);
});
2021-09-14 02:55:45 -04:00
// Status Page Config
router.get("/api/status-page/config", async (_request, response) => {
allowDevAllOrigin(response);
2021-09-15 02:34:30 -04:00
2021-09-14 02:55:45 -04:00
let config = getSettings("statusPage");
if (! config.statusPageTheme) {
config.statusPageTheme = "light";
}
2021-09-15 02:34:30 -04:00
if (! config.statusPagePublished) {
config.statusPagePublished = true;
}
2021-09-14 11:27:11 -04:00
if (! config.title) {
config.title = "Uptime Kuma";
}
2021-09-14 02:55:45 -04:00
response.json(config);
});
2021-09-15 02:34:30 -04: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 02:55:45 -04:00
// Status Page - Monitor List
2021-09-15 02:34:30 -04:00
// Can fetch only if published
2021-09-14 02:55:45 -04:00
router.get("/api/status-page/monitor-list", async (_request, response) => {
allowDevAllOrigin(response);
2021-09-15 02:34:30 -04:00
try {
await checkPublished();
const monitorList = {};
let list = await R.find("monitor", " public = 1 ORDER BY weight DESC, name ", [
]);
2021-09-14 02:55:45 -04:00
2021-09-15 02:34:30 -04:00
for (let monitor of list) {
monitorList[monitor.id] = await monitor.toJSON();
}
response.json(monitorList);
2021-09-14 02:55:45 -04:00
2021-09-15 02:34:30 -04:00
} catch (error) {
send403(response, error.message);
}
2021-09-14 02:55:45 -04:00
});
// Status Page Polling Data
2021-09-15 02:34:30 -04:00
// Can fetch only if published
2021-09-14 02:55:45 -04:00
router.get("/api/status-page/heartbeat", async (_request, response) => {
allowDevAllOrigin(response);
2021-09-15 02:34:30 -04:00
try {
await checkPublished();
2021-09-14 02:55:45 -04:00
2021-09-15 02:34:30 -04:00
const monitorList = {};
let list = await R.find("", " ", [
])
2021-09-14 02:55:45 -04:00
2021-09-15 02:34:30 -04:00
for (let monitor of list) {
monitorList[monitor.id] = await monitor.toJSON();
}
response.json({
monitorList: monitorList,
});
2021-09-14 02:55:45 -04:00
2021-09-15 02:34:30 -04:00
} catch (error) {
send403(response, error.message);
}
2021-09-14 02:55:45 -04:00
});
2021-09-15 06:28:48 -04: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 02:34:30 -04:00
async function checkPublished() {
if (! await isPublished()) {
throw new Error("The status page is not published");
}
}
2021-09-15 06:28:48 -04:00
/**
* Default is published
* @returns {Promise<boolean>}
*/
2021-09-15 02:34:30 -04:00
async function isPublished() {
2021-09-15 06:28:48 -04:00
const value = await setting("statusPagePublished");
if (value === null) {
return true;
}
return value;
2021-09-15 02:34:30 -04:00
}
function send403(res, msg = "") {
res.status(403).json({
"status": "fail",
"msg": msg,
})
}
2021-09-14 02:55:45 -04:00
module.exports = router;