-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers.go
61 lines (52 loc) · 1.44 KB
/
providers.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 main
import (
"encoding/json"
"net/http"
"path"
"github.com/libp2p/go-libp2p/core/peer"
)
func (s *server) providers(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.Header().Set("Allow", http.MethodGet)
http.Error(w, "", http.StatusMethodNotAllowed)
return
}
pinfos := s.pcache.List()
// Write out combined.
//
// Note that /providers never returns 404. Instead, when there are no
// providers, an empty JSON array is returned.
outData, err := json.Marshal(pinfos)
if err != nil {
log.Warnw("failed marshal response", "err", err)
http.Error(w, "", http.StatusInternalServerError)
return
}
writeJsonResponse(w, http.StatusOK, outData)
}
// provider returns most recent state of a single provider.
func (s *server) provider(w http.ResponseWriter, r *http.Request) {
pid, err := peer.Decode(path.Base(r.URL.Path))
if err != nil {
log.Warnw("bad provider ID", "err", err)
http.Error(w, "", http.StatusBadRequest)
return
}
pinfo, err := s.pcache.Get(r.Context(), pid)
if err != nil {
log.Warnw("count not get provider information", "err", err)
http.Error(w, "", http.StatusInternalServerError)
return
}
if pinfo == nil {
http.Error(w, "", http.StatusNotFound)
return
}
outData, err := json.Marshal(pinfo)
if err != nil {
log.Warnw("failed marshal response", "err", err)
http.Error(w, "", http.StatusInternalServerError)
return
}
writeJsonResponse(w, http.StatusOK, outData)
}