Implement health check for deploy. Make version endpoint to return revision if not set
This commit is contained in:
46
internal/api/health/health_handler.go
Normal file
46
internal/api/health/health_handler.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package health
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"payouts/internal/service/database"
|
||||
)
|
||||
|
||||
// Route health route
|
||||
const Route = "/health"
|
||||
|
||||
// New constructs a new health Handler.
|
||||
func New(dbService database.Service) (Handler, error) {
|
||||
return &handler{
|
||||
dbService: dbService,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type handler struct {
|
||||
dbService database.Service
|
||||
}
|
||||
|
||||
// HealthHandler handles the health check requests
|
||||
func (h *handler) Health(w http.ResponseWriter, r *http.Request) {
|
||||
defer r.Body.Close()
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
status := map[string]any{}
|
||||
|
||||
// Check database connection
|
||||
err := h.dbService.HealthCheck()
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusServiceUnavailable)
|
||||
status["Error"] = fmt.Sprintf("%v", err)
|
||||
slog.Error("Health check failed", slog.String("error", status["Error"].(string)))
|
||||
} else {
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
status["OK"] = (err == nil)
|
||||
|
||||
encoder := json.NewEncoder(w)
|
||||
encoder.Encode(status)
|
||||
}
|
||||
Reference in New Issue
Block a user