···88 "time"
991010 "github.com/eagleusb/proxycon/internal/types"
1111+ "github.com/eagleusb/proxycon/internal/utility"
1112)
12131314// averageEndpoint is the handler function for `GET /average?start=<timestamp>&end=<timestamp>`.
1414-func (h *HttpServer) averageEndpoint(w http.ResponseWriter, r *http.Request) {
1515+func (h *handler) averageEndpoint(w http.ResponseWriter, r *http.Request) {
1516 startStr := r.URL.Query().Get("start")
1617 endStr := r.URL.Query().Get("end")
17181819 if startStr == "" || endStr == "" {
1919- writeJSONError(w, "missing query parameters: start and end are required", http.StatusBadRequest)
2020+ utility.WriteJSONError(w, "missing query parameters: start and end are required", http.StatusBadRequest)
2021 return
2122 }
22232324 startTime, err := time.Parse(time.RFC3339, startStr)
2425 if err != nil {
2525- writeJSONError(w, "invalid start timestamp: must be RFC3339 format", http.StatusBadRequest)
2626+ utility.WriteJSONError(w, "invalid start timestamp: must be RFC3339 format", http.StatusBadRequest)
2627 return
2728 }
28292930 endTime, err := time.Parse(time.RFC3339, endStr)
3031 if err != nil {
3131- writeJSONError(w, "invalid end timestamp: must be RFC3339 format", http.StatusBadRequest)
3232+ utility.WriteJSONError(w, "invalid end timestamp: must be RFC3339 format", http.StatusBadRequest)
3233 return
3334 }
34353536 if startTime.After(endTime) {
3636- writeJSONError(w, "start timestamp must be before end timestamp", http.StatusBadRequest)
3737+ utility.WriteJSONError(w, "start timestamp must be before end timestamp", http.StatusBadRequest)
3738 return
3839 }
3940···5152 }
52535354 if len(matched) == 0 {
5454- writeJSONError(w, "no prices found in the given range", http.StatusNotFound)
5555+ utility.WriteJSONError(w, "no prices found in the given range", http.StatusNotFound)
5556 return
5657 }
5758
···11+package handlers
22+33+import (
44+ "net/http"
55+66+ "github.com/eagleusb/proxycon/internal/data"
77+)
88+99+// handler holds dependencies for HTTP handlers.
1010+type handler struct {
1111+ datasource *data.BitcoinPrices
1212+ pageSize int
1313+}
1414+1515+// Register registers all API handlers on the given mux.
1616+func Register(mux *http.ServeMux, datasource *data.BitcoinPrices, pageSize int) {
1717+ h := &handler{
1818+ datasource: datasource,
1919+ pageSize: pageSize,
2020+ }
2121+ mux.HandleFunc("GET /list", h.listEndpoint)
2222+ mux.HandleFunc("GET /price", h.priceEndpoint)
2323+ mux.HandleFunc("GET /average", h.averageEndpoint)
2424+}
+3-2
internal/handlers/list.go
···77 "strconv"
8899 "github.com/eagleusb/proxycon/internal/types"
1010+ "github.com/eagleusb/proxycon/internal/utility"
1011)
11121213// listEndpoint is the handler function for `GET /list`.
1313-func (h *HttpServer) listEndpoint(w http.ResponseWriter, r *http.Request) {
1414+func (h *handler) listEndpoint(w http.ResponseWriter, r *http.Request) {
1415 cursor := 0
15161617 if c := r.URL.Query().Get("cursor"); c != "" {
1718 parsed, err := strconv.Atoi(c)
1819 if err != nil || parsed < 0 {
1919- writeJSONError(w, "invalid cursor", http.StatusBadRequest)
2020+ utility.WriteJSONError(w, "invalid cursor", http.StatusBadRequest)
2021 return
2122 }
2223 cursor = parsed
+5-4
internal/handlers/price.go
···77 "time"
8899 "github.com/eagleusb/proxycon/internal/types"
1010+ "github.com/eagleusb/proxycon/internal/utility"
1011)
11121213// priceEndpoint is the handler function for `GET /price?at=<timestamp>`.
1313-func (h *HttpServer) priceEndpoint(w http.ResponseWriter, r *http.Request) {
1414+func (h *handler) priceEndpoint(w http.ResponseWriter, r *http.Request) {
1415 at := r.URL.Query().Get("at")
1516 if at == "" {
1616- writeJSONError(w, "missing query parameter: at", http.StatusBadRequest)
1717+ utility.WriteJSONError(w, "missing query parameter: at", http.StatusBadRequest)
1718 return
1819 }
19202021 if _, err := time.Parse(time.RFC3339, at); err != nil {
2121- writeJSONError(w, "invalid timestamp: must be RFC3339 format", http.StatusBadRequest)
2222+ utility.WriteJSONError(w, "invalid timestamp: must be RFC3339 format", http.StatusBadRequest)
2223 return
2324 }
2425···3839 }
3940 }
40414141- writeJSONError(w, "no price found for the given timestamp", http.StatusNotFound)
4242+ utility.WriteJSONError(w, "no price found for the given timestamp", http.StatusNotFound)
4243}