* move get_post to blog.rs
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

* wip index blog
This commit is contained in:
Mylloon 2023-04-19 20:17:03 +02:00
parent ec2389ce2f
commit c344243484
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
4 changed files with 54 additions and 32 deletions

View file

@ -1,2 +1,2 @@
pub mod github;
pub mod utils;
mod utils;

View file

@ -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<File>, 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(", ")),
}
}

View file

@ -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<Config>) -> impl Responder {
@ -11,24 +14,58 @@ pub async fn index(config: web::Data<Config>) -> impl Responder {
#[derive(Content)]
struct BlogIndexTemplate {
post: Option<File>,
posts: Option<Vec<File>>,
}
#[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<File>,
}
#[get("/blog/{id}")]
pub async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> 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<File>, 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(", ")),
}
}

View file

@ -0,0 +1,8 @@
<!DOCTYPE html>
<html class="index" lang="fr">
{{>head.html}}
<body class="index">
<p style="color: aliceblue">Index</p>
{{>footer.html}}
</body>
</html>