mirror of
https://github.com/cjdenio/uta-trax-api.git
synced 2026-10-03 16:06:19 +00:00
1 file changed
+2
-27
+2
-27
@@ -2,7 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"log"
|
"log"
|
||||||
@@ -11,7 +10,6 @@ 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"
|
||||||
@@ -21,8 +19,6 @@ 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)
|
||||||
@@ -98,21 +94,13 @@ func fetchVehicleFeed() ([]byte, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getVehicles() ([]*pb.VehiclePosition, *pb.FeedHeader, error) {
|
func getVehicles() ([]*pb.VehiclePosition, *pb.FeedHeader, error) {
|
||||||
var bytes []byte
|
bytes, err := fetchVehicleFeed()
|
||||||
|
|
||||||
if len(gtfsRtCache) > 0 {
|
|
||||||
bytes = gtfsRtCache
|
|
||||||
} else {
|
|
||||||
return nil, nil, errors.New("no cache")
|
|
||||||
var err error
|
|
||||||
bytes, err = fetchVehicleFeed()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, err
|
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
|
||||||
}
|
}
|
||||||
@@ -223,19 +211,6 @@ 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