this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(api) add price endpoint handler

eagleusb 70417208 2fb65059

+42
+42
internal/handlers/price.go
··· 1 + package handlers 2 + 3 + import ( 4 + "encoding/json" 5 + "log/slog" 6 + "net/http" 7 + "time" 8 + 9 + "github.com/eagleusb/proxycon/internal/types" 10 + ) 11 + 12 + // priceEndpoint is the handler function for `GET /price?at=<timestamp>` 13 + func (h *HttpServer) priceEndpoint(w http.ResponseWriter, r *http.Request) { 14 + at := r.URL.Query().Get("at") 15 + if at == "" { 16 + http.Error(w, "missing query parameter: at", http.StatusBadRequest) 17 + return 18 + } 19 + 20 + if _, err := time.Parse(time.RFC3339, at); err != nil { 21 + http.Error(w, "invalid timestamp: must be RFC3339 format", http.StatusBadRequest) 22 + return 23 + } 24 + 25 + prices := h.datasource.GetPrices() 26 + 27 + for _, p := range prices { 28 + if p.Timestamp == at { 29 + w.Header().Set("Content-Type", "application/json") 30 + if err := json.NewEncoder(w).Encode(types.PriceResponse{ 31 + Data: []types.Entry{ 32 + {Amount: p.Price, Timestamp: p.Timestamp}, 33 + }, 34 + }); err != nil { 35 + slog.Error("encode price response", "error", err) 36 + } 37 + return 38 + } 39 + } 40 + 41 + http.Error(w, "no price found for the given timestamp", http.StatusNotFound) 42 + }