Browse Source

Added Makefile, gitignore, and updated API

master
Patrick G 2 years ago
parent
commit
66507465dc
Signed by: geek1011 GPG Key ID: A2FD79F68A2AB707
4 changed files with 28 additions and 15 deletions
  1. +1
    -0
      .gitignore
  2. +11
    -0
      Makefile
  3. +3
    -0
      drt/drt.go
  4. +13
    -15
      drtapi.go

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
build

+ 11
- 0
Makefile View File

@ -0,0 +1,11 @@
.PHONY: default
default: clean build
.PHONY: clean
clean:
rm -rfv build
.PHONY: build
build:
mkdir -p build
go build -ldflags "-X main.gitShortSHA=$(shell git rev-parse --short HEAD)" -o "build/drtapi"

+ 3
- 0
drt/drt.go View File

@ -77,6 +77,9 @@ type APIClient struct {
type keyValue map[string]string
// gitShortSHA is populated by the build process.
var gitShortSHA = "undefined"
func (c *APIClient) makeRequest(path string, params keyValue, target interface{}) error {
hc := &http.Client{
Timeout: c.Timeout,

+ 13
- 15
drtapi.go View File

@ -49,40 +49,38 @@ func handleJSON(w http.ResponseWriter, r *http.Request, err error, v interface{}
fmt.Fprintf(w, j)
}
func index(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
http.Redirect(w, r, "/v1/", 301)
}
func main() {
ac := drt.NewAPIClient(time.Second * 10)
go func() {
err := ac.LoadGtfs()
if err != nil {
fmt.Printf("\nError loading GTFS data: %s\n\n", err)
fmt.Printf("Error loading GTFS data: %s\n\n", err)
return
}
fmt.Printf("\nLoaded GTFS data\n\n")
fmt.Printf("Loaded GTFS data\n\n")
}()
router := httprouter.New()
router.GET("/", index)
router.GET("/v1/stops", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
router.GET("/", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
http.Redirect(w, r, "/v1/", 301)
})
router.GET("/v1/stops/all", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
s, e := ac.GetStops()
handleJSON(w, r, e, s)
})
router.GET("/v1/stops/:stopid", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
router.GET("/v1/stops/bystop/:stopid", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s, e := ac.GetStopInfo(drt.StopID(p.ByName("stopid")))
handleJSON(w, r, e, s)
})
router.GET("/v1/stops/:stopid/departures", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
router.GET("/v1/stops/bystop/:stopid/departures", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s, e := ac.GetDeparturesByStop(drt.StopID(p.ByName("stopid")))
handleJSON(w, r, e, s)
})
router.GET("/v1/stops/:stopid/routes", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
router.GET("/v1/stops/bystop/:stopid/routes", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s, e := ac.GetRoutesByStop(drt.StopID(p.ByName("stopid")))
handleJSON(w, r, e, s)
})
router.GET("/v1/nearbystops/:lat/:lon", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
router.GET("/v1/stops/nearby/:lat/:lon", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
lat, err := strconv.ParseFloat(p.ByName("lat"), 64)
if err != nil {
writeError(w, 500, err)
@ -97,15 +95,15 @@ func main() {
s, e := ac.GetNearbyStops(drt.Lat(lat), drt.Lon(lon), drt.Meter(maxDistance), numResults)
handleJSON(w, r, e, s)
})
router.GET("/v1/routes", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
router.GET("/v1/routes/all", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s, e := ac.GetRoutes()
handleJSON(w, r, e, s)
})
router.GET("/v1/routes/:routeid", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
router.GET("/v1/routes/byroute/:routeid", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s, e := ac.GetRouteInfo(drt.RouteID(p.ByName("routeid")))
handleJSON(w, r, e, s)
})
router.GET("/v1/routes/:routeid/stops", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
router.GET("/v1/routes/byroute/:routeid/stops", func(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
s, e := ac.GetRouteStops(drt.RouteID(p.ByName("routeid")))
handleJSON(w, r, e, s)
})

Loading…
Cancel
Save