[web] The web UI: hosts, add/edit with apply progress, undo, history and restore, share links, engine switch, certificates and device installs, signing by hand, settings and tokens; smoke test counts every §4 flow's clicks

This commit is contained in:
2026-11-07 23:44:27 +01:00
parent aed5fbfcff
commit 3e326e85a0
47 changed files with 7868 additions and 3 deletions
@@ -0,0 +1,107 @@
<!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at https://mozilla.org/MPL/2.0/. -->
<script lang="ts">
// Handoff §9: the engine popover. Click 1 is the chip, click 2 "Switch to Caddy" (the popover
// is the confirmation). The switch runs in control; this polls GET /engine until it ends.
import { onDestroy } from "svelte";
import { Banner, Button, Dialog, Icon, Progress } from "@ianitor/ui";
import { call, get, newKey, sentence } from "../lib/api";
import { app, refresh } from "../lib/app.svelte";
import { when } from "../lib/format";
import type { EngineView } from "../lib/types";
let { onclose }: { onclose: () => void } = $props();
const e = $derived(app.overview?.engine ?? null);
const running = $derived(e?.running ?? "pingora");
const name = $derived(running === "caddy" ? "Caddy" : "Pingora");
const other = $derived(running === "caddy" ? "Pingora" : "Caddy");
const target = $derived(running === "caddy" ? "pingora" : "caddy");
let switching = $state(false);
let problem = $state<string | null>(null);
let timer: ReturnType<typeof setTimeout> | undefined;
onDestroy(() => clearTimeout(timer));
const activeLinks = $derived(app.overview?.hosts.filter((h) => h.share_links).length ?? 0);
const notReady = $derived(running === "pingora" && !e?.fallback_ready?.built_at);
const disabledReason = $derived(!e?.switchable ? "This server does not let Ianitor switch engines." : notReady ? "Caddy is not ready: its settings have not been built yet." : null);
async function doSwitch() {
problem = null;
switching = true;
const from = name;
const to = other;
try {
await call("POST", "/engine", { target }, { idem: newKey() });
const poll = async () => {
try {
const v = await get<EngineView>("/engine", { quiet: true });
if (v.state === "switching") {
timer = setTimeout(poll, 1000);
return;
}
switching = false;
await refresh();
if (v.last_error) {
app.switchResult = { ok: false, title: `${to} did not start, so ${from} is serving again.`, text: "Visitors had a short break and nothing else changed.", detail: v.last_error.detail ?? null };
} else {
app.switchResult = { ok: true, title: `Now serving with ${to}.`, text: "Switch back from the engine chip at any time; it is the same 2 clicks." };
}
onclose();
} catch {
// Contact may drop for a few seconds while the engines change (§9.2).
timer = setTimeout(poll, 1000);
}
};
timer = setTimeout(poll, 500);
} catch (err) {
switching = false;
problem = sentence(err);
}
}
</script>
<Dialog title="Serving with {name}" kind="popover" modal={false} {onclose}>
<p class="ia-label">Engine</p>
{#if e?.state === "stopped"}
<p><strong>{name} is not answering.</strong> Visitors may not reach any host. Switching to {other} is the quickest way back.</p>
{:else}
<p class="ia-text2">
{#if e?.since}Since {when(e.since, app.now)}. {/if}
{#if running === "pingora"}
{#if notReady}Caddy is not ready: its settings have not been built yet.{:else}Caddy is ready with the same routes: built {when(e?.fallback_ready?.built_at, app.now)}{e?.fallback_ready?.current ? "" : " (it is rebuilt from the current routes when you switch)"}.{/if}
{:else}Pingora is ready with the same routes.{/if}
</p>
{/if}
<div class="ia-card">
<p><strong>What happens</strong></p>
<ul>
<li>{name} stops and {other} starts. New visits reach {other} in about 2 seconds.</li>
<li>Open connections close; videos and uploads in progress restart.</li>
<li>Hosts and certificates stay the same.</li>
{#if running === "pingora"}
<li>Share links stop working while Caddy serves: visitors with a link get the normal sign-in. They work again when you switch back.{#if activeLinks} Hosts with share links: {activeLinks}.{/if}</li>
<li>Changes wait while Caddy serves; switch back to make them.</li>
{:else}
<li>Share links work again, and changes can be made again.</li>
{/if}
<li>This page may lose contact for a few seconds.</li>
</ul>
</div>
{#if switching}
<p class="ia-status" data-s="running" role="status"><Icon name="clock" /><strong>Switching to {other}</strong></p>
<Progress label="Switching engine" max={1} now={0} text="{name} stopping, starting {other}" />
{/if}
{#if problem}<Banner tone="notice" live="status">{#snippet title()}{problem}{/snippet}</Banner>{/if}
{#if disabledReason}<p class="ia-hint" id="switch-reason">{disabledReason}</p>{/if}
{#snippet footer()}
<Button onclick={onclose}>Cancel</Button>
<Button variant={e?.state === "stopped" ? "primary" : "outline"} icon="swap" onclick={doSwitch} pending={switching}
disabled={!!disabledReason} describedby={disabledReason ? "switch-reason" : undefined}>Switch to {other}</Button>
{/snippet}
</Dialog>
<style>
ul { margin: 6px 0 0; padding-left: 1.1rem; color: var(--text-2); }
</style>