diff --git a/Dockerfile b/Dockerfile index 73dec98..91f72ae 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,11 +1,10 @@ -FROM gunosy/ci-go-glide as builder -WORKDIR /go/src/gitlab.com/unboundsoftware/geo-service -COPY glide.* ./ -RUN glide install +FROM golang:1.11-stretch as builder +WORKDIR /go/src/gitlab.com/unboundsoftware/dancefinder/geo-service COPY * ./ +RUN go get RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o geo-service . FROM scratch 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"] diff --git a/deployment_files/deploy.yaml b/deployment_files/deploy.yaml index c4ace90..433639f 100644 --- a/deployment_files/deploy.yaml +++ b/deployment_files/deploy.yaml @@ -42,7 +42,24 @@ spec: memory: "100Mi" imagePullPolicy: Always image: registry.gitlab.com/unboundsoftware/dancefinder/geo-service:${COMMIT} + ports: + - containerPort: 80 + name: http envFrom: - secretRef: name: google-maps-api 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 diff --git a/glide.lock b/glide.lock deleted file mode 100644 index e63a263..0000000 --- a/glide.lock +++ /dev/null @@ -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: [] diff --git a/glide.yaml b/glide.yaml deleted file mode 100644 index 35110a4..0000000 --- a/glide.yaml +++ /dev/null @@ -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 diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..2826060 --- /dev/null +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..5fc1405 --- /dev/null +++ b/go.sum @@ -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= diff --git a/main.go b/main.go index ae944c3..40f7f8b 100644 --- a/main.go +++ b/main.go @@ -8,15 +8,42 @@ import ( "googlemaps.github.io/maps" "log" "net/http" + "strings" ) type config struct { MapsApiKey string `env:"MAPS_API_KEY"` + Port int `env:"PORT" envDefault:"80"` } type location struct { - Lat float64 `json:"lat"` - Long float64 `json:"long"` + Lat float64 `json:"latitude"` + 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() { @@ -33,7 +60,8 @@ func main() { } 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 { @@ -41,6 +69,51 @@ func makeHandler(fn func(http.ResponseWriter, *http.Request, *maps.Client), clie 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) { req := &maps.GeocodingRequest{ Address: r.URL.Path[len("/latlong/"):],