From be44541a87ba9deeb6f4ef1650ab80dd96551b7b Mon Sep 17 00:00:00 2001 From: peterrabbit <peterrabbit@msi.home> Date: Wed, 17 Aug 2022 18:32:01 +0200 Subject: [PATCH] bind tls & basic route pattern --- Cargo.lock | 11 ++++++++++ Cargo.toml | 2 ++ src/main.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++------ 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c2cb4bb..24761b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -375,6 +375,8 @@ name = "cms_rust" version = "0.1.0" dependencies = [ "actix-web", + "rustls", + "rustls-pemfile", ] [[package]] @@ -978,6 +980,15 @@ dependencies = [ "webpki", ] +[[package]] +name = "rustls-pemfile" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0864aeff53f8c05aa08d86e5ef839d3dfcf07aeba2db32f12db0ef716e87bd55" +dependencies = [ + "base64", +] + [[package]] name = "ryu" version = "1.0.11" diff --git a/Cargo.toml b/Cargo.toml index b2438c5..eb87a3d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,5 @@ edition = "2021" [dependencies] actix-web = { version = "4.1.0", features = ["rustls", "secure-cookies"] } +rustls = "0.20.6" +rustls-pemfile = "1.0.1" diff --git a/src/main.rs b/src/main.rs index 0468b1b..70790a5 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,65 @@ -use actix_web::{get, web, App, HttpServer, Responder}; +use actix_web::{get, App, HttpResponse, HttpServer, Responder}; -#[get("/hello/{name}")] -async fn greet(name: web::Path<String>) -> impl Responder { - format!("Hello {}!", name) +#[get("/{pth:.*}")] +async fn page(pth: actix_web::web::Path<String>) -> impl Responder { + HttpResponse::Ok().body(format!("Page path {}", 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") } #[actix_web::main] async fn main() -> std::io::Result<()> { + // 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 + + // LOAD A WEBSITE SCHEMA (JSON) FROM CLI ARGUMENT PATH OR CREATE A NEW ONE + // create pages resources with templates and the contents from the json file + // Save the resources in an appstate + + // create the static dir + // create the static files index (like Arc<Mutex<Index>>) + + // 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"); + HttpServer::new(|| { - App::new().service(greet) + App::new() + .wrap(actix_web::middleware::Logger::default()) + .service(admin_dashboard) + .service(admin_login) + .service(page) }) - .bind(("127.0.0.1", 8080))? + .bind("127.0.0.1:8080")? + .bind_rustls("127.0.0.1:8443", srv_conf)? .run() .await -} \ No newline at end of file +} -- GitLab