| 1 |
package health |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/json" |
| 5 |
"net/http" |
| 6 |
"sync" |
| 7 |
) |
| 8 |
|
| 9 |
var ( |
| 10 |
handler = &healthHandler{ |
| 11 |
useJSON: false, |
| 12 |
status: Up, |
| 13 |
} |
| 14 |
) |
| 15 |
|
| 16 |
|
| 17 |
type responseBody struct { |
| 18 |
Status string `json:"status"` |
| 19 |
Reason string `json:"reason,omitempty"` |
| 20 |
} |
| 21 |
|
| 22 |
|
| 23 |
type healthHandler struct { |
| 24 |
useJSON bool |
| 25 |
|
| 26 |
status Status |
| 27 |
reason string |
| 28 |
|
| 29 |
mutex sync.RWMutex |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
func (h *healthHandler) WithJSON(v bool) *healthHandler { |
| 36 |
h.useJSON = v |
| 37 |
return h |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
func (h *healthHandler) ServeHTTP(writer http.ResponseWriter, _ *http.Request) { |
| 42 |
statusCode, body, useJSON := h.getResponseStatusCodeAndBodyAndWhetherBodyUsesJSON() |
| 43 |
if useJSON { |
| 44 |
writer.Header().Set("Content-Type", "application/json") |
| 45 |
} |
| 46 |
writer.WriteHeader(statusCode) |
| 47 |
_, _ = writer.Write(body) |
| 48 |
} |
| 49 |
|
| 50 |
func (h *healthHandler) GetResponseStatusCodeAndBody() (statusCode int, body []byte) { |
| 51 |
statusCode, body, _ = h.getResponseStatusCodeAndBodyAndWhetherBodyUsesJSON() |
| 52 |
return statusCode, body |
| 53 |
} |
| 54 |
|
| 55 |
func (h *healthHandler) getResponseStatusCodeAndBodyAndWhetherBodyUsesJSON() (statusCode int, body []byte, useJSON bool) { |
| 56 |
var status Status |
| 57 |
var reason string |
| 58 |
h.mutex.RLock() |
| 59 |
status, reason, useJSON = h.status, h.reason, h.useJSON |
| 60 |
h.mutex.RUnlock() |
| 61 |
if status == Up { |
| 62 |
statusCode = http.StatusOK |
| 63 |
} else { |
| 64 |
statusCode = http.StatusInternalServerError |
| 65 |
} |
| 66 |
if useJSON { |
| 67 |
|
| 68 |
body, _ = json.Marshal(responseBody{Status: string(status), Reason: reason}) |
| 69 |
} else { |
| 70 |
if len(reason) == 0 { |
| 71 |
body = []byte(status) |
| 72 |
} else { |
| 73 |
body = []byte(string(status) + ": " + reason) |
| 74 |
} |
| 75 |
} |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func Handler() *healthHandler { |
| 81 |
return handler |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
func GetStatus() Status { |
| 86 |
handler.mutex.RLock() |
| 87 |
defer handler.mutex.RUnlock() |
| 88 |
return handler.status |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
func SetStatus(status Status) { |
| 93 |
handler.mutex.Lock() |
| 94 |
handler.status = status |
| 95 |
handler.mutex.Unlock() |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
func GetReason() string { |
| 100 |
handler.mutex.RLock() |
| 101 |
defer handler.mutex.RUnlock() |
| 102 |
return handler.reason |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func SetReason(reason string) { |
| 107 |
handler.mutex.Lock() |
| 108 |
handler.reason = reason |
| 109 |
handler.mutex.Unlock() |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func SetStatusAndReason(status Status, reason string) { |
| 114 |
handler.mutex.Lock() |
| 115 |
handler.status = status |
| 116 |
handler.reason = reason |
| 117 |
handler.mutex.Unlock() |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
func SetStatusAndResetReason(status Status) { |
| 122 |
handler.mutex.Lock() |
| 123 |
handler.status = status |
| 124 |
handler.reason = "" |
| 125 |
handler.mutex.Unlock() |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
func SetHealthy() { |
| 130 |
SetStatusAndResetReason(Up) |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
func SetUnhealthy(reason string) { |
| 138 |
handler.mutex.Lock() |
| 139 |
handler.status = Down |
| 140 |
handler.reason = reason |
| 141 |
handler.mutex.Unlock() |
| 142 |
} |