forked from actix/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
62 lines (52 loc) · 1.72 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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()
}