-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathconnect.go
44 lines (38 loc) · 942 Bytes
/
connect.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
package logic
import (
"context"
"github.com/opensearch-project/opensearch-go"
"github.com/opensearch-project/opensearch-go/opensearchapi"
"log"
"os"
)
var opensearchConnectionURL = func() string {
if osConnURL := os.Getenv("OPENSEARCH_CONNECTION_URL"); osConnURL != "" {
return osConnURL
}
return "http://localhost:9200"
}()
func ConnectWithOpenSearch(ctx context.Context) (*opensearch.Client, error) {
newClient, err := opensearch.NewClient(opensearch.Config{
Addresses: []string{
opensearchConnectionURL,
},
Username: "opensearch_user",
Password: "W&lcome123",
})
if err != nil {
log.Printf("Error parsing the Redis URL: %v", err)
return nil, err
}
pingRequest := opensearchapi.PingRequest{
Pretty: true,
Human: true,
ErrorTrace: true,
}
_, err = pingRequest.Do(ctx, newClient)
if err != nil {
log.Printf("Ping request failed: %v", err)
return nil, err
}
return newClient, nil
}