Geo-IP API at the edge

Create an API endpoint for fetching GeoIP data for the requesting browser, implemented 100% at the edge. The response should show your current approximate location, but no requests to any origin servers.

VCL

Use this solution in your VCL service (click RUN below to test this solution or clone it to make changes):

Compute@Edge

Use this solution in your Compute@Edge service:

  1. Rust
let client_ip = req
.get_client_ip_addr()
.ok_or_else(|| anyhow!("could not get client ip"))?;
let geo = geo_lookup(client_ip).ok_or_else(|| anyhow!("no geographic data available"))?;
let body = json!({
"as": {
"name": geo.as_name(),
},
"geo" : {
"city": geo.city(),
"client_ip": client_ip,
"country_name": geo.country_name(),
"gmt_offset:": geo.utc_offset().to_string(),
},
});
Ok(Response::new().with_body_json(&body)?)