use bytes::Bytes;
use http_body_util::{BodyExt, Full, combinators::BoxBody};
use hyper::{Response, StatusCode};
type ApiBody = BoxBody<Bytes, std::convert::Infallible>;
pub struct Asset {
pub route: &'static str,
pub content_type: &'static str,
pub etag: &'static str,
pub no_cache: bool,
pub gz: &'static [u8],
}
static ASSETS: &[Asset] = include!(concat!(env!("OUT_DIR"), "/webui_assets.rs"));
pub fn serve(path: &str) -> Option<Response<ApiBody>> {
let asset = ASSETS.iter().find(|a| a.route == path)?;
let cache_control = if asset.no_cache {
"no-cache"
} else {
"public, max-age=31536000, immutable"
};
Some(
Response::builder()
.status(StatusCode::OK)
.header(hyper::header::CONTENT_TYPE, asset.content_type)
.header(hyper::header::CONTENT_ENCODING, "gzip")
.header(hyper::header::CACHE_CONTROL, cache_control)
.header(hyper::header::ETAG, format!("\"{}\"", asset.etag))
.body(Full::new(Bytes::from_static(asset.gz)).boxed())
.expect("resposta estática bem-formada"),
)
}
#[cfg(test)]
mod headless_tests;