A JSON API server
Armed with the information from the last section, it is possible to use the HTTP package to create services over HTTP. Earlier we discussed the perils of creating services using raw TCP directly when we created a server for our global monetary currency service. In this section, we explore how to create an API server for the same service using HTTP as the underlying protocol. The new HTTP-based service has the following design goals:
- Use HTTP as the transport protocol
- Use JSON for structured communication between client and server
- Clients query the server for currency information using JSON-formatted requests
- The server respond using JSON-formatted responses
The following shows the code involved in the implementation of the new service. This time, the server will use the curr1
package (see github.com/vladimirvivien/learning-go /ch11/curr1) to load and query ISO 4217 currency data from a local CSV file.
The code in the curr1 package defines two types, CurrencyRequest
and Currency...