-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeo.go
58 lines (51 loc) · 1.62 KB
/
geo.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
package fastlike
import (
"encoding/json"
"net"
"net/http"
)
// Geo represents geographic data associated with a particular IP address
// See: https://docs.rs/crate/fastly/0.3.2/source/src/geo.rs
type Geo struct {
ASName string `json:"as_name"`
ASNumber int `json:"as_number"`
AreaCode int `json:"area_code"`
City string `json:"city"`
ConnSpeed string `json:"conn_speed"`
ConnType string `json:"conn_type"`
Continent string `json:"continent"`
CountryCode string `json:"country_code"`
CountryCode3 string `json:"country_code3"`
CountryName string `json:"country_name"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
MetroCode int `json:"metro_code"`
PostalCode string `json:"postal_code"`
ProxyDescription string `json:"proxy_description"`
ProxyType string `json:"proxy_type"`
Region string `json:"region,omitempty"`
UTCOffset int `json:"utc_offset"`
}
func defaultGeoLookup(ip net.IP) Geo {
return Geo{
ASName: "fastlike",
ASNumber: 64496,
AreaCode: 512,
City: "Austin",
CountryCode: "US",
CountryCode3: "USA",
CountryName: "United States of America",
Continent: "NA",
Region: "TX",
ConnSpeed: "satellite",
ConnType: "satellite",
}
}
func geoHandler(fn func(ip net.IP) Geo) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
addr := net.ParseIP(r.Header.Get("fastly-xqd-arg1"))
geo := fn(addr)
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(geo)
})
}