Defining routes

The next step to implement a RESTful API is to define the different URLs that correspond to the different API actions we need to happen. This is also known as defining routes, since the URLs are the routes to our API resources.

We'll go through the RESTful API interactions one by one and define their routes. But first, let's start by creating a new file for our RESTful API.

In the backend/src folder, create a new folder called rest. Inside the rest folder, create a file called rest.go. This is where we start using the Gin framework. In your favorite Terminal, type the following command in order to deploy and install the Gin framework to your development environment:

go get -u github.com/gin-gonic/gin

In the rest.go file, start by declaring the package name and importing the Gin framework, as follows:

package rest

import (
"github.com/gin-gonic/gin"
)

Then, declare the function that will act as the entry point for our RESTful API. This is where we define the HTTP routes for our RESTful API:

func RunAPI(address string) error{
}

The preceding method takes one argument that will host the address of our RESTful API server. 

In order to make use of Gin, we first need to obtain a Gin engine. A Gin engine is the object type that gives us access to assign HTTP methods to URLs to action: 

func RunAPI(address string) error{
r := gin.Default()
}

Next, we need to start mapping HTTP methods to URLs to action. For that, we need to make use of the Gin engine object that we just created. The following code block is a simple example of where we use the Gin engine to accept a GET request coming to the relative URL, /relativepath/to/url:

func RunAPI(address string) error{
r := gin.Default()
r.GET("/relativepath/to/url", func(c *gin.Context) {
//take action
})
}

The anonymous function in the preceding code, func(c *gin.Context){}, is where we define the action that we want to be performed when we receive an incoming client request that satisfies our conditions (the /relativepath/to/url relative path, and the GET HTTP method).

The *gin.Context type is provided to us by the Gin framework. It supplies us with all the tools that we need to not only explore the incoming request, but also to take action and provide the appropriate response. We will discuss the *gin.Context type in more detail in the next section, but for now, let's focus on building the routes. Let's revisit the list of API interactions and write some code to represent each interaction:

  1. Our backend needs to provide a list of available products to the frontend using a GET request:
//get products
r.GET("/products",func(c *gin.Context) {
//return a list of all products to the client
}
)
  1. Our backend needs to provide a list of available promotions to the frontend using a GET request:
//get promos
r.GET("/promos",func(c *gin.Context) {
//return a list of all promotions to the client
}
)
  1. Our frontend needs to send user information to our backend in order to sign in existing users or to add new users using a POST request:
//post user sign in
r.POST("/users/signin", func(c *gin.Context) {
//sign in a user
}
)
//add user
r.POST("/users",func(c *gin.Context){
//add a user
}
)
  1. Our frontend needs to send user sign out requests to the backend using a POST request:
//post user sign out
/*
In the path below, our relative url needs to include the user id
Since the id will differ based on the user, the Gin framework allows us to include a wildcard. In Gin, the wildcard will take the form ':id' to indicate that we are expecting a parameter here with the name 'id'
*/
r.POST("/user/:id/signout",func(c *gin.Context) {
//sign out a user with the provided id
}
)
  1. Our backend needs to provide a list of existing orders belonging to a specific user using a GET request:
//get user orders
r.GET("/user/:id/orders", func(c *gin.Context) {
//get all orders belonging to the provided user id
}
)
  1. Our frontend needs to send credit card token information to the backend in order to process a charge:
//post purchase charge
r.POST("/users/charge", func(c *gin.Context) {
//charge credit card for user
}
)

Let's see how to build an HTTP handler in the next section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.144.65