File size: 2,781 Bytes
cb1edad
 
 
c170de8
137c62e
15fc415
 
137c62e
2945665
12bb0ee
 
 
 
 
 
137c62e
 
12bb0ee
2945665
12bb0ee
 
137c62e
12bb0ee
 
 
 
 
 
 
 
 
137c62e
cb1edad
12bb0ee
71fae8b
12bb0ee
71fae8b
12bb0ee
4a505fb
12bb0ee
137c62e
12bb0ee
 
2945665
 
12bb0ee
2945665
12bb0ee
 
 
 
 
 
 
137c62e
12bb0ee
 
2945665
 
 
 
 
 
 
 
12bb0ee
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! 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.

pub mod cache;
pub mod config_parser;
pub mod engines;
pub mod search_results_handler;
pub mod server;
pub mod theme_handler;

use std::net::TcpListener;

use crate::server::routes;

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.
///
/// # 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
///
/// ```rust
/// use std::net::TcpListener;
/// use websurfx::{config_parser::parser::Config, run};
///
/// let config = Config::parse().unwrap();
/// let listener = TcpListener::bind("127.0.0.1:8080").expect("Failed to bind address");
/// let server = run(listener,config).expect("Failed to start server");
/// ```
pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> {
    let mut handlebars: Handlebars = Handlebars::new();

    let theme_folder_path: String = handle_different_theme_path()?;

    handlebars
        .register_templates_directory(".html", format!("{}/templates", theme_folder_path))
        .unwrap();

    let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars);

    let server = HttpServer::new(move || {
        App::new()
            .app_data(handlebars_ref.clone())
            .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", 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
            .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)
}