use fastly::{Request, Response};
use fastly::http::StatusCode;
use fastly::geo::{geo_lookup};

#[fastly::main]
fn main(req: Request) -> Result<Response, fastly::Error> {
    if req.get_url().path() == "/log" {
        if let Some(client_geo) = req.get_client_ip_addr().and_then(geo_lookup) {
            let city = client_geo.city();
            let query_string = match req.get_query_str() {
                Some(init_query_string) => format!("{}&clientCity={}", init_query_string, city),
                None => format!("clientCity={}", city),
            };            
            req.with_query_str(&query_string);

            log_fastly::init_simple("s3_rum_data", log::LevelFilter::Info);
            log::info!("request query parameters are {}", query_string); 
        }

        Ok(Response::new()
            .with_status(StatusCode::NO_CONTENT)
            .with_body("No Content"))
    } else {
        Ok(Response::new()
            .with_status(StatusCode::NOT_FOUND)
            .with_body("Not found"))
    }
}