1+ use fastly:: geo:: geo_lookup;
12use fastly:: http:: { header, Method , StatusCode } ;
23use fastly:: KVStore ;
34use fastly:: { Error , Request , Response } ;
4- use fastly:: geo:: geo_lookup;
55use log:: LevelFilter :: Info ;
66use serde_json:: json;
77use std:: env;
@@ -64,28 +64,37 @@ fn main(req: Request) -> Result<Response, Error> {
6464fn get_dma_code ( req : & mut Request ) -> Option < String > {
6565 // Debug: Check if we're running in Fastly environment
6666 println ! ( "Fastly Environment Check:" ) ;
67- println ! ( " FASTLY_POP: {}" , std:: env:: var( "FASTLY_POP" ) . unwrap_or_else( |_| "not in Fastly" . to_string( ) ) ) ;
68- println ! ( " FASTLY_REGION: {}" , std:: env:: var( "FASTLY_REGION" ) . unwrap_or_else( |_| "not in Fastly" . to_string( ) ) ) ;
69-
67+ println ! (
68+ " FASTLY_POP: {}" ,
69+ std:: env:: var( "FASTLY_POP" ) . unwrap_or_else( |_| "not in Fastly" . to_string( ) )
70+ ) ;
71+ println ! (
72+ " FASTLY_REGION: {}" ,
73+ std:: env:: var( "FASTLY_REGION" ) . unwrap_or_else( |_| "not in Fastly" . to_string( ) )
74+ ) ;
75+
7076 // Get detailed geo information using geo_lookup
7177 if let Some ( geo) = req. get_client_ip_addr ( ) . and_then ( geo_lookup) {
7278 println ! ( "Geo Information Found:" ) ;
73-
79+
7480 // Set all available geo information in headers
7581 let city = geo. city ( ) ;
7682 req. set_header ( "X-Geo-City" , city) ;
7783 println ! ( " City: {}" , city) ;
78-
84+
7985 let country = geo. country_code ( ) ;
8086 req. set_header ( "X-Geo-Country" , country) ;
8187 println ! ( " Country: {}" , country) ;
82-
88+
8389 req. set_header ( "X-Geo-Continent" , format ! ( "{:?}" , geo. continent( ) ) ) ;
8490 println ! ( " Continent: {:?}" , geo. continent( ) ) ;
85-
86- req. set_header ( "X-Geo-Coordinates" , format ! ( "{},{}" , geo. latitude( ) , geo. longitude( ) ) ) ;
91+
92+ req. set_header (
93+ "X-Geo-Coordinates" ,
94+ format ! ( "{},{}" , geo. latitude( ) , geo. longitude( ) ) ,
95+ ) ;
8796 println ! ( " Location: ({}, {})" , geo. latitude( ) , geo. longitude( ) ) ;
88-
97+
8998 // Get and set the metro code (DMA)
9099 let metro_code = geo. metro_code ( ) ;
91100 req. set_header ( "X-Geo-Metro-Code" , metro_code. to_string ( ) ) ;
@@ -122,7 +131,9 @@ fn handle_main_page(settings: &Settings, mut req: Request) -> Result<Response, E
122131 if !consent. functional {
123132 // Return a version of the page without tracking
124133 return Ok ( Response :: from_status ( StatusCode :: OK )
125- . with_body ( HTML_TEMPLATE . replace ( "fetch('/prebid-test')" , "console.log('Tracking disabled')" ) )
134+ . with_body (
135+ HTML_TEMPLATE . replace ( "fetch('/prebid-test')" , "console.log('Tracking disabled')" ) ,
136+ )
126137 . with_header ( header:: CONTENT_TYPE , "text/html" )
127138 . with_header ( header:: CACHE_CONTROL , "no-store, private" ) ) ;
128139 }
@@ -157,7 +168,14 @@ fn handle_main_page(settings: &Settings, mut req: Request) -> Result<Response, E
157168 . with_header ( "x-compress-hint" , "on" ) ;
158169
159170 // Copy geo headers from request to response
160- for header_name in & [ "X-Geo-City" , "X-Geo-Country" , "X-Geo-Continent" , "X-Geo-Coordinates" , "X-Geo-Metro-Code" , "X-Geo-Info-Available" ] {
171+ for header_name in & [
172+ "X-Geo-City" ,
173+ "X-Geo-Country" ,
174+ "X-Geo-Continent" ,
175+ "X-Geo-Coordinates" ,
176+ "X-Geo-Metro-Code" ,
177+ "X-Geo-Info-Available" ,
178+ ] {
161179 if let Some ( value) = req. get_header ( * header_name) {
162180 response. set_header ( * header_name, value) ;
163181 }
@@ -191,15 +209,16 @@ fn handle_main_page(settings: &Settings, mut req: Request) -> Result<Response, E
191209
192210fn handle_ad_request ( settings : & Settings , mut req : Request ) -> Result < Response , Error > {
193211 // Check GDPR consent to determine if we should serve personalized or non-personalized ads
194- let consent = get_consent_from_request ( & req) . unwrap_or_default ( ) ;
195- let advertising_consent = req. get_header ( "X-Consent-Advertising" )
212+ let _consent = get_consent_from_request ( & req) . unwrap_or_default ( ) ;
213+ let advertising_consent = req
214+ . get_header ( "X-Consent-Advertising" )
196215 . and_then ( |h| h. to_str ( ) . ok ( ) )
197216 . map ( |v| v == "true" )
198217 . unwrap_or ( false ) ;
199218
200219 // Add DMA code extraction
201220 let dma_code = get_dma_code ( & mut req) ;
202-
221+
203222 println ! ( "Client location - DMA Code: {:?}" , dma_code) ;
204223
205224 // Log headers for debugging
@@ -231,16 +250,14 @@ fn handle_ad_request(settings: &Settings, mut req: Request) -> Result<Response,
231250 println ! ( "Fetching current count for synthetic ID: {}" , synthetic_id) ;
232251 let current_count: i32 = store
233252 . lookup ( & synthetic_id)
234- . and_then ( |mut val| {
235- match String :: from_utf8 ( val. take_body_bytes ( ) ) {
236- Ok ( s) => {
237- println ! ( "Value from KV store: {}" , s) ;
238- Ok ( Some ( s) )
239- }
240- Err ( e) => {
241- println ! ( "Error converting bytes to string: {}" , e) ;
242- Ok ( None )
243- }
253+ . and_then ( |mut val| match String :: from_utf8 ( val. take_body_bytes ( ) ) {
254+ Ok ( s) => {
255+ println ! ( "Value from KV store: {}" , s) ;
256+ Ok ( Some ( s) )
257+ }
258+ Err ( e) => {
259+ println ! ( "Error converting bytes to string: {}" , e) ;
260+ Ok ( None )
244261 }
245262 } )
246263 . and_then ( |opt_s| {
@@ -264,24 +281,33 @@ fn handle_ad_request(settings: &Settings, mut req: Request) -> Result<Response,
264281
265282 // Modify the ad server URL construction to include DMA code if available
266283 let ad_server_url = if advertising_consent {
267- let mut url = settings. ad_server . sync_url . replace ( "{{synthetic_id}}" , & synthetic_id) ;
284+ let mut url = settings
285+ . ad_server
286+ . sync_url
287+ . replace ( "{{synthetic_id}}" , & synthetic_id) ;
268288 if let Some ( dma) = dma_code {
269289 url = format ! ( "{}&dma={}" , url, dma) ;
270290 }
271291 url
272292 } else {
273293 // Use a different URL or parameter for non-personalized ads
274- settings. ad_server . sync_url . replace ( "{{synthetic_id}}" , "non-personalized" )
294+ settings
295+ . ad_server
296+ . sync_url
297+ . replace ( "{{synthetic_id}}" , "non-personalized" )
275298 } ;
276299
277300 println ! ( "Sending request to backend: {}" , ad_server_url) ;
278301
279302 // Add header logging here
280303 let mut ad_req = Request :: get ( ad_server_url) ;
281-
304+
282305 // Add consent information to the ad request
283- ad_req. set_header ( "X-Consent-Advertising" , if advertising_consent { "true" } else { "false" } ) ;
284-
306+ ad_req. set_header (
307+ "X-Consent-Advertising" ,
308+ if advertising_consent { "true" } else { "false" } ,
309+ ) ;
310+
285311 println ! ( "Request headers to Equativ:" ) ;
286312 for ( name, value) in ad_req. get_headers ( ) {
287313 println ! ( " {}: {:?}" , name, value) ;
@@ -395,7 +421,14 @@ fn handle_ad_request(settings: &Settings, mut req: Request) -> Result<Response,
395421 . with_body ( body) ;
396422
397423 // Copy geo headers from request to response
398- for header_name in & [ "X-Geo-City" , "X-Geo-Country" , "X-Geo-Continent" , "X-Geo-Coordinates" , "X-Geo-Metro-Code" , "X-Geo-Info-Available" ] {
424+ for header_name in & [
425+ "X-Geo-City" ,
426+ "X-Geo-Country" ,
427+ "X-Geo-Continent" ,
428+ "X-Geo-Coordinates" ,
429+ "X-Geo-Metro-Code" ,
430+ "X-Geo-Info-Available" ,
431+ ] {
399432 if let Some ( value) = req. get_header ( * header_name) {
400433 response. set_header ( * header_name, value) ;
401434 }
@@ -429,7 +462,8 @@ async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result<Res
429462 println ! ( "Starting prebid test request handling" ) ;
430463
431464 // Check consent status from headers
432- let advertising_consent = req. get_header ( "X-Consent-Advertising" )
465+ let advertising_consent = req
466+ . get_header ( "X-Consent-Advertising" )
433467 . and_then ( |h| h. to_str ( ) . ok ( ) )
434468 . map ( |v| v == "true" )
435469 . unwrap_or ( false ) ;
@@ -441,7 +475,10 @@ async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result<Res
441475 ( fresh, synth)
442476 } else {
443477 // Use non-personalized IDs when no consent
444- ( "non-personalized" . to_string ( ) , "non-personalized" . to_string ( ) )
478+ (
479+ "non-personalized" . to_string ( ) ,
480+ "non-personalized" . to_string ( ) ,
481+ )
445482 } ;
446483
447484 println ! (
@@ -455,7 +492,10 @@ async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result<Res
455492 // Set both IDs as headers
456493 req. set_header ( SYNTHETIC_HEADER_FRESH , & fresh_id) ;
457494 req. set_header ( SYNTHETIC_HEADER_POTSI , & synthetic_id) ;
458- req. set_header ( "X-Consent-Advertising" , if advertising_consent { "true" } else { "false" } ) ;
495+ req. set_header (
496+ "X-Consent-Advertising" ,
497+ if advertising_consent { "true" } else { "false" } ,
498+ ) ;
459499
460500 println ! ( "Using POTSI ID: {}, Fresh ID: {}" , synthetic_id, fresh_id) ;
461501
@@ -497,7 +537,10 @@ async fn handle_prebid_test(settings: &Settings, mut req: Request) -> Result<Res
497537 . with_header ( header:: CONTENT_TYPE , "application/json" )
498538 . with_header ( "X-Prebid-Test" , "true" )
499539 . with_header ( "X-Synthetic-ID" , & prebid_req. synthetic_id )
500- . with_header ( "X-Consent-Advertising" , if advertising_consent { "true" } else { "false" } )
540+ . with_header (
541+ "X-Consent-Advertising" ,
542+ if advertising_consent { "true" } else { "false" } ,
543+ )
501544 . with_header ( "x-compress-hint" , "on" )
502545 . with_body ( body) )
503546 }
0 commit comments