2023-04-28 07:43:02 -04:00
|
|
|
//! This main library module provides the functionality to provide and handle the Tcp server
|
|
|
|
//! and register all the routes for the `websurfx` meta search engine website.
|
|
|
|
|
2023-09-03 12:21:23 -04:00
|
|
|
#![forbid(unsafe_code, clippy::panic)]
|
|
|
|
#![deny(missing_docs, clippy::missing_docs_in_private_items, clippy::perf)]
|
|
|
|
#![warn(clippy::cognitive_complexity, rust_2018_idioms)]
|
|
|
|
|
2023-05-02 04:58:21 -04:00
|
|
|
pub mod cache;
|
2023-07-03 13:30:25 -04:00
|
|
|
pub mod config;
|
2023-04-22 07:35:07 -04:00
|
|
|
pub mod engines;
|
2023-05-25 04:50:37 -04:00
|
|
|
pub mod handler;
|
2023-09-03 13:50:50 -04:00
|
|
|
pub mod models;
|
2023-07-03 13:30:25 -04:00
|
|
|
pub mod results;
|
2023-04-30 11:16:08 -04:00
|
|
|
pub mod server;
|
2023-11-18 13:27:49 -05:00
|
|
|
pub mod templates;
|
2023-04-27 22:39:58 -04:00
|
|
|
|
|
|
|
use std::net::TcpListener;
|
|
|
|
|
2023-09-03 13:50:50 -04:00
|
|
|
use crate::server::router;
|
2023-04-27 22:39:58 -04:00
|
|
|
|
2023-08-03 10:44:13 -04:00
|
|
|
use actix_cors::Cors;
|
2023-04-27 22:39:58 -04:00
|
|
|
use actix_files as fs;
|
2023-09-02 13:22:24 -04:00
|
|
|
use actix_governor::{Governor, GovernorConfigBuilder};
|
2023-12-31 04:57:50 -05:00
|
|
|
use actix_web::{
|
|
|
|
dev::Server,
|
|
|
|
http::header,
|
|
|
|
middleware::{Compress, Logger},
|
|
|
|
web, App, HttpServer,
|
|
|
|
};
|
2023-11-28 01:17:35 -05:00
|
|
|
use cache::cacher::{Cacher, SharedCache};
|
2023-07-03 13:30:25 -04:00
|
|
|
use config::parser::Config;
|
2023-11-27 01:45:28 -05:00
|
|
|
use handler::{file_path, FileType};
|
2023-04-27 22:39:58 -04:00
|
|
|
|
|
|
|
/// Runs the web server on the provided TCP listener and returns a `Server` instance.
|
2023-04-30 11:16:08 -04:00
|
|
|
///
|
2023-04-27 22:39:58 -04:00
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `listener` - A `TcpListener` instance representing the address and port to listen on.
|
|
|
|
///
|
|
|
|
/// # Returns
|
|
|
|
///
|
|
|
|
/// Returns a `Result` containing a `Server` instance on success, or an `std::io::Error` on failure.
|
|
|
|
///
|
|
|
|
/// # Example
|
2023-04-30 11:16:08 -04:00
|
|
|
///
|
2023-04-28 07:43:02 -04:00
|
|
|
/// ```rust
|
2023-04-27 22:39:58 -04:00
|
|
|
/// use std::net::TcpListener;
|
2023-11-28 01:17:35 -05:00
|
|
|
/// use websurfx::{config::parser::Config, run, cache::cacher::create_cache};
|
2023-04-27 22:39:58 -04:00
|
|
|
///
|
2023-11-28 01:17:35 -05:00
|
|
|
/// #[tokio::main]
|
|
|
|
/// async fn main(){
|
|
|
|
/// let config = Config::parse(true).unwrap();
|
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind address");
|
|
|
|
/// let cache = create_cache(&config).await;
|
|
|
|
/// let server = run(listener,config,cache).expect("Failed to start server");
|
|
|
|
/// }
|
2023-04-27 22:39:58 -04:00
|
|
|
/// ```
|
2023-11-28 01:17:35 -05:00
|
|
|
pub fn run(
|
|
|
|
listener: TcpListener,
|
|
|
|
config: Config,
|
|
|
|
cache: impl Cacher + 'static,
|
|
|
|
) -> std::io::Result<Server> {
|
2023-08-27 14:07:57 -04:00
|
|
|
let public_folder_path: &str = file_path(FileType::Theme)?;
|
2023-05-24 10:37:41 -04:00
|
|
|
|
2023-08-02 13:05:39 -04:00
|
|
|
let cloned_config_threads_opt: u8 = config.threads;
|
|
|
|
|
2023-09-09 12:17:29 -04:00
|
|
|
let cache = web::Data::new(SharedCache::new(cache));
|
|
|
|
|
2023-04-27 22:39:58 -04:00
|
|
|
let server = HttpServer::new(move || {
|
2023-08-03 10:44:13 -04:00
|
|
|
let cors: Cors = Cors::default()
|
|
|
|
.allow_any_origin()
|
|
|
|
.allowed_methods(vec!["GET"])
|
|
|
|
.allowed_headers(vec![
|
|
|
|
header::ORIGIN,
|
|
|
|
header::CONTENT_TYPE,
|
|
|
|
header::REFERER,
|
|
|
|
header::COOKIE,
|
|
|
|
]);
|
|
|
|
|
2023-04-27 22:39:58 -04:00
|
|
|
App::new()
|
2023-12-31 04:57:50 -05:00
|
|
|
// Compress the responses provided by the server for the client requests.
|
|
|
|
.wrap(Compress::default())
|
2023-09-02 13:22:24 -04:00
|
|
|
.wrap(Logger::default()) // added logging middleware for logging.
|
2023-04-30 11:16:08 -04:00
|
|
|
.app_data(web::Data::new(config.clone()))
|
2023-09-09 12:17:29 -04:00
|
|
|
.app_data(cache.clone())
|
2023-08-03 10:44:13 -04:00
|
|
|
.wrap(cors)
|
2023-09-02 13:22:24 -04:00
|
|
|
.wrap(Governor::new(
|
|
|
|
&GovernorConfigBuilder::default()
|
2023-09-11 02:28:31 -04:00
|
|
|
.per_second(config.rate_limiter.time_limit as u64)
|
|
|
|
.burst_size(config.rate_limiter.number_of_requests as u32)
|
2023-09-02 13:22:24 -04:00
|
|
|
.finish()
|
|
|
|
.unwrap(),
|
|
|
|
))
|
2023-04-27 22:39:58 -04:00
|
|
|
// Serve images and static files (css and js files).
|
2023-05-24 10:37:41 -04:00
|
|
|
.service(
|
2023-05-25 04:50:37 -04:00
|
|
|
fs::Files::new("/static", format!("{}/static", public_folder_path))
|
2023-05-24 10:37:41 -04:00
|
|
|
.show_files_listing(),
|
|
|
|
)
|
|
|
|
.service(
|
2023-05-25 04:50:37 -04:00
|
|
|
fs::Files::new("/images", format!("{}/images", public_folder_path))
|
2023-05-24 10:37:41 -04:00
|
|
|
.show_files_listing(),
|
|
|
|
)
|
2023-09-03 13:50:50 -04:00
|
|
|
.service(router::robots_data) // robots.txt
|
|
|
|
.service(router::index) // index page
|
2023-09-03 14:03:58 -04:00
|
|
|
.service(server::routes::search::search) // search page
|
2023-09-03 13:50:50 -04:00
|
|
|
.service(router::about) // about page
|
|
|
|
.service(router::settings) // settings page
|
|
|
|
.default_service(web::route().to(router::not_found)) // error page
|
2023-04-27 22:39:58 -04:00
|
|
|
})
|
2023-08-02 13:05:39 -04:00
|
|
|
.workers(cloned_config_threads_opt as usize)
|
2023-04-27 22:39:58 -04:00
|
|
|
// Start server on 127.0.0.1 with the user provided port number. for example 127.0.0.1:8080.
|
|
|
|
.listen(listener)?
|
|
|
|
.run();
|
|
|
|
Ok(server)
|
|
|
|
}
|