Go is a rising multi-purpose programming language, widely used for mostly server-side applications because of its speed, concurrency and ease of use. It has a built-in and production-ready template engine and http server, which also makes it a excellent choice for web applications. This example shows how to create a small web application with Go, using Gentics Mesh as backend to manage all the data for us.
Make sure you have Go installed or set it up as described here. Now get and start the example from Github:
# Download example and change to directory
go get github.com/gentics/mesh-go-example
cd $GOPATH/src/github.com/gentics/mesh-go-example
# Download Gentics Mesh from http://getmesh.io/Download and start it in another terminal
java -jar mesh-demo-0.6.xx.jar
# Run the example
go run main.go
Navigate your browser to http://localhost:8081/. The example web app is simply a Golang reimplementation of our previous examples in PHP and NodeJS. A small website listing vehicles from out demo data, grouping them into categories and generating detail pages.
While our PHP and Node.js examples did only use one http route handler, there are two handler functions in this application. I like to use the popular Gorilla toolkits excellent routing via mux
instead of checking in the handler function itself if the request path was /
. The first IndexHandler
simply generates the welcome page, the only dynamic content on it is the breadcrumb navigation. The second PathHandler
is more complex, it handles every request which is not to the welcome page, including requests to images. It uses the request path to retrieve a node from Mesh, first determining via content type header if the requested node is actually an image. If thats the case, the binary data is simply forwarded to the requesting client. Else, the node is decoded to JSON and depending on its schema - vehicle or category - the handler either renders a product detail page or product list page.
https://gist.github.com/simt2/48f80570b64a7ca456c4cdd3475d46aa
The Go programming language is a strong and static typed language, working with a struct for every object to unmarshal from JSON is usually the way to go. But for small applications which come in contact with a relatively high count of different data structures from a backend API like this, it is often more convenient and without disadvantage to treat our APIs responses as nested JSON map with arbitrary depth. The GJSON library provides a very fast way of indexing JSON and is used in our functions and templates to parse Mesh node objects.
There are three kinds of pages our Example, the welcome pages, product list pages and a product detail page. Every page displays the navigation, the product pages additionally a category and list of products. This data is given to our templates via the following struct:
https://gist.github.com/simt2/8f3f59609f8b2d785ee5892143d54415
The field Breadcrumb
is used on every page to render the navigation no top. The following LoadBreadcrumb()
function uses the (navroot endpoint)[http://getmesh.io/docs/beta/raml/#projectName__navroot] of Gentics Mesh to retrieve it.
https://gist.github.com/simt2/16ed876bcaf92e4c91769abe75e47da6
The base.html
template is the base for all pages. It includes Twitter Bootstrap, the navigation template on top of the page and below one of the actual content templates.
https://gist.github.com/simt2/ce3a503fafeca06b897678daa4ee3184
https://gist.github.com/simt2/f930c410deb687192d23f5be9efe0fec
The welcome.html
template has no magic going on, just display information about the example when accessing the index page http://localhost:8081/
.
https://gist.github.com/simt2/59a42888cdacfe83ea7f3bea7fc00456
productList.html
is used for displaying all products of a category. It iterates over the field Products
of the given templateData
struct, which was populated using the Gentics Mesh /api/v1/demo/nodes/:categoryUuid/children
endpoint earlier in the handler via LoadChildren()
function. The attributes of products represented as GJSON
values are extracted by the template using the Get function.
https://gist.github.com/simt2/eef0eb5aa5e90e70a654499b5d5d4a30
https://gist.github.com/simt2/4366d7db80ce7c40bce3badc7999582d
The productDetail.html
template renders detailed information about a single product, given as a list with only one element. This might seem strange, one could argue that defining multiple data structs for each different template or even using anonymous structs is a better approach, but for the sake of this examples simplicity we keep it to one structure.
https://gist.github.com/simt2/d513d1ba7ee0199ab41225fbe21abfba
In this example I'm using a session cookie instead of basic auth to authenticate every request to the Gentics Mesh backend. The main advantage is that Mesh only needs to check my username and password once at login, this leads to a noticeable speedup of all later requests.
https://gist.github.com/simt2/08f6905f999d1291b0e996c00ee73a92
We hope this example was informative and showed you how easy it is to utilize Go in combination with Gentics Mesh to build a small template-based website. Feel free to leave a comment if you have any questions or feedback about this example.
You can also find it on Github: https://github.com/gentics/mesh-go-example