-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathelastic.go
64 lines (48 loc) · 1.33 KB
/
elastic.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
package vc5
import (
"bytes"
"context"
"github.com/elastic/go-elasticsearch/v7"
"github.com/elastic/go-elasticsearch/v7/esapi"
)
// https://pkg.go.dev/github.com/elastic/go-elasticsearch/v7
// https://www.elastic.co/guide/en/elasticsearch/reference/7.17/security-minimal-setup.html
type Elasticsearch struct {
Index string `json:"index,omitempty"`
Addresses []string `json:"addresses,omitempty"`
Username secret `json:"username,omitempty"`
Password secret `json:"password,omitempty"`
client *elasticsearch.Client
}
func (e *Elasticsearch) start() (err error) {
e.client, err = elasticsearch.NewClient(elasticsearch.Config{
Addresses: e.Addresses,
Username: string(e.Username),
Password: string(e.Password),
})
if err != nil {
return err
}
return nil
}
func (e *Elasticsearch) log(host string, id uint64, body []byte) bool {
if e.client == nil {
return false
}
ctx := context.Background()
req := esapi.IndexRequest{
Index: e.Index,
//DocumentID: fmt.Sprintf("%s-%d", host, id), // don't think that this was ever really needed ...
Body: bytes.NewReader(body),
Refresh: "true",
}
res, err := req.Do(ctx, e.client)
if err != nil {
return false
}
defer res.Body.Close()
//if res.StatusCode != 201 {
// log.Println(err, res.StatusCode, string(body))
//}
return res.StatusCode == 201
}