-
Notifications
You must be signed in to change notification settings - Fork 1
/
handler.go
84 lines (69 loc) · 1.87 KB
/
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
75
76
77
78
79
80
81
82
83
84
package merkletree
import (
"encoding/json"
"fmt"
"net/http"
"github.com/go-chi/chi"
)
// Handler handles incoming requests for merkletree service
type Handler struct {
service *Service
}
// NewHandler returns a new handler
func NewHandler(service *Service) *Handler {
return &Handler{
service: service,
}
}
// AddHandler handlers requests to add items to the tree
func (h *Handler) AddHandler(w http.ResponseWriter, r *http.Request) {
var p = struct {
Credential string `json:"credential"`
Sender string `json:"sender"`
}{}
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
_, err = h.service.AddEntry(p.Credential, p.Sender)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, _ = fmt.Fprintf(w, "ok")
}
// RevokeHandler handles requests to revoke a claim
func (h *Handler) RevokeHandler(w http.ResponseWriter, r *http.Request) {
var p = struct {
Credential string `json:"credential"`
}{}
err := json.NewDecoder(r.Body).Decode(&p)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
err = h.service.RevokeEntry(p.Credential)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_, _ = fmt.Fprintf(w, "ok")
}
// GetProofHandler returns a proof for a given credential
func (h *Handler) GetProofHandler(w http.ResponseWriter, r *http.Request) {
credential := chi.URLParam(r, "credential")
proof, err := h.service.GenerateProof(credential)
if err != nil {
// TODO(walfly): make this more nuanced in the way it chooses a status code
http.Error(w, err.Error(), http.StatusNotFound)
return
}
js, err := json.Marshal(proof)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write(js)
}