In this tutorial I will illustrate how you can build your own in and . All the code used in this demo can be found on my . RESTful API Go MongoDB Github 1 — API Specification The will expose endpoints to manage a store of movies. The operations that our endpoints will allow are: REST API service 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 : Parse the configuration file ( server & credentials) toml MongoDB : Request router and dispatcher for matching incoming requests to their respective handler mux : driver mgo MongoDB 3 — API structure Once the dependencies are installed, we create a file called “ “, with the following content: app.go The code above creates a controller for each endpoint, then expose an on port . HTTP server 3000 Note: We are using , , , and where appropriate. We are also defining parameters that can be passed in GET POST PUT DELETE To run the server in local, type the following command: go run app.go If you point your browser to , you should see: http://localhost:3000/movies 4 — Model Now that we have a minimal application, it’s time to create a basic model. In , we use keyword to create a model: Movie Go struct Next, we will create the to manage database operations. Data Access Object 5 — Data Access Object 5.1 — Establish Connection The method as its name implies, establish a connection to connect() MongoDB database. 5.2 — Database Queries The implementation is relatively straighforward and just includes issuing right method using object and returning the results. These methods can be implemented as follows: db.C(COLLECTION) 6 — Setup API Endpoints 6.1 — Create a Movie Update the method as follows: CreateMovieEndpoint It decodes the request body into a object, assign it an , and uses the method to create a in database. movie ID DAO Insert movie Let’s test it out: With : Postman With cURL curl -sSX POST -d ‘{“name”:”dunkirk”,”cover_image”:” ", “description”:”world war 2 movie”}’ | jq ‘.’ https://image.tmdb.org/t/p/w640/cUqEgoP6kj8ykfNjJx3Tl5zHCcN.jpg http://localhost:3000/movies 6.2 — List of Movies The code below is self explanatory: It uses method of to fetch list of movies from database. FindAll DAO Let’s test it out: With : Postman With : cURL curl -sSX GET | jq ‘.’ http://localhost:3000/movies 6.3 — Find a Movie We will use the library to get the parameters that the users passed in with the request: mux Let’s test it out: With : Postman With : cURL curl -sSX GET | jq ‘.’ http://localhost:3000/movies/599570faf0429b4494cfa5d4 6.4 — Update an existing Movie Update the method as follows: UpdateMovieEndPoint Let’s test it out: With : Postman With : cURL curl -sSX PUT -d ‘{“name”:”dunkirk”,”cover_image”:” ", “description”:”world war 2 movie”}’ | jq ‘.’ https://image.tmdb.org/t/p/w640/cUqEgoP6kj8ykfNjJx3Tl5zHCcN.jpg http://localhost:3000/movies 6.5 — Delete an existing Movie Update the method as follows: DeleteMovieEndPoint Let’s test it out: With : Postman With : cURL curl -sSX DELETE -d ‘{“name”:”dunkirk”,”cover_image”:” ", “description”:”world war 2 movie”}’ | jq ‘.’ https://image.tmdb.org/t/p/w640/cUqEgoP6kj8ykfNjJx3Tl5zHCcN.jpg http://localhost:3000/movies Taking this further ? On my upcoming posts, I will show you how : Write in for each Endpoint Unit Tests Go Build a UI in Angular 4 Setup a with CI/CD CircleCI Deploy the stack on and much more … AWS So stay tuned !
Share Your Thoughts