summaryrefslogtreecommitdiff
path: root/web/static/catalog.mjs
diff options
context:
space:
mode:
Diffstat (limited to 'web/static/catalog.mjs')
-rw-r--r--web/static/catalog.mjs103
1 files changed, 103 insertions, 0 deletions
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 };
+}