-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.ts
50 lines (43 loc) · 1.4 KB
/
index.ts
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
const axios = require("axios").default;
import { getSystems, updateStatus } from "./helpers/db";
import { getConfig } from "./helpers/config";
import { mail } from "./helpers/mailer";
import { getStatus } from "./helpers/telegram";
import { EntityLike } from "telegram/define";
import * as mongoose from "mongoose";
import * as config from "config";
const uri: string = process.env.DB_URL || config.get("db");
mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
mongoose.set("useFindAndModify", false);
const checkSite = async (url: String) => {
try {
const response = await axios.get(url);
return response.data ? "up" : "down";
} catch (e) {
return "down";
}
};
async function main() {
const systems = await getSystems();
for (let system of systems) {
try {
if (system.type === "web") {
const result = await checkSite(system.url);
await updateStatus(system._id, result);
if (result === "down") {
getConfig() && (await mail(system.name as string));
}
console.log(`Success ${system.name}`);
} else if (system.type === "telegram") {
const result = await getStatus(system.url as EntityLike);
await updateStatus(system._id, result);
if (result === "down") {
getConfig() && (await mail(system.name as string));
}
}
} catch (e) {
console.log(e);
}
}
}
main();