Pour tout problème contactez-nous par mail : support@froggit.fr | La FAQ :grey_question: | Rejoignez-nous sur le Chat :speech_balloon:

Skip to content
Snippets Groups Projects
main.rs 1.64 KiB
mod app_state;
mod service;
mod static_files;
mod testing;
mod tls_config;
mod website;
use actix_web::{App, HttpServer};
use actix_web_lab::middleware::RedirectHttps;
use app_state::AppState;
use static_files::StaticFilesManager;
use tls_config::tls_config;
use website::WebSite;

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    let app_state = AppState::new();

    env_logger::Builder::from_env(
        env_logger::Env::default().default_filter_or(&app_state.config.get_log_level()),
    )
    .init();

    let website = WebSite::load(&app_state.config);
    let mut static_files_manager = StaticFilesManager::new(&app_state).unwrap();
    static_files_manager.build_index();

    let host = app_state.config.host.clone();
    let port = app_state.config.port;
    let port_tls = app_state.config.port_tls;

    let srv_conf = tls_config(&app_state.config);

    let static_files_manager =
        actix_web::web::Data::new(std::sync::Mutex::new(static_files_manager));

    let app_state = actix_web::web::Data::new(std::sync::Mutex::new(app_state));

    HttpServer::new(move || {
        App::new()
            .wrap(actix_web::middleware::Logger::default())
            .wrap(actix_web::middleware::Compress::default())
            .wrap(RedirectHttps::default().to_port(port_tls))
            .app_data(actix_web::web::Data::clone(&app_state))
            .app_data(actix_web::web::Data::clone(&static_files_manager))
            .app_data(actix_web::web::Data::new(website.clone()))
            .service(service::page)
    })
    .bind(format!("{}:{}", host, port))?
    .bind_rustls(format!("{}:{}", host, port_tls), srv_conf)?
    .run()
    .await
}