Skip to content

Commit c7e3179

Browse files
authored
Separate HTTPS and HTTP adapter (#196)
This prevents issues when ca certs are not available in custom docker images.
1 parent 5e8a3f6 commit c7e3179

File tree

2 files changed

+70
-25
lines changed

2 files changed

+70
-25
lines changed

src/lib.rs

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use http::{
2222
use hyper::{
2323
body,
2424
body::HttpBody,
25-
client::{Client, HttpConnector},
25+
client::{connect::Connect, Client, HttpConnector},
2626
Body,
2727
};
2828
use hyper_rustls::HttpsConnector;
@@ -99,8 +99,8 @@ impl AdapterOptions {
9999
}
100100

101101
#[derive(Clone)]
102-
pub struct Adapter {
103-
client: Arc<Client<HttpsConnector<HttpConnector>>>,
102+
pub struct Adapter<C> {
103+
client: Arc<Client<C>>,
104104
healthcheck_url: Url,
105105
healthcheck_protocol: Protocol,
106106
async_init: bool,
@@ -110,11 +110,11 @@ pub struct Adapter {
110110
compression: bool,
111111
}
112112

113-
impl Adapter {
114-
/// Create a new Adapter instance.
115-
/// This function initializes a new HTTP client
113+
impl Adapter<HttpsConnector<HttpConnector>> {
114+
/// Create a new HTTPS Adapter instance.
115+
/// This function initializes a new HTTPS client
116116
/// to talk with the web server.
117-
pub fn new(options: &AdapterOptions) -> Adapter {
117+
pub fn new_https(options: &AdapterOptions) -> Adapter<HttpsConnector<HttpConnector>> {
118118
if let Some(cert_file) = &options.tls_cert_file {
119119
env::set_var("SSL_CERT_FILE", cert_file);
120120
}
@@ -133,10 +133,7 @@ impl Adapter {
133133

134134
let client = Client::builder().pool_idle_timeout(Duration::from_secs(4)).build(https);
135135

136-
let schema = match options.enable_tls {
137-
true => "https",
138-
false => "http",
139-
};
136+
let schema = "https";
140137

141138
let healthcheck_url = format!(
142139
"{}://{}:{}{}",
@@ -160,15 +157,47 @@ impl Adapter {
160157
compression: options.compression,
161158
}
162159
}
160+
}
161+
162+
impl Adapter<HttpConnector> {
163+
/// Create a new HTTP Adapter instance.
164+
/// This function initializes a new HTTP client
165+
/// to talk with the web server.
166+
pub fn new(options: &AdapterOptions) -> Adapter<HttpConnector> {
167+
let client = Client::builder()
168+
.pool_idle_timeout(Duration::from_secs(4))
169+
.build(HttpConnector::new());
170+
171+
let schema = "http";
172+
173+
let healthcheck_url = format!(
174+
"{}://{}:{}{}",
175+
schema, options.host, options.readiness_check_port, options.readiness_check_path
176+
)
177+
.parse()
178+
.unwrap();
179+
180+
let domain = format!("{}://{}:{}", schema, options.host, options.port)
181+
.parse()
182+
.unwrap();
163183

164-
/// Switch the default HTTP client with a different one.
165-
pub fn with_client(self, client: Client<HttpsConnector<HttpConnector>>) -> Self {
166184
Adapter {
167185
client: Arc::new(client),
168-
..self
186+
healthcheck_url,
187+
healthcheck_protocol: options.readiness_check_protocol,
188+
domain,
189+
base_path: options.base_path.clone(),
190+
async_init: options.async_init,
191+
ready_at_init: Arc::new(AtomicBool::new(false)),
192+
compression: options.compression,
169193
}
170194
}
195+
}
171196

197+
impl<C> Adapter<C>
198+
where
199+
C: Connect + Clone + Send + Sync + 'static,
200+
{
172201
/// Register a Lambda Extension to ensure
173202
/// that the adapter is loaded before any Lambda function
174203
/// associated with it.
@@ -350,7 +379,10 @@ impl Adapter {
350379

351380
/// Implement a `Tower.Service` that sends the requests
352381
/// to the web server.
353-
impl Service<Request> for Adapter {
382+
impl<C> Service<Request> for Adapter<C>
383+
where
384+
C: Connect + Clone + Send + Sync + 'static,
385+
{
354386
type Response = Response<Body>;
355387
type Error = Error;
356388
type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send>>;

src/main.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,28 @@ async fn main() -> Result<(), Error> {
1515
// get configuration options from environment variables
1616
let options = AdapterOptions::from_env();
1717

18-
// create an adapter
19-
let mut adapter = Adapter::new(&options);
18+
match options.enable_tls {
19+
false => {
20+
// create a HTTP adapter
21+
let mut adapter = Adapter::new(&options);
22+
// register the adapter as an extension
23+
adapter.register_default_extension();
24+
// check if the web application is ready
25+
adapter.check_init_health().await;
26+
// start lambda runtime after the web application is ready
27+
adapter.run().await.expect("lambda runtime failed");
28+
}
29+
true => {
30+
// create a HTTPS adapter
31+
let mut adapter = Adapter::new_https(&options);
32+
// register the adapter as an extension
33+
adapter.register_default_extension();
34+
// check if the web application is ready
35+
adapter.check_init_health().await;
36+
// start lambda runtime after the web application is ready
37+
adapter.run().await.expect("lambda runtime failed");
38+
}
39+
}
2040

21-
// register the adapter as an extension
22-
adapter.register_default_extension();
23-
24-
// check if the web application is ready
25-
adapter.check_init_health().await;
26-
27-
// start lambda runtime after the web application is ready
28-
adapter.run().await
41+
Ok(())
2942
}

0 commit comments

Comments
 (0)