initial commit
This commit is contained in:
commit
88c14b627e
224 files changed
+17204
No files matched your search
@@ -0,0 +1,46 @@
|
||||
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})
|
||||
}
|
||||
Reference in new issue
Block a user