Add rudimentary HTTP server for RideWithGPS routes.

This commit is contained in:
Ray Miller 2020-04-17 13:01:05 +01:00
parent f879acf25d
commit d1934a0e85
3 changed files with 96 additions and 8 deletions

View file

@ -52,8 +52,12 @@ func summarizeDirectory(gs *openname.GPXSummarizer, dirName string) error {
continue
}
filename := path.Join(dirName, f.Name())
r, err := os.Open(filename)
if err != nil {
return fmt.Errorf("error opening %s for reading: %v", filename, err)
}
log.Printf("Analyzing %s", filename)
summary, err := gs.SummarizeTrack(filename)
summary, err := gs.SummarizeTrack(r)
if err != nil {
return fmt.Errorf("error creating summary of GPX track %s: %v", filename, err)
}
@ -75,7 +79,11 @@ func summarizeDirectory(gs *openname.GPXSummarizer, dirName string) error {
}
func summarizeSingleFile(gs *openname.GPXSummarizer, filename string) error {
summary, err := gs.SummarizeTrack(filename)
r, err := os.Open(filename)
if err != nil {
return fmt.Errorf("error opening %s for reading: %v", filename, err)
}
summary, err := gs.SummarizeTrack(r)
if err != nil {
return fmt.Errorf("error creating summary of GPX track %s: %v", filename, err)
}

84
cmd/serve-rwgps/main.go Normal file
View file

@ -0,0 +1,84 @@
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"github.com/ray1729/gpx-utils/pkg/openname"
)
func main() {
listenAddr := os.Getenv("LISTEN_ADDR")
if listenAddr == "" {
listenAddr = ":8000"
}
openNames := os.Getenv("OPNAMES")
if openNames == "" {
log.Fatal("OPNAMES not set")
}
rt, err := openname.BuildIndex(openNames)
if err != nil {
log.Fatal(err)
}
gs, err := openname.NewGPXSummarizer(rt)
if err != nil {
log.Fatal(err)
}
gpxSummarizer = gs
http.HandleFunc("/rwgps", rwgpsHandler)
log.Fatal(http.ListenAndServe(listenAddr, nil))
}
var gpxSummarizer *openname.GPXSummarizer
func rwgpsHandler(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
x := q.Get("routeId")
if x == "" {
http.Error(w, "routeId is required", http.StatusBadRequest)
return
}
routeId, err := strconv.Atoi(x)
if err != nil {
http.Error(w, "invalid route id", http.StatusBadRequest)
return
}
track, err := FetchTrack(routeId)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
summary, err := gpxSummarizer.SummarizeTrack(bytes.NewReader(track))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
result, err := json.Marshal(summary)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Add("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(result)
}
func FetchTrack(routeId int) ([]byte, error) {
url := fmt.Sprintf("https://ridewithgps.com/routes/%d.gpx?sub_format=track", routeId)
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("error getting %s: %v", url, err)
}
defer resp.Body.Close()
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("error reading response from %s: %v", url, err)
}
return data, nil
}

View file

@ -1,8 +1,8 @@
package openname
import (
"io"
"math"
"os"
"strings"
"time"
@ -58,11 +58,7 @@ type TrackSummary struct {
PointsOfInterest []POI
}
func (gs *GPXSummarizer) SummarizeTrack(filename string) (*TrackSummary, error) {
r, err := os.Open(filename)
if err != nil {
return nil, err
}
func (gs *GPXSummarizer) SummarizeTrack(r io.Reader) (*TrackSummary, error) {
g, err := gpx.Read(r)
if err != nil {
return nil, err