-
Notifications
You must be signed in to change notification settings - Fork 0
/
geo.go
105 lines (85 loc) · 2.07 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
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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net"
"net/http"
"github.com/oschwald/geoip2-golang"
)
var db *geoip2.Reader
func main() {
env := flag.String("c", "development", "the program config environment")
flag.Parse()
var c config
err := c.read(env)
if err != nil {
log.Fatal("config", err)
}
err = initDb(c)
if err != nil {
log.Fatal("initDb", err)
}
http.HandleFunc("/lookup", handleLookup)
http.HandleFunc("/distance", handleDistance)
err = http.ListenAndServe(fmt.Sprintf(":%v", c.Port), nil)
if err != nil {
log.Fatal("unable to start server", err)
}
}
func initDb(c config) error {
var err error
db, err = geoip2.Open(c.DbFile)
return err
}
func handleLookup(res http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.NotFound(res, req)
return
}
sIp := req.URL.Query().Get("ip")
ip := net.ParseIP(sIp)
if ip == nil {
http.Error(res, fmt.Sprintf("Invalid IP address %v", sIp), 400)
return
}
record, err := db.City(ip)
if err != nil {
http.Error(res, fmt.Sprintf("Unexpected error: %v", err), 500)
return
}
location := NewLocation(record)
data, err := json.Marshal(location)
if err != nil {
http.Error(res, fmt.Sprintf("Unexpected error: %v", err), 500)
return
}
res.Header().Set("Content-Type", "application/json")
res.Write(data)
}
func handleDistance(res http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.NotFound(res, req)
return
}
latlng1 := req.URL.Query().Get("latlng1")
latlng2 := req.URL.Query().Get("latlng2")
if latlng1 == "" || latlng2 == "" {
http.Error(res, "latlng1 and latlng2 are required", 400)
return
}
c1, err := ParseCoordinates(latlng1)
if err != nil {
http.Error(res, fmt.Sprintf("invalid latlng1 format %v:%v", latlng1, err), 400)
return
}
c2, err := ParseCoordinates(latlng2)
if err != nil {
http.Error(res, fmt.Sprintf("invalid latlng2 format %v:%v", latlng2, err), 400)
return
}
d := GreatCircleDistance(c1, c2)
res.Header().Set("Content-Type", "application/json")
res.Write([]byte(fmt.Sprintf("{\"distance\": %.2f}", d)))
}