Add assets route
This commit is contained in:
22
src/controllers/assets_controller.rs
Normal file
22
src/controllers/assets_controller.rs
Normal 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
1
src/controllers/mod.rs
Normal file
@ -0,0 +1 @@
|
||||
pub mod assets_controller;
|
Reference in New Issue
Block a user