-
Notifications
You must be signed in to change notification settings - Fork 0
/
serve.go
197 lines (170 loc) · 4.02 KB
/
serve.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package api
import (
"encoding/json"
"fmt"
"math"
"net/http"
"strconv"
"strings"
"github.com/kpotier/carburant/pkg/gas"
)
const degToRad = 0.017453292519943295
type Result struct {
Distance float32 `json:"distance"`
gas.Data
}
func (a *API) getStations(w http.ResponseWriter, r *http.Request) {
err := a.parseForm(w, r)
if err != nil {
a.log.Printf("parseForm: %s", err)
w.Write([]byte(`{"error": "POST_ERR"}`))
return
}
lng, lat, err := a.parseLngLat(r)
if err != nil {
a.log.Printf("parseLngLat: %s", err)
w.Write([]byte(`{"error": "POST_ERR"}`))
return
}
limStr := r.PostFormValue("lim")
lim, err := strconv.Atoi(limStr)
if err != nil {
a.log.Printf("lim: %s", err)
w.Write([]byte(`{"error": "POST_ERR"}`))
return
}
g := r.PostFormValue("gas")
a.mux.RLock()
defer a.mux.RUnlock()
var list = make([]Result, lim)
var sort = make([]int, 0, lim)
for i := 0; i < lim; i++ {
var distance float64 = math.MaxFloat64
var id int
for j, d := range a.data {
// Does it has the selected gas?
if _, ok := d.Gas[gas.GasType(g)]; !ok {
continue
}
// Already in the list?
var found bool
for _, k := range sort {
if k == j {
found = true
break
}
}
if found {
continue
}
dist := calcDistance(d.Coords[1], d.Coords[0], lng, lat)
if dist < distance {
distance = dist
id = j
}
}
sort = append(sort, id)
list[i] = Result{endDistance(distance), a.data[id]}
}
enc := json.NewEncoder(w)
err = enc.Encode(list)
if err != nil {
a.log.Println(err)
}
}
func (a *API) getHistory(w http.ResponseWriter, r *http.Request) {
err := a.parseForm(w, r)
if err != nil {
a.log.Printf("parseForm: %s", err)
w.Write([]byte(`{"error": "POST_ERR"}`))
return
}
id := r.PostFormValue("id")
gas := r.PostFormValue("gas")
a.muxDB.RLock()
defer a.muxDB.RUnlock()
enc := json.NewEncoder(w)
err = enc.Encode(a.db[id+gas])
if err != nil {
a.log.Println(err)
}
}
func (a *API) getFavorites(w http.ResponseWriter, r *http.Request) {
err := a.parseForm(w, r)
if err != nil {
a.log.Printf("parseForm: %s", err)
w.Write([]byte(`{"error": "POST_ERR"}`))
return
}
lng, lat, err := a.parseLngLat(r)
if err != nil {
a.log.Printf("parseLngLat: %s", err)
w.Write([]byte(`{"error": "POST_ERR"}`))
return
}
listStr := r.PostFormValue("list")
listSt := strings.Split(listStr, ",")
var listInt = make([]int, len(listSt))
for i, l := range listSt {
listInt[i], err = strconv.Atoi(l)
if err != nil {
a.log.Printf("list: %s", err)
w.Write([]byte(`{"error": "POST_ERR"}`))
return
}
}
a.mux.RLock()
defer a.mux.RUnlock()
var list = make([]Result, 0, len(listInt))
for _, d := range a.data {
// In the list?
var found bool
for _, k := range listInt {
if k == d.ID {
found = true
break
}
}
if !found {
continue
}
dist := calcDistance(d.Coords[1], d.Coords[0], lng, lat)
list = append(list, Result{endDistance(dist), d})
}
enc := json.NewEncoder(w)
err = enc.Encode(list)
if err != nil {
a.log.Println(err)
}
}
func calcDistance(lng1, lat1, lng2, lat2 float64) float64 {
// Faster but a little bit more inaccurate than the Haversine
// formula. As we only deal with short distances, this formula
// should not lead to problems.
x := degToRad * (lng1 - lng2) * math.Cos(0.5*degToRad*(lat2+lat1))
y := degToRad * (lat1 - lat2)
return x*x + y*y
}
func endDistance(dist float64) float32 {
return float32(math.Sqrt(dist) * 6371.)
}
func (a *API) parseForm(w http.ResponseWriter, r *http.Request) error {
w.Header().Set("content-type", "application/json; charset=utf-8")
if r.Method != "POST" {
return fmt.Errorf("getStations: method is %s, want %s", r.Method, "POST")
}
return r.ParseForm()
}
func (a *API) parseLngLat(r *http.Request) (float64, float64, error) {
lngStr := r.PostFormValue("lng")
latStr := r.PostFormValue("lat")
lng, err := strconv.ParseFloat(lngStr, 32)
if err != nil {
return 0, 0, err
}
lat, err := strconv.ParseFloat(latStr, 32)
if err != nil {
return 0, 0, err
}
return lng, lat, nil
}