readysite / website / controllers / health.go
1.5 KB
health.go
package controllers

import (
	"encoding/json"
	"net/http"

	"github.com/readysite/readysite/pkg/application"
	"github.com/readysite/readysite/website/models"
)

// Health returns the health controller.
func Health() (string, *HealthController) {
	return "health", &HealthController{}
}

// HealthController handles health check endpoints.
type HealthController struct {
	application.BaseController
}

// Setup registers routes.
func (c *HealthController) Setup(app *application.App) {
	c.BaseController.Setup(app)

	http.Handle("GET /health", app.Method(c, "Check", nil))
	http.Handle("GET /healthz", app.Method(c, "Check", nil))
}

// Handle implements Controller interface with value receiver for request isolation.
func (c HealthController) Handle(r *http.Request) application.Controller {
	c.Request = r
	return &c
}

// HealthStatus represents the health check response.
type HealthStatus struct {
	Status   string `json:"status"`
	Database string `json:"database"`
}

// Check returns the health status of the application.
func (c *HealthController) Check(w http.ResponseWriter, r *http.Request) {
	status := HealthStatus{
		Status:   "ok",
		Database: "ok",
	}

	// Check database connectivity
	if err := checkDatabase(); err != nil {
		status.Status = "degraded"
		status.Database = "error"
		w.WriteHeader(http.StatusServiceUnavailable)
	}

	w.Header().Set("Content-Type", "application/json")
	json.NewEncoder(w).Encode(status)
}

// checkDatabase verifies database connectivity.
func checkDatabase() error {
	return models.DB.Ping()
}
← Back