50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package api
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/hex"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
func hexHash(hash []byte) string { return hex.EncodeToString(hash) }
|
||
|
|
|
||
|
|
// handleHealth is liveness: the process is up. It touches no dependency, so an
|
||
|
|
// orchestrator does not restart the container just because Emby is down.
|
||
|
|
func (s *Server) handleHealth(w http.ResponseWriter, _ *http.Request) {
|
||
|
|
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
|
||
|
|
}
|
||
|
|
|
||
|
|
// handleReady is readiness: everything this service needs is reachable.
|
||
|
|
func (s *Server) handleReady(w http.ResponseWriter, r *http.Request) {
|
||
|
|
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
|
||
|
|
defer cancel()
|
||
|
|
|
||
|
|
checks := map[string]string{}
|
||
|
|
status := http.StatusOK
|
||
|
|
|
||
|
|
if err := s.store.Ping(ctx); err != nil {
|
||
|
|
checks["postgres"] = err.Error()
|
||
|
|
status = http.StatusServiceUnavailable
|
||
|
|
} else {
|
||
|
|
checks["postgres"] = "ok"
|
||
|
|
}
|
||
|
|
|
||
|
|
if err := s.cache.Ping(ctx); err != nil {
|
||
|
|
checks["redis"] = err.Error()
|
||
|
|
status = http.StatusServiceUnavailable
|
||
|
|
} else {
|
||
|
|
checks["redis"] = "ok"
|
||
|
|
}
|
||
|
|
|
||
|
|
// Emby being unreachable is reported but does not fail readiness: cached responses
|
||
|
|
// are still worth serving, and flapping the container would not bring Emby back.
|
||
|
|
if err := s.emby.Ping(ctx); err != nil {
|
||
|
|
checks["emby"] = err.Error()
|
||
|
|
} else {
|
||
|
|
checks["emby"] = "ok"
|
||
|
|
}
|
||
|
|
|
||
|
|
writeJSON(w, status, checks)
|
||
|
|
}
|