Specifying the endpoints for the frontend application

The following table lists the HTTP request types and endpoints that our frontend service needs to handle in order to implement all the features we described previously:

Request Type Path Description
GET / Displays the index page
GET /search?q=TERM Displays the first page of results for TERM
GET /search?q=TERM&offset=X Displays the results for TERM, starting from a particular offset
GET /submit/site Displays the site submission form
POST /submit/site Handles a site submission
ANY Any other path Displays a 404 page

 

To make our life easier, we will be using gorilla/mux as our preferred router. Creating the router and registering the endpoint handlers is as simple as using the following code:

To make the frontend service easier to test, the Service type stores a reference to the router. This way, we can use the httptest package primitives to perform HTTP requests directly at the mux without having to spin up any servers.