use fastly::{Error, Request, Response, geo};

const CHUNKSIZE: f64 = 0.1;

#[fastly::main]
fn main(mut req: Request) -> Result<Response, Error> {
  if CHUNKSIZE > 0.0 {
    // Get client ip.
    let client_ip = req.get_client_ip_addr().unwrap();
    // Get client ip associated geo data.
    if let Some(geo) = geo::geo_lookup(client_ip) {
      // Round the coordinates to the nearest decimal point.
      let grid_id = format!("{:.1},{:.1}", geo.latitude(), geo.longitude());
      println!("Grid ID: {} Nearest city: {}", grid_id, geo.city());

      req.set_header("geo-grid-id", grid_id);
    }    
  }

  let beresp = req.send("http_me")?;

  Ok(beresp)

}