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 session imformation. Needs to be initialized once type DB struct { session *mgo.Session collection *mgo.Collection } type Movie struct { ID bson.ObjectId...