mirror of
https://github.com/cjdenio/uta-trax-api.git
synced 2026-08-04 22:06:34 +00:00
better json
This commit is contained in:
+23
-18
@@ -207,23 +207,8 @@ func feedifyVehicles(vehicles []*pb.VehiclePosition, header *pb.FeedHeader, rout
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Opening database...")
|
||||
|
||||
_db, err := sql.Open("sqlite3", "uta-gtfs.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
scheduleDb = _db
|
||||
|
||||
http.Handle("/", http.FileServer(http.Dir("./static")))
|
||||
|
||||
http.HandleFunc("/schema.proto", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./proto/schema.proto")
|
||||
})
|
||||
|
||||
http.HandleFunc("/api", func(w http.ResponseWriter, r *http.Request) {
|
||||
func vehicleHandler(mode string) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
vehicles, header, err := getVehicles()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@@ -244,7 +229,7 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
if r.Header.Get("Accept") == "application/json" {
|
||||
if mode == "json" || r.Header.Get("Accept") == "application/json" {
|
||||
feed := feedifyVehicles(vehicles, header, routeTypeFilter)
|
||||
response, err := json.Marshal(&feed)
|
||||
if err != nil {
|
||||
@@ -269,8 +254,28 @@ func main() {
|
||||
w.Header().Add("Content-Length", strconv.Itoa(len(b)))
|
||||
w.Write(b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("Opening database...")
|
||||
|
||||
_db, err := sql.Open("sqlite3", "uta-gtfs.db")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
scheduleDb = _db
|
||||
|
||||
http.Handle("/", http.FileServer(http.Dir("./static")))
|
||||
|
||||
http.HandleFunc("/schema.proto", func(w http.ResponseWriter, r *http.Request) {
|
||||
http.ServeFile(w, r, "./proto/schema.proto")
|
||||
})
|
||||
|
||||
http.HandleFunc("/api", vehicleHandler(""))
|
||||
http.HandleFunc("/api.json", vehicleHandler("json"))
|
||||
|
||||
port := "3000"
|
||||
if portEnv, ok := os.LookupEnv("PORT"); ok {
|
||||
port = portEnv
|
||||
|
||||
Reference in New Issue
Block a user