From 099624218ca84503d31c0f4c21139138d55d0630 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 14 Jan 2018 20:01:33 +0100 Subject: Serve static files from public directory if no other route match. --- src/controllers/home_controller.rs | 10 ++++++++++ src/main.rs | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs index 563be40..6423a30 100644 --- a/src/controllers/home_controller.rs +++ b/src/controllers/home_controller.rs @@ -1,5 +1,7 @@ use ::{models, utils}; +use rocket; use rocket_contrib::Json; +use std::path::PathBuf; #[derive(BartDisplay, Serialize)] #[template = "templates/index.html"] @@ -23,3 +25,11 @@ fn index(conn: utils::DbConn) -> utils::Page { fn index_json(conn: utils::DbConn) -> Json> { Json(models::Post::get_all(conn)) } + +// +// Serve files from the public directory if no routes matches. +// +#[get("/", rank = 99)] +fn public_file(file: PathBuf) -> Option { + rocket::response::NamedFile::open(PathBuf::from("public/").join(file)).ok() +} diff --git a/src/main.rs b/src/main.rs index 4e96a65..4876b35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,7 +26,8 @@ fn main() { .mount("/", routes![ home_controller::index, - home_controller::index_json]) + home_controller::index_json, + home_controller::public_file]) .mount("/posts", routes![ posts_controller::new, -- cgit v1.2.3