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

42 lines
1.3 KiB
Rust
Raw Normal View History

2024-03-02 17:30:54 +00:00
use std::{fs::File, io::BufReader};
2019-06-17 18:31:10 -04:00
// <main>
use actix_web::{web, App, HttpRequest, HttpServer, Responder};
2019-12-29 01:37:19 +09:00
async fn index(_req: HttpRequest) -> impl Responder {
2024-03-02 17:30:54 +00:00
"Hello TLS World!"
2019-06-17 18:31:10 -04:00
}
2020-09-12 16:21:54 +01:00
#[actix_web::main]
2019-12-29 01:37:19 +09:00
async fn main() -> std::io::Result<()> {
rustls::crypto::aws_lc_rs::default_provider()
.install_default()
.unwrap();
2024-03-02 17:30:54 +00:00
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
2019-06-18 17:17:43 -04:00
// 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'`
2024-03-02 17:30:54 +00:00
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))
2019-06-17 18:31:10 -04:00
.unwrap();
HttpServer::new(|| App::new().route("/", web::get().to(index)))
.bind_rustls_0_23(("127.0.0.1", 8443), tls_config)?
2019-06-18 17:17:43 -04:00
.run()
2019-12-29 01:37:19 +09:00
.await
2019-06-17 18:31:10 -04:00
}
// </main>