From 11ba676828a050317d779f1ca1b818765d3b470c Mon Sep 17 00:00:00 2001 From: Ahmet Alp Balkan Date: Sun, 24 Jun 2018 21:37:16 -0700 Subject: [PATCH] frontend: connect to productcatalog, currencysvc Signed-off-by: Ahmet Alp Balkan --- src/frontend/main.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/frontend/main.go b/src/frontend/main.go index 1cf591e..6e26111 100644 --- a/src/frontend/main.go +++ b/src/frontend/main.go @@ -34,11 +34,11 @@ type frontendServer struct { productCatalogSvcAddr string productCatalogSvcConn *grpc.ClientConn - cartSvcAddr string - cartSvcConn *grpc.ClientConn - currencySvcAddr string currencySvcConn *grpc.ClientConn + + cartSvcAddr string + cartSvcConn *grpc.ClientConn } func main() { @@ -49,15 +49,19 @@ func main() { srvPort = os.Getenv("PORT") } svc := new(frontendServer) - // mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR") - // mustMapEnv(&svc.cartSvcAddr, "CART_SERVICE_ADDR") + mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR") mustMapEnv(&svc.currencySvcAddr, "CURRENCY_SERVICE_ADDR") + // mustMapEnv(&svc.cartSvcAddr, "CART_SERVICE_ADDR") var err error svc.currencySvcConn, err = grpc.DialContext(ctx, svc.currencySvcAddr, grpc.WithInsecure()) if err != nil { log.Fatalf("failed to connect currency service: %+v", err) } + svc.productCatalogSvcConn, err = grpc.DialContext(ctx, svc.productCatalogSvcAddr, grpc.WithInsecure()) + if err != nil { + log.Fatalf("failed to connect productcatalog service: %+v", err) + } r := mux.NewRouter() r.HandleFunc("/", svc.ensureSessionID(svc.homeHandler)). @@ -99,14 +103,23 @@ func (fe *frontendServer) homeHandler(w http.ResponseWriter, r *http.Request) { log.Printf("[home] session_id=%+v", r.Context().Value(ctxKeySessionID{})) currencies, err := fe.getCurrencies(r.Context()) if err != nil { + log.Println(err) // TODO(ahmetb) use structured logging w.WriteHeader(http.StatusInternalServerError) return } log.Printf("currencies: %+v", currencies) + products, err := fe.getProducts(r.Context()) + if err != nil { + log.Println(err) + w.WriteHeader(http.StatusInternalServerError) + } + log.Printf("# products: %d", len(products)) } 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) @@ -143,6 +156,11 @@ func (fe *frontendServer) getCurrencies(ctx context.Context) ([]string, error) { return out, nil } +func (fe *frontendServer) getProducts(ctx context.Context) ([]*pb.Product, error) { + resp, err := pb.NewProductCatalogServiceClient(fe.productCatalogSvcConn).ListProducts(ctx, &pb.Empty{}) + return resp.GetProducts(), err +} + func mustMapEnv(target *string, envKey string) { v := os.Getenv(envKey) if v == "" {