-
Notifications
You must be signed in to change notification settings - Fork 185
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(volo-http): add https examples
Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
- Loading branch information
1 parent
f07e28e
commit 928c08d
Showing
4 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use volo::net::tls::TlsConnector; | ||
use volo_http::{body::BodyConversion, client::Client}; | ||
|
||
#[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 connector = TlsConnector::builder() | ||
.enable_default_root_certs(false) | ||
.add_pem_from_file(data_dir.join("tls/ca.pem")) | ||
.expect("failed to read ca.pem") | ||
.build() | ||
.expect("failed to build TlsConnector"); | ||
|
||
let client = Client::builder().set_tls_config(connector).build(); | ||
|
||
let resp = client | ||
.get("https://[::1]:8080/") | ||
.expect("invalid uri") | ||
.send() | ||
.await | ||
.expect("request failed") | ||
.into_string() | ||
.await | ||
.expect("response failed to convert to string"); | ||
|
||
println!("{resp}"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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(); | ||
} |