Add distance matrix
This commit is contained in:
+4
-5
@@ -1,11 +1,10 @@
|
|||||||
FROM gunosy/ci-go-glide as builder
|
FROM golang:1.11-stretch as builder
|
||||||
WORKDIR /go/src/gitlab.com/unboundsoftware/geo-service
|
WORKDIR /go/src/gitlab.com/unboundsoftware/dancefinder/geo-service
|
||||||
COPY glide.* ./
|
|
||||||
RUN glide install
|
|
||||||
COPY * ./
|
COPY * ./
|
||||||
|
RUN go get
|
||||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o geo-service .
|
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o geo-service .
|
||||||
|
|
||||||
FROM scratch
|
FROM scratch
|
||||||
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
|
||||||
COPY --from=builder /go/src/gitlab.com/unboundsoftware/geo-service/geo-service /
|
COPY --from=builder /go/src/gitlab.com/unboundsoftware/dancefinder/geo-service/geo-service /
|
||||||
CMD ["/geo-service"]
|
CMD ["/geo-service"]
|
||||||
|
|||||||
@@ -42,7 +42,24 @@ spec:
|
|||||||
memory: "100Mi"
|
memory: "100Mi"
|
||||||
imagePullPolicy: Always
|
imagePullPolicy: Always
|
||||||
image: registry.gitlab.com/unboundsoftware/dancefinder/geo-service:${COMMIT}
|
image: registry.gitlab.com/unboundsoftware/dancefinder/geo-service:${COMMIT}
|
||||||
|
ports:
|
||||||
|
- containerPort: 80
|
||||||
|
name: http
|
||||||
envFrom:
|
envFrom:
|
||||||
- secretRef:
|
- secretRef:
|
||||||
name: google-maps-api
|
name: google-maps-api
|
||||||
restartPolicy: Always
|
restartPolicy: Always
|
||||||
|
---
|
||||||
|
apiVersion: v1
|
||||||
|
kind: Service
|
||||||
|
metadata:
|
||||||
|
name: geo-service
|
||||||
|
spec:
|
||||||
|
ports:
|
||||||
|
- port: 80
|
||||||
|
name: http
|
||||||
|
protocol: TCP
|
||||||
|
targetPort: 80
|
||||||
|
selector:
|
||||||
|
app: geo-service
|
||||||
|
type: ClusterIP
|
||||||
|
|||||||
Generated
-16
@@ -1,16 +0,0 @@
|
|||||||
hash: 970b38f08b875634ffd52ae0c930abe90a206ae26803a5c66462ea0358178d58
|
|
||||||
updated: 2019-02-12T16:59:28.793243+01:00
|
|
||||||
imports:
|
|
||||||
- name: github.com/caarlos0/env
|
|
||||||
version: 518fcfb943252f77846f92ce5794086d9cb4a1b5
|
|
||||||
- name: github.com/google/uuid
|
|
||||||
version: 9b3b1e0f5f99ae461456d768e7d301a7acdaa2d8
|
|
||||||
- name: golang.org/x/time
|
|
||||||
version: 85acf8d2951cb2a3bde7632f9ff273ef0379bcbd
|
|
||||||
subpackages:
|
|
||||||
- rate
|
|
||||||
- name: googlemaps.github.io/maps
|
|
||||||
version: be134e760d707b2057d6a68668e19c509147d805
|
|
||||||
subpackages:
|
|
||||||
- internal
|
|
||||||
testImports: []
|
|
||||||
@@ -1,7 +0,0 @@
|
|||||||
package: gitlab.com/unboundsoftware/geo-service
|
|
||||||
import:
|
|
||||||
- package: googlemaps.github.io/maps
|
|
||||||
- package: github.com/caarlos0/env
|
|
||||||
version: ^3.5.0
|
|
||||||
- package: github.com/google/uuid
|
|
||||||
version: ^1.1.0
|
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
module gitlab.com/unboundsoftware/dancefinder/geo-service
|
||||||
|
|
||||||
|
require (
|
||||||
|
github.com/caarlos0/env v3.5.0+incompatible
|
||||||
|
github.com/google/uuid v1.1.0
|
||||||
|
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c
|
||||||
|
googlemaps.github.io/maps v0.0.0-20190206003505-be134e760d70
|
||||||
|
)
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
github.com/caarlos0/env v3.5.0+incompatible h1:Yy0UN8o9Wtr/jGHZDpCBLpNrzcFLLM2yixi/rBrKyJs=
|
||||||
|
github.com/caarlos0/env v3.5.0+incompatible/go.mod h1:tdCsowwCzMLdkqRYDlHpZCp2UooDD3MspDBjZ2AD02Y=
|
||||||
|
github.com/google/uuid v1.1.0 h1:Jf4mxPC/ziBnoPIdpQdPJ9OeiomAUHLvxmPRSPH9m4s=
|
||||||
|
github.com/google/uuid v1.1.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||||
|
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg=
|
||||||
|
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
|
||||||
|
googlemaps.github.io/maps v0.0.0-20190206003505-be134e760d70 h1:PC1NdMj+SxZxIgOfhklcq1HZCxGZ6cdh9UIogjorVuw=
|
||||||
|
googlemaps.github.io/maps v0.0.0-20190206003505-be134e760d70/go.mod h1:skwIRP56b3wXI7uVor5+NBjKLuQ3WXPpUvSKq4k7luo=
|
||||||
@@ -8,15 +8,42 @@ import (
|
|||||||
"googlemaps.github.io/maps"
|
"googlemaps.github.io/maps"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
type config struct {
|
type config struct {
|
||||||
MapsApiKey string `env:"MAPS_API_KEY"`
|
MapsApiKey string `env:"MAPS_API_KEY"`
|
||||||
|
Port int `env:"PORT" envDefault:"80"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type location struct {
|
type location struct {
|
||||||
Lat float64 `json:"lat"`
|
Lat float64 `json:"latitude"`
|
||||||
Long float64 `json:"long"`
|
Long float64 `json:"longitude"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type distance struct {
|
||||||
|
Text string `json:"text"`
|
||||||
|
Value int `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type duration struct {
|
||||||
|
Text string `json:"text"`
|
||||||
|
Value float64 `json:"value"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type destination struct {
|
||||||
|
Destination string `json:"destination"`
|
||||||
|
Distance distance `json:"distance"`
|
||||||
|
Duration duration `json:"duration"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type origin struct {
|
||||||
|
Origin string `json:"origin"`
|
||||||
|
Destinations []destination `json:"destinations"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type distanceResponse struct {
|
||||||
|
Origins []origin `json:"origins"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
@@ -33,7 +60,8 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
http.HandleFunc("/latlong/", makeHandler(handleLatLongRequest, client))
|
http.HandleFunc("/latlong/", makeHandler(handleLatLongRequest, client))
|
||||||
log.Fatal(http.ListenAndServe(":8000", nil))
|
http.HandleFunc("/distance/", makeHandler(handleDistanceMatrixRequest, client))
|
||||||
|
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", cfg.Port), nil))
|
||||||
}
|
}
|
||||||
|
|
||||||
func makeHandler(fn func(http.ResponseWriter, *http.Request, *maps.Client), client *maps.Client) http.HandlerFunc {
|
func makeHandler(fn func(http.ResponseWriter, *http.Request, *maps.Client), client *maps.Client) http.HandlerFunc {
|
||||||
@@ -41,6 +69,51 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, *maps.Client), clie
|
|||||||
fn(w, r, client)
|
fn(w, r, client)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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"), "|")
|
||||||
|
|
||||||
|
req := &maps.DistanceMatrixRequest{
|
||||||
|
Origins: origins,
|
||||||
|
Destinations: destinations,
|
||||||
|
Language: "sv",
|
||||||
|
Units: maps.UnitsMetric,
|
||||||
|
}
|
||||||
|
|
||||||
|
if result, err := client.DistanceMatrix(context.Background(), req); err != nil {
|
||||||
|
log.Fatalf("fatal error: %s", err)
|
||||||
|
w.WriteHeader(400)
|
||||||
|
} else {
|
||||||
|
res := distanceResponse{}
|
||||||
|
|
||||||
|
for i, r := range result.Rows {
|
||||||
|
var dests []destination
|
||||||
|
for j, e := range r.Elements {
|
||||||
|
dests = append(dests, destination{
|
||||||
|
destinations[j],
|
||||||
|
distance{
|
||||||
|
Text: e.Distance.HumanReadable,
|
||||||
|
Value: e.Distance.Meters,
|
||||||
|
},
|
||||||
|
duration{
|
||||||
|
e.Duration.String(),
|
||||||
|
e.Duration.Minutes(),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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/"):],
|
||||||
|
|||||||
Reference in New Issue
Block a user