Build RESTful API in Go and MongoDB

Written by mlabouardy | Published 2017/11/11
Tech Story Tags: golang | mongodb | rest-api | api

TLDRvia the TL;DR App

In this tutorial I will illustrate how you can build your own RESTful API in Go and MongoDB. All the code used in this demo can be found on my Github.

1 — API Specification

The REST API service will expose endpoints to manage a store of movies. The operations that our endpoints will allow are:

2 — Fetching Dependencies

Before we begin, we need to get the packages we need to setup the API:

go get github.com/BurntSushi/toml gopkg.in/mgo.v2 github.com/gorilla/mux

  • toml : Parse the configuration file (MongoDB server & credentials)
  • mux : Request router and dispatcher for matching incoming requests to their respective handler
  • mgo : MongoDB driver

3 — API structure

Once the dependencies are installed, we create a file called “app.go“, with the following content:

The code above creates a controller for each endpoint, then expose an HTTP server on port 3000.

Note: We are using GET, POST, PUT, and DELETE where appropriate. We are also defining parameters that can be passed in

To run the server in local, type the following command:

go run app.go

If you point your browser to http://localhost:3000/movies, you should see:

4 — Model

Now that we have a minimal application, it’s time to create a basic Movie model. In Go, we use struct keyword to create a model:

Next, we will create the Data Access Object to manage database operations.

5 — Data Access Object

5.1 — Establish Connection

The connect() method as its name implies, establish a connection to MongoDB database.

5.2 — Database Queries

The implementation is relatively straighforward and just includes issuing right method using db.C(COLLECTION) object and returning the results. These methods can be implemented as follows:

6 — Setup API Endpoints

6.1 — Create a Movie

Update the CreateMovieEndpoint method as follows:

It decodes the request body into a movie object, assign it an ID, and uses the DAO Insert method to create a movie in database.

Let’s test it out:

With Postman:

With cURL

curl -sSX POST -d ‘{“name”:”dunkirk”,”cover_image”:”https://image.tmdb.org/t/p/w640/cUqEgoP6kj8ykfNjJx3Tl5zHCcN.jpg", “description”:”world war 2 movie”}’ http://localhost:3000/movies | jq ‘.’

6.2 — List of Movies

The code below is self explanatory:

It uses FindAll method of DAO to fetch list of movies from database.

Let’s test it out:

With Postman:

With cURL:

curl -sSX GET http://localhost:3000/movies | jq ‘.’

6.3 — Find a Movie

We will use the mux library to get the parameters that the users passed in with the request:

Let’s test it out:

With Postman:

With cURL:

curl -sSX GET http://localhost:3000/movies/599570faf0429b4494cfa5d4 | jq ‘.’

6.4 — Update an existing Movie

Update the UpdateMovieEndPoint method as follows:

Let’s test it out:

With Postman:

With cURL:

curl -sSX PUT -d ‘{“name”:”dunkirk”,”cover_image”:”https://image.tmdb.org/t/p/w640/cUqEgoP6kj8ykfNjJx3Tl5zHCcN.jpg", “description”:”world war 2 movie”}’ http://localhost:3000/movies | jq ‘.’

6.5 — Delete an existing Movie

Update the DeleteMovieEndPoint method as follows:

Let’s test it out:

With Postman:

With cURL:

curl -sSX DELETE -d ‘{“name”:”dunkirk”,”cover_image”:”https://image.tmdb.org/t/p/w640/cUqEgoP6kj8ykfNjJx3Tl5zHCcN.jpg", “description”:”world war 2 movie”}’ http://localhost:3000/movies | jq ‘.’

Taking this further ? On my upcoming posts, I will show you how :

  • Write Unit Tests in Go for each Endpoint
  • Build a UI in Angular 4
  • Setup a CI/CD with CircleCI
  • Deploy the stack on AWS and much more …

So stay tuned !


Published by HackerNoon on 2017/11/11