From 29456650f1c86eb6bf690c92b3006ac8d1cfceb7 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Wed, 24 May 2023 17:37:41 +0300 Subject: [PATCH 1/4] feat: add ability to put the themes folder on different paths --- src/lib.rs | 16 ++++++++++--- src/server/routes.rs | 4 +++- src/theme_handler/mod.rs | 1 + src/theme_handler/theme_path_handler.rs | 31 +++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 src/theme_handler/mod.rs create mode 100644 src/theme_handler/theme_path_handler.rs diff --git a/src/lib.rs b/src/lib.rs index c234658..0763c96 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod config_parser; pub mod engines; pub mod search_results_handler; pub mod server; +pub mod theme_handler; use std::net::TcpListener; @@ -15,6 +16,7 @@ use actix_files as fs; use actix_web::{dev::Server, middleware::Logger, web, App, HttpServer}; use config_parser::parser::Config; use handlebars::Handlebars; +use theme_handler::theme_path_handler::handle_different_theme_path; /// Runs the web server on the provided TCP listener and returns a `Server` instance. /// @@ -39,8 +41,10 @@ use handlebars::Handlebars; pub fn run(listener: TcpListener, config: Config) -> std::io::Result { let mut handlebars: Handlebars = Handlebars::new(); + let theme_folder_path: String = handle_different_theme_path()?; + handlebars - .register_templates_directory(".html", "./public/templates") + .register_templates_directory(".html", format!("{}/templates", theme_folder_path)) .unwrap(); let handlebars_ref: web::Data = web::Data::new(handlebars); @@ -51,8 +55,14 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result { .app_data(web::Data::new(config.clone())) .wrap(Logger::default()) // added logging middleware for logging. // Serve images and static files (css and js files). - .service(fs::Files::new("/static", "./public/static").show_files_listing()) - .service(fs::Files::new("/images", "./public/images").show_files_listing()) + .service( + fs::Files::new("/static", format!("{}/static", theme_folder_path)) + .show_files_listing(), + ) + .service( + fs::Files::new("/images", format!("{}/images", theme_folder_path)) + .show_files_listing(), + ) .service(routes::robots_data) // robots.txt .service(routes::index) // index page .service(routes::search) // search page diff --git a/src/server/routes.rs b/src/server/routes.rs index 0f84cc9..bb6be27 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -8,6 +8,7 @@ use crate::{ cache::cacher::RedisCache, config_parser::parser::Config, search_results_handler::{aggregation_models::SearchResults, aggregator::aggregate}, + theme_handler::theme_path_handler::handle_different_theme_path, }; use actix_web::{get, web, HttpRequest, HttpResponse}; use handlebars::Handlebars; @@ -146,7 +147,8 @@ pub async fn search( /// Handles the route of robots.txt page of the `websurfx` meta search engine website. #[get("/robots.txt")] pub async fn robots_data(_req: HttpRequest) -> Result> { - let page_content: String = read_to_string("./public/robots.txt")?; + let page_content: String = + read_to_string(format!("{}/robots.txt", handle_different_theme_path()?))?; Ok(HttpResponse::Ok() .content_type("text/plain; charset=ascii") .body(page_content)) diff --git a/src/theme_handler/mod.rs b/src/theme_handler/mod.rs new file mode 100644 index 0000000..3bbca9d --- /dev/null +++ b/src/theme_handler/mod.rs @@ -0,0 +1 @@ +pub mod theme_path_handler; diff --git a/src/theme_handler/theme_path_handler.rs b/src/theme_handler/theme_path_handler.rs new file mode 100644 index 0000000..df3e5cc --- /dev/null +++ b/src/theme_handler/theme_path_handler.rs @@ -0,0 +1,31 @@ +//! This module provides the functionality to handle theme folder present on different paths and +//! provide one appropriate path on which it is present and can be used. + +use std::io::Error; +use std::path::Path; + +// ------- Constants -------- +static THEME_DIRECTORY_NAME: &str = "public"; + +/// A function which returns an appropriate theme directory path checking if the theme +/// directory exists on that path. +/// +/// # Error +/// +/// Returns a `Theme (public) folder not found!!` error if the theme folder is not present under following +/// paths which are: +/// 1. `/opt/websurfx` if it not present here then it fallbacks to the next one (2) +/// 2. Under project folder ( or codebase in other words) if it is not present +/// here then it returns an error as mentioned above. +pub fn handle_different_theme_path() -> Result { + if Path::new(format!("/opt/websurfx/{}/", THEME_DIRECTORY_NAME).as_str()).exists() { + Ok(format!("/opt/websurfx/{}", THEME_DIRECTORY_NAME)) + } else if Path::new(format!("./{}/", THEME_DIRECTORY_NAME).as_str()).exists() { + Ok(format!("./{}", THEME_DIRECTORY_NAME)) + } else { + Err(Error::new( + std::io::ErrorKind::NotFound, + "Themes (public) folder not found!!", + )) + } +} From 42feda0c460fa47d0adf2553197b519aef94c295 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Wed, 24 May 2023 17:40:21 +0300 Subject: [PATCH 2/4] chore: Bump version to v0.9.0 --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index d19acb2..76335f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "websurfx" -version = "0.8.0" +version = "0.9.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html From 211f170178b12c6c40a033b7a70a91156132a89f Mon Sep 17 00:00:00 2001 From: neon_arch Date: Wed, 24 May 2023 17:46:35 +0300 Subject: [PATCH 3/4] chore: fix lock error during build --- Cargo.lock | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 02bf14f..ef32619 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1242,9 +1242,9 @@ dependencies = [ [[package]] name = "io-lifetimes" -version = "1.0.10" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9c66c74d2ae7e79a5a8f7ac924adbe38ee42a859c6539ad869eb51f0b52dc220" +checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2" dependencies = [ "hermit-abi 0.3.1", "libc", @@ -3319,7 +3319,7 @@ dependencies = [ [[package]] name = "websurfx" -version = "0.8.0" +version = "0.9.0" dependencies = [ "actix-files", "actix-web", From dc3308cb5f5e28f6dbebe80aafdc2bd2efcf2532 Mon Sep 17 00:00:00 2001 From: neon_arch Date: Thu, 25 May 2023 11:50:37 +0300 Subject: [PATCH 4/4] chore: rename from theme to public --- src/handler/mod.rs | 1 + .../public_path_handler.rs} | 12 ++++++------ src/lib.rs | 12 ++++++------ src/server/routes.rs | 4 ++-- src/theme_handler/mod.rs | 1 - 5 files changed, 15 insertions(+), 15 deletions(-) create mode 100644 src/handler/mod.rs rename src/{theme_handler/theme_path_handler.rs => handler/public_path_handler.rs} (68%) delete mode 100644 src/theme_handler/mod.rs diff --git a/src/handler/mod.rs b/src/handler/mod.rs new file mode 100644 index 0000000..daa5212 --- /dev/null +++ b/src/handler/mod.rs @@ -0,0 +1 @@ +pub mod public_path_handler; diff --git a/src/theme_handler/theme_path_handler.rs b/src/handler/public_path_handler.rs similarity index 68% rename from src/theme_handler/theme_path_handler.rs rename to src/handler/public_path_handler.rs index df3e5cc..b99283e 100644 --- a/src/theme_handler/theme_path_handler.rs +++ b/src/handler/public_path_handler.rs @@ -5,7 +5,7 @@ use std::io::Error; use std::path::Path; // ------- Constants -------- -static THEME_DIRECTORY_NAME: &str = "public"; +static PUBLIC_DIRECTORY_NAME: &str = "public"; /// A function which returns an appropriate theme directory path checking if the theme /// directory exists on that path. @@ -17,11 +17,11 @@ static THEME_DIRECTORY_NAME: &str = "public"; /// 1. `/opt/websurfx` if it not present here then it fallbacks to the next one (2) /// 2. Under project folder ( or codebase in other words) if it is not present /// here then it returns an error as mentioned above. -pub fn handle_different_theme_path() -> Result { - if Path::new(format!("/opt/websurfx/{}/", THEME_DIRECTORY_NAME).as_str()).exists() { - Ok(format!("/opt/websurfx/{}", THEME_DIRECTORY_NAME)) - } else if Path::new(format!("./{}/", THEME_DIRECTORY_NAME).as_str()).exists() { - Ok(format!("./{}", THEME_DIRECTORY_NAME)) +pub fn handle_different_public_path() -> Result { + if Path::new(format!("/opt/websurfx/{}/", PUBLIC_DIRECTORY_NAME).as_str()).exists() { + Ok(format!("/opt/websurfx/{}", PUBLIC_DIRECTORY_NAME)) + } else if Path::new(format!("./{}/", PUBLIC_DIRECTORY_NAME).as_str()).exists() { + Ok(format!("./{}", PUBLIC_DIRECTORY_NAME)) } else { Err(Error::new( std::io::ErrorKind::NotFound, diff --git a/src/lib.rs b/src/lib.rs index 0763c96..6b6d4fe 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,9 +4,9 @@ pub mod cache; pub mod config_parser; pub mod engines; +pub mod handler; pub mod search_results_handler; pub mod server; -pub mod theme_handler; use std::net::TcpListener; @@ -16,7 +16,7 @@ use actix_files as fs; use actix_web::{dev::Server, middleware::Logger, web, App, HttpServer}; use config_parser::parser::Config; use handlebars::Handlebars; -use theme_handler::theme_path_handler::handle_different_theme_path; +use handler::public_path_handler::handle_different_public_path; /// Runs the web server on the provided TCP listener and returns a `Server` instance. /// @@ -41,10 +41,10 @@ use theme_handler::theme_path_handler::handle_different_theme_path; pub fn run(listener: TcpListener, config: Config) -> std::io::Result { let mut handlebars: Handlebars = Handlebars::new(); - let theme_folder_path: String = handle_different_theme_path()?; + let public_folder_path: String = handle_different_public_path()?; handlebars - .register_templates_directory(".html", format!("{}/templates", theme_folder_path)) + .register_templates_directory(".html", format!("{}/templates", public_folder_path)) .unwrap(); let handlebars_ref: web::Data = web::Data::new(handlebars); @@ -56,11 +56,11 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result { .wrap(Logger::default()) // added logging middleware for logging. // Serve images and static files (css and js files). .service( - fs::Files::new("/static", format!("{}/static", theme_folder_path)) + fs::Files::new("/static", format!("{}/static", public_folder_path)) .show_files_listing(), ) .service( - fs::Files::new("/images", format!("{}/images", theme_folder_path)) + fs::Files::new("/images", format!("{}/images", public_folder_path)) .show_files_listing(), ) .service(routes::robots_data) // robots.txt diff --git a/src/server/routes.rs b/src/server/routes.rs index bb6be27..ead1612 100644 --- a/src/server/routes.rs +++ b/src/server/routes.rs @@ -7,8 +7,8 @@ use std::fs::read_to_string; use crate::{ cache::cacher::RedisCache, config_parser::parser::Config, + handler::public_path_handler::handle_different_public_path, search_results_handler::{aggregation_models::SearchResults, aggregator::aggregate}, - theme_handler::theme_path_handler::handle_different_theme_path, }; use actix_web::{get, web, HttpRequest, HttpResponse}; use handlebars::Handlebars; @@ -148,7 +148,7 @@ pub async fn search( #[get("/robots.txt")] pub async fn robots_data(_req: HttpRequest) -> Result> { let page_content: String = - read_to_string(format!("{}/robots.txt", handle_different_theme_path()?))?; + read_to_string(format!("{}/robots.txt", handle_different_public_path()?))?; Ok(HttpResponse::Ok() .content_type("text/plain; charset=ascii") .body(page_content)) diff --git a/src/theme_handler/mod.rs b/src/theme_handler/mod.rs deleted file mode 100644 index 3bbca9d..0000000 --- a/src/theme_handler/mod.rs +++ /dev/null @@ -1 +0,0 @@ -pub mod theme_path_handler;