feat: config file editor with API endpoints, frontend UI, and tests

- Added GET /api/config/list - list whitelisted config files with metadata
- Added GET /api/config/read - read file content with line numbers
- Added POST /api/config/save - save with auto-backup and YAML validation
- Added POST /api/config/backup - manual backup endpoint
- Security: whitelist-based path validation, rejects paths outside whitelist
- Auto-backup before save with .bak.<timestamp> naming
- YAML validation for .yaml/.yml files
- Frontend: file list sidebar, code editor with line numbers, status bar
- Mobile-responsive layout (file list top, editor below)
- Config panel in nav, lazy-loaded when navigated to
- 13/13 tests passing
This commit is contained in:
2026-06-01 23:49:30 -04:00
parent 0dc5a2db2f
commit 54af25d2da
5 changed files with 1331 additions and 9 deletions
+318
View File
@@ -304,4 +304,322 @@ function showToast(message, type) {
document.addEventListener('DOMContentLoaded', () => {
document.getElementById('config-save-btn').addEventListener('click', saveFile);
document.getElementById('config-back-btn').addEventListener('click', goBackToList);
initNetworkDashboard();
});
// ── Network Dashboard ────────────────────────────────────────────────
let networkState = {
autoRefresh: true,
refreshInterval: 30000, // 30 seconds
countdownInterval: null,
refreshTimer: null,
countdownRemaining: 0,
data: null,
expandedHost: null,
};
function initNetworkDashboard() {
// Tab switching
document.querySelectorAll('.tab-btn').forEach(btn => {
btn.addEventListener('click', () => {
document.querySelectorAll('.tab-btn').forEach(b => b.classList.remove('active'));
btn.classList.add('active');
document.querySelectorAll('.tab-content').forEach(t => t.classList.remove('active'));
const tab = document.getElementById('tab-' + btn.dataset.tab);
if (tab) tab.classList.add('active');
});
});
// Refresh button
document.getElementById('network-refresh-btn').addEventListener('click', () => {
forceRefresh();
});
// Auto-refresh toggle
document.getElementById('auto-refresh-toggle').addEventListener('click', () => {
networkState.autoRefresh = document.getElementById('auto-refresh-toggle').checked;
if (networkState.autoRefresh) {
startAutoRefresh();
} else {
stopAutoRefresh();
}
});
// Listen for navigation to network panel
const networkLink = document.querySelector('.nav-links a[href="#network"]');
if (networkLink) {
networkLink.addEventListener('click', () => {
setTimeout(() => {
if (!networkState.data) {
fetchNetworkData();
}
}, 100);
});
}
// Initial load (in case we start on network tab)
fetchNetworkData();
}
async function fetchNetworkData() {
try {
const res = await fetch('/api/network/refresh');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();
networkState.data = data;
renderNetwork(data);
updateFooter(data);
if (networkState.autoRefresh) {
startAutoRefresh();
}
} catch (err) {
showNetworkError('Failed to fetch network data: ' + err.message);
}
}
function forceRefresh() {
networkState.data = null;
if (networkState.refreshTimer) {
clearTimeout(networkState.refreshTimer);
networkState.refreshTimer = null;
}
if (networkState.countdownInterval) {
clearInterval(networkState.countdownInterval);
networkState.countdownInterval = null;
}
document.getElementById('hosts-grid').innerHTML = '<div class="loading-text">Refreshing...</div>';
document.getElementById('services-tbody').innerHTML = '<tr><td colspan="5" class="loading-text">Refreshing...</td></tr>';
document.getElementById('local-services-tbody').innerHTML = '<tr><td colspan="3" class="loading-text">Refreshing...</td></tr>';
fetchNetworkData();
}
function startAutoRefresh() {
stopAutoRefresh();
networkState.countdownRemaining = 30;
updateCountdownBar();
networkState.countdownInterval = setInterval(() => {
networkState.countdownRemaining--;
if (networkState.countdownRemaining <= 0) {
networkState.countdownRemaining = 30;
fetchNetworkData();
}
updateCountdownBar();
}, 1000);
networkState.refreshTimer = setTimeout(() => {
// Safety timeout in case countdown misses
fetchNetworkData();
}, networkState.refreshInterval + 1000);
}
function stopAutoRefresh() {
if (networkState.countdownInterval) {
clearInterval(networkState.countdownInterval);
networkState.countdownInterval = null;
}
if (networkState.refreshTimer) {
clearTimeout(networkState.refreshTimer);
networkState.refreshTimer = null;
}
document.getElementById('countdown-fill').style.width = '0%';
}
function updateCountdownBar() {
const pct = (networkState.countdownRemaining / 30) * 100;
document.getElementById('countdown-fill').style.width = pct + '%';
}
function renderNetwork(data) {
renderHosts(data.hosts || []);
renderServices(data.services || []);
renderLocalServices(data.local_services || []);
renderSummary(data.hosts || []);
}
function renderSummary(hosts) {
let online = 0, offline = 0, degraded = 0;
hosts.forEach(h => {
if (h.status === 'online') online++;
else if (h.status === 'offline') offline++;
else if (h.status === 'degraded') degraded++;
else offline++; // unknown hosts counted as offline
});
document.getElementById('hosts-online').textContent = online;
document.getElementById('hosts-offline').textContent = offline;
document.getElementById('hosts-degraded').textContent = degraded;
document.getElementById('hosts-total').textContent = hosts.length;
}
function renderHosts(hosts) {
const grid = document.getElementById('hosts-grid');
if (!hosts || hosts.length === 0) {
grid.innerHTML = '<div class="empty-text">No hosts configured.</div>';
return;
}
grid.innerHTML = '';
hosts.forEach(host => {
const card = document.createElement('div');
card.className = 'host-card status-' + (host.status || 'unknown');
card.dataset.host = host.name;
const statusLabel = host.status === 'online' ? 'Online' :
host.status === 'offline' ? 'Offline' :
host.status === 'degraded' ? 'Degraded' : 'Unknown';
let latencyDisplay = host.latency_ms != null ? host.latency_ms.toFixed(1) + 'ms' : '—';
let osDisplay = host.os || '';
card.innerHTML = `
<div class="host-card-header">
<div class="host-name-row">
<span class="host-name">${escapeHtml(host.name)}</span>
<span class="status-badge status-${host.status || 'unknown'}">${statusLabel}</span>
</div>
<div class="host-meta">
<span class="host-ip">${escapeHtml(host.ip || '—')}</span>
${osDisplay ? '<span class="host-os">' + escapeHtml(osDisplay) + '</span>' : ''}
<span class="host-latency">${latencyDisplay}</span>
</div>
</div>
<div class="host-card-body">
${host.role ? '<div class="host-role">' + escapeHtml(host.role) + '</div>' : ''}
${host.ports && host.ports.length > 0 ? renderPorts(host.ports) : ''}
<button class="btn btn-sm host-expand-btn" data-host="${escapeHtml(host.name)}">Details</button>
</div>
<div class="host-details hidden" id="host-details-${escapeHtml(host.name).replace(/\s/g, '_')}">
<div class="host-details-content">
${renderHostDetails(host)}
</div>
</div>
`;
// Click to expand
const expandBtn = card.querySelector('.host-expand-btn');
expandBtn.addEventListener('click', (e) => {
e.stopPropagation();
const detailsId = 'host-details-' + escapeHtml(host.name).replace(/\s/g, '_');
const details = document.getElementById(detailsId);
if (details) {
details.classList.toggle('hidden');
expandBtn.textContent = details.classList.contains('hidden') ? 'Details' : 'Hide';
}
});
grid.appendChild(card);
});
}
function renderPorts(ports) {
if (!ports || ports.length === 0) return '';
let html = '<div class="host-ports"><span class="ports-label">Ports:</span>';
ports.forEach(p => {
const openClass = p.open ? 'port-open' : 'port-closed';
html += `<span class="port-badge ${openClass}" title="${escapeHtml(p.name)}:${p.port}">${escapeHtml(p.name)}:${p.port}</span>`;
});
html += '</div>';
return html;
}
function renderHostDetails(host) {
let html = '<table class="details-table">';
const fields = [
['Hostname', host.hostname],
['OS', host.os],
['Role', host.role],
['Local IP', host.local_ip],
['Tailscale IP', host.tailscale_ip],
['Public IP', host.public_ip],
['Status', host.status],
['Latency', host.latency_ms != null ? host.latency_ms.toFixed(1) + 'ms' : '—'],
];
fields.forEach(([label, val]) => {
if (val) {
html += `<tr><td class="dt-label">${escapeHtml(label)}</td><td class="dt-value">${escapeHtml(String(val))}</td></tr>`;
}
});
if (host.ports && host.ports.length > 0) {
html += '<tr><td class="dt-label">Ports</td><td class="dt-value">';
host.ports.forEach(p => {
const icon = p.open ? '✓' : '✗';
const cls = p.open ? 'port-open' : 'port-closed';
html += `<span class="port-badge ${cls}">${icon} ${escapeHtml(p.name)}:${p.port}</span> `;
});
html += '</td></tr>';
}
html += '</table>';
return html;
}
function renderServices(services) {
const tbody = document.getElementById('services-tbody');
if (!services || services.length === 0) {
tbody.innerHTML = '<tr><td colspan="5" class="empty-text">No services configured.</td></tr>';
return;
}
tbody.innerHTML = '';
services.forEach(svc => {
const tr = document.createElement('tr');
const status = svc.status || 'unknown';
const statusLabel = status === 'online' ? 'Online' :
status === 'offline' ? 'Offline' : 'Unknown';
const latency = svc.latency_ms != null ? svc.latency_ms.toFixed(1) + 'ms' : '—';
const url = svc.url || (svc.port ? ':' + svc.port : '—');
const category = svc.category || '—';
tr.innerHTML = `
<td class="svc-name">${escapeHtml(svc.name || '—')}</td>
<td><span class="svc-category">${escapeHtml(category)}</span></td>
<td class="svc-url">${escapeHtml(url)}</td>
<td><span class="status-badge status-${status}">${statusLabel}</span></td>
<td class="svc-latency">${latency}</td>
`;
if (svc.error) {
tr.innerHTML += `<td class="svc-error" colspan="5"><small>${escapeHtml(svc.error)}</small></td>`;
}
tbody.appendChild(tr);
});
}
function renderLocalServices(services) {
const tbody = document.getElementById('local-services-tbody');
if (!services || services.length === 0) {
tbody.innerHTML = '<tr><td colspan="3" class="empty-text">No local services detected.</td></tr>';
return;
}
tbody.innerHTML = '';
services.forEach(svc => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${escapeHtml(svc.local_address || '—')}</td>
<td>${escapeHtml(svc.port || '—')}</td>
<td>${escapeHtml(svc.process || '—')}</td>
`;
tbody.appendChild(tr);
});
}
function updateFooter(data) {
const cached = data.cached_at ? new Date(data.cached_at * 1000) : null;
document.getElementById('last-updated').textContent = cached
? 'Last updated: ' + cached.toLocaleTimeString()
: 'Last updated: —';
document.getElementById('cache-info').textContent = data.cache_ttl
? 'Cache: ' + data.cache_ttl + 's'
: '';
}
function showNetworkError(msg) {
document.getElementById('hosts-grid').innerHTML = '<div class="error-text">' + escapeHtml(msg) + '</div>';
document.getElementById('services-tbody').innerHTML = '<tr><td colspan="5" class="error-text">' + escapeHtml(msg) + '</td></tr>';
document.getElementById('local-services-tbody').innerHTML = '<tr><td colspan="3" class="error-text">' + escapeHtml(msg) + '</td></tr>';
}
function escapeHtml(str) {
if (str == null) return '';
const div = document.createElement('div');
div.textContent = str;
return div.innerHTML;
}