GeoIP Example
This recipe demonstrates how to build a GeoIP lookup service with Fiber. It proxies requests to the ip-api.com JSON API and caches responses for 10 minutes using Fiber's built-in cache middleware.
Note: This recipe depends on the free ip-api.com service. The free tier is limited to 1000 requests per minute from a single IP address. For higher traffic, consider a paid plan or a self-hosted alternative such as geoip-maxmind.
Prerequisites
- Go 1.21+
- Internet access (ip-api.com is called at runtime — no local database required)
Setup
-
Clone the repository:
git clone https://github.com/gofiber/recipes.gitcd recipes/geoip -
Install dependencies:
go get
Running the Application
go run main.go
The server starts on port 3000 by default. Set the PORT environment variable to override:
PORT=8080 go run main.go
Open http://localhost:3000 in a browser to use the web UI.
Example
Look up geolocation data for an IP address via the /geo endpoint:
curl "http://localhost:3000/geo?ip=178.62.56.160"
Example response:
{
"status": "success",
"country": "United Kingdom",
"countryCode": "GB",
"region": "ENG",
"regionName": "England",
"city": "London",
"zip": "EC1A",
"lat": 51.5085,
"lon": -0.1257,
"timezone": "Europe/London",
"isp": "DigitalOcean, LLC",
"org": "DigitalOcean, LLC",
"as": "AS14061 DigitalOcean, LLC",
"query": "178.62.56.160"
}
Omit the ip query parameter to look up the caller's own IP address:
curl "http://localhost:3000/geo"