-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeolocation.go
More file actions
54 lines (47 loc) · 1.61 KB
/
geolocation.go
File metadata and controls
54 lines (47 loc) · 1.61 KB
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
package ipapi
import (
"encoding/json"
"io/ioutil"
"net/http"
)
// Geolocation response struct
type Geolocation struct {
Query string `json:"query"`
Status string `json:"status"`
Continent string `json:"continent"`
ContinentCode string `json:"continentCode"`
Country string `json:"country"`
CountryCode string `json:"countryCode"`
Region string `json:"region"`
RegionName string `json:"regionName"`
City string `json:"city"`
District string `json:"district"`
Zip string `json:"zip"`
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
Timezone string `json:"timezone"`
Currency string `json:"currency"`
Isp string `json:"isp"`
Org string `json:"org"`
AS string `json:"as"`
ASname string `json:"asname"`
Reverse string `json:"reverse"`
Mobile bool `json:"mobile"`
Proxy bool `json:"proxy"`
}
// GetLocation get geolocation by IP in localization
func GetLocation(ip string, localization string) (Geolocation, error) {
resp, err := http.DefaultClient.Get("http://ip-api.com/json/" + ip + "?fields=status,message,continent,continentCode,country,countryCode,region,regionName,city,district,zip,lat,lon,timezone,currency,isp,org,as,asname,reverse,mobile,proxy,query&lang=" + localization)
if err != nil {
return Geolocation{}, err
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Geolocation{}, err
}
var geo Geolocation
if err = json.Unmarshal(data, &geo); err != nil {
return Geolocation{}, err
}
return geo, nil
}