This repository has been archived by the owner on Jan 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
es.go
227 lines (183 loc) · 5.55 KB
/
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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Package es implements a simple Elasticsearch client.
package es
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"time"
"go.uber.org/zap"
)
// Config
type Config struct {
Log *zap.Logger
HttpClient *http.Client
ElasticServer string
}
// Client
type Client struct {
Config
}
// CreateClient returns an Elasticsearch client object
func CreateClient(cfg Config) *Client {
client := &Client{Config: cfg}
defaultElasticServer := false
defaultLogger := false
defaultHttpClient := false
// default elastic server if not provided
if client.ElasticServer == "" {
defaultElasticServer = true
client.ElasticServer = "http://elasticsearch:9200"
}
// default zap logger if not provided
if client.Log == nil {
defaultLogger = true
zapCfg := zap.NewDevelopmentConfig()
logger, err := zapCfg.Build()
if err != nil {
fmt.Printf("Can not build logger: %s\n", err.Error())
os.Exit(1)
}
client.Log = logger
}
// default HttpClient is none provided
if client.HttpClient == nil {
defaultHttpClient = true
// Http Client Configuration for outbound connections
netTransport := &http.Transport{
MaxIdleConnsPerHost: 10,
DialContext: (&net.Dialer{
Timeout: 10 * time.Second,
}).DialContext,
TLSHandshakeTimeout: 10 * time.Second,
}
client.HttpClient = &http.Client{
Timeout: time.Second * 60,
Transport: netTransport,
}
}
client.Log.Info("Created es client",
zap.Bool("defaultHttpClient", defaultHttpClient),
zap.Bool("defaultLogger", defaultLogger),
zap.Bool("defaultElasticServer", defaultElasticServer),
zap.String("ElasticServer", client.ElasticServer))
return client
}
// Get uses HTTP Get method to retrieve data from elasticsearch
// returns a byte array that may be used to unmarshal into a
// specific type depending on the returned code.
func (es *Client) Get(url string) (int, []byte, error) {
return es.req(http.MethodGet, url, []byte{})
}
// Put uses HTTP Put method to send data to elasticsearch
func (es *Client) Put(url string, data []byte) (int, Result, *ErrorResponse, error) {
return es.reqRes(url, data, http.MethodPut)
}
// Post uses HTTP POST method to send data to elasticsearch
func (es *Client) Post(url string, data []byte) (int, Result, *ErrorResponse, error) {
return es.reqRes(url, data, http.MethodPost)
}
// PutObj Marshals and object into json and PUTs it to Elasticsearch
func (es *Client) PutObj(url string, dataObj interface{}) (int, Result, *ErrorResponse, error) {
data, err := json.Marshal(dataObj)
if err != nil {
es.Log.Error("Error marshaling object to json.", zap.Error(err))
return 0, Result{}, nil, err
}
return es.Put(url, data)
}
// PostObj Marshals and object into json and POSTs it to Elasticsearch
func (es *Client) PostObj(url string, dataObj interface{}) (int, Result, *ErrorResponse, error) {
data, err := json.Marshal(dataObj)
if err != nil {
es.Log.Error("Error marshaling object to json.", zap.Error(err))
return 0, Result{}, nil, err
}
return es.Post(url, data)
}
// PostObjUnmarshal Unmarshals results to retObj, likely
// a overridden es.SearchResults struct
func (es *Client) PostObjUnmarshal(url string, dataObj interface{}, retObj interface{}) (int, *ErrorResponse, error) {
data, err := json.Marshal(dataObj)
if err != nil {
es.Log.Error("Error marshaling object to json.", zap.Error(err))
return 0, nil, err
}
code, res, err := es.req(http.MethodPost, url, data)
if err != nil {
return code, &ErrorResponse{Message: string(res)}, err
}
if code != 200 {
return code, &ErrorResponse{Message: string(res)}, err
}
err = json.Unmarshal(res, retObj)
if err != nil {
es.Log.Error("Error unmarshaling result object.", zap.Error(err))
return 0, nil, err
}
return code, nil, err
}
// SendEsMapping
func (es *Client) SendEsMapping(mapping IndexTemplate) (int, Result, *ErrorResponse, error) {
es.Log.Info("Sending template",
zap.String("type", "SendEsMapping"),
zap.String("mapping", mapping.Name),
)
code, esResult, errorResonse, err := es.PutObj(fmt.Sprintf("_template/%s", mapping.Name), mapping.Template)
if err != nil {
es.Log.Error("SendEsMapping got error sending template", zap.Error(err))
return code, esResult, errorResonse, err
}
if code != 200 {
return code, esResult, errorResonse, errors.New("Error setting up " + mapping.Name + " template, got code " + string(code))
}
return code, esResult, nil, err
}
// reqRes
func (es *Client) reqRes(url string, data []byte, method string) (int, Result, *ErrorResponse, error) {
resObj := Result{}
code, res, err := es.req(method, url, data)
if err != nil {
return code, resObj, nil, err
}
if code != 200 {
return code, resObj, &ErrorResponse{Message: string(res)}, err
}
err = json.Unmarshal(res, &resObj)
if err != nil {
es.Log.Error("reqRes error unmarshaling result object.",
zap.ByteString("result", res),
zap.Error(err),
)
resObj.Error = string(res)
return 0, resObj, nil, err
}
return code, resObj, nil, nil
}
// req
func (es *Client) req(method string, url string, data []byte) (int, []byte, error) {
fq := fmt.Sprintf("%s/%s", es.ElasticServer, url)
req, err := http.NewRequest(method, fq, bytes.NewBuffer(data))
if err != nil {
return 0, nil, err
}
if method == http.MethodPut || method == http.MethodPost {
req.Header.Set("Content-Type", "application/json")
}
resp, err := es.HttpClient.Do(req)
if err != nil {
return 0, nil, err
}
if resp != nil {
defer resp.Body.Close()
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return 0, nil, err
}
return resp.StatusCode, body, nil
}