Debian Codegraph @

health.go

raw symbols SCIP
1 package healthscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/
2
3 import (
4 "encoding/jsonscip-go gomod github.com/golang/go/src go1.19 `encoding/json`/"
5 "net/httpscip-go gomod github.com/golang/go/src go1.19 `net/http`/"
6 "syncscip-go gomod github.com/golang/go/src go1.19 sync/"
7 )
8
9 var (
10 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler. = &healthHandlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#{
11 useJSONscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#useJSON.: false,
12 statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#status.: Upscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Up.,
13 }
14 )
15
16 // responseBody is the body of the response returned by the health handler.
17 type responseBodyscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/responseBody# struct {
18 Statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/responseBody#Status. string `json:"status"`
19 Reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/responseBody#Reason. string `json:"reason,omitempty"`
20 }
21
22 // healthHandler is the HTTP handler for serving the health endpoint
23 type healthHandlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler# struct {
24 useJSONscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#useJSON. bool
25
26 statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#status. Statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Status#
27 reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#reason. string
28
29 mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex. syncscip-go gomod github.com/golang/go/src go1.19 sync/.RWMutexscip-go gomod github.com/golang/go/src go1.19 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 *healthHandlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#) WithJSONscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#WithJSON().(v bool) *healthHandlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler# {
36 h.useJSONscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#useJSON. = v
37 return h
38 }
39
40 // ServeHTTP serves the HTTP request for the health handler
41 func (h *healthHandlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#) ServeHTTPscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#ServeHTTP().(writer httpscip-go gomod github.com/golang/go/src go1.19 `net/http`/.ResponseWriterscip-go gomod github.com/golang/go/src go1.19 `net/http`/ResponseWriter#, _ *httpscip-go gomod github.com/golang/go/src go1.19 `net/http`/.Requestscip-go gomod github.com/golang/go/src go1.19 `net/http`/Request#) {
42 statusCode, body, useJSON := h.getResponseStatusCodeAndBodyAndWhetherBodyUsesJSONscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#getResponseStatusCodeAndBodyAndWhetherBodyUsesJSON().()
43 if useJSON {
44 writer.Headerscip-go gomod github.com/golang/go/src go1.19 `net/http`/ResponseWriter#Header().().Setscip-go gomod github.com/golang/go/src go1.19 `net/http`/Header#Set().("Content-Type", "application/json")
45 }
46 writer.WriteHeaderscip-go gomod github.com/golang/go/src go1.19 `net/http`/ResponseWriter#WriteHeader().(statusCode)
47 _, _ = writer.Writescip-go gomod github.com/golang/go/src go1.19 `net/http`/ResponseWriter#Write().(body)
48 }
49
50 func (h *healthHandlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#) GetResponseStatusCodeAndBodyscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#GetResponseStatusCodeAndBody().() (statusCode int, body []byte) {
51 statusCode, body, _ = h.getResponseStatusCodeAndBodyAndWhetherBodyUsesJSONscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#getResponseStatusCodeAndBodyAndWhetherBodyUsesJSON().()
52 return statusCode, body
53 }
54
55 func (h *healthHandlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#) getResponseStatusCodeAndBodyAndWhetherBodyUsesJSONscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#getResponseStatusCodeAndBodyAndWhetherBodyUsesJSON().() (statusCode int, body []byte, useJSON bool) {
56 var status Statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Status#
57 var reason string
58 h.mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..RLockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#RLock().()
59 status, reason, useJSON = h.statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#status., h.reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#reason., h.useJSONscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#useJSON.
60 h.mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..RUnlockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#RUnlock().()
61 if status == Upscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Up. {
62 statusCode = httpscip-go gomod github.com/golang/go/src go1.19 `net/http`/.StatusOKscip-go gomod github.com/golang/go/src go1.19 `net/http`/StatusOK.
63 } else {
64 statusCode = httpscip-go gomod github.com/golang/go/src go1.19 `net/http`/.StatusInternalServerErrorscip-go gomod github.com/golang/go/src go1.19 `net/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, _ = jsonscip-go gomod github.com/golang/go/src go1.19 `encoding/json`/.Marshalscip-go gomod github.com/golang/go/src go1.19 `encoding/json`/Marshal().(responseBodyscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/responseBody#{Statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/responseBody#Status.: string(status), Reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/responseBody#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 Handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Handler().() *healthHandlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler# {
81 return handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler.
82 }
83
84 // GetStatus retrieves the current status returned by the health handler
85 func GetStatusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/GetStatus().() Statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Status# {
86 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..RLockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#RLock().()
87 defer handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..RUnlockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#RUnlock().()
88 return handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#status.
89 }
90
91 // SetStatus sets the status to be returned by the health handler
92 func SetStatusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/SetStatus().(status Statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Status#) {
93 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Lockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Lock().()
94 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#status. = status
95 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Unlockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Unlock().()
96 }
97
98 // GetReason retrieves the current status returned by the health handler
99 func GetReasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/GetReason().() string {
100 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..RLockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#RLock().()
101 defer handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..RUnlockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#RUnlock().()
102 return handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#reason.
103 }
104
105 // SetReason sets a reason for the current status to be returned by the health handler
106 func SetReasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/SetReason().(reason string) {
107 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Lockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Lock().()
108 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#reason. = reason
109 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Unlockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Unlock().()
110 }
111
112 // SetStatusAndReason sets the status and reason to be returned by the health handler
113 func SetStatusAndReasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/SetStatusAndReason().(status Statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Status#, reason string) {
114 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Lockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Lock().()
115 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#status. = status
116 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#reason. = reason
117 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Unlockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Unlock().()
118 }
119
120 // SetStatusAndResetReason sets the status and resets the reason to a blank string
121 func SetStatusAndResetReasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/SetStatusAndResetReason().(status Statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Status#) {
122 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Lockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Lock().()
123 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#status. = status
124 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#reason. = ""
125 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Unlockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Unlock().()
126 }
127
128 // SetHealthy sets the status to Up and the reason to a blank string
129 func SetHealthyscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/SetHealthy().() {
130 SetStatusAndResetReasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/SetStatusAndResetReason().(Upscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/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 SetUnhealthyscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/SetUnhealthy().(reason string) {
138 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Lockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Lock().()
139 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..statusscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#status. = Downscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/Down.
140 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..reasonscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#reason. = reason
141 handlerscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/handler..mutexscip-go gomod github.com/TwiN/health . `github.com/TwiN/health`/healthHandler#mutex..Unlockscip-go gomod github.com/golang/go/src go1.19 sync/RWMutex#Unlock().()
142 }