Add TTL cache for stops index.

This commit is contained in:
Ray Miller 2020-04-21 08:45:05 +01:00
parent 3caaf59506
commit 507d20810d
5 changed files with 118 additions and 59 deletions

View file

@ -2,6 +2,7 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
@ -17,27 +18,32 @@ import (
func main() {
log.SetFlags(0)
if len(os.Args) != 2 {
log.Fatal("Usage: %s GPX_FILE_OR_DIRECTORY")
stopNames := flag.String("stops", "", "Source for refreshment stops")
flag.Parse()
if flag.NArg() != 1 {
log.Fatal("Usage: %s [--stops=ctccambridge|cyclingmaps] GPX_FILE_OR_DIRECTORY")
}
inFile := os.Args[1]
inFile := flag.Arg(0)
info, err := os.Stat(inFile)
if err != nil {
log.Fatal(err)
}
// TODO add --stops flag to select stops database
stopsIndex, err := cafes.FetchCtcCamIndex()
if err != nil {
log.Fatal(err)
var stops *rtreego.Rtree
if *stopNames != "" {
var err error
stops, err = cafes.New().Get(*stopNames)
if err != nil {
log.Fatal(err)
}
}
gs, err := placenames.NewGPXSummarizer()
if err != nil {
log.Fatal(err)
}
if info.IsDir() {
err = summarizeDirectory(gs, stopsIndex, inFile)
err = summarizeDirectory(gs, stops, inFile)
} else {
err = summarizeSingleFile(gs, stopsIndex, inFile)
err = summarizeSingleFile(gs, stops, inFile)
}
if err != nil {
log.Fatal(err)

View file

@ -3,6 +3,7 @@ package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"log"
"net/http"
@ -26,33 +27,13 @@ func main() {
log.Fatal(err)
}
gpxSummarizer = gs
if err = loadStops(); err != nil {
log.Fatal(err)
}
http.HandleFunc("/rwgps", rwgpsHandler)
log.Printf("Listening for requests on %s", listenAddr)
log.Fatal(http.ListenAndServe(listenAddr, nil))
}
func loadStops() error {
var err error
log.Println("Fetching CTC Cambridge cafe stops")
stops["ctccam"], err = cafes.FetchCtcCamIndex()
if err != nil {
return err
}
log.Printf("Loaded %d ctccam stops", stops["ctccam"].Size())
log.Println("Fetching cyclingmaps.net cafe stops")
stops["cyclingmapsnet"], err = cafes.FetchCyclingMapsIndex()
if err != nil {
return err
}
log.Printf("Loaded %d cyclingmapsnet stops", stops["cyclingmapsnet"].Size())
return nil
}
var gpxSummarizer *placenames.GPXSummarizer
var stops = make(map[string]*rtreego.Rtree)
var stops = cafes.New()
func rwgpsHandler(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
@ -72,10 +53,15 @@ func rwgpsHandler(w http.ResponseWriter, r *http.Request) {
}
var stopsIndex *rtreego.Rtree
if stopsName != "" {
stopsIndex = stops[stopsName]
if stopsIndex == nil {
log.Printf("Invalid stops: %s", stopsName)
http.Error(w, fmt.Sprintf("Invalid stops: %s", stopsName), http.StatusBadRequest)
var err error
stopsIndex, err = stops.Get(stopsName)
if err != nil {
log.Println(err)
if errors.Is(err, cafes.ErrInvalidStops) {
http.Error(w, err.Error(), http.StatusBadRequest)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
}