From 5ce39b045343a3d922b555ee39494a44a7e0f31d Mon Sep 17 00:00:00 2001 From: Mylloon Date: Wed, 24 Jan 2024 11:52:20 +0100 Subject: [PATCH] variable for data directory path --- src/config.rs | 14 ++++++++++++-- src/main.rs | 2 +- src/routes/blog.rs | 30 ++++++++++++++++++++---------- src/routes/contact.rs | 13 ++++++------- src/routes/portfolio.rs | 4 ++-- 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/src/config.rs b/src/config.rs index fd3a8cf..d1edb9e 100644 --- a/src/config.rs +++ b/src/config.rs @@ -69,13 +69,20 @@ impl FileConfig { } } +// Paths where files are stored +#[derive(Clone, Debug)] +pub struct Locations { + pub static_dir: String, + pub data_dir: String, +} + /// Configuration used internally in the app #[derive(Clone, Debug)] pub struct Config { /// Information given in the config file pub fc: FileConfig, /// Location where the static files are stored - pub static_location: String, + pub locations: Locations, /// Informations about templates pub tmpl: Template, } @@ -110,7 +117,10 @@ pub fn get_config(file_path: &str) -> Config { Config { fc: internal_config.to_owned(), - static_location: format!("{}/{}", files_root, static_dir), + locations: Locations { + static_dir: format!("{}/{}", files_root, static_dir), + data_dir: String::from("data"), + }, tmpl: Template { directory: format!("{}/{}", files_root, templates_dir), app_name: internal_config.app_name.unwrap(), diff --git a/src/main.rs b/src/main.rs index 5058cc3..289fa22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,7 +60,7 @@ async fn main() -> Result<()> { .service(portfolio::page) .service(setup::page) .service(web3::page) - .service(Files::new("/", config.static_location.to_owned())) + .service(Files::new("/", config.locations.static_dir.to_owned())) .default_service(web::to(not_found::page)) }) .bind(addr)? diff --git a/src/routes/blog.rs b/src/routes/blog.rs index 066e518..c40a36d 100644 --- a/src/routes/blog.rs +++ b/src/routes/blog.rs @@ -42,7 +42,7 @@ struct BlogIndexTemplate { #[once(time = 60)] fn build_index(config: Config) -> String { - let mut posts = get_posts("data/blog"); + let mut posts = get_posts(format!("{}/blog", config.locations.data_dir)); // Sort from newest to oldest posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); @@ -81,8 +81,8 @@ struct Post { impl Post { // Fetch the file content - fn fetch_content(&mut self) { - let blog_dir = "data/blog"; + fn fetch_content(&mut self, data_dir: &str) { + let blog_dir = format!("{}/blog", data_dir); let ext = ".md"; if let Some(file) = read_file( @@ -102,8 +102,8 @@ impl Hash for Post { } } -fn get_posts(location: &str) -> Vec { - let entries = match std::fs::read_dir(location) { +fn get_posts(location: String) -> Vec { + let entries = match std::fs::read_dir(&location) { Ok(res) => res .flatten() .filter(|f| match f.path().extension() { @@ -190,7 +190,12 @@ async fn page(path: web::Path<(String,)>, config: web::Data) -> impl Res fn build_post(file: String, config: Config) -> String { let mut post = None; - let (infos, toc) = get_post(&mut post, file, config.fc.name.unwrap_or_default()); + let (infos, toc) = get_post( + &mut post, + file, + config.fc.name.unwrap_or_default(), + config.locations.data_dir, + ); config.tmpl.render( "blog/post.html", @@ -206,8 +211,13 @@ fn build_post(file: String, config: Config) -> String { ) } -fn get_post(post: &mut Option, filename: String, name: String) -> (Infos, String) { - let blog_dir = "data/blog"; +fn get_post( + post: &mut Option, + filename: String, + name: String, + data_dir: String, +) -> (Infos, String) { + let blog_dir = format!("{}/blog", data_dir); let ext = ".md"; *post = read_file( @@ -268,7 +278,7 @@ async fn rss(config: web::Data) -> impl Responder { #[once(time = 10800)] // 3h fn build_rss(config: Config) -> String { - let mut posts = get_posts("data/blog"); + let mut posts = get_posts(format!("{}/blog", config.locations.data_dir)); // Sort from newest to oldest posts.sort_by_cached_key(|p| (p.date.year, p.date.month, p.date.day)); @@ -315,7 +325,7 @@ fn build_rss(config: Config) -> String { .iter_mut() .map(|p| { // Get post data - p.fetch_content(); + p.fetch_content(&config.locations.data_dir); // Build item Item { diff --git a/src/routes/contact.rs b/src/routes/contact.rs index 91b7775..6e6f690 100644 --- a/src/routes/contact.rs +++ b/src/routes/contact.rs @@ -38,13 +38,12 @@ struct ContactLink { } #[once(time = 60)] -fn find_links() -> Vec { - // TOML file location - let contacts_dir = "data/contacts"; +fn find_links(directory: String) -> Vec { + // TOML filename let toml_file = "links.toml"; // Read the TOML file and parse it - let toml_str = read_to_string(format!("{contacts_dir}/{toml_file}")).unwrap_or_default(); + let toml_str = read_to_string(format!("{directory}/{toml_file}")).unwrap_or_default(); let mut redirections = vec![]; match toml::de::from_str::(&toml_str) { @@ -74,9 +73,9 @@ fn find_links() -> Vec { #[routes] #[get("/{service}")] #[get("/{service}/{scope}")] -async fn service_redirection(req: HttpRequest) -> impl Responder { +async fn service_redirection(config: web::Data, req: HttpRequest) -> impl Responder { let info = req.match_info(); - let link = find_links() + let link = find_links(format!("{}/contacts", config.locations.data_dir)) .iter() // Find requested service .filter(|&x| x.service == *info.query("service")) @@ -123,7 +122,7 @@ fn remove_paragraphs(list: &mut [File]) { #[once(time = 60)] fn build_page(config: Config) -> String { - let contacts_dir = "data/contacts"; + let contacts_dir = format!("{}/contacts", config.locations.data_dir); let ext = ".md"; let socials_dir = "socials"; diff --git a/src/routes/portfolio.rs b/src/routes/portfolio.rs index e0fad8d..9b3f1ee 100644 --- a/src/routes/portfolio.rs +++ b/src/routes/portfolio.rs @@ -29,7 +29,7 @@ struct PortfolioTemplate<'a> { #[once(time = 60)] fn build_page(config: Config) -> String { - let projects_dir = "data/projects"; + let projects_dir = format!("{}/projects", config.locations.data_dir); let ext = ".md"; // Get apps @@ -39,7 +39,7 @@ fn build_page(config: Config) -> String { .collect::>(); let appdata = if apps.is_empty() { - (None, Some(projects_dir)) + (None, Some(projects_dir.as_str())) } else { (Some(apps), None) };