-
Notifications
You must be signed in to change notification settings - Fork 166
/
http-tls-server.rs
50 lines (41 loc) · 1.26 KB
/
http-tls-server.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
//! Test it with:
//!
//! ```bash
//! curl -v --cacert examples/data/tls/ca.pem https://127.0.0.1:8080/
//! ```
//!
//! Or use the tls client directly.
use std::{net::SocketAddr, time::Duration};
use volo::net::tls::ServerTlsConfig;
use volo_http::server::{
layer::TimeoutLayer,
route::{get, Router},
Server,
};
async fn index() -> &'static str {
"It Works!\n"
}
#[volo::main]
async fn main() {
let subscriber = tracing_subscriber::FmtSubscriber::builder()
.with_max_level(tracing::Level::TRACE)
.finish();
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");
let data_dir = std::path::PathBuf::from_iter([std::env!("CARGO_MANIFEST_DIR"), "data"]);
let tls_config = ServerTlsConfig::from_pem_file(
data_dir.join("tls/server.pem"),
data_dir.join("tls/server.key"),
)
.expect("failed to load certs");
let app = Router::new()
.route("/", get(index))
.layer(TimeoutLayer::new(Duration::from_secs(5)));
let addr: SocketAddr = "[::]:8080".parse().unwrap();
let addr = volo::net::Address::from(addr);
println!("Listening on {addr}");
Server::new(app)
.tls_config(tls_config)
.run(addr)
.await
.unwrap();
}