aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 8dd67711ffcf3c219d82aca0a1d317953968aa3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#![feature(plugin)]
#![plugin(rocket_codegen)]

extern crate diesel;
extern crate rocket;
extern crate rocket_blog;
#[macro_use] extern crate bart_derive;

use self::diesel::prelude::*;
use self::rocket_blog::{schema, models};

#[derive(BartDisplay)]
#[template = "templates/index.html"]
struct IndexTemplate<'a> {
    title: &'a str,
    content: &'a str
}

#[get("/")]
fn index(conn: rocket_blog::DbConn) -> String {
    use schema::posts::dsl::*;
    let results = posts.filter(published.eq(false))
        .limit(5)
        .load::<models::Post>(&*conn)
        .expect("Error loading posts");

    let output : String = results.iter().map(|post| {
        format!("{}", &IndexTemplate{ title: &post.title, content: &post.body })
    }).collect();

    output
}

fn main() {
    rocket::ignite()
        .manage(rocket_blog::init_db_pool())
        .mount("/", routes![index])
        .launch();
}