Skip to content

添加环境变量配置支持#36

Open
TheOrdinaryWow wants to merge 1 commit intoarloor:masterfrom
TheOrdinaryWow:master
Open

添加环境变量配置支持#36
TheOrdinaryWow wants to merge 1 commit intoarloor:masterfrom
TheOrdinaryWow:master

Conversation

@TheOrdinaryWow
Copy link

通过clap的envfeature和dotenvy支持通过环境变量设置配置

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

该 PR 为 rust_http_proxy 的配置系统新增了环境变量支持:通过 clapenv feature 读取环境变量,并在 CLI 入口中用 dotenvy 加载 .env 文件,方便在容器/部署环境中注入配置。

Changes:

  • 在 CLI 启动时加载 .env,并通过 Param::parse() 同时解析命令行参数与环境变量
  • Param 各配置项增加 #[arg(env = "...")] 映射
  • 更新依赖:启用 clapenv feature,并引入 dotenvy

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 5 comments.

File Description
rust_http_proxy/src/main.rs 启动时加载 .env 后再解析 Param
rust_http_proxy/src/config.rs 为各参数添加 env 支持以读取环境变量
rust_http_proxy/Cargo.toml 启用 clap env feature,并新增 dotenvy 依赖
Cargo.lock 锁文件随依赖变更更新

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 21 to 27
#[arg(
short,
long,
env = "PORT",
value_name = "PORT",
default_value = "3128",
help = "可以多次指定来实现多端口\n"
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using very generic env var names like PORT (and others like USER, KEY, CERT) risks accidental overrides in common environments (e.g., many platforms set PORT/USER by default), changing runtime behavior unexpectedly. Prefer a namespaced prefix (e.g., RUST_HTTP_PROXY_PORT) via #[command(env_prefix = "RUST_HTTP_PROXY_")] and/or renaming the env vars.

Copilot uses AI. Check for mistakes.
Comment on lines 21 to 29
#[arg(
short,
long,
env = "PORT",
value_name = "PORT",
default_value = "3128",
help = "可以多次指定来实现多端口\n"
)]
port: Vec<u16>,
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These args are Vec<...> and the help text says they can be specified multiple times, but env = ... will typically provide only a single string. Without a value_delimiter (or similar) env-based multi-values like PORT=3128,3129 will fail to parse; consider adding a delimiter (and documenting it) for all Vec fields that support env.

Copilot uses AI. Check for mistakes.
async-compression = { version = "0.4", features = ["tokio", "gzip"] }
clap = { version = "4.4", features = ["derive"] }
clap = { version = "4.4", features = ["derive", "env"] }
dotenvy = { version = "0", features = ["cli"] }
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotenvy = { version = "0", features = ["cli"] } is overly broad and the cli feature pulls in an additional (older) clap dependency (visible in Cargo.lock as clap 3.x). Since the code only uses dotenvy::dotenv(), drop the cli feature and pin an explicit compatible dotenvy version to avoid duplicate clap versions and extra transitive deps.

Suggested change
dotenvy = { version = "0", features = ["cli"] }
dotenvy = "0.15"

Copilot uses AI. Check for mistakes.
let param = Param::parse();
// 解析命令行参数和环境变量
let param = {
dotenvy::dotenv().ok();
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotenvy::dotenv().ok() swallows all errors, including malformed .env files. Consider ignoring only the “not found” case but logging/returning an error for parse/IO errors so configuration mistakes don’t get silently skipped.

Suggested change
dotenvy::dotenv().ok();
match dotenvy::dotenv() {
Ok(_) => {}
Err(err) => {
if !matches!(
err,
dotenvy::Error::Io(ref io_err)
if io_err.kind() == std::io::ErrorKind::NotFound
) {
return Err(format!("failed to load .env file: {err}").into());
}
}
}

Copilot uses AI. Check for mistakes.
Comment on lines +26 to +30
// 解析命令行参数和环境变量
let param = {
dotenvy::dotenv().ok();
Param::parse()
};
Copy link

Copilot AI Feb 21, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only the rust_http_proxy binary loads .env before Param::parse(). The Windows service binary (src/winservice.rs) also parses Param but won’t read .env, which makes env-based configuration inconsistent across entry points; consider centralizing dotenv loading in a shared helper used by both binaries.

Copilot uses AI. Check for mistakes.
@arloor
Copy link
Owner

arloor commented Feb 21, 2026

@TheOrdinaryWow 看起来copilot给出的评论挺有意义的,辛苦看看修改下吧

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants