const $ = id => document.getElementById(id); let activeLog = null; const element = (tag, text) => { const e = document.createElement(tag); e.textContent = text; return e; }; function url(address, port, protocol = 'http') { let host = address; if (!host || host === '0.0.0.0' || host === '::') host = location.hostname; host = host.replace(/^\[|\]$/g, ''); return `${protocol}://${host.includes(':') ? `[${host}]` : host}:${port}/`; } async function control(name, command) { try { const r = await fetch(`/_cloud9/sites/${encodeURIComponent(name)}/ctl`, { method:'POST', headers:{'Content-Type':'text/plain'}, body:command }); if (!r.ok) throw new Error(await r.text()); $('message').textContent = `${name}: ${command} requested`; await refresh(); } catch (e) { $('message').textContent = e.message; } } function button(text, action, disabled = false) { const b = element('button', text); b.disabled = disabled; b.onclick = action; return b; } function listenerRow(item) { const tr = element('tr', ''); const name = element('td', ''); if (item.protocol !== 'unknown') { const a = element('a', item.service); a.href = url(item.address, item.port, item.protocol); name.append(a); } else name.textContent = item.process || 'Unknown'; tr.append(name, element('td', `${item.address}:${item.port}${item.local ? ' · local only' : ''}`)); const process = element('td', item.pid ? `PID ${item.pid}` : 'Unavailable'); process.className = 'process'; tr.append(process, element('td', item.evidence)); return tr; } async function refresh() { try { const r = await fetch('/_cloud9/catalog.json'); if (!r.ok) throw new Error(`Dashboard: ${r.status}`); const data = await r.json(); $('sites').replaceChildren(); for (const site of data.sites) { const card = element('article', ''); const h = element('h2', ''); const a = element('a', site.title); a.href = url('', site.port); h.append(a); const s = site.status; card.append(h, element('code', site.root), element('p', `${s.phase}${s.watching ? ' · watch on' : ''}${s.queued ? ' · build queued' : ''}${s.exit_code !== null ? ` · exit ${s.exit_code}` : ''}`)); if (s.finished) card.append(element('small', `Last finished ${new Date(s.finished * 1000).toLocaleString()}`), element('br', '')); card.append(button('Build', () => control(site.name, 'build'), !site.build), button(s.watching ? 'Stop watching' : 'Watch', () => control(site.name, s.watching ? 'watch off' : 'watch on'), !site.watch), button('Cancel', () => control(site.name, 'cancel'), !s.pid && !s.queued), button('Log', () => { activeLog = site.name; $('log-title').textContent = site.title; $('logs').showModal(); refreshLog(); })); $('sites').append(card); } const known = data.listeners.filter(x => x.protocol !== 'unknown'); const unknown = data.listeners.filter(x => x.protocol === 'unknown'); $('services').replaceChildren(...known.map(listenerRow)); $('unknown').replaceChildren(...unknown.map(listenerRow)); $('unknown-title').textContent = `Other listeners (${unknown.length})`; if (data.discovery_error) $('message').textContent = data.discovery_error; } catch (e) { $('message').textContent = e.message; } } async function refreshLog() { if (!activeLog) return; try { const r = await fetch(`/fs/sites/${encodeURIComponent(activeLog)}/log`); $('log-text').textContent = await r.text(); } catch (e) { $('log-text').textContent = e.message; } } $('close-log').onclick = () => { activeLog = null; $('logs').close(); }; $('logs').onclose = () => { activeLog = null; }; refresh(); setInterval(refresh, 5000); setInterval(refreshLog, 1500);