mirror of
https://github.com/cjdenio/uta-trax-api.git
synced 2026-08-04 22:06:34 +00:00
make
This commit is contained in:
@@ -2,3 +2,4 @@
|
|||||||
/gtfs.zip
|
/gtfs.zip
|
||||||
/pretend-feed.bin
|
/pretend-feed.bin
|
||||||
/tmp/
|
/tmp/
|
||||||
|
/bin
|
||||||
|
|||||||
@@ -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: proto/schema.pb.go proto/gtfs-realtime.pb.go
|
||||||
|
|
||||||
proto/gtfs-realtime.pb.go: proto/gtfs-realtime.proto
|
proto/gtfs-realtime.pb.go: proto/gtfs-realtime.proto
|
||||||
|
|||||||
+29
-4
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@@ -10,6 +11,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
pb "github.com/cjdenio/uta-trax-api/proto"
|
pb "github.com/cjdenio/uta-trax-api/proto"
|
||||||
"google.golang.org/protobuf/proto"
|
"google.golang.org/protobuf/proto"
|
||||||
@@ -19,6 +21,8 @@ import (
|
|||||||
|
|
||||||
var scheduleDb *sql.DB
|
var scheduleDb *sql.DB
|
||||||
|
|
||||||
|
var gtfsRtCache []byte
|
||||||
|
|
||||||
func distance(lat1 float64, lng1 float64, lat2 float64, lng2 float64) float64 {
|
func distance(lat1 float64, lng1 float64, lat2 float64, lng2 float64) float64 {
|
||||||
radlat1 := float64(math.Pi * lat1 / 180)
|
radlat1 := float64(math.Pi * lat1 / 180)
|
||||||
radlat2 := float64(math.Pi * lat2 / 180)
|
radlat2 := float64(math.Pi * lat2 / 180)
|
||||||
@@ -94,13 +98,21 @@ func fetchVehicleFeed() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getVehicles() ([]*pb.VehiclePosition, *pb.FeedHeader, error) {
|
func getVehicles() ([]*pb.VehiclePosition, *pb.FeedHeader, error) {
|
||||||
bytes, err := fetchVehicleFeed()
|
var bytes []byte
|
||||||
if err != nil {
|
|
||||||
return nil, nil, err
|
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{}
|
feed := pb.FeedMessage{}
|
||||||
err = proto.Unmarshal(bytes, &feed)
|
err := proto.Unmarshal(bytes, &feed)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
@@ -211,6 +223,19 @@ func main() {
|
|||||||
|
|
||||||
scheduleDb = _db
|
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.Handle("/", http.FileServer(http.Dir("./static")))
|
||||||
|
|
||||||
http.HandleFunc("/schema.proto", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/schema.proto", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|||||||
Reference in New Issue
Block a user