mylloon.fr/src/routes/blog.rs

45 lines
1.1 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;
2023-04-19 18:55:03 +02:00
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 {
post: Option<File>,
}
2023-04-14 11:30:58 +02:00
#[once(time = 60)]
pub fn get_index(_config: Config) -> String {
String::from("Salut pour tous")
}
#[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 {
2023-04-14 11:30:58 +02:00
HttpResponse::Ok().body(get_page(path.into_inner().0, config.get_ref().clone()))
}
2023-04-19 18:55:03 +02:00
pub fn get_page(file: String, config: Config) -> String {
let blog_dir = "data/blog";
let ext = ".md";
let post = read_md_file(&format!("{blog_dir}/{file}{ext}"));
let infos = Infos {
page_title: Some(format!("Post: {file}")),
page_desc: Some("Blog d'Anri".to_string()),
};
config
.tmpl
.render("blog/post.html", BlogIndexTemplate { post }, infos)
2023-04-14 11:30:58 +02:00
}