Repository navigation
Expand file tree
/
Copy pathserver.js
More file actions
110 lines (96 loc) · 3.4 KB
/
Copy pathserver.js
File metadata and controls
110 lines (96 loc) · 3.4 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
const express = require('express');
const axios = require('axios');
const fs = require('fs');
const path = require('path');
const app = express();
const port = 3000;
const MAX_HISTORY = 20; // Limit history to 20 entries
const HISTORY_FILE = path.join(__dirname, 'statusHistory.json');
const WEBSITES_FILE = path.join(__dirname, 'URLs.txt'); // Renamed to URLs.txt
let statusHistory = {};
let websites = [];
// Load history from file
function loadHistory() {
if (fs.existsSync(HISTORY_FILE)) {
const data = fs.readFileSync(HISTORY_FILE, 'utf8');
statusHistory = JSON.parse(data);
}
}
// Save history to file
function saveHistory() {
fs.writeFileSync(HISTORY_FILE, JSON.stringify(statusHistory, null, 2));
}
// Load websites from file
function loadWebsites() {
if (fs.existsSync(WEBSITES_FILE)) {
const data = fs.readFileSync(WEBSITES_FILE, 'utf8');
websites = data.split('\n').filter(Boolean);
}
}
// Save websites to file
function saveWebsites() {
fs.writeFileSync(WEBSITES_FILE, websites.join('\n'));
}
app.use(express.static('public'));
app.use(express.json());
// Check the status of a website
async function checkStatus(url) {
try {
const response = await axios.get(url);
return { url, status: response.status, timestamp: new Date().toISOString() };
} catch (error) {
const status = error.response ? error.response.status : `Error: ${error.message}`;
return { url, status, timestamp: new Date().toISOString() };
}
}
// Update the status history for all websites
async function updateStatusHistory() {
const results = await Promise.all(websites.map(url => checkStatus(url)));
results.forEach(result => {
if (!statusHistory[result.url]) {
statusHistory[result.url] = [];
}
statusHistory[result.url].push(result);
if (statusHistory[result.url].length > MAX_HISTORY) {
statusHistory[result.url].shift(); // Remove oldest entry if history exceeds MAX_HISTORY
}
});
saveHistory(); // Save history after updating
}
// Endpoint to get the status history
app.get('/check-status', (req, res) => {
res.json(statusHistory);
});
// Endpoint to get the list of websites
app.get('/websites', (req, res) => {
res.json(websites);
});
// Endpoint to add a new website
app.post('/websites', async (req, res) => {
const { url } = req.body;
if (url && !websites.includes(url)) {
websites.push(url);
saveWebsites();
await updateStatusHistory(); // Update status history immediately after adding a website
res.status(201).json({ message: 'Website added' });
} else {
res.status(400).json({ message: 'Invalid URL or already exists' });
}
});
// Endpoint to remove a website
app.delete('/websites', async (req, res) => {
const { url } = req.body;
websites = websites.filter(website => website !== url);
delete statusHistory[url]; // Remove history for the website
saveWebsites();
saveHistory(); // Save history after removing
res.status(200).json({ message: 'Website removed' });
});
// Start the server and load initial data
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
loadHistory(); // Load history on startup
loadWebsites(); // Load websites on startup
updateStatusHistory();
setInterval(updateStatusHistory, 60 * 1000); // Recheck every minute
});