2023-03-11 17:40:38 -05:00
|
|
|
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]>)> {
|
2023-03-12 00:54:16 -05:00
|
|
|
eprintln!("file: {file:?}");
|
2023-03-11 17:40:38 -05:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2023-03-12 00:54:16 -05:00
|
|
|
[".html", "/index.html"].iter().find_map(|ext| {
|
2023-03-11 17:50:14 -05:00
|
|
|
Asset::get(&format!("{}{}", file.display().to_string(), ext))
|
|
|
|
.map(|a| (ContentType::HTML, a.data))
|
2023-03-11 17:40:38 -05:00
|
|
|
})
|
|
|
|
}
|
2023-03-12 00:54:16 -05:00
|
|
|
|
|
|
|
#[get("/")]
|
|
|
|
pub(crate) fn index() -> Option<(ContentType, Cow<'static, [u8]>)> {
|
|
|
|
Asset::get(&"index.html").map(|a| (ContentType::HTML, a.data))
|
|
|
|
}
|