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
import { email } from "https://esm.town/v/std/email";
export const isMyWebsiteDown = async () => {
const urls = ["https://docs.73zls.com/zlsgo/#", "https://api.73zls.com/notion/d31996e1b5474530b8aa23c429942aa0.md"];
for (const url of urls) {
website(url);
}
};
async function website(url) {
const [date, time] = new Date().toISOString().split("T");
let ok = true;
let reason: string;
try {
const res = await fetch(url, { redirect: "follow" });
if (res.status !== 200) {
reason = `(status code: ${res.status})`;
ok = false;
}
} catch (e) {
reason = `couldn't fetch: ${e}`;
ok = false;
}
if (!ok) {
const subject = `Website down (${url})`;
const text = `At ${date} ${time} (UTC), ${url} was down (reason: ${reason}).`;
console.log(subject, text);
await email({ subject, text });
} else {
try {
url = URL.parse(url).hostname;
} catch (e) {}
console.log("Website ok: " + url);
}
}
Val Town is a social website to write and deploy JavaScript.
Build APIs and schedule functions from your browser.
Comments
Nobody has commented on this val yet: be the first!
July 22, 2024