Add assets route

This commit is contained in:
2022-03-30 09:40:46 +02:00
parent 6d8b8979ca
commit d75242d213
8 changed files with 12268 additions and 27 deletions

View File

@ -0,0 +1,22 @@
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()))
}
}
}

1
src/controllers/mod.rs Normal file
View File

@ -0,0 +1 @@
pub mod assets_controller;