feat: support HTTPS_PROXY and ALL_PROXY (#68)

This commit is contained in:
sigoden 2023-03-12 08:31:06 +08:00 committed by GitHub
parent 0ed0ad1fd2
commit 8fa2e2683b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 1 deletions

View File

@ -126,7 +126,8 @@ impl ChatGptClient {
fn build_client(&self) -> Result<Client> {
let mut builder = Client::builder();
if let Some(proxy) = self.config.read().proxy.as_ref() {
builder = builder.proxy(Proxy::all(proxy).with_context(|| "Invalid config.proxy")?);
builder = builder
.proxy(Proxy::all(proxy).with_context(|| format!("Invalid proxy `{proxy}`"))?);
}
let client = builder
.connect_timeout(CONNECT_TIMEOUT)

View File

@ -106,6 +106,7 @@ impl Config {
bail!("api_key not set");
}
config.merge_env_vars();
config.maybe_proxy();
config.load_roles()?;
Ok(config)
@ -430,6 +431,15 @@ impl Config {
}
}
}
fn maybe_proxy(&mut self) {
if self.proxy.is_some() {
return;
}
if let Ok(value) = env::var("HTTPS_PROXY").or_else(|_| env::var("ALL_PROXY")) {
self.proxy = Some(value);
}
}
}
fn create_config_file(config_path: &Path) -> Result<()> {