-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
72 lines (61 loc) · 1.85 KB
/
index.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
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
const express = require("express");
const axios = require("axios");
const app = express();
const port = 3000;
app.listen(port, () => {
console.log(`Server listening on port ${port}`);
});
const urlClusters = [
{
name: "Cluster 1",
pingInterval: 300000, // 5 minutes in milliseconds
urls: [
"https://mangamemories.onrender.com",
"https://discord-cards.onrender.com/api/compact/784141856426033233",
],
},
// Add more URL clusters with names and ping intervals here
];
let statusList = {};
const fetchData = async (url) => {
try {
const response = await axios.get(url, { timeout: 5000 }); // 5 seconds timeout
const status = response.status;
return status;
} catch (error) {
console.error(`Error fetching data from ${url}:`, error.message);
return error.message;
}
};
const keepReplAlive = async () => {
statusList = {};
await Promise.all(
urlClusters.map(async (cluster) => {
const clusterName = cluster.name;
const clusterStatus = {};
const promises = cluster.urls.map(async (url) => {
try {
const status = await fetchData(url);
clusterStatus[url] = status;
} catch (error) {
console.error(`Error fetching data from ${url}:`, error.message);
clusterStatus[url] = "Error"; // Handle the error case
}
});
await Promise.race([Promise.all(promises), new Promise(resolve => setTimeout(resolve, cluster.pingInterval))]);
statusList[clusterName] = clusterStatus;
})
);
};
app.get("/", (req, res) => {
res.send("Server is running");
});
app.get("/status", async (req, res) => {
await keepReplAlive(); // Fetch the latest status before responding to /status
console.log(statusList);
res.json(statusList);
});
app.get("/keepalive", (req, res) => {
res.send("Keep-alive is working!");
});
module.exports = app;