fix: return empty response if no destinations are provided
This commit is contained in:
@@ -4,21 +4,22 @@ 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 {
|
||||||
MapsApiKey string `env:"MAPS_API_KEY"`
|
MapsApiKey string `env:"MAPS_API_KEY"`
|
||||||
Port int `env:"PORT" envDefault:"80"`
|
Port int `env:"PORT" envDefault:"80"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type location struct {
|
type location struct {
|
||||||
Lat float64 `json:"latitude"`
|
Lat float64 `json:"latitude"`
|
||||||
Long float64 `json:"longitude"`
|
Long float64 `json:"longitude"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -27,23 +28,23 @@ type address struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type distance struct {
|
type distance struct {
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
Value int `json:"value"`
|
Value int `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type duration struct {
|
type duration struct {
|
||||||
Text string `json:"text"`
|
Text string `json:"text"`
|
||||||
Value float64 `json:"value"`
|
Value float64 `json:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type destination struct {
|
type destination struct {
|
||||||
Destination string `json:"destination"`
|
Destination string `json:"destination"`
|
||||||
Distance distance `json:"distance"`
|
Distance distance `json:"distance"`
|
||||||
Duration duration `json:"duration"`
|
Duration duration `json:"duration"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type origin struct {
|
type origin struct {
|
||||||
Origin string `json:"origin"`
|
Origin string `json:"origin"`
|
||||||
Destinations []destination `json:"destinations"`
|
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) {
|
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,
|
||||||
Destinations: destinations,
|
Destinations: destinations,
|
||||||
Language: "sv",
|
Language: "sv",
|
||||||
Units: maps.UnitsMetric,
|
Units: maps.UnitsMetric,
|
||||||
}
|
}
|
||||||
|
|
||||||
if result, err := client.DistanceMatrix(context.Background(), req); err != nil {
|
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})
|
res.Origins = append(res.Origins, origin{origins[i], dests})
|
||||||
}
|
}
|
||||||
|
|
||||||
if response, err := json.Marshal(res); err != nil {
|
writeResponse(res, w)
|
||||||
log.Fatalf("fatal error: %s", err)
|
}
|
||||||
w.WriteHeader(404)
|
}
|
||||||
} else {
|
|
||||||
_, _ = w.Write(response)
|
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 {
|
} else {
|
||||||
if len(result) > 0 {
|
if len(result) > 0 {
|
||||||
l := address{
|
l := address{
|
||||||
Address: result[0].FormattedAddress,
|
Address: result[0].FormattedAddress,
|
||||||
}
|
}
|
||||||
if response, err := json.Marshal(l); err != nil {
|
if response, err := json.Marshal(l); err != nil {
|
||||||
log.Fatalf("fatal error: %s", err)
|
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) {
|
func handleLatLongRequest(w http.ResponseWriter, r *http.Request, client *maps.Client) {
|
||||||
req := &maps.GeocodingRequest{
|
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 {
|
if result, err := client.Geocode(context.Background(), req); err != nil {
|
||||||
w.WriteHeader(400)
|
w.WriteHeader(400)
|
||||||
} else {
|
} else {
|
||||||
if len(result) > 0 {
|
if len(result) > 0 {
|
||||||
l := location{
|
l := location{
|
||||||
Lat: result[0].Geometry.Location.Lat,
|
Lat: result[0].Geometry.Location.Lat,
|
||||||
Long: result[0].Geometry.Location.Lng,
|
Long: result[0].Geometry.Location.Lng,
|
||||||
}
|
}
|
||||||
if response, err := json.Marshal(l); err != nil {
|
if response, err := json.Marshal(l); err != nil {
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
@@ -163,7 +177,7 @@ func handleLatLongRequest(w http.ResponseWriter, r *http.Request, client *maps.C
|
|||||||
_, _ = w.Write(response)
|
_, _ = w.Write(response)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
w.WriteHeader(404)
|
w.WriteHeader(404)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user