GeoIP
Many web service providers need to determine the country of an incoming IP address, such as to display the website in the correct language. A common solution to this challenge is purchasing a GeoIP database and implementing an API to fetch the country information associated with the requester's IP. However, this approach has its downsides—costs, the need to provision servers or containers, maintaining a database, and ensuring High Availability (HA).
Arvancloud's Edge Computing service provides a faster and simpler alternative to setting up GeoIP functionality.
When a request reaches Arvancloud's CDN network, the updated databases identify the requester's country information, add it as an HTTP header to the request, and forward it to your backend server. Arvancloud's Edge Computing works similarly to a backend service, with the advantage of running your code distributed across all CDN locations.
Development and Deployment in Edge Computing
The following code sample shows how you can implement and run this GeoIP service using Arvancloud's Edge Computing. This code reads the X-Country-Code
header, added by Arvancloud's CDN, and returns it to the requester in JSON format:
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request));
});
async function handleRequest(request) {
const COUNTRY_CODE = "X-Country-Code";
const country = request.headers.get(COUNTRY_CODE);
return new Response(JSON.stringify({ "country": country }), {
headers: { 'content-type': 'application/json' }
});
}
The country code is provided as a two-letter abbreviation, as shown in the example below:
{
"country": "IR"
}
You can easily import this code into the Arvancloud Edge Computing panel and publish it to a web address.
If you need to send requests from other domains to your Edge Computing service, you can configure this in the "New Path" section.
Additionally, you can view service analytics, such as the number of requests received over time, in the "Reports" section.
With Arvancloud's Edge Computing, you can set up a GeoIP service to detect the requester's country in under a minute—quickly and without the complexity of traditional approaches.