|
|
|
|
|
|
|
pub mod cache; |
|
pub mod config; |
|
pub mod engines; |
|
pub mod handler; |
|
pub mod results; |
|
pub mod server; |
|
|
|
use std::net::TcpListener; |
|
|
|
use crate::server::routes; |
|
|
|
use actix_cors::Cors; |
|
use actix_files as fs; |
|
use actix_web::{dev::Server, http::header, middleware::Logger, web, App, HttpServer}; |
|
use config::parser::Config; |
|
use handlebars::Handlebars; |
|
use handler::paths::{file_path, FileType}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn run(listener: TcpListener, config: Config) -> std::io::Result<Server> { |
|
let mut handlebars: Handlebars = Handlebars::new(); |
|
|
|
let public_folder_path: String = file_path(FileType::Theme)?; |
|
|
|
handlebars |
|
.register_templates_directory(".html", format!("{}/templates", public_folder_path)) |
|
.unwrap(); |
|
|
|
let handlebars_ref: web::Data<Handlebars> = web::Data::new(handlebars); |
|
|
|
let cloned_config_threads_opt: u8 = config.threads; |
|
|
|
let server = HttpServer::new(move || { |
|
let cors: Cors = Cors::default() |
|
.allow_any_origin() |
|
.allowed_methods(vec!["GET"]) |
|
.allowed_headers(vec![ |
|
header::ORIGIN, |
|
header::CONTENT_TYPE, |
|
header::REFERER, |
|
header::COOKIE, |
|
]); |
|
|
|
App::new() |
|
.app_data(handlebars_ref.clone()) |
|
.app_data(web::Data::new(config.clone())) |
|
.wrap(cors) |
|
.wrap(Logger::default()) |
|
|
|
.service( |
|
fs::Files::new("/static", format!("{}/static", public_folder_path)) |
|
.show_files_listing(), |
|
) |
|
.service( |
|
fs::Files::new("/images", format!("{}/images", public_folder_path)) |
|
.show_files_listing(), |
|
) |
|
.service(routes::robots_data) |
|
.service(routes::index) |
|
.service(routes::search) |
|
.service(routes::about) |
|
.service(routes::settings) |
|
.default_service(web::route().to(routes::not_found)) |
|
}) |
|
.workers(cloned_config_threads_opt as usize) |
|
|
|
.listen(listener)? |
|
.run(); |
|
Ok(server) |
|
} |
|
|