Пример #1
0
func (this *LisaService) distanceHandler(w http.ResponseWriter, r *http.Request) {

	w.Header().Add("content-type", "application/json")

	decoder := json.NewDecoder(r.Body)
	req := &DistanceRequest{}
	if err := decoder.Decode(req); err != nil {
		log.Print(err)
		w.WriteHeader(400)
		fmt.Fprintf(w, errBody(err))
		return
	}

	if err := req.validate(); err != nil {
		w.WriteHeader(400)
		fmt.Fprintf(w, errBody(err))
		return
	}

	calc, err := geo.NewGeoCalculator(geo.AtoUnits(req.Units))
	if err != nil {
		log.Printf("Error: %v", err)
		fmt.Fprintf(w, errBody(err))
		return
	}

	distances := calculateDistances(calc, req)

	enc := json.NewEncoder(w)

	if err := enc.Encode(distances); err != nil {
		log.Printf("Error: %v", err)
		fmt.Fprintf(w, errBody(err))
	}
}
Пример #2
0
func TestCalculateDistances(t *testing.T) {

	calc, err := geo.NewGeoCalculator(geo.KM)
	if err != nil {
		t.Fatal(err)
	}

	fromTo := FromTo{
		From: Coords{
			Lat: 0.0,
			Lon: 0.0,
		},
		To: Coords{
			Lat: 0.0,
			Lon: 0.0,
		},
	}

	units := "KM"

	req := &DistanceRequest{
		FromTos: []FromTo{
			fromTo, fromTo, fromTo, fromTo, fromTo,
		},
		Units: units,
	}

	resp := calculateDistances(calc, req)

	for _, distance := range resp.Distances {
		if distance.Error != "" {
			t.Errorf("Distance response contained unexpected error: %v", err)
		}
		if distance.Distance != 0 {
			t.Errorf("Expected 0, got %f", distance.Distance)
		}
		if distance.FromTo != fromTo {
			t.Errorf("Expected %v, got %v", fromTo, distance.FromTo)
		}
		if distance.Units != units {
			t.Errorf("Expected %v, got %v", units, distance.Units)
		}
	}
}