Updating the Phone Book Application
Let’s enhance the phone book application to work as a web service.
We'll cover the following
This time, the phone book application is going to work as a web service. The two main tasks that need to be performed are defining the API along with the endpoints and implementing the API. A third task that needs to be determined concerns data exchange between the application server and its clients. There exist four main approaches regarding data exchange:
Using plain text
Using HTML
Using a hybrid approach that combines plain text and JSON data
HTML might not be the best option for a service because we need to separate the data from the HTML tags and parse the data, we are going to use the first approach. Therefore, the service is going to work with plaintext data. We begin by defining the API that supports the operation of the phone book application.
Defining the API
The API has support for the following URLs:
/list
: This lists all available entries./insert/name/surname/telephone/
: This inserts a new entry. Later on, we are going to see how to extract the desired information from a URL that contains user data./delete/telephone/
: This deletes an entry based on the value oftelephone
./search/telephone/
: This searches for an entry based on the value oftelephone
./status
: This is an extra URL that returns the number of entries in the phone book.
Note: The list of endpoints does not follow standard REST conventions.
This time we not using the default Go router, which means that we define and configure our own http.NewServeMux()
variable. This changes the way we provide handler functions: a handler function with the func(http.ResponseWriter, *http. Request)
signature has to be converted into an http.HandlerFunc
type and be used by the ServeMux
type and its own Handle()
method. Therefore, when using a different ServeMux
than the default one, we should do that conversion explicitly by calling http.HandlerFunc()
, which makes the http.HandlerFunc
type act as an adapter that allows the use of ordinary functions because HTTP handlers provided that they have the required signature. This is not a problem when using the default Go router (DefaultServeMux
) because the http.HandleFunc()
function does that conversion automatically and internally.
Note: To make things clearer, the
http.HandlerFunc
type has support for a method namedHandlerFunc()
—both the type and method are defined in thehttp
package. The similarly named thehttp.HandleFunc()
function (without anr
) is used with the default Go router.
As an example, for the /time
endpoint and the timeHandler()
handler function, we should call mux.Handle()
as mux.Handle("/time", http.HandlerFunc(timeHandler))
. If we were using http.HandleFunc()
and as a consequence DefaultServeMux
, then we should have called http.HandleFunc("/time", timeHandler)
instead.
Implementing the handlers
The new version of the phone book is going to be created on a dedicated GitHub repository for storing and sharing it. After creating the repository, we need to do the following:
Get hands-on with 1400+ tech skills courses.