fix: return empty response if no destinations are provided
This commit is contained in:
@@ -4,12 +4,13 @@ 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 {
|
||||
@@ -78,7 +79,16 @@ 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,
|
||||
@@ -111,13 +121,17 @@ func handleDistanceMatrixRequest(w http.ResponseWriter, r *http.Request, client
|
||||
res.Origins = append(res.Origins, origin{origins[i], dests})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func handleAddressRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
|
||||
|
||||
Reference in New Issue
Block a user