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" "encoding/gob"
"errors" "errors"
"flag" "flag"
"fmt"
"git.trj.tw/golang/mtfosbot/module/es"
"log" "log"
"os" "os"
"path" "path"
@ -45,17 +43,6 @@ func main() {
log.Fatal(err) 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 // connect to database
db, err := model.NewDB() db, err := model.NewDB()
if err != nil { if err != nil {

View File

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