Spaces:
Running
Running
neon_arch
commited on
Commit
·
2945665
1
Parent(s):
707bf15
feat: add ability to put the themes folder on different paths
Browse files- src/lib.rs +13 -3
- src/server/routes.rs +3 -1
- src/theme_handler/mod.rs +1 -0
- src/theme_handler/theme_path_handler.rs +31 -0
src/lib.rs
CHANGED
|
@@ -6,6 +6,7 @@ pub mod config_parser;
|
|
| 6 |
pub mod engines;
|
| 7 |
pub mod search_results_handler;
|
| 8 |
pub mod server;
|
|
|
|
| 9 |
|
| 10 |
use std::net::TcpListener;
|
| 11 |
|
|
@@ -15,6 +16,7 @@ use actix_files as fs;
|
|
| 15 |
use actix_web::{dev::Server, middleware::Logger, web, App, HttpServer};
|
| 16 |
use config_parser::parser::Config;
|
| 17 |
use handlebars::Handlebars;
|
|
|
|
| 18 |
|
| 19 |
/// Runs the web server on the provided TCP listener and returns a `Server` instance.
|
| 20 |
///
|
|
@@ -39,8 +41,10 @@ use handlebars::Handlebars;
|
|
| 39 |
pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
| 40 |
let mut handlebars: Handlebars = Handlebars::new();
|
| 41 |
|
|
|
|
|
|
|
| 42 |
handlebars
|
| 43 |
-
.register_templates_directory(".html", "
|
| 44 |
.unwrap();
|
| 45 |
|
| 46 |
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
|
|
@@ -51,8 +55,14 @@ pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
|
| 51 |
.app_data(web::Data::new(config.clone()))
|
| 52 |
.wrap(Logger::default()) // added logging middleware for logging.
|
| 53 |
// Serve images and static files (css and js files).
|
| 54 |
-
.service(
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
.service(routes::robots_data) // robots.txt
|
| 57 |
.service(routes::index) // index page
|
| 58 |
.service(routes::search) // search page
|
|
|
|
| 6 |
pub mod engines;
|
| 7 |
pub mod search_results_handler;
|
| 8 |
pub mod server;
|
| 9 |
+
pub mod theme_handler;
|
| 10 |
|
| 11 |
use std::net::TcpListener;
|
| 12 |
|
|
|
|
| 16 |
use actix_web::{dev::Server, middleware::Logger, web, App, HttpServer};
|
| 17 |
use config_parser::parser::Config;
|
| 18 |
use handlebars::Handlebars;
|
| 19 |
+
use theme_handler::theme_path_handler::handle_different_theme_path;
|
| 20 |
|
| 21 |
/// Runs the web server on the provided TCP listener and returns a `Server` instance.
|
| 22 |
///
|
|
|
|
| 41 |
pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
| 42 |
let mut handlebars: Handlebars = Handlebars::new();
|
| 43 |
|
| 44 |
+
let theme_folder_path: String = handle_different_theme_path()?;
|
| 45 |
+
|
| 46 |
handlebars
|
| 47 |
+
.register_templates_directory(".html", format!("{}/templates", theme_folder_path))
|
| 48 |
.unwrap();
|
| 49 |
|
| 50 |
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
|
|
|
|
| 55 |
.app_data(web::Data::new(config.clone()))
|
| 56 |
.wrap(Logger::default()) // added logging middleware for logging.
|
| 57 |
// Serve images and static files (css and js files).
|
| 58 |
+
.service(
|
| 59 |
+
fs::Files::new("/static", format!("{}/static", theme_folder_path))
|
| 60 |
+
.show_files_listing(),
|
| 61 |
+
)
|
| 62 |
+
.service(
|
| 63 |
+
fs::Files::new("/images", format!("{}/images", theme_folder_path))
|
| 64 |
+
.show_files_listing(),
|
| 65 |
+
)
|
| 66 |
.service(routes::robots_data) // robots.txt
|
| 67 |
.service(routes::index) // index page
|
| 68 |
.service(routes::search) // search page
|
src/server/routes.rs
CHANGED
|
@@ -8,6 +8,7 @@ use crate::{
|
|
| 8 |
cache::cacher::RedisCache,
|
| 9 |
config_parser::parser::Config,
|
| 10 |
search_results_handler::{aggregation_models::SearchResults, aggregator::aggregate},
|
|
|
|
| 11 |
};
|
| 12 |
use actix_web::{get, web, HttpRequest, HttpResponse};
|
| 13 |
use handlebars::Handlebars;
|
|
@@ -146,7 +147,8 @@ pub async fn search(
|
|
| 146 |
/// Handles the route of robots.txt page of the `websurfx` meta search engine website.
|
| 147 |
#[get("/robots.txt")]
|
| 148 |
pub async fn robots_data(_req: HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
| 149 |
-
let page_content: String =
|
|
|
|
| 150 |
Ok(HttpResponse::Ok()
|
| 151 |
.content_type("text/plain; charset=ascii")
|
| 152 |
.body(page_content))
|
|
|
|
| 8 |
cache::cacher::RedisCache,
|
| 9 |
config_parser::parser::Config,
|
| 10 |
search_results_handler::{aggregation_models::SearchResults, aggregator::aggregate},
|
| 11 |
+
theme_handler::theme_path_handler::handle_different_theme_path,
|
| 12 |
};
|
| 13 |
use actix_web::{get, web, HttpRequest, HttpResponse};
|
| 14 |
use handlebars::Handlebars;
|
|
|
|
| 147 |
/// Handles the route of robots.txt page of the `websurfx` meta search engine website.
|
| 148 |
#[get("/robots.txt")]
|
| 149 |
pub async fn robots_data(_req: HttpRequest) -> Result<HttpResponse, Box<dyn std::error::Error>> {
|
| 150 |
+
let page_content: String =
|
| 151 |
+
read_to_string(format!("{}/robots.txt", handle_different_theme_path()?))?;
|
| 152 |
Ok(HttpResponse::Ok()
|
| 153 |
.content_type("text/plain; charset=ascii")
|
| 154 |
.body(page_content))
|
src/theme_handler/mod.rs
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
pub mod theme_path_handler;
|
src/theme_handler/theme_path_handler.rs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//! This module provides the functionality to handle theme folder present on different paths and
|
| 2 |
+
//! provide one appropriate path on which it is present and can be used.
|
| 3 |
+
|
| 4 |
+
use std::io::Error;
|
| 5 |
+
use std::path::Path;
|
| 6 |
+
|
| 7 |
+
// ------- Constants --------
|
| 8 |
+
static THEME_DIRECTORY_NAME: &str = "public";
|
| 9 |
+
|
| 10 |
+
/// A function which returns an appropriate theme directory path checking if the theme
|
| 11 |
+
/// directory exists on that path.
|
| 12 |
+
///
|
| 13 |
+
/// # Error
|
| 14 |
+
///
|
| 15 |
+
/// Returns a `Theme (public) folder not found!!` error if the theme folder is not present under following
|
| 16 |
+
/// paths which are:
|
| 17 |
+
/// 1. `/opt/websurfx` if it not present here then it fallbacks to the next one (2)
|
| 18 |
+
/// 2. Under project folder ( or codebase in other words) if it is not present
|
| 19 |
+
/// here then it returns an error as mentioned above.
|
| 20 |
+
pub fn handle_different_theme_path() -> Result<String, Error> {
|
| 21 |
+
if Path::new(format!("/opt/websurfx/{}/", THEME_DIRECTORY_NAME).as_str()).exists() {
|
| 22 |
+
Ok(format!("/opt/websurfx/{}", THEME_DIRECTORY_NAME))
|
| 23 |
+
} else if Path::new(format!("./{}/", THEME_DIRECTORY_NAME).as_str()).exists() {
|
| 24 |
+
Ok(format!("./{}", THEME_DIRECTORY_NAME))
|
| 25 |
+
} else {
|
| 26 |
+
Err(Error::new(
|
| 27 |
+
std::io::ErrorKind::NotFound,
|
| 28 |
+
"Themes (public) folder not found!!",
|
| 29 |
+
))
|
| 30 |
+
}
|
| 31 |
+
}
|