frontend: connect to productcatalog, currencysvc
Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
parent
f734e96733
commit
11ba676828
1 changed files with 23 additions and 5 deletions
|
@ -34,11 +34,11 @@ type frontendServer struct {
|
||||||
productCatalogSvcAddr string
|
productCatalogSvcAddr string
|
||||||
productCatalogSvcConn *grpc.ClientConn
|
productCatalogSvcConn *grpc.ClientConn
|
||||||
|
|
||||||
cartSvcAddr string
|
|
||||||
cartSvcConn *grpc.ClientConn
|
|
||||||
|
|
||||||
currencySvcAddr string
|
currencySvcAddr string
|
||||||
currencySvcConn *grpc.ClientConn
|
currencySvcConn *grpc.ClientConn
|
||||||
|
|
||||||
|
cartSvcAddr string
|
||||||
|
cartSvcConn *grpc.ClientConn
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
@ -49,15 +49,19 @@ func main() {
|
||||||
srvPort = os.Getenv("PORT")
|
srvPort = os.Getenv("PORT")
|
||||||
}
|
}
|
||||||
svc := new(frontendServer)
|
svc := new(frontendServer)
|
||||||
// mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR")
|
mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR")
|
||||||
// mustMapEnv(&svc.cartSvcAddr, "CART_SERVICE_ADDR")
|
|
||||||
mustMapEnv(&svc.currencySvcAddr, "CURRENCY_SERVICE_ADDR")
|
mustMapEnv(&svc.currencySvcAddr, "CURRENCY_SERVICE_ADDR")
|
||||||
|
// mustMapEnv(&svc.cartSvcAddr, "CART_SERVICE_ADDR")
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
svc.currencySvcConn, err = grpc.DialContext(ctx, svc.currencySvcAddr, grpc.WithInsecure())
|
svc.currencySvcConn, err = grpc.DialContext(ctx, svc.currencySvcAddr, grpc.WithInsecure())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to connect currency service: %+v", err)
|
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 := mux.NewRouter()
|
||||||
r.HandleFunc("/", svc.ensureSessionID(svc.homeHandler)).
|
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{}))
|
log.Printf("[home] session_id=%+v", r.Context().Value(ctxKeySessionID{}))
|
||||||
currencies, err := fe.getCurrencies(r.Context())
|
currencies, err := fe.getCurrencies(r.Context())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println(err) // TODO(ahmetb) use structured logging
|
||||||
w.WriteHeader(http.StatusInternalServerError)
|
w.WriteHeader(http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
log.Printf("currencies: %+v", currencies)
|
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) {
|
func (fe *frontendServer) logoutHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Printf("[home] session_id=%+v", r.Context().Value(ctxKeySessionID{}))
|
log.Printf("[home] session_id=%+v", r.Context().Value(ctxKeySessionID{}))
|
||||||
|
|
||||||
|
// clear all cookies
|
||||||
for _, c := range r.Cookies() {
|
for _, c := range r.Cookies() {
|
||||||
c.MaxAge = -1
|
c.MaxAge = -1
|
||||||
http.SetCookie(w, c)
|
http.SetCookie(w, c)
|
||||||
|
@ -143,6 +156,11 @@ func (fe *frontendServer) getCurrencies(ctx context.Context) ([]string, error) {
|
||||||
return out, nil
|
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) {
|
func mustMapEnv(target *string, envKey string) {
|
||||||
v := os.Getenv(envKey)
|
v := os.Getenv(envKey)
|
||||||
if v == "" {
|
if v == "" {
|
||||||
|
|
Loading…
Reference in a new issue