Summary

In this chapter, we exposed the data for our social polling solution through a highly scalable RESTful API and built a simple website that consumes the API to provide an intuitive way for users to interact with it. The website consists of static content only, with no server-side processing (since the API does the heavy lifting for us). This allows us to host the website very cheaply on static hosting sites such as bitballoon.com, or to distribute the files to content delivery networks.

Within our API service, we learned how to share data between handlers without breaking or obfuscating the handler pattern from the standard library. We also saw how writing wrapped handler functions allows us to build a pipeline of functionality in a very simple and intuitive way.

We wrote some basic encoding and decoding functions that—while only simply wrapping their counterparts from the encoding/json package for now—could be improved later to support a range of different data representations without changing the internal interface to our code. We wrote a few simple helper functions that make responding to data requests easy, while providing the same kind of abstraction that would allow us to evolve our API later.

We saw how, for simple cases, switching on to HTTP methods is an elegant way to support many functions for a single endpoint. We also saw how, with a few extra lines of code, we are able to build in support for CORS to allow applications running on different domains to interact with our services—without the need for hacks like JSONP.

The code in this chapter, combined with the work we did in the previous chapter, provides a real-world, production-ready solution that implements the following flow:

  1. The user clicks on the Create Poll button on the website, and enters the title and options for a poll.
  2. The JavaScript running in the browser encodes the data as a JSON string and sends it in the body of a POST request to our API.
  3. The API receives the request, and after validating the API key, setting up a database session, and storing it in our variables map, calls the handlePolls function that processes the request and stores the new poll in the MongoDB database.
  4. The API redirects the user to the view.html page for the newly created poll.
  5. Meanwhile, the twittervotes program loads all polls from the database, including the new one, and opens a connection to Twitter filtering on the hashtags that represent options from the polls.
  6. As votes come in, twittervotes pushes them to NSQ.
  7. The counter program is listening in on the appropriate channel and notices the votes coming in, counting each one and periodically making updates to the database.
  8. The user sees the results displayed (and refreshed) on the view.html page as the website continually makes GET requests to the API endpoint for the selected poll.

In the next chapter, we will evolve our API and web skills to build a brand new start-up app called Meander. We'll see how we can write a full, static web server in just a few lines of Go code, and explore an interesting way of representing enumerators in a language that doesn't officially support them!

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

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