aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-07-31 15:14:44 +0200
committerHarald Eilertsen <haraldei@anduin.net>2018-07-31 15:14:44 +0200
commit6e5801a75399f7b8bc7adb1b19bc2d26e169f970 (patch)
treecd5f6fda4d6b8c85dba9f81a989ca9330d3b302c
parentf90641a4bce63797434228c32c72db879dbeef58 (diff)
downloadrocket-blog-6e5801a75399f7b8bc7adb1b19bc2d26e169f970.tar.gz
rocket-blog-6e5801a75399f7b8bc7adb1b19bc2d26e169f970.tar.bz2
rocket-blog-6e5801a75399f7b8bc7adb1b19bc2d26e169f970.zip
Move routing details into controllers.
-rw-r--r--src/controllers/home_controller.rs8
-rw-r--r--src/controllers/login_controller.rs4
-rw-r--r--src/controllers/posts_controller.rs12
-rw-r--r--src/controllers/users_controller.rs5
-rw-r--r--src/main.rs35
5 files changed, 39 insertions, 25 deletions
diff --git a/src/controllers/home_controller.rs b/src/controllers/home_controller.rs
index 2f657c3..bef47de 100644
--- a/src/controllers/home_controller.rs
+++ b/src/controllers/home_controller.rs
@@ -37,3 +37,11 @@ fn index_json(conn: utils::DbConn) -> Json<Vec<models::Post>> {
fn public_file(file: PathBuf) -> Option<rocket::response::NamedFile> {
rocket::response::NamedFile::open(PathBuf::from("public/").join(file)).ok()
}
+
+pub fn routes() -> Vec<rocket::Route> {
+ routes![
+ index,
+ index_json,
+ public_file
+ ]
+}
diff --git a/src/controllers/login_controller.rs b/src/controllers/login_controller.rs
index 35841b3..800a6b2 100644
--- a/src/controllers/login_controller.rs
+++ b/src/controllers/login_controller.rs
@@ -36,3 +36,7 @@ pub fn create(login: Form<LoginForm>, conn: utils::DbConn) -> Flash<Redirect> {
Flash::error(Redirect::to("/login"), "Invalid email or passoword!")
}
+
+pub fn routes() -> Vec<rocket::Route> {
+ routes![new, create]
+}
diff --git a/src/controllers/posts_controller.rs b/src/controllers/posts_controller.rs
index aca9d13..f10f312 100644
--- a/src/controllers/posts_controller.rs
+++ b/src/controllers/posts_controller.rs
@@ -1,4 +1,5 @@
use comrak::{markdown_to_html, ComrakOptions};
+use rocket;
use rocket::request::Form;
use rocket::response::{Flash, Redirect};
use utils;
@@ -83,3 +84,14 @@ fn delete(id: i32, conn: utils::DbConn) -> Flash<Redirect> {
::models::Post::delete(id, conn);
Flash::success(Redirect::to("/"), "Post deleted!")
}
+
+pub fn routes() -> Vec<rocket::Route> {
+ routes![
+ new,
+ create,
+ show,
+ edit,
+ update,
+ delete
+ ]
+}
diff --git a/src/controllers/users_controller.rs b/src/controllers/users_controller.rs
index 8dcd4c0..9708a1b 100644
--- a/src/controllers/users_controller.rs
+++ b/src/controllers/users_controller.rs
@@ -1,3 +1,4 @@
+use rocket;
use rocket::request::Form;
use rocket::response::{Flash, Redirect};
use utils;
@@ -38,3 +39,7 @@ fn create(user: Form<::models::NewUser>, conn: utils::DbConn) -> Flash<Redirect>
Err(_) => Flash::error(Redirect::to("/"), "Could not create user!")
}
}
+
+pub fn routes() -> Vec<rocket::Route> {
+ routes![new, create]
+}
diff --git a/src/main.rs b/src/main.rs
index 6fa552f..4515a2d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -16,36 +16,21 @@ extern crate rocket_contrib;
mod models;
mod schema;
mod controllers;
-use controllers::home_controller;
-use controllers::{login_controller, posts_controller, users_controller};
+use controllers::{
+ home_controller,
+ login_controller,
+ posts_controller,
+ users_controller
+};
fn main() {
if let Ok(dburl) = dotenv::var("DATABASE_URL") {
rocket::ignite()
.manage(utils::init_db_pool(&dburl))
- .mount("/",
- routes![
- home_controller::index,
- home_controller::index_json,
- home_controller::public_file])
- .mount("/posts",
- routes![
- posts_controller::new,
- posts_controller::create,
- posts_controller::show,
- posts_controller::edit,
- posts_controller::update,
- posts_controller::delete])
- .mount("/users",
- routes![
- users_controller::new,
- users_controller::create,
- ])
- .mount("/login",
- routes![
- login_controller::new,
- login_controller::create,
- ])
+ .mount("/", home_controller::routes())
+ .mount("/posts", posts_controller::routes())
+ .mount("/users", users_controller::routes())
+ .mount("/login", login_controller::routes())
.launch();
}
else {