From 0a7015b2b1d1e6682385ff3c02f14c1911158a8e Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Thu, 11 Jan 2018 22:57:24 +0100 Subject: Move homepage processing to separate controller. --- src/controllers/home_controller.rs | 21 +++++++++++++++++++++ src/controllers/mod.rs | 1 + src/main.rs | 27 +++++---------------------- 3 files changed, 27 insertions(+), 22 deletions(-) create mode 100644 src/controllers/home_controller.rs diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs new file mode 100644 index 0000000..c4c0c56 --- /dev/null +++ b/src/controllers/home_controller.rs @@ -0,0 +1,21 @@ +use ::{models, utils}; +use rocket_contrib::Json; + +#[derive(BartDisplay, Serialize)] +#[template = "templates/index.html"] +struct IndexTemplate<'a> { + title: &'a str, + posts: Vec +} + +implement_responder_for!(IndexTemplate<'a>); + +#[get("/", format = "text/html")] +fn index<'a>(conn: utils::DbConn) -> IndexTemplate<'a> { + IndexTemplate { title: "Bloggen", posts: models::Post::get_all(conn) } +} + +#[get("/", format = "application/json")] +fn index_json(conn: utils::DbConn) -> Json> { + Json(models::Post::get_all(conn)) +} diff --git a/src/controllers/mod.rs b/src/controllers/mod.rs index 0a95bd5..b23cd61 100644 --- a/src/controllers/mod.rs +++ b/src/controllers/mod.rs @@ -1 +1,2 @@ +pub mod home_controller; pub mod posts_controller; diff --git a/src/main.rs b/src/main.rs index 0bbdc2e..be61d2d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,37 +12,20 @@ extern crate r2d2_diesel; extern crate rocket; extern crate rocket_contrib; -use rocket_contrib::Json; - #[macro_use] mod utils; mod models; mod schema; mod controllers; +use controllers::home_controller; use controllers::posts_controller; -#[derive(BartDisplay, Serialize)] -#[template = "templates/index.html"] -struct IndexTemplate<'a> { - title: &'a str, - posts: Vec -} - -implement_responder_for!(IndexTemplate<'a>); - -#[get("/", format = "text/html")] -fn index<'a>(conn: utils::DbConn) -> IndexTemplate<'a> { - IndexTemplate { title: "Bloggen", posts: models::Post::get_all(conn) } -} - -#[get("/", format = "application/json")] -fn index_json(conn: utils::DbConn) -> Json> { - Json(models::Post::get_all(conn)) -} - fn main() { rocket::ignite() .manage(utils::init_db_pool()) - .mount("/", routes![index, index_json]) + .mount("/", + routes![ + home_controller::index, + home_controller::index_json]) .mount("/posts", routes![ posts_controller::new, -- cgit v1.2.3