-
Notifications
You must be signed in to change notification settings - Fork 625
/
Copy pathelasticsearch.go
executable file
·113 lines (93 loc) · 2.81 KB
/
elasticsearch.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package elasticsearch // import "github.com/elastic/go-elasticsearch"
import (
"fmt"
"net/http"
"net/url"
"os"
"strings"
"github.com/elastic/go-elasticsearch/esapi"
"github.com/elastic/go-elasticsearch/estransport"
)
const (
defaultURL = "http://localhost:9200"
)
// Config represents the client configuration.
//
type Config struct {
Addresses []string // A list of Elasticsearch nodes to use.
Transport http.RoundTripper // The HTTP transport object.
}
// Client represents the Elasticsearch client.
//
type Client struct {
*esapi.API // Embeds the API methods
Transport estransport.Interface
}
// NewDefaultClient creates a new client with default options.
//
// It will use http://localhost:9200 as the default address.
//
// It will use the ELASTICSEARCH_URL environment variable, if set,
// to configure the addresses; use a comma to separate multiple URLs.
//
func NewDefaultClient() (*Client, error) {
return NewClient(Config{})
}
// NewClient creates a new client with configuration from cfg.
//
// It will use http://localhost:9200 as the default address.
//
// It will use the ELASTICSEARCH_URL environment variable, if set,
// to configure the addresses; use a comma to separate multiple URLs.
//
// It's an error to set both cfg.Addresses and the ELASTICSEARCH_URL
// environment variable.
//
func NewClient(cfg Config) (*Client, error) {
var (
urls []*url.URL
tran estransport.Interface
)
addrs := addressesFromEnvironment()
if len(addrs) > 0 && len(cfg.Addresses) > 0 {
return nil, fmt.Errorf("cannot create client: both ELASTICSEARCH_URL and Addresses are set")
}
for _, addr := range addrs {
u, err := url.Parse(strings.TrimRight(addr, "/"))
if err != nil {
return nil, fmt.Errorf("cannot create client: %s", err)
}
urls = append(urls, u)
}
for _, addr := range cfg.Addresses {
u, err := url.Parse(strings.TrimRight(addr, "/"))
if err != nil {
return nil, fmt.Errorf("cannot create client: %s", err)
}
urls = append(urls, u)
}
if len(urls) < 1 {
u, _ := url.Parse(defaultURL) // errcheck exclude
urls = append(urls, u)
}
tran = estransport.New(estransport.Config{URLs: urls, Transport: cfg.Transport})
return &Client{Transport: tran, API: esapi.New(tran)}, nil
}
// Perform delegates to Transport to execute a request and return a response.
//
func (c *Client) Perform(req *http.Request) (*http.Response, error) {
return c.Transport.Perform(req)
}
// addressesFromEnvironment returns a list of addresses by splitting
// the ELASTICSEARCH_URL environment variable with comma, or an empty list.
//
func addressesFromEnvironment() []string {
var addrs []string
if envURLs, ok := os.LookupEnv("ELASTICSEARCH_URL"); ok && envURLs != "" {
list := strings.Split(envURLs, ",")
for _, u := range list {
addrs = append(addrs, strings.TrimSpace(u))
}
}
return addrs
}