A fast Go-Micro based microservice for looking up MaxMind GeoIP2 and GeoLite2 database.
Requires a go installation.
A Database (choose one):
- Free GeoLite 2 database. Download the "MaxMind DB binary, gzipped" and unpack it.
- GeoIP2 Downloadable Database. This is a more detailed database and should be compatible, but since I don't have access to that, I have been unable to verify this.
This service works as Go-Micro microservice. You may want to
setup your own registry with MICRO_REGISTRY
/MICRO_REGISTRY_ADDRESS
or use other go-micro flags.
Download it
go get github.com/ProtocolONE/geoip-service
If you need it uou can rebuild proto file with protoc
protoc --proto_path=. --micro_out=. --go_out=. geoip.proto
Setup environment variable MAXMIND_GEOIP_DB_PATH
with path to the maxmind database path.
The path can be local file path like /application/assets/GeoLite2-City.mmdb
,
or it can be AWS S3 object path like s3://bucketName/GeoLite2-City.mmdb
. In the latter case it is required to provide S3 access credentials with the environment variables AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
.
By default service will be executed with declared by MICRO_REGISTRY
registry and GRPC as a transport.
The docker file in this project used to launch geoip-service in Protocol One environment. You may change it in any way you need it.
Once the service is running you can use go-micro to make requests
package main
import (
"context"
"fmt"
"github.com/ProtocolONE/geoip-service/pkg"
"github.com/ProtocolONE/geoip-service/pkg/proto"
"github.com/micro/go-micro"
)
func main() {
// create a new service
service := micro.NewService()
// parse command line flags
service.Init()
// Create new greeter client
client := proto.NewGeoIpService(geoip.ServiceName, service.Client())
// Call it
rsp, err := client.GetIpData(context.TODO(), &proto.GeoIpDataRequest{IP: "8.8.8.8"})
if err != nil {
fmt.Println(err)
}
// Print response
fmt.Println(rsp)
}