go-restful, a framework for REST API creation

go-restful is a package for building REST-style web services in Go. REST, as we discussed in the preceding section, asks developers to follow a set of design protocols. We have already discussed how the REST verbs should be defined and what they do to the resources.

Using go-restful, we can separate the logic for API handlers and attach REST verbs. The benefit of this is that it clearly tells us by looking at the code what API we are creating. Before jumping into an example, we need to install a database called SQLite3 for our REST API with go-restful. The installation steps are as follows:

  • On Ubuntu, run this command:
      apt-get install sqlite3 libsqlite3-dev
  • On OS X, you can use the brew command to install SQLite3:
      brew install sqlite3
  • Now, install the go-restful package with the following get command:
      go get github.com/emicklei/go-restful

We are ready to go. First, let us write a simple program showing what go-restful can do in a few lines of code. Let us create a simple ping server that echoes the server time back to the client:

package main
import (
"fmt"
"github.com/emicklei/go-restful"
"io"
"net/http"
"time"
)
func main() {
// Create a web service
webservice := new(restful.WebService)
// Create a route and attach it to handler in the service
webservice.Route(webservice.GET("/ping").To(pingTime))
// Add the service to application
restful.Add(webservice)
http.ListenAndServe(":8000", nil)
}
func pingTime(req *restful.Request, resp *restful.Response) {
// Write to the response
io.WriteString(resp, fmt.Sprintf("%s", time.Now()))
}

If we run this program:

go run basicExample.go

The server will be running on port 8000 of localhost. So, we can either make a curl request or use a browser to see the GET request output:

curl -X GET "http://localhost:8000/ping"
2017-06-06 07:37:26.238146296 +0530 IST

In the preceding program, we imported the go-restful library and we created a new service using a new instance of the restful.WebService struct. Next, we can create a REST verb using the following statement: 

webservice.GET("/ping")

We can attach a function handler to execute this verb; pingTime is one such function. These chained functions are passed to a Route function to create a router. Then comes the following important statement:

restful.Add(webservice)

This registers the newly created webservice with the go-restful. If you observe, we are not passing any ServeMux objects to the http.ListenServe function; go-restful will take care of it. The main concept here is to use the resource-based REST API creation in go-restful. Going from the basic example, let us build something practical.

Take a scenario where your city is getting a new Metro Rail and you need to develop a REST API for other developers to consume and create an app accordingly. We will create one such API in this chapter and use various frameworks to show the implementation. Before that, for Create, Read, Update, Delete (CRUD) operations, we should know how to query or insert them into the SQLite DB with Go code.

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

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