Skip to content

Commit cb4e2b1

Browse files
perfectacle2siddontang
authored andcommitted
add elasticsearch https setting (#182)
1 parent 7b2144d commit cb4e2b1

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

elastic/client.go

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import (
77
"io/ioutil"
88
"net/http"
99
"net/url"
10+
"crypto/tls"
1011

1112
"github.com/juju/errors"
1213
)
1314

1415
// Although there are many Elasticsearch clients with Go, I still want to implement one by myself.
1516
// Because we only need some very simple usages.
1617
type Client struct {
18+
Protocol string
1719
Addr string
1820
User string
1921
Password string
@@ -22,6 +24,7 @@ type Client struct {
2224
}
2325

2426
type ClientConfig struct {
27+
Https bool
2528
Addr string
2629
User string
2730
Password string
@@ -34,7 +37,16 @@ func NewClient(conf *ClientConfig) *Client {
3437
c.User = conf.User
3538
c.Password = conf.Password
3639

37-
c.c = &http.Client{}
40+
if conf.Https {
41+
c.Protocol = "https"
42+
tr := &http.Transport{
43+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
44+
}
45+
c.c = &http.Client{Transport: tr}
46+
} else {
47+
c.Protocol = "http"
48+
c.c = &http.Client{}
49+
}
3850

3951
return c
4052
}
@@ -237,7 +249,7 @@ func (c *Client) DoBulk(url string, items []*BulkRequest) (*BulkResponse, error)
237249
}
238250

239251
func (c *Client) CreateMapping(index string, docType string, mapping map[string]interface{}) error {
240-
reqUrl := fmt.Sprintf("http://%s/%s", c.Addr,
252+
reqUrl := fmt.Sprintf("%s://%s/%s", c.Protocol, c.Addr,
241253
url.QueryEscape(index))
242254

243255
r, err := c.Do("HEAD", reqUrl, nil)
@@ -256,7 +268,7 @@ func (c *Client) CreateMapping(index string, docType string, mapping map[string]
256268
return errors.Errorf("Error: %s, code: %d", http.StatusText(r.Code), r.Code)
257269
}
258270

259-
reqUrl = fmt.Sprintf("http://%s/%s/%s/_mapping", c.Addr,
271+
reqUrl = fmt.Sprintf("%s://%s/%s/%s/_mapping", c.Protocol, c.Addr,
260272
url.QueryEscape(index),
261273
url.QueryEscape(docType))
262274

@@ -265,7 +277,7 @@ func (c *Client) CreateMapping(index string, docType string, mapping map[string]
265277
}
266278

267279
func (c *Client) GetMapping(index string, docType string) (*MappingResponse, error){
268-
reqUrl := fmt.Sprintf("http://%s/%s/%s/_mapping", c.Addr,
280+
reqUrl := fmt.Sprintf("%s://%s/%s/%s/_mapping", c.Protocol, c.Addr,
269281
url.QueryEscape(index),
270282
url.QueryEscape(docType))
271283
buf := bytes.NewBuffer(nil)
@@ -293,7 +305,7 @@ func (c *Client) GetMapping(index string, docType string) (*MappingResponse, err
293305
}
294306

295307
func (c *Client) DeleteIndex(index string) error {
296-
reqUrl := fmt.Sprintf("http://%s/%s", c.Addr,
308+
reqUrl := fmt.Sprintf("%s://%s/%s", c.Protocol, c.Addr,
297309
url.QueryEscape(index))
298310

299311
r, err := c.Do("DELETE", reqUrl, nil)
@@ -309,7 +321,7 @@ func (c *Client) DeleteIndex(index string) error {
309321
}
310322

311323
func (c *Client) Get(index string, docType string, id string) (*Response, error) {
312-
reqUrl := fmt.Sprintf("http://%s/%s/%s/%s", c.Addr,
324+
reqUrl := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
313325
url.QueryEscape(index),
314326
url.QueryEscape(docType),
315327
url.QueryEscape(id))
@@ -319,7 +331,7 @@ func (c *Client) Get(index string, docType string, id string) (*Response, error)
319331

320332
// Can use Update to create or update the data
321333
func (c *Client) Update(index string, docType string, id string, data map[string]interface{}) error {
322-
reqUrl := fmt.Sprintf("http://%s/%s/%s/%s", c.Addr,
334+
reqUrl := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
323335
url.QueryEscape(index),
324336
url.QueryEscape(docType),
325337
url.QueryEscape(id))
@@ -337,7 +349,7 @@ func (c *Client) Update(index string, docType string, id string, data map[string
337349
}
338350

339351
func (c *Client) Exists(index string, docType string, id string) (bool, error) {
340-
reqUrl := fmt.Sprintf("http://%s/%s/%s/%s", c.Addr,
352+
reqUrl := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
341353
url.QueryEscape(index),
342354
url.QueryEscape(docType),
343355
url.QueryEscape(id))
@@ -351,7 +363,7 @@ func (c *Client) Exists(index string, docType string, id string) (bool, error) {
351363
}
352364

353365
func (c *Client) Delete(index string, docType string, id string) error {
354-
reqUrl := fmt.Sprintf("http://%s/%s/%s/%s", c.Addr,
366+
reqUrl := fmt.Sprintf("%s://%s/%s/%s/%s", c.Protocol, c.Addr,
355367
url.QueryEscape(index),
356368
url.QueryEscape(docType),
357369
url.QueryEscape(id))
@@ -370,20 +382,20 @@ func (c *Client) Delete(index string, docType string, id string) error {
370382

371383
// only support parent in 'Bulk' related apis
372384
func (c *Client) Bulk(items []*BulkRequest) (*BulkResponse, error) {
373-
reqUrl := fmt.Sprintf("http://%s/_bulk", c.Addr)
385+
reqUrl := fmt.Sprintf("%s://%s/_bulk", c.Protocol, c.Addr)
374386

375387
return c.DoBulk(reqUrl, items)
376388
}
377389

378390
func (c *Client) IndexBulk(index string, items []*BulkRequest) (*BulkResponse, error) {
379-
reqUrl := fmt.Sprintf("http://%s/%s/_bulk", c.Addr,
391+
reqUrl := fmt.Sprintf("%s://%s/%s/_bulk", c.Protocol, c.Addr,
380392
url.QueryEscape(index))
381393

382394
return c.DoBulk(reqUrl, items)
383395
}
384396

385397
func (c *Client) IndexTypeBulk(index string, docType string, items []*BulkRequest) (*BulkResponse, error) {
386-
reqUrl := fmt.Sprintf("http://%s/%s/%s/_bulk", c.Addr,
398+
reqUrl := fmt.Sprintf("%s://%s/%s/%s/_bulk", c.Protocol, c.Addr,
387399
url.QueryEscape(index),
388400
url.QueryEscape(docType))
389401

etc/river.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ my_user = "root"
55
my_pass = ""
66
my_charset = "utf8"
77

8+
# Set true when elasticsearch use https
9+
#es_https = false
810
# Elasticsearch address
911
es_addr = "127.0.0.1:9200"
1012
# Elasticsearch user and password, maybe set by shield, nginx, or x-pack

river/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Config struct {
1919
MyPassword string `toml:"my_pass"`
2020
MyCharset string `toml:"my_charset"`
2121

22+
ESHttps bool `toml:"es_https"`
2223
ESAddr string `toml:"es_addr"`
2324
ESUser string `toml:"es_user"`
2425
ESPassword string `toml:"es_pass"`

river/river.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func NewRiver(c *Config) (*River, error) {
7171
cfg.Addr = r.c.ESAddr
7272
cfg.User = r.c.ESUser
7373
cfg.Password = r.c.ESPassword
74+
cfg.Https = r.c.ESHttps
7475
r.es = elastic.NewClient(cfg)
7576

7677
r.st = &stat{r: r}

0 commit comments

Comments
 (0)