Bank: HTTP REST API

The past few days have been busy. After the big rewrite I had enough done to start the HTTP REST API.

I tried to keep the implementation as simple as possible, staying away from big frameworks etc, so I landed on just using net/http and gorilla/mux. The Gorilla Mux is small enough to add minimal overhead abd be close enough to the metal.

Implementation

With this simplicity in mind, the implementation is easy.

Three new files were added, the first file sets up the server and creates a helper function to send responses down the line.

The second file sets up all the routes used in the application. The struct for a route is below:

type Route struct {
	Name        string
	Method      string
	Pattern     string
	HandlerFunc http.HandlerFunc
}

These routes are then added to an array and mapped back to callback functions.

The third file holds the callback functions. This checks for the token in X-Auth-Token header, and then pulls the relevant variables and passes them to the relevant function.

Routes

The functionality has already been implemented, so I just had to extract that into the routes for the new API. The current route list is as follows:

  • POST /auth - extend token
  • POST /auth/login - get token
  • POST /auth/account - create an auth account
  • DELETE /auth/account - remove an auth account
  • GET /account - get user’s account details
  • POST /account - create an account
  • GET /account/all - get all accounts (Note: this will be removed down the line as it poses a security risk)
  • GET /account/{accountId} - get a single account by identification number
  • POST /payment/credit - initiate payment
  • POST /payment/deposit - initiate deposit

These have been tested using Postman, and you can view and run the tests here.

These routes are available right now at https://thebankoftoday.com:8443, so feel free to play around.

Conclusion

This first implementation of the API is a great start. I’ll now be moving my attention to the iOS app, updating it to use the new HTTP API, and move away from the TCP CLI. I will also clean up and extend this API.

As always, the code is available on Github. If you’re keen to get involved or find out more, you can get in touch with me.