22 lines
721 B
Rust
22 lines
721 B
Rust
|
use std::path::PathBuf;
|
||
|
|
||
|
use include_dir::{Dir, include_dir};
|
||
|
use rocket::http::{ContentType, Status};
|
||
|
|
||
|
/// Assets directory
|
||
|
static ASSETS_DIR: Dir = include_dir!("$CARGO_MANIFEST_DIR/assets");
|
||
|
|
||
|
#[get("/<file..>")]
|
||
|
pub fn assets_route(file: PathBuf) -> (Status, (ContentType, &'static [u8])) {
|
||
|
match ASSETS_DIR.get_file(file) {
|
||
|
None =>
|
||
|
(Status::NotFound, (ContentType::Text, "404 Not found".as_bytes())),
|
||
|
Some(file) => {
|
||
|
(Status::Ok, (
|
||
|
ContentType::from_extension(file.path().extension().unwrap_or_default()
|
||
|
.to_string_lossy().as_ref())
|
||
|
.unwrap_or(ContentType::Binary),
|
||
|
file.contents()))
|
||
|
}
|
||
|
}
|
||
|
}
|