forked from harlow/go-micro-services
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.go
110 lines (95 loc) · 2.84 KB
/
frontend.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
package services
import (
"encoding/json"
"fmt"
"net/http"
"github.com/harlow/go-micro-services/internal/trace"
"github.com/harlow/go-micro-services/internal/proto/profile"
"github.com/harlow/go-micro-services/internal/proto/search"
opentracing "github.com/opentracing/opentracing-go"
"google.golang.org/grpc"
)
// NewFrontend returns a new server
func NewFrontend(t opentracing.Tracer, searchconn, profileconn *grpc.ClientConn) *Frontend {
return &Frontend{
searchClient: search.NewSearchClient(searchconn),
profileClient: profile.NewProfileClient(profileconn),
tracer: t,
}
}
// Frontend implements frontend service
type Frontend struct {
searchClient search.SearchClient
profileClient profile.ProfileClient
tracer opentracing.Tracer
}
// Run the server
func (s *Frontend) Run(port int) error {
mux := trace.NewServeMux(s.tracer)
mux.Handle("/", http.FileServer(http.Dir("public")))
mux.Handle("/hotels", http.HandlerFunc(s.searchHandler))
return http.ListenAndServe(fmt.Sprintf(":%d", port), mux)
}
func (s *Frontend) searchHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
ctx := r.Context()
// in/out dates from query params
inDate, outDate := r.URL.Query().Get("inDate"), r.URL.Query().Get("outDate")
if inDate == "" || outDate == "" {
http.Error(w, "Please specify inDate/outDate params", http.StatusBadRequest)
return
}
// search for best hotels
// TODO(hw): allow lat/lon from input params
searchResp, err := s.searchClient.Nearby(ctx, &search.NearbyRequest{
Lat: 37.7749,
Lon: -122.4194,
InDate: inDate,
OutDate: outDate,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// grab locale from query params or default to en
locale := r.URL.Query().Get("locale")
if locale == "" {
locale = "en"
}
// hotel profiles
profileResp, err := s.profileClient.GetProfiles(ctx, &profile.Request{
HotelIds: searchResp.HotelIds,
Locale: locale,
})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
json.NewEncoder(w).Encode(geoJSONResponse(profileResp.Hotels))
}
// return a geoJSON response that allows google map to plot points directly on map
// https://developers.google.com/maps/documentation/javascript/datalayer#sample_geojson
func geoJSONResponse(hs []*profile.Hotel) map[string]interface{} {
fs := []interface{}{}
for _, h := range hs {
fs = append(fs, map[string]interface{}{
"type": "Feature",
"id": h.Id,
"properties": map[string]string{
"name": h.Name,
"phone_number": h.PhoneNumber,
},
"geometry": map[string]interface{}{
"type": "Point",
"coordinates": []float32{
h.Address.Lon,
h.Address.Lat,
},
},
})
}
return map[string]interface{}{
"type": "FeatureCollection",
"features": fs,
}
}