cloduflare
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/models"
|
||||
"github.com/sujit-baniya/fiber-boilerplate/pkg/services"
|
||||
@@ -39,8 +41,8 @@ func SaveCloudflareConfig(c *fiber.Ctx) error {
|
||||
}
|
||||
|
||||
cfg := models.CloudflareConfig{
|
||||
APIToken: b.APIToken,
|
||||
AccountID: b.AccountID,
|
||||
APIToken: strings.TrimSpace(b.APIToken),
|
||||
AccountID: strings.TrimSpace(b.AccountID),
|
||||
Nota: b.Nota,
|
||||
}
|
||||
cfg.ID = b.ID
|
||||
@@ -58,7 +60,28 @@ func cloudflareClient() (*services.CloudflareClient, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return services.NewCloudflareClient(cfg.APIToken, cfg.AccountID), nil
|
||||
return services.NewCloudflareClient(strings.TrimSpace(cfg.APIToken), strings.TrimSpace(cfg.AccountID)), nil
|
||||
}
|
||||
|
||||
// VerifyCloudflareToken verifica que el token activo sea válido contra la API de Cloudflare.
|
||||
func VerifyCloudflareToken(c *fiber.Ctx) error {
|
||||
cfg, err := models.GetCloudflareConfig()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusServiceUnavailable).JSON(fiber.Map{
|
||||
"ok": false,
|
||||
"error": "No hay configuración activa de Cloudflare",
|
||||
})
|
||||
}
|
||||
token := strings.TrimSpace(cfg.APIToken)
|
||||
if token == "" {
|
||||
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"ok": false, "error": "Token vacío"})
|
||||
}
|
||||
client := services.NewCloudflareClient(token, strings.TrimSpace(cfg.AccountID))
|
||||
result, err := client.VerifyToken()
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{"ok": false, "error": err.Error()})
|
||||
}
|
||||
return c.JSON(fiber.Map{"ok": true, "data": result})
|
||||
}
|
||||
|
||||
// ─── Endpoints de datos ───────────────────────────────────────────────────────
|
||||
|
||||
Reference in New Issue
Block a user