-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.go
79 lines (65 loc) · 1.85 KB
/
http.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
package autonotif
import (
"context"
"fmt"
"io"
"net/http"
"strconv"
"strings"
)
func (a *Autonotif) HealthHandler(w http.ResponseWriter, r *http.Request) {
err := a.HealthCheck()
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString(w, "NOT OK\n")
}
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "OK\n")
}
func (a *Autonotif) ForceLastIDHandler(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
chainType, lastID, ok := a.parseForceLastIDHeader(w, r)
if !ok {
return
}
err := a.ForceLastID(ctx, chainType, lastID)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
_, _ = io.WriteString(w, fmt.Sprintf("Error: %s\n", err.Error()))
return
}
_, _ = io.WriteString(w, fmt.Sprintf("%s: %d revoked successfully\n", chainType, lastID))
}
func (a *Autonotif) ForceLastID(ctx context.Context, chainType string, lastID int) error {
// start proposal from zero
if lastID == -1 {
lastID = 0
}
err := a.d.dsStore.RevokeLastID(ctx, chainType, lastID)
if err != nil {
return fmt.Errorf("dsStore.RevokeLastID: %s", err.Error())
}
return nil
}
func (a *Autonotif) parseForceLastIDHeader(w http.ResponseWriter, r *http.Request) (string, int, bool) {
chainType := r.Header.Get("chain")
if chainType == "" {
return handleBadRequest(w, "Invalid value: chain empty\n")
}
chainType = strings.ToLower(chainType)
_, ok := a.d.conf.ChainList[chainType]
if !ok {
handleBadRequest(w, "Invalid value: chain unknown\n")
}
lastIDStr := r.Header.Get("lastId")
lastID, err := strconv.Atoi(lastIDStr)
if err != nil || lastID == 0 {
return handleBadRequest(w, "Invalid value: lastId\n")
}
return chainType, lastID, true
}
func handleBadRequest(w http.ResponseWriter, msg string) (string, int, bool) {
w.WriteHeader(http.StatusBadRequest)
_, _ = io.WriteString(w, msg)
return "", 0, false
}