fix: return empty response if no destinations are provided

This commit is contained in:
2022-08-27 11:10:28 +02:00
parent 170877cc4e
commit 5dedddafe2
+41 -27
View File
@@ -4,21 +4,22 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/caarlos0/env"
"googlemaps.github.io/maps"
"log"
"net/http"
"strconv"
"strings"
"github.com/caarlos0/env"
"googlemaps.github.io/maps"
)
type config struct {
MapsApiKey string `env:"MAPS_API_KEY"`
Port int `env:"PORT" envDefault:"80"`
Port int `env:"PORT" envDefault:"80"`
}
type location struct {
Lat float64 `json:"latitude"`
Lat float64 `json:"latitude"`
Long float64 `json:"longitude"`
}
@@ -27,23 +28,23 @@ type address struct {
}
type distance struct {
Text string `json:"text"`
Value int `json:"value"`
Text string `json:"text"`
Value int `json:"value"`
}
type duration struct {
Text string `json:"text"`
Text string `json:"text"`
Value float64 `json:"value"`
}
type destination struct {
Destination string `json:"destination"`
Distance distance `json:"distance"`
Duration duration `json:"duration"`
Destination string `json:"destination"`
Distance distance `json:"distance"`
Duration duration `json:"duration"`
}
type origin struct {
Origin string `json:"origin"`
Origin string `json:"origin"`
Destinations []destination `json:"destinations"`
}
@@ -78,13 +79,22 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, *maps.Client), clie
func handleDistanceMatrixRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
origins := strings.Split(r.URL.Query().Get("origins"), "|")
destinations := strings.Split(r.URL.Query().Get("destinations"), "|")
destinationsString := r.URL.Query().Get("destinations")
if destinationsString == "" {
x := make([]origin, len(origins))
for i, o := range origins {
x[i] = origin{Origin: o}
}
writeResponse(distanceResponse{Origins: x}, w)
return
}
destinations := strings.Split(destinationsString, "|")
req := &maps.DistanceMatrixRequest{
Origins: origins,
Origins: origins,
Destinations: destinations,
Language: "sv",
Units: maps.UnitsMetric,
Language: "sv",
Units: maps.UnitsMetric,
}
if result, err := client.DistanceMatrix(context.Background(), req); err != nil {
@@ -111,12 +121,16 @@ func handleDistanceMatrixRequest(w http.ResponseWriter, r *http.Request, client
res.Origins = append(res.Origins, origin{origins[i], dests})
}
if response, err := json.Marshal(res); err != nil {
log.Fatalf("fatal error: %s", err)
w.WriteHeader(404)
} else {
_, _ = w.Write(response)
}
writeResponse(res, w)
}
}
func writeResponse(res distanceResponse, w http.ResponseWriter) {
if response, err := json.Marshal(res); err != nil {
log.Fatalf("fatal error: %s", err)
w.WriteHeader(404)
} else {
_, _ = w.Write(response)
}
}
@@ -133,7 +147,7 @@ func handleAddressRequest(w http.ResponseWriter, r *http.Request, client *maps.C
} else {
if len(result) > 0 {
l := address{
Address: result[0].FormattedAddress,
Address: result[0].FormattedAddress,
}
if response, err := json.Marshal(l); err != nil {
log.Fatalf("fatal error: %s", err)
@@ -147,15 +161,15 @@ func handleAddressRequest(w http.ResponseWriter, r *http.Request, client *maps.C
func handleLatLongRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
req := &maps.GeocodingRequest{
Address: r.URL.Path[len("/latlong/"):],
Address: r.URL.Path[len("/latlong/"):],
}
if result, err := client.Geocode(context.Background(), req); err != nil {
w.WriteHeader(400)
} else {
if len(result) > 0 {
l := location{
Lat: result[0].Geometry.Location.Lat,
Long: result[0].Geometry.Location.Lng,
Lat: result[0].Geometry.Location.Lat,
Long: result[0].Geometry.Location.Lng,
}
if response, err := json.Marshal(l); err != nil {
w.WriteHeader(500)
@@ -163,7 +177,7 @@ func handleLatLongRequest(w http.ResponseWriter, r *http.Request, client *maps.C
_, _ = w.Write(response)
}
} else {
w.WriteHeader(404)
}
w.WriteHeader(404)
}
}
}