/
es.go
39 lines (35 loc) · 947 Bytes
/
es.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
package es
import (
"github.com/andresusanto/aws-elastic-search-demo/ingester/internal/config"
"github.com/aws/aws-sdk-go/aws/defaults"
v4 "github.com/aws/aws-sdk-go/aws/signer/v4"
"github.com/olivere/elastic/v7"
"github.com/sha1sum/aws_signing_client"
)
// NewClient returns new elastic search client based from environments
func NewClient(c *config.Config) (*elastic.Client, error) {
if !c.SignESClient {
return elastic.NewClient(
elastic.SetURL(c.ESEndpoint),
elastic.SetSniff(false),
elastic.SetHealthcheck(false),
)
}
signer := v4.NewSigner(
defaults.CredChain(
defaults.Config(),
defaults.Handlers(),
),
)
awsClient, err := aws_signing_client.New(signer, nil, "es", c.Region)
if err != nil {
return nil, err
}
return elastic.NewClient(
elastic.SetURL(c.ESEndpoint),
elastic.SetScheme("https"),
elastic.SetHttpClient(awsClient),
elastic.SetSniff(false),
elastic.SetHealthcheck(false),
)
}