diff options
Diffstat (limited to 'web/static')
| -rw-r--r-- | web/static/app.mjs | 70 | ||||
| -rw-r--r-- | web/static/catalog.html | 14 | ||||
| -rw-r--r-- | web/static/catalog.js | 56 | ||||
| -rw-r--r-- | web/static/catalog.mjs | 103 | ||||
| -rw-r--r-- | web/static/index.html | 44 | ||||
| -rw-r--r-- | web/static/style.css | 18 |
6 files changed, 224 insertions, 81 deletions
diff --git a/web/static/app.mjs b/web/static/app.mjs index 429bef7..b31177a 100644 --- a/web/static/app.mjs +++ b/web/static/app.mjs @@ -1,8 +1,40 @@ import { Client } from './client.mjs'; +import { catalogViews } from './catalog.mjs'; const $ = id => document.getElementById(id); const text = new TextDecoder('utf-8', { fatal: true }); let client = null, current = null, currentPath = '/', busy = false, dirty = false; let configuration = null; +let activeView = 'files'; +const catalog = catalogViews(pathLink); + +async function loadConfiguration() { + if (!configuration) { + const response = await fetch('/_cloud9/config.json'); + if (!response.ok) throw new Error('Unable to load server settings. Reload to try again.'); + configuration = await response.json(); + for (const view of ['sites', 'services']) $(`view-${view}`).hidden = !configuration.catalog; + } + return configuration; +} +function locationView() { + const view = new URLSearchParams(location.search).get('view'); + return configuration?.catalog && ['sites', 'services'].includes(view) ? view : 'files'; +} +function viewURL(view, path = currentPath) { + return pathURL(path) + (view === 'files' ? '' : `?view=${view}`); +} +function showView(view) { + activeView = view; + for (const name of ['files', 'sites', 'services']) { + $(`${name}-view`).hidden = view !== name; + const link = $(`view-${name}`); + link.classList.toggle('selected', view === name); + if (view === name) link.setAttribute('aria-current', 'page'); else link.removeAttribute('aria-current'); + link.href = viewURL(name); + } + document.title = `${view === 'files' ? currentPath : view} — cloud9`; + catalog.show(view); +} function status(message, error = false) { $('status').textContent = message; @@ -17,7 +49,7 @@ function controls() { $('delete').disabled = busy || currentPath === '/'; $('editor').readOnly = busy; $('edit-status').textContent = dirty ? 'Unsaved changes.' : 'No unsaved changes.'; - document.querySelector('main').setAttribute('aria-busy', String(busy)); + document.querySelector('#files-view main').setAttribute('aria-busy', String(busy)); } async function action(operation) { if (busy) return; @@ -29,11 +61,7 @@ async function action(operation) { } async function connectedClient() { if (client && !client.closed) return client; - if (!configuration) { - const response = await fetch('/_cloud9/config.json'); - if (!response.ok) throw new Error('Unable to load server settings. Reload to try again.'); - configuration = await response.json(); - } + await loadConfiguration(); try { client = await Client.connect('/_cloud9/9p', configuration.user, configuration.tree); } catch { throw new Error('Unable to reach the file server. Reload to try again.'); } return client; @@ -105,10 +133,12 @@ function breadcrumbs(path) { } async function navigate(path, historyMode = 'push') { path = normalizePath(path); + showView('files'); stopFollow(); status('Loading…'); const result = await readPath(path); current = result; currentPath = path; dirty = false; + for (const view of ['files', 'sites', 'services']) $(`view-${view}`).href = viewURL(view); $('path').value = path; const url = pathURL(path); if (historyMode === 'push' && location.pathname + location.search + location.hash !== url) history.pushState(null, '', url); @@ -149,6 +179,17 @@ async function navigate(path, historyMode = 'push') { } function mayLeave() { return !dirty || window.confirm('Discard unsaved changes?'); } document.addEventListener('click', event => { + const tab = event.target.closest('a[data-view]'); + if (tab && event.button === 0 && !event.ctrlKey && !event.metaKey && !event.shiftKey && !event.altKey) { + event.preventDefault(); + if (busy || tab.hidden) return; + const view = tab.dataset.view; + if (view === activeView) return; + history.pushState(null, '', viewURL(view)); + showView(view); + if (view === 'files' && !current) action(() => navigate(currentPath, 'none')).then(loadTree); + return; + } const link = event.target.closest('a[data-path]'); if (!link || event.button !== 0 || event.ctrlKey || event.metaKey || event.shiftKey || event.altKey) return; event.preventDefault(); @@ -180,8 +221,10 @@ $('download').onclick = () => { setTimeout(() => URL.revokeObjectURL(url), 1000); }; window.addEventListener('popstate', () => { - if (busy || !mayLeave()) { history.pushState(null, '', pathURL(currentPath)); return; } - action(() => navigate(locationPath(), 'none')); + const view = locationView(), path = locationPath(); + if (busy || (view === 'files' && path !== currentPath && !mayLeave())) { history.pushState(null, '', viewURL(activeView)); return; } + showView(view); + if (view === 'files' && (!current || path !== currentPath)) action(() => navigate(path, 'none')).then(loadTree); }); window.addEventListener('beforeunload', event => { if (dirty) { event.preventDefault(); event.returnValue = ''; } @@ -357,4 +400,13 @@ $('upload-input').onchange = () => { $('upload-input').value = ''; }; -action(() => navigate(locationPath(), 'none')).then(loadTree); +async function start() { + try { + await loadConfiguration(); + currentPath = locationPath(); + const view = locationView(); + showView(view); + if (view === 'files') await action(() => navigate(currentPath, 'none')).then(loadTree); + } catch (error) { status(error.message, true); } +} +start(); diff --git a/web/static/catalog.html b/web/static/catalog.html deleted file mode 100644 index 2160612..0000000 --- a/web/static/catalog.html +++ /dev/null @@ -1,14 +0,0 @@ -<!doctype html> -<html lang="en"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"> -<title>9web · local sites</title> -<style> -:root{color-scheme:light dark;font:16px/1.5 system-ui;background:#151917;color:#e3e9df}body{max-width:1100px;margin:3rem auto;padding:0 1.5rem}h1{font-size:2.5rem;margin-bottom:0}a{color:#b8dfa6}header p{color:#aab8a6}.sites{display:grid;grid-template-columns:repeat(auto-fit,minmax(290px,1fr));gap:1rem}article{background:#212922;border:1px solid #3c4b3c;border-radius:10px;padding:1.2rem}article h2{margin:0 0 .6rem;font-size:1.3rem}code,pre{font:13px/1.5 monospace;overflow-wrap:anywhere}button{background:#324631;color:inherit;border:1px solid #61765a;border-radius:5px;padding:.4rem .7rem;cursor:pointer;margin:.4rem .3rem .1rem 0}button:disabled{opacity:.4}table{width:100%;border-collapse:collapse}td,th{text-align:left;border-bottom:1px solid #354234;padding:.6rem;overflow-wrap:anywhere}th{color:#aab8a6}pre{max-height:35rem;overflow:auto;background:#101410;padding:1rem;white-space:pre-wrap}small{color:#aab8a6}.error{color:#ffb4a9}.toolbar{display:flex;gap:1rem;align-items:center}dialog{width:min(850px,85vw);background:#212922;color:inherit;border:1px solid #61765a}#message{min-height:1.5em}details{margin-top:1.5rem}@media(max-width:650px){td,th{padding:.4rem;font-size:13px}.process{display:none}} -</style> -<header><h1>9web</h1><p>Local sites, builds, and listening services.</p></header> -<p id="message" role="status"></p><main><div id="sites" class="sites"></div> -<div class="toolbar"><h2>HTTP services</h2><a href="/fs/">9P tree</a></div> -<p><small>Identified from socket ownership and process arguments. No endpoint probing. Loopback links work only on this computer.</small></p> -<table><thead><tr><th>Service</th><th>Address</th><th class="process">Process</th><th>Evidence</th></tr></thead><tbody id="services"></tbody></table> -<details><summary id="unknown-title">Other listeners</summary><table><tbody id="unknown"></tbody></table></details></main> -<dialog id="logs"><button id="close-log">Close</button><h2 id="log-title"></h2><pre id="log-text"></pre></dialog> -<script src="/_cloud9/catalog.js"></script></html> diff --git a/web/static/catalog.js b/web/static/catalog.js deleted file mode 100644 index f868a16..0000000 --- a/web/static/catalog.js +++ /dev/null @@ -1,56 +0,0 @@ -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); diff --git a/web/static/catalog.mjs b/web/static/catalog.mjs new file mode 100644 index 0000000..d1d3444 --- /dev/null +++ b/web/static/catalog.mjs @@ -0,0 +1,103 @@ +// Sites and services are views of the same 9web application. File/log links +// use app.mjs navigation; control writes use the normal HTTP-to-9P mapping. +const $ = id => document.getElementById(id); +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}/`; +} + +export function catalogViews(pathLink) { + let active = 'files', timer = null, request = null, revision = 0, pending = new Set(); + const rows = new Map(); + function status(view, text, error = false) { + const p = $(`${view}-status`); p.textContent = text; p.classList.toggle('error', error); + } + async function control(site, command) { + if (pending.has(site.name)) return; + pending.add(site.name); updateSite(site); + try { + const r = await fetch(`/fs/sites/${encodeURIComponent(site.name)}/ctl`, { method: 'PUT', body: command }); + if (!r.ok) throw new Error(await r.text()); + status('sites', `${site.title}: ${command} requested.`); + } catch (error) { status('sites', error.message, true); } + finally { pending.delete(site.name); updateSite(site); if (active === 'sites') await refresh(false); } + } + function updateSite(site) { + let row = rows.get(site.name); + if (!row) { + const tr = element('tr'); tr.dataset.site = site.name; + const name = element('td'), phase = element('td'), finished = element('td'), actions = element('td'); + const link = element('a'); link.className = 'site-name'; + const root = element('span'); root.className = 'site-root'; name.append(link, root); + const tools = element('div'); tools.className = 'site-actions'; + const build = element('button', 'build'), watch = element('button', 'watch'), cancel = element('button', 'cancel'); + for (const b of [build, watch, cancel]) b.type = 'button'; + tools.append(build, watch, cancel, pathLink('files', `/sites/${site.name}/public`), pathLink('log', `/sites/${site.name}/log`)); + actions.append(tools); tr.append(name, phase, finished, actions); + row = { tr, link, root, phase, finished, build, watch, cancel }; rows.set(site.name, row); + $('sites-entries').append(tr); + } + const s = site.status; + row.link.textContent = site.title; row.link.href = url('', site.port); row.root.textContent = site.root; + row.phase.textContent = `${s.phase}${s.watching ? ' · watch on' : ''}${s.queued ? ' · queued' : ''}${s.exit_code !== null ? ` · exit ${s.exit_code}` : ''}`; + row.phase.className = `site-phase${s.phase === 'failed' ? ' failed' : ''}`; + row.finished.textContent = s.finished ? new Date(s.finished * 1000).toLocaleString() : '—'; + row.build.disabled = pending.has(site.name) || !site.build; + row.watch.disabled = pending.has(site.name) || !site.watch; + row.watch.textContent = s.watching ? 'stop watching' : 'watch'; + row.cancel.disabled = pending.has(site.name) || (!s.pid && !s.queued && !s.watching); + row.build.onclick = () => control(site, 'build'); + row.watch.onclick = () => control(site, s.watching ? 'watch off' : 'watch on'); + row.cancel.onclick = () => control(site, 'cancel'); + } + function listenerRow(item, sites) { + const tr = element('tr'), name = element('td'); + if (item.protocol !== 'unknown') { + const site = item.service === '9web' && sites.find(s => s.port === item.port); + const a = element('a', site ? site.title : item.service); a.href = url(item.address, item.port, item.protocol); name.append(a); + } else name.textContent = item.process || 'Unknown'; + const address = element('td', `${item.address.includes(':') ? `[${item.address}]` : item.address}:${item.port}${item.local ? ' · local only' : ''}`); + address.className = 'listener-address'; + tr.append(name, address, element('td', item.pid ? `${item.process || ''} (${item.pid})` : 'Unavailable'), element('td', item.evidence)); + return tr; + } + async function refresh(showCount = true) { + if (active === 'files' || document.hidden) return; + request?.abort(); request = new AbortController(); + const ticket = ++revision, view = active; + $(`${view}-reload`).disabled = true; + try { + const r = await fetch('/_cloud9/catalog.json', { signal: request.signal }); + if (!r.ok) throw new Error(`Unable to load ${view}: HTTP ${r.status}`); + const data = await r.json(); + if (ticket !== revision || active !== view) return; + if (view === 'sites') { + const names = new Set(data.sites.map(s => s.name)); + for (const [name, row] of rows) if (!names.has(name)) { row.tr.remove(); rows.delete(name); } + for (const site of data.sites) updateSite(site); + $('sites-empty').hidden = data.sites.length !== 0; + if (showCount) status(view, `${data.sites.length} ${data.sites.length === 1 ? 'site' : 'sites'} · generated files are served continuously`); + } else { + const known = data.listeners.filter(x => x.protocol !== 'unknown'), unknown = data.listeners.filter(x => x.protocol === 'unknown'); + $('services-entries').replaceChildren(...known.map(x => listenerRow(x, data.sites))); + $('other-listeners-entries').replaceChildren(...unknown.map(x => listenerRow(x, data.sites))); + $('services-empty').hidden = known.length !== 0; + $('other-listeners-title').textContent = `Other listeners (${unknown.length})`; + status(view, data.discovery_error || `${known.length} HTTP services · refreshed every 5 seconds`, Boolean(data.discovery_error)); + } + } catch (error) { if (error.name !== 'AbortError' && ticket === revision) status(view, error.message, true); } + finally { if (ticket === revision) $(`${view}-reload`).disabled = false; } + } + function show(view) { + clearInterval(timer); request?.abort(); revision++; + active = view; + for (const name of ['sites', 'services']) $(`${name}-reload`).disabled = false; + if (view !== 'files') { refresh(); timer = setInterval(refresh, 5000); } + } + for (const name of ['sites', 'services']) $(`${name}-reload`).onclick = () => refresh(); + document.addEventListener('visibilitychange', () => { if (!document.hidden) refresh(); }); + return { show }; +} diff --git a/web/static/index.html b/web/static/index.html index b012b3c..8e2b5cc 100644 --- a/web/static/index.html +++ b/web/static/index.html @@ -12,8 +12,13 @@ <a class="brand" href="/" data-path="/">cloud9</a> <div class="description">A web interface to your file server.</div> </header> - <nav class="toolbar" aria-label="File actions"> - <a class="selected" href="/" data-path="/">files</a> + <nav class="toolbar" id="views" aria-label="Views"> + <a class="selected" id="view-files" href="/" data-view="files" aria-current="page">files</a> + <a id="view-sites" href="/?view=sites" data-view="sites" hidden>sites</a> + <a id="view-services" href="/?view=services" data-view="services" hidden>services</a> + </nav> + <div id="files-view"> + <nav class="toolbar file-tools" aria-label="File actions"> <button id="reload" type="button">reload</button> <button id="newfile" type="button">new file</button> <button id="newdir" type="button">new folder</button> @@ -66,6 +71,41 @@ </section> </main> </div> + </div> + <main id="sites-view" class="catalog-view" aria-label="Sites" hidden> + <div class="summary"> + <p id="sites-status" role="status" aria-live="polite">Loading sites…</p> + <button id="sites-reload" type="button">reload</button> + </div> + <div class="table-scroll"> + <table> + <thead><tr><th scope="col">Site</th><th scope="col">Status</th><th scope="col">Last build</th><th scope="col">Actions</th></tr></thead> + <tbody id="sites-entries"></tbody> + </table> + </div> + <p id="sites-empty" class="muted" hidden>No sites configured.</p> + </main> + <main id="services-view" class="catalog-view" aria-label="Services" hidden> + <div class="summary"> + <p id="services-status" role="status" aria-live="polite">Loading services…</p> + <button id="services-reload" type="button">reload</button> + </div> + <p class="muted">Listeners identified from process metadata, without endpoint probes. Local-only links work on this computer.</p> + <div class="table-scroll"> + <table> + <thead><tr><th scope="col">Service</th><th scope="col">Address</th><th scope="col">Process</th><th scope="col">Evidence</th></tr></thead> + <tbody id="services-entries"></tbody> + </table> + </div> + <p id="services-empty" class="muted" hidden>No HTTP services identified.</p> + <details id="other-listeners"> + <summary id="other-listeners-title">Other listeners</summary> + <div class="table-scroll"><table> + <thead><tr><th scope="col">Process</th><th scope="col">Address</th><th scope="col">PID</th><th scope="col">Evidence</th></tr></thead> + <tbody id="other-listeners-entries"></tbody> + </table></div> + </details> + </main> <footer>served by cloud9</footer> </body> </html> diff --git a/web/static/style.css b/web/static/style.css index 965406b..53ccda3 100644 --- a/web/static/style.css +++ b/web/static/style.css @@ -26,6 +26,21 @@ header { padding: 0 8px 17px; } font-size: 15px; } .toolbar .selected { background: #ccc; color: #111; } +.file-tools { border-bottom: 1px solid #ddd; padding-top: 7px; } +.catalog-view { padding-top: 13px; } +.catalog-view .summary p { margin: 0; color: #777; } +.catalog-view .summary .error { color: #a22; } +.catalog-view td { padding-top: 8px; padding-bottom: 8px; } +.catalog-view .site-name { font-weight: bold; } +.catalog-view .site-root { display: block; color: #777; font: 12px/1.6 monospace; overflow-wrap: anywhere; } +.catalog-view .site-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 5px 9px; font-size: 12px; } +.catalog-view .site-phase { white-space: nowrap; } +.catalog-view .site-phase.failed { color: #a22; } +.catalog-view .listener-address { font-family: monospace; overflow-wrap: anywhere; } +.catalog-view .muted { color: #777; font-size: 12px; } +.table-scroll { overflow-x: auto; } +#other-listeners { margin-top: 20px; } +#other-listeners summary { cursor: pointer; padding-bottom: 8px; color: #777; } button, input, textarea { font: inherit; color: inherit; } button { cursor: pointer; @@ -90,6 +105,9 @@ footer { margin: 30px 8px 0; padding-top: 7px; border-top: 1px solid #ddd; text- a:focus-visible, button:focus-visible { outline: 2px solid #07539b; outline-offset: 2px; } [hidden] { display: none !important; } @media (max-width: 600px) { + #sites-view .site-root, #sites-view th:nth-child(3), #sites-view td:nth-child(3) { display: none; } + .catalog-view .site-phase { white-space: normal; } + #services-view table { min-width: 600px; } body { padding: 14px 10px; } header { padding-left: 3px; } .brand { font-size: 26px; } |
