checkoutservice: partial implementation

Signed-off-by: Ahmet Alp Balkan <ahmetb@google.com>
This commit is contained in:
Ahmet Alp Balkan 2018-06-21 12:15:53 -07:00
parent 95ed2f3b64
commit f06818db0c
2 changed files with 43 additions and 7 deletions

View file

@ -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

View file

@ -7,6 +7,9 @@ import (
"net" "net"
"os" "os"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "./genproto" pb "./genproto"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@ -31,21 +34,22 @@ func main() {
} }
svc := new(checkoutService) 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.shippingSvcAddr, "SHIPPING_SERVICE_ADDR")
mustMapEnv(&svc.emailSvcAddr, "EMAIL_SERVICE_ADDR") // mustMapEnv(&svc.productCatalogSvcAddr, "PRODUCT_CATALOG_SERVICE_ADDR")
mustMapEnv(&svc.paymentSvcAddr, "PAYMENT_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)) lis, err := net.Listen("tcp", fmt.Sprintf(":%s", port))
if err != nil { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
srv := grpc.NewServer() srv := grpc.NewServer()
pb.RegisterCheckoutServiceServer(srv, svc) 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)) 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) { 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) log.Printf("[CreateOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency)
resp := new(pb.CreateOrderResponse) 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 return resp, nil
} }