Пример #1
0
func main() {
	addr := flag.String("listen", "0.0.0.0:9003", "Endpoint address")
	redisEndpoint := flag.String("redis", "127.0.0.1:6379", "Redis endpoint")
	services.ParseBalanceEndpointsFlags()
	flag.Parse()
	services.ParseLoadBalanceConfig()
	services.EnableResponseLogging()
	golib.ConfigureOpenFilesLimit()

	redisClient, err := services.ConnectRedis(*redisEndpoint)
	if err != nil {
		log.Fatalln(err)
	}

	if err := services.RegisterLockScripts(redisClient); err != nil {
		log.Fatalln("Failed to register redis scripts", err)
	}

	catalog := &Catalog{
		redis:          redisClient,
		redisLockValue: services.EndpointLockValue(*addr),
	}
	if err := fillDefaultCatalog(catalog); err != nil {
		log.Fatalln("Error filling default catalog:", err)
	}
	go catalog.RefillItems(refill_timeout, map[string]uint64{
		"DVD":       500,
		"Toaster":   100,
		"Laptop":    50,
		"TV":        10,
		"Spaceship": 1,
	})

	mux := mux.NewRouter()
	mux.HandleFunc("/items", catalog.show_items).Methods("GET")
	mux.HandleFunc("/item/{name}", catalog.show_item).Methods("GET")
	mux.HandleFunc("/item/{name}/ship", catalog.ship_item).Methods("POST").MatcherFunc(services.MatchFormKeys("user", "qty", "ts"))
	mux.HandleFunc("/shipment/{id}", catalog.show_shipment).Methods("GET")
	mux.HandleFunc("/shipment/{id}/commit", catalog.commit_shipment).Methods("POST")
	mux.HandleFunc("/shipment/{id}/cancel", catalog.cancel_shipment).Methods("POST")
	mux.HandleFunc("/shipment/{id}/deliver", catalog.deliver_shipment).Methods("POST")
	services.L.Warnf("Running on " + *addr)
	if err := http.ListenAndServe(*addr, mux); err != nil {
		log.Fatal(err)
	}
}
Пример #2
0
func main() {
	addr := flag.String("listen", "0.0.0.0:9004", "Endpoint address")
	redisEndpoint := flag.String("redis", "127.0.0.1:6379", "Redis endpoint")
	paymentEndpoint := flag.String("payment", "localhost:9002", "Endpoint for payment service")
	catalogEndpoint := flag.String("catalog", "localhost:9003", "Endpoint for catalog service")
	services.ParseBalanceEndpointsFlags()
	flag.Parse()
	services.ParseLoadBalanceConfig()
	services.EnableResponseLogging()
	golib.ConfigureOpenFilesLimit()

	redisClient, err := services.ConnectRedis(*redisEndpoint)
	if err != nil {
		log.Fatalln(err)
	}

	if err := services.RegisterLockScripts(redisClient); err != nil {
		log.Fatalln("Failed to register redis scripts", err)
	}

	shop := &Shop{
		redis:           redisClient,
		redisLockValue:  services.EndpointLockValue(*addr),
		catalogEndpoint: *catalogEndpoint,
		paymentEndpoint: *paymentEndpoint,
	}
	launchOrderProcessing(shop)

	mux := mux.NewRouter()
	mux.HandleFunc("/shop", shop.show_items).Methods("GET")
	mux.HandleFunc("/order", shop.order_item).Methods("POST").MatcherFunc(services.MatchFormKeys("user", "item", "qty"))
	mux.HandleFunc("/order/{order}", shop.get_order).Methods("GET")
	mux.HandleFunc("/orders/{user}", shop.show_orders).Methods("GET")

	services.L.Warnf("Running on " + *addr)
	if err := http.ListenAndServe(*addr, mux); err != nil {
		log.Fatal(err)
	}
}
Пример #3
0
func main() {
	addr := flag.String("listen", "0.0.0.0:9002", "Endpoint address")
	redisEndpoint := flag.String("redis", "127.0.0.1:6379", "Redis endpoint")
	bankEndpoint := flag.String("bank", "localhost:9001", "Endpoint for bank service")
	services.ParseBalanceEndpointsFlags()
	flag.Parse()
	services.ParseLoadBalanceConfig()
	services.EnableResponseLogging()
	golib.ConfigureOpenFilesLimit()

	bank := bankApi.NewHttpBank(*bankEndpoint)
	redisClient, err := services.ConnectRedis(*redisEndpoint)
	if err != nil {
		log.Fatalln(err)
	}

	if err := services.RegisterLockScripts(redisClient); err != nil {
		log.Fatalln("Failed to register redis scripts", err)
	}

	payments := &Payments{
		bank:           bank,
		redis:          redisClient,
		accountName:    "store",
		redisLockValue: services.EndpointLockValue(*addr),
	}
	mux := mux.NewRouter()
	mux.HandleFunc("/payment", payments.new_payment).Methods("POST").MatcherFunc(services.MatchFormKeys("user", "value", "ts"))
	mux.HandleFunc("/payment/{id}", payments.show_payment).Methods("GET")
	mux.HandleFunc("/payment/{id}/commit", payments.commit_payment).Methods("POST")
	mux.HandleFunc("/payment/{id}/cancel", payments.cancel_payment).Methods("POST")

	services.L.Warnf("Running on " + *addr)
	if err := http.ListenAndServe(*addr, mux); err != nil {
		log.Fatal(err)
	}
}