-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
health.go
49 lines (40 loc) · 1.03 KB
/
health.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package http
import (
"github.com/SyncYomi/SyncYomi/internal/database"
"github.com/go-chi/chi/v5"
"net/http"
)
type healthHandler struct {
encoder encoder
db *database.DB
}
func newHealthHandler(encoder encoder, db *database.DB) *healthHandler {
return &healthHandler{
encoder: encoder,
db: db,
}
}
func (h healthHandler) Routes(r chi.Router) {
r.Get("/liveness", h.handleLiveness)
r.Get("/readiness", h.handleReadiness)
}
func (h healthHandler) handleLiveness(w http.ResponseWriter, _ *http.Request) {
writeHealthy(w)
}
func (h healthHandler) handleReadiness(w http.ResponseWriter, _ *http.Request) {
if err := h.db.Ping(); err != nil {
writeUnhealthy(w)
return
}
writeHealthy(w)
}
func writeHealthy(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func writeUnhealthy(w http.ResponseWriter) {
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusInternalServerError)
w.Write([]byte("Unhealthy. Database unreachable"))
}