fix: return empty response if no destinations are provided
This commit is contained in:
@@ -4,12 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/caarlos0/env"
|
|
||||||
"googlemaps.github.io/maps"
|
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/caarlos0/env"
|
||||||
|
"googlemaps.github.io/maps"
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
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) {
|
func handleDistanceMatrixRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
|
||||||
origins := strings.Split(r.URL.Query().Get("origins"), "|")
|
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{
|
req := &maps.DistanceMatrixRequest{
|
||||||
Origins: origins,
|
Origins: origins,
|
||||||
@@ -111,6 +121,11 @@ func handleDistanceMatrixRequest(w http.ResponseWriter, r *http.Request, client
|
|||||||
res.Origins = append(res.Origins, origin{origins[i], dests})
|
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 {
|
if response, err := json.Marshal(res); err != nil {
|
||||||
log.Fatalf("fatal error: %s", err)
|
log.Fatalf("fatal error: %s", err)
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
@@ -118,7 +133,6 @@ func handleDistanceMatrixRequest(w http.ResponseWriter, r *http.Request, client
|
|||||||
_, _ = w.Write(response)
|
_, _ = w.Write(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
func handleAddressRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
|
func handleAddressRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
|
||||||
p := strings.Split(r.URL.Path[len("/address/"):], ",")
|
p := strings.Split(r.URL.Path[len("/address/"):], ",")
|
||||||
|
|||||||
Reference in New Issue
Block a user