Updating the Phone Book Application

Let’s enhance the phone book application to work as a web service.

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 JSON

  • 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 of telephone.

  • /search/telephone/: This searches for an entry based on the value of telephone.

  • /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() ...

Get hands-on with 1400+ tech skills courses.