This commit is contained in:
Lizandro Guarnizo
2026-05-14 22:30:31 -05:00
parent 277ad6bb63
commit 61b3eca31d
16 changed files with 1341 additions and 25 deletions
+38
View File
@@ -1,8 +1,12 @@
package controllers
import (
"io"
"math"
"net/http"
"strconv"
"strings"
"time"
"github.com/gofiber/fiber/v2"
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
@@ -57,6 +61,7 @@ func CreateSaasProducto(c *fiber.Ctx) error {
ServicioID *uint `json:"servicio_id"`
Activo bool `json:"activo"`
Orden int `json:"orden"`
HealthURL string `json:"health_url"`
}
var req Req
if err := c.BodyParser(&req); err != nil {
@@ -73,6 +78,7 @@ func CreateSaasProducto(c *fiber.Ctx) error {
ServicioID: req.ServicioID,
Activo: req.Activo,
Orden: req.Orden,
HealthURL: strings.TrimSpace(req.HealthURL),
}
if err := models.CreateSaasProducto(item); err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"error": err.Error()})
@@ -94,6 +100,7 @@ func UpdateSaasProducto(c *fiber.Ctx) error {
ServicioID *uint `json:"servicio_id"`
Activo bool `json:"activo"`
Orden int `json:"orden"`
HealthURL string `json:"health_url"`
}
var req Req
if err := c.BodyParser(&req); err != nil {
@@ -107,6 +114,7 @@ func UpdateSaasProducto(c *fiber.Ctx) error {
ServicioID: req.ServicioID,
Activo: req.Activo,
Orden: req.Orden,
HealthURL: strings.TrimSpace(req.HealthURL),
}
item.ID = uint(id)
if err := models.UpdateSaasProducto(item); err != nil {
@@ -126,3 +134,33 @@ func DeleteSaasProducto(c *fiber.Ctx) error {
}
return c.JSON(fiber.Map{"message": "Producto SaaS eliminado"})
}
// HealthCheckSaas hace un GET a la HealthURL del producto y devuelve status + latencia.
func HealthCheckSaas(c *fiber.Ctx) error {
id, err := strconv.ParseUint(c.Params("id"), 10, 32)
if err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": "ID inválido"})
}
item, err := models.GetSaasProductoByID(uint(id))
if err != nil {
return c.Status(fiber.StatusNotFound).JSON(fiber.Map{"error": "no encontrado"})
}
if strings.TrimSpace(item.HealthURL) == "" {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{"error": "Sin URL de health check configurada"})
}
client := &http.Client{Timeout: 10 * time.Second}
start := time.Now()
resp, reqErr := client.Get(item.HealthURL) //nolint:noctx
latency := time.Since(start).Milliseconds()
if reqErr != nil {
return c.JSON(fiber.Map{"ok": false, "error": reqErr.Error(), "latency_ms": latency})
}
defer resp.Body.Close()
body, _ := io.ReadAll(io.LimitReader(resp.Body, 8*1024))
return c.JSON(fiber.Map{
"ok": resp.StatusCode >= 200 && resp.StatusCode < 300,
"http_status": resp.StatusCode,
"body": string(body),
"latency_ms": latency,
})
}