From 38b82e7dadee0c532ee235b98cd3c7122abf6549 Mon Sep 17 00:00:00 2001 From: Aram Grigoryan <132480+aram356@users.noreply.github.com> Date: Tue, 18 Mar 2025 21:31:55 -0700 Subject: [PATCH 1/4] Replace constants with settings --- potsi.toml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/potsi.toml b/potsi.toml index 70abe9766..b1e1a2bf5 100644 --- a/potsi.toml +++ b/potsi.toml @@ -1,3 +1,7 @@ +[ad_server] +backend = "equativ_ad_api_2" + [synthetic] counter_store = "jevans_synth_id_counter" -opid_store = "jevans_synth_id_opid" \ No newline at end of file +opid_store = "jevans_synth_id_opid" +secret_key = "stackpop" From 06939c1870c3a5e82eab296c7c3adce748ecef99 Mon Sep 17 00:00:00 2001 From: Aram Grigoryan <132480+aram356@users.noreply.github.com> Date: Wed, 19 Mar 2025 15:55:41 -0700 Subject: [PATCH 2/4] Refactored to use constants --- potsi.toml | 4 ++++ src/constants.rs | 2 ++ src/main.rs | 21 +++++++++++++-------- src/prebid.rs | 24 +++++++++++++----------- src/settings.rs | 16 ++++++++++++++++ src/synthetic.rs | 9 +++++---- 6 files changed, 53 insertions(+), 23 deletions(-) diff --git a/potsi.toml b/potsi.toml index b1e1a2bf5..b95548164 100644 --- a/potsi.toml +++ b/potsi.toml @@ -1,5 +1,9 @@ [ad_server] backend = "equativ_ad_api_2" +server_url = "https://adapi-srv-eu.smartadserver.com/ac?pgid=2040327&fmtid=137675&synthetic_id={}" + +[prebid] +server_url = "http://68.183.113.79:8000/openrtb2/auction" [synthetic] counter_store = "jevans_synth_id_counter" diff --git a/src/constants.rs b/src/constants.rs index b2462f439..4fd501edd 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -2,3 +2,5 @@ pub const BACKEND2: &str = "equativ_ad_api_2"; pub const SECRET_KEY: &[u8] = b"stackpop"; pub const SYNTH_ID_COUNTER_STORE: &str = "jevans_synth_id_counter"; pub const SYNTH_ID_OPID_STORE: &str = "jevans_synth_id_opid"; +pub const SYNTH_HEADER_FRESH: &str = "X-Synthetic-Fresh"; +pub const SYNTH_HEADER_POTSI: &str = "X-Synthetic-Potsi"; diff --git a/src/main.rs b/src/main.rs index 7d12ce6f7..b9fb5072f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,9 @@ use std::env; mod constants; mod cookies; -use constants::*; +use constants::{ + BACKEND2, SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI, SYNTH_ID_COUNTER_STORE, SYNTH_ID_OPID_STORE, +}; mod models; use models::AdResponse; mod prebid; @@ -21,7 +23,10 @@ use templates::HTML_TEMPLATE; #[fastly::main] fn main(req: Request) -> Result { - let _settings = Settings::new(); + let settings = Settings::new(); + + println!("Settings {settings:?}"); + futures::executor::block_on(async { println!( "FASTLY_SERVICE_VERSION: {}", @@ -58,7 +63,7 @@ fn handle_main_page(req: Request) -> Result { println!( "Existing POTSI header: {:?}", - req.get_header("X-Synthetic-Potsi") + req.get_header(SYNTH_HEADER_POTSI) ); println!("Generated Fresh ID: {}", fresh_id); println!("Using POTSI ID: {}", synthetic_id); @@ -67,8 +72,8 @@ fn handle_main_page(req: Request) -> Result { let mut response = Response::from_status(StatusCode::OK) .with_body(HTML_TEMPLATE) .with_header(header::CONTENT_TYPE, "text/html") - .with_header("X-Synthetic-Fresh", &fresh_id) // Fresh ID always changes - .with_header("X-Synthetic-Potsi", &synthetic_id); // POTSI ID remains stable + .with_header(SYNTH_HEADER_FRESH, &fresh_id) // Fresh ID always changes + .with_header(SYNTH_HEADER_POTSI, &synthetic_id); // POTSI ID remains stable // Always set the cookie with the synthetic ID response.set_header( @@ -304,14 +309,14 @@ async fn handle_prebid_test(mut req: Request) -> Result { println!( "Existing POTSI header: {:?}", - req.get_header("X-Synthetic-Potsi") + req.get_header(SYNTH_HEADER_POTSI) ); println!("Generated Fresh ID: {}", fresh_id); println!("Using POTSI ID: {}", synthetic_id); // Set both IDs as headers - req.set_header("X-Synthetic-Fresh", &fresh_id); - req.set_header("X-Synthetic-Potsi", &synthetic_id); + req.set_header(SYNTH_HEADER_FRESH, &fresh_id); + req.set_header(SYNTH_HEADER_POTSI, &synthetic_id); println!("Using POTSI ID: {}, Fresh ID: {}", synthetic_id, fresh_id); diff --git a/src/prebid.rs b/src/prebid.rs index 704b0ba8e..c7906f310 100644 --- a/src/prebid.rs +++ b/src/prebid.rs @@ -1,9 +1,11 @@ -use crate::synthetic::generate_synthetic_id; -use fastly::http::Method; +use fastly::http::{header, Method}; use fastly::{Error, Request, Response}; use serde_json::json; use url; +use crate::constants::{SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI}; +use crate::synthetic::generate_synthetic_id; + /// Represents a request to the Prebid Server with all necessary parameters pub struct PrebidRequest { /// Synthetic ID used for user identification across requests @@ -29,7 +31,7 @@ impl PrebidRequest { pub fn new(req: &Request) -> Result { // Get the POTSI ID from header (which we just set in handle_prebid_test) let synthetic_id = req - .get_header("X-Synthetic-Potsi") + .get_header(SYNTH_HEADER_POTSI) .and_then(|h| h.to_str().ok()) .map(|s| s.to_string()) .unwrap_or_else(|| generate_synthetic_id(req)); @@ -50,12 +52,12 @@ impl PrebidRequest { // Try to get domain from Referer or Origin headers, fallback to default let domain = req - .get_header("Referer") + .get_header(header::REFERER) .and_then(|h| h.to_str().ok()) .and_then(|r| url::Url::parse(r).ok()) .and_then(|u| u.host_str().map(|h| h.to_string())) .or_else(|| { - req.get_header("Origin") + req.get_header(header::ORIGIN) .and_then(|h| h.to_str().ok()) .and_then(|o| url::Url::parse(o).ok()) .and_then(|u| u.host_str().map(|h| h.to_string())) @@ -66,7 +68,7 @@ impl PrebidRequest { // Create origin with owned String let origin = req - .get_header("Origin") + .get_header(header::ORIGIN) .and_then(|h| h.to_str().ok()) .map(|s| s.to_string()) .unwrap_or_else(|| format!("https://{}", domain)); @@ -92,7 +94,7 @@ impl PrebidRequest { // Get and store the POTSI ID value from the incoming request let potsi_id = incoming_req - .get_header("X-Synthetic-Potsi") + .get_header(SYNTH_HEADER_POTSI) .and_then(|h| h.to_str().ok()) .map(|s| s.to_string()) .unwrap_or_else(|| self.synthetic_id.clone()); @@ -160,11 +162,11 @@ impl PrebidRequest { "at": 1 }); - req.set_header("Content-Type", "application/json"); + req.set_header(header::CONTENT_TYPE, "application/json"); req.set_header("X-Forwarded-For", &self.client_ip); - req.set_header("Origin", &self.origin); - req.set_header("X-Synthetic-Fresh", &self.synthetic_id); - req.set_header("X-Synthetic-Potsi", &potsi_id); + req.set_header(header::ORIGIN, &self.origin); + req.set_header(SYNTH_HEADER_FRESH, &self.synthetic_id); + req.set_header(SYNTH_HEADER_POTSI, &potsi_id); println!( "Sending prebid request with Fresh ID: {} and POTSI ID: {}", diff --git a/src/settings.rs b/src/settings.rs index 9b35e55e5..c9c07afb7 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -2,16 +2,32 @@ use config::{Config, ConfigError, File, FileFormat}; use serde::Deserialize; use std::str; +#[derive(Debug, Deserialize)] +#[allow(unused)] +struct AdServer { + backend: String, + server_url: String, +} + +#[derive(Debug, Deserialize)] +#[allow(unused)] +struct Prebid { + server_url: String, +} + #[derive(Debug, Deserialize)] #[allow(unused)] struct Synthetic { counter_store: String, opid_store: String, + secret_key: String, } #[derive(Debug, Deserialize)] #[allow(unused)] pub(crate) struct Settings { + ad_server: AdServer, + prebid: Prebid, synthetic: Synthetic, } diff --git a/src/synthetic.rs b/src/synthetic.rs index 0c14a316f..802d8cb91 100644 --- a/src/synthetic.rs +++ b/src/synthetic.rs @@ -1,11 +1,12 @@ -use crate::constants::SECRET_KEY; -use crate::cookies::handle_request_cookies; use fastly::http::header; use fastly::Request; use hmac::{Hmac, Mac}; use log; use sha2::Sha256; +use crate::constants::{SECRET_KEY, SYNTH_HEADER_POTSI}; +use crate::cookies::handle_request_cookies; + type HmacSha256 = Hmac; /// Generates a fresh synthetic_id based on request parameters @@ -54,7 +55,7 @@ pub fn generate_synthetic_id(req: &Request) -> String { pub fn get_or_generate_synthetic_id(req: &Request) -> String { // First try to get existing POTSI ID from header if let Some(potsi) = req - .get_header("X-Synthetic-Potsi") + .get_header(SYNTH_HEADER_POTSI) .and_then(|h| h.to_str().ok()) .map(|s| s.to_string()) { @@ -116,7 +117,7 @@ mod tests { #[test] fn test_get_or_generate_synthetic_id_with_header() { - let req = create_test_request(vec![("X-Synthetic-Potsi", "existing_potsi_id")]); + let req = create_test_request(vec![(SYNTH_HEADER_POTSI, "existing_potsi_id")]); let synthetic_id = get_or_generate_synthetic_id(&req); assert_eq!(synthetic_id, "existing_potsi_id"); From 3ddeac995d5c90a5e0ea6366772e1c08e6125d62 Mon Sep 17 00:00:00 2001 From: Aram Grigoryan <132480+aram356@users.noreply.github.com> Date: Wed, 19 Mar 2025 17:42:54 -0700 Subject: [PATCH 3/4] Changes to use settings --- potsi.toml | 2 +- src/constants.rs | 5 +---- src/main.rs | 53 +++++++++++++++++++++++++----------------------- src/prebid.rs | 5 +++-- src/settings.rs | 28 ++++++++++++------------- src/synthetic.rs | 25 +++++++++++++++-------- 6 files changed, 63 insertions(+), 55 deletions(-) diff --git a/potsi.toml b/potsi.toml index b95548164..68cacc975 100644 --- a/potsi.toml +++ b/potsi.toml @@ -8,4 +8,4 @@ server_url = "http://68.183.113.79:8000/openrtb2/auction" [synthetic] counter_store = "jevans_synth_id_counter" opid_store = "jevans_synth_id_opid" -secret_key = "stackpop" +secret_key = "potsi" diff --git a/src/constants.rs b/src/constants.rs index 4fd501edd..2853ce2c7 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,6 +1,3 @@ -pub const BACKEND2: &str = "equativ_ad_api_2"; -pub const SECRET_KEY: &[u8] = b"stackpop"; -pub const SYNTH_ID_COUNTER_STORE: &str = "jevans_synth_id_counter"; -pub const SYNTH_ID_OPID_STORE: &str = "jevans_synth_id_opid"; +// pub const SYNTH_ID_OPID_STORE: &str = "jevans_synth_id_opid"; pub const SYNTH_HEADER_FRESH: &str = "X-Synthetic-Fresh"; pub const SYNTH_HEADER_POTSI: &str = "X-Synthetic-Potsi"; diff --git a/src/main.rs b/src/main.rs index b9fb5072f..5fa7ec0af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,9 +7,7 @@ use std::env; mod constants; mod cookies; -use constants::{ - BACKEND2, SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI, SYNTH_ID_COUNTER_STORE, SYNTH_ID_OPID_STORE, -}; +use constants::{SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI}; mod models; use models::AdResponse; mod prebid; @@ -23,8 +21,7 @@ use templates::HTML_TEMPLATE; #[fastly::main] fn main(req: Request) -> Result { - let settings = Settings::new(); - + let settings = Settings::new().unwrap(); println!("Settings {settings:?}"); futures::executor::block_on(async { @@ -34,9 +31,9 @@ fn main(req: Request) -> Result { ); match (req.get_method(), req.get_path()) { - (&Method::GET, "/") => handle_main_page(req), - (&Method::GET, "/ad-creative") => handle_ad_request(req), - (&Method::GET, "/prebid-test") => handle_prebid_test(req).await, + (&Method::GET, "/") => handle_main_page(&settings, req), + (&Method::GET, "/ad-creative") => handle_ad_request(&settings, req), + (&Method::GET, "/prebid-test") => handle_prebid_test(&settings, req).await, _ => Ok(Response::from_status(StatusCode::NOT_FOUND) .with_body("Not Found") .with_header(header::CONTENT_TYPE, "text/plain")), @@ -44,22 +41,22 @@ fn main(req: Request) -> Result { }) } -fn handle_main_page(req: Request) -> Result { +fn handle_main_page(settings: &Settings, req: Request) -> Result { println!( "Testing constants - BACKEND2: {}, SYNTH_ID_COUNTER_STORE: {}", - BACKEND2, SYNTH_ID_COUNTER_STORE + settings.ad_server.backend, settings.synthetic.counter_store, ); log_fastly::init_simple("mylogs", Info); // Calculate fresh ID first using the synthetic module - let fresh_id = synthetic::generate_synthetic_id(&req); + let fresh_id = synthetic::generate_synthetic_id(settings, &req); // Check for existing POTSI ID in this specific order: // 1. X-Synthetic-Potsi header // 2. Cookie // 3. Fall back to fresh ID - let synthetic_id = synthetic::get_or_generate_synthetic_id(&req); + let synthetic_id = synthetic::get_or_generate_synthetic_id(settings, &req); println!( "Existing POTSI header: {:?}", @@ -99,7 +96,7 @@ fn handle_main_page(req: Request) -> Result { Ok(response) } -fn handle_ad_request(req: Request) -> Result { +fn handle_ad_request(settings: &Settings, req: Request) -> Result { // Log headers for debugging let client_ip = req .get_client_ip_addr() @@ -113,11 +110,11 @@ fn handle_ad_request(req: Request) -> Result { println!("X-Forwarded-For: {}", x_forwarded_for.unwrap_or("None")); // Generate synthetic ID - let synthetic_id = generate_synthetic_id(&req); + let synthetic_id = generate_synthetic_id(settings, &req); // Increment visit counter in KV store - println!("Opening KV store: {}", SYNTH_ID_COUNTER_STORE); - let store = match KVStore::open(SYNTH_ID_COUNTER_STORE) { + println!("Opening KV store: {}", settings.synthetic.counter_store); + let store = match KVStore::open(settings.synthetic.counter_store.as_str()) { Ok(Some(store)) => store, Ok(None) => { println!("KV store not found"); @@ -180,7 +177,7 @@ fn handle_ad_request(req: Request) -> Result { println!(" {}: {:?}", name, value); } - match req.send(BACKEND2) { + match req.send(settings.ad_server.backend.as_str()) { Ok(mut res) => { println!( "Received response from backend with status: {}", @@ -241,8 +238,11 @@ fn handle_ad_request(req: Request) -> Result { println!("Found opid: {}", opid); // Store in opid KV store - println!("Attempting to open KV store: {}", SYNTH_ID_OPID_STORE); - match KVStore::open(SYNTH_ID_OPID_STORE) { + println!( + "Attempting to open KV store: {}", + settings.synthetic.opid_store + ); + match KVStore::open(settings.synthetic.opid_store.as_str()) { Ok(Some(store)) => { println!("Successfully opened KV store"); match store.insert(&synthetic_id, opid.as_bytes()) { @@ -256,12 +256,15 @@ fn handle_ad_request(req: Request) -> Result { } } Ok(None) => { - println!("KV store returned None: {}", SYNTH_ID_OPID_STORE); + println!( + "KV store returned None: {}", + settings.synthetic.opid_store + ); } Err(e) => { println!( "Error opening KV store '{}': {:?}", - SYNTH_ID_OPID_STORE, e + settings.synthetic.opid_store, e ); } }; @@ -298,14 +301,14 @@ fn handle_ad_request(req: Request) -> Result { } /// Handles the prebid test route with detailed error logging -async fn handle_prebid_test(mut req: Request) -> Result { +async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result { println!("Starting prebid test request handling"); // Calculate fresh ID - let fresh_id = synthetic::generate_synthetic_id(&req); + let fresh_id = synthetic::generate_synthetic_id(settings, &req); // Check for existing POTSI ID in same order as handle_main_page - let synthetic_id = synthetic::get_or_generate_synthetic_id(&req); + let synthetic_id = synthetic::get_or_generate_synthetic_id(settings, &req); println!( "Existing POTSI header: {:?}", @@ -320,7 +323,7 @@ async fn handle_prebid_test(mut req: Request) -> Result { println!("Using POTSI ID: {}, Fresh ID: {}", synthetic_id, fresh_id); - let prebid_req = match PrebidRequest::new(&req) { + let prebid_req = match PrebidRequest::new(settings, &req) { Ok(req) => { println!( "Successfully created PrebidRequest with synthetic ID: {}", diff --git a/src/prebid.rs b/src/prebid.rs index c7906f310..eb2e71a00 100644 --- a/src/prebid.rs +++ b/src/prebid.rs @@ -4,6 +4,7 @@ use serde_json::json; use url; use crate::constants::{SYNTH_HEADER_FRESH, SYNTH_HEADER_POTSI}; +use crate::settings::Settings; use crate::synthetic::generate_synthetic_id; /// Represents a request to the Prebid Server with all necessary parameters @@ -28,13 +29,13 @@ impl PrebidRequest { /// /// # Returns /// * `Result` - New PrebidRequest or error - pub fn new(req: &Request) -> Result { + pub fn new(settings: &Settings, req: &Request) -> Result { // Get the POTSI ID from header (which we just set in handle_prebid_test) let synthetic_id = req .get_header(SYNTH_HEADER_POTSI) .and_then(|h| h.to_str().ok()) .map(|s| s.to_string()) - .unwrap_or_else(|| generate_synthetic_id(req)); + .unwrap_or_else(|| generate_synthetic_id(settings, req)); // Get the original client IP from Fastly headers let client_ip = req diff --git a/src/settings.rs b/src/settings.rs index c9c07afb7..302dc4aec 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -4,37 +4,37 @@ use std::str; #[derive(Debug, Deserialize)] #[allow(unused)] -struct AdServer { - backend: String, - server_url: String, +pub struct AdServer { + pub backend: String, + pub server_url: String, } #[derive(Debug, Deserialize)] #[allow(unused)] -struct Prebid { - server_url: String, +pub struct Prebid { + pub server_url: String, } #[derive(Debug, Deserialize)] #[allow(unused)] -struct Synthetic { - counter_store: String, - opid_store: String, - secret_key: String, +pub struct Synthetic { + pub counter_store: String, + pub opid_store: String, + pub secret_key: String, } #[derive(Debug, Deserialize)] #[allow(unused)] pub(crate) struct Settings { - ad_server: AdServer, - prebid: Prebid, - synthetic: Synthetic, + pub ad_server: AdServer, + pub prebid: Prebid, + pub synthetic: Synthetic, } impl Settings { pub(crate) fn new() -> Result { - let tom_bytes = include_bytes!("../potsi.toml"); - let toml_str = str::from_utf8(tom_bytes).unwrap(); + let toml_bytes = include_bytes!("../potsi.toml"); + let toml_str = str::from_utf8(toml_bytes).unwrap(); let s = Config::builder() .add_source(File::from_str(toml_str, FileFormat::Toml)) diff --git a/src/synthetic.rs b/src/synthetic.rs index 802d8cb91..c6b868ed6 100644 --- a/src/synthetic.rs +++ b/src/synthetic.rs @@ -4,13 +4,14 @@ use hmac::{Hmac, Mac}; use log; use sha2::Sha256; -use crate::constants::{SECRET_KEY, SYNTH_HEADER_POTSI}; +use crate::constants::SYNTH_HEADER_POTSI; use crate::cookies::handle_request_cookies; +use crate::settings::Settings; type HmacSha256 = Hmac; /// Generates a fresh synthetic_id based on request parameters -pub fn generate_synthetic_id(req: &Request) -> String { +pub fn generate_synthetic_id(settings: &Settings, req: &Request) -> String { let user_agent = req .get_header(header::USER_AGENT) .map(|h| h.to_str().unwrap_or("Unknown")); @@ -42,7 +43,8 @@ pub fn generate_synthetic_id(req: &Request) -> String { log::info!("Input string for fresh ID: {}", input_string); - let mut mac = HmacSha256::new_from_slice(SECRET_KEY).expect("HMAC can take key of any size"); + let mut mac = HmacSha256::new_from_slice(settings.synthetic.secret_key.as_bytes()) + .expect("HMAC can take key of any size"); mac.update(input_string.as_bytes()); let fresh_id = hex::encode(mac.finalize().into_bytes()); @@ -52,7 +54,7 @@ pub fn generate_synthetic_id(req: &Request) -> String { } /// Gets or creates a synthetic_id from the request -pub fn get_or_generate_synthetic_id(req: &Request) -> String { +pub fn get_or_generate_synthetic_id(settings: &Settings, req: &Request) -> String { // First try to get existing POTSI ID from header if let Some(potsi) = req .get_header(SYNTH_HEADER_POTSI) @@ -79,7 +81,7 @@ pub fn get_or_generate_synthetic_id(req: &Request) -> String { } // If no existing POTSI ID found, generate a fresh one - let fresh_id = generate_synthetic_id(req); + let fresh_id = generate_synthetic_id(settings, req); log::info!("No existing POTSI ID found, using fresh ID: {}", fresh_id); fresh_id } @@ -100,6 +102,8 @@ mod tests { #[test] fn test_generate_synthetic_id() { + let settings = Settings::new().unwrap(); + let req = create_test_request(vec![ (&header::USER_AGENT.to_string(), "Mozilla/5.0"), (&header::COOKIE.to_string(), "pub_userid=12345"), @@ -108,7 +112,7 @@ mod tests { (&header::ACCEPT_LANGUAGE.to_string(), "en-US,en;q=0.9"), ]); - let synthetic_id = generate_synthetic_id(&req); + let synthetic_id = generate_synthetic_id(&settings, &req); assert_eq!( synthetic_id, "5023f58a61668e5405a804d18662fc0b37518875cac551ed86e5e7223b541600" @@ -117,28 +121,31 @@ mod tests { #[test] fn test_get_or_generate_synthetic_id_with_header() { + let settings = Settings::new().unwrap(); let req = create_test_request(vec![(SYNTH_HEADER_POTSI, "existing_potsi_id")]); - let synthetic_id = get_or_generate_synthetic_id(&req); + let synthetic_id = get_or_generate_synthetic_id(&settings, &req); assert_eq!(synthetic_id, "existing_potsi_id"); } #[test] fn test_get_or_generate_synthetic_id_with_cookie() { + let settings = Settings::new().unwrap(); let req = create_test_request(vec![( &header::COOKIE.to_string(), "synthetic_id=existing_cookie_id", )]); - let synthetic_id = get_or_generate_synthetic_id(&req); + let synthetic_id = get_or_generate_synthetic_id(&settings, &req); assert_eq!(synthetic_id, "existing_cookie_id"); } #[test] fn test_get_or_generate_synthetic_id_generate_new() { + let settings = Settings::new().unwrap(); let req = create_test_request(vec![]); - let synthetic_id = get_or_generate_synthetic_id(&req); + let synthetic_id = get_or_generate_synthetic_id(&settings, &req); assert!(!synthetic_id.is_empty()); } } From 884a5b7d4888cb6f28595ce44f94219cec9bd46e Mon Sep 17 00:00:00 2001 From: Aram Grigoryan <132480+aram356@users.noreply.github.com> Date: Wed, 19 Mar 2025 18:14:49 -0700 Subject: [PATCH 4/4] Changed to use settings --- potsi.toml | 4 ++-- src/constants.rs | 1 - src/main.rs | 11 ++++------- src/settings.rs | 4 ++-- src/synthetic.rs | 28 ++++++++++++++++++++++------ 5 files changed, 30 insertions(+), 18 deletions(-) diff --git a/potsi.toml b/potsi.toml index 68cacc975..fcf2f38e6 100644 --- a/potsi.toml +++ b/potsi.toml @@ -1,6 +1,6 @@ [ad_server] -backend = "equativ_ad_api_2" -server_url = "https://adapi-srv-eu.smartadserver.com/ac?pgid=2040327&fmtid=137675&synthetic_id={}" +ad_partner_url = "equativ_ad_api_2" +sync_url = "https://adapi-srv-eu.smartadserver.com/ac?pgid=2040327&fmtid=137675&synthetic_id={{synthetic_id}}" [prebid] server_url = "http://68.183.113.79:8000/openrtb2/auction" diff --git a/src/constants.rs b/src/constants.rs index 2853ce2c7..1b99afe5b 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1,3 +1,2 @@ -// pub const SYNTH_ID_OPID_STORE: &str = "jevans_synth_id_opid"; pub const SYNTH_HEADER_FRESH: &str = "X-Synthetic-Fresh"; pub const SYNTH_HEADER_POTSI: &str = "X-Synthetic-Potsi"; diff --git a/src/main.rs b/src/main.rs index 5fa7ec0af..e4ccefd35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -43,8 +43,8 @@ fn main(req: Request) -> Result { fn handle_main_page(settings: &Settings, req: Request) -> Result { println!( - "Testing constants - BACKEND2: {}, SYNTH_ID_COUNTER_STORE: {}", - settings.ad_server.backend, settings.synthetic.counter_store, + "Using ad_partner_url: {}, counter_store: {}", + settings.ad_server.ad_partner_url, settings.synthetic.counter_store, ); log_fastly::init_simple("mylogs", Info); @@ -163,10 +163,7 @@ fn handle_ad_request(settings: &Settings, req: Request) -> Result Result { println!( "Received response from backend with status: {}", diff --git a/src/settings.rs b/src/settings.rs index 302dc4aec..62acc4ec9 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -5,8 +5,8 @@ use std::str; #[derive(Debug, Deserialize)] #[allow(unused)] pub struct AdServer { - pub backend: String, - pub server_url: String, + pub ad_partner_url: String, + pub sync_url: String, } #[derive(Debug, Deserialize)] diff --git a/src/synthetic.rs b/src/synthetic.rs index c6b868ed6..4a05eb5ed 100644 --- a/src/synthetic.rs +++ b/src/synthetic.rs @@ -100,10 +100,26 @@ mod tests { req } + fn create_settings() -> Settings { + Settings { + ad_server: crate::settings::AdServer { + ad_partner_url: "https://example.com".to_string(), + sync_url: "https://example.com/synthetic_id={{synthetic_id}}".to_string(), + }, + prebid: crate::settings::Prebid { + server_url: "https://example.com".to_string(), + }, + synthetic: crate::settings::Synthetic { + counter_store: "https://example.com".to_string(), + opid_store: "https://example.com".to_string(), + secret_key: "secret_key".to_string(), + }, + } + } + #[test] fn test_generate_synthetic_id() { - let settings = Settings::new().unwrap(); - + let settings: Settings = create_settings(); let req = create_test_request(vec![ (&header::USER_AGENT.to_string(), "Mozilla/5.0"), (&header::COOKIE.to_string(), "pub_userid=12345"), @@ -115,13 +131,13 @@ mod tests { let synthetic_id = generate_synthetic_id(&settings, &req); assert_eq!( synthetic_id, - "5023f58a61668e5405a804d18662fc0b37518875cac551ed86e5e7223b541600" + "07cd73bb8c7db39753ab6b10198b10c3237a3f5a6d2232c6ce578f2c2a623e56" ) } #[test] fn test_get_or_generate_synthetic_id_with_header() { - let settings = Settings::new().unwrap(); + let settings = create_settings(); let req = create_test_request(vec![(SYNTH_HEADER_POTSI, "existing_potsi_id")]); let synthetic_id = get_or_generate_synthetic_id(&settings, &req); @@ -130,7 +146,7 @@ mod tests { #[test] fn test_get_or_generate_synthetic_id_with_cookie() { - let settings = Settings::new().unwrap(); + let settings = create_settings(); let req = create_test_request(vec![( &header::COOKIE.to_string(), "synthetic_id=existing_cookie_id", @@ -142,7 +158,7 @@ mod tests { #[test] fn test_get_or_generate_synthetic_id_generate_new() { - let settings = Settings::new().unwrap(); + let settings = create_settings(); let req = create_test_request(vec![]); let synthetic_id = get_or_generate_synthetic_id(&settings, &req);