30 lines
858 B
Rust
30 lines
858 B
Rust
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]>)> {
|
|
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));
|
|
}
|
|
|
|
[".html", "/index.html"].iter().find_map(|ext| {
|
|
Asset::get(&format!("{}{}", file.display(), 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))
|
|
}
|