2025-10-15 10:43:58 -04:00

56 lines
1.4 KiB
Go

package http
import (
"crypto/tls"
"sync"
"time"
"github.com/go-resty/resty/v2"
"github.com/tuusuario/go-sync-service/internal/config"
)
var (
clientInstance *resty.Client
once sync.Once
)
func InitClient() {
//Inicializacion del client
once.Do(func() {
client := resty.New().
SetTimeout(time.Duration(config.GlobalConfig.TimeoutSeconds) * time.Second)
client.SetTLSClientConfig(&tls.Config{InsecureSkipVerify: config.GlobalConfig.TLSSkipVerify})
if config.GlobalConfig.LogRequests {
var start time.Time // 👈 aquí declaramos la variable de tiempo
client.OnBeforeRequest(func(c *resty.Client, r *resty.Request) error {
start = time.Now()
config.Log.Printf("➡️ Request: %s %s\nHeaders: %v\nBody: %v\n",
r.Method, r.URL, r.Header, r.Body)
return nil
})
client.OnAfterResponse(func(c *resty.Client, r *resty.Response) error {
duration := time.Since(start)
config.Log.Printf("⬅️ Response: %s %s | Status: %d | Duration: %s\nHeaders: %v\nBody: %s\n",
r.Request.Method, r.Request.URL, r.StatusCode(), duration,
r.Header(), r.String())
return nil
})
}
client.SetDebug(config.GlobalConfig.EnableDebug)
config.Log.Printf("🚀 Inicializando Resty client...")
clientInstance = client
})
}
func GetClient() *resty.Client {
if clientInstance == nil {
panic("❌ Resty client no inicializado. Llama a InitClient primero.")
}
return clientInstance
}