a
This commit is contained in:
parent
4190bf8059
commit
c3f308af14
|
@ -1,12 +1,15 @@
|
||||||
const express = require("express");
|
const express = require("express");
|
||||||
const axios = require("axios");
|
const axios = require("axios");
|
||||||
const cors = require("cors");
|
const cors = require("cors");
|
||||||
|
const fs = require("fs");
|
||||||
|
const https = require("https");
|
||||||
|
const path = require("path");
|
||||||
|
|
||||||
const app = express();
|
const app = express();
|
||||||
const PORT = 2589; // Set the API to run on port 2589
|
const PORT = 2589;
|
||||||
|
const REMOTE_SERVER = "https://example.com"; // Change this
|
||||||
|
|
||||||
const CHECK_INTERVAL = 30 * 1000; // Check every 30 seconds
|
const CHECK_INTERVAL = 30 * 1000;
|
||||||
const REMOTE_SERVER = "https://blahaj.tr"; // Change this to the server you want to check
|
|
||||||
|
|
||||||
let serverStatus = {
|
let serverStatus = {
|
||||||
online: false,
|
online: false,
|
||||||
|
@ -14,14 +17,12 @@ let serverStatus = {
|
||||||
responseTime: null,
|
responseTime: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
// Enable CORS to allow requests from other domains
|
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
// Function to check server status
|
|
||||||
async function checkServer() {
|
async function checkServer() {
|
||||||
const startTime = Date.now();
|
const startTime = Date.now();
|
||||||
try {
|
try {
|
||||||
await axios.get(REMOTE_SERVER, { timeout: 5000 }); // 5-second timeout
|
await axios.get(REMOTE_SERVER, { timeout: 5000 });
|
||||||
serverStatus.online = true;
|
serverStatus.online = true;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
serverStatus.online = false;
|
serverStatus.online = false;
|
||||||
|
@ -30,16 +31,24 @@ async function checkServer() {
|
||||||
serverStatus.lastChecked = new Date().toISOString();
|
serverStatus.lastChecked = new Date().toISOString();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the check every 30 seconds
|
|
||||||
setInterval(checkServer, CHECK_INTERVAL);
|
setInterval(checkServer, CHECK_INTERVAL);
|
||||||
checkServer(); // Initial check
|
checkServer();
|
||||||
|
|
||||||
// API route to get the server status
|
|
||||||
app.get("/", (req, res) => {
|
app.get("/", (req, res) => {
|
||||||
res.json(serverStatus);
|
res.json(serverStatus);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Start the server on port 2589
|
app.get("/", (req, res) => {
|
||||||
app.listen(PORT, () => {
|
res.send("<h1>Status API</h1><p>Use <code>/status</code> to check server status.</p>");
|
||||||
console.log(`API running at https://localhost:${PORT}`);
|
});
|
||||||
|
|
||||||
|
// Load SSL Certificates
|
||||||
|
const sslOptions = {
|
||||||
|
key: fs.readFileSync("/etc/letsencrypt/live/blahaj.tr/privkey.pem"),
|
||||||
|
cert: fs.readFileSync("/etc/letsencrypt/live/blahaj.tr/fullchain.pem"),
|
||||||
|
};
|
||||||
|
|
||||||
|
// Start HTTPS Server
|
||||||
|
https.createServer(sslOptions, app).listen(PORT, () => {
|
||||||
|
console.log(`API running at https://blahaj.tr:${PORT}`);
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue