Skip to content

Commit

Permalink
chore(volo-http): add https examples
Browse files Browse the repository at this point in the history
Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
  • Loading branch information
yukiiiteru committed Mar 11, 2024
1 parent f07e28e commit 928c08d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
15 changes: 14 additions & 1 deletion examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ path = "src/http/example-http-server.rs"
name = "example-http-client"
path = "src/http/example-http-client.rs"

[[bin]]
name = "http-tls-server"
path = "src/http/http-tls-server.rs"
required-features = ["__tls"]

[[bin]]
name = "http-tls-client"
path = "src/http/http-tls-client.rs"
required-features = ["__tls"]

[dependencies]
anyhow.workspace = true
async-stream.workspace = true
Expand All @@ -108,7 +118,7 @@ pilota.workspace = true
volo = { path = "../volo" }
volo-grpc = { path = "../volo-grpc" }
volo-thrift = { path = "../volo-thrift", features = ["multiplex"] }
volo-http = { path = "../volo-http", features = ["full"] }
volo-http = { path = "../volo-http", features = ["default_client", "default_server", "cookie"] }

volo-gen = { path = "./volo-gen" }

Expand All @@ -118,14 +128,17 @@ rustls = [
"__tls",
"volo/rustls",
"volo-grpc/rustls",
"volo-http/rustls",
]
native-tls = [
"__tls",
"volo/native-tls",
"volo-grpc/native-tls",
"volo-http/native-tls",
]
native-tls-vendored = [
"__tls",
"volo/native-tls-vendored",
"volo-grpc/native-tls-vendored",
"volo-http/native-tls-vendored",
]
13 changes: 12 additions & 1 deletion examples/src/http/example-http-client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ async fn main() -> Result<(), BoxError> {
tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed");

// simple `get` function and dns resolve
println!("{}", get("http://www.126.com/").await?.into_string().await?);
println!(
"{}",
get("http://httpbin.org/get").await?.into_string().await?
);

// HTTPS `get`
//
// If tls is not enabled, the `httpbin.org` will response 400 Bad Request.
println!(
"{}",
get("https://httpbin.org/get").await?.into_string().await?
);

// create client by builder
let client = ClientBuilder::new()
Expand Down
32 changes: 32 additions & 0 deletions examples/src/http/http-tls-client.rs
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}");
}
50 changes: 50 additions & 0 deletions examples/src/http/http-tls-server.rs
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();
}

0 comments on commit 928c08d

Please sign in to comment.