From 54fd75cc14cd4a3a878e1b08efc8f176d2f6b3fe Mon Sep 17 00:00:00 2001 From: Andrew McGivery Date: Thu, 3 Oct 2024 17:26:35 -0400 Subject: [PATCH 1/3] Add dotenv to allow router to read environment variables from .env file --- Cargo.lock | 7 +++++++ apollo-router/Cargo.toml | 1 + apollo-router/src/executable.rs | 3 +++ 3 files changed, 11 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 7128f24194..96e0aaf187 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -266,6 +266,7 @@ dependencies = [ "dhat", "diff", "displaydoc", + "dotenv", "ecdsa", "flate2", "fred", @@ -2249,6 +2250,12 @@ dependencies = [ "syn 2.0.76", ] +[[package]] +name = "dotenv" +version = "0.15.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" + [[package]] name = "downcast" version = "0.11.0" diff --git a/apollo-router/Cargo.toml b/apollo-router/Cargo.toml index ff09306e2a..b95036fece 100644 --- a/apollo-router/Cargo.toml +++ b/apollo-router/Cargo.toml @@ -272,6 +272,7 @@ bytesize = { version = "1.3.0", features = ["serde"] } ahash = "0.8.11" itoa = "1.0.9" ryu = "1.0.15" +dotenv = "0.15.0" [target.'cfg(macos)'.dependencies] uname = "0.1.1" diff --git a/apollo-router/src/executable.rs b/apollo-router/src/executable.rs index 86bdee162f..9216e5a424 100644 --- a/apollo-router/src/executable.rs +++ b/apollo-router/src/executable.rs @@ -19,6 +19,7 @@ use clap::Args; use clap::CommandFactory; use clap::Parser; use clap::Subcommand; +use dotenv::dotenv; #[cfg(any(feature = "dhat-heap", feature = "dhat-ad-hoc"))] use once_cell::sync::OnceCell; use regex::Captures; @@ -398,6 +399,8 @@ impl Executable { config: Option, cli_args: Option, ) -> Result<()> { + dotenv().ok(); + let opt = cli_args.unwrap_or_else(Opt::parse); if opt.version { From b112f377b29bf99bfa1f38cc75c1947f41442ae5 Mon Sep 17 00:00:00 2001 From: Andrew McGivery Date: Thu, 3 Oct 2024 17:35:10 -0400 Subject: [PATCH 2/3] add changeset file --- .changesets/feat_feature_dotenv.md | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 .changesets/feat_feature_dotenv.md diff --git a/.changesets/feat_feature_dotenv.md b/.changesets/feat_feature_dotenv.md new file mode 100644 index 0000000000..03764c34eb --- /dev/null +++ b/.changesets/feat_feature_dotenv.md @@ -0,0 +1,13 @@ +### Add dotenv to allow router to read environment variables from .env file ([PR #6117](https://github.com/apollographql/router/pull/6117)) + +Router will now read environment variables from `.env` file. This is helpful for local development to inject in `APOLLO_KEY` and `APOLLO_GRAPH_REF` from a `.env` file. It also makes these environment variables available everywhere they are currently available such as in rhai scripts: + +``` +# .env +MY_COOL_VARIABLE="yeaaaaa man!" + +# main.rhai +log_info(`MY_COOL_VARIABLE: ${env::get("MY_COOL_VARIABLE")}`); +``` + +By [@andrewmcgivery](https://github.com/andrewmcgivery) in https://github.com/apollographql/router/pull/6117 From 59749ab3d8babb401158c7dc11229b017a560fc9 Mon Sep 17 00:00:00 2001 From: Andrew McGivery Date: Fri, 4 Oct 2024 11:23:55 -0400 Subject: [PATCH 3/3] Switch to dotenvy and load values before threads spin up --- Cargo.lock | 8 ++++---- apollo-router/Cargo.toml | 2 +- apollo-router/src/executable.rs | 5 ++--- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 96e0aaf187..1e9c65be86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -266,7 +266,7 @@ dependencies = [ "dhat", "diff", "displaydoc", - "dotenv", + "dotenvy", "ecdsa", "flate2", "fred", @@ -2251,10 +2251,10 @@ dependencies = [ ] [[package]] -name = "dotenv" -version = "0.15.0" +name = "dotenvy" +version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "77c90badedccf4105eca100756a0b1289e191f6fcbdadd3cee1d2f614f97da8f" +checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" [[package]] name = "downcast" diff --git a/apollo-router/Cargo.toml b/apollo-router/Cargo.toml index b95036fece..f92f6b002a 100644 --- a/apollo-router/Cargo.toml +++ b/apollo-router/Cargo.toml @@ -272,7 +272,7 @@ bytesize = { version = "1.3.0", features = ["serde"] } ahash = "0.8.11" itoa = "1.0.9" ryu = "1.0.15" -dotenv = "0.15.0" +dotenvy = "0.15.7" [target.'cfg(macos)'.dependencies] uname = "0.1.1" diff --git a/apollo-router/src/executable.rs b/apollo-router/src/executable.rs index 9216e5a424..182388fd2f 100644 --- a/apollo-router/src/executable.rs +++ b/apollo-router/src/executable.rs @@ -19,7 +19,6 @@ use clap::Args; use clap::CommandFactory; use clap::Parser; use clap::Subcommand; -use dotenv::dotenv; #[cfg(any(feature = "dhat-heap", feature = "dhat-ad-hoc"))] use once_cell::sync::OnceCell; use regex::Captures; @@ -332,6 +331,8 @@ pub fn main() -> Result<()> { #[cfg(feature = "dhat-ad-hoc")] create_ad_hoc_profiler(); + dotenvy::dotenv()?; + let mut builder = tokio::runtime::Builder::new_multi_thread(); builder.enable_all(); if let Some(nb) = std::env::var("APOLLO_ROUTER_NUM_CORES") @@ -399,8 +400,6 @@ impl Executable { config: Option, cli_args: Option, ) -> Result<()> { - dotenv().ok(); - let opt = cli_args.unwrap_or_else(Opt::parse); if opt.version {