36 lines
801 B
Go
36 lines
801 B
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
|
|
redis "github.com/go-redis/redis/v8"
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
amqp "github.com/rabbitmq/amqp091-go"
|
|
)
|
|
|
|
type HealthResponse struct {
|
|
Status string `json:"status,omitempty"`
|
|
}
|
|
|
|
func HealthHandlerFn(db *pgxpool.Pool, bus *amqp.Channel, cache *redis.Client) fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
// Only 404 indicate service as not-healthy
|
|
if err := db.Ping(context.Background()); err != nil {
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
if closed := bus.IsClosed(); closed {
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
if err := cache.Ping(context.Background()).Err(); err != nil {
|
|
return c.SendStatus(fiber.StatusNotFound)
|
|
}
|
|
|
|
return c.JSON(&HealthResponse{
|
|
Status: "OK",
|
|
})
|
|
}
|
|
}
|