mylloon.fr/src/routes/blog.rs

79 lines
1.9 KiB
Rust
Raw Normal View History

2023-04-14 11:30:58 +02:00
use actix_web::{get, web, HttpResponse, Responder};
use cached::proc_macro::once;
use ramhorns::Content;
use crate::{
config::Config,
template::{read_md_file, File, Infos},
};
2023-04-14 11:30:58 +02:00
#[get("/blog")]
pub async fn index(config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_index(config.get_ref().clone()))
}
#[derive(Content)]
2023-04-19 18:55:03 +02:00
struct BlogIndexTemplate {
2023-04-19 20:27:40 +02:00
posts: Option<Vec<Post>>,
}
#[derive(Content)]
struct Post {
title: String,
url: String,
file: File,
2023-04-19 18:55:03 +02:00
}
2023-04-14 11:30:58 +02:00
#[once(time = 60)]
pub fn get_index(config: Config) -> String {
config.tmpl.render(
"blog/index.html",
BlogIndexTemplate { posts: None },
Infos {
2023-04-19 20:32:04 +02:00
page_title: Some("Blog".to_string()),
page_desc: Some("Liste des posts d'Anri".to_string()),
page_kw: Some(["blog", "blogging"].join(", ")),
},
)
}
#[derive(Content)]
struct BlogPostTemplate {
post: Option<File>,
2023-04-14 11:30:58 +02:00
}
#[get("/blog/{id}")]
2023-04-19 18:55:03 +02:00
pub async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Responder {
HttpResponse::Ok().body(get_post(path.into_inner().0, config.get_ref().clone()))
2023-04-14 11:30:58 +02:00
}
pub fn get_post(file: String, config: Config) -> String {
2023-04-19 20:08:15 +02:00
let mut post = None;
let infos = _get_post(&mut post, file);
2023-04-19 18:55:03 +02:00
config
.tmpl
.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(", ")),
}
2023-04-14 11:30:58 +02:00
}