// Linux BenchTools - Settings Logic const { copyToClipboard, showToast, escapeHtml } = window.BenchUtils; let tokenVisible = false; const API_TOKEN = 'YOUR_API_TOKEN_HERE'; // Will be replaced by actual token or fetched from backend // Initialize settings page document.addEventListener('DOMContentLoaded', () => { loadSettings(); generateBenchCommand(); }); // Load settings function loadSettings() { // In a real scenario, these would be fetched from backend or localStorage const savedBackendUrl = localStorage.getItem('backendUrl') || getDefaultBackendUrl(); const savedIperfServer = localStorage.getItem('iperfServer') || ''; const savedBenchMode = localStorage.getItem('benchMode') || ''; document.getElementById('backendUrl').value = savedBackendUrl; document.getElementById('iperfServer').value = savedIperfServer; document.getElementById('benchMode').value = savedBenchMode; // Set API token (in production, this should be fetched securely) document.getElementById('apiToken').value = API_TOKEN; // Add event listeners for auto-generation document.getElementById('backendUrl').addEventListener('input', () => { saveAndRegenerate(); }); document.getElementById('iperfServer').addEventListener('input', () => { saveAndRegenerate(); }); document.getElementById('benchMode').addEventListener('change', () => { saveAndRegenerate(); }); } // Get default backend URL function getDefaultBackendUrl() { const protocol = window.location.protocol; const hostname = window.location.hostname; return `${protocol}//${hostname}:8007`; } // Save settings and regenerate command function saveAndRegenerate() { const backendUrl = document.getElementById('backendUrl').value.trim(); const iperfServer = document.getElementById('iperfServer').value.trim(); const benchMode = document.getElementById('benchMode').value; localStorage.setItem('backendUrl', backendUrl); localStorage.setItem('iperfServer', iperfServer); localStorage.setItem('benchMode', benchMode); generateBenchCommand(); } // Generate bench command function generateBenchCommand() { const backendUrl = document.getElementById('backendUrl').value.trim(); const iperfServer = document.getElementById('iperfServer').value.trim(); const benchMode = document.getElementById('benchMode').value; if (!backendUrl) { document.getElementById('generatedCommand').textContent = 'Veuillez configurer l\'URL du backend'; return; } // Construct script URL (assuming script is served from same host as frontend) const scriptUrl = `${backendUrl.replace(':8007', ':8087')}/scripts/bench.sh`; // Build command parts let command = `curl -s ${scriptUrl} | bash -s -- \\ --server ${backendUrl}/api/benchmark \\ --token "${API_TOKEN}"`; if (iperfServer) { command += ` \\\n --iperf-server ${iperfServer}`; } if (benchMode) { command += ` \\\n ${benchMode}`; } document.getElementById('generatedCommand').textContent = command; showToast('Commande générée', 'success'); } // Copy generated command async function copyGeneratedCommand() { const command = document.getElementById('generatedCommand').textContent; if (command === 'Veuillez configurer l\'URL du backend') { showToast('Veuillez d\'abord configurer l\'URL du backend', 'error'); return; } const success = await copyToClipboard(command); if (success) { showToast('Commande copiée dans le presse-papier !', 'success'); } else { showToast('Erreur lors de la copie', 'error'); } } // Toggle token visibility function toggleTokenVisibility() { const tokenInput = document.getElementById('apiToken'); tokenVisible = !tokenVisible; if (tokenVisible) { tokenInput.type = 'text'; } else { tokenInput.type = 'password'; } } // Copy token async function copyToken() { const token = document.getElementById('apiToken').value; if (!token || token === 'Chargement...') { showToast('Token non disponible', 'error'); return; } const success = await copyToClipboard(token); if (success) { showToast('Token copié dans le presse-papier !', 'success'); } else { showToast('Erreur lors de la copie', 'error'); } } // Make functions available globally window.generateBenchCommand = generateBenchCommand; window.copyGeneratedCommand = copyGeneratedCommand; window.toggleTokenVisibility = toggleTokenVisibility; window.copyToken = copyToken;