diff --git a/Cargo.lock b/Cargo.lock index 791cc2f..2ce9f65 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2041,6 +2041,7 @@ dependencies = [ "im-client", "in-client", "in-core", + "reqwest", "serde", "serde_json", "sha2 0.11.0", diff --git a/crates/in-web/Cargo.toml b/crates/in-web/Cargo.toml index 3213e2d..42dbbbf 100644 --- a/crates/in-web/Cargo.toml +++ b/crates/in-web/Cargo.toml @@ -41,6 +41,8 @@ http-body = "1" # this binary (see `server.rs`'s `stylesheet_guard`). sha2 = "0.11" ulid = { workspace = true } +# The wordmark flyout probes each sibling's `/healthz` where it stands. +reqwest = { version = "0.13.5", default-features = false, features = ["rustls"] } [build-dependencies] grass = "0.13" diff --git a/crates/in-web/src/health.rs b/crates/in-web/src/health.rs new file mode 100644 index 0000000..d7de382 --- /dev/null +++ b/crates/in-web/src/health.rs @@ -0,0 +1,39 @@ +//! The one family-health probe, shared by the signed-in wordmark's flyout: +//! a service is asked `GET {url}/healthz` where it stands — no credentials, +//! two seconds to answer — and the callers fan the probes out concurrently, +//! so a family member that is down costs its two seconds, not two seconds +//! each. + +/// One `/healthz` reading. `Up` carries the body — the deploy contract's +/// `ok ` — and the answer's latency; everything else, refused +/// or wrong status or a body that does not begin ok or the two-second +/// ceiling, is `Down`. +pub enum Probe { + Up { body: String, ms: u128 }, + Down, +} + +pub(crate) async fn probe_healthz(http: &reqwest::Client, url: &str) -> Probe { + let started = std::time::Instant::now(); + let Ok(answer) = http + .get(url) + .timeout(std::time::Duration::from_secs(2)) + .send() + .await + else { + return Probe::Down; + }; + if !answer.status().is_success() { + return Probe::Down; + } + let body = answer.text().await.unwrap_or_default(); + let body = body.trim(); + if body.starts_with("ok") { + Probe::Up { + body: body.chars().take(64).collect(), + ms: started.elapsed().as_millis(), + } + } else { + Probe::Down + } +} diff --git a/crates/in-web/src/layout.rs b/crates/in-web/src/layout.rs index c6efc2c..430efd2 100644 --- a/crates/in-web/src/layout.rs +++ b/crates/in-web/src/layout.rs @@ -20,6 +20,7 @@ use topcoat::{ use in_core::store::User; +use crate::health::{Probe, probe_healthz}; use crate::i18n::{Key, Lang, t}; use crate::server::{app, current_user}; @@ -248,8 +249,10 @@ async fn family_of(cx: &Cx) -> Vec { /// sibling services' wordmarks in a flyout that opens under it on hover /// and on keyboard focus — the mark is an ``, so it is already /// focusable and `:focus-within` needs nothing added. Middots between, -/// each a plain link to the service's own address, so the cross-origin -/// hop navigates the browser natively. No script: the flyout is CSS on +/// each a plain link to the service's own address, carrying its health +/// dot — the same `/healthz` reading: green while the sibling answers +/// `ok`, muted while it does not. The cross-origin hop navigates the +/// browser natively. No script: the flyout is CSS on /// `:hover` / `:focus-within`, and it stays in the DOM at opacity zero so /// Tab reaches its links. A family that is absent, or holds no one but /// this app, renders the bare mark — there is nothing to reveal. @@ -262,29 +265,74 @@ async fn family_mark<'a>(cx: &'a Cx) -> Result { if siblings.is_empty() { return mark(cx).await.map(|v| v.boxed()); } + // Every probe at once: a family member that is down costs its two + // seconds, not two seconds each. + let http = reqwest::Client::new(); + let mut probes = Vec::new(); + for service in &siblings { + let http = http.clone(); + let url = format!("{}/healthz", service.url.trim_end_matches('/')); + probes.push(tokio::spawn(async move { probe_healthz(&http, &url).await })); + } + let mut rows = Vec::new(); + for (service, probe) in siblings.iter().zip(probes) { + let probe = probe.await.unwrap_or(Probe::Down); + rows.push(( + service.key.as_str(), + service.url.as_str(), + service.name.as_str(), + probe, + )); + } + let marks = switcher_marks(rows); Ok(view! { cx => } .boxed()) } +/// The flyout's marks as final HTML, pure over resolved probes: in's own +/// row filtered out, every sibling a plain link carrying its probe's dot +/// — `health-on` while it answers `ok`, `health-off` while it does not — +/// middots between. Dots only: the probe's body and latency never reach +/// the chrome. Extracted from `family_mark` so the tests pin this exact +/// markup without a router or an asset bundle. +pub fn switcher_marks<'a>( + rows: impl IntoIterator, +) -> String { + rows.into_iter() + .filter(|(key, _, _, _)| *key != SELF_KEY) + .map(|(key, url, name, probe)| { + let key = escape(key); + let url = escape(url); + let name = escape(name); + let dot = match probe { + Probe::Up { .. } => "health-on", + Probe::Down => "health-off", + }; + format!( + r#"{key}"# + ) + }) + .collect::>() + .join(r#"·"#) +} + +/// Everything the flyout's marks interpolate crosses them as raw HTML — +/// key, address, title — so each passes through `escape` first: the same +/// discipline im's chrome keeps, anything the family list carries is +/// escaped before it reaches the markup. +fn escape(raw: &str) -> String { + raw.replace('&', "&") + .replace('<', "<") + .replace('>', ">") + .replace('"', """) +} + /// The signed-in chrome: the monogram with the family behind it, the page /// nav, the identity menu. Wave-2 pages render their content under this. pub async fn topbar<'a>( diff --git a/crates/in-web/src/lib.rs b/crates/in-web/src/lib.rs index f0518e9..9637577 100644 --- a/crates/in-web/src/lib.rs +++ b/crates/in-web/src/lib.rs @@ -7,6 +7,7 @@ pub mod avatar; pub mod drive; pub mod dropdown; pub mod files; +pub mod health; pub mod i18n; pub mod layout; pub mod live; diff --git a/crates/in-web/tests/http.rs b/crates/in-web/tests/http.rs index fd4e1b0..d824722 100644 --- a/crates/in-web/tests/http.rs +++ b/crates/in-web/tests/http.rs @@ -3267,10 +3267,121 @@ async fn the_family_flyout_holds_the_siblings_when_the_family_is_mirrored() { assert!(nav.contains("title=\"Account\""), "{nav}"); assert_eq!(nav.matches("app-switcher-mark").count(), 2, "{nav}"); assert_eq!(nav.matches("app-switcher-sep").count(), 1, "{nav}"); + // Both siblings sit at example.com addresses nothing answers — the + // probes fail inside their two-second ceiling, concurrently — so both + // read Off. The flyout carries dots only, one per mark. + assert!(nav.contains("health-dot health-off"), "{nav}"); + assert!(!nav.contains("health-on"), "{nav}"); + assert_eq!(nav.matches("health-dot").count(), 2, "{nav}"); // The flyout hangs off the monogram, not the page nav. assert!(body.contains("wordmark-family"), "{body}"); } +/// The flyout marks each sibling with its probe's reading: a sibling +/// answering `ok` renders `health-on`, a dark port `health-off`, dots +/// only — no probe body, no latency — and in's own row stays out. The +/// markup itself is pinned router-free in +/// `the_family_flyout_marks_pure_html` below. +#[tokio::test] +async fn the_family_flyout_marks_siblings_with_health_dots() { + use tokio::io::{AsyncReadExt as _, AsyncWriteExt as _}; + + // A live sibling answering the deploy body, and a dark one. + let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let live_url = format!("http://{}", listener.local_addr().unwrap()); + let dark = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap(); + let dark_url = format!("http://{}", dark.local_addr().unwrap()); + drop(dark); + // `in` gets a row too — one the flyout must leave out. + let app = TestApp::build_with( + None, + &[ + ("in", "Files", "https://files.example.com"), + ("iz", "Board", &live_url), + ("im", "Account", &dark_url), + ], + ) + .await; + let cookie = app + .sign_in("sub-switch-dot", "switchdot@in.test", "SwitchDot") + .await; + let talker = tokio::spawn(async move { + let (mut sock, _) = listener.accept().await.unwrap(); + let mut buf = [0u8; 1024]; + let _ = sock.read(&mut buf).await; + sock.write_all(b"HTTP/1.1 200 OK\r\ncontent-length: 6\r\n\r\nok dev") + .await + .unwrap(); + }); + let page = app.get("/drive", Some(&cookie)).await; + assert_eq!(page.status, StatusCode::OK, "{}", page.text()); + talker.await.unwrap(); + let body = page.text(); + let nav_start = body.find("app-switcher").expect("no switcher"); + let nav_end = body[nav_start..] + .find("") + .expect("switcher never closes"); + let nav = &body[nav_start..nav_start + nav_end]; + assert!( + nav.contains(r#"iz"), "the live sibling renders: {nav}"); + assert!( + nav.contains("health-dot health-off"), + "the dark sibling reads off: {nav}" + ); + assert!(nav.contains(">im"), "the dark sibling renders: {nav}"); + assert!(!nav.contains(">in"), "in's own row stays out: {nav}"); + assert!(!body.contains("ok dev"), "the flyout carries dots only: {body}"); + assert!(!nav.contains(" ms"), "the flyout carries no latency: {nav}"); +} + +/// The CI gate for the flyout markup: `switcher_marks` is the exact HTML +/// the flyout renders, so the probe-to-dot mapping and in's omission are +/// pinned without a router or an asset bundle — which is where the +/// renders above get skipped. Up reads `health-on`, Down `health-off`, +/// the keys stay links with their titles, and the probe's body and +/// latency never reach the chrome. +#[test] +fn the_family_flyout_marks_pure_html() { + use in_web::health::Probe; + + let marks = in_web::layout::switcher_marks([ + ("iz", "http://127.0.0.1:9001", "Board", Probe::Up { body: "ok dev".into(), ms: 12 }), + ("im", "http://127.0.0.1:9002", "Account", Probe::Down), + // in's own row goes in; it must not come back out. + ("in", "http://127.0.0.1:7655", "Files", Probe::Up { body: "ok dev".into(), ms: 1 }), + ]); + assert!( + marks.contains( + r#""# + ), + "the marks stay links: {marks}" + ); + assert!( + marks.contains("health-dot health-on"), + "the ok sibling reads on: {marks}" + ); + assert!(marks.contains(">iz"), "the live sibling renders: {marks}"); + assert!( + marks.contains("health-dot health-off"), + "the dark sibling reads off: {marks}" + ); + assert!(marks.contains(">im"), "the dark sibling renders: {marks}"); + assert!(!marks.contains(">in"), "in's own row stays out: {marks}"); + assert!(!marks.contains("ok dev"), "the flyout carries dots only: {marks}"); + assert!(!marks.contains(" ms"), "the flyout carries no latency: {marks}"); + assert!( + marks.contains(r#"·"#), + "the marks join on middots: {marks}" + ); +} + /// The `back` the sign-out hands im is read per sign-out: an origin saved /// on the running install — the `base_url` setting the settings panel /// writes — takes effect on the very next exit, without a restart. Until diff --git a/style/main.scss b/style/main.scss index b967c76..77c656f 100644 --- a/style/main.scss +++ b/style/main.scss @@ -503,8 +503,11 @@ a { // from a row the flyout hangs across. z-index: 50; display: flex; - align-items: baseline; - gap: 10px; + align-items: center; + // The flyout shrink-fits to the monogram's width; without this the + // health dot wraps off its key. It grows right instead, over the page + // nav, under the house dropdown's panel. + white-space: nowrap; padding: 6px 12px; background: var(--surface); border: 1px solid var(--border); @@ -528,6 +531,21 @@ a { &-sep { color: var(--muted-soft); } } + +// The wordmark flyout's per-service state dot: green while the service's +// /healthz answers ok, muted when it does not — the same light iz's +// connection card and im's flyout carry, one green across the family. +.health-dot { + display: inline-block; + width: 0.55rem; + height: 0.55rem; + border-radius: 50%; + margin-right: 0.4rem; + vertical-align: baseline; + + &.health-on { background: #2e9e5b; } + &.health-off { background: var(--muted); } +} .scaffold-note { margin: 24px; padding: 20px 24px;