this_algorithm/web/src/static_assets.rs

30 lines
858 B
Rust
Raw Normal View History

use rocket::get;
use rocket::http::ContentType;
use std::borrow::Cow;
use std::ffi::OsStr;
use std::path::PathBuf;
use web_frontend::Asset;
#[get("/<file..>")]
pub(crate) fn dist(file: PathBuf) -> Option<(ContentType, Cow<'static, [u8]>)> {
2023-03-24 16:20:56 -04:00
if let Some(a) = Asset::get(&format!("{}", file.display())) {
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));
}
2023-03-12 00:54:16 -05:00
[".html", "/index.html"].iter().find_map(|ext| {
2023-03-25 00:50:56 -04:00
Asset::get(&format!("{}{}", file.display(), ext)).map(|a| (ContentType::HTML, a.data))
})
}
2023-03-12 00:54:16 -05:00
#[get("/")]
pub(crate) fn index() -> Option<(ContentType, Cow<'static, [u8]>)> {
2023-03-24 16:20:56 -04:00
Asset::get("index.html").map(|a| (ContentType::HTML, a.data))
2023-03-12 00:54:16 -05:00
}