Skip to content

Commit bd93f87

Browse files
committed
add leader information into HTTP status endpoint
Signed-off-by: Andrei Kvapil <[email protected]>
1 parent a91ff9b commit bd93f87

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

main.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -329,15 +329,21 @@ func (el *AwaitElection) startStatusEndpoint(ctx context.Context) {
329329
serveMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
330330
resp, err := el.EtcdClient.Get(ctx, el.LockName)
331331
if err != nil || len(resp.Kvs) == 0 {
332-
writer.Write([]byte("{\"status\": \"ok\", \"phase\": \"awaiting\"}\n"))
332+
writer.Header().Set("Content-Type", "text/plain")
333+
writer.WriteHeader(http.StatusOK)
334+
writer.Write([]byte(fmt.Sprintf("{\"status\": \"ok\", \"phase\": \"awaiting\", \"leader\": \"unknown\"}\n")))
333335
return
334336
}
335-
if string(resp.Kvs[0].Value) == el.LeaderIdentity {
337+
338+
currentLeader := string(resp.Kvs[0].Value)
339+
if currentLeader == el.LeaderIdentity {
336340
writer.Header().Set("Content-Type", "application/json")
337341
writer.WriteHeader(http.StatusOK)
338-
writer.Write([]byte("{\"status\": \"ok\", \"phase\": \"running\"}\n"))
342+
writer.Write([]byte(fmt.Sprintf("{\"status\": \"ok\", \"phase\": \"running\", \"leader\": \"%s\"}\n", currentLeader)))
339343
} else {
340-
writer.Write([]byte("{\"status\": \"ok\", \"phase\": \"awaiting\"}\n"))
344+
writer.Header().Set("Content-Type", "application/json")
345+
writer.WriteHeader(http.StatusOK)
346+
writer.Write([]byte(fmt.Sprintf("{\"status\": \"ok\", \"phase\": \"awaiting\", \"leader\": \"%s\"}\n", currentLeader)))
341347
}
342348
})
343349

0 commit comments

Comments
 (0)