Registering the server-side handlers

After we have initialized the template set inside the main function of the igweb.go source file, we create a new Gorilla Mux router, and we call the registerRoutes function to register all the routes of the server-side web application. Let's examine the lines of the registerRoutes function that are essential for the proper functioning of the client-side web application:

// Register Handlers for Client-Side JavaScript Application
r.Handle("/js/client.js", isokit.GopherjsScriptHandler(WebAppRoot)).Methods("GET")
r.Handle("/js/client.js.map", isokit.GopherjsScriptMapHandler(WebAppRoot)).Methods("GET")

// Register handler for the delivery of the template bundle
r.Handle("/template-bundle", handlers.TemplateBundleHandler(env)).Methods("POST")

We register a handler for the /js/client.js route and specify that it will be handled by the GopherjsScriptHandler function from the isokit package. This will associate the route to serving the client.js JavaScript source file that was built by running the gopherjs build command in the client directory.

We handle the map file of the client.js.map in a similar manner. We register a /js/client.js.map route and specify that it will be handled by the GopherjsScriptMapHandler function from the isokit package.

Now that we've registered the routes for the JavaScript source file and the JavaScript source map file that are critical for our client-side application to function, we need to register a route to access the template bundle. We will call the Handle method on the r router object and specify that the /template-bundle route will be handled by the TemplateBundleHandler function, found in the handlers package. This route will be retrieved by the client through an XHR call, and the server will send the template bundle as gob encoded data.

The very last route that we register, which is of particular interest for us right now, is the /about route. Here's the line of code where we register the /about route and associate it with the AboutHandler function found in the handlers package:

r.Handle("/about", handlers.AboutHandler(env)).Methods("GET")

Now that we've seen how to set up the template set in our server-side web application, and how we registered the routes that are of importance to us in this chapter, let's go ahead and take a look at the server-side handlers, starting with the TemplateBundleHandler function in the handlers package.

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

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