this_algorithm/web/src/static_assets.rs

31 lines
813 B
Rust
Raw Normal View History

use rocket::get;
use rocket::http::ContentType;
use rust_embed::RustEmbed;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::PathBuf;
#[derive(RustEmbed)]
#[folder = "../build/"]
struct Asset;
#[get("/<file..>")]
pub(crate) fn dist(file: PathBuf) -> Option<(ContentType, Cow<'static, [u8]>)> {
if let Some(a) = Asset::get(&format!("{}", file.display().to_string())) {
let content_type = file
.extension()
.and_then(OsStr::to_str)
.and_then(ContentType::from_extension)
.unwrap_or(ContentType::Bytes);
return Some((content_type, a.data));
}
[".html", "index.html"].iter().find_map(|ext| {
let p = format!("{}{}", file.display().to_string(), ext);
Asset::get(&p).map(|a| (ContentType::HTML, a.data))
})
}