add timeout for http requests

This commit is contained in:
Matthew R. Kasun 2022-07-06 09:32:00 -04:00
parent afc0fa01b5
commit a17173fa0f

View file

@ -11,6 +11,7 @@ import (
"net/http"
"os"
"strings"
"time"
"github.com/gravitl/netmaker/logger"
"github.com/gravitl/netmaker/models"
@ -25,6 +26,9 @@ import (
// LINUX_APP_DATA_PATH - linux path
const LINUX_APP_DATA_PATH = "/etc/netmaker"
// HTTP_TIMEOUT - timeout in seconds for http requests
const HTTP_TIMEOUT = 30
// ListPorts - lists ports of WireGuard devices
func ListPorts() error {
wgclient, err := wgctrl.New()
@ -339,7 +343,9 @@ func API(data any, method, url, authorization string) (*http.Response, error) {
if authorization != "" {
request.Header.Set("authorization", "Bearer "+authorization)
}
client := http.Client{}
client := http.Client{
Timeout: HTTP_TIMEOUT * time.Second,
}
return client.Do(request)
}