2015-04-23 18:53:31 +00:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
2016-02-22 20:40:03 +00:00
|
|
|
"time"
|
2015-04-23 18:53:31 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleSysHealth(core *vault.Core) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
handleSysHealthGet(core, w, r)
|
|
|
|
default:
|
|
|
|
respondError(w, http.StatusMethodNotAllowed, nil)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleSysHealthGet(core *vault.Core, w http.ResponseWriter, r *http.Request) {
|
2015-07-03 00:49:35 +00:00
|
|
|
// Check if being a standby is allowed for the purpose of a 200 OK
|
|
|
|
_, standbyOK := r.URL.Query()["standbyok"]
|
|
|
|
|
2015-04-23 18:53:31 +00:00
|
|
|
// Check system status
|
|
|
|
sealed, _ := core.Sealed()
|
|
|
|
standby, _ := core.Standby()
|
|
|
|
init, err := core.Initialized()
|
|
|
|
if err != nil {
|
|
|
|
respondError(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine the status code
|
|
|
|
code := http.StatusOK
|
|
|
|
switch {
|
|
|
|
case !init:
|
|
|
|
code = http.StatusInternalServerError
|
|
|
|
case sealed:
|
|
|
|
code = http.StatusInternalServerError
|
2015-07-03 00:49:35 +00:00
|
|
|
case !standbyOK && standby:
|
2015-04-23 18:53:31 +00:00
|
|
|
code = 429 // Consul warning code
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the body
|
|
|
|
body := &HealthResponse{
|
2016-02-22 20:40:03 +00:00
|
|
|
Initialized: init,
|
|
|
|
Sealed: sealed,
|
|
|
|
Standby: standby,
|
|
|
|
ServerTimeUTC: time.Now().UTC().Unix(),
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Generate the response
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
enc := json.NewEncoder(w)
|
|
|
|
enc.Encode(body)
|
|
|
|
}
|
|
|
|
|
|
|
|
type HealthResponse struct {
|
2016-02-22 20:40:03 +00:00
|
|
|
Initialized bool `json:"initialized"`
|
|
|
|
Sealed bool `json:"sealed"`
|
|
|
|
Standby bool `json:"standby"`
|
|
|
|
ServerTimeUTC int64 `json:"server_time_utc"`
|
2015-04-23 18:53:31 +00:00
|
|
|
}
|