2021-08-03 18:14:32 +00:00
|
|
|
package health
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/go-redis/redis/v8"
|
2021-08-03 18:14:32 +00:00
|
|
|
"github.com/gofiber/fiber/v2"
|
2021-09-26 19:13:38 +00:00
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
2021-08-03 18:14:32 +00:00
|
|
|
)
|
|
|
|
|
2021-09-26 19:13:38 +00:00
|
|
|
type Dependencies struct {
|
2021-09-27 10:10:19 +00:00
|
|
|
DB *pgxpool.Pool
|
|
|
|
Redis *redis.Client
|
|
|
|
Context *context.Context
|
2021-09-26 19:13:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *Dependencies) Health(c *fiber.Ctx) error {
|
2021-09-27 10:10:19 +00:00
|
|
|
conn, err := d.DB.Acquire(*d.Context)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Release()
|
|
|
|
|
2021-08-03 18:14:32 +00:00
|
|
|
// Ping REDIS database
|
2021-09-27 10:10:19 +00:00
|
|
|
err = d.Redis.Ping(*d.Context).Err()
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.
|
|
|
|
Status(fiber.StatusServiceUnavailable).
|
2021-09-26 19:13:38 +00:00
|
|
|
JSON(Error{
|
2021-08-03 18:14:32 +00:00
|
|
|
Error: "REDIS: " + err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-09-27 10:10:19 +00:00
|
|
|
_, err = conn.Query(*d.Context, "SELECT \"id\" FROM \"jokesbapak2\" LIMIT 1")
|
2021-08-03 18:14:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return c.
|
|
|
|
Status(fiber.StatusServiceUnavailable).
|
2021-09-26 19:13:38 +00:00
|
|
|
JSON(Error{
|
2021-08-03 18:14:32 +00:00
|
|
|
Error: "POSTGRESQL: " + err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
|
|
}
|