@@ -19,36 +19,36 @@ import (
19
19
type status string
20
20
21
21
const (
22
- // pass healthy (acceptable aliases: "ok" to support Node's Terminus and "up" for Java's SpringBoot)
22
+ // Pass healthy (acceptable aliases: "ok" to support Node's Terminus and "up" for Java's SpringBoot)
23
23
// fail unhealthy (acceptable aliases: "error" to support Node's Terminus and "down" for Java's SpringBoot), and
24
24
// warn healthy, with some concerns.
25
25
//
26
26
// ref https://datatracker.ietf.org/doc/html/draft-inadarei-api-health-check#section-3.1
27
27
// status: (required) indicates whether the service status is acceptable
28
28
// or not. API publishers SHOULD use following values for the field:
29
29
// The value of the status field is case-insensitive and is tightly
30
- // related with the HTTP response code returned by the health endpoint.
31
- // For "pass" status, HTTP response code in the 2xx-3xx range MUST be
32
- // used. For "fail" status, HTTP response code in the 4xx-5xx range
30
+ // related with the HTTP Response code returned by the health endpoint.
31
+ // For "pass" status, HTTP Response code in the 2xx-3xx range MUST be
32
+ // used. For "fail" status, HTTP Response code in the 4xx-5xx range
33
33
// MUST be used. In case of the "warn" status, endpoints MUST return
34
34
// HTTP status in the 2xx-3xx range, and additional information SHOULD
35
- // be provided, utilizing optional fields of the response .
36
- pass status = "pass"
37
- fail status = "fail"
35
+ // be provided, utilizing optional fields of the Response .
36
+ Pass status = "pass"
37
+ Fail status = "fail"
38
38
warn status = "warn"
39
39
)
40
40
41
41
func (s status ) ToHTTPStatus () int {
42
- if s == pass || s == warn {
42
+ if s == Pass || s == warn {
43
43
return http .StatusOK
44
44
}
45
45
return http .StatusFailedDependency
46
46
}
47
47
48
48
type checks map [string ][]componentStatus
49
49
50
- // response is the data returned by the health endpoint, which will be marshaled to JSON format
51
- type response struct {
50
+ // Response is the data returned by the health endpoint, which will be marshaled to JSON format
51
+ type Response struct {
52
52
Status status `json:"status"`
53
53
Description string `json:"description"` // a human-friendly description of the service
54
54
Checks checks `json:"checks,omitempty"` // The Checks Object, should be omitted on installation route
@@ -65,8 +65,8 @@ type componentStatus struct {
65
65
66
66
// Check is the health check API handler
67
67
func Check (w http.ResponseWriter , r * http.Request ) {
68
- rsp := response {
69
- Status : pass ,
68
+ rsp := Response {
69
+ Status : Pass ,
70
70
Description : setting .AppName ,
71
71
Checks : make (checks ),
72
72
}
@@ -77,14 +77,15 @@ func Check(w http.ResponseWriter, r *http.Request) {
77
77
statuses = append (statuses , checkCache (rsp .Checks ))
78
78
}
79
79
for _ , s := range statuses {
80
- if s != pass {
81
- rsp .Status = fail
80
+ if s != Pass {
81
+ rsp .Status = Fail
82
82
break
83
83
}
84
84
}
85
85
86
86
data , _ := json .MarshalIndent (rsp , "" , " " )
87
87
w .Header ().Set ("Content-Type" , "application/json" )
88
+ w .Header ().Set ("Cache-Control" , "no-store" )
88
89
w .WriteHeader (rsp .Status .ToHTTPStatus ())
89
90
_ , _ = w .Write (data )
90
91
}
@@ -93,22 +94,22 @@ func Check(w http.ResponseWriter, r *http.Request) {
93
94
func checkDatabase (ctx context.Context , checks checks ) status {
94
95
st := componentStatus {}
95
96
if err := db .GetEngine (ctx ).Ping (); err != nil {
96
- st .Status = fail
97
+ st .Status = Fail
97
98
st .Time = getCheckTime ()
98
99
log .Error ("database ping failed with error: %v" , err )
99
100
} else {
100
- st .Status = pass
101
+ st .Status = Pass
101
102
st .Time = getCheckTime ()
102
103
}
103
104
104
- if setting .Database .Type .IsSQLite3 () && st .Status == pass {
105
+ if setting .Database .Type .IsSQLite3 () && st .Status == Pass {
105
106
if ! setting .EnableSQLite3 {
106
- st .Status = fail
107
+ st .Status = Fail
107
108
st .Time = getCheckTime ()
108
109
log .Error ("SQLite3 health check failed with error: %v" , "this Forgejo binary is built without SQLite3 enabled" )
109
110
} else {
110
111
if _ , err := os .Stat (setting .Database .Path ); err != nil {
111
- st .Status = fail
112
+ st .Status = Fail
112
113
st .Time = getCheckTime ()
113
114
log .Error ("SQLite3 file exists check failed with error: %v" , err )
114
115
}
@@ -123,11 +124,11 @@ func checkDatabase(ctx context.Context, checks checks) status {
123
124
func checkCache (checks checks ) status {
124
125
st := componentStatus {}
125
126
if err := cache .GetCache ().Ping (); err != nil {
126
- st .Status = fail
127
+ st .Status = Fail
127
128
st .Time = getCheckTime ()
128
129
log .Error ("cache ping failed with error: %v" , err )
129
130
} else {
130
- st .Status = pass
131
+ st .Status = Pass
131
132
st .Time = getCheckTime ()
132
133
}
133
134
checks ["cache:ping" ] = []componentStatus {st }
0 commit comments