Files
mtusi2026lab4-implementation/internal/handler/health.go
T

33 lines
714 B
Go

package handler
import (
"context"
"fmt"
"net/http"
"time"
"messenger/internal/domain"
)
const readyTimeout = time.Second
func (h *Handler) Live(w http.ResponseWriter, _ *http.Request) {
writeJSON(w, http.StatusOK, healthResponse{Status: "ok"})
}
func (h *Handler) Ready(w http.ResponseWriter, r *http.Request) {
if h.draining.Load() {
h.fail(w, r, fmt.Errorf("%w: draining", domain.ErrUnavailable))
return
}
ctx, cancel := context.WithTimeout(r.Context(), readyTimeout)
defer cancel()
for _, dep := range h.deps {
if err := dep(ctx); err != nil {
h.fail(w, r, fmt.Errorf("%w: %v", domain.ErrUnavailable, err))
return
}
}
writeJSON(w, http.StatusOK, healthResponse{Status: "ok"})
}