-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfifteen.go
118 lines (92 loc) · 2.85 KB
/
fifteen.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
package fifteen
import (
"encoding/json"
"log"
"net/http"
"os"
"github.com/GoogleCloudPlatform/functions-framework-go/functions"
"github.com/joho/godotenv"
"github.com/yeoffrey/fifteen/places"
"github.com/yeoffrey/fifteen/routing"
"googlemaps.github.io/maps"
)
var client *maps.Client
func loadSecret() string {
if envApiKey := os.Getenv("GOOGLE_MAPS_API"); envApiKey != "" {
return envApiKey
}
// Load .env file
if err := godotenv.Load(); err != nil {
log.Fatalf("Error loading .env file: %v", err)
}
return os.Getenv("GOOGLE_MAPS_API")
}
func init() {
functions.HTTP("Fifteen", Entrypoint)
c, err := maps.NewClient(maps.WithAPIKey(loadSecret()))
if err != nil {
log.Fatalf(": %v\n", err)
}
client = c
}
func Entrypoint(w http.ResponseWriter, r *http.Request) {
// Set CORS headers
w.Header().Set("Access-Control-Allow-Origin", "*") // Allow all origins, change '*' to specific origin if needed
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
// Handle preflight request
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
if r.Method != http.MethodPost {
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
}
var reqBody FifteenRequest
// Parse request to a FifteenRequest
err := json.NewDecoder(r.Body).Decode(&reqBody)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
originAddress := places.GetOrigin(client, reqBody.OriginAddress)
// TODO: We can write a function to build this
placesMap := map[LocationType]*places.Place{
Cafe: places.GetPlace(client, originAddress.ID, string(Cafe), PlaceTypeLookup[Cafe]),
School: places.GetPlace(client, originAddress.ID, string(School), PlaceTypeLookup[School]),
}
placeRoutes := handleRouteMatrix(*originAddress, placesMap)
// Send response
jsonResponse, err := json.Marshal(placeRoutes)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonResponse)
}
func handleRouteMatrix(origin places.Place, places map[LocationType]*places.Place) map[LocationType]*PlaceRoute {
placeIDs := []string{}
for _, place := range places {
placeIDs = append(placeIDs, place.ID)
}
routeMatrix := routing.GetRouteMatrix(client, origin.ID, placeIDs)
placeRoutes := map[LocationType]*PlaceRoute{}
i := 0
for key := range places {
// TODO: What happens if there is 0 rows?
// TODO: What happens if there if Status != OK
routeMetadata := routeMatrix.Rows[0].Elements[i]
placeRoutes[key] = &PlaceRoute{
Place: places[key],
Route: &routing.Route{
Distance: routeMetadata.Distance.Meters,
Duration: routeMetadata.Duration.Seconds(),
Mode: routing.Biking,
},
}
i++
}
return placeRoutes
}