Skip to content

Commit

Permalink
add leader information into HTTP status endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: Andrei Kvapil <[email protected]>
  • Loading branch information
kvaps committed Jul 12, 2024
1 parent a91ff9b commit bd93f87
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,21 @@ func (el *AwaitElection) startStatusEndpoint(ctx context.Context) {
serveMux.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
resp, err := el.EtcdClient.Get(ctx, el.LockName)
if err != nil || len(resp.Kvs) == 0 {
writer.Write([]byte("{\"status\": \"ok\", \"phase\": \"awaiting\"}\n"))
writer.Header().Set("Content-Type", "text/plain")
writer.WriteHeader(http.StatusOK)
writer.Write([]byte(fmt.Sprintf("{\"status\": \"ok\", \"phase\": \"awaiting\", \"leader\": \"unknown\"}\n")))
return
}
if string(resp.Kvs[0].Value) == el.LeaderIdentity {

currentLeader := string(resp.Kvs[0].Value)
if currentLeader == el.LeaderIdentity {
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
writer.Write([]byte("{\"status\": \"ok\", \"phase\": \"running\"}\n"))
writer.Write([]byte(fmt.Sprintf("{\"status\": \"ok\", \"phase\": \"running\", \"leader\": \"%s\"}\n", currentLeader)))
} else {
writer.Write([]byte("{\"status\": \"ok\", \"phase\": \"awaiting\"}\n"))
writer.Header().Set("Content-Type", "application/json")
writer.WriteHeader(http.StatusOK)
writer.Write([]byte(fmt.Sprintf("{\"status\": \"ok\", \"phase\": \"awaiting\", \"leader\": \"%s\"}\n", currentLeader)))
}
})

Expand Down

0 comments on commit bd93f87

Please sign in to comment.