es配置添加xoath basic认证

master
zcy 2021-02-08 10:16:24 +08:00
parent 17897cc87c
commit ce21b97dba
3 changed files with 28 additions and 11 deletions

View File

@ -29,6 +29,8 @@ type ConfAPI struct {
}
type ElasticSearch struct{
Address string `yaml:"address"`
User string `yaml:"user"`
Password string `yaml:"password"`
}
type EntityRedis struct {

View File

@ -61,16 +61,16 @@ func (p *ElkEngine) Delete(query elastic.Query, index string) error {
}
func (p *ElkEngine) Query(index string, query elastic.Query, v interface{},
limit int, offset int) ([]string, error) {
limit int, offset int) ( []string, error) {
if reflect.ValueOf(v).Kind() != reflect.Ptr {
return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Ptr")
}
if reflect.ValueOf(v).Elem().Kind() != reflect.Slice {
return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Slice")
return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Slice")
}
if reflect.ValueOf(v).Elem().Type().Elem().Kind() != reflect.Struct {
return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Struct")
return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Struct")
}
eletype := reflect.ValueOf(v).Elem().Type().Elem()
obj := reflect.ValueOf(v).Elem()
@ -81,19 +81,34 @@ func (p *ElkEngine) Query(index string, query elastic.Query, v interface{},
res, err := p.cli.Search(index).Query(query).Do(context.Background())
if err != nil {
print(err)
return nil, err
return nil, err
}
id := []string{}
for _, vs := range res.Hits.Hits {
id = append(id, vs.Id)
data, e := vs.Source.MarshalJSON()
if nil != e {
log.Print(e.Error())
}
mapobj := map[string]interface{}{}
e = json.Unmarshal(data, &mapobj)
if nil != e {
log.Print(e.Error())
}
obj, e := utils.UnmarshalJson2StructGen(eletype, mapobj)
log.Print(obj)
if nil != e {
log.Print(e.Error())
}
objAdd = append(objAdd, reflect.ValueOf(obj))
}
return id, nil
return id, nil
} else {
res, err := p.cli.Search(index).Query(query).Size(limit).From(limit * offset).Do(context.Background())
if err != nil {
print(err)
return nil, err
return nil, err
}
id := []string{}
for _, vs := range res.Hits.Hits {
@ -113,14 +128,13 @@ func (p *ElkEngine) Query(index string, query elastic.Query, v interface{},
log.Print(e.Error())
}
objAdd = append(objAdd, reflect.ValueOf(obj))
//v = append(v, obj)
}
addOp := reflect.Append(obj, objAdd...)
obj.Set(addOp)
return id, nil
return id, nil
}
} else {
return nil, errors.New(ERROR_PTR)
return nil, errors.New(ERROR_PTR)
}
}

View File

@ -115,11 +115,12 @@ func Init() {
func InitELK() {
var e error
elkconf := config.GetElkConfig()
log.Print(elkconf)
gElkEngine.cli, e = elastic.NewClient(
elastic.SetURL(elkconf.Address),
// Must turn off sniff in docker
elastic.SetSniff(false))
elastic.SetSniff(false),
elastic.SetBasicAuth(elkconf.User, elkconf.Password),
)
if nil != e {
logs.Error(e.Error())
gElkEngine.cli = nil