Skip to content

Commit

Permalink
refactor: build default client if CACERTS_PATH is not populated
Browse files Browse the repository at this point in the history
  • Loading branch information
winstxnhdw authored and lpil committed Feb 11, 2025
1 parent 4a7c51a commit 82fa2b6
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
making Gleam packages discoverable through global search of HexDocs.
([Diemo Gebhardt](https://github.com/diemogebhardt))

- Allow users to set the `GLEAM_CACERTS_PATH` environment variable to specify a
path to a directory containing CA certificates to install Hex packages.
([winstxnhdw](https://github.com/winstxnhdw))

### Language server

- The language server now offers a code action to convert the first step of a
Expand Down
26 changes: 13 additions & 13 deletions compiler-cli/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use gleam_core::{
use http::{Request, Response};
use reqwest::{Certificate, Client};

use crate::fs;

static REQWEST_CLIENT: OnceLock<Client> = OnceLock::new();

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

let certificate_path = std::env::var("GLEAM_CACERTS_PATH").map_err(|error| Error::FileIo {
kind: FileKind::Directory,
action: FileIoAction::Read,
path: Utf8PathBuf::new(),
err: Some(error.to_string()),
})?;

let certificate_bytes = std::fs::read(&certificate_path).map_err(|error| Error::FileIo {
kind: FileKind::File,
action: FileIoAction::Parse,
path: Utf8PathBuf::from(&certificate_path),
err: Some(error.to_string()),
})?;
let certificate_path = match std::env::var("GLEAM_CACERTS_PATH") {
Ok(path) => path,
Err(_) => {
return Ok(REQWEST_CLIENT.get_or_init(|| {
Client::builder()
.build()
.expect("Failed to create reqwest client")
}));
}
};

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

0 comments on commit 82fa2b6

Please sign in to comment.