aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2017-11-17 11:30:32 +0100
committerHarald Eilertsen <haraldei@anduin.net>2017-11-17 11:30:32 +0100
commit48a517335bdaa6d8e72eb0d0fe6512033b83d43e (patch)
tree351a5865b182120e84931ff0215e0c1110e31232 /src
parentf09e8e56921361b406e40eddd6ded9daca9dc8a5 (diff)
downloadrocket-blog-48a517335bdaa6d8e72eb0d0fe6512033b83d43e.tar.gz
rocket-blog-48a517335bdaa6d8e72eb0d0fe6512033b83d43e.tar.bz2
rocket-blog-48a517335bdaa6d8e72eb0d0fe6512033b83d43e.zip
Implement responder for IndexTemplate.
This means the actual handler just returns the template, which is then rendered by the responder for that type. Looking into making this generic so the responder can return data in other formats as well, like json or xml.
Diffstat (limited to 'src')
-rw-r--r--src/main.rs18
1 files changed, 15 insertions, 3 deletions
diff --git a/src/main.rs b/src/main.rs
index e0501d0..03c34df 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,7 +8,10 @@ extern crate rocket_blog;
use self::diesel::prelude::*;
use self::rocket_blog::{schema, models};
-use rocket::response;
+use rocket::response::{Response, Responder};
+use rocket::request::Request;
+use rocket::http::{ContentType, Status};
+use std::io::Cursor;
#[derive(BartDisplay)]
#[template = "templates/index.html"]
@@ -17,15 +20,24 @@ struct IndexTemplate<'a> {
posts: Vec<models::Post>
}
+impl<'a> Responder<'a> for IndexTemplate<'a> {
+ fn respond_to(self, _: &Request) -> Result<Response<'static>, Status> {
+ Response::build()
+ .header(ContentType::HTML)
+ .sized_body(Cursor::new(format!("{}", &self)))
+ .ok()
+ }
+}
+
#[get("/")]
-fn index(conn: rocket_blog::DbConn) -> content::Html<String> {
+fn index<'a>(conn: rocket_blog::DbConn) -> IndexTemplate<'a> {
use schema::posts::dsl::*;
let results = posts.filter(published.eq(false))
.limit(5)
.load::<models::Post>(&*conn)
.expect("Error loading posts");
- content::Html(format!("{}", &IndexTemplate{ title: "Bloggen", posts: results }))
+ IndexTemplate { title: "Bloggen", posts: results }
}
fn main() {