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 2.08 KiB
Newer Older
peterrabbit's avatar
peterrabbit committed
mod app_state;
peterrabbit's avatar
peterrabbit committed
mod static_files;
peterrabbit's avatar
peterrabbit committed
mod tls_config;
peterrabbit's avatar
peterrabbit committed
mod website;
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
peterrabbit's avatar
peterrabbit committed
use app_state::AppState;
peterrabbit's avatar
peterrabbit committed
use static_files::StaticFilesManager;
peterrabbit's avatar
peterrabbit committed
use std::path::PathBuf;
peterrabbit's avatar
peterrabbit committed
use tls_config::tls_config;
peterrabbit's avatar
peterrabbit committed
use website::WebSite;

#[get("/{pth:.*}")]
peterrabbit's avatar
peterrabbit committed
async fn page(
    website: actix_web::web::Data<WebSite>,
peterrabbit's avatar
peterrabbit committed
    pth: actix_web::web::Path<PathBuf>,
peterrabbit's avatar
peterrabbit committed
) -> impl Responder {
peterrabbit's avatar
peterrabbit committed
    let pth = pth.into_inner();
peterrabbit's avatar
peterrabbit committed
    match website.get_page_by_url(&pth) {
peterrabbit's avatar
peterrabbit committed
        Some(page) => HttpResponse::Ok().body(page.html_doc.to_string()),
        None => HttpResponse::NotFound().body(format!("Not found {}", pth.display())),
peterrabbit's avatar
peterrabbit committed
    }
}

#[get("/admin/dashboard")]
async fn admin_dashboard() -> impl Responder {
    HttpResponse::Ok().body("Admin")
}

#[get("/admin/login")]
async fn admin_login() -> impl Responder {
    HttpResponse::Ok().body("Login")
peterrabbit's avatar
peterrabbit committed
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
peterrabbit's avatar
peterrabbit committed
    let app_state = AppState::new();
    let website = WebSite::load(&app_state.config);
    let mut static_files_manager = StaticFilesManager::new(&app_state).unwrap();
peterrabbit's avatar
peterrabbit committed
    static_files_manager.build_index().unwrap();
peterrabbit's avatar
peterrabbit committed
    let host = app_state.config.host.clone();
    let port = app_state.config.port;
    let port_tls = app_state.config.port_tls;
peterrabbit's avatar
peterrabbit committed
    let srv_conf = tls_config(&app_state.config);
peterrabbit's avatar
peterrabbit committed
    let static_files_manager =
        actix_web::web::Data::new(std::sync::Mutex::new(static_files_manager));

peterrabbit's avatar
peterrabbit committed
    let app_state = actix_web::web::Data::new(std::sync::Mutex::new(app_state));

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