Skip to content

Commit f9bcec0

Browse files
author
Thomas Luijken
committed
Load from .env file for local development
1 parent a64cac5 commit f9bcec0

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

oxybox/.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_FILE="example-config.yml"
2+
DNS_HOSTS="8.8.8.8,1.1.1.1"
3+
MIMIR_ENDPOINT="http://localhost:9009"

oxybox/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ serde = { version = "1.0", features = ["derive"] }
3131
unicode-truncate = "2.0"
3232
openssl = { version = "0.10", features = ["vendored"] }
3333
serde_yaml = "0.9"
34+
dotenvy = "0.15"
3435

3536
[build-dependencies]
3637
prost-build = "0.14" # Protobuf code generator for Prost
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,10 @@ demo:
55
- url: https://www.google.com
66
- url: https://www.github.com
77
accepted_status_codes: [200, 301]
8-
- url: https://www.kloosterdolphia.nl
9-
- url: https://expired.badssl.com
10-
accepted_status_codes: [200, 495, 526]
8+
- url: https://grafana.com/
119

1210
organisationX:
13-
organisation_id: 1
11+
organisation_id: another-org
1412
polling_interval_seconds: 20
1513
targets:
1614
- url: http://www.example.com

oxybox/src/config/app_config.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use std::{net::IpAddr, time::Duration};
2+
use std::env;
23

4+
use dotenvy::dotenv;
35
use tokio_native_tls::TlsConnector as TokioTlsConnector;
46
use trust_dns_resolver::{
57
TokioAsyncResolver,
@@ -20,14 +22,17 @@ pub struct AppConfig {
2022
/// parses it into a `Config` struct, and overrides certain values with environment variables.
2123
/// It also sets up the DNS hosts and Mimir endpoint.
2224
pub fn load_config() -> AppConfig {
25+
26+
dotenv().ok();
27+
2328
let config_file_location =
24-
std::env::var("CONFIG_FILE").unwrap_or_else(|_| "config.yml".to_string());
29+
env::var("CONFIG_FILE").unwrap_or_else(|_| "config.yml".to_string());
2530
let config_str = std::fs::read_to_string(&config_file_location)
2631
.expect("Failed to read config.yaml");
2732

2833
let config: Config = serde_yaml::from_str(&config_str).expect("Invalid YAML");
2934

30-
let dns_hosts = std::env::var("DNS_HOSTS")
35+
let dns_hosts = env::var("DNS_HOSTS")
3136
.unwrap_or_else(|_| "1.1.1.1,8.8.8.8".to_string())
3237
.split(',')
3338
.map(|s| s.trim().to_string())
@@ -36,7 +41,7 @@ pub fn load_config() -> AppConfig {
3641
println!("Using DNS hosts: {:?}", dns_hosts);
3742

3843
let mimir_endpoint =
39-
std::env::var("MIMIR_ENDPOINT").unwrap_or_else(|_| "http://localhost:9009".to_string());
44+
env::var("MIMIR_ENDPOINT").unwrap_or_else(|_| "http://localhost:9009".to_string());
4045

4146
let max_org_width = config.keys().map(|org| org.len()).max().unwrap_or(10);
4247

0 commit comments

Comments
 (0)