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-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-07-03 13:30:25 -04:00
|
|
|
pub mod results;
|
2023-04-30 11:16:08 -04:00
|
|
|
pub mod server;
|
2023-04-27 22:39:58 -04:00
|
|
|
|
|
|
|
use std::net::TcpListener;
|
|
|
|
|
|
|
|
use crate::server::routes;
|
|
|
|
|
|
|
|
use actix_files as fs;
|
2023-04-30 11:16:08 -04:00
|
|
|
use actix_web::{dev::Server, middleware::Logger, web, App, HttpServer};
|
2023-07-03 13:30:25 -04:00
|
|
|
use config::parser::Config;
|
2023-04-27 22:39:58 -04:00
|
|
|
use handlebars::Handlebars;
|
2023-07-04 18:11:30 -04:00
|
|
|
use handler::public_paths::public_path;
|
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-07-03 13:38:17 -04:00
|
|
|
/// use websurfx::{config::parser::Config, run};
|
2023-04-27 22:39:58 -04:00
|
|
|
///
|
2023-04-30 13:04:31 -04:00
|
|
|
/// let config = Config::parse().unwrap();
|
2023-04-27 22:39:58 -04:00
|
|
|
/// let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind address");
|
2023-04-30 12:24:16 -04:00
|
|
|
/// let server = run(listener,config).expect("Failed to start server");
|
2023-04-27 22:39:58 -04:00
|
|
|
/// ```
|
2023-04-30 11:16:08 -04:00
|
|
|
pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
|
2023-04-27 22:39:58 -04:00
|
|
|
let mut handlebars: Handlebars = Handlebars::new();
|
|
|
|
|
2023-07-04 18:11:30 -04:00
|
|
|
let public_folder_path: String = public_path()?;
|
2023-05-24 10:37:41 -04:00
|
|
|
|
2023-04-27 22:39:58 -04:00
|
|
|
handlebars
|
2023-05-25 04:50:37 -04:00
|
|
|
.register_templates_directory(".html", format!("{}/templates", public_folder_path))
|
2023-04-27 22:39:58 -04:00
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);
|
|
|
|
|
|
|
|
let server = HttpServer::new(move || {
|
|
|
|
App::new()
|
|
|
|
.app_data(handlebars_ref.clone())
|
2023-04-30 11:16:08 -04:00
|
|
|
.app_data(web::Data::new(config.clone()))
|
2023-04-27 22:39:58 -04:00
|
|
|
.wrap(Logger::default()) // added logging middleware for logging.
|
|
|
|
// 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-04-27 22:39:58 -04:00
|
|
|
.service(routes::robots_data) // robots.txt
|
|
|
|
.service(routes::index) // index page
|
|
|
|
.service(routes::search) // search page
|
|
|
|
.service(routes::about) // about page
|
|
|
|
.service(routes::settings) // settings page
|
|
|
|
.default_service(web::route().to(routes::not_found)) // error page
|
|
|
|
})
|
|
|
|
// 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)
|
|
|
|
}
|