From 4ef23c5da0d09b036b7ea84e81a1c6b22f69270a Mon Sep 17 00:00:00 2001 From: L-ING Date: Fri, 24 Nov 2023 00:29:00 +0800 Subject: [PATCH] fix: unable to load env var through docker compose - Use arguments from command line instead of environment variables. - Environment in docker compose seems different from docker run -e, which can't be loaded in images from scratch. --- Cargo.lock | 9 ++++++++- Cargo.toml | 3 ++- Dockerfile | 2 +- README.md | 2 +- src/requests.rs | 5 +++-- 5 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 326b4f4..73df09d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -859,7 +859,7 @@ dependencies = [ [[package]] name = "gpts-code-analyst" -version = "1.1.1" +version = "1.1.2" dependencies = [ "actix-rt", "actix-web", @@ -867,6 +867,7 @@ dependencies = [ "awc", "env_logger", "log", + "pico-args", "serde", "serde_json", ] @@ -1170,6 +1171,12 @@ version = "2.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b2a4787296e9989611394c33f193f676704af1686e70b8f8033ab5ba9a35a94" +[[package]] +name = "pico-args" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" + [[package]] name = "pin-project-lite" version = "0.2.13" diff --git a/Cargo.toml b/Cargo.toml index 58b7652..27cc13c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "gpts-code-analyst" -version = "1.1.1" +version = "1.1.2" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html @@ -18,5 +18,6 @@ async-std = "1.12.0" awc = { version = "3.2.0", features = ["rustls-0_21"] } env_logger = "0.10.1" log = "0.4.20" +pico-args = { version = "0.5.0", features = ["short-space-opt"] } serde = { version = "1.0.193", features = ["derive"] } serde_json = "1.0.108" diff --git a/Dockerfile b/Dockerfile index 0bbad17..a7ae54f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -7,4 +7,4 @@ RUN apk add --update --no-cache musl-dev &&\ FROM scratch COPY --from=builder /workdir/target/release/gpts-code-analyst / COPY --from=builder /workdir/static /static -CMD ["/gpts-code-analyst"] +ENTRYPOINT ["/gpts-code-analyst"] diff --git a/README.md b/README.md index 9cd2a17..e7c00dc 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ Github Code Analyst over GPTs ```sh PORT=8080 # 改成你的端口号 GITHUB_TOKEN=xxxxxxxxxxxxxx # 改成你的Github Token -docker run -d --name code-analyst -p $PORT:8080 -e GITHUB_TOKEN=$GITHUB_TOKEN hlf01/gpts-code-analyst +docker run -d --name code-analyst -p $PORT:8080 hlf01/gpts-code-analyst --token $GITHUB_TOKEN ``` ## Docker构建 diff --git a/src/requests.rs b/src/requests.rs index 1726095..8ea8bdd 100644 --- a/src/requests.rs +++ b/src/requests.rs @@ -1,5 +1,5 @@ use async_std::io::{Error, ErrorKind, Result}; -use std::env::var; +use pico_args::Arguments; pub async fn get(url: &str) -> Result { let client = awc::Client::builder() @@ -8,7 +8,8 @@ pub async fn get(url: &str) -> Result { let mut request = client.get(url).insert_header(("User-Agent", "Actix-web")); - if let Ok(github_token) = var("GITHUB_TOKEN") { + let mut args = Arguments::from_env(); + if let Ok(github_token) = args.value_from_str::<&str, String>("--token") { request = request.insert_header(("Authorization", format!("Bearer {}", github_token))); } else { return Err(Error::new(