Skip to content

Commit 14f11d9

Browse files
authored
Rebuild when TRUSTED_SERVER env variables change
1 parent 4321f9b commit 14f11d9

File tree

4 files changed

+84
-15
lines changed

4 files changed

+84
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3939
- Changed to use log statements
4040
- Updated fastly.toml for local development
4141

42+
### Fixed
43+
- Rebuild when `TRUSTED_SERVER__*` env variables change
44+
4245
## [1.0.6] - 2025-05-29
4346

4447
### Changed

crates/common/Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ sha2 = "0.10.9"
2626
tokio = { version = "1.46", features = ["sync", "macros", "io-util", "rt", "time"] }
2727
url = "2.4.1"
2828

29+
[build-dependencies]
30+
serde = { version = "1.0", features = ["derive"] }
31+
serde_json = "1.0.91"
32+
config = "0.15.11"
33+
2934
[dev-dependencies]
3035
regex = "1.1.1"
3136
temp-env = "0.3.6"

crates/common/build.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#[path = "src/settings.rs"]
2+
mod settings;
3+
4+
use serde_json::Value;
5+
use std::collections::HashSet;
6+
7+
fn main() {
8+
// Watch the settings.rs file for changes
9+
println!("cargo:rerun-if-changed=../../trusted-server.toml");
10+
11+
// Create a default Settings instance and convert to JSON to discover all fields
12+
let default_settings = settings::Settings::default();
13+
let settings_json = serde_json::to_value(&default_settings).unwrap();
14+
15+
let mut env_vars = HashSet::new();
16+
collect_env_vars(&settings_json, &mut env_vars, vec![]);
17+
18+
// Print rerun-if-env-changed for each variable
19+
let mut sorted_vars: Vec<_> = env_vars.into_iter().collect();
20+
sorted_vars.sort();
21+
22+
for var in sorted_vars {
23+
println!("cargo:rerun-if-env-changed={}", var);
24+
}
25+
}
26+
27+
fn collect_env_vars(value: &Value, env_vars: &mut HashSet<String>, path: Vec<String>) {
28+
if let Value::Object(map) = value {
29+
for (key, val) in map {
30+
let mut new_path = path.clone();
31+
new_path.push(key.to_uppercase());
32+
33+
match val {
34+
Value::String(_) | Value::Number(_) | Value::Bool(_) => {
35+
// Leaf node - create environment variable
36+
let env_var = format!(
37+
"{}{}{}",
38+
settings::ENVIRONMENT_VARIABLE_PREFIX,
39+
settings::ENVIRONMENT_VARIABLE_SEPARATOR,
40+
new_path.join(settings::ENVIRONMENT_VARIABLE_SEPARATOR)
41+
);
42+
env_vars.insert(env_var);
43+
}
44+
Value::Object(_) => {
45+
// Recurse into nested objects
46+
collect_env_vars(val, env_vars, new_path);
47+
}
48+
_ => {}
49+
}
50+
}
51+
}
52+
}

crates/common/src/settings.rs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
11
use std::str;
22

33
use config::{Config, ConfigError, Environment, File, FileFormat};
4-
use serde::Deserialize;
4+
use serde::{Deserialize, Serialize};
55

6-
#[derive(Debug, Deserialize)]
7-
#[allow(unused)]
6+
pub const ENVIRONMENT_VARIABLE_PREFIX: &str = "TRUSTED_SERVER";
7+
pub const ENVIRONMENT_VARIABLE_SEPARATOR: &str = "__";
8+
9+
#[derive(Debug, Default, Deserialize, Serialize)]
810
pub struct AdServer {
911
pub ad_partner_url: String,
1012
pub sync_url: String,
1113
}
1214

13-
#[derive(Debug, Deserialize)]
14-
#[allow(unused)]
15+
#[derive(Debug, Default, Deserialize, Serialize)]
1516
pub struct Publisher {
1617
pub domain: String,
1718
pub cookie_domain: String,
1819
pub origin_url: String,
1920
}
2021

21-
#[derive(Debug, Deserialize)]
22-
#[allow(unused)]
22+
#[derive(Debug, Default, Deserialize, Serialize)]
2323
pub struct Prebid {
2424
pub server_url: String,
2525
}
2626

27-
#[derive(Debug, Deserialize)]
28-
#[allow(unused)]
27+
#[derive(Debug, Default, Deserialize, Serialize)]
2928
pub struct Synthetic {
3029
pub counter_store: String,
3130
pub opid_store: String,
3231
pub secret_key: String,
3332
pub template: String,
3433
}
3534

36-
#[derive(Debug, Deserialize)]
37-
#[allow(unused)]
35+
#[derive(Debug, Default, Deserialize, Serialize)]
3836
pub struct Settings {
3937
pub ad_server: AdServer,
4038
pub publisher: Publisher,
4139
pub prebid: Prebid,
4240
pub synthetic: Synthetic,
4341
}
4442

43+
#[allow(unused)]
4544
impl Settings {
4645
pub fn new() -> Result<Self, ConfigError> {
4746
let toml_bytes = include_bytes!("../../../trusted-server.toml");
@@ -52,8 +51,8 @@ impl Settings {
5251

5352
pub fn from_toml(toml_str: &str) -> Result<Self, ConfigError> {
5453
let environment = Environment::default()
55-
.prefix("TRUSTED_SERVER")
56-
.separator("__");
54+
.prefix(ENVIRONMENT_VARIABLE_PREFIX)
55+
.separator(ENVIRONMENT_VARIABLE_SEPARATOR);
5756

5857
let toml = File::from_str(toml_str, FileFormat::Toml);
5958
let config = Config::builder()
@@ -181,7 +180,12 @@ mod tests {
181180
let toml_str = re.replace(&toml_str, "");
182181

183182
temp_env::with_var(
184-
"TRUSTED_SERVER__AD_SERVER__AD_PARTNER_URL",
183+
format!(
184+
"{}{}AD_SERVER{}AD_PARTNER_URL",
185+
ENVIRONMENT_VARIABLE_PREFIX,
186+
ENVIRONMENT_VARIABLE_SEPARATOR,
187+
ENVIRONMENT_VARIABLE_SEPARATOR
188+
),
185189
Some("https://change-ad.com/serve"),
186190
|| {
187191
let settings = Settings::from_toml(&toml_str);
@@ -200,7 +204,12 @@ mod tests {
200204
let toml_str = crate_test_settings_str();
201205

202206
temp_env::with_var(
203-
"TRUSTED_SERVER__AD_SERVER__AD_PARTNER_URL",
207+
format!(
208+
"{}{}AD_SERVER{}AD_PARTNER_URL",
209+
ENVIRONMENT_VARIABLE_PREFIX,
210+
ENVIRONMENT_VARIABLE_SEPARATOR,
211+
ENVIRONMENT_VARIABLE_SEPARATOR
212+
),
204213
Some("https://change-ad.com/serve"),
205214
|| {
206215
let settings = Settings::from_toml(&toml_str);

0 commit comments

Comments
 (0)