Debian Codegraph @

health.go

raw symbols SCIP
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 // responseBody is the body of the response returned by the health handler.
17 type responseBody struct {
18 Status string `json:"status"`
19 Reason string `json:"reason,omitempty"`
20 }
21
22 // healthHandler is the HTTP handler for serving the health endpoint
23 type healthHandler struct {
24 useJSON bool
25
26 status Status
27 reason string
28
29 mutex sync.RWMutex
30 }
31
32 // WithJSON configures whether the handler should output a response in JSON or in raw text
33 //
34 // Defaults to false
35 func (h *healthHandler) WithJSON(v bool) *healthHandler {
36 h.useJSON = v
37 return h
38 }
39
40 // ServeHTTP serves the HTTP request for the health handler
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 // We can safely ignore the error here because we know that both values are strings, therefore are supported encoders.
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 // Handler retrieves the health handler
80 func Handler() *healthHandler {
81 return handler
82 }
83
84 // GetStatus retrieves the current status returned by the health handler
85 func GetStatus() Status {
86 handler.mutex.RLock()
87 defer handler.mutex.RUnlock()
88 return handler.status
89 }
90
91 // SetStatus sets the status to be returned by the health handler
92 func SetStatus(status Status) {
93 handler.mutex.Lock()
94 handler.status = status
95 handler.mutex.Unlock()
96 }
97
98 // GetReason retrieves the current status returned by the health handler
99 func GetReason() string {
100 handler.mutex.RLock()
101 defer handler.mutex.RUnlock()
102 return handler.reason
103 }
104
105 // SetReason sets a reason for the current status to be returned by the health handler
106 func SetReason(reason string) {
107 handler.mutex.Lock()
108 handler.reason = reason
109 handler.mutex.Unlock()
110 }
111
112 // SetStatusAndReason sets the status and reason to be returned by the health handler
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 // SetStatusAndResetReason sets the status and resets the reason to a blank string
121 func SetStatusAndResetReason(status Status) {
122 handler.mutex.Lock()
123 handler.status = status
124 handler.reason = ""
125 handler.mutex.Unlock()
126 }
127
128 // SetHealthy sets the status to Up and the reason to a blank string
129 func SetHealthy() {
130 SetStatusAndResetReason(Up)
131 }
132
133 // SetUnhealthy sets the status to Down and the reason to the string passed as parameter
134 //
135 // Unlike SetHealthy, this function enforces setting a reason, because it's good practice to give at least a bit
136 // of information as to why an application is unhealthy, and this library attempts to promote good practices.
137 func SetUnhealthy(reason string) {
138 handler.mutex.Lock()
139 handler.status = Down
140 handler.reason = reason
141 handler.mutex.Unlock()
142 }