add es module

This commit is contained in:
Jay 2018-10-09 00:11:27 +08:00
parent ac8284b7e3
commit e0323e964f
2 changed files with 25 additions and 34 deletions

13
main.go
View File

@ -4,8 +4,6 @@ import (
"encoding/gob"
"errors"
"flag"
"fmt"
"git.trj.tw/golang/mtfosbot/module/es"
"log"
"os"
"path"
@ -45,17 +43,6 @@ func main() {
log.Fatal(err)
}
err = es.NewClient()
if err != nil {
log.Fatal(err)
}
err = es.PutLog()
if err != nil {
log.Fatal(err)
}
fmt.Println("run done")
return
// connect to database
db, err := model.NewDB()
if err != nil {

View File

@ -1,30 +1,34 @@
package es
import (
"context"
"fmt"
"git.trj.tw/golang/mtfosbot/module/config"
"github.com/olivere/elastic"
"context"
"fmt"
"git.trj.tw/golang/mtfosbot/module/config"
"github.com/olivere/elastic"
)
var client *elastic.Client
func NewClient () (err error) {
conf := config.GetConf()
client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(conf.Elasticsearch.Host))
fmt.Println("host ", conf.Elasticsearch.Host)
if err != nil {
fmt.Println(err)
return
}
return
// NewClient -
func NewClient() (err error) {
conf := config.GetConf()
client, err = elastic.NewClient(elastic.SetSniff(false), elastic.SetURL(conf.Elasticsearch.Host))
fmt.Println("host ", conf.Elasticsearch.Host)
if err != nil {
fmt.Println(err)
return
}
return
}
func PutLog () (err error) {
conf := config.GetConf()
ctx := context.Background()
_, err = client.Index().Index(conf.Elasticsearch.Index).Type("type").BodyJson(map[string]interface{}{
"key1": "value1",
}).Do(ctx)
return
}
// PutLog -
func PutLog(t string, body map[string]interface{}) (err error) {
if len(t) == 0 || body == nil {
return
}
conf := config.GetConf()
ctx := context.Background()
_, err = client.Index().Index(conf.Elasticsearch.Index).Type(t).BodyJson(body).Do(ctx)
return
}