RESTful API with Gorilla Mux and MongoDB

In the previous chapters, we explored all the possible ways of building a RESTful API. We first looked into HTTP routers, then web frameworks. But as a personal choice, in order to make our API lightweight, one prefers Gorilla Mux as the default choice and mgo for the MongoDB driver. In this section, we are going to build a proper movies API with an end-to-end integration of the database and HTTP router. We saw how to create a new resource and retrieve it back using Go and MongoDB. Using that knowledge, let us write this program:

package main import ( "encoding/json" "io/ioutil" "log" "net/http" "time" "github.com/gorilla/mux" mgo "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ) // DB stores the database ...

Get Building RESTful Web services with Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.