es配置添加xoath basic认证
This commit is contained in:
parent
17897cc87c
commit
ce21b97dba
@ -29,6 +29,8 @@ type ConfAPI struct {
|
|||||||
}
|
}
|
||||||
type ElasticSearch struct{
|
type ElasticSearch struct{
|
||||||
Address string `yaml:"address"`
|
Address string `yaml:"address"`
|
||||||
|
User string `yaml:"user"`
|
||||||
|
Password string `yaml:"password"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EntityRedis struct {
|
type EntityRedis struct {
|
||||||
|
@ -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{},
|
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 {
|
if reflect.ValueOf(v).Kind() != reflect.Ptr {
|
||||||
return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Ptr")
|
return nil, errors.New(INPUT_TYPE_ERROR + "shoulbe be Ptr")
|
||||||
}
|
}
|
||||||
if reflect.ValueOf(v).Elem().Kind() != reflect.Slice {
|
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 {
|
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()
|
eletype := reflect.ValueOf(v).Elem().Type().Elem()
|
||||||
obj := reflect.ValueOf(v).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())
|
res, err := p.cli.Search(index).Query(query).Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print(err)
|
print(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
id := []string{}
|
id := []string{}
|
||||||
for _, vs := range res.Hits.Hits {
|
for _, vs := range res.Hits.Hits {
|
||||||
id = append(id, vs.Id)
|
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 {
|
} else {
|
||||||
res, err := p.cli.Search(index).Query(query).Size(limit).From(limit * offset).Do(context.Background())
|
res, err := p.cli.Search(index).Query(query).Size(limit).From(limit * offset).Do(context.Background())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
print(err)
|
print(err)
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
id := []string{}
|
id := []string{}
|
||||||
for _, vs := range res.Hits.Hits {
|
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())
|
log.Print(e.Error())
|
||||||
}
|
}
|
||||||
objAdd = append(objAdd, reflect.ValueOf(obj))
|
objAdd = append(objAdd, reflect.ValueOf(obj))
|
||||||
//v = append(v, obj)
|
|
||||||
}
|
}
|
||||||
addOp := reflect.Append(obj, objAdd...)
|
addOp := reflect.Append(obj, objAdd...)
|
||||||
obj.Set(addOp)
|
obj.Set(addOp)
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return nil, errors.New(ERROR_PTR)
|
return nil, errors.New(ERROR_PTR)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,11 +115,12 @@ func Init() {
|
|||||||
func InitELK() {
|
func InitELK() {
|
||||||
var e error
|
var e error
|
||||||
elkconf := config.GetElkConfig()
|
elkconf := config.GetElkConfig()
|
||||||
log.Print(elkconf)
|
|
||||||
gElkEngine.cli, e = elastic.NewClient(
|
gElkEngine.cli, e = elastic.NewClient(
|
||||||
elastic.SetURL(elkconf.Address),
|
elastic.SetURL(elkconf.Address),
|
||||||
// Must turn off sniff in docker
|
// Must turn off sniff in docker
|
||||||
elastic.SetSniff(false))
|
elastic.SetSniff(false),
|
||||||
|
elastic.SetBasicAuth(elkconf.User, elkconf.Password),
|
||||||
|
)
|
||||||
if nil != e {
|
if nil != e {
|
||||||
logs.Error(e.Error())
|
logs.Error(e.Error())
|
||||||
gElkEngine.cli = nil
|
gElkEngine.cli = nil
|
||||||
|
Loading…
Reference in New Issue
Block a user