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

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