gitdataai/libs/frontend/src/lib.rs
ZhenYi 215846b1db feat(api): pre-compress static assets with brotli and gzip
- build.rs: generate .gz and .br compressed variants at build time,
  embed both into the FRONTEND constant
- dist.rs: content-type for SPA fallback uses index.html path explicitly,
  explicit extension mapping for Vite content-hashed filenames
- frontend lib: get_frontend_asset_compressed returns (data, encoding, etag)
2026-04-25 20:09:09 +08:00

39 lines
1.5 KiB
Rust

//! Embedded frontend static assets, built via pnpm and embedded at compile time.
include!(concat!(env!("OUT_DIR"), "/frontend.rs"));
/// Returns the embedded frontend static asset for the given path, or `None` if not found.
pub fn get_frontend_asset(path: &str) -> Option<&'static [u8]> {
FRONTEND.iter().find(|(k, _, _, _, _)| *k == path).map(|(_, v, _, _, _)| *v)
}
/// Returns the embedded frontend static asset and its ETag for the given path.
pub fn get_frontend_asset_with_etag(path: &str) -> Option<(&'static [u8], &'static str)> {
FRONTEND
.iter()
.find(|(k, _, _, _, _)| *k == path)
.map(|(_, v, etag, _, _)| (v as &[u8], *etag))
}
/// Returns the best compressed variant (brotli > gzip) and its ETag, if smaller than uncompressed.
/// Returns `(data, encoding, etag)` — `encoding` is "" when no compressed variant exists.
pub fn get_frontend_asset_compressed(path: &str) -> Option<(&'static [u8], &'static str, &'static str)> {
FRONTEND
.iter()
.find(|(k, _, _, _, _)| *k == path)
.map(|(_, v, etag, brotli, gzip)| {
// brotli preferred; fall back to gzip; fall back to uncompressed
if let Some((ref data, enc)) = *brotli {
if !enc.is_empty() {
return (data.as_slice(), enc, *etag);
}
}
if let Some((ref data, enc)) = *gzip {
if !enc.is_empty() {
return (data.as_slice(), enc, *etag);
}
}
(v as &[u8], "", *etag)
})
}