use actix_web::{get, web, Responder}; use cached::proc_macro::once; use ramhorns::Content; use crate::{ config::Config, misc::{ markdown::{read_file, File, TypeFileMetadata}, utils::{make_kw, Html}, }, template::{Infos, NavBar}, }; #[get("/")] async fn page(config: web::Data) -> impl Responder { Html(build_page(config.get_ref().to_owned())) } #[derive(Content, Debug)] struct IndexTemplate { navbar: NavBar, name: String, pronouns: Option, content: Option, avatar: String, avatar_caption: String, } #[once(time = 60)] fn build_page(config: Config) -> String { let mut file = read_file( &format!("{}/index.md", config.locations.data_dir), TypeFileMetadata::Index, ); // Default values let mut name = config.fc.fullname.to_owned().unwrap_or_default(); let mut pronouns = None; let mut avatar = "/icons/apple-touch-icon.png".to_owned(); let mut avatar_caption = "EWP avatar".to_owned(); if let Some(f) = &file { if let Some(m) = &f.metadata.info.index { name = m.name.to_owned().unwrap_or(name); avatar = m.avatar.to_owned().unwrap_or(avatar); pronouns = m.pronouns.to_owned(); avatar_caption = m.avatar_caption.to_owned().unwrap_or(avatar_caption); } } else { file = read_file("README.md", TypeFileMetadata::Generic); } config.tmpl.render( "index.html", IndexTemplate { navbar: NavBar { index: true, ..NavBar::default() }, content: file, name, pronouns, avatar, avatar_caption, }, Infos { page_title: config.fc.fullname, page_desc: Some("Page principale".into()), page_kw: make_kw(&["index", "étudiant", "accueil"]), }, ) }