TodoList App – Part 1

Reading Time: 3 minutes

Creating REST API with Go

This post will introduce how to write a REST API using Go. We’ll add the following components to this example,

  1. nosql database. We’ll  be using MongoDB and we’ll be using the free offering from MongoDB-Atlas.
  2. Add Authorization using JWT token.
  3. For the client we’ll also create a simple Android Application.

With all three components the whole application would give an introduction on how to develop a complete end to end solution.

Describe our service

We’ll create a simple to-do list application where the data could be synced to a web server. For this purpose let’s outline what we’ll need on the server side,

  1. Models for our database.
  2. Models for our external views. That’s what we’ll return as a response to an API request.
  3. API endpoints.

So let’s create our models first, this is shown below

type LoginType int

const (
    WebLogin = LoginType(iota)
    GoogleLogin
    FacebookLogin
    TwitterLogin
    GithubLogin
    GitlabLogin
)

type User struct {
    //This is a unique field.
    ID string `json:"id"`
    Meta map[string]interface{} `json:"extra" bson:"extra"`
    SignInType LoginType `json:"-" bson:"type"`
} 

In the above structure definition we’re creating a User Model. Some of the fields have json or bson tag. These can be used to control how the data is sent back ( json tag) and how the data is stored in MongoDB (bson tag).  

Since we want to keep the json keys all lower case we use the json tag to name them in lower case.

We can name then anything though and the response would contain that key name. This will be more clear as we write some more code on sending data back.

NOTE: Any field with lower name can’t be seen outside that package. 

The SignInType needs to be saved to database but doesn’t need to be sent back in the response which is why the json tag has a “-“ which would skip this field when Marshalling.

Describe a Todo Item

Let’s identify what needs to be saved to a TODO item. So we can have,

  1. A Name of item.
  2. An ID of item.
  3. Text or Images or a combination of both.
  4. Completion Time Remaining.
  5. Actions (Like Send an email)
type TodoItem struct {
    Owner string `json:"-"`
    Name string `json:"name"`
    Content map[string]interface{} `json:"content,omitempty"`
    Actions map[string]interface{} `json:"actions",omitempty`
    StartTime string `json:"start_time",omitempty`
    EndTime string `json:"end_time", omitempty`
} 

In the above structure we define what we would like to see in our TodoItem. You can see there are a couple of items which look exactly the same but serve different purpose.

Since we don’t know what would a TODO item contain, we create it to be a map of generic things. The same holds true about Actions.

Configuring MongoDB Atlas

MongoDB provides a very helpful feature to allow upto 500MB of data for free. This is great when doing small POC things like I’m right now. To see how to configure MongoDB check out my Connecting to MongoDB Atlas Cluster post. You can also install MongoDB locally and use that instead but having it on the cloud means more reachability and guaranteed uptime.

Write Server Code

The only thing that remains is writing the server handling of the API endpoints. We’ll write in the following order,

  1. Registration.
  2. Login.
  3. User Profile.
  4. Todo Addition.
  5. Todo Removal.
  6. Todo Editing.
import (
"net/http"
)

func main() {

http.HandleFunc("/login", Login)
http.HandleFunc("/register", Register)
http.HandleFunc("/user", User)
http.HandleFunc("/post/add", PostAdd)
http.HandleFunc("/post/remove", PostRemove)
http.HandleFunc("/post/edit", PostEdit)
} 

Instead of using any frameworks we’re going to write all server code using the provided net/http package. We don’t loose out anything and are fully in control of what goes on with each request.

In the above code I added some endpoints. Now all we need to do is make an http server listen on a well defined port. But before we do that let’s take a moment to think about how we’re going to deploy this thing.

Deploying server via Heroku

Heroku is a great way to deploy server side code. It works with a variety of sources viz C, Java, Haskell, Go etc. The nice part is that you can push your code to Heroku git repository and it automatically creates a CI/CD pipeline to deploy a new revision of your application. To do this very few steps are required in order to get started.

  • Create a Heroku account.
  • Clone the git repository Todo List REST api.
  • Create a Heroku project. Simply use the command heroku create after installing Heroku CLI.

You might’ve to change the project name to something more rememberable. You can do that from Heroku Website or using the CLI.

If you’re using TodoList Github then your browser should receive a 501 Not Implemented.

We’ll extend the functionality of this as we move ahead in this tutorial series.

Leave a Reply