~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/hud

A remote MCP server that shows atproto records as interactive UI inside MCP Apps hosts

0
0
63 lines · 2.8 KB Raw History
//! Embeds what `web/` builds: the view, `web/dist/index.html`, one
//! self-contained HTML file that `resources/read` of the view's URI answers
//! with; the workspace's page, `web/dist/workspace.html`, which
//! `GET /workspace` answers with; the playground's,
//! `web/dist/playground.html`, which `GET /playground` answers with; and
//! HUD's icon, `web/dist/favicon.svg` (from `web/public/`), which
//! `GET /favicon.svg` answers with. The view's
//! URI carries a hash of its file (`ui://records/view/<hash>`), so a host
//! that keeps a resource it read never draws a new server's results with an
//! old view: a new view is a new resource. A build without any of these
//! files fails, saying how to build `web/` first.

#![forbid(unsafe_code)]

use std::{env, fs, path::Path, path::PathBuf};

const BUILD_WEB: &str = "build the view first: `cd web && npm ci && npm run build`";

/// FNV-1a, 64 bits: enough to tell one build of the view from another.
fn fnv1a(bytes: &[u8]) -> u64 {
    bytes.iter().fold(0xcbf2_9ce4_8422_2325, |h, b| {
        (h ^ u64::from(*b)).wrapping_mul(0x0000_0100_0000_01b3)
    })
}

/// A file `web/` builds, by its name in `web/dist/`.
fn built(name: &str) -> PathBuf {
    let path = Path::new(&env::var("CARGO_MANIFEST_DIR").unwrap())
        .join("../../web/dist")
        .join(name);
    println!("cargo:rerun-if-changed={}", path.display());
    path.canonicalize()
        .unwrap_or_else(|e| panic!("web/dist/{name}: {e}; {BUILD_WEB}"))
}

fn main() {
    let view = built("index.html");
    let workspace = built("workspace.html");
    let playground = built("playground.html");
    let favicon = built("favicon.svg");
    let hash = fnv1a(&fs::read(&view).unwrap());
    let to = Path::new(&env::var("OUT_DIR").unwrap()).join("view.rs");
    fs::write(
        to,
        format!(
            "/// The view, as `web/` builds it: one HTML file, its script and styles inline.\n\
             pub const VIEW_HTML: &str = include_str!({:?});\n\
             /// The view's resource URI, versioned by its content.\n\
             pub const VIEW_URI: &str = \"ui://records/view/{hash:016x}\";\n\
             /// The workspace's page, as `web/` builds it: one HTML file, its script and styles inline.\n\
             pub const WORKSPACE_HTML: &str = include_str!({:?});\n\
             /// The playground's page, as `web/` builds it: one HTML file, its script and styles inline.\n\
             pub const PLAYGROUND_HTML: &str = include_str!({:?});\n\
             /// HUD's icon, as `web/` copies it from `web/public/`: an SVG.\n\
             pub const FAVICON_SVG: &str = include_str!({:?});\n",
            view.display().to_string(),
            workspace.display().to_string(),
            playground.display().to_string(),
            favicon.display().to_string()
        ),
    )
    .unwrap();
}