From 7063092393dbfecb1e8da7633e0a83281f6951fd Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Mon, 25 Jun 2018 11:17:51 -0700 Subject: [PATCH] frontend: more organization Signed-off-by: Ahmet Alp Balkan --- src/frontend/handlers.go | 30 +++++++++++++++++++++++------- src/frontend/main.go | 1 + src/frontend/rpc.go | 14 ++++++++++++++ src/frontend/templates/home.html | 4 +++- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/frontend/handlers.go b/src/frontend/handlers.go index 71f6edf..1fbed2a 100644 --- a/src/frontend/handlers.go +++ b/src/frontend/handlers.go @@ -2,11 +2,14 @@ package main import ( "context" + "fmt" "html/template" "log" "net/http" "github.com/google/uuid" + + pb "frontend/genproto" ) var ( @@ -36,8 +39,7 @@ func ensureSessionID(next http.HandlerFunc) http.HandlerFunc { MaxAge: cookieMaxAge, }) } else if err != nil { - log.Printf("unrecognized cookie error: %+v", err) - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, fmt.Sprintf("unrecognized cookie error: %+v", err), http.StatusInternalServerError) return } else { sessionID = c.Value @@ -59,16 +61,32 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) { log.Printf("currencies: %+v", currencies) products, err := fe.getProducts(r.Context()) if err != nil { - log.Println(err) - w.WriteHeader(http.StatusInternalServerError) + http.Error(w, err.Error(), http.StatusInternalServerError) return } log.Printf("# products: %d", len(products)) + type productView struct { + Item *pb.Product + Price *pb.Money + } + ps := make([]productView, len(products)) + for i, p := range products { + price, err := fe.convertCurrency(r.Context(), &pb.Money{ + Amount: p.PriceUsd, + CurrencyCode: defaultCurrency, + }, currentCurrency(r)) + if err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + ps[i] = productView{p, price} + } + if err := homeTemplate.Execute(w, map[string]interface{}{ "user_currency": currentCurrency(r), "currencies": currencies, - "products": products, + "products": ps, }); err != nil { log.Println(err) } @@ -76,8 +94,6 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) { func (fe *frontendServer) logoutHandler(w http.ResponseWriter, r *http.Request) { log.Printf("[home] session_id=%+v", r.Context().Value(ctxKeySessionID{})) - - // clear all cookies for _, c := range r.Cookies() { c.MaxAge = -1 http.SetCookie(w, c) diff --git a/src/frontend/main.go b/src/frontend/main.go index 89ff4ac..d25128b 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -40,6 +40,7 @@ type frontendServer struct { func main() { ctx := context.Background() + log.SetFlags(log.Lshortfile | log.Ltime) srvPort := port if os.Getenv("PORT") != "" { diff --git a/src/frontend/rpc.go b/src/frontend/rpc.go index 20031b2..5d31b1b 100644 --- a/src/frontend/rpc.go +++ b/src/frontend/rpc.go @@ -6,6 +6,10 @@ import ( pb "frontend/genproto" ) +const ( + avoidNoopCurrencyConversionRPC = false +) + func (fe *frontendServer) getCurrencies(ctx context.Context) ([]string, error) { currs, err := pb.NewCurrencyServiceClient(fe.currencySvcConn). GetSupportedCurrencies(ctx, &pb.Empty{}) @@ -26,3 +30,13 @@ func (fe *frontendServer) getProducts(ctx context.Context) ([]*pb.Product, error ListProducts(ctx, &pb.Empty{}) return resp.GetProducts(), err } + +func (fe *frontendServer) convertCurrency(ctx context.Context, money *pb.Money, currency string) (*pb.Money, error) { + if avoidNoopCurrencyConversionRPC && money.GetCurrencyCode() == currency { + return money, nil + } + return pb.NewCurrencyServiceClient(fe.currencySvcConn). + Convert(ctx, &pb.CurrencyConversionRequest{ + From: money, + ToCode: currency}) +} diff --git a/src/frontend/templates/home.html b/src/frontend/templates/home.html index 4917d2d..2cb062c 100644 --- a/src/frontend/templates/home.html +++ b/src/frontend/templates/home.html @@ -17,7 +17,9 @@ -

{{ len $.products }} products

+ {{range $.products }} +

{{.Item.Name}} -- <{{.Price}}>

+ {{end}}