mirror of
https://github.com/gravitl/netmaker.git
synced 2025-10-25 01:40:46 +08:00
add a simple get request function
This commit is contained in:
@@ -2,11 +2,8 @@ package functions
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/cli/config"
|
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -27,25 +24,7 @@ func GetExtClient(networkName, clientID string) *models.ExtClient {
|
|||||||
|
|
||||||
// GetExtClientConfig - fetch a wireguard config of an external client
|
// GetExtClientConfig - fetch a wireguard config of an external client
|
||||||
func GetExtClientConfig(networkName, clientID string) string {
|
func GetExtClientConfig(networkName, clientID string) string {
|
||||||
_, ctx := config.GetCurrentContext()
|
return get(fmt.Sprintf("/api/extclients/%s/%s/file", networkName, clientID))
|
||||||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/api/extclients/%s/%s/file", ctx.Endpoint, networkName, clientID), nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if ctx.MasterKey != "" {
|
|
||||||
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
|
||||||
} else {
|
|
||||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
|
||||||
}
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
bodyBytes, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return string(bodyBytes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateExtClient - create an external client
|
// CreateExtClient - create an external client
|
||||||
|
|||||||
@@ -92,3 +92,25 @@ retry:
|
|||||||
}
|
}
|
||||||
return body
|
return body
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func get(route string) string {
|
||||||
|
_, ctx := config.GetCurrentContext()
|
||||||
|
req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+route, nil)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
if ctx.MasterKey != "" {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
||||||
|
} else {
|
||||||
|
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
||||||
|
}
|
||||||
|
res, err := http.DefaultClient.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
bodyBytes, err := io.ReadAll(res.Body)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
return string(bodyBytes)
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,36 +1,15 @@
|
|||||||
package functions
|
package functions
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gravitl/netmaker/cli/config"
|
|
||||||
cfg "github.com/gravitl/netmaker/config"
|
cfg "github.com/gravitl/netmaker/config"
|
||||||
"github.com/gravitl/netmaker/models"
|
"github.com/gravitl/netmaker/models"
|
||||||
)
|
)
|
||||||
|
|
||||||
// GetLogs - fetch Netmaker server logs
|
// GetLogs - fetch Netmaker server logs
|
||||||
func GetLogs() string {
|
func GetLogs() string {
|
||||||
_, ctx := config.GetCurrentContext()
|
return get("/api/logs")
|
||||||
req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+"/api/logs", nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if ctx.MasterKey != "" {
|
|
||||||
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
|
||||||
} else {
|
|
||||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
|
||||||
}
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
bodyBytes, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return string(bodyBytes)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetServerInfo - fetch minimal server info
|
// GetServerInfo - fetch minimal server info
|
||||||
@@ -45,23 +24,5 @@ func GetServerConfig() *cfg.ServerConfig {
|
|||||||
|
|
||||||
// GetServerHealth - fetch server current health status
|
// GetServerHealth - fetch server current health status
|
||||||
func GetServerHealth() string {
|
func GetServerHealth() string {
|
||||||
_, ctx := config.GetCurrentContext()
|
return get("/api/server/health")
|
||||||
req, err := http.NewRequest(http.MethodGet, ctx.Endpoint+"/api/server/health", nil)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
if ctx.MasterKey != "" {
|
|
||||||
req.Header.Set("Authorization", "Bearer "+ctx.MasterKey)
|
|
||||||
} else {
|
|
||||||
req.Header.Set("Authorization", "Bearer "+getAuthToken(ctx, true))
|
|
||||||
}
|
|
||||||
res, err := http.DefaultClient.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
bodyBytes, err := io.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
return string(bodyBytes)
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user