Browse Source

Improvements

master
Patrick G 2 years ago
parent
commit
a91e0a227d
Signed by: geek1011 GPG Key ID: A2FD79F68A2AB707
2 changed files with 46 additions and 35 deletions
  1. +1
    -13
      README.md
  2. +45
    -22
      drtapi.go

+ 1
- 13
README.md View File

@ -1,15 +1,3 @@
# DRTApi
An api server and client for the Durham Region Transit buses. It combines the realtime and GTFS data.
## API server
- `/v1/stops` - All stops
- `/v1/stops/:stopid` - Stop info
- `/v1/stops/:stopid/departures` - Realtime departures
- `/v1/stops/:stopid/routes` - Routes for stop
- `/v1/search/stops?q=query` - Search for stop
- `/v1/routes` - All routes
- `/v1/routes/:routeid` - Route info
- `/v1/routes/:routeid/stops` - Stops for route
- `/v1/nearby/stops/:lat/:lon` - Nearby stops
- `/v1/nearby/stops/:lat/:lon?numResults=10&maxDistance=300` - Nearby stops (with optional parameters) (distance in meters)
An api server and client for the Durham Region Transit buses. It combines the realtime and GTFS data.

+ 45
- 22
drtapi.go View File

@ -51,6 +51,16 @@ func handleJSON(w http.ResponseWriter, r *http.Request, err error, v interface{}
fmt.Fprintf(w, j)
}
func handleURLList(w http.ResponseWriter, r *http.Request, v map[string]string) {
w.Header().Set("Content-Type", "application/json")
j, err := toJSON(v)
if err != nil {
writeError(w, 500, err)
return
}
fmt.Fprintf(w, j)
}
func main() {
ac := drt.NewAPIClient(time.Second * 10)
go func() {
@ -66,9 +76,34 @@ func main() {
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
http.Redirect(w, r, "/v1/", 301)
})
router.GET("/v1", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
base := "http://" + r.Host + "/v1"
handleURLList(w, r, map[string]string{
"stops_url": base + "/stops{?q}",
"stop_url": base + "/stops/{id}",
"stop_departures_url": base + "/stops/{id}/departures",
"stop_routes_url": base + "/stops/{id}/routes",
"nearby_stops_url": base + "/nearby/stops/{lat}/{lon}{?maxDistance,numResults}",
"routes_url": base + "/routes",
"route_url": base + "/routes/{id}",
"route_stops_url": base + "/routes/{id}/stops",
})
})
router.GET("/v1/stops", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
q := strings.TrimSpace(strings.ToLower(r.URL.Query().Get("q")))
s, e := ac.GetStops()
handleJSON(w, r, e, s)
if e != nil || q == "" {
handleJSON(w, r, e, s)
return
}
f := []drt.Stop{}
for _, stop := range s {
if strings.Contains(strings.ToLower(stop.Name), q) || strings.Contains(string(stop.ID), q) {
f = append(f, stop)
}
}
handleJSON(w, r, e, f)
})
router.GET("/v1/stops/:stopid", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s, e := ac.GetStopInfo(drt.StopID(p.ByName("stopid")))
@ -82,27 +117,6 @@ func main() {
s, e := ac.GetRoutesByStop(drt.StopID(p.ByName("stopid")))
handleJSON(w, r, e, s)
})
router.GET("/v1/search/stops", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
q := strings.ToLower(r.URL.Query().Get("q"))
s, e := ac.GetStops()
f := []drt.Stop{}
if e == nil {
for _, stop := range s {
t := false
if strings.Contains(strings.ToLower(stop.Name), q) {
t = true
} else if strings.Contains(string(stop.ID), q) {
t = true
}
if t {
f = append(f, stop)
}
}
}
handleJSON(w, r, e, f)
})
router.GET("/v1/nearby/stops/:lat/:lon", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
lat, err := strconv.ParseFloat(p.ByName("lat"), 64)
if err != nil {
@ -130,6 +144,15 @@ func main() {
s, e := ac.GetRouteStops(drt.RouteID(p.ByName("routeid")))
handleJSON(w, r, e, s)
})
router.NotFound = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
j, err := toJSON(result{"error", time.Now(), "not found"})
if err != nil {
writeError(w, 500, err)
return
}
fmt.Fprintf(w, j)
})
log.Println("Serving on :8001")
err := http.ListenAndServe(":8001", router)

Loading…
Cancel
Save