Basic cours support #44
1 changed files with 17 additions and 10 deletions
|
@ -1,6 +1,7 @@
|
|||
use std::path::Path;
|
||||
|
||||
use actix_web::{get, web, Responder};
|
||||
use cached::proc_macro::cached;
|
||||
use ramhorns::Content;
|
||||
use regex::Regex;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -31,21 +32,23 @@ struct CoursTemplate {
|
|||
content: Option<File>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
#[derive(Debug, Serialize, Clone)]
|
||||
struct FileNode {
|
||||
name: String,
|
||||
is_dir: bool,
|
||||
children: Vec<FileNode>,
|
||||
}
|
||||
|
||||
fn compile_patterns(exclusion_list: &[String]) -> Vec<Regex> {
|
||||
#[cached]
|
||||
fn compile_patterns(exclusion_list: Vec<String>) -> Vec<Regex> {
|
||||
exclusion_list
|
||||
.iter()
|
||||
.map(|pattern| Regex::new(pattern).unwrap())
|
||||
.collect()
|
||||
}
|
||||
|
||||
fn get_filetree(dir_path: &str, exclusion_patterns: Vec<Regex>) -> FileNode {
|
||||
// #[once(time = 240)]
|
||||
fn get_filetree(dir_path: &str, exclusion_patterns: &Vec<Regex>) -> FileNode {
|
||||
let children = std::fs::read_dir(dir_path)
|
||||
.unwrap()
|
||||
.filter_map(Result::ok)
|
||||
|
@ -65,10 +68,14 @@ fn get_filetree(dir_path: &str, exclusion_patterns: Vec<Regex>) -> FileNode {
|
|||
children: vec![],
|
||||
})
|
||||
} else {
|
||||
Some(get_filetree(
|
||||
entry_path.to_str().unwrap(),
|
||||
exclusion_patterns.to_owned(),
|
||||
))
|
||||
// Exclude empty directories
|
||||
let children_of_children =
|
||||
get_filetree(entry_path.to_str().unwrap(), exclusion_patterns);
|
||||
if children_of_children.is_dir && children_of_children.children.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(children_of_children)
|
||||
}
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
@ -109,13 +116,13 @@ fn get_content(
|
|||
)
|
||||
}
|
||||
|
||||
// #[once(time = 60)]
|
||||
// TODO: Uncomment before release
|
||||
// #[once(time = 60)]
|
||||
fn build_page(info: web::Query<PathRequest>, config: Config) -> String {
|
||||
let cours_dir = "data/cours";
|
||||
let exclusion_list = config.fc.exclude_courses.unwrap();
|
||||
let exclusion_patterns = compile_patterns(&exclusion_list);
|
||||
let filetree = get_filetree(cours_dir, exclusion_patterns);
|
||||
let exclusion_patterns = compile_patterns(exclusion_list.to_owned());
|
||||
let filetree = get_filetree(cours_dir, &exclusion_patterns);
|
||||
|
||||
config.tmpl.render(
|
||||
"cours.html",
|
||||
|
|
Loading…
Reference in a new issue