aboutsummaryrefslogblamecommitdiffstats
path: root/src/controllers/home_controller.rs
blob: 6423a3027b56806b14d1d29817a49ecba6c4fcfc (plain) (tree)
1
2
3
4
5
6
7
8
                      
           
                         
                       


                                    
                      


                            
                                        

                                 






                                                             





                                                               







                                                                               
use ::{models, utils};
use rocket;
use rocket_contrib::Json;
use std::path::PathBuf;

#[derive(BartDisplay, Serialize)]
#[template = "templates/index.html"]
struct IndexTemplate {
    posts: Vec<models::Post>
}

implement_responder_for!(IndexTemplate);

#[get("/", format = "text/html")]
fn index(conn: utils::DbConn) -> utils::Page<IndexTemplate> {
    utils::Page {
        title: String::from("Bloggen"),
        content: IndexTemplate {
            posts: models::Post::get_all(conn)
        }
    }
}

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

//
// Serve files from the public directory if no routes matches.
//
#[get("/<file..>", rank = 99)]
fn public_file(file: PathBuf) -> Option<rocket::response::NamedFile> {
    rocket::response::NamedFile::open(PathBuf::from("public/").join(file)).ok()
}