Skip to content

Commit afadfb7

Browse files
committed
Convert more environment variables to dotenv::var
At most of these call sites in library code, `dotenv` should already be initialized. Most of these changes are for style consistency. Some binaries will initialize sooner and will pick some variables that would not previously work from `.env`.
1 parent e725353 commit afadfb7

File tree

9 files changed

+32
-39
lines changed

9 files changed

+32
-39
lines changed

src/app.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Application-wide components in a struct accessible from each request
22
33
use crate::{db, util::CargoResult, Config, Env};
4-
use std::{env, path::PathBuf, sync::Arc, time::Duration};
4+
use std::{path::PathBuf, sync::Arc, time::Duration};
55

66
use diesel::r2d2;
77
use scheduled_thread_pool::ScheduledThreadPool;
@@ -45,25 +45,25 @@ impl App {
4545
);
4646
github.scopes.push(String::from("read:org"));
4747

48-
let db_pool_size = match (env::var("DB_POOL_SIZE"), config.env) {
48+
let db_pool_size = match (dotenv::var("DB_POOL_SIZE"), config.env) {
4949
(Ok(num), _) => num.parse().expect("couldn't parse DB_POOL_SIZE"),
5050
(_, Env::Production) => 10,
5151
_ => 1,
5252
};
5353

54-
let db_min_idle = match (env::var("DB_MIN_IDLE"), config.env) {
54+
let db_min_idle = match (dotenv::var("DB_MIN_IDLE"), config.env) {
5555
(Ok(num), _) => Some(num.parse().expect("couldn't parse DB_MIN_IDLE")),
5656
(_, Env::Production) => Some(5),
5757
_ => None,
5858
};
5959

60-
let db_helper_threads = match (env::var("DB_HELPER_THREADS"), config.env) {
60+
let db_helper_threads = match (dotenv::var("DB_HELPER_THREADS"), config.env) {
6161
(Ok(num), _) => num.parse().expect("couldn't parse DB_HELPER_THREADS"),
6262
(_, Env::Production) => 3,
6363
_ => 1,
6464
};
6565

66-
let db_connection_timeout = match (env::var("DB_TIMEOUT"), config.env) {
66+
let db_connection_timeout = match (dotenv::var("DB_TIMEOUT"), config.env) {
6767
(Ok(num), _) => num.parse().expect("couldn't parse DB_TIMEOUT"),
6868
(_, Env::Production) => 10,
6969
(_, Env::Test) => 1,

src/bin/background-worker.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
use cargo_registry::git::Repository;
1313
use cargo_registry::{background, background_jobs::*, db};
1414
use diesel::r2d2;
15-
use std::env;
1615
use std::thread::sleep;
1716
use std::time::Duration;
1817

@@ -25,8 +24,8 @@ fn main() {
2524
let db_config = r2d2::Pool::builder().max_size(2);
2625
let db_pool = db::diesel_pool(&config.db_url, config.env, db_config);
2726

28-
let username = env::var("GIT_HTTP_USER");
29-
let password = env::var("GIT_HTTP_PWD");
27+
let username = dotenv::var("GIT_HTTP_USER");
28+
let password = dotenv::var("GIT_HTTP_PWD");
3029
let credentials = match (username, password) {
3130
(Ok(u), Ok(p)) => Some((u, p)),
3231
_ => None,

src/bin/monitor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ mod on_call;
1313

1414
use cargo_registry::{db, util::CargoResult};
1515
use diesel::prelude::*;
16-
use std::env;
1716

1817
fn main() -> CargoResult<()> {
1918
let conn = db::connect_now()?;
@@ -30,7 +29,7 @@ fn check_stalled_background_jobs(conn: &PgConnection) -> CargoResult<()> {
3029

3130
println!("Checking for stalled background jobs");
3231

33-
let max_job_time = env::var("MAX_JOB_TIME")
32+
let max_job_time = dotenv::var("MAX_JOB_TIME")
3433
.map(|s| s.parse::<i32>().unwrap())
3534
.unwrap_or(15);
3635

src/bin/on_call/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use cargo_registry::util::{internal, CargoResult};
2-
use std::env;
32

43
use reqwest::{header, StatusCode as Status};
54

@@ -27,8 +26,8 @@ impl Event {
2726
/// If the variant is `Trigger`, this will page whoever is on call
2827
/// (potentially waking them up at 3 AM).
2928
pub fn send(self) -> CargoResult<()> {
30-
let api_token = env::var("PAGERDUTY_API_TOKEN")?;
31-
let service_key = env::var("PAGERDUTY_INTEGRATION_KEY")?;
29+
let api_token = dotenv::var("PAGERDUTY_API_TOKEN")?;
30+
let service_key = dotenv::var("PAGERDUTY_INTEGRATION_KEY")?;
3231

3332
let mut response = reqwest::Client::new()
3433
.post("https://events.pagerduty.com/generic/2010-04-15/create_event.json")

src/bin/server.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
use cargo_registry::{boot, App, Env};
44
use jemalloc_ctl;
55
use std::{
6-
env,
76
fs::File,
87
sync::{mpsc::channel, Arc},
98
};
@@ -33,16 +32,16 @@ fn main() {
3332
let categories_toml = include_str!("../boot/categories.toml");
3433
boot::categories::sync(categories_toml).unwrap();
3534

36-
let heroku = env::var("HEROKU").is_ok();
35+
let heroku = dotenv::var("HEROKU").is_ok();
3736
let port = if heroku {
3837
8888
3938
} else {
40-
env::var("PORT")
39+
dotenv::var("PORT")
4140
.ok()
4241
.and_then(|s| s.parse().ok())
4342
.unwrap_or(8888)
4443
};
45-
let threads = env::var("SERVER_THREADS")
44+
let threads = dotenv::var("SERVER_THREADS")
4645
.map(|s| s.parse().expect("SERVER_THREADS was not a valid number"))
4746
.unwrap_or_else(|_| {
4847
if config.env == Env::Development {
@@ -52,7 +51,7 @@ fn main() {
5251
}
5352
});
5453

55-
let server = if env::var("USE_HYPER").is_ok() {
54+
let server = if dotenv::var("USE_HYPER").is_ok() {
5655
println!("Booting with a hyper based server");
5756
Hyper(HyperService::new(app, threads as usize))
5857
} else {

src/config.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{env, uploaders::Uploader, Env, Replica};
2-
use std::{env, path::PathBuf};
2+
use std::path::PathBuf;
33
use url::Url;
44

55
#[derive(Clone, Debug)]
@@ -43,12 +43,12 @@ impl Default for Config {
4343
fn default() -> Config {
4444
let checkout = PathBuf::from(env("GIT_REPO_CHECKOUT"));
4545
let api_protocol = String::from("https");
46-
let mirror = if env::var("MIRROR").is_ok() {
46+
let mirror = if dotenv::var("MIRROR").is_ok() {
4747
Replica::ReadOnlyMirror
4848
} else {
4949
Replica::Primary
5050
};
51-
let heroku = env::var("HEROKU").is_ok();
51+
let heroku = dotenv::var("HEROKU").is_ok();
5252
let cargo_env = if heroku {
5353
Env::Production
5454
} else {
@@ -62,12 +62,12 @@ impl Default for Config {
6262
Uploader::S3 {
6363
bucket: s3::Bucket::new(
6464
env("S3_BUCKET"),
65-
env::var("S3_REGION").ok(),
65+
dotenv::var("S3_REGION").ok(),
6666
env("S3_ACCESS_KEY"),
6767
env("S3_SECRET_KEY"),
6868
&api_protocol,
6969
),
70-
cdn: env::var("S3_CDN").ok(),
70+
cdn: dotenv::var("S3_CDN").ok(),
7171
proxy: None,
7272
}
7373
}
@@ -83,18 +83,18 @@ impl Default for Config {
8383
Uploader::S3 {
8484
bucket: s3::Bucket::new(
8585
env("S3_BUCKET"),
86-
env::var("S3_REGION").ok(),
87-
env::var("S3_ACCESS_KEY").unwrap_or_default(),
88-
env::var("S3_SECRET_KEY").unwrap_or_default(),
86+
dotenv::var("S3_REGION").ok(),
87+
dotenv::var("S3_ACCESS_KEY").unwrap_or_default(),
88+
dotenv::var("S3_SECRET_KEY").unwrap_or_default(),
8989
&api_protocol,
9090
),
91-
cdn: env::var("S3_CDN").ok(),
91+
cdn: dotenv::var("S3_CDN").ok(),
9292
proxy: None,
9393
}
9494
}
9595
// In Development mode, either running as a primary instance or a read-only mirror
9696
_ => {
97-
if env::var("S3_BUCKET").is_ok() {
97+
if dotenv::var("S3_BUCKET").is_ok() {
9898
// If we've set the `S3_BUCKET` variable to any value, use all of the values
9999
// for the related S3 environment variables and configure the app to upload to
100100
// and read from S3 like production does. All values except for bucket are
@@ -103,12 +103,12 @@ impl Default for Config {
103103
Uploader::S3 {
104104
bucket: s3::Bucket::new(
105105
env("S3_BUCKET"),
106-
env::var("S3_REGION").ok(),
107-
env::var("S3_ACCESS_KEY").unwrap_or_default(),
108-
env::var("S3_SECRET_KEY").unwrap_or_default(),
106+
dotenv::var("S3_REGION").ok(),
107+
dotenv::var("S3_ACCESS_KEY").unwrap_or_default(),
108+
dotenv::var("S3_SECRET_KEY").unwrap_or_default(),
109109
&api_protocol,
110110
),
111-
cdn: env::var("S3_CDN").ok(),
111+
cdn: dotenv::var("S3_CDN").ok(),
112112
proxy: None,
113113
}
114114
} else {

src/controllers/site_metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use super::prelude::*;
66
/// If `HEROKU_SLUG_COMMIT` is not set, returns `"unknown"`.
77
pub fn show_deployed_sha(req: &mut dyn Request) -> CargoResult<Response> {
88
let deployed_sha =
9-
::std::env::var("HEROKU_SLUG_COMMIT").unwrap_or_else(|_| String::from("unknown"));
9+
dotenv::var("HEROKU_SLUG_COMMIT").unwrap_or_else(|_| String::from("unknown"));
1010

1111
#[derive(Serialize)]
1212
struct R<'a> {

src/db.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::env;
2-
31
use conduit::Request;
42
use diesel::prelude::*;
53
use diesel::r2d2::{self, ConnectionManager, CustomizeConnection};
@@ -68,7 +66,7 @@ impl Deref for DieselPooledConn<'_> {
6866
pub fn connect_now() -> ConnectionResult<PgConnection> {
6967
use diesel::Connection;
7068
let mut url = Url::parse(&crate::env("DATABASE_URL")).expect("Invalid database URL");
71-
if env::var("HEROKU").is_ok() && !url.query_pairs().any(|(k, _)| k == "sslmode") {
69+
if dotenv::var("HEROKU").is_ok() && !url.query_pairs().any(|(k, _)| k == "sslmode") {
7270
url.query_pairs_mut().append_pair("sslmode", "require");
7371
}
7472
PgConnection::establish(&url.to_string())
@@ -80,7 +78,7 @@ pub fn diesel_pool(
8078
config: r2d2::Builder<ConnectionManager<PgConnection>>,
8179
) -> DieselPool {
8280
let mut url = Url::parse(url).expect("Invalid database URL");
83-
if env::var("HEROKU").is_ok() && !url.query_pairs().any(|(k, _)| k == "sslmode") {
81+
if dotenv::var("HEROKU").is_ok() && !url.query_pairs().any(|(k, _)| k == "sslmode") {
8482
url.query_pairs_mut().append_pair("sslmode", "require");
8583
}
8684

src/tests/record.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use cargo_registry::models::NewUser;
33
use std::{
44
borrow::Cow,
55
collections::HashSet,
6-
env,
76
fs::{self, File},
87
io::{self, prelude::*},
98
net,
@@ -80,7 +79,7 @@ enum Record {
8079

8180
pub fn proxy() -> (String, Bomb) {
8281
let me = thread::current().name().unwrap().to_string();
83-
let record = env::var("RECORD").is_ok();
82+
let record = dotenv::var("RECORD").is_ok();
8483

8584
let a = t!(net::TcpListener::bind("127.0.0.1:0"));
8685
let ret = format!("http://{}", t!(a.local_addr()));

0 commit comments

Comments
 (0)