Merge pull request #55 from neon-mmd/put-the-themes-on-different-paths
Browse files- Cargo.lock +3 -3
- Cargo.toml +1 -1
- src/handler/mod.rs +1 -0
- src/handler/public_path_handler.rs +31 -0
- src/lib.rs +13 -3
- src/server/routes.rs +3 -1
Cargo.lock
CHANGED
@@ -1242,9 +1242,9 @@ dependencies = [
|
|
1242 |
|
1243 |
[[package]]
|
1244 |
name = "io-lifetimes"
|
1245 |
-
version = "1.0.
|
1246 |
source = "registry+https://github.com/rust-lang/crates.io-index"
|
1247 |
-
checksum = "
|
1248 |
dependencies = [
|
1249 |
"hermit-abi 0.3.1",
|
1250 |
"libc",
|
@@ -3319,7 +3319,7 @@ dependencies = [
|
|
3319 |
|
3320 |
[[package]]
|
3321 |
name = "websurfx"
|
3322 |
-
version = "0.
|
3323 |
dependencies = [
|
3324 |
"actix-files",
|
3325 |
"actix-web",
|
|
|
1242 |
|
1243 |
[[package]]
|
1244 |
name = "io-lifetimes"
|
1245 |
+
version = "1.0.11"
|
1246 |
source = "registry+https://github.com/rust-lang/crates.io-index"
|
1247 |
+
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
|
1248 |
dependencies = [
|
1249 |
"hermit-abi 0.3.1",
|
1250 |
"libc",
|
|
|
3319 |
|
3320 |
[[package]]
|
3321 |
name = "websurfx"
|
3322 |
+
version = "0.9.0"
|
3323 |
dependencies = [
|
3324 |
"actix-files",
|
3325 |
"actix-web",
|
Cargo.toml
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
[package]
|
2 |
name = "websurfx"
|
3 |
-
version = "0.
|
4 |
edition = "2021"
|
5 |
|
6 |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
|
|
1 |
[package]
|
2 |
name = "websurfx"
|
3 |
+
version = "0.9.0"
|
4 |
edition = "2021"
|
5 |
|
6 |
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
src/handler/mod.rs
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
pub mod public_path_handler;
|
src/handler/public_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 PUBLIC_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_public_path() -> Result<String, Error> {
|
21 |
+
if Path::new(format!("/opt/websurfx/{}/", PUBLIC_DIRECTORY_NAME).as_str()).exists() {
|
22 |
+
Ok(format!("/opt/websurfx/{}", PUBLIC_DIRECTORY_NAME))
|
23 |
+
} else if Path::new(format!("./{}/", PUBLIC_DIRECTORY_NAME).as_str()).exists() {
|
24 |
+
Ok(format!("./{}", PUBLIC_DIRECTORY_NAME))
|
25 |
+
} else {
|
26 |
+
Err(Error::new(
|
27 |
+
std::io::ErrorKind::NotFound,
|
28 |
+
"Themes (public) folder not found!!",
|
29 |
+
))
|
30 |
+
}
|
31 |
+
}
|
src/lib.rs
CHANGED
@@ -4,6 +4,7 @@
|
|
4 |
pub mod cache;
|
5 |
pub mod config_parser;
|
6 |
pub mod engines;
|
|
|
7 |
pub mod search_results_handler;
|
8 |
pub mod server;
|
9 |
|
@@ -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
|
|
|
4 |
pub mod cache;
|
5 |
pub mod config_parser;
|
6 |
pub mod engines;
|
7 |
+
pub mod handler;
|
8 |
pub mod search_results_handler;
|
9 |
pub mod server;
|
10 |
|
|
|
16 |
use actix_web::{dev::Server, middleware::Logger, web, App, HttpServer};
|
17 |
use config_parser::parser::Config;
|
18 |
use handlebars::Handlebars;
|
19 |
+
use handler::public_path_handler::handle_different_public_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 public_folder_path: String = handle_different_public_path()?;
|
45 |
+
|
46 |
handlebars
|
47 |
+
.register_templates_directory(".html", format!("{}/templates", public_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", public_folder_path))
|
60 |
+
.show_files_listing(),
|
61 |
+
)
|
62 |
+
.service(
|
63 |
+
fs::Files::new("/images", format!("{}/images", public_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
@@ -7,6 +7,7 @@ use std::fs::read_to_string;
|
|
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};
|
@@ -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))
|
|
|
7 |
use crate::{
|
8 |
cache::cacher::RedisCache,
|
9 |
config_parser::parser::Config,
|
10 |
+
handler::public_path_handler::handle_different_public_path,
|
11 |
search_results_handler::{aggregation_models::SearchResults, aggregator::aggregate},
|
12 |
};
|
13 |
use actix_web::{get, web, HttpRequest, HttpResponse};
|
|
|
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_public_path()?))?;
|
152 |
Ok(HttpResponse::Ok()
|
153 |
.content_type("text/plain; charset=ascii")
|
154 |
.body(page_content))
|