@@ -18,9 +18,9 @@ import (
18
18
"context"
19
19
"encoding/json"
20
20
"fmt"
21
- "html/template"
22
21
"math/rand"
23
22
"net/http"
23
+ "os"
24
24
"strconv"
25
25
"time"
26
26
@@ -32,12 +32,56 @@ import (
32
32
"github.com/GoogleCloudPlatform/microservices-demo/src/frontend/money"
33
33
)
34
34
35
- var (
36
- templates = template .Must (template .New ("" ).
37
- Funcs (template.FuncMap {
38
- "renderMoney" : renderMoney ,
39
- }).ParseGlob ("templates/*.html" ))
40
- )
35
+ func (fe * frontendServer ) homeHandler (w http.ResponseWriter , r * http.Request ) {
36
+ log := r .Context ().Value (ctxKeyLog {}).(logrus.FieldLogger )
37
+ log .WithField ("currency" , currentCurrency (r )).Info ("home" )
38
+ currencies , err := fe .getCurrencies (r .Context ())
39
+ if err != nil {
40
+ renderHTTPError (log , r , w , errors .Wrap (err , "could not retrieve currencies" ), http .StatusInternalServerError )
41
+ return
42
+ }
43
+ products , err := fe .getProducts (r .Context ())
44
+ if err != nil {
45
+ renderHTTPError (log , r , w , errors .Wrap (err , "could not retrieve products" ), http .StatusInternalServerError )
46
+ return
47
+ }
48
+ cart , err := fe .getCart (r .Context (), sessionID (r ))
49
+ if err != nil {
50
+ renderHTTPError (log , r , w , errors .Wrap (err , "could not retrieve cart" ), http .StatusInternalServerError )
51
+ return
52
+ }
53
+
54
+ type productView struct {
55
+ Item * pb.Product
56
+ Price * pb.Money
57
+ }
58
+ ps := make ([]productView , len (products ))
59
+ for i , p := range products {
60
+ price , err := fe .convertCurrency (r .Context (), p .GetPriceUsd (), currentCurrency (r ))
61
+ if err != nil {
62
+ renderHTTPError (log , r , w , errors .Wrapf (err , "failed to do currency conversion for product %s" , p .GetId ()), http .StatusInternalServerError )
63
+ return
64
+ }
65
+ ps [i ] = productView {p , price }
66
+ }
67
+
68
+ finalData := map [string ]interface {}{
69
+ "session_id" : sessionID (r ),
70
+ "request_id" : r .Context ().Value (ctxKeyRequestID {}),
71
+ "user_currency" : currentCurrency (r ),
72
+ "currencies" : currencies ,
73
+ "products" : ps ,
74
+ "cart_size" : len (cart ),
75
+ "banner_color" : os .Getenv ("BANNER_COLOR" ), // illustrates canary deployments
76
+ "ad" : fe .chooseAd (r .Context (), []string {}, log ),
77
+ }
78
+
79
+ b , err := json .Marshal (finalData )
80
+ if err != nil {
81
+ log .Println (err )
82
+ }
83
+ w .Write (b )
84
+ }
41
85
42
86
func (fe * frontendServer ) productHandler (w http.ResponseWriter , r * http.Request ) {
43
87
log := r .Context ().Value (ctxKeyLog {}).(logrus.FieldLogger )
@@ -68,9 +112,9 @@ func (fe *frontendServer) productHandler(w http.ResponseWriter, r *http.Request)
68
112
69
113
b , err := json .Marshal (product )
70
114
if err != nil {
71
- log .Println (b )
115
+ log .Println (err )
72
116
}
73
-
117
+ w . Write ( b )
74
118
}
75
119
76
120
func (fe * frontendServer ) addToCartHandler (w http.ResponseWriter , r * http.Request ) {
@@ -165,18 +209,18 @@ func (fe *frontendServer) viewCartHandler(w http.ResponseWriter, r *http.Request
165
209
166
210
year := time .Now ().Year ()
167
211
168
- var finalCart = map [string ]interface {}{
169
- "session_id" : sessionID (r ),
170
- "request_id" : r .Context ().Value (ctxKeyRequestID {}),
171
- "user_currency" : currentCurrency (r ),
172
- "currencies" : currencies ,
173
- "recommendations" : recommendations ,
174
- "cart_size" : len (cart ),
175
- "shipping_cost" : shippingCost ,
176
- "total_cost" : totalPrice ,
177
- "items" : items ,
178
- "expiration_years" : []int {year , year + 1 , year + 2 , year + 3 , year + 4 },
179
- }
212
+ var finalCart = map [string ]interface {}{
213
+ "session_id" : sessionID (r ),
214
+ "request_id" : r .Context ().Value (ctxKeyRequestID {}),
215
+ "user_currency" : currentCurrency (r ),
216
+ "currencies" : currencies ,
217
+ "recommendations" : recommendations ,
218
+ "cart_size" : len (cart ),
219
+ "shipping_cost" : shippingCost ,
220
+ "total_cost" : totalPrice ,
221
+ "items" : items ,
222
+ "expiration_years" : []int {year , year + 1 , year + 2 , year + 3 , year + 4 },
223
+ }
180
224
181
225
b , err := json .Marshal (finalCart )
182
226
if err != nil {
@@ -232,15 +276,18 @@ func (fe *frontendServer) placeOrderHandler(w http.ResponseWriter, r *http.Reque
232
276
totalPaid = money .Must (money .Sum (totalPaid , * v .GetCost ()))
233
277
}
234
278
235
- if err := templates . ExecuteTemplate ( w , "order" , map [string ]interface {}{
279
+ var finalData = map [string ]interface {}{
236
280
"session_id" : sessionID (r ),
237
281
"request_id" : r .Context ().Value (ctxKeyRequestID {}),
238
282
"user_currency" : currentCurrency (r ),
239
283
"order" : order .GetOrder (),
240
284
"total_paid" : & totalPaid ,
241
285
"recommendations" : recommendations ,
242
- }); err != nil {
243
- log .Println (err )
286
+ }
287
+
288
+ b , err := json .Marshal (finalData )
289
+ if err != nil {
290
+ log .Println (b )
244
291
}
245
292
}
246
293
@@ -252,7 +299,7 @@ func (fe *frontendServer) logoutHandler(w http.ResponseWriter, r *http.Request)
252
299
c .MaxAge = - 1
253
300
http .SetCookie (w , c )
254
301
}
255
- w .Header ().Set ("Location" , "/" )
302
+ w .Header ().Set ("Location" , "/api/v1 " )
256
303
w .WriteHeader (http .StatusFound )
257
304
}
258
305
@@ -271,7 +318,7 @@ func (fe *frontendServer) setCurrencyHandler(w http.ResponseWriter, r *http.Requ
271
318
}
272
319
referer := r .Header .Get ("referer" )
273
320
if referer == "" {
274
- referer = "/"
321
+ referer = "/api/v1 "
275
322
}
276
323
w .Header ().Set ("Location" , referer )
277
324
w .WriteHeader (http .StatusFound )
@@ -293,12 +340,19 @@ func renderHTTPError(log logrus.FieldLogger, r *http.Request, w http.ResponseWri
293
340
errMsg := fmt .Sprintf ("%+v" , err )
294
341
295
342
w .WriteHeader (code )
296
- templates . ExecuteTemplate ( w , "error" , map [string ]interface {}{
343
+ var finalData = map [string ]interface {}{
297
344
"session_id" : sessionID (r ),
298
345
"request_id" : r .Context ().Value (ctxKeyRequestID {}),
299
346
"error" : errMsg ,
300
347
"status_code" : code ,
301
- "status" : http .StatusText (code )})
348
+ "status" : http .StatusText (code ),
349
+ }
350
+
351
+ b , err := json .Marshal (finalData )
352
+ if err != nil {
353
+ w .Write (b )
354
+ }
355
+
302
356
}
303
357
304
358
func currentCurrency (r * http.Request ) string {
0 commit comments