47 lines
1.3 KiB
Go
47 lines
1.3 KiB
Go
package v1
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
|
|
"lincodex/backend/internal/adapter/primary/http/api"
|
|
"lincodex/backend/internal/port/inbound"
|
|
)
|
|
|
|
type HealthHandler struct {
|
|
readiness inbound.Readiness
|
|
readinessTimeout time.Duration
|
|
}
|
|
|
|
func NewHealthHandler(readiness inbound.Readiness, readinessTimeout time.Duration) *HealthHandler {
|
|
return &HealthHandler{readiness: readiness, readinessTimeout: readinessTimeout}
|
|
}
|
|
|
|
func (handler *HealthHandler) GetHealthz(context.Context) (*api.HealthResponse, error) {
|
|
return &api.HealthResponse{Status: api.HealthResponseStatusOk}, nil
|
|
}
|
|
|
|
func (handler *HealthHandler) Healthz(w http.ResponseWriter, _ *http.Request) {
|
|
writeStatus(w, http.StatusOK, "ok")
|
|
}
|
|
|
|
func (handler *HealthHandler) Readyz(w http.ResponseWriter, r *http.Request) {
|
|
ctx, cancel := context.WithTimeout(r.Context(), handler.readinessTimeout)
|
|
defer cancel()
|
|
if err := handler.readiness.Check(ctx); err != nil {
|
|
writeStatus(w, http.StatusServiceUnavailable, "not_ready")
|
|
return
|
|
}
|
|
writeStatus(w, http.StatusOK, "ready")
|
|
}
|
|
|
|
func writeStatus(w http.ResponseWriter, status int, value string) {
|
|
w.Header().Set("Content-Type", "application/json")
|
|
w.WriteHeader(status)
|
|
_ = json.NewEncoder(w).Encode(struct {
|
|
Status string `json:"status"`
|
|
}{Status: value})
|
|
}
|