#![cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] use models::Post; use posts_controller::ShowPostTemplate; use rocket::{request::FlashMessage, response::NamedFile, Route}; use rocket_contrib::Json; use std::path::PathBuf; use utils::{DbConn, Page}; #[derive(BartDisplay)] #[template = "templates/index.html"] struct IndexTemplate { posts: Vec, } implement_responder_for!(IndexTemplate); #[get("/", format = "text/html")] fn index(flash: Option, conn: DbConn) -> Page { Page { title: String::from("Bloggen"), flash, content: IndexTemplate { posts: Post::get_all(&conn) .into_iter() .map(|p| ShowPostTemplate { post: p }) .collect(), }, } } #[get("/", format = "application/json")] fn index_json(conn: DbConn) -> Json> { Json(Post::get_all(&conn)) } // // Serve files from the public directory if no routes matches. // #[get("/", rank = 99)] fn public_file(file: PathBuf) -> Option { NamedFile::open(PathBuf::from("public/").join(file)).ok() } pub fn routes() -> Vec { routes![index, index_json, public_file] }