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 3.12 KiB
Newer Older
peterrabbit's avatar
peterrabbit committed
mod website;
use actix_web::{get, App, HttpResponse, HttpServer, Responder};
peterrabbit's avatar
peterrabbit committed
use website::WebSite;

fn load_website_template() -> WebSite {
    let site_template_path = std::env::current_dir()
        .unwrap()
        .join("templates")
        .join("new_website.json");
    let site_template = std::fs::read_to_string(site_template_path).unwrap();
    WebSite::new(serde_json::from_str(&site_template).unwrap())
}
peterrabbit's avatar
peterrabbit committed

#[get("/{pth:.*}")]
peterrabbit's avatar
peterrabbit committed
async fn page(
    website: actix_web::web::Data<WebSite>,
    pth: actix_web::web::Path<String>,
) -> impl Responder {
    let pth = format!("/{}/", pth.into_inner()); // BUG Use trailing slash middleware ? or match root or not root
    match website.get_page_by_url(&pth) {
        Some(page) => HttpResponse::Ok().body(page.html_doc.clone()),
        None => HttpResponse::NotFound().body(format!("Not found {}", pth)),
    }
}

#[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 website = load_website_template();
    // GET HOST AND CERTS DIR FROM CLI ARGUMENT
    // Get port from arg, or get context from arg and define default port, or set default port to standard

peterrabbit's avatar
peterrabbit committed
    // LOAD A WEBSITE SCHEMA (JSON) FROM CLI ARGUMENT PATH OR search in /var/{sitename} and load it CREATE A NEW ONE
    // create pages resources with templates and the contents from the json file
    // Save the resources in an appstate

peterrabbit's avatar
peterrabbit committed
    // create the static dir in standard location if doesn't exist (like /var/{sitename}/static)
    // create the static files index (like Arc<Mutex<StaticFilesIndex>>)

    // create a Rest service at root with extensive path argument: like #[get(/{pth:.*})]
    // Then parse the website document and return the corresponding template, or 404 template

    let host = "localhost";
    let certs_dir = std::path::PathBuf::from("/etc/letsencrypt/live").join(host);
    let cert_file =
        &mut std::io::BufReader::new(std::fs::File::open(certs_dir.join("fullchain.pem")).unwrap());
    let key_file =
        &mut std::io::BufReader::new(std::fs::File::open(certs_dir.join("privkey.pem")).unwrap());

    let cert = rustls::Certificate(rustls_pemfile::certs(cert_file).unwrap().remove(0).to_vec());
    let key = rustls::PrivateKey(
        rustls_pemfile::pkcs8_private_keys(key_file)
            .unwrap()
            .remove(0)
            .to_vec(),
    );

    let srv_conf = rustls::ServerConfig::builder()
        .with_safe_defaults()
        .with_no_client_auth()
        .with_single_cert(vec![cert], key)
        .expect("bad certificate/key");

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())
            .app_data(actix_web::web::Data::new(website.clone()))
            .service(admin_dashboard)
            .service(admin_login)
            .service(page)
peterrabbit's avatar
peterrabbit committed
    })
    .bind("127.0.0.1:8080")?
    .bind_rustls("127.0.0.1:8443", srv_conf)?
peterrabbit's avatar
peterrabbit committed
    .run()
    .await