* wip index blog
This commit is contained in:
parent
ec2389ce2f
commit
c344243484
4 changed files with 54 additions and 32 deletions
|
@ -1,2 +1,2 @@
|
||||||
pub mod github;
|
pub mod github;
|
||||||
pub mod utils;
|
mod utils;
|
||||||
|
|
|
@ -1,8 +1,6 @@
|
||||||
use cached::proc_macro::cached;
|
use cached::proc_macro::cached;
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
|
|
||||||
use crate::template::{read_md_file, File, Infos};
|
|
||||||
|
|
||||||
#[cached]
|
#[cached]
|
||||||
pub fn get_reqwest_client() -> Client {
|
pub fn get_reqwest_client() -> Client {
|
||||||
Client::builder()
|
Client::builder()
|
||||||
|
@ -10,24 +8,3 @@ pub fn get_reqwest_client() -> Client {
|
||||||
.build()
|
.build()
|
||||||
.unwrap()
|
.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(", ")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,7 +2,10 @@ use actix_web::{get, web, HttpResponse, Responder};
|
||||||
use cached::proc_macro::once;
|
use cached::proc_macro::once;
|
||||||
use ramhorns::Content;
|
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")]
|
#[get("/blog")]
|
||||||
pub async fn index(config: web::Data<Config>) -> impl Responder {
|
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)]
|
#[derive(Content)]
|
||||||
struct BlogIndexTemplate {
|
struct BlogIndexTemplate {
|
||||||
post: Option<File>,
|
posts: Option<Vec<File>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[once(time = 60)]
|
#[once(time = 60)]
|
||||||
pub fn get_index(_config: Config) -> String {
|
pub fn get_index(config: Config) -> String {
|
||||||
String::from("Salut pour tous")
|
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}")]
|
#[get("/blog/{id}")]
|
||||||
pub async fn page(path: web::Path<(String,)>, config: web::Data<Config>) -> impl Responder {
|
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 mut post = None;
|
||||||
let infos = get_post(&mut post, file);
|
let infos = _get_post(&mut post, file);
|
||||||
|
|
||||||
config
|
config
|
||||||
.tmpl
|
.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(", ")),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
8
templates/blog/index.html
Normal file
8
templates/blog/index.html
Normal 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>
|
Loading…
Reference in a new issue