mirror of
https://github.com/Octops/quake-kube.git
synced 2026-04-08 10:50:34 +00:00
Initial commit
This commit is contained in:
41
internal/util/net/http/http.go
Normal file
41
internal/util/net/http/http.go
Normal file
@ -0,0 +1,41 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func GetBody(url string) ([]byte, error) {
|
||||
client := http.Client{
|
||||
Timeout: 30 * time.Second,
|
||||
}
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
return ioutil.ReadAll(resp.Body)
|
||||
}
|
||||
|
||||
func GetUntil(url string, stop <-chan struct{}) error {
|
||||
client := http.Client{
|
||||
Timeout: 1 * time.Second,
|
||||
}
|
||||
for {
|
||||
select {
|
||||
case <-stop:
|
||||
return errors.Errorf("not available: %q", url)
|
||||
default:
|
||||
resp, err := client.Get(url)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
resp.Body.Close()
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
25
internal/util/net/net.go
Normal file
25
internal/util/net/net.go
Normal file
@ -0,0 +1,25 @@
|
||||
package net
|
||||
|
||||
import (
|
||||
"net"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
// DetectHostIPv4 attempts to determine the host IPv4 address by finding the
|
||||
// first non-loopback device with an assigned IPv4 address.
|
||||
func DetectHostIPv4() (string, error) {
|
||||
addrs, err := net.InterfaceAddrs()
|
||||
if err != nil {
|
||||
return "", errors.WithStack(err)
|
||||
}
|
||||
for _, a := range addrs {
|
||||
if ipnet, ok := a.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
|
||||
if ipnet.IP.To4() == nil {
|
||||
continue
|
||||
}
|
||||
return ipnet.IP.String(), nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("cannot detect host IPv4 address")
|
||||
}
|
||||
Reference in New Issue
Block a user