Integrate metrics support and refactor the code.

Two things really:
- Provide ability to emit metrics for API calls.
- Nicer code organization.
You could also use http.Error here:

`http.Error(w, "Internal Server Error", http.StatusInternalServerError)`