-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
181 lines (175 loc) · 4.66 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package main
import (
"encoding/json"
"flag"
"log"
"net/http"
"strconv"
"strings"
"time"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/julienschmidt/httprouter"
"github.com/petoc/hgt"
)
type (
// Location ...
Location struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
// ResultItem ...
ResultItem struct {
Location
Elevation float64 `json:"elevation,omitempty"`
Resolution float64 `json:"resolution,omitempty"`
Error int `json:"error,omitempty"`
}
// Response ...
Response struct {
Result []*ResultItem `json:"result"`
}
// Request ...
Request struct {
Locations []*Location `json:"locations"`
}
)
func locatationNotFound(response *Response, location *Location) {
response.Result = append(response.Result, &ResultItem{
Location: *location,
Error: 404,
})
}
func jsonLocationHandler(hgtDataDir *hgt.DataDir) func(http.ResponseWriter, *http.Request, httprouter.Params) {
return func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
locations := []*Location{}
if r.Method == "GET" {
query := r.URL.Query()
queryLocations := query.Get("locations")
if queryLocations == "" {
w.WriteHeader(http.StatusBadRequest)
return
}
strLocations := strings.Split(queryLocations, "|")
if len(strLocations) == 1 && strings.Index(strLocations[0], ",") < 1 {
w.WriteHeader(http.StatusBadRequest)
return
}
for _, strLocation := range strLocations {
coordinates := strings.Split(strLocation, ",")
location := &Location{}
if len(coordinates) == 2 {
location.Latitude, _ = strconv.ParseFloat(coordinates[0], 64)
location.Longitude, _ = strconv.ParseFloat(coordinates[1], 64)
}
locations = append(locations, location)
}
} else if r.Method == "POST" {
if !strings.HasPrefix(r.Header.Get("Content-Type"), "application/json") {
w.WriteHeader(http.StatusBadRequest)
return
}
request := &Request{}
err := json.NewDecoder(r.Body).Decode(request)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
locations = request.Locations
}
response := &Response{
Result: []*ResultItem{},
}
noLocationFound := true
for _, location := range locations {
elevation, resolution, err := hgtDataDir.ElevationAt(location.Latitude, location.Longitude)
if err != nil {
locatationNotFound(response, location)
continue
}
noLocationFound = false
response.Result = append(response.Result, &ResultItem{
Location: *location,
Elevation: float64(elevation),
Resolution: float64(resolution),
})
}
if noLocationFound {
w.WriteHeader(http.StatusNotFound)
return
}
b, err := json.Marshal(response)
if err != nil {
log.Println(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(200)
w.Write(b)
}
}
func main() {
var host, dataDir string
var port int
flag.StringVar(&host, "host", "127.0.0.1", "Host")
flag.IntVar(&port, "port", 8080, "Port")
flag.StringVar(&dataDir, "dir", "data", "Data directory")
flag.Parse()
// log.SetFlags(0)
lruCache, err := lru.NewWithEvict(1000, func(key, value interface{}) {
if file, ok := value.(*hgt.File); ok {
file.Close()
}
})
if err != nil {
log.Fatal(err)
}
cache := &hgt.Cache{
OnGet: func(key string) (*hgt.File, bool) {
if v, ok := lruCache.Get(key); ok {
return v.(*hgt.File), true
}
return nil, false
},
OnAdd: func(key string, value *hgt.File) {
lruCache.Add(key, value)
},
OnClear: func() error {
lruCache.Purge()
return nil
},
}
hgtDataDir, err := hgt.OpenDataDir(dataDir, &hgt.DataDirOptions{
Cache: cache,
RangeValidator: hgt.DefaultRangeValidator(),
})
if err != nil {
log.Fatal(err)
}
defer hgtDataDir.Close()
router := httprouter.New()
router.GlobalOPTIONS = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Header.Get("Access-Control-Request-Method") != "" {
header := w.Header()
header.Set("Access-Control-Allow-Methods", r.Header.Get("Allow"))
header.Set("Access-Control-Allow-Origin", "*")
}
w.WriteHeader(http.StatusNoContent)
})
router.PanicHandler = func(w http.ResponseWriter, r *http.Request, p interface{}) {
log.Println(p)
w.WriteHeader(http.StatusInternalServerError)
}
jsonHandler := jsonLocationHandler(hgtDataDir)
router.GET("/json", jsonHandler)
router.POST("/json", jsonHandler)
addr := strings.Join([]string{host, strconv.Itoa(port)}, ":")
server := &http.Server{
Addr: addr,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
Handler: router,
}
log.Printf("listening=%s", addr)
log.Fatal(server.ListenAndServe())
}