This commit is contained in:
2026-04-26 16:10:20 -06:00
parent 299947e536
commit f625af16b9
3 changed files with 35 additions and 4 deletions
+1
View File
@@ -2,3 +2,4 @@
/gtfs.zip
/pretend-feed.bin
/tmp/
/bin
+5
View File
@@ -1,3 +1,8 @@
all: bin
bin: cmd/main.go proto/*.go
go build -o bin cmd/main.go
proto: proto/schema.pb.go proto/gtfs-realtime.pb.go
proto/gtfs-realtime.pb.go: proto/gtfs-realtime.proto
+29 -4
View File
@@ -2,6 +2,7 @@ package main
import (
"database/sql"
"errors"
"fmt"
"io"
"log"
@@ -10,6 +11,7 @@ import (
"os"
"strconv"
"strings"
"time"
pb "github.com/cjdenio/uta-trax-api/proto"
"google.golang.org/protobuf/proto"
@@ -19,6 +21,8 @@ import (
var scheduleDb *sql.DB
var gtfsRtCache []byte
func distance(lat1 float64, lng1 float64, lat2 float64, lng2 float64) float64 {
radlat1 := float64(math.Pi * lat1 / 180)
radlat2 := float64(math.Pi * lat2 / 180)
@@ -94,13 +98,21 @@ func fetchVehicleFeed() ([]byte, error) {
}
func getVehicles() ([]*pb.VehiclePosition, *pb.FeedHeader, error) {
bytes, err := fetchVehicleFeed()
if err != nil {
return nil, nil, err
var bytes []byte
if len(gtfsRtCache) > 0 {
bytes = gtfsRtCache
} else {
return nil, nil, errors.New("no cache")
var err error
bytes, err = fetchVehicleFeed()
if err != nil {
return nil, nil, err
}
}
feed := pb.FeedMessage{}
err = proto.Unmarshal(bytes, &feed)
err := proto.Unmarshal(bytes, &feed)
if err != nil {
return nil, nil, err
}
@@ -211,6 +223,19 @@ func main() {
scheduleDb = _db
go func() {
for {
feed, err := fetchVehicleFeed()
if err == nil {
gtfsRtCache = feed
log.Println("refreshed cache")
} else {
log.Println(err)
}
time.Sleep(1 * time.Second)
}
}()
http.Handle("/", http.FileServer(http.Dir("./static")))
http.HandleFunc("/schema.proto", func(w http.ResponseWriter, r *http.Request) {