diff --git a/src/misc/mod.rs b/src/misc/mod.rs index 683ac39..0c8ab90 100644 --- a/src/misc/mod.rs +++ b/src/misc/mod.rs @@ -1,2 +1,2 @@ pub mod github; -pub mod utils; +mod utils; diff --git a/src/misc/utils.rs b/src/misc/utils.rs index 162da32..b740952 100644 --- a/src/misc/utils.rs +++ b/src/misc/utils.rs @@ -1,8 +1,6 @@ use cached::proc_macro::cached; use reqwest::Client; -use crate::template::{read_md_file, File, Infos}; - #[cached] pub fn get_reqwest_client() -> Client { Client::builder() @@ -10,24 +8,3 @@ pub fn get_reqwest_client() -> Client { .build() .unwrap() } - -pub fn get_post(post: &mut Option, filename: String) -> Infos { - let blog_dir = "data/blog"; - let ext = ".md"; - - *post = read_md_file(&format!("{blog_dir}/{filename}{ext}")); - - let title = match post { - Some(data) => match &data.metadata.info.title { - Some(text) => text, - None => &filename, - }, - None => &filename, - }; - - Infos { - page_title: Some(format!("Post: {}", title)), - page_desc: Some("Blog d'Anri".to_string()), - page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")), - } -} diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 2bae2ec..1908fe0 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -2,7 +2,10 @@ use actix_web::{get, web, HttpResponse, Responder}; use cached::proc_macro::once; use ramhorns::Content; -use crate::{config::Config, misc::utils::get_post, template::File}; +use crate::{ + config::Config, + template::{read_md_file, File, Infos}, +}; #[get("/blog")] pub async fn index(config: web::Data) -> impl Responder { @@ -11,24 +14,58 @@ pub async fn index(config: web::Data) -> impl Responder { #[derive(Content)] struct BlogIndexTemplate { - post: Option, + posts: Option>, } #[once(time = 60)] -pub fn get_index(_config: Config) -> String { - String::from("Salut pour tous") +pub fn get_index(config: Config) -> String { + config.tmpl.render( + "blog/index.html", + BlogIndexTemplate { posts: None }, + Infos { + page_title: None, + page_desc: None, + page_kw: None, + }, + ) +} + +#[derive(Content)] +struct BlogPostTemplate { + post: Option, } #[get("/blog/{id}")] pub async fn page(path: web::Path<(String,)>, config: web::Data) -> impl Responder { - HttpResponse::Ok().body(get_page(path.into_inner().0, config.get_ref().clone())) + HttpResponse::Ok().body(get_post(path.into_inner().0, config.get_ref().clone())) } -pub fn get_page(file: String, config: Config) -> String { +pub fn get_post(file: String, config: Config) -> String { let mut post = None; - let infos = get_post(&mut post, file); + let infos = _get_post(&mut post, file); config .tmpl - .render("blog/post.html", BlogIndexTemplate { post }, infos) + .render("blog/post.html", BlogPostTemplate { post }, infos) +} + +fn _get_post(post: &mut Option, filename: String) -> Infos { + let blog_dir = "data/blog"; + let ext = ".md"; + + *post = read_md_file(&format!("{blog_dir}/{filename}{ext}")); + + let title = match post { + Some(data) => match &data.metadata.info.title { + Some(text) => text, + None => &filename, + }, + None => &filename, + }; + + Infos { + page_title: Some(format!("Post: {}", title)), + page_desc: Some("Blog d'Anri".to_string()), + page_kw: Some(["blog", "blogging", "write", "writing"].join(", ")), + } } diff --git a/templates/blog/index.html b/templates/blog/index.html new file mode 100644 index 0000000..60d353a --- /dev/null +++ b/templates/blog/index.html @@ -0,0 +1,8 @@ + + + {{>head.html}} + +

Index

+ {{>footer.html}} + +