-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
97 lines (88 loc) · 2.2 KB
/
Copy pathserver.js
File metadata and controls
97 lines (88 loc) · 2.2 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import express from "express";
import {
getData,
RemoveOnline,
AddOnline,
ForceUpdateData,
} from "./HetrixClient";
import { config } from "dotenv";
import path from "path";
config();
const key = process.env.HETRIX_AUTH;
const expectedAuth = `Bearer ${key}`;
export function runServer() {
const app = express();
app.use(express.json());
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
const port = process.env.PORT || 3000;
const key = process.env.ADMIN_KEY;
const border = process.env.BORDER_COLOR || "#fff";
const bgColor = process.env.BACKGROUND_COLOR || "#000";
const textColor = process.env.TEXT_COLOR || "#fff";
app.get("/", async (req, res) => {
const json = await getData();
const total = json.TotalMonitors;
const down_monit = json.DownMonitors;
if (down_monit > 0) {
res.render("iframe", {
dotColor: "#ffff00",
customText: "Partial outage",
borderColor: border,
bgColor: bgColor,
textColor: textColor,
});
}
if (down_monit == 0) {
res.render("iframe", {
dotColor: "#00ff00",
customText: "All systems online",
borderColor: border,
bgColor: bgColor,
textColor: textColor,
});
}
if (down_monit == total) {
res.render("iframe", {
dotColor: "#ff0000",
customText: "Major outage",
borderColor: border,
bgColor: bgColor,
textColor: textColor,
});
}
});
app.get("/raw", async (req, res) => {
const json = await getData();
res.json(json);
});
app.get("/update", async (req, res) => {
const keyEntry = req.query.key;
if (keyEntry !== key) {
res.status(401).send("Unauthorized");
return;
}
console.log("Remote update request received!");
ForceUpdateData();
res.send("Data Resynced!");
});
app.post("/ingest", (req, res) => {
const json = req.body;
const auth = req.headers.authorization;
console.log("Data received!");
if (auth !== expectedAuth) {
console.error("Unauthorized request");
res.status(401).send("Unauthorized");
return;
}
if (json.monitor_status === "offline") {
RemoveOnline();
}
if (json.monitor_status === "online") {
AddOnline();
}
});
app.listen(port, () => {
console.log(`Server is running on port ${port}`);
});
}