Documentation ¶
Overview ¶
Package freegeoip provides an API for searching the geolocation of IP addresses. It uses a database that can be either a local file or a remote resource from a URL.
Local databases are monitored by fsnotify and reloaded when the file is either updated or overwritten.
Remote databases are automatically downloaded and updated in background so you can focus on using the API and not managing the database.
Index ¶
- Variables
- func MaxMindUpdateURL(hostname, productID, userID, licenseKey string) (string, error)
- type DB
- func (db *DB) Close()
- func (db *DB) Date() time.Time
- func (db *DB) Lookup(addr net.IP, result interface{}) error
- func (db *DB) NotifyClose() <-chan struct{}
- func (db *DB) NotifyError() (errChan <-chan error)
- func (db *DB) NotifyInfo() <-chan string
- func (db *DB) NotifyOpen() (filename <-chan string)
- type DefaultQuery
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // points to a URL and is not yet available because it's being // downloaded in background. ErrUnavailable = errors.New("no database available") // MaxMindDB is the URL of the free MaxMind GeoLite2 database. MaxMindDB = "https://cdn.jsdelivr.net/npm/geolite2-city/GeoLite2-City.mmdb.gz" )
Functions ¶
func MaxMindUpdateURL ¶
MaxMindUpdateURL generates the URL for MaxMind paid databases.
Types ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the IP geolocation database.
func Open ¶
Open creates and initializes a DB from a local file.
The database file is monitored by fsnotify and automatically reloads when the file is updated or overwritten.
Example ¶
db, err := Open("./testdata.gz") if err != nil { log.Fatal(err) } var result customQuery err = db.Lookup(net.ParseIP("8.8.8.8"), &result) if err != nil { log.Fatal(err) } defer db.Close() log.Printf("%#v", result)
Output:
func OpenURL ¶
OpenURL creates and initializes a DB from a URL. It automatically downloads and updates the file in background, and keeps a local copy on $TMPDIR.
Example ¶
updateInterval := 24 * time.Hour maxRetryInterval := time.Hour db, err := OpenURL(MaxMindDB, updateInterval, maxRetryInterval) if err != nil { log.Fatal(err) } defer db.Close() select { case <-db.NotifyOpen(): // Wait for the db to be downloaded. case err := <-db.NotifyError(): log.Fatal(err) } var result customQuery err = db.Lookup(net.ParseIP("8.8.8.8"), &result) if err != nil { log.Fatal(err) } log.Printf("%#v", result)
Output:
func (*DB) Date ¶
Date returns the UTC date the database file was last modified. If no database file has been opened the behaviour of Date is undefined.
func (*DB) Lookup ¶
Lookup performs a database lookup of the given IP address, and stores the response into the result value. The result value must be a struct with specific fields and tags as described here: https://godoc.org/github.com/oschwald/maxminddb-golang#Reader.Lookup
See the DefaultQuery for an example of the result struct.
func (*DB) NotifyClose ¶
func (db *DB) NotifyClose() <-chan struct{}
NotifyClose returns a channel that is closed when the database is closed.
func (*DB) NotifyError ¶
NotifyError returns a channel that notifies when an error occurs while downloading or reloading a DB that points to a URL.
func (*DB) NotifyInfo ¶
NotifyInfo returns a channel that notifies informational messages while downloading or reloading.
func (*DB) NotifyOpen ¶
NotifyOpen returns a channel that notifies when a new database is loaded or reloaded. This can be used to monitor background updates when the DB points to a URL.
type DefaultQuery ¶
type DefaultQuery struct { Continent struct { Names map[string]string `maxminddb:"names"` } `maxminddb:"continent"` Country struct { ISOCode string `maxminddb:"iso_code"` Names map[string]string `maxminddb:"names"` } `maxminddb:"country"` Region []struct { ISOCode string `maxminddb:"iso_code"` Names map[string]string `maxminddb:"names"` } `maxminddb:"subdivisions"` City struct { Names map[string]string `maxminddb:"names"` } `maxminddb:"city"` Location struct { Latitude float64 `maxminddb:"latitude"` Longitude float64 `maxminddb:"longitude"` MetroCode uint `maxminddb:"metro_code"` TimeZone string `maxminddb:"time_zone"` } `maxminddb:"location"` Postal struct { Code string `maxminddb:"code"` } `maxminddb:"postal"` }
DefaultQuery is the default query used for database lookups.