mylloon.fr/src/routes/index.rs

94 lines
2.4 KiB
Rust
Raw Normal View History

use actix_web::{get, web, Responder};
2023-04-11 10:45:25 +02:00
use cached::proc_macro::once;
use ramhorns::Content;
2023-02-08 20:49:05 +01:00
use crate::{
config::Config,
template::{InfosPage, NavBar},
2024-11-09 16:36:04 +01:00
utils::{
markdown::{File, TypeFileMetadata},
misc::{make_kw, read_file, Html},
},
};
2023-02-09 11:19:53 +01:00
2023-02-08 20:49:05 +01:00
#[get("/")]
2024-06-17 15:56:57 +02:00
pub async fn page(config: web::Data<Config>) -> impl Responder {
Html(build_page(config.get_ref().to_owned()))
2023-02-08 20:49:05 +01:00
}
2023-02-08 22:14:57 +01:00
#[derive(Content, Debug)]
struct IndexTemplate {
navbar: NavBar,
2024-01-24 13:09:20 +01:00
name: String,
pronouns: Option<String>,
2024-06-02 18:19:58 +02:00
file: Option<File>,
2024-01-24 13:09:20 +01:00
avatar: String,
avatar_caption: String,
2024-03-03 20:55:00 +01:00
avatar_style: StyleAvatar,
}
#[derive(Content, Debug, Default)]
struct StyleAvatar {
round: bool,
square: bool,
}
2023-04-11 10:45:25 +02:00
#[once(time = 60)]
2023-10-24 11:50:30 +02:00
fn build_page(config: Config) -> String {
2024-01-24 13:09:20 +01:00
let mut file = read_file(
&format!("{}/index.md", config.locations.data_dir),
2024-05-28 20:26:58 +02:00
&TypeFileMetadata::Index,
);
2024-01-24 13:09:20 +01:00
// Default values
2024-05-28 20:26:58 +02:00
let mut name = config.fc.fullname.clone().unwrap_or_default();
2024-01-24 13:09:20 +01:00
let mut pronouns = None;
let mut avatar = "/icons/apple-touch-icon.png".to_owned();
let mut avatar_caption = "EWP avatar".to_owned();
2024-03-03 20:55:00 +01:00
let mut avatar_style = StyleAvatar {
round: true,
square: false,
};
2024-01-24 13:09:20 +01:00
if let Some(f) = &file {
if let Some(m) = &f.metadata.info.index {
2024-05-28 20:26:58 +02:00
name = m.name.clone().unwrap_or(name);
avatar = m.avatar.clone().unwrap_or(avatar);
2024-05-16 18:08:58 +02:00
m.pronouns.clone_into(&mut pronouns);
2024-05-28 20:26:58 +02:00
avatar_caption = m.avatar_caption.clone().unwrap_or(avatar_caption);
2024-03-03 20:55:00 +01:00
2024-05-28 20:26:58 +02:00
if let Some(style) = m.avatar_style.clone() {
2024-03-03 20:55:00 +01:00
if style.trim() == "square" {
avatar_style = StyleAvatar {
square: true,
..StyleAvatar::default()
}
}
}
2024-01-24 13:09:20 +01:00
}
} else {
2024-05-28 20:26:58 +02:00
file = read_file("README.md", &TypeFileMetadata::Generic);
}
2023-04-11 10:16:46 +02:00
config.tmpl.render(
"index.html",
IndexTemplate {
navbar: NavBar {
index: true,
..NavBar::default()
},
2024-06-02 18:19:58 +02:00
file,
2024-01-24 13:09:20 +01:00
name,
pronouns,
avatar,
avatar_caption,
2024-03-03 20:55:00 +01:00
avatar_style,
},
2024-05-28 20:26:58 +02:00
InfosPage {
title: config.fc.fullname,
desc: Some("Page principale".into()),
kw: Some(make_kw(&["index", "étudiant", "accueil"])),
2023-04-11 10:16:46 +02:00
},
)
2023-02-08 22:14:57 +01:00
}