diff options
-rw-r--r-- | src/main.rs | 18 |
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() { |