@@ -8,7 +8,7 @@ use fastly::Request;
88
99use crate :: constants:: {
1010 HEADER_X_GEO_CITY , HEADER_X_GEO_CONTINENT , HEADER_X_GEO_COORDINATES , HEADER_X_GEO_COUNTRY ,
11- HEADER_X_GEO_INFO_AVAILABLE , HEADER_X_GEO_METRO_CODE ,
11+ HEADER_X_GEO_METRO_CODE ,
1212} ;
1313
1414/// Geographic information extracted from a request.
@@ -81,74 +81,26 @@ impl GeoInfo {
8181 pub fn has_metro_code ( & self ) -> bool {
8282 self . metro_code > 0
8383 }
84- }
85-
86- /// Extracts the DMA (Designated Market Area) code from the request's geolocation data.
87- ///
88- /// This function:
89- /// 1. Checks if running in Fastly environment
90- /// 2. Performs geo lookup based on client IP
91- /// 3. Sets various geo headers on the request
92- /// 4. Returns the metro code (DMA) if available
93- ///
94- /// # Arguments
95- ///
96- /// * `req` - The request to extract DMA code from
97- ///
98- /// # Returns
99- ///
100- /// The DMA/metro code as a string if available, None otherwise
101- pub fn get_dma_code ( req : & mut Request ) -> Option < String > {
102- // Debug: Check if we're running in Fastly environment
103- log:: info!( "Fastly Environment Check:" ) ;
104- log:: info!(
105- " FASTLY_POP: {}" ,
106- std:: env:: var( "FASTLY_POP" ) . unwrap_or_else( |_| "not in Fastly" . to_string( ) )
107- ) ;
108- log:: info!(
109- " FASTLY_REGION: {}" ,
110- std:: env:: var( "FASTLY_REGION" ) . unwrap_or_else( |_| "not in Fastly" . to_string( ) )
111- ) ;
112-
113- // Get detailed geo information using geo_lookup
114- if let Some ( geo) = req. get_client_ip_addr ( ) . and_then ( geo_lookup) {
115- log:: info!( "Geo Information Found:" ) ;
11684
117- // Set all available geo information in headers
118- let city = geo. city ( ) ;
119- req. set_header ( HEADER_X_GEO_CITY , city) ;
120- log:: info!( " City: {}" , city) ;
121-
122- let country = geo. country_code ( ) ;
123- req. set_header ( HEADER_X_GEO_COUNTRY , country) ;
124- log:: info!( " Country: {}" , country) ;
125-
126- req. set_header ( HEADER_X_GEO_CONTINENT , format ! ( "{:?}" , geo. continent( ) ) ) ;
127- log:: info!( " Continent: {:?}" , geo. continent( ) ) ;
128-
129- req. set_header (
130- HEADER_X_GEO_COORDINATES ,
131- format ! ( "{},{}" , geo. latitude( ) , geo. longitude( ) ) ,
132- ) ;
133- log:: info!( " Location: ({}, {})" , geo. latitude( ) , geo. longitude( ) ) ;
134-
135- // Get and set the metro code (DMA)
136- let metro_code = geo. metro_code ( ) ;
137- req. set_header ( HEADER_X_GEO_METRO_CODE , metro_code. to_string ( ) ) ;
138- log:: info!( "Found DMA/Metro code: {}" , metro_code) ;
139- return Some ( metro_code. to_string ( ) ) ;
140- } else {
141- log:: info!( "No geo information available for the request" ) ;
142- req. set_header ( HEADER_X_GEO_INFO_AVAILABLE , "false" ) ;
143- }
144-
145- // If no metro code is found, log all request headers for debugging
146- log:: info!( "No DMA/Metro code found. All request headers:" ) ;
147- for ( name, value) in req. get_headers ( ) {
148- log:: info!( " {}: {:?}" , name, value) ;
85+ /// Sets the geographic information headers on the given request.
86+ ///
87+ /// This sets the following headers:
88+ /// - `x-geo-city`
89+ /// - `x-geo-country`
90+ /// - `x-geo-continent`
91+ /// - `x-geo-coordinates`
92+ /// - `x-geo-metro-code`
93+ /// - `x-geo-region` (if available)
94+ pub fn set_headers ( & self , req : & mut Request ) {
95+ req. set_header ( HEADER_X_GEO_CITY , & self . city ) ;
96+ req. set_header ( HEADER_X_GEO_COUNTRY , & self . country ) ;
97+ req. set_header ( HEADER_X_GEO_CONTINENT , & self . continent ) ;
98+ req. set_header ( HEADER_X_GEO_COORDINATES , self . coordinates_string ( ) ) ;
99+ req. set_header ( HEADER_X_GEO_METRO_CODE , self . metro_code . to_string ( ) ) ;
100+ if let Some ( region) = & self . region {
101+ req. set_header ( "x-geo-region" , region) ;
102+ }
149103 }
150-
151- None
152104}
153105
154106/// Returns the geographic information for the request as a JSON response.
0 commit comments