// js/caddy.js - Caddy 实例状态管理与控制 import { api } from './api.js'; import { notification } from './notifications.js'; let caddyStatusInterval; const POLLING_INTERVAL = 5000; const DOMElements = { caddyStatusIndicator: document.getElementById('caddy-status-indicator'), caddyActionButtonContainer: document.getElementById('caddy-action-button-container'), }; function createButton(text, className, onClick) { const button = document.createElement('button'); button.className = `btn ${className}`; button.innerHTML = `${text}`; button.addEventListener('click', onClick); return button; } function updateCaddyStatusView(status) { const dot = DOMElements.caddyStatusIndicator.querySelector('.status-dot'); const text = DOMElements.caddyStatusIndicator.querySelector('.status-text'); const buttonContainer = DOMElements.caddyActionButtonContainer; if(!dot || !text || !buttonContainer) return; // 如果元素不存在,则不执行 dot.className = 'status-dot'; buttonContainer.innerHTML = ''; let statusText, dotClass; switch (status) { case 'running': statusText = '运行中'; dotClass = 'running'; buttonContainer.appendChild(createButton('重载配置', 'btn-warning', handleReloadCaddy)); buttonContainer.appendChild(createButton('停止 Caddy', 'btn-danger', handleStopCaddy)); break; case 'stopped': statusText = '已停止'; dotClass = 'stopped'; buttonContainer.appendChild(createButton('启动 Caddy', 'btn-success', handleStartCaddy)); break; case 'checking': statusText = '检查中...'; dotClass = 'checking'; break; default: statusText = '状态未知'; dotClass = 'error'; break; } text.textContent = statusText; dot.classList.add(dotClass); } async function checkCaddyStatus() { try { const response = await api.get('/caddy/status'); updateCaddyStatusView(response.message === 'Caddy is running' ? 'running' : 'stopped'); } catch (error) { console.error('Error checking Caddy status:', error); updateCaddyStatusView('error'); } } async function handleStartCaddy() { try { const result = await api.post('/caddy/run'); notification.toast(result.message || '启动命令已发送。', 'success'); setTimeout(checkCaddyStatus, 500); } catch (error) { notification.toast(`启动失败: ${error.message}`, 'error'); } } async function handleStopCaddy() { if (!await notification.confirm('您确定要停止 Caddy 实例吗?')) return; try { const result = await api.post('/caddy/stop'); notification.toast(result.message || '停止命令已发送。', 'info'); setTimeout(checkCaddyStatus, 500); } catch(error) { notification.toast(`操作失败: ${error.message}`, 'error'); } } async function handleReloadCaddy() { if (!await notification.confirm('确定要重载 Caddy 配置吗?')) return; try { const result = await api.post('/caddy/restart'); notification.toast(result.message || '重载命令已发送。', 'success'); setTimeout(checkCaddyStatus, 500); } catch(error) { notification.toast(`重载失败: ${error.message}`, 'error'); } } export function initCaddyStatus() { // 确保通知模块已经初始化 const dialogContainer = document.getElementById('dialog-container'); const toastContainer = document.getElementById('toast-container'); if (dialogContainer && toastContainer) { notification.init(toastContainer, dialogContainer); } checkCaddyStatus(); if (caddyStatusInterval) { clearInterval(caddyStatusInterval); } caddyStatusInterval = setInterval(checkCaddyStatus, POLLING_INTERVAL); }