split logic of contact
This commit is contained in:
parent
744857d685
commit
b9bc57c1e0
3 changed files with 54 additions and 49 deletions
|
@ -2,15 +2,15 @@ use actix_web::{get, routes, web, HttpRequest, Responder};
|
|||
use cached::proc_macro::once;
|
||||
use glob::glob;
|
||||
use ramhorns::Content;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use crate::{
|
||||
config::Config,
|
||||
template::{InfosPage, NavBar},
|
||||
utils::{
|
||||
markdown::{File, TypeFileMetadata},
|
||||
misc::{make_kw, read_file, Html},
|
||||
routes::contact::{find_links, remove_paragraphs},
|
||||
},
|
||||
template::{InfosPage, NavBar},
|
||||
};
|
||||
|
||||
const CONTACT_DIR: &str = "contacts";
|
||||
|
@ -32,47 +32,6 @@ async fn page(config: web::Data<Config>) -> impl Responder {
|
|||
Html(build_page(config.get_ref().to_owned()))
|
||||
}
|
||||
|
||||
/// Contact node
|
||||
#[derive(Clone, Debug)]
|
||||
struct ContactLink {
|
||||
service: String,
|
||||
scope: Option<String>,
|
||||
link: String,
|
||||
}
|
||||
|
||||
#[once(time = 60)]
|
||||
fn find_links(directory: String) -> Vec<ContactLink> {
|
||||
// TOML filename
|
||||
let toml_file = "links.toml";
|
||||
|
||||
// Read the TOML file and parse it
|
||||
let toml_str = read_to_string(format!("{directory}/{toml_file}")).unwrap_or_default();
|
||||
|
||||
let mut redirections = vec![];
|
||||
match toml::de::from_str::<toml::Value>(&toml_str) {
|
||||
Ok(data) => {
|
||||
if let Some(section) = data.as_table() {
|
||||
section.iter().for_each(|(key, value)| {
|
||||
// Scopes are delimited with `/`
|
||||
let (service, scope) = match key.split_once('/') {
|
||||
Some((service, scope)) => (service.to_owned(), Some(scope.to_owned())),
|
||||
None => (key.to_owned(), None),
|
||||
};
|
||||
|
||||
redirections.push(ContactLink {
|
||||
service,
|
||||
scope,
|
||||
link: value.as_str().unwrap().to_owned(),
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(_) => return vec![],
|
||||
}
|
||||
|
||||
redirections
|
||||
}
|
||||
|
||||
#[routes]
|
||||
#[get("/{service}")]
|
||||
#[get("/{service}/{scope}")]
|
||||
|
@ -92,7 +51,7 @@ async fn service_redirection(config: web::Data<Config>, req: HttpRequest) -> imp
|
|||
_ => false,
|
||||
})
|
||||
// Returns the link
|
||||
.map(|data| data.link.clone())
|
||||
.map(|data| data.url.clone())
|
||||
.collect::<Vec<String>>();
|
||||
|
||||
// This shouldn't be more than one link here
|
||||
|
@ -119,11 +78,6 @@ struct NetworksTemplate {
|
|||
others: Vec<File>,
|
||||
}
|
||||
|
||||
fn remove_paragraphs(list: &mut [File]) {
|
||||
list.iter_mut()
|
||||
.for_each(|file| file.content = file.content.replace("<p>", "").replace("</p>", ""));
|
||||
}
|
||||
|
||||
#[once(time = 60)]
|
||||
fn build_page(config: Config) -> String {
|
||||
let contacts_dir = format!("{}/{}", config.locations.data_dir, CONTACT_DIR);
|
||||
|
|
50
src/utils/routes/contact.rs
Normal file
50
src/utils/routes/contact.rs
Normal file
|
@ -0,0 +1,50 @@
|
|||
use cached::proc_macro::once;
|
||||
use std::fs::read_to_string;
|
||||
|
||||
use crate::utils::markdown::File;
|
||||
|
||||
/// Contact node
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Link {
|
||||
pub service: String,
|
||||
pub scope: Option<String>,
|
||||
pub url: String,
|
||||
}
|
||||
|
||||
#[once(time = 60)]
|
||||
pub fn find_links(directory: String) -> Vec<Link> {
|
||||
// TOML filename
|
||||
let toml_file = "links.toml";
|
||||
|
||||
// Read the TOML file and parse it
|
||||
let toml_str = read_to_string(format!("{directory}/{toml_file}")).unwrap_or_default();
|
||||
|
||||
let mut redirections = vec![];
|
||||
match toml::de::from_str::<toml::Value>(&toml_str) {
|
||||
Ok(data) => {
|
||||
if let Some(section) = data.as_table() {
|
||||
section.iter().for_each(|(key, value)| {
|
||||
// Scopes are delimited with `/`
|
||||
let (service, scope) = match key.split_once('/') {
|
||||
Some((service, scope)) => (service.to_owned(), Some(scope.to_owned())),
|
||||
None => (key.to_owned(), None),
|
||||
};
|
||||
|
||||
redirections.push(Link {
|
||||
service,
|
||||
scope,
|
||||
url: value.as_str().unwrap().to_owned(),
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(_) => return vec![],
|
||||
}
|
||||
|
||||
redirections
|
||||
}
|
||||
|
||||
pub fn remove_paragraphs(list: &mut [File]) {
|
||||
list.iter_mut()
|
||||
.for_each(|file| file.content = file.content.replace("<p>", "").replace("</p>", ""));
|
||||
}
|
|
@ -1,2 +1,3 @@
|
|||
pub mod blog;
|
||||
pub mod contact;
|
||||
pub mod cours;
|
||||
|
|
Loading…
Reference in a new issue