aboutsummaryrefslogblamecommitdiffstats
path: root/src/main.rs
blob: 40fa802b5d55b5b834300e663645748062fad389 (plain) (tree)
1
2
3
4
5
6
7


                          
                                      
                                      
                                       
 




                            
                         
 

          
                                 


                                    
                            
 
 
                                            
 

                                                              
                                                                          
 
 

                                                                     
                                     



                    
                                            
                                               
                                                                         

                  
#![feature(plugin)]
#![plugin(rocket_codegen)]

#[macro_use] extern crate rocket_blog;
#[macro_use] extern crate bart_derive;
#[macro_use] extern crate serde_derive;

extern crate diesel;
extern crate rocket;
extern crate rocket_contrib;

use rocket_blog::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>);

#[get("/", format = "text/html")]
fn index<'a>(conn: rocket_blog::DbConn) -> IndexTemplate<'a> {
    IndexTemplate { title: "Bloggen", posts: models::Post::get_all(conn) }
}

#[get("/", format = "application/json")]
fn index_json(conn: rocket_blog::DbConn) -> Json<Vec<models::Post>> {
    Json(models::Post::get_all(conn))
}

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