diff options
author | Earl Warren <earl-warren@noreply.codeberg.org> | 2024-04-06 10:09:39 +0200 |
---|---|---|
committer | Earl Warren <earl-warren@noreply.codeberg.org> | 2024-04-06 10:09:39 +0200 |
commit | 3093f8765f0b27d1fd109d7e92c928216220bace (patch) | |
tree | 1f24944f2eae86049956bb3994bf2048ec1c9f2d /routers | |
parent | Merge pull request 'feat: improve nuget nuspec api' (#2996) from viceice/forg... (diff) | |
parent | Remove useless t.run (diff) | |
download | forgejo-3093f8765f0b27d1fd109d7e92c928216220bace.tar.xz forgejo-3093f8765f0b27d1fd109d7e92c928216220bace.zip |
Merge pull request 'Fix #3030 add Cache-Control header for health-check' (#3060) from adaaa/forgejo:cache-control-healthz into forgejo
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/3060
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Diffstat (limited to 'routers')
-rw-r--r-- | routers/web/healthcheck/check.go | 43 |
1 files changed, 22 insertions, 21 deletions
diff --git a/routers/web/healthcheck/check.go b/routers/web/healthcheck/check.go index d278ee1709..83dfe62537 100644 --- a/routers/web/healthcheck/check.go +++ b/routers/web/healthcheck/check.go @@ -19,7 +19,7 @@ import ( type status string const ( - // pass healthy (acceptable aliases: "ok" to support Node's Terminus and "up" for Java's SpringBoot) + // Pass healthy (acceptable aliases: "ok" to support Node's Terminus and "up" for Java's SpringBoot) // fail unhealthy (acceptable aliases: "error" to support Node's Terminus and "down" for Java's SpringBoot), and // warn healthy, with some concerns. // @@ -27,19 +27,19 @@ const ( // status: (required) indicates whether the service status is acceptable // or not. API publishers SHOULD use following values for the field: // The value of the status field is case-insensitive and is tightly - // related with the HTTP response code returned by the health endpoint. - // For "pass" status, HTTP response code in the 2xx-3xx range MUST be - // used. For "fail" status, HTTP response code in the 4xx-5xx range + // related with the HTTP Response code returned by the health endpoint. + // For "pass" status, HTTP Response code in the 2xx-3xx range MUST be + // used. For "fail" status, HTTP Response code in the 4xx-5xx range // MUST be used. In case of the "warn" status, endpoints MUST return // HTTP status in the 2xx-3xx range, and additional information SHOULD - // be provided, utilizing optional fields of the response. - pass status = "pass" - fail status = "fail" + // be provided, utilizing optional fields of the Response. + Pass status = "pass" + Fail status = "fail" warn status = "warn" ) func (s status) ToHTTPStatus() int { - if s == pass || s == warn { + if s == Pass || s == warn { return http.StatusOK } return http.StatusFailedDependency @@ -47,8 +47,8 @@ func (s status) ToHTTPStatus() int { type checks map[string][]componentStatus -// response is the data returned by the health endpoint, which will be marshaled to JSON format -type response struct { +// Response is the data returned by the health endpoint, which will be marshaled to JSON format +type Response struct { Status status `json:"status"` Description string `json:"description"` // a human-friendly description of the service Checks checks `json:"checks,omitempty"` // The Checks Object, should be omitted on installation route @@ -65,8 +65,8 @@ type componentStatus struct { // Check is the health check API handler func Check(w http.ResponseWriter, r *http.Request) { - rsp := response{ - Status: pass, + rsp := Response{ + Status: Pass, Description: setting.AppName, Checks: make(checks), } @@ -77,14 +77,15 @@ func Check(w http.ResponseWriter, r *http.Request) { statuses = append(statuses, checkCache(rsp.Checks)) } for _, s := range statuses { - if s != pass { - rsp.Status = fail + if s != Pass { + rsp.Status = Fail break } } data, _ := json.MarshalIndent(rsp, "", " ") w.Header().Set("Content-Type", "application/json") + w.Header().Set("Cache-Control", "no-store") w.WriteHeader(rsp.Status.ToHTTPStatus()) _, _ = w.Write(data) } @@ -93,22 +94,22 @@ func Check(w http.ResponseWriter, r *http.Request) { func checkDatabase(ctx context.Context, checks checks) status { st := componentStatus{} if err := db.GetEngine(ctx).Ping(); err != nil { - st.Status = fail + st.Status = Fail st.Time = getCheckTime() log.Error("database ping failed with error: %v", err) } else { - st.Status = pass + st.Status = Pass st.Time = getCheckTime() } - if setting.Database.Type.IsSQLite3() && st.Status == pass { + if setting.Database.Type.IsSQLite3() && st.Status == Pass { if !setting.EnableSQLite3 { - st.Status = fail + st.Status = Fail st.Time = getCheckTime() log.Error("SQLite3 health check failed with error: %v", "this Forgejo binary is built without SQLite3 enabled") } else { if _, err := os.Stat(setting.Database.Path); err != nil { - st.Status = fail + st.Status = Fail st.Time = getCheckTime() log.Error("SQLite3 file exists check failed with error: %v", err) } @@ -123,11 +124,11 @@ func checkDatabase(ctx context.Context, checks checks) status { func checkCache(checks checks) status { st := componentStatus{} if err := cache.GetCache().Ping(); err != nil { - st.Status = fail + st.Status = Fail st.Time = getCheckTime() log.Error("cache ping failed with error: %v", err) } else { - st.Status = pass + st.Status = Pass st.Time = getCheckTime() } checks["cache:ping"] = []componentStatus{st} |