This repository was archived by the owner on Feb 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (66 loc) · 2.13 KB
/
index.js
File metadata and controls
75 lines (66 loc) · 2.13 KB
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
73
74
75
require('dotenv').config();
const express = require('express');
const fetch = require('node-fetch');
const app = express();
const port = process.env.PORT || 3000;
const websites = (process.env.WEBSITES || '').split(',').filter(Boolean);
const checkInterval = parseInt(process.env.CHECK_INTERVAL) || 60000;
const uiRefreshInterval = parseInt(process.env.UI_REFRESH_INTERVAL) || 5000;
const theme = (process.env.THEME || 'light').toLowerCase();
// Store website statuses
const websiteStatuses = new Map();
// Function to check website status
async function checkWebsite(url) {
try {
const startTime = Date.now();
const response = await fetch(url);
const responseTime = Date.now() - startTime;
return {
status: response.ok ? 'up' : 'down',
statusCode: response.status,
responseTime,
lastChecked: new Date().toISOString(),
error: null
};
} catch (error) {
return {
status: 'down',
statusCode: null,
responseTime: null,
lastChecked: new Date().toISOString(),
error: error.message
};
}
}
// Check all websites
async function checkAllWebsites() {
for (const website of websites) {
const status = await checkWebsite(website);
websiteStatuses.set(website, status);
}
}
// Start periodic checking
setInterval(checkAllWebsites, checkInterval);
checkAllWebsites(); // Initial check
// Serve static files
app.use(express.static('public'));
// API endpoint to get all statuses
app.get('/api/statuses', (req, res) => {
const statuses = Object.fromEntries(websiteStatuses);
res.json(statuses);
});
// API endpoint to get configuration
app.get('/api/config', (req, res) => {
res.json({
checkInterval,
uiRefreshInterval,
theme
});
});
app.listen(port, () => {
console.log(`DumbMonitor running on http://localhost:${port}`);
console.log('Monitoring websites:', websites);
console.log('Check interval:', checkInterval, 'ms');
console.log('UI refresh interval:', uiRefreshInterval, 'ms');
console.log('Theme:', theme);
});