-
Notifications
You must be signed in to change notification settings - Fork 893
/
availability.go
61 lines (53 loc) · 1.63 KB
/
availability.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package gateway
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/gorilla/mux"
"github.com/celestiaorg/celestia-node/share"
)
const heightAvailabilityEndpoint = "/data_available"
// AvailabilityResponse represents the response to a
// `/data_available` request.
type AvailabilityResponse struct {
Available bool `json:"available"`
}
func (h *Handler) handleHeightAvailabilityRequest(w http.ResponseWriter, r *http.Request) {
heightStr := mux.Vars(r)[heightKey]
height, err := strconv.Atoi(heightStr)
if err != nil {
writeError(w, http.StatusBadRequest, heightAvailabilityEndpoint, err)
return
}
header, err := h.header.GetByHeight(r.Context(), uint64(height))
if err != nil {
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
return
}
err = h.share.SharesAvailable(r.Context(), header)
switch {
case err == nil:
resp, err := json.Marshal(&AvailabilityResponse{Available: true})
if err != nil {
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
return
}
_, werr := w.Write(resp)
if werr != nil {
log.Errorw("serving request", "endpoint", heightAvailabilityEndpoint, "err", err)
}
case errors.Is(err, share.ErrNotAvailable):
resp, err := json.Marshal(&AvailabilityResponse{Available: false})
if err != nil {
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
return
}
_, werr := w.Write(resp)
if werr != nil {
log.Errorw("serving request", "endpoint", heightAvailabilityEndpoint, "err", err)
}
default:
writeError(w, http.StatusInternalServerError, heightAvailabilityEndpoint, err)
}
}