November 2017
Intermediate to advanced
670 pages
17h 35m
English
The first thing we do in the main() function is check the RUN_HTTP_SERVER environment variable. If it's set to true, then the program will set up two routes. The first route /cars returns the index page that displays all the cars that have been loaded from the .csv files. The second route /cars/:id retrieves an individual car object and returns its JSON representation:
func main() { if os.Getenv("RUN_HTTP_SERVER") == "TRUE" { router := httprouter.New() router.GET("/cars", CarsIndexHandler) router.GET("/cars/:id", CarHandler) log.Println("Listening on port 8000") log.Fatal(http.ListenAndServe(":8000", router))
The IndexedCars variable is defined in types.go as follows:
IndexedCar struct { Index int `json:"index"` Car ...