forked from Deen-Bridge/dnb-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjobsRoutes.js
More file actions
38 lines (33 loc) · 1.37 KB
/
Copy pathjobsRoutes.js
File metadata and controls
38 lines (33 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import express from "express";
import Job from "../models/Job.js";
const router = express.Router();
const escapeHtml = (value) =>
String(value)
.replaceAll("&", "&")
.replaceAll("<", "<")
.replaceAll(">", ">")
.replaceAll('"', """)
.replaceAll("'", "'");
router.use((req, res, next) => {
const token = process.env.JOBS_DASHBOARD_TOKEN;
if (!token) return res.status(404).json({ success: false, message: "Not found" });
if (req.headers.authorization !== `Bearer ${token}`) {
return res.status(401).json({ success: false, message: "Unauthorized" });
}
next();
});
router.get("/", async (req, res) => {
const jobs = await Job.find().sort({ createdAt: -1 }).limit(100).lean();
if (req.accepts(["html", "json"]) === "html") {
const rows = jobs
.map((job) => `<tr><td>${escapeHtml(job.name)}</td><td>${escapeHtml(job.status)}</td><td>${job.attemptsMade}/${job.maxAttempts}</td><td>${escapeHtml(job.lastError || "")}</td></tr>`)
.join("");
return res.type("html").send(`<h1>DeenBridge Jobs</h1><table><tr><th>Name</th><th>Status</th><th>Attempts</th><th>Error</th></tr>${rows}</table>`);
}
res.json({ success: true, jobs });
});
router.get("/dead", async (req, res) => {
const jobs = await Job.find({ status: "dead" }).sort({ failedAt: -1 }).lean();
res.json({ success: true, jobs });
});
export default router;