ci / rust (push) Failing after 3m2s
build.rs now builds web/dist before include_dir! evaluates, so the checked-in .gitkeep placeholder is no longer needed for a fresh checkout to compile (verified by building with web/dist entirely absent). Ignore dist/ wholesale and update the static_bundle doc comment. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
107 lines
3.4 KiB
Rust
107 lines
3.4 KiB
Rust
//! Embedded `web/dist` SPA bundle.
|
|
//!
|
|
//! The build of `web/` is consumed at compile time via `include_dir!`, which
|
|
//! requires the directory to exist. `build.rs` runs `pnpm build` before the
|
|
//! crate compiles, so `web/dist` is always present and populated — even on a
|
|
//! fresh checkout (`web/dist` is gitignored and never committed). The
|
|
//! placeholder below is therefore defensive only; a successful build always
|
|
//! embeds the real bundle.
|
|
|
|
use axum::body::Body;
|
|
use axum::http::{header, StatusCode};
|
|
use axum::response::{IntoResponse, Response};
|
|
use include_dir::{include_dir, Dir};
|
|
|
|
static BUNDLE: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../../web/dist");
|
|
|
|
/// Serve `/index.html`, falling back to a tiny placeholder when the bundle
|
|
/// hasn't been built yet. Used both for `GET /` and SPA-route fallthrough
|
|
/// (so refreshing on `#/sessions/foo` doesn't 404).
|
|
pub fn serve_index() -> Response {
|
|
match BUNDLE.get_file("index.html") {
|
|
Some(f) => html_response(f.contents()),
|
|
None => placeholder(),
|
|
}
|
|
}
|
|
|
|
/// Serve any path beneath the bundle (e.g. `assets/index-deadbeef.js`).
|
|
pub fn serve_path(path: &str) -> Response {
|
|
let trimmed = path.trim_start_matches('/');
|
|
if trimmed.is_empty() {
|
|
return serve_index();
|
|
}
|
|
match BUNDLE.get_file(trimmed) {
|
|
Some(f) => {
|
|
let mime = mime_guess::from_path(trimmed).first_or_octet_stream();
|
|
(
|
|
[(header::CONTENT_TYPE, mime.as_ref())],
|
|
f.contents().to_vec(),
|
|
)
|
|
.into_response()
|
|
}
|
|
// SPA fallback: any unknown path falls through to index.html so the
|
|
// client-side router can resolve the route.
|
|
None => serve_index(),
|
|
}
|
|
}
|
|
|
|
fn html_response(bytes: &[u8]) -> Response {
|
|
(
|
|
[(header::CONTENT_TYPE, "text/html; charset=utf-8")],
|
|
bytes.to_vec(),
|
|
)
|
|
.into_response()
|
|
}
|
|
|
|
fn placeholder() -> Response {
|
|
(
|
|
StatusCode::OK,
|
|
[(header::CONTENT_TYPE, "text/html; charset=utf-8")],
|
|
Body::from(PLACEHOLDER_HTML),
|
|
)
|
|
.into_response()
|
|
}
|
|
|
|
const PLACEHOLDER_HTML: &str = r#"<!doctype html>
|
|
<meta charset="utf-8">
|
|
<title>maestro-web — bundle not built</title>
|
|
<style>
|
|
body { font-family: ui-sans-serif, system-ui, sans-serif; max-width: 40em; margin: 4em auto; padding: 0 1em; line-height: 1.5; }
|
|
code { background: #f3f3f3; padding: 0.1em 0.3em; border-radius: 3px; }
|
|
</style>
|
|
<h1>maestro-web is running</h1>
|
|
<p>but the frontend bundle hasn't been built yet. Run:</p>
|
|
<pre><code>pnpm -C web install
|
|
pnpm -C web build
|
|
cargo build -p maestro-web --release</code></pre>
|
|
<p>or, from the repo root: <code>just web</code>.</p>
|
|
"#;
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn placeholder_is_html() {
|
|
let resp = serve_index();
|
|
// Either the real index or the placeholder — both must be HTML.
|
|
let ct = resp
|
|
.headers()
|
|
.get(header::CONTENT_TYPE)
|
|
.and_then(|v| v.to_str().ok())
|
|
.unwrap_or("");
|
|
assert!(ct.starts_with("text/html"), "ct={ct}");
|
|
}
|
|
|
|
#[test]
|
|
fn unknown_path_falls_back_to_index() {
|
|
let resp = serve_path("/some/missing/route");
|
|
let ct = resp
|
|
.headers()
|
|
.get(header::CONTENT_TYPE)
|
|
.and_then(|v| v.to_str().ok())
|
|
.unwrap_or("");
|
|
assert!(ct.starts_with("text/html"), "ct={ct}");
|
|
}
|
|
}
|