mirror of
https://github.com/idanoo/autobrr
synced 2025-07-23 00:39:13 +00:00
feat(http): add healthcheck endpoints liveness and readiness (#240)
* feat(http): add liveness and readiness endpoints * feat(http): improve unhealthy msg
This commit is contained in:
parent
4b74a006c8
commit
2a3b5ce448
4 changed files with 61 additions and 2 deletions
51
internal/http/health.go
Normal file
51
internal/http/health.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/autobrr/autobrr/internal/database"
|
||||
|
||||
"github.com/go-chi/chi"
|
||||
)
|
||||
|
||||
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"))
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue