blob: 0cf1acfd5edb446f0ea995a5074347b83e8abc5b (
plain) (
tree)
|
|
#![feature(plugin)]
#![plugin(rocket_codegen)]
extern crate diesel;
extern crate rocket;
#[macro_use] extern crate rocket_blog;
extern crate rocket_contrib;
#[macro_use] extern crate bart_derive;
#[macro_use] extern crate serde_derive;
use self::diesel::prelude::*;
use self::rocket_blog::{schema, models};
use rocket_contrib::Json;
mod posts;
#[derive(BartDisplay, Serialize)]
#[template = "templates/index.html"]
struct IndexTemplate<'a> {
title: &'a str,
posts: Vec<models::Post>
}
implement_responder_for!(IndexTemplate<'a>);
fn get_posts(conn: rocket_blog::DbConn) -> Vec<models::Post> {
use schema::posts::dsl::*;
posts.filter(published.eq(false))
.limit(5)
.load::<models::Post>(&*conn)
.expect("Error loading posts")
}
#[get("/", format = "text/html")]
fn index<'a>(conn: rocket_blog::DbConn) -> IndexTemplate<'a> {
IndexTemplate { title: "Bloggen", posts: get_posts(conn) }
}
#[get("/", format = "application/json")]
fn index_json(conn: rocket_blog::DbConn) -> Json<Vec<models::Post>> {
Json(get_posts(conn))
}
fn main() {
rocket::ignite()
.manage(rocket_blog::init_db_pool())
.mount("/", routes![index, index_json])
.mount("/posts", routes![posts::new, posts::create])
.launch();
}
|