api-jer / index.js
jerdev38282's picture
Update index.js
3278e26 verified
const express = require("express");
const axios = require("axios");
const fs = require("fs");
const path = require("path");
const app = express();
const PORT = process.env.PORT || 7860;
const startTime = Date.now();
// Load uptime targets
const configPath = path.join(__dirname, "up.json");
const config = JSON.parse(fs.readFileSync(configPath, "utf8"));
let stats = {};
// Init stats
config.targets.forEach(t => {
stats[t.name] = {
url: t.url,
success: 0,
fail: 0,
lastPing: null,
lastStatus: "pending"
};
});
// Serve static HTML
app.use(express.static(path.join(__dirname, "public")));
// πŸ” UPTIME PINGER
async function pingTargets() {
for (const target of config.targets) {
try {
await axios.get(target.url, { timeout: 8000 });
stats[target.name].success++;
stats[target.name].lastStatus = "online";
} catch (err) {
stats[target.name].fail++;
stats[target.name].lastStatus = "offline";
}
stats[target.name].lastPing = new Date().toLocaleString();
}
}
// Run pinger
setInterval(pingTargets, config.interval_seconds * 1000);
pingTargets();
// 🧠 STATUS API
app.get("/status", (req, res) => {
const uptimeSeconds = Math.floor((Date.now() - startTime) / 1000);
res.json({
status: "running",
uptimeSeconds,
serverTime: new Date().toLocaleString(),
targets: stats
});
});
// Root
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname, "public", "index.html"));
});
// Start server
app.listen(PORT, () => {
console.log(`πŸš€ Uptime Monitor running on http://localhost:${PORT}`);
});