forked from matryer/goblueprints
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
61 lines (56 loc) · 1.57 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
package main
import (
"encoding/json"
"log"
"net/http"
"runtime"
"strconv"
"strings"
"github.com/matryer/goblueprints/chapter7/meander"
)
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
meander.APIKey = "YOUR API KEY HERE"
http.HandleFunc("/journeys", cors(func(w http.ResponseWriter, r *http.Request) {
respond(w, r, meander.Journeys)
}))
http.HandleFunc("/recommendations", cors(func(w http.ResponseWriter, r *http.Request) {
q := &meander.Query{
Journey: strings.Split(r.URL.Query().Get("journey"), "|"),
}
var err error
q.Lat, err = strconv.ParseFloat(r.URL.Query().Get("lat"), 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
q.Lng, err = strconv.ParseFloat(r.URL.Query().Get("lng"), 64)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
q.Radius, err = strconv.Atoi(r.URL.Query().Get("radius"))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
q.CostRangeStr = r.URL.Query().Get("cost")
places := q.Run()
respond(w, r, places)
}))
log.Println("serving meander API on :8080")
http.ListenAndServe(":8080", http.DefaultServeMux)
}
func respond(w http.ResponseWriter, r *http.Request, data []interface{}) error {
publicData := make([]interface{}, len(data))
for i, d := range data {
publicData[i] = meander.Public(d)
}
return json.NewEncoder(w).Encode(publicData)
}
func cors(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
f(w, r)
}
}