Geolocation REST API, obtain IP address geolocation data Location is extracted by looking up user IP in a Geo DB (from Maxmind).
- Make sure you have Go installed (download).
- Make sure you update the MaxMind license key in the configuration with your key.
- Then you can either run
make run
to run locally, ormake api-up
to run in a docker container. - To build an executable binary file, run
make build
in a command line terminal. This will build and creategeolocation
file.
- There are two configuration files by default:
dev
andlocaldocker
, but we can add as many config files per each environment, for exampleprod
. To use a specific config file before running the application, specifySERVER_MODE
environment variable. for exampleSERVER_MODE=dev
,SERVER_MODE=prod
orSERVER_MODE=your_config_file
. This will tell the application to look fordev.yaml
,prod.yaml
oryour_config_file.yaml
file and reads the configurations from that file. If you don't specify a value forSERVER_MODE
environment,dev
will be selected by default. - Configuration files are located under
configs
folder. - Format of the configuration file is YAML.
The file name of GEO DB. (The latest version will be downloaded from https://dev.maxmind.com/geoip/geoip2/geolite2/ each time you run the api, and will be scheduled to download it every day. It needs you to sign up and create a License Key)
It is located under the geodb
folder.
- Build the application (read the instructions section)
- Run this command in the terminal:
SERVER_MODE=dev ./geolocation
Output:
11:22AM INF pkg/config/config.go:13 > Configs are being initialized
11:22AM INF pkg/maxmind/maxmind.go:55 > Downloading maxmind db...
11:22AM INF cmd/api/main.go:115 > starting api server addr=:3000
- Build and run api (
make api-up
) Docker will run and, then you can use the webservice with port 3000 (configurable in the configs files).
loglevel
: log level can be a value fromtrace
,debug
,info
,warn
,error
examplemake run -loglevel=info
-
To check if the webservice is running, call this utl in a browser (http://127.0.0.1:3000). Note: Make sure the port 3000 is already free in the hosting environment.
-
This is the swagger documentation of the webservice. In this page you can also download the swagger file.
GET http://localhost:3000/api/v1/geo/1.2.3.4
Response:
{
"City": {
"GeoNameID": 0,
"Names": null
},
"Continent": {
"Code": "OC",
"GeoNameID": 6255151,
"Names": {
"de": "Ozeanien",
"en": "Oceania",
"es": "Oceanía",
"fr": "Océanie",
"ja": "オセアニア",
"pt-BR": "Oceania",
"ru": "Океания",
"zh-CN": "大洋洲"
}
},
"Country": {
"GeoNameID": 2077456,
"IsInEuropeanUnion": false,
"IsoCode": "AU",
"Names": {
"de": "Australien",
"en": "Australia",
"es": "Australia",
"fr": "Australie",
"ja": "オーストラリア",
"pt-BR": "Austrália",
"ru": "Австралия",
"zh-CN": "澳大利亚"
}
},
"Location": {
"AccuracyRadius": 1000,
"Latitude": -33.494,
"Longitude": 143.2104,
"MetroCode": 0,
"TimeZone": "Australia/Sydney"
},
"Postal": {
"Code": ""
},
"RegisteredCountry": {
"GeoNameID": 2077456,
"IsInEuropeanUnion": false,
"IsoCode": "AU",
"Names": {
"de": "Australien",
"en": "Australia",
"es": "Australia",
"fr": "Australie",
"ja": "オーストラリア",
"pt-BR": "Austrália",
"ru": "Австралия",
"zh-CN": "澳大利亚"
}
},
"RepresentedCountry": {
"GeoNameID": 0,
"IsInEuropeanUnion": false,
"IsoCode": "",
"Names": null,
"Type": ""
},
"Subdivisions": null,
"Traits": {
"IsAnonymousProxy": false,
"IsSatelliteProvider": false
}
}
- geoip-golang (https://github.com/oschwald/geoip2-golang)
To read from geoip db file.
Viper is a complete configuration solution for Go applications including 12-Factor apps. It is designed to work within an application, and can handle all types of configuration needs and formats.
GeoLite2: This product includes GeoLite2 data created by MaxMind, available from https://www.maxmind.com