xargana-srv/status/server.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2025-03-25 10:00:04 +01:00
const express = require("express");
const axios = require("axios");
2025-03-25 10:33:35 +01:00
const cors = require("cors");
2025-03-25 10:00:04 +01:00
const app = express();
2025-03-25 10:33:35 +01:00
const PORT = 2589; // Set the API to run on port 2589
2025-03-25 10:00:04 +01:00
2025-03-25 10:33:35 +01:00
const CHECK_INTERVAL = 30 * 1000; // Check every 30 seconds
2025-03-26 16:02:57 +01:00
const REMOTE_SERVER = "https://blahaj.tr"; // Change this to the server you want to check
2025-03-25 10:00:04 +01:00
let serverStatus = {
online: false,
2025-03-26 15:59:23 +01:00
lastChecked: null,
responseTime: null,
};
// Enable CORS to allow requests from other domains
app.use(cors());
// Function to check server status
async function checkServer() {
const startTime = Date.now();
try {
await axios.get(REMOTE_SERVER, { timeout: 5000 }); // 5-second timeout
serverStatus.online = true;
} catch (error) {
serverStatus.online = false;
}
serverStatus.responseTime = Date.now() - startTime;
serverStatus.lastChecked = new Date().toISOString();
}
// Run the check every 30 seconds
setInterval(checkServer, CHECK_INTERVAL);
checkServer(); // Initial check
// API route to get the server status
2025-03-26 16:02:57 +01:00
app.get("/", (req, res) => {
2025-03-26 15:59:23 +01:00
res.json(serverStatus);
});
// Start the server on port 2589
app.listen(PORT, () => {
console.log(`API running at https://localhost:${PORT}`);
});