xargana-srv/api/status/status.js

76 lines
2 KiB
JavaScript
Raw Normal View History

2025-03-25 10:00:04 +01:00
const express = require("express");
2025-03-26 17:02:24 +01:00
const ping = require("ping");
2025-03-25 10:00:04 +01:00
2025-03-29 11:35:11 +01:00
const router = express.Router();
2025-03-26 16:48:32 +01:00
const REMOTE_SERVERS = [
2025-03-29 08:45:26 +01:00
{ name: "blahaj.tr", host: "blahaj.tr" },
{ name: "xargana.com", host: "xargana.com" },
{ name: "home server", host: "31.223.36.208" }
2025-03-26 16:48:32 +01:00
];
2025-03-25 10:00:04 +01:00
2025-03-26 16:17:49 +01:00
const CHECK_INTERVAL = 5 * 1000;
2025-03-25 10:00:04 +01:00
2025-03-26 16:48:32 +01:00
let serversStatus = {};
REMOTE_SERVERS.forEach(server => {
2025-03-29 08:45:26 +01:00
serversStatus[server.name] = {
2025-03-26 16:48:32 +01:00
online: false,
lastChecked: null,
responseTime: null,
};
});
2025-03-26 15:59:23 +01:00
2025-03-26 16:48:32 +01:00
async function checkServers() {
2025-04-08 15:42:43 +02:00
try {
for (const server of REMOTE_SERVERS) {
try {
const res = await ping.promise.probe(server.host, {
timeout: 2, // Set a timeout of 2 seconds
});
serversStatus[server.name].online = res.alive;
serversStatus[server.name].responseTime = res.time;
} catch (error) {
console.error(`Error pinging ${server.host}:`, error);
serversStatus[server.name].online = false;
serversStatus[server.name].responseTime = null;
}
serversStatus[server.name].lastChecked = new Date().toISOString();
2025-03-26 16:48:32 +01:00
}
2025-04-08 15:42:43 +02:00
} catch (error) {
console.error("Error in checkServers function:", error);
2025-03-26 15:59:23 +01:00
}
}
2025-04-08 15:42:43 +02:00
// Initial check with error handling
try {
2025-04-08 15:57:47 +02:00
checkServers();
2025-04-08 15:42:43 +02:00
} catch (error) {
console.error("Error during initial check:", error);
}
// Set interval with error handling
setInterval(() => {
try {
2025-04-08 15:57:47 +02:00
checkServers();
2025-04-08 15:42:43 +02:00
} catch (error) {
console.error("Error during scheduled check:", error);
}
}, CHECK_INTERVAL);
2025-03-26 15:59:23 +01:00
2025-04-08 15:42:43 +02:00
// Route with error handling
2025-03-29 11:35:11 +01:00
router.get("/", (req, res) => {
2025-04-08 15:42:43 +02:00
try {
2025-04-08 15:57:47 +02:00
res.json(serversStatus);
2025-04-08 15:42:43 +02:00
} catch (error) {
console.error("Error sending status response:", error);
res.status(500).json({ error: "Internal server error" });
}
});
// Add a simple health check endpoint
router.get("/health", (req, res) => {
res.status(200).send("OK");
2025-03-26 16:04:33 +01:00
});
2025-04-08 15:31:20 +02:00
module.exports = router;