aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-01-14 20:01:33 +0100
committerHarald Eilertsen <haraldei@anduin.net>2018-01-14 20:01:33 +0100
commit099624218ca84503d31c0f4c21139138d55d0630 (patch)
tree421a4c1ac81fca011ae4752c50246a86f520a277
parent47e46a6e00c47622e99cfd65083887b3dce02716 (diff)
downloadrocket-blog-099624218ca84503d31c0f4c21139138d55d0630.tar.gz
rocket-blog-099624218ca84503d31c0f4c21139138d55d0630.tar.bz2
rocket-blog-099624218ca84503d31c0f4c21139138d55d0630.zip
Serve static files from public directory if no other route match.
-rw-r--r--src/controllers/home_controller.rs10
-rw-r--r--src/main.rs3
2 files changed, 12 insertions, 1 deletions
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<IndexTemplate> {
fn index_json(conn: utils::DbConn) -> Json<Vec<models::Post>> {
Json(models::Post::get_all(conn))
}
+
+//
+// Serve files from the public directory if no routes matches.
+//
+#[get("/<file..>", rank = 99)]
+fn public_file(file: PathBuf) -> Option<rocket::response::NamedFile> {
+ 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,