Refactor http client for unix sockets
This commit is contained in:
parent
589c9a20f9
commit
2f124e4b76
1 changed files with 13 additions and 7 deletions
|
@ -50,11 +50,17 @@ var StreamSocket = "/tmp/ingress-stream.sock"
|
||||||
|
|
||||||
var statusLocation = "nginx-status"
|
var statusLocation = "nginx-status"
|
||||||
|
|
||||||
|
var httpClient *http.Client
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
httpClient = buildUnixSocketClient(HealthCheckTimeout)
|
||||||
|
}
|
||||||
|
|
||||||
// NewGetStatusRequest creates a new GET request to the internal NGINX status server
|
// NewGetStatusRequest creates a new GET request to the internal NGINX status server
|
||||||
func NewGetStatusRequest(path string) (int, []byte, error) {
|
func NewGetStatusRequest(path string) (int, []byte, error) {
|
||||||
url := fmt.Sprintf("http+unix://%v%v", statusLocation, path)
|
url := fmt.Sprintf("%v://%v%v", httpunix.Scheme, statusLocation, path)
|
||||||
|
|
||||||
res, err := buildUnixSocketClient(HealthCheckTimeout).Get(url)
|
res, err := httpClient.Get(url)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
|
@ -70,14 +76,14 @@ func NewGetStatusRequest(path string) (int, []byte, error) {
|
||||||
|
|
||||||
// NewPostStatusRequest creates a new POST request to the internal NGINX status server
|
// NewPostStatusRequest creates a new POST request to the internal NGINX status server
|
||||||
func NewPostStatusRequest(path, contentType string, data interface{}) (int, []byte, error) {
|
func NewPostStatusRequest(path, contentType string, data interface{}) (int, []byte, error) {
|
||||||
url := fmt.Sprintf("http+unix://%v%v", statusLocation, path)
|
url := fmt.Sprintf("%v://%v%v", httpunix.Scheme, statusLocation, path)
|
||||||
|
|
||||||
buf, err := json.Marshal(data)
|
buf, err := json.Marshal(data)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
res, err := buildUnixSocketClient(HealthCheckTimeout).Post(url, contentType, bytes.NewReader(buf))
|
res, err := httpClient.Post(url, contentType, bytes.NewReader(buf))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, nil, err
|
return 0, nil, err
|
||||||
}
|
}
|
||||||
|
@ -112,11 +118,11 @@ func GetServerBlock(conf string, host string) (string, error) {
|
||||||
|
|
||||||
// ReadNginxConf reads the nginx configuration file into a string
|
// ReadNginxConf reads the nginx configuration file into a string
|
||||||
func ReadNginxConf() (string, error) {
|
func ReadNginxConf() (string, error) {
|
||||||
return ReadFileToString("/etc/nginx/nginx.conf")
|
return readFileToString("/etc/nginx/nginx.conf")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ReadFileToString reads any file into a string
|
// readFileToString reads any file into a string
|
||||||
func ReadFileToString(path string) (string, error) {
|
func readFileToString(path string) (string, error) {
|
||||||
f, err := os.Open(path)
|
f, err := os.Open(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
Loading…
Reference in a new issue