mirror of
https://github.com/go-gitea/gitea.git
synced 2024-09-01 14:56:30 +00:00
Prepare rest request with basic auth
This commit is contained in:
parent
9ee79aa016
commit
c0c35f52dc
@ -4,9 +4,12 @@
|
|||||||
package elasticsearch
|
package elasticsearch
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/tls"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -22,13 +25,33 @@ type elasticRootResponse struct {
|
|||||||
|
|
||||||
// DetectVersion detects the major version of the elasticsearch server.
|
// DetectVersion detects the major version of the elasticsearch server.
|
||||||
// Currently only supports version 7 and 8.
|
// Currently only supports version 7 and 8.
|
||||||
func DetectVersion(url string) (int, error) {
|
func DetectVersion(connStr string) (int, error) {
|
||||||
|
u, err := url.Parse(connStr)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to parse url: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
client := &http.Client{
|
client := &http.Client{
|
||||||
Timeout: 5 * time.Second,
|
Timeout: 5 * time.Second,
|
||||||
}
|
}
|
||||||
resp, err := client.Get(url)
|
if u.Scheme == "https" {
|
||||||
|
client.Transport = &http.Transport{
|
||||||
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
req, err := http.NewRequest("GET", u.String(), nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, fmt.Errorf("failed to create request: %v", err)
|
||||||
|
}
|
||||||
|
pass, ok := u.User.Password()
|
||||||
|
if ok {
|
||||||
|
req.SetBasicAuth(u.User.Username(), pass)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp, err := client.Do(req)
|
||||||
|
if err != nil {
|
||||||
|
return 0, fmt.Errorf("failed to get response: %v", err)
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
defer resp.Body.Close()
|
||||||
return parseElasticVersion(resp.Body)
|
return parseElasticVersion(resp.Body)
|
||||||
|
Loading…
Reference in New Issue
Block a user