1
0
mirror of https://github.com/actix/actix-website synced 2025-02-11 15:52:52 +01:00
2024-06-05 05:20:08 +01:00

42 lines
1.3 KiB
Rust

use std::{fs::File, io::BufReader};
// <main>
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
async fn index(_req: HttpRequest) -> impl Responder {
"Hello TLS World!"
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();
let mut certs_file = BufReader::new(File::open("cert.pem").unwrap());
let mut key_file = BufReader::new(File::open("key.pem").unwrap());
// load TLS certs and key
// to create a self-signed temporary cert for testing:
// `openssl req -x509 -newkey rsa:4096 -nodes -keyout key.pem -out cert.pem -days 365 -subj '/CN=localhost'`
let tls_certs = rustls_pemfile::certs(&mut certs_file)
.collect::<Result<Vec<_>, _>>()
.unwrap();
let tls_key = rustls_pemfile::pkcs8_private_keys(&mut key_file)
.next()
.unwrap()
.unwrap();
// set up TLS config options
let tls_config = rustls::ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(tls_certs, rustls::pki_types::PrivateKeyDer::Pkcs8(tls_key))
.unwrap();
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind_rustls_0_23(("127.0.0.1", 8443), tls_config)?
.run()
.await
}
// </main>