Skip to content

Commit 838df29

Browse files
authored
Templatize synthetic
1 parent 0803d61 commit 838df29

File tree

9 files changed

+166
-34
lines changed

9 files changed

+166
-34
lines changed

Cargo.lock

Lines changed: 110 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ futures = "0.3"
2525
tokio = { version = "1.0", features = ["sync", "macros", "io-util", "rt", "time"] }
2626
url = "2.4.1"
2727
config = "0.15.11"
28+
handlebars = "6.3.2"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ git clone [email protected]:IABTechLab/trusted-server.git
9191
:information_source: Note that you’ll have to edit the following files for your setup:
9292

9393
- fastly.toml (service ID, author, description)
94-
- Potsi.toml (KV store ID names)
94+
- potsi.toml (KV store ID names)
9595

9696
### Build
9797

potsi.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,11 @@ server_url = "http://68.183.113.79:8000/openrtb2/auction"
99
counter_store = "jevans_synth_id_counter"
1010
opid_store = "jevans_synth_id_opid"
1111
secret_key = "potsi"
12+
# Possible values
13+
# - "client_ip"
14+
# - "user_agent"
15+
# - "first_party_id"
16+
# - "auth_user_id"
17+
# - "publisher_domain"
18+
# - "accept_language"
19+
template = "{{ client_ip }}:{{ user_agent }}:{{ first_party_id }}:{{ auth_user_id }}:{{ publisher_domain }}:{{ accept_language }}"

src/constants.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
pub const SYNTH_HEADER_FRESH: &str = "X-Synthetic-Fresh";
2-
pub const SYNTH_HEADER_POTSI: &str = "X-Synthetic-Potsi";
1+
pub const SYNTHETIC_HEADER_FRESH: &str = "X-Synthetic-Fresh";
2+
pub const SYNTHETIC_HEADER_POTSI: &str = "X-Synthetic-Potsi";
3+
pub const SYNTHETIC_HEADER_PUB_USER_ID: &str = "X-Pub-User-ID";

src/main.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::env;
77

88
mod constants;
99
mod cookies;
10-
use constants::{SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI};
10+
use constants::{SYNTHETIC_HEADER_FRESH, SYNTHETIC_HEADER_POTSI};
1111
mod models;
1212
use models::AdResponse;
1313
mod prebid;
@@ -60,7 +60,7 @@ fn handle_main_page(settings: &Settings, req: Request) -> Result<Response, Error
6060

6161
println!(
6262
"Existing POTSI header: {:?}",
63-
req.get_header(SYNTH_HEADER_POTSI)
63+
req.get_header(SYNTHETIC_HEADER_POTSI)
6464
);
6565
println!("Generated Fresh ID: {}", fresh_id);
6666
println!("Using POTSI ID: {}", synthetic_id);
@@ -69,8 +69,8 @@ fn handle_main_page(settings: &Settings, req: Request) -> Result<Response, Error
6969
let mut response = Response::from_status(StatusCode::OK)
7070
.with_body(HTML_TEMPLATE)
7171
.with_header(header::CONTENT_TYPE, "text/html")
72-
.with_header(SYNTH_HEADER_FRESH, &fresh_id) // Fresh ID always changes
73-
.with_header(SYNTH_HEADER_POTSI, &synthetic_id); // POTSI ID remains stable
72+
.with_header(SYNTHETIC_HEADER_FRESH, &fresh_id) // Fresh ID always changes
73+
.with_header(SYNTHETIC_HEADER_POTSI, &synthetic_id); // POTSI ID remains stable
7474

7575
// Always set the cookie with the synthetic ID
7676
response.set_header(
@@ -312,14 +312,14 @@ async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result<Res
312312

313313
println!(
314314
"Existing POTSI header: {:?}",
315-
req.get_header(SYNTH_HEADER_POTSI)
315+
req.get_header(SYNTHETIC_HEADER_POTSI)
316316
);
317317
println!("Generated Fresh ID: {}", fresh_id);
318318
println!("Using POTSI ID: {}", synthetic_id);
319319

320320
// Set both IDs as headers
321-
req.set_header(SYNTH_HEADER_FRESH, &fresh_id);
322-
req.set_header(SYNTH_HEADER_POTSI, &synthetic_id);
321+
req.set_header(SYNTHETIC_HEADER_FRESH, &fresh_id);
322+
req.set_header(SYNTHETIC_HEADER_POTSI, &synthetic_id);
323323

324324
println!("Using POTSI ID: {}, Fresh ID: {}", synthetic_id, fresh_id);
325325

@@ -344,7 +344,7 @@ async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result<Res
344344

345345
println!("Attempting to send bid request to Prebid Server at prebid_backend");
346346

347-
match prebid_req.send_bid_request(&req).await {
347+
match prebid_req.send_bid_request(settings, &req).await {
348348
// Pass the original request
349349
Ok(mut prebid_response) => {
350350
println!("Received response from Prebid Server");

src/prebid.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use fastly::http::{header, Method};
22
use fastly::{Error, Request, Response};
33
use serde_json::json;
44

5-
use crate::constants::{SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI};
5+
use crate::constants::{SYNTHETIC_HEADER_FRESH, SYNTHETIC_HEADER_POTSI};
66
use crate::settings::Settings;
77
use crate::synthetic::generate_synthetic_id;
88

@@ -31,7 +31,7 @@ impl PrebidRequest {
3131
pub fn new(settings: &Settings, req: &Request) -> Result<Self, Error> {
3232
// Get the POTSI ID from header (which we just set in handle_prebid_test)
3333
let synthetic_id = req
34-
.get_header(SYNTH_HEADER_POTSI)
34+
.get_header(SYNTHETIC_HEADER_POTSI)
3535
.and_then(|h| h.to_str().ok())
3636
.map(|s| s.to_string())
3737
.unwrap_or_else(|| generate_synthetic_id(settings, req));
@@ -89,12 +89,16 @@ impl PrebidRequest {
8989
///
9090
/// # Returns
9191
/// * `Result<Response, Error>` - Prebid Server response or error
92-
pub async fn send_bid_request(&self, incoming_req: &Request) -> Result<Response, Error> {
93-
let mut req = Request::new(Method::POST, "http://68.183.113.79:8000/openrtb2/auction");
92+
pub async fn send_bid_request(
93+
&self,
94+
settings: &Settings,
95+
incoming_req: &Request,
96+
) -> Result<Response, Error> {
97+
let mut req = Request::new(Method::POST, settings.prebid.server_url.to_owned());
9498

9599
// Get and store the POTSI ID value from the incoming request
96100
let potsi_id = incoming_req
97-
.get_header(SYNTH_HEADER_POTSI)
101+
.get_header(SYNTHETIC_HEADER_POTSI)
98102
.and_then(|h| h.to_str().ok())
99103
.map(|s| s.to_string())
100104
.unwrap_or_else(|| self.synthetic_id.clone());
@@ -165,8 +169,8 @@ impl PrebidRequest {
165169
req.set_header(header::CONTENT_TYPE, "application/json");
166170
req.set_header("X-Forwarded-For", &self.client_ip);
167171
req.set_header(header::ORIGIN, &self.origin);
168-
req.set_header(SYNTH_HEADER_FRESH, &self.synthetic_id);
169-
req.set_header(SYNTH_HEADER_POTSI, &potsi_id);
172+
req.set_header(SYNTHETIC_HEADER_FRESH, &self.synthetic_id);
173+
req.set_header(SYNTHETIC_HEADER_POTSI, &potsi_id);
170174

171175
println!(
172176
"Sending prebid request with Fresh ID: {} and POTSI ID: {}",

src/settings.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Synthetic {
2121
pub counter_store: String,
2222
pub opid_store: String,
2323
pub secret_key: String,
24+
pub template: String,
2425
}
2526

2627
#[derive(Debug, Deserialize)]

0 commit comments

Comments
 (0)