Exemplo n.º 1
0
func (api api) requestHandler(w http.ResponseWriter, r *http.Request) {
	t := trace.NewTracer()
	t.In("www", "api.v1")
	defer t.Out("api.v1", "www", time.Now())

	// context and metadata
	md := metadata.Pairs("traceID", t.TraceID, "from", serverName)
	ctx := context.Background()
	ctx = metadata.NewContext(ctx, md)

	// parse token from Authorization header
	authToken, err := auth_token.Parse(r.Header.Get("Authorization"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}

	// verify auth token
	_, err = api.VerifyToken(ctx, &auth.Args{
		From:      serverName,
		AuthToken: authToken,
	})
	if err != nil {
		http.Error(w, "Unauthorized", http.StatusForbidden)
		return
	}

	// read and validate in/out arguments
	inDate := r.URL.Query().Get("inDate")
	outDate := r.URL.Query().Get("outDate")
	if inDate == "" || outDate == "" {
		http.Error(w, "Please specify inDate / outDate", http.StatusBadRequest)
		return
	}

	// get hotels within geo box
	reply, err := api.BoundedBox(ctx, &geo.Rectangle{
		Lo: &geo.Point{Latitude: 400000000, Longitude: -750000000},
		Hi: &geo.Point{Latitude: 420000000, Longitude: -730000000},
	})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	inventory := &inventory{}
	for i := 0; i < 2; i++ {
		select {
		case profileReply := <-api.getHotels(ctx, reply.HotelIds):
			if err := profileReply.err; err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			inventory.Hotels = profileReply.hotels
		case rateReply := <-api.getRatePlans(ctx, reply.HotelIds, inDate, outDate):
			if err := rateReply.err; err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
				return
			}
			inventory.RatePlans = rateReply.ratePlans
		}
	}
	encoder := json.NewEncoder(w)
	if err = encoder.Encode(inventory); err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
	}
}
Exemplo n.º 2
0
func (api api) requestHandler(w http.ResponseWriter, r *http.Request) {
	t := trace.NewTracer()
	t.In("www", "api.v1")
	defer t.Out("api.v1", "www", time.Now())

	// context and metadata
	md := metadata.Pairs("traceID", t.TraceID, "from", serverName)
	ctx := context.Background()
	ctx = metadata.NewContext(ctx, md)

	// parse token from Authorization header
	authToken, err := auth_token.Parse(r.Header.Get("Authorization"))
	if err != nil {
		http.Error(w, err.Error(), http.StatusForbidden)
		return
	}

	// verify auth token
	err = api.authClient.VerifyToken(ctx, serverName, authToken)
	if err != nil {
		http.Error(w, "Unauthorized", http.StatusForbidden)
		return
	}

	// read and validate in/out arguments
	inDate := r.URL.Query().Get("inDate")
	outDate := r.URL.Query().Get("outDate")
	if inDate == "" || outDate == "" {
		http.Error(w, "Please specify inDate / outDate", http.StatusBadRequest)
		return
	}

	// get hotels within geo box
	hotelIDs, err := api.geoClient.HotelsWithinBoundedBox(ctx, 100, 100)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	profileCh := api.getHotels(ctx, hotelIDs)
	rateCh := api.getRatePlans(ctx, hotelIDs, inDate, outDate)

	profileReply := <-profileCh
	if err := profileReply.err; err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	rateReply := <-rateCh
	if err := rateReply.err; err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	inventory := inventory{
		Hotels:    profileReply.hotels,
		RatePlans: rateReply.ratePlans,
	}

	body, err := json.Marshal(inventory)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Write(body)
}