this_algorithm/web/src/static_assets.rs
2023-03-12 00:54:16 -05:00

36 lines
991 B
Rust

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]>)> {
eprintln!("file: {file:?}");
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| {
Asset::get(&format!("{}{}", file.display().to_string(), ext))
.map(|a| (ContentType::HTML, a.data))
})
}
#[get("/")]
pub(crate) fn index() -> Option<(ContentType, Cow<'static, [u8]>)> {
Asset::get(&"index.html").map(|a| (ContentType::HTML, a.data))
}