forked from fiorix/freegeoip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_test.go
118 lines (106 loc) · 3.01 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Copyright 2009-2014 The freegeoip authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package freegeoip
import (
"encoding/json"
"log"
"net"
"net/http"
"time"
)
var maxmindFile = "http://geolite.maxmind.com/download/geoip/database/GeoLite2-City.mmdb.gz"
func ExampleDatabaseQuery() {
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)
}
func ExampleRemoteDatabaseQuery() {
updateInterval := 24 * time.Hour
maxRetryInterval := time.Hour
db, err := OpenURL(maxmindFile, 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)
}
func ExampleServer() {
db, err := OpenURL(maxmindFile, 24*time.Hour, time.Hour)
if err != nil {
log.Fatal(err)
}
http.Handle("/csv/", NewHandler(db, &CSVEncoder{}))
http.Handle("/xml/", NewHandler(db, &XMLEncoder{}))
http.Handle("/json/", NewHandler(db, &JSONEncoder{}))
http.ListenAndServe(":8080", nil)
}
func ExampleServerWithCustomEncoder() {
db, err := Open("./testdata/db.gz")
if err != nil {
log.Fatal(err)
}
http.Handle("/custom/json/", NewHandler(db, &customEncoder{}))
http.ListenAndServe(":8080", nil)
}
// A customEncoder writes a custom JSON object to an http response.
type customEncoder struct{}
// A customQuery is the query executed in the maxmind database for
// every IP lookup request.
type customQuery struct {
Country struct {
ISOCode string `maxminddb:"iso_code"`
Names map[string]string `maxminddb:"names"`
} `maxminddb:"country"`
Location struct {
Latitude float64 `maxminddb:"latitude"`
Longitude float64 `maxminddb:"longitude"`
TimeZone string `maxminddb:"time_zone"`
} `maxminddb:"location"`
}
// A customResponse is what gets written to the http response as JSON.
type customResponse struct {
IP string
CountryCode string
CountryName string
Latitude float64
Longitude float64
TimeZone string
}
// NewQuery implements the freegeoip.Encoder interface.
func (f *customEncoder) NewQuery() Query {
return &customQuery{}
}
// Encode implements the freegeoip.Encoder interface.
func (f *customEncoder) Encode(w http.ResponseWriter, r *http.Request, q Query, ip net.IP) error {
record := q.(*customQuery)
out := &customResponse{
IP: ip.String(),
CountryCode: record.Country.ISOCode,
CountryName: record.Country.Names["en"], // Set to client lang.
Latitude: record.Location.Latitude,
Longitude: record.Location.Longitude,
TimeZone: record.Location.TimeZone,
}
w.Header().Set("Content-Type", "application/json")
return json.NewEncoder(w).Encode(&out)
}