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.
Also, the freegeoip package provides http handlers that any Go http server (net/http) can use. These handlers can process IP geolocation lookup requests and return data in multiple formats like CSV, XML, JSON and JSONP. It has also an API for supporting custom formats.
Index ¶
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") )
Functions ¶
func ProxyHandler ¶
ProxyHandler is a wrapper for other http handlers that sets the client IP address in request.RemoteAddr to the first value of a comma separated list of IPs from the X-Forwarded-For request header. It resets the original RemoteAddr back after running the designated handler f.
Types ¶
type CSVEncoder ¶
type CSVEncoder struct {
UseCRLF bool
}
CSVEncoder encodes the results of an IP lookup as CSV.
func (*CSVEncoder) Encode ¶
func (f *CSVEncoder) Encode(w http.ResponseWriter, r *http.Request, q Query, ip net.IP) error
Encode implements the Encoder interface.
func (*CSVEncoder) NewQuery ¶
func (f *CSVEncoder) NewQuery() Query
NewQuery implements the Encoder interface.
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 remote file. It automatically downloads and updates the file in background.
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) 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 Encoder ¶
type Encoder interface { // NewQuery returns a query specification that is used to query // the IP database. It should be a data structure with tags // associated to its fields describing what fields to query in // the IP database, such as country and city. // // See the maxminddb package documentation for details on // fields available for the MaxMind database. NewQuery() Query // Encode writes data to the response of an http request // using the results of a query to the IP database. // // It encodes the query object into a specific format such // as XML or JSON and writes to the response. // // The IP passed to the encoder may be the result of a DNS // lookup, and if there are multiple IPs associated to the // hostname this will be a random one from the list. Encode(w http.ResponseWriter, r *http.Request, q Query, ip net.IP) error }
An Encoder that can provide a query specification to be used for querying the IP database, and later encode the results of that query in a specific format.
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
A Handler provides http handlers that can process requests and return data in multiple formats.
Usage:
handle := NewHandler(db) http.Handle("/json/", handle.JSON())
Note that the url pattern must end with a trailing slash since the handler looks for IP addresses or hostnames as parameters, for example /json/8.8.8.8 or /json/domain.com.
If no IP or hostname is provided, then the handler will query the IP address of the caller. See the ProxyHandler for more.
func NewHandler ¶
NewHandler creates and initializes a new Handler.
type JSONEncoder ¶
type JSONEncoder struct {
Indent bool
}
JSONEncoder encodes the results of an IP lookup as JSON.
func (*JSONEncoder) Encode ¶
func (f *JSONEncoder) Encode(w http.ResponseWriter, r *http.Request, q Query, ip net.IP) error
Encode implements the Encoder interface.
func (*JSONEncoder) NewQuery ¶
func (f *JSONEncoder) NewQuery() Query
NewQuery implements the Encoder interface.
func (*JSONEncoder) P ¶
func (f *JSONEncoder) P(w http.ResponseWriter, r *http.Request, record *responseRecord, callback string) error
P writes JSONP to an http response.
type Query ¶
type Query interface{}
A Query object is used to query the IP database.
Currently the only database supported is MaxMind, and the query is a data structure with tags that are used by the maxminddb.Lookup function.
type XMLEncoder ¶
type XMLEncoder struct {
Indent bool
}
XMLEncoder encodes the results of an IP lookup as XML.
func (*XMLEncoder) Encode ¶
func (f *XMLEncoder) Encode(w http.ResponseWriter, r *http.Request, q Query, ip net.IP) error
Encode implements the Encoder interface.
func (*XMLEncoder) NewQuery ¶
func (f *XMLEncoder) NewQuery() Query
NewQuery implements the Encoder interface.