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 GeoIPUpdateURL(hostName string, userID string, licenseKey string, productID string) (url string, err 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
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 = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz" )
Functions ¶
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.
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.
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 takes an IP address and a pointer to the result value to decode into. The result value pointed to must be a data value that corresponds to a record in the database. This may include a struct representation of the data, a map capable of holding the data or an empty interface{} value.
If result is a pointer to a struct, the struct need not include a field for every value that may be in the database. If a field is not present in the structure, the decoder will not decode that field, reducing the time required to decode the record.
See https://godoc.org/github.com/oschwald/maxminddb-golang#Reader.Lookup for details.
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.