35 lines
738 B
Go
35 lines
738 B
Go
package handlers
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"time"
|
|
|
|
"amnezia-share/internal/web"
|
|
)
|
|
|
|
func registerHealth(a *web.App, mux *http.ServeMux) {
|
|
mux.HandleFunc("GET /health", Health(a))
|
|
}
|
|
|
|
// Health reports basic liveness plus a DB ping, mirroring health.php.
|
|
func Health(a *web.App) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
dbOK := true
|
|
ctx, cancel := context.WithTimeout(r.Context(), 3*time.Second)
|
|
defer cancel()
|
|
if err := a.Pool.Ping(ctx); err != nil {
|
|
dbOK = false
|
|
}
|
|
status := http.StatusOK
|
|
if !dbOK {
|
|
status = http.StatusServiceUnavailable
|
|
}
|
|
writeJSON(w, status, map[string]any{
|
|
"ok": dbOK,
|
|
"db": dbOK,
|
|
"time": time.Now().UTC().Format(time.RFC3339),
|
|
})
|
|
}
|
|
}
|