The gopher team Rest API endpoint

The /restapi/get-gopher-team route is handled by the GetGopherTeamEndpoint function defined in the gopherteam.go source file found in the endpoints folder:

func GetGopherTeamEndpoint(env *common.Env) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gophers := env.DB.GetGopherTeam()
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(gophers)
})
}

We will declare and initialize the gophers variable to the value returned from calling the GetGopherTeam method of the Redis datastore object, env.DB. We will then set a header to indicate that the server will be sending a JSON response. Finally, we will use a JSON encoder to encode the slice of gophers as JSON data. The data is sent to the client using http.ResponseWriter, w.

We have now set up everything we need to render the About page from the client side. We can view our client-side rendered About page by clicking on the About link on the navigation bar. Here's what the About page looks like rendered from the client side:

Figure 4.6 The About page rendered from the client-side

Can you make out any difference between the About page rendered on the server side and the one that was rendered on the client side? You shouldn't be able to see any differences since they are practically identical! We saved the user from having to witness a full page reload by simply rendering the About page content in the primary content area div container.

Take a look at the start times that are displayed for each gopher. The first time presented here, follows Go's default time formatting. The second time is the time using the Ruby date format. Recall that we use a custom function to present the time in this format. The third start time is displayed in human readable format. It uses a reusable component to format the time, which we will cover in Chapter 9, Cogs – Reusable Components.

We now know how to render templates isomorphically, and we will be following this same procedure for the other pages on IGWEB.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.113.163