chore: add .gitignore to exclude node_modules and log files
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
1 parent
3c856ea9d4
commit
05fe86c454
2 files changed
+177
-7
No files matched your search
@@ -1,6 +1,8 @@
|
||||
const express = require('express');
|
||||
const https = require('https');
|
||||
const http = require('http');
|
||||
const app = express();
|
||||
app.use(express.json());
|
||||
const PORT = 3000;
|
||||
|
||||
// ─── IP Helpers ───────────────────────────────────────────────────────────────
|
||||
@@ -30,8 +32,9 @@ function getPublicIP() {
|
||||
|
||||
// ─── Internet Monitor State ───────────────────────────────────────────────────
|
||||
|
||||
const CHECK_INTERVAL_MS = 5000;
|
||||
const CHECK_URL = 'https://www.google.com';
|
||||
let checkIntervalMs = 5000;
|
||||
let checkUrl = 'https://www.google.com';
|
||||
let intervalId = null;
|
||||
|
||||
let isOnline = true;
|
||||
let outageStart = null;
|
||||
@@ -41,7 +44,8 @@ let totalDowntimeMs = 0;
|
||||
|
||||
function checkConnectivity() {
|
||||
return new Promise((resolve) => {
|
||||
const req = https.get(CHECK_URL, { timeout: 4000 }, (res) => {
|
||||
const mod = checkUrl.startsWith('https://') ? https : http;
|
||||
const req = mod.get(checkUrl, { timeout: 4000 }, (res) => {
|
||||
res.resume();
|
||||
resolve(true);
|
||||
});
|
||||
@@ -75,7 +79,12 @@ function formatDuration(ms) {
|
||||
return `${sec}s`;
|
||||
}
|
||||
|
||||
setInterval(monitor, CHECK_INTERVAL_MS);
|
||||
function restartMonitor() {
|
||||
if (intervalId) clearInterval(intervalId);
|
||||
intervalId = setInterval(monitor, checkIntervalMs);
|
||||
}
|
||||
|
||||
restartMonitor();
|
||||
monitor();
|
||||
|
||||
// ─── Routes ───────────────────────────────────────────────────────────────────
|
||||
@@ -249,6 +258,63 @@ app.get('/', async (req, res) => {
|
||||
}
|
||||
.export-menu a:hover { background: #243044; }
|
||||
.export-menu a + a { border-top: 1px solid #334155; }
|
||||
|
||||
/* Settings */
|
||||
.settings-btn {
|
||||
display: inline-flex; align-items: center; gap: 0.45rem;
|
||||
background: #1e293b; border: 1px solid #334155; color: #e2e8f0;
|
||||
padding: 0.4rem 1rem; border-radius: 8px; font-size: 0.85rem;
|
||||
font-weight: 600; cursor: pointer; transition: background 0.15s;
|
||||
}
|
||||
.settings-btn:hover { background: #243044; }
|
||||
.modal-overlay {
|
||||
display: none; position: fixed; inset: 0;
|
||||
background: rgba(0,0,0,.65); z-index: 200;
|
||||
align-items: center; justify-content: center;
|
||||
}
|
||||
.modal-overlay.open { display: flex; }
|
||||
.modal {
|
||||
background: #1e293b; border: 1px solid #334155;
|
||||
border-radius: 16px; padding: 1.75rem;
|
||||
width: 100%; max-width: 420px;
|
||||
box-shadow: 0 20px 60px rgba(0,0,0,.5);
|
||||
}
|
||||
.modal-title { font-size: 1rem; font-weight: 700; margin-bottom: 1.25rem; }
|
||||
.form-group { margin-bottom: 1rem; }
|
||||
.form-label {
|
||||
display: block; font-size: 0.75rem; font-weight: 600; color: #94a3b8;
|
||||
text-transform: uppercase; letter-spacing: 0.06em; margin-bottom: 0.4rem;
|
||||
}
|
||||
.form-input {
|
||||
width: 100%; background: #0f172a; border: 1px solid #334155;
|
||||
border-radius: 8px; padding: 0.55rem 0.75rem;
|
||||
color: #e2e8f0; font-size: 0.9rem; outline: none;
|
||||
transition: border-color 0.15s;
|
||||
}
|
||||
.form-input:focus { border-color: #6366f1; }
|
||||
.form-hint { font-size: 0.72rem; color: #64748b; margin-top: 0.3rem; }
|
||||
.modal-actions { display: flex; gap: 0.75rem; margin-top: 1.5rem; justify-content: flex-end; }
|
||||
.btn-save {
|
||||
background: #6366f1; color: white; border: none;
|
||||
padding: 0.5rem 1.25rem; border-radius: 8px;
|
||||
font-size: 0.85rem; font-weight: 600; cursor: pointer;
|
||||
transition: background 0.15s;
|
||||
}
|
||||
.btn-save:hover { background: #4f46e5; }
|
||||
.btn-cancel {
|
||||
background: transparent; color: #94a3b8; border: 1px solid #334155;
|
||||
padding: 0.5rem 1.25rem; border-radius: 8px;
|
||||
font-size: 0.85rem; cursor: pointer; transition: background 0.15s;
|
||||
}
|
||||
.btn-cancel:hover { background: #243044; color: #e2e8f0; }
|
||||
.toast {
|
||||
position: fixed; bottom: 1.5rem; right: 1.5rem;
|
||||
background: #14532d; color: #4ade80; border: 1px solid #166534;
|
||||
padding: 0.65rem 1.25rem; border-radius: 10px; font-size: 0.85rem;
|
||||
font-weight: 600; opacity: 0; transition: opacity 0.3s; z-index: 300;
|
||||
pointer-events: none;
|
||||
}
|
||||
.toast.show { opacity: 1; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -259,6 +325,7 @@ app.get('/', async (req, res) => {
|
||||
<div class="subtitle" id="monitor-since">Loading...</div>
|
||||
</div>
|
||||
<div id="status-badge" class="online"><div class="dot"></div><span id="status-text">Checking…</span></div>
|
||||
<button class="settings-btn" onclick="openSettings()">⚙ Settings</button>
|
||||
<div class="export-wrap">
|
||||
<button class="export-btn" onclick="toggleExportMenu(event)">⬇ Export</button>
|
||||
<div class="export-menu" id="export-menu">
|
||||
@@ -339,8 +406,30 @@ app.get('/', async (req, res) => {
|
||||
|
||||
<div id="last-updated"></div>
|
||||
|
||||
<!-- Settings Modal -->
|
||||
<div class="modal-overlay" id="settings-overlay" onclick="closeSettingsOnOverlay(event)">
|
||||
<div class="modal">
|
||||
<div class="modal-title">⚙ Settings</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label" for="inp-interval">Check Interval (seconds)</label>
|
||||
<input class="form-input" type="number" id="inp-interval" min="1" max="3600" step="1">
|
||||
<div class="form-hint">How often to check connectivity (1–3600 s)</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label class="form-label" for="inp-url">Check URL</label>
|
||||
<input class="form-input" type="url" id="inp-url" placeholder="https://www.google.com">
|
||||
<div class="form-hint">URL used to test internet connectivity</div>
|
||||
</div>
|
||||
<div class="modal-actions">
|
||||
<button class="btn-cancel" onclick="closeSettings()">Cancel</button>
|
||||
<button class="btn-save" onclick="saveSettings()">Save</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="toast" id="toast">✓ Settings saved</div>
|
||||
|
||||
<script>
|
||||
const CHECK_INTERVAL_MS = ${CHECK_INTERVAL_MS};
|
||||
let checkIntervalMs = ${checkIntervalMs};
|
||||
|
||||
function fmtMs(ms) {
|
||||
const s = Math.floor(ms / 1000);
|
||||
@@ -411,7 +500,7 @@ app.get('/', async (req, res) => {
|
||||
|
||||
document.getElementById('last-updated').textContent =
|
||||
'Last updated ' + new Date().toLocaleTimeString() +
|
||||
' · checks every ' + (CHECK_INTERVAL_MS / 1000) + 's';
|
||||
' · checks every ' + (checkIntervalMs / 1000) + 's';
|
||||
|
||||
} catch (e) {
|
||||
document.getElementById('status-text').textContent = 'Error';
|
||||
@@ -420,6 +509,52 @@ app.get('/', async (req, res) => {
|
||||
|
||||
refresh();
|
||||
setInterval(refresh, 1000);
|
||||
|
||||
// ── Export menu ───────────────────────────────────────────────────────────
|
||||
function toggleExportMenu(e) {
|
||||
e.stopPropagation();
|
||||
document.getElementById('export-menu').classList.toggle('open');
|
||||
}
|
||||
document.addEventListener('click', () => {
|
||||
document.getElementById('export-menu').classList.remove('open');
|
||||
});
|
||||
|
||||
// ── Settings ─────────────────────────────────────────────────────────────
|
||||
function openSettings() {
|
||||
fetch('/api/settings').then(r => r.json()).then(s => {
|
||||
document.getElementById('inp-interval').value = s.checkIntervalMs / 1000;
|
||||
document.getElementById('inp-url').value = s.checkUrl;
|
||||
document.getElementById('settings-overlay').classList.add('open');
|
||||
});
|
||||
}
|
||||
function closeSettings() {
|
||||
document.getElementById('settings-overlay').classList.remove('open');
|
||||
}
|
||||
function closeSettingsOnOverlay(e) {
|
||||
if (e.target === document.getElementById('settings-overlay')) closeSettings();
|
||||
}
|
||||
async function saveSettings() {
|
||||
const intervalSec = parseFloat(document.getElementById('inp-interval').value);
|
||||
const url = document.getElementById('inp-url').value.trim();
|
||||
if (!intervalSec || intervalSec < 1) return alert('Interval must be at least 1 second.');
|
||||
if (!url) return alert('URL cannot be empty.');
|
||||
const res = await fetch('/api/settings', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ checkIntervalMs: Math.round(intervalSec * 1000), checkUrl: url })
|
||||
});
|
||||
if (res.ok) {
|
||||
const s = await res.json();
|
||||
checkIntervalMs = s.checkIntervalMs;
|
||||
closeSettings();
|
||||
const toast = document.getElementById('toast');
|
||||
toast.classList.add('show');
|
||||
setTimeout(() => toast.classList.remove('show'), 2500);
|
||||
} else {
|
||||
const err = await res.json().catch(() => ({}));
|
||||
alert('Failed to save settings: ' + (err.error || res.status));
|
||||
}
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>`);
|
||||
@@ -439,7 +574,8 @@ app.get('/api/status', (req, res) => {
|
||||
outageCount: outages.length + (!isOnline ? 1 : 0),
|
||||
currentOutageStart: outageStart,
|
||||
outages: outages.slice().reverse(),
|
||||
checkIntervalMs: CHECK_INTERVAL_MS
|
||||
checkIntervalMs,
|
||||
checkUrl
|
||||
});
|
||||
});
|
||||
|
||||
@@ -518,6 +654,33 @@ app.get('/export/csv', (req, res) => {
|
||||
res.send(lines.join('\r\n'));
|
||||
});
|
||||
|
||||
// Settings API
|
||||
app.get('/api/settings', (req, res) => {
|
||||
res.json({ checkIntervalMs, checkUrl });
|
||||
});
|
||||
|
||||
app.post('/api/settings', (req, res) => {
|
||||
const { checkIntervalMs: newInterval, checkUrl: newUrl } = req.body || {};
|
||||
if (newInterval !== undefined) {
|
||||
const ms = parseInt(newInterval, 10);
|
||||
if (isNaN(ms) || ms < 1000) {
|
||||
return res.status(400).json({ error: 'checkIntervalMs must be >= 1000' });
|
||||
}
|
||||
checkIntervalMs = ms;
|
||||
restartMonitor();
|
||||
}
|
||||
if (newUrl !== undefined) {
|
||||
try {
|
||||
const u = new URL(newUrl);
|
||||
if (u.protocol !== 'http:' && u.protocol !== 'https:') throw new Error();
|
||||
} catch (e) {
|
||||
return res.status(400).json({ error: 'checkUrl must be a valid http/https URL' });
|
||||
}
|
||||
checkUrl = newUrl;
|
||||
}
|
||||
res.json({ checkIntervalMs, checkUrl });
|
||||
});
|
||||
|
||||
// ─── Start ────────────────────────────────────────────────────────────────────
|
||||
|
||||
app.listen(PORT, () => {
|
||||
|
||||
Reference in new issue
Block a user