Skip to content

Commit 82fa2b6

Browse files
winstxnhdwlpil
authored andcommitted
refactor: build default client if CACERTS_PATH is not populated
1 parent 4a7c51a commit 82fa2b6

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
making Gleam packages discoverable through global search of HexDocs.
1414
([Diemo Gebhardt](https://github.com/diemogebhardt))
1515

16+
- Allow users to set the `GLEAM_CACERTS_PATH` environment variable to specify a
17+
path to a directory containing CA certificates to install Hex packages.
18+
([winstxnhdw](https://github.com/winstxnhdw))
19+
1620
### Language server
1721

1822
- The language server now offers a code action to convert the first step of a

compiler-cli/src/http.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ use gleam_core::{
1010
use http::{Request, Response};
1111
use reqwest::{Certificate, Client};
1212

13+
use crate::fs;
14+
1315
static REQWEST_CLIENT: OnceLock<Client> = OnceLock::new();
1416

1517
#[derive(Debug)]
@@ -50,20 +52,18 @@ fn init_client() -> Result<&'static Client, Error> {
5052
return Ok(client);
5153
}
5254

53-
let certificate_path = std::env::var("GLEAM_CACERTS_PATH").map_err(|error| Error::FileIo {
54-
kind: FileKind::Directory,
55-
action: FileIoAction::Read,
56-
path: Utf8PathBuf::new(),
57-
err: Some(error.to_string()),
58-
})?;
59-
60-
let certificate_bytes = std::fs::read(&certificate_path).map_err(|error| Error::FileIo {
61-
kind: FileKind::File,
62-
action: FileIoAction::Parse,
63-
path: Utf8PathBuf::from(&certificate_path),
64-
err: Some(error.to_string()),
65-
})?;
55+
let certificate_path = match std::env::var("GLEAM_CACERTS_PATH") {
56+
Ok(path) => path,
57+
Err(_) => {
58+
return Ok(REQWEST_CLIENT.get_or_init(|| {
59+
Client::builder()
60+
.build()
61+
.expect("Failed to create reqwest client")
62+
}));
63+
}
64+
};
6665

66+
let certificate_bytes = fs::read_bytes(&certificate_path)?;
6767
let certificate = Certificate::from_pem(&certificate_bytes).map_err(|error| Error::FileIo {
6868
kind: FileKind::File,
6969
action: FileIoAction::Parse,

0 commit comments

Comments
 (0)