This repository has been archived by the owner on Oct 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
way_get.go
67 lines (52 loc) · 1.67 KB
/
way_get.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
package way
import (
"net"
"strings"
"github.com/pghq/go-tea"
"github.com/pghq/go-way/country"
"github.com/pghq/go-way/geonames"
)
// IP lookup
func (r *Radar) IP(addr string) (*geonames.Location, error) {
ip := net.ParseIP(addr)
if ip == nil {
return nil, tea.Err("invalid ip")
}
city, err := r.maxmind.Get(ip)
if err != nil {
return nil, tea.Stacktrace(err)
}
loc := geonames.Location{
Country: country.Country(strings.ToUpper(city.Country.IsoCode)),
PostalCode: city.Postal.Code,
}
if city, present := city.City.Names["en"]; present {
loc.City = city
}
if len(city.Subdivisions) > 0 {
loc.Subdivision1 = city.Subdivisions[0].IsoCode
}
loc.Latitude = city.Location.Latitude
loc.Longitude = city.Location.Longitude
return &loc, nil
}
// PSD primary subdivision lookup
func (r *Radar) PSD(country country.Country, subdivision1 string) (*geonames.Location, error) {
return r.geonames.Get(geonames.Primary(country, subdivision1))
}
// City lookup
func (r *Radar) City(country country.Country, subdivision1, city string) (*geonames.Location, error) {
return r.geonames.Get(geonames.City(country, subdivision1, city))
}
// Postal lookup
func (r *Radar) Postal(country country.Country, postal string) (*geonames.Location, error) {
return r.geonames.Get(geonames.PostalCode(country, postal))
}
// SSD secondary division lookup
func (r *Radar) SSD(country country.Country, subdivision1, subdivision2 string) (*geonames.Location, error) {
return r.geonames.Get(geonames.Secondary(country, subdivision1, subdivision2))
}
// Country lookup
func (r *Radar) Country(country country.Country) (*geonames.Location, error) {
return r.geonames.Get(geonames.Country(country))
}