As a Go enthusiast, it’s great to see first class support for MongoDB in the form of an official MongoDB Go driver. We will learn how to use it by building a simple REST API for a good old CRUD-style app!

In this blog, will cover the following topics:

  • Walkthrough the application: CRUD operations.
  • Setup Azure Cosmos DB for MongoDB API.
  • Setup Azure App service and deploy the application to the cloud.
  • Take the REST API for a spin.

To follow along, you’re free to use a MongoDB cluster of your choice (Docker is probably the fastest/easiest option). I will be using Azure Cosmos DB, which is Microsoft’s globally distributed, multi-model database service that supports document, key-value, graph, and columnar data models. It implements the wire protocol for MongoDB making if possible for any MongoDB client driver (including the Go driver) that understands this protocol version to natively connect to Cosmos DB.

The application (API) will be deployed to Azure App Service. It enables you to build and host web apps, mobile back ends, and RESTful APIs in the programming language of your choice without managing infrastructure. You can use it on Linux to host web apps natively on Linux for supported application stacks with support for built-in Docker images for multiple languages such as Node.js, Java, Python etc.

Although we have a Go app in this example, we can still host it on App Service since it supports custom Docker images as well!

Overview

The application is a simple one that exposes a REST API to create, read, update and delete developer profiles with a GitHub ID, blog and skills.

As usual, the code is available on GitHub, but let’s walk through it real quick!

cowboy using computer on barrel

Here is the code layout:

The main CRUD operations are in the crud.go file within the api package. It contains implementation for create, read, update and delete operations.

The Create function gets a handle to the MongoDB collection and marshals the request body (JSON payload) into a struct (model.Developer). The struct is then inserted using the InsertOne function, the error is handled (not shown above) and an HTTP 201 is sent back in case of success.

Here is how the read operation works:

The FindOne function is used with a filter criteria of github_id, which is the partition key for the collection bson.M{githubIDAttribute: githubID}. If found, the result is converted to the struct and returned to the caller.

Fetching all developer profiles is similar

Find uses an empty bson document as a filter in order to list all the documents in the collection and the result is sent back to the caller in the form of a JSON array

FindOneAndReplace is used to update the a specific record. github_id is used as the filter criteria and the passed in JSON payload is the updated record.

Delete is accomplished using FindOneAndDelete which accepts the github_id as the filter criteria for the record to be deleted

Everything is tied together in main.go. It associates the CRUD operation handlers to HTTP routes, starts the server and also sets a graceful exit mechanism

Alright, with that aside, it’s time to provision the required services and deploy the application. Before that, let’s go over some of the pre-requisites

Prerequisites

Setup Azure Cosmos DB

You need to create an Azure Cosmos DB account with the MongoDB API support enabled along with a Database and Collection. You can use the Azure Portal or the Azure CLI.

Use the Azure Portal

Follow these steps:

Use Azure CLI

(The same commands can be used for Azure CLI or Azure Cloud Shell.)

Export the following environment variables:

Start by creating a new resource group under which you can place all your resources. Once you’re done, you can delete the resource group which in turn will delete all the services

Create an account (notice --kind MongoDB)

Create the database

Finally, create a collection within the database

Get the connection string and save it. You will be using it later.


Deploy to Azure App Service

Build a Docker Image for the App

This step is optional. You can use the pre-built image abhirockzz/mongodb-go-app, which I have made available on DockerHub

You can use the Dockerfile to build your own image and push it to a Docker registry (public/private) of your choice

Here is a tutorial which provides an example of how to use Azure Container Registry with Azure Web App Service


Setup Azure App Service

It’s time to deploy our app to the cloud. Let’s quickly do that using Azure App service. Start by creating an App Service Plan which defines a set of compute resources for our web app to run.

Refer to the documentation for details on App Service plans

The plan is associated with an SKU – just like Cognitive Services, you need to choose an SKU (pricing tier) for App Service as well. We will use a small tier (B1) for this example.

Accepted values are B1, B2, B3, D1, F1, FREE, P1V2, P2V2, P3V2, PC2, PC3, PC4, S1, S2, S3,
SHARED

documentation for az appservice plan create

Setup environment variables

please ensure that you use double-quotes ("") around the connection string value

Deploy the application

Add the environment variables as configuration settings required by the application

The API should be deployed in some time. When its complete, go ahead and give it a try!

Test the API

I have used curl for demonstration, but you can use any other tool of your choice

Get the endpoint/host name for the deployed app

Create a few developer profiles

Find a single developer profile using GitHub ID

If you search for a profile which does not exist

Fetch all dev profiles

Delete a developer profile

Confirm again

Once you’re done, you can delete the resource group to delete all the services

That’s all for this blog. I would love to have your feedback and suggestions!



Source link

Write A Comment