forked from robustirc/robustirc
/
status.go
202 lines (181 loc) · 4.62 KB
/
status.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package main
import (
"encoding/json"
"log"
"net/http"
"strconv"
"time"
"github.com/robustirc/robustirc/config"
"github.com/robustirc/robustirc/ircserver"
"github.com/robustirc/robustirc/types"
"github.com/hashicorp/raft"
)
//go:generate go run gentmpl.go status irclog
func handleStatus(res http.ResponseWriter, req *http.Request) {
p, _ := peerStore.Peers()
// robustirc-rollingrestart wants a machine-readable version of the status.
if req.Header.Get("Accept") == "application/json" {
type jsonStatus struct {
State string
Leader string
Peers []string
AppliedIndex uint64
CommitIndex uint64
LastContact time.Time
ExecutableHash string
CurrentTime time.Time
}
res.Header().Set("Content-Type", "application/json")
leaderStr := node.Leader()
stats := node.Stats()
appliedIndex, err := strconv.ParseUint(stats["applied_index"], 0, 64)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
commitIndex, err := strconv.ParseUint(stats["commit_index"], 0, 64)
if err != nil {
http.Error(res, err.Error(), http.StatusInternalServerError)
return
}
if err := json.NewEncoder(res).Encode(jsonStatus{
State: node.State().String(),
Leader: leaderStr,
AppliedIndex: appliedIndex,
CommitIndex: commitIndex,
Peers: p,
LastContact: node.LastContact(),
ExecutableHash: executablehash,
CurrentTime: time.Now(),
}); err != nil {
log.Printf("%v\n", err)
http.Error(res, err.Error(), http.StatusInternalServerError)
}
return
}
lo, err := ircStore.FirstIndex()
if err != nil {
log.Printf("Could not get first index: %v", err)
http.Error(res, "internal error", 500)
return
}
hi, err := ircStore.LastIndex()
if err != nil {
log.Printf("Could not get last index: %v", err)
http.Error(res, "internal error", 500)
return
}
// Show the last 50 messages by default.
if hi > 50 && hi-50 > lo {
lo = hi - 50
}
if offsetStr := req.FormValue("offset"); offsetStr != "" {
offset, err := strconv.ParseInt(offsetStr, 0, 64)
if err != nil {
http.Error(res, err.Error(), http.StatusBadRequest)
return
}
lo = uint64(offset)
}
if hi > lo+50 {
hi = lo + 50
}
var entries []*raft.Log
if lo != 0 && hi != 0 {
for i := lo; i <= hi; i++ {
l := new(raft.Log)
if err := ircStore.GetLog(i, l); err != nil {
// Not every message goes into the ircStore (e.g. raft peer change
// messages do not).
continue
}
if l.Type == raft.LogCommand {
msg := types.NewRobustMessageFromBytes(l.Data)
msg.Data = msg.PrivacyFilter()
l.Data, _ = json.Marshal(&msg)
}
entries = append(entries, l)
}
}
prevOffset := int64(lo) - 50
if prevOffset < 0 {
prevOffset = 1
}
args := struct {
Addr string
State raft.RaftState
Leader string
Peers []string
First uint64
Last uint64
Entries []*raft.Log
Stats map[string]string
Sessions map[types.RobustId]ircserver.Session
GetMessageRequests map[string]GetMessageStats
PrevOffset int64
NextOffset uint64
NetConfig config.Network
}{
*peerAddr,
node.State(),
node.Leader(),
p,
lo,
hi,
entries,
node.Stats(),
ircServer.GetSessions(),
GetMessageRequests,
prevOffset,
lo + 50,
netConfig,
}
statusTpl.Execute(res, args)
}
func handleIrclog(w http.ResponseWriter, r *http.Request) {
id, err := strconv.ParseInt(r.FormValue("sessionid"), 0, 64)
if err != nil || id == 0 {
http.Error(w, "Invalid session", http.StatusBadRequest)
return
}
session := types.RobustId{Id: id}
// TODO(secure): pagination
var messages []*types.RobustMessage
first, _ := ircStore.FirstIndex()
last, _ := ircStore.LastIndex()
for idx := first; idx <= last; idx++ {
var elog raft.Log
if err := ircStore.GetLog(idx, &elog); err != nil {
// Not every message goes into the ircStore (e.g. raft peer change
// messages do not).
continue
}
if elog.Type != raft.LogCommand {
continue
}
msg := types.NewRobustMessageFromBytes(elog.Data)
if msg.Session.Id == session.Id {
messages = append(messages, &msg)
}
output, ok := ircServer.Get(msg.Id)
if ok {
for _, msg := range output {
if !msg.InterestingFor[session.Id] {
continue
}
messages = append(messages, msg)
}
}
}
args := struct {
Session types.RobustId
Messages []*types.RobustMessage
}{
session,
messages,
}
if err := irclogTpl.Execute(w, args); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}