diff --git a/cmd/analyze-gpx/main.go b/cmd/analyze-gpx/main.go index 03b4fcd..17e2a76 100644 --- a/cmd/analyze-gpx/main.go +++ b/cmd/analyze-gpx/main.go @@ -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) } diff --git a/cmd/serve-rwgps/main.go b/cmd/serve-rwgps/main.go new file mode 100644 index 0000000..f149d44 --- /dev/null +++ b/cmd/serve-rwgps/main.go @@ -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 +} diff --git a/pkg/openname/summarize.go b/pkg/openname/summarize.go index c9e513e..c5231a2 100644 --- a/pkg/openname/summarize.go +++ b/pkg/openname/summarize.go @@ -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