70 lines
1.8 KiB
Go
70 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
"net"
|
|
"os"
|
|
|
|
pb "./genproto"
|
|
"google.golang.org/grpc"
|
|
)
|
|
|
|
const (
|
|
listenPort = "5000"
|
|
)
|
|
|
|
type checkoutService struct {
|
|
productCatalogSvcAddr string
|
|
cartSvcAddr string
|
|
currencySvcAddr string
|
|
shippingSvcAddr string
|
|
emailSvcAddr string
|
|
paymentSvcAddr string
|
|
}
|
|
|
|
func main() {
|
|
port := listenPort
|
|
if os.Getenv("PORT") != "" {
|
|
port = os.Getenv("PORT")
|
|
}
|
|
|
|
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")
|
|
|
|
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.Fatal(srv.Serve(lis))
|
|
}
|
|
|
|
func mustMapEnv(target *string, envKey string) {
|
|
v := os.Getenv(envKey)
|
|
if v == "" {
|
|
panic(fmt.Sprintf("environment variable %q not set", envKey))
|
|
}
|
|
*target = v
|
|
}
|
|
|
|
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)
|
|
return resp, nil
|
|
}
|
|
|
|
func (cs *checkoutService) PlaceOrder(ctx context.Context, req *pb.PlaceOrderRequest) (*pb.PlaceOrderResponse, error) {
|
|
log.Printf("[PlaceOrder] user_id=%q user_currency=%q", req.UserId, req.UserCurrency)
|
|
resp := new(pb.PlaceOrderResponse)
|
|
return resp, nil
|
|
}
|