mirror of
https://github.com/actix/examples
synced 2025-06-09 10:45:19 +02:00
63 lines
1.7 KiB
Rust
63 lines
1.7 KiB
Rust
use actix_files::Files;
|
|
use actix_web::{
|
|
App, HttpRequest, HttpResponse, HttpServer, http::header::ContentType, middleware, web,
|
|
};
|
|
use log::debug;
|
|
use rustls::{
|
|
ServerConfig,
|
|
pki_types::{CertificateDer, PrivateKeyDer, pem::PemObject},
|
|
};
|
|
|
|
/// simple handle
|
|
async fn index(req: HttpRequest) -> HttpResponse {
|
|
debug!("{req:?}");
|
|
|
|
HttpResponse::Ok().content_type(ContentType::html()).body(
|
|
"<!DOCTYPE html><html><body>\
|
|
<p>Welcome to your TLS-secured homepage!</p>\
|
|
</body></html>",
|
|
)
|
|
}
|
|
|
|
#[actix_web::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
env_logger::init_from_env(env_logger::Env::default().default_filter_or("info"));
|
|
|
|
let config = load_rustls_config();
|
|
|
|
log::info!("starting HTTPS server at https://localhost:8443");
|
|
|
|
HttpServer::new(|| {
|
|
App::new()
|
|
// enable logger
|
|
.wrap(middleware::Logger::default())
|
|
// register simple handler, handle all methods
|
|
.service(web::resource("/index.html").to(index))
|
|
.service(web::redirect("/", "/index.html"))
|
|
.service(Files::new("/static", "static"))
|
|
})
|
|
.bind_rustls_0_23("127.0.0.1:8443", config)?
|
|
.run()
|
|
.await
|
|
}
|
|
|
|
fn load_rustls_config() -> rustls::ServerConfig {
|
|
rustls::crypto::aws_lc_rs::default_provider()
|
|
.install_default()
|
|
.unwrap();
|
|
|
|
// load TLS key/cert files
|
|
let cert_chain = CertificateDer::pem_file_iter("cert.pem")
|
|
.unwrap()
|
|
.flatten()
|
|
.collect();
|
|
|
|
let key_der =
|
|
PrivateKeyDer::from_pem_file("key.pem").expect("Could not locate PKCS 8 private keys.");
|
|
|
|
ServerConfig::builder()
|
|
.with_no_client_auth()
|
|
.with_single_cert(cert_chain, key_der)
|
|
.unwrap()
|
|
}
|