internal/httpd/healthz.go
31 lines · 935 bytes
1package httpd
2
3import (
4 "encoding/json"
5 "net/http"
6
7 "gitbay.org/gitbay/internal/buildinfo"
8)
9
10// healthz answers from inside the process: whether the database answers,
11// and which build is serving. No auth, no repository data; a monitor or
12// an uptime check reads it. 503 when the database does not answer, so a
13// checker that only reads the status code still learns the truth.
14func (s *Server) healthz(w http.ResponseWriter, r *http.Request) {
15 dbErr := ""
16 if err := s.st.DB.Ping(); err != nil {
17 dbErr = err.Error()
18 } else if _, err := s.st.Version(); err != nil {
19 dbErr = err.Error()
20 }
21 w.Header().Set("Content-Type", "application/json")
22 w.Header().Set("Cache-Control", "no-store")
23 if dbErr != "" {
24 w.WriteHeader(http.StatusServiceUnavailable)
25 }
26 json.NewEncoder(w).Encode(map[string]any{
27 "ok": dbErr == "",
28 "commit": buildinfo.String(),
29 "db": map[string]any{"ok": dbErr == "", "error": dbErr},
30 })
31}