-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthcheck-worker.js
44 lines (44 loc) · 1.25 KB
/
healthcheck-worker.js
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
// Nubango Health Checker
// Checks to see if Apache is still working.
addEventListener("scheduled", (event) => {
event.waitUntil(
check(true).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
addEventListener("fetch", (event) => {
event.respondWith(
check(false).catch(
(err) => new Response(err.stack, { status: 500 })
)
);
});
const smsURL = "https://textbelt.com/text";
async function alert(statusCode) {
await fetch(smsURL, {
method: "post",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
phone: smsNumber,
message: "⚠️⚠️⚠️ ALERT: Nubango HTTP Healthcheck FAILED! Status code: " + statusCode,
key: smsKey
})
});
}
async function check(sendSMS) {
const srvResponse = await fetch(serverURL, { headers: { "User-Agent": "CFHealthCheckWorker/iTunes" } });
const hcBody = (await srvResponse.text()).trim();
const hcStatus = srvResponse.status.toString();
if (sendSMS) {
if (hcStatus !== "200" || hcBody !== "OK") await alert(hcStatus);
return;
}
const srvStatus = JSON.stringify({
result: hcBody === "OK" ? "OK" : "ERROR",
status: hcStatus
});
return new Response(srvStatus, {
headers: { "Content-Type": "text/json" },
});
}