Skip to content

Commit

Permalink
reimplement the buildin postlists
Browse files Browse the repository at this point in the history
  • Loading branch information
strawmelonjuice committed Aug 27, 2024
1 parent 3148659 commit 52fec67
Show file tree
Hide file tree
Showing 3 changed files with 335 additions and 12 deletions.
5 changes: 4 additions & 1 deletion source/Main/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use futures::join;
use log::LevelFilter;
use log::{debug, error};
use log::{info, trace};
use requestresponse::{assets_with_cache, serve};
use requestresponse::{assets_with_cache, category, post, serve, tags};
use simplelog::{ColorChoice, CombinedLogger, TermLogger, TerminalMode, WriteLogger};
use std::fs::File;
use std::path::PathBuf;
Expand Down Expand Up @@ -628,8 +628,11 @@ async fn start() {
Data::new(server_context_arc_mutex.clone());
let main_server = match HttpServer::new(move || {
App::new()
.service(tags)
.service(category)
.service(assets_with_cache)
.service(serve)
.service(post)
.app_data(server_context_data.clone())
})
.bind(("localhost", config.port))
Expand Down
48 changes: 44 additions & 4 deletions source/Main/publications.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ impl PostLists for CynthiaPostList {
.collect(),
PostListFilter::Category(category) => self
.iter()
.filter(|x| x.category == Some(category.clone()))
.filter(|x| {
if let Some(c) = &x.category {
c.to_lowercase() == category.to_lowercase()
} else {
false
}
})
.cloned()
.collect(),
PostListFilter::Author(author) => self
Expand All @@ -66,6 +72,16 @@ impl PostLists for CynthiaPostList {
}
}
fn get_by_id(&self, id: String) -> Option<CynthiaPublication> {
if id.starts_with("virtual:") {
let l = id.split("virtual:").collect::<Vec<&str>>()[1];
return match serde_json::from_str(l) {
Ok(t) => Some(t),
Err(e) => {
error!("Couldn't parse virtual publication.\n\n\t\t{e}");
None
}
};
};
let mut a: Option<CynthiaPublication> = None;
for i in self {
if i.id == id {
Expand Down Expand Up @@ -173,18 +189,42 @@ impl CynthiaPublicationListTrait for CynthiaPublicationList {
.cloned()
}
fn get_by_id(&self, id: String) -> Option<CynthiaPublication> {
if id.starts_with("virtual:") {
let l = id.split("virtual:").collect::<Vec<&str>>()[1];
return match serde_json::from_str(l) {
Ok(t) => Some(t),
Err(e) => {
error!("Couldn't parse virtual publication.\n\n\t\t{e}");
None
}
};
};
self.iter().find(|x| x.get_id() == id).cloned()
}
fn validate(&self, config: CynthiaConfClone) -> bool {
// Collect validation results in a vector
let mut valid: Vec<bool> = vec![];

// Check for ids with reserved names or prefixes
// - Reserved prefixes: "es/", "category/", "tag/", "virtual:"
let reserved_prefixes = vec!["es/", "category/", "tag/", "virtual:"];
let reserved_prefix = self.iter().all(|x| {
let id = x.get_id();
if reserved_prefixes.iter().any(|&p| id.starts_with(p)) {
error!("Id with reserved prefix found in publication file: {}", id);
false
} else {
true
}
});
valid.push(reserved_prefix);

// Check for duplicate ids
let mut ids: Vec<String> = vec![];
let duplication = self.iter().all(|x| {
let id = x.get_id();
if ids.contains(&id) {
error!("Duplicate id found in published.jsonc: {}", id);
error!("Duplicate id found in publication file: {}", id);
false
} else {
ids.push(id);
Expand All @@ -196,14 +236,14 @@ impl CynthiaPublicationListTrait for CynthiaPublicationList {
// - 404 page
let notfound_exists = self.get_notfound(config).is_some();
if !notfound_exists {
error!("404 page not found in published.jsonc: Add a page with id being either \"404\" or \"notfound\" or the id specified in the config.");
error!("404 page not found in publication file: Add a page with id being either \"404\" or \"notfound\" or the id specified in the config.");
}
valid.push(notfound_exists);

// - Root page
let root_exists = self.get_root().is_some();
if !root_exists {
error!("Root page not found in published.jsonc: Add a page with id being either \"root\" or \"/\"");
error!("Root page not found in publication file: Add a page with id being either \"root\" or \"/\"");
}
valid.push(root_exists);

Expand Down
Loading

0 comments on commit 52fec67

Please sign in to comment.