Esempio n. 1
0
// Status handles shoe status.
func (srv *server) Status(w http.ResponseWriter, r *http.Request) {
	// validate path
	path := r.URL.Path
	log.Println("status path: ", path)
	validPath, err := normalizePath(path, statusHandlerPrefix)
	if err != nil {
		http.Error(w, "invalid path", http.StatusBadRequest)
		log.Printf("invalid path: %q\n", path)
		return
	}

	// check shoe id
	shoeID := parseShoeID(validPath)
	// cancel request for invalid shoe id
	if shoeID == 0 {
		http.Error(w, "invalid shoe ID", http.StatusBadRequest)
		log.Printf("invalid shoe ID: %q\n", path)
		return
	}

	c := pb.NewShoeClient(srv.grpcConn)

	status, err := c.Status(context.Background(), &pb.ShoeID{Id: shoeID})
	if err != nil {
		http.Error(w, "status error", http.StatusBadRequest)
		log.Printf("status error: %v", err)
		return
	}
	// show status
	msg := fmt.Sprintf("shoe_id: %d shoe_status: %t", shoeID, status.Done)
	log.Print(msg)
	if _, err := w.Write([]byte(msg)); err != nil {
		log.Print("w.Write failed.")
	}
}
Esempio n. 2
0
// Make handles make shoe request.
func (srv *server) Make(w http.ResponseWriter, r *http.Request) {
	// validate path
	path := r.URL.Path
	log.Println("path: ", path)
	validPath, err := normalizePath(path, makeHandlerPrefix)
	if err != nil {
		http.Error(w, "invalid path", http.StatusBadRequest)
		log.Printf("invalid path: %q\n", path)
		return
	}

	// check shoe size
	shoeSize := parseShoeSize(validPath)
	// cancel request for invalid shoe size
	if shoeSize == 0 {
		http.Error(w, "invalid shoe size", http.StatusBadRequest)
		log.Printf("invalid shoe size: %q\n", path)
		return
	}

	c := pb.NewShoeClient(srv.grpcConn)

	shoeID, err := c.Make(context.Background(), &pb.ShoeSize{Value: shoeSize})
	if err != nil {
		http.Error(w, "make error", http.StatusBadRequest)
		log.Printf("make error: %v", err)
		return
	}
	// show id
	log.Printf("shoe_id: %d", shoeID.Id)
	msg := fmt.Sprintf("shoe_id: %d", shoeID.Id)
	if _, err := w.Write([]byte(msg)); err != nil {
		log.Print("w.Write failed.")
	}
}