From 9be4b31cc8791111b9d1c34905c112ded648bd39 Mon Sep 17 00:00:00 2001 From: Tarkin25 Date: Tue, 8 Feb 2022 23:00:55 +0100 Subject: [PATCH] #36 use the "PUBSUB_EMULATOR_HOST" environment variable if present --- google-cloud/src/pubsub/client.rs | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/google-cloud/src/pubsub/client.rs b/google-cloud/src/pubsub/client.rs index b28bfaa6..8f714c9b 100644 --- a/google-cloud/src/pubsub/client.rs +++ b/google-cloud/src/pubsub/client.rs @@ -56,14 +56,24 @@ impl Client { project_name: impl Into, creds: ApplicationCredentials, ) -> Result { - let tls_config = ClientTlsConfig::new() - .ca_certificate(Certificate::from_pem(TLS_CERTS)) - .domain_name(Client::DOMAIN_NAME); - - let channel = Channel::from_static(Client::ENDPOINT) - .tls_config(tls_config)? - .connect() - .await?; + let endpoint_str = env::var("PUBSUB_EMULATOR_HOST") + .map(|s| format!("http://{}", s)) + .map(|s| &*Box::leak(s.into_boxed_str())) + .unwrap_or(Client::ENDPOINT); + + let endpoint = Channel::from_static(endpoint_str); + + let endpoint = if endpoint_str.starts_with("https") { + let tls_config = ClientTlsConfig::new() + .ca_certificate(Certificate::from_pem(TLS_CERTS)) + .domain_name(Client::DOMAIN_NAME); + + endpoint.tls_config(tls_config)? + } else { + endpoint + }; + + let channel = endpoint.connect().await?; Ok(Client { project_name: project_name.into(),