pub struct Assets { /* private fields */ }Expand description
A surface’s asset directory, with what has been read from it: the Cockpit’s
share/cockpit/ under /cockpit/assets/, or another surface’s beside it (ADR 0135).
Implementations§
Source§impl Assets
impl Assets
Sourcepub fn new(share_dir: &Path) -> Self
pub fn new(share_dir: &Path) -> Self
The assets under share_dir/cockpit, when that directory exists. A distribution
without one serves no assets and every page still renders: the stylesheet link and
the module scripts simply resolve to 404, and the Cockpit degrades to unstyled
server-rendered HTML rather than failing.
Sourcepub fn under(share_dir: &Path, dir: &'static str, prefix: &'static str) -> Self
pub fn under(share_dir: &Path, dir: &'static str, prefix: &'static str) -> Self
The assets under share_dir/<dir>, served under prefix: one surface’s files, read
and digested exactly as the Cockpit’s are. A surface does not get a second file
server; it gets this one pointed at its own directory.
use majordomus_cli::cockpit::assets::Assets;
let dir = tempfile::tempdir().unwrap();
std::fs::create_dir_all(dir.path().join("ui")).unwrap();
std::fs::write(dir.path().join("ui/ui.css"), "body{}").unwrap();
let ui = Assets::under(dir.path(), "ui", "/ui/assets/");
assert!(ui.url("ui.css").starts_with("/ui/assets/ui.css?v="));
assert!(!Assets::new(dir.path()).present(), "the Cockpit's directory is another one");Sourcepub fn none_under(dir: &'static str, prefix: &'static str) -> Self
pub fn none_under(dir: &'static str, prefix: &'static str) -> Self
No asset directory, for a surface whose files would live under dir and prefix:
nothing is servable, and every URL keeps its plain path under that surface’s prefix,
so a missing file is a 404 that names where it was looked for.
use majordomus_cli::cockpit::assets::Assets;
let ui = Assets::none_under("ui", "/ui/assets/");
assert!(!ui.present());
assert!(!ui.has("ui.css"));
assert_eq!(ui.url("ui.css"), "/ui/assets/ui.css");Sourcepub fn url(&self, name: &str) -> String
pub fn url(&self, name: &str) -> String
The URL a page emits for an asset: the path with the digest of its bytes, so the URL changes exactly when the file does. An asset that is not there keeps its plain path, so the 404 names the file a reader should look for.
Sourcepub fn respond(&self, name: &str, req: &Request) -> Response
pub fn respond(&self, name: &str, req: &Request) -> Response
Answer a request for prefix + name. Immutable only when the request’s v is
exactly the version the URL a page emits carries; anything else (no v, an empty
one, a prefix, a stale one) revalidates. An If-None-Match naming the current
ETag is answered 304 Not Modified with no body: the 200’s validator, caching
and length, and nothing else crosses the wire.