-
Notifications
You must be signed in to change notification settings - Fork 15
/
announce_handler.go
74 lines (65 loc) 路 1.71 KB
/
announce_handler.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
62
63
64
65
66
67
68
69
70
71
72
73
74
package adminserver
import (
"encoding/json"
"io"
"net/http"
"net/url"
)
func (s *Server) announceHandler(w http.ResponseWriter, r *http.Request) {
if !methodOK(w, r, http.MethodPost) {
return
}
adCid, err := s.e.PublishLatest(r.Context())
if err != nil {
log.Errorw("Could not republish latest advertisement", "err", err)
if adCid.Defined() {
http.Error(w, err.Error(), http.StatusBadGateway)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
// Respond with successful announce result.
resp := &AnnounceRes{adCid}
respond(w, http.StatusOK, resp)
}
func (s *Server) announceHttpHandler(w http.ResponseWriter, r *http.Request) {
if !methodOK(w, r, http.MethodPost) {
return
}
body, err := io.ReadAll(r.Body)
if err != nil {
log.Errorw("failed reading import cidlist request", "err", err)
http.Error(w, "", http.StatusBadRequest)
return
}
var params map[string][]byte
err = json.Unmarshal(body, ¶ms)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
indexer, ok := params["indexer"]
if !ok {
http.Error(w, "missing indexer url in request", http.StatusBadRequest)
return
}
indexerURL, err := url.Parse(string(indexer))
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
adCid, err := s.e.PublishLatestHTTP(r.Context(), indexerURL)
if err != nil {
log.Errorw("Could not publish latest advertisement via http", "err", err)
if adCid.Defined() {
http.Error(w, err.Error(), http.StatusBadGateway)
} else {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
return
}
// Respond with successful announce result.
resp := &AnnounceRes{adCid}
respond(w, http.StatusOK, resp)
}