freegeoip: github.com/fiorix/freegeoip Index | Files | Directories

package freegeoip

import "github.com/fiorix/freegeoip"

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

Package Files

db.go doc.go

Variables

var (
    // ErrUnavailable may be returned by DB.Lookup when the database
    // 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"
)

func GeoIPUpdateURL Uses

func GeoIPUpdateURL(hostName string, userID string, licenseKey string, productID string) (url string, err error)

Calculate geoipupdate URL The auto update URL for paid products has a fun scheme. Use this function to calculate that URL from various information

type DB Uses

type DB struct {
    // contains filtered or unexported fields
}

DB is the IP geolocation database.

func Open Uses

func Open(dsn string) (db *DB, err error)

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 Uses

func OpenURL(url string, updateInterval, maxRetryInterval time.Duration) (db *DB, err error)

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) Close Uses

func (db *DB) Close()

Close the database.

func (*DB) Date Uses

func (db *DB) Date() time.Time

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 Uses

func (db *DB) Lookup(addr net.IP, result interface{}) error

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 Uses

func (db *DB) NotifyClose() <-chan struct{}

NotifyClose returns a channel that is closed when the database is closed.

func (*DB) NotifyError Uses

func (db *DB) NotifyError() (errChan <-chan error)

NotifyError returns a channel that notifies when an error occurs while downloading or reloading a DB that points to a URL.

func (*DB) NotifyInfo Uses

func (db *DB) NotifyInfo() <-chan string

NotifyInfo returns a channel that notifies informational messages while downloading or reloading.

func (*DB) NotifyOpen Uses

func (db *DB) NotifyOpen() (filename <-chan string)

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 Uses

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.

Directories

PathSynopsis
apiserverPackage apiserver provides the freegeoip web server.

Package freegeoip imports 15 packages (graph) and is imported by 7 packages. Updated 2017-01-06. Refresh now. Tools for package owners.