/
main.go
356 lines (289 loc) · 9.82 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
package main
import (
"net"
"net/http"
)
import "github.com/azer/crud"
import _ "github.com/go-sql-driver/mysql"
import "encoding/json"
import "fmt"
import "os"
import "time"
import "strconv"
import "io/ioutil"
import gj "github.com/kpawlik/geojson"
import "sync"
import "github.com/go-kit/kit/log"
var DB *crud.DB
var ingest_mutex = &sync.Mutex{}
var logger log.Logger
func main() {
logger = log.NewLogfmtLogger(os.Stderr)
logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
logger.Log("msg", "Starting bikefinder")
var err error
db_url := os.Getenv("BF_DATABASE_URL")
DB, err = crud.Connect("mysql", db_url)
if err != nil {
logger.Log("msg", "Failed to connect to db at "+db_url, "error", err)
panic(err)
}
err = DB.CreateTables(DockingStationStatus{})
if err != nil {
logger.Log("msg", "Failed to create db table", "error", err)
panic(err)
}
http.HandleFunc("/schemes", getSchemes)
http.HandleFunc("/stations", getStationsInside)
http.HandleFunc("/bikes-near", getBikesNear)
http.HandleFunc("/freedocks-near", getFreeDocksNear)
http.HandleFunc("/map", getMap)
http.HandleFunc("/ingest", ingest)
http.Handle("/", http.FileServer(http.Dir("./static")))
bind := fmt.Sprintf("%s:%s", os.Getenv("BF_IP"), os.Getenv("BF_PORT"))
logger.Log("msg", "Attempting to listen on "+bind)
err = http.ListenAndServe(bind, nil)
if err != nil {
logger.Log("msg", "Failed to listen", "error", err)
panic(err)
}
}
/**
* HAndler for returning a map
*/
func getMap(w http.ResponseWriter, r *http.Request) {
//map_url := "http://foo.bar"
lat := r.URL.Query().Get("lat")
lon := r.URL.Query().Get("lon")
mapbox_access_token := os.Getenv("MAPBOX_ACCESS_TOKEN")
var map_cache_filename = "./map-cache/" + lon + "-" + lat + ".png"
map_image, err := ioutil.ReadFile(map_cache_filename)
if err != nil {
var map_url = "https://api.mapbox.com/v4/mapbox.streets/url-https%3A%2F%2Fbikefinder-4ndrewl.rhcloud.com%2Fimages%2Fbike.png(" + lon + "," + lat + ")/" + lon + "," + lat + ",15/256x256.png?access_token=" + mapbox_access_token
logger.Log("msg", "cache-miss. Retrieving map from web", "url", map_url)
resp, err := http.Get(map_url)
defer resp.Body.Close()
if err != nil {
logger.Log("msg", "Failed to retrieve map", "map_url", map_url, "err", err)
http.Error(w, "", 500)
return
}
map_image, err = ioutil.ReadAll(resp.Body)
if err != nil {
logger.Log("msg", "Failed to read map body", "map_url", map_url, "err", err)
http.Error(w, "", 500)
return
}
ioutil.WriteFile(map_cache_filename, map_image, 0644)
}
w.Header().Set("Content-Type", "image/png")
w.Write(map_image)
}
/**
* Handler for returning stations inside a bounding box
*/
func getStationsInside(w http.ResponseWriter, r *http.Request) {
writeDockingStations(w, r, "stations")
}
/**
* Handler for returning a list of docking stations.
*/
func getFreeDocksNear(w http.ResponseWriter, r *http.Request) {
writeDockingStations(w, r, "freedocks")
}
/**
* Handler for returning a list of bikes.
*/
func getBikesNear(w http.ResponseWriter, r *http.Request) {
writeDockingStations(w, r, "bikes")
}
/**
* or free docks, or an error if that was not possible.
* @see getFreeDocksNear
* @see getBikesNear
*/
func writeDockingStations(w http.ResponseWriter, r *http.Request, filter_type string) {
lat, _ := strconv.ParseFloat(r.URL.Query().Get("lat"), 64)
lon, _ := strconv.ParseFloat(r.URL.Query().Get("lon"), 64)
x0, _ := strconv.ParseFloat(r.URL.Query().Get("x0"), 64)
y0, _ := strconv.ParseFloat(r.URL.Query().Get("y0"), 64)
x1, _ := strconv.ParseFloat(r.URL.Query().Get("x1"), 64)
y1, _ := strconv.ParseFloat(r.URL.Query().Get("y1"), 64)
var dockingStations DockingStations
var err error
var ret []byte
if filter_type == "bikes" {
err = dockingStations.GetBikesNear(lat, lon, 4)
}
if filter_type == "freedocks" {
err = dockingStations.GetBikesNear(lat, lon, 4)
}
if filter_type == "stations" {
err = dockingStations.GetStationsInside(x0, y0, x1, y1)
}
if err == nil {
features := []*gj.Feature{}
for _, dockingStationStatus := range dockingStations.DockingStationStatuses {
properties := map[string]interface{}{"name": dockingStationStatus.Name, "i": dockingStationStatus.DockId, "bikes": dockingStationStatus.Bikes, "docks": dockingStationStatus.Docks, "updated": dockingStationStatus.Time}
lat, _ := strconv.ParseFloat(dockingStationStatus.Lat, 64)
lon, _ := strconv.ParseFloat(dockingStationStatus.Lon, 64)
p := gj.NewPoint(gj.Coordinate{gj.CoordType(lon), gj.CoordType(lat)})
f := gj.NewFeature(p, properties, nil)
features = append(features, f)
}
ret, err = json.Marshal(features)
}
if err != nil {
//@todo set return code
logger.Log("msg", "There was an error retrieving docking stations", "err", err)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{msg: \"There was an error\"}"))
return
}
w.Header().Set("Server", "bikefinder")
w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}
/**
* Retrieve bikes near a lat long
*/
func (ds *DockingStations) GetBikesNear(lat float64, lon float64, min_bikes int) error {
sql := fmt.Sprintf("WHERE bikes >= %d AND lon > %.4f and lon < %.4f and lat > %.4f and lat < %.4f ORDER BY (POW((lon-(%.4f)),2) + POW((lat-(%.4f)),2)) LIMIT 10", min_bikes, lon-0.5, lon+0.5, lat-0.5, lat+0.5, lon, lat)
logger.Log("msg", "Getting bikes near", "sql", sql)
err := DB.Read(&ds.DockingStationStatuses, sql)
if err != nil {
logger.Log("msg", "There was an error", "err", err)
}
return err
}
/**
* Retrieve bikes near a lat long
*/
func (ds *DockingStations) GetStationsInside(x0 float64, y0 float64, x1 float64, y1 float64) error {
sql := fmt.Sprintf("WHERE lon > %.4f and lon < %.4f and lat > %.4f and lat < %.4f", x0, y0, x1, y1)
logger.Log("msg", "Getting stations inside", "sql", sql)
err := DB.Read(&ds.DockingStationStatuses, sql)
if err != nil {
logger.Log("msg", "There was an error", "err", err)
}
return err
}
/**
* Retrieve free docks near a lat long
*/
func (ds *DockingStations) GetFreeDocksNear(lat float64, lon float64, min_docks int) error {
sql := fmt.Sprintf("WHERE docks >= %d ORDER BY (POW((lon-%.4f),2) + POW((lat-%.4f),2)) LIMIT 10", min_docks, lon, lat)
err := DB.Read(&ds.DockingStationStatuses, sql)
logger.Log("msg", "Getting docks near", "sql", sql)
if err != nil {
logger.Log("msg", "There was an error", "err", err)
}
return err
}
func getSchemes(w http.ResponseWriter, r *http.Request) {
config_file := os.Getenv("BF_CONFIG")
file, _ := os.Open(config_file)
decoder := json.NewDecoder(file)
configuration := []BikeHireSchemeConfig{}
err := decoder.Decode(&configuration)
if err != nil {
logger.Log("msg", "Failed retrieve config", "err", err)
return
}
var public_config = []BikeHireSchemePublicConfig{}
for _, schemeConfig := range configuration {
public_config = append(public_config, schemeConfig.BikeHireSchemePublicConfig)
}
ret, err := json.Marshal(public_config)
w.Header().Set("Server", "bikefinder")
w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}
func ingest(w http.ResponseWriter, r *http.Request) {
incoming_ip, _, _ := net.SplitHostPort(r.RemoteAddr)
valid_ip := os.Getenv("BF_INGEST_IP")
if incoming_ip != valid_ip {
logger.Log("msg", "Attempt to ingest from invalid ip address", "ip", incoming_ip)
http.Error(w, "", 401)
return
}
config_file := os.Getenv("BF_CONFIG")
logger.Log("msg", "Startng Ingestion. Using config file at "+config_file)
file, _ := os.Open(config_file)
decoder := json.NewDecoder(file)
configuration := []BikeHireSchemeConfig{}
err := decoder.Decode(&configuration)
if err != nil {
logger.Log("msg", "Failed retrieve config", "err", err)
return
}
var bikeHireSchemes = []DockingStationStatusCollector{}
for _, schemeConfig := range configuration {
newScheme, err := bikeHireSchemeFactory(schemeConfig)
if err != nil {
logger.Log("msg", "Failed to create bikeScheme", "err", err)
}
if err == nil {
bikeHireSchemes = append(bikeHireSchemes, newScheme)
}
}
// We put a lock here because we don't want multiple processes overwriting data. This ensures that the most recent data is always written into the DB.
ingest_mutex.Lock()
defer ingest_mutex.Unlock()
_, err = DB.Query("delete from docking_station_status")
if err != nil {
logger.Log("msg", "Failed to delete old docking station status", "err", err)
return
}
msgc, errc := make(chan string), make(chan error)
for _, bikeHireScheme := range bikeHireSchemes {
go writeDockingStationStatusesToDB(bikeHireScheme, msgc, errc)
}
for i := 0; i < len(bikeHireSchemes); i++ {
select {
case msg := <-msgc:
logger.Log("msg", msg)
case err := <-errc:
logger.Log("msg", "There was an error", "err", err)
}
}
logger.Log("msg", "Done Ingesting")
logger.Log("msg", "Dumping all bikes")
var dss []DockingStationStatus
err = DB.Read(&dss)
if err != nil {
logger.Log("msg", "There was an error", "err", err)
}
ret, err := json.Marshal(dss)
if err != nil {
//@todo set return code
logger.Log("msg", "There was an error dumping docking stations", "err", err)
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{msg: \"There was an error\"}"))
return
}
w.Header().Set("Server", "bikefinder")
w.Header().Set("Content-Type", "application/json")
w.Write(ret)
}
/**
* Takes a bike hire scheme and writes the output to the db, informing a channel on error or completion.
*/
func writeDockingStationStatusesToDB(bikeHireScheme DockingStationStatusCollector, msgc chan string, errc chan error) {
var requestTime = time.Now()
data, err := bikeHireScheme.GetDockingStationStatuses()
if err != nil {
errc <- err
return
}
for _, ds := range data {
ds.RequestTime = requestTime
ds.SchemeDockId = ds.SchemeID + "-" + ds.DockId
err = DB.Create(ds)
if err != nil {
errc <- err
return
}
}
msgc <- "Done"
}