diff --git a/src/checkoutservice/Dockerfile b/src/checkoutservice/Dockerfile new file mode 100644 index 0000000..62907c3 --- /dev/null +++ b/src/checkoutservice/Dockerfile @@ -0,0 +1,13 @@ +FROM golang:1.10-alpine as builder +RUN apk add --no-cache ca-certificates git +WORKDIR /src/microservices-demo/catalogservice +COPY . . +RUN go get -d ./... +RUN go build -v -o /catalogservice . + +FROM alpine as release +RUN apk add --no-cache \ + ca-certificates +COPY --from=builder /catalogservice /catalogservice +EXPOSE 5000 +ENTRYPOINT /catalogservice diff --git a/src/checkoutservice/main.go b/src/checkoutservice/main.go index 7225898..49b744f 100644 --- a/src/checkoutservice/main.go +++ b/src/checkoutservice/main.go @@ -7,6 +7,9 @@ import ( "net" "os" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + pb "./genproto" "google.golang.org/grpc" ) @@ -31,21 +34,22 @@ func main() { } svc := new(checkoutService) - mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR") - mustMapEnv(&svc.cartSvcAddr, "CART_SERVICE_ADDR") - mustMapEnv(&svc.currencySvcAddr, "CURRENCY_SERVICE_ADDR") mustMapEnv(&svc.shippingSvcAddr, "SHIPPING_SERVICE_ADDR") - mustMapEnv(&svc.emailSvcAddr, "EMAIL_SERVICE_ADDR") - mustMapEnv(&svc.paymentSvcAddr, "PAYMENT_SERVICE_ADDR") + // mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR") + // mustMapEnv(&svc.cartSvcAddr, "CART_SERVICE_ADDR") + // mustMapEnv(&svc.currencySvcAddr, "CURRENCY_SERVICE_ADDR") + // mustMapEnv(&svc.emailSvcAddr, "EMAIL_SERVICE_ADDR") + // mustMapEnv(&svc.paymentSvcAddr, "PAYMENT_SERVICE_ADDR") + + log.Printf("service config: %+v", svc) - log.Printf("starting to listen on tcp/%s", port) lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port)) if err != nil { log.Fatal(err) } srv := grpc.NewServer() pb.RegisterCheckoutServiceServer(srv, svc) - log.Println("starting to listen on tcp: %q", lis.Addr().String()) + log.Printf("starting to listen on tcp: %q", lis.Addr().String()) log.Fatal(srv.Serve(lis)) } @@ -60,6 +64,25 @@ func mustMapEnv(target *string, envKey string) { func (cs *checkoutService) CreateOrder(ctx context.Context, req *pb.CreateOrderRequest) (*pb.CreateOrderResponse, error) { log.Printf("[CreateOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency) resp := new(pb.CreateOrderResponse) + conn, err := grpc.Dial(cs.shippingSvcAddr, grpc.WithInsecure()) + if err != nil { + return nil, status.Errorf(codes.Unavailable, "could not connect shippping service: %+v", err) + } + defer conn.Close() + + shippingQuote, err := pb.NewShippingServiceClient(conn). + GetQuote(ctx, &pb.GetQuoteRequest{ + Address: req.Address, + Items: nil}) // TODO(ahmetb): query CartService for items + if err != nil { + return nil, status.Errorf(codes.Unavailable, "failed to get shipping quote: %+v", err) + } + resp.ShippingCost = &pb.Money{ + Amount: shippingQuote.GetCostUsd(), + CurrencyCode: "USD", // TOD(ahmetb) convert to req.UserCurrency + } + // TODO(ahmetb) calculate resp.OrderItem with req.UserCurrency + return resp, nil }